xref: /petsc/src/dm/interface/dm.c (revision 1baa6e3354bfe224b33a0c158f482508792a8a6e)
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));
146*1baa6e33SBarry Smith   if (dm->ops->clone) PetscCall((*dm->ops->clone)(dm, newdm));
1473f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1489566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1499566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1509566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1519566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
152be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
153be4c1c3eSMatthew G. Knepley     DM           ncdm;
154be4c1c3eSMatthew G. Knepley     PetscSection cs;
1555a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
156be4c1c3eSMatthew G. Knepley 
1579566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm->coordinateDM, &cs));
1589566063dSJacob Faibussowitsch     if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
1599566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm)));
1605a0206caSToby Isaac     if (pEndMax >= 0) {
1619566063dSJacob Faibussowitsch       PetscCall(DMClone(dm->coordinateDM, &ncdm));
1629566063dSJacob Faibussowitsch       PetscCall(DMCopyDisc(dm->coordinateDM, ncdm));
1639566063dSJacob Faibussowitsch       PetscCall(DMSetLocalSection(ncdm, cs));
1649566063dSJacob Faibussowitsch       PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1659566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&ncdm));
166be4c1c3eSMatthew G. Knepley     }
167be4c1c3eSMatthew G. Knepley   }
1689566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1699566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1709566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
17138221697SMatthew G. Knepley   if (coords) {
1729566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
17338221697SMatthew G. Knepley   } else {
1749566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1759566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
17638221697SMatthew G. Knepley   }
17790b157c4SStefano Zampini   {
17890b157c4SStefano Zampini     PetscBool             isper;
179c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1805dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1819566063dSJacob Faibussowitsch     PetscCall(DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd));
1829566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd));
183c6b900c6SMatthew G. Knepley   }
18434aa8a36SMatthew G. Knepley   {
18534aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18634aa8a36SMatthew G. Knepley 
1879566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
1889566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
18934aa8a36SMatthew G. Knepley   }
19038221697SMatthew G. Knepley   PetscFunctionReturn(0);
19138221697SMatthew G. Knepley }
19238221697SMatthew G. Knepley 
1939a42bb27SBarry Smith /*@C
194564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1959a42bb27SBarry Smith 
196d083f849SBarry Smith    Logically Collective on da
1979a42bb27SBarry Smith 
198147403d9SBarry Smith    Input Parameters:
1999a42bb27SBarry Smith +  da - initial distributed array
200147403d9SBarry Smith -  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2019a42bb27SBarry Smith 
2029a42bb27SBarry Smith    Options Database:
203147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2049a42bb27SBarry Smith 
2059a42bb27SBarry Smith    Level: intermediate
2069a42bb27SBarry Smith 
207db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`
2089a42bb27SBarry Smith @*/
20919fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2109a42bb27SBarry Smith {
2119a42bb27SBarry Smith   PetscFunctionBegin;
2129a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2139566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2149566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype,(char**)&da->vectype));
2159a42bb27SBarry Smith   PetscFunctionReturn(0);
2169a42bb27SBarry Smith }
2179a42bb27SBarry Smith 
218c0dedaeaSBarry Smith /*@C
219c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
220c0dedaeaSBarry Smith 
221d083f849SBarry Smith    Logically Collective on da
222c0dedaeaSBarry Smith 
223c0dedaeaSBarry Smith    Input Parameter:
224c0dedaeaSBarry Smith .  da - initial distributed array
225c0dedaeaSBarry Smith 
226c0dedaeaSBarry Smith    Output Parameter:
227c0dedaeaSBarry Smith .  ctype - the vector type
228c0dedaeaSBarry Smith 
229c0dedaeaSBarry Smith    Level: intermediate
230c0dedaeaSBarry Smith 
231db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
232c0dedaeaSBarry Smith @*/
233c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
234c0dedaeaSBarry Smith {
235c0dedaeaSBarry Smith   PetscFunctionBegin;
236c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
237c0dedaeaSBarry Smith   *ctype = da->vectype;
238c0dedaeaSBarry Smith   PetscFunctionReturn(0);
239c0dedaeaSBarry Smith }
240c0dedaeaSBarry Smith 
2415f1ad066SMatthew G Knepley /*@
24234f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley   Not collective
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   Input Parameter:
2475f1ad066SMatthew G Knepley . v - The Vec
2485f1ad066SMatthew G Knepley 
2495f1ad066SMatthew G Knepley   Output Parameter:
2505f1ad066SMatthew G Knepley . dm - The DM
2515f1ad066SMatthew G Knepley 
2525f1ad066SMatthew G Knepley   Level: intermediate
2535f1ad066SMatthew G Knepley 
254db781477SPatrick Sanan .seealso: `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2555f1ad066SMatthew G Knepley @*/
2565f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2575f1ad066SMatthew G Knepley {
2585f1ad066SMatthew G Knepley   PetscFunctionBegin;
2595f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2605f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2619566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm));
2625f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2635f1ad066SMatthew G Knepley }
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley /*@
266d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2675f1ad066SMatthew G Knepley 
2685f1ad066SMatthew G Knepley   Not collective
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   Input Parameters:
2715f1ad066SMatthew G Knepley + v - The Vec
2725f1ad066SMatthew G Knepley - dm - The DM
2735f1ad066SMatthew G Knepley 
274d9805387SMatthew 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.
275d9805387SMatthew G. Knepley 
2765f1ad066SMatthew G Knepley   Level: intermediate
2775f1ad066SMatthew G Knepley 
278db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2795f1ad066SMatthew G Knepley @*/
2805f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2815f1ad066SMatthew G Knepley {
2825f1ad066SMatthew G Knepley   PetscFunctionBegin;
2835f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
284d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2859566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm));
2865f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2875f1ad066SMatthew G Knepley }
2885f1ad066SMatthew G Knepley 
289521d9a4cSLisandro Dalcin /*@C
2908f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2918f1509bcSBarry Smith 
292d083f849SBarry Smith    Logically Collective on dm
2938f1509bcSBarry Smith 
2948f1509bcSBarry Smith    Input Parameters:
2958f1509bcSBarry Smith +  dm - the DM context
2968f1509bcSBarry Smith -  ctype - the matrix type
2978f1509bcSBarry Smith 
2988f1509bcSBarry Smith    Options Database:
2998f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3008f1509bcSBarry Smith 
3018f1509bcSBarry Smith    Level: intermediate
3028f1509bcSBarry Smith 
303db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
304db781477SPatrick Sanan           `DMGetISColoringType()`
3058f1509bcSBarry Smith @*/
3068f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3078f1509bcSBarry Smith {
3088f1509bcSBarry Smith   PetscFunctionBegin;
3098f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3108f1509bcSBarry Smith   dm->coloringtype = ctype;
3118f1509bcSBarry Smith   PetscFunctionReturn(0);
3128f1509bcSBarry Smith }
3138f1509bcSBarry Smith 
3148f1509bcSBarry Smith /*@C
3158f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
316521d9a4cSLisandro Dalcin 
317d083f849SBarry Smith    Logically Collective on dm
318521d9a4cSLisandro Dalcin 
319521d9a4cSLisandro Dalcin    Input Parameter:
3208f1509bcSBarry Smith .  dm - the DM context
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith    Output Parameter:
3238f1509bcSBarry Smith .  ctype - the matrix type
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith    Options Database:
3268f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3278f1509bcSBarry Smith 
3288f1509bcSBarry Smith    Level: intermediate
3298f1509bcSBarry Smith 
330db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
331db781477SPatrick Sanan           `DMGetISColoringType()`
3328f1509bcSBarry Smith @*/
3338f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3348f1509bcSBarry Smith {
3358f1509bcSBarry Smith   PetscFunctionBegin;
3368f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3378f1509bcSBarry Smith   *ctype = dm->coloringtype;
3388f1509bcSBarry Smith   PetscFunctionReturn(0);
3398f1509bcSBarry Smith }
3408f1509bcSBarry Smith 
3418f1509bcSBarry Smith /*@C
3428f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3438f1509bcSBarry Smith 
344d083f849SBarry Smith    Logically Collective on dm
3458f1509bcSBarry Smith 
3468f1509bcSBarry Smith    Input Parameters:
347521d9a4cSLisandro Dalcin +  dm - the DM context
348a2b5a043SBarry Smith -  ctype - the matrix type
349521d9a4cSLisandro Dalcin 
350521d9a4cSLisandro Dalcin    Options Database:
351147403d9SBarry Smith .   -dm_mat_type ctype - the type of the matrix to create
352521d9a4cSLisandro Dalcin 
353521d9a4cSLisandro Dalcin    Level: intermediate
354521d9a4cSLisandro Dalcin 
355db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`
356521d9a4cSLisandro Dalcin @*/
35719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
358521d9a4cSLisandro Dalcin {
359521d9a4cSLisandro Dalcin   PetscFunctionBegin;
360521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3619566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3629566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype,(char**)&dm->mattype));
363521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
364521d9a4cSLisandro Dalcin }
365521d9a4cSLisandro Dalcin 
366c0dedaeaSBarry Smith /*@C
367c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
368c0dedaeaSBarry Smith 
369d083f849SBarry Smith    Logically Collective on dm
370c0dedaeaSBarry Smith 
371c0dedaeaSBarry Smith    Input Parameter:
372c0dedaeaSBarry Smith .  dm - the DM context
373c0dedaeaSBarry Smith 
374c0dedaeaSBarry Smith    Output Parameter:
375c0dedaeaSBarry Smith .  ctype - the matrix type
376c0dedaeaSBarry Smith 
377c0dedaeaSBarry Smith    Options Database:
378147403d9SBarry Smith .   -dm_mat_type ctype - the matrix type
379c0dedaeaSBarry Smith 
380c0dedaeaSBarry Smith    Level: intermediate
381c0dedaeaSBarry Smith 
382db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
383c0dedaeaSBarry Smith @*/
384c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
385c0dedaeaSBarry Smith {
386c0dedaeaSBarry Smith   PetscFunctionBegin;
387c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
388c0dedaeaSBarry Smith   *ctype = dm->mattype;
389c0dedaeaSBarry Smith   PetscFunctionReturn(0);
390c0dedaeaSBarry Smith }
391c0dedaeaSBarry Smith 
392c688c046SMatthew G Knepley /*@
39334f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
394c688c046SMatthew G Knepley 
395c688c046SMatthew G Knepley   Not collective
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   Input Parameter:
398c688c046SMatthew G Knepley . A - The Mat
399c688c046SMatthew G Knepley 
400c688c046SMatthew G Knepley   Output Parameter:
401c688c046SMatthew G Knepley . dm - The DM
402c688c046SMatthew G Knepley 
403c688c046SMatthew G Knepley   Level: intermediate
404c688c046SMatthew G Knepley 
4058f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4068f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4078f1509bcSBarry Smith 
408db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
409c688c046SMatthew G Knepley @*/
410c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
411c688c046SMatthew G Knepley {
412c688c046SMatthew G Knepley   PetscFunctionBegin;
413c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
414c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
4159566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm));
416c688c046SMatthew G Knepley   PetscFunctionReturn(0);
417c688c046SMatthew G Knepley }
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley /*@
420c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
421c688c046SMatthew G Knepley 
422c688c046SMatthew G Knepley   Not collective
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Input Parameters:
425c688c046SMatthew G Knepley + A - The Mat
426c688c046SMatthew G Knepley - dm - The DM
427c688c046SMatthew G Knepley 
428c688c046SMatthew G Knepley   Level: intermediate
429c688c046SMatthew G Knepley 
4308f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4318f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4328f1509bcSBarry Smith 
433db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
434c688c046SMatthew G Knepley @*/
435c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
436c688c046SMatthew G Knepley {
437c688c046SMatthew G Knepley   PetscFunctionBegin;
438c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4398865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
4409566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm));
441c688c046SMatthew G Knepley   PetscFunctionReturn(0);
442c688c046SMatthew G Knepley }
443c688c046SMatthew G Knepley 
4449a42bb27SBarry Smith /*@C
4459a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4466757b960SDave May    DM options in the database.
4479a42bb27SBarry Smith 
448d083f849SBarry Smith    Logically Collective on dm
4499a42bb27SBarry Smith 
450d8d19677SJose E. Roman    Input Parameters:
4518353ddbbSDave May +  da - the DM context
4529a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4539a42bb27SBarry Smith 
4549a42bb27SBarry Smith    Notes:
4559a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4569a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4579a42bb27SBarry Smith 
4589a42bb27SBarry Smith    Level: advanced
4599a42bb27SBarry Smith 
460db781477SPatrick Sanan .seealso: `DMSetFromOptions()`
4619a42bb27SBarry Smith @*/
4627087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4639a42bb27SBarry Smith {
4649a42bb27SBarry Smith   PetscFunctionBegin;
4659a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4669566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm,prefix));
467*1baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix));
468*1baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix));
4699a42bb27SBarry Smith   PetscFunctionReturn(0);
4709a42bb27SBarry Smith }
4719a42bb27SBarry Smith 
47231697293SDave May /*@C
47331697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47431697293SDave May    DM options in the database.
47531697293SDave May 
476d083f849SBarry Smith    Logically Collective on dm
47731697293SDave May 
47831697293SDave May    Input Parameters:
47931697293SDave May +  dm - the DM context
48031697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48131697293SDave May 
48231697293SDave May    Notes:
48331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48531697293SDave May 
48631697293SDave May    Level: advanced
48731697293SDave May 
488db781477SPatrick Sanan .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`
48931697293SDave May @*/
49031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49131697293SDave May {
49231697293SDave May   PetscFunctionBegin;
49331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4949566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix));
49531697293SDave May   PetscFunctionReturn(0);
49631697293SDave May }
49731697293SDave May 
49831697293SDave May /*@C
49931697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50031697293SDave May    DM options in the database.
50131697293SDave May 
50231697293SDave May    Not Collective
50331697293SDave May 
50431697293SDave May    Input Parameters:
50531697293SDave May .  dm - the DM context
50631697293SDave May 
50731697293SDave May    Output Parameters:
50831697293SDave May .  prefix - pointer to the prefix string used is returned
50931697293SDave May 
51095452b02SPatrick Sanan    Notes:
51195452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51231697293SDave May    sufficient length to hold the prefix.
51331697293SDave May 
51431697293SDave May    Level: advanced
51531697293SDave May 
516db781477SPatrick Sanan .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`
51731697293SDave May @*/
51831697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
51931697293SDave May {
52031697293SDave May   PetscFunctionBegin;
52131697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5229566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm,prefix));
52331697293SDave May   PetscFunctionReturn(0);
52431697293SDave May }
52531697293SDave May 
52688bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
52788bdff64SToby Isaac {
5286eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
52988bdff64SToby Isaac 
53088bdff64SToby Isaac   PetscFunctionBegin;
531aab5bcd8SJed Brown   *ncrefct = 0;
53288bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
53388bdff64SToby Isaac     refct--;
53488bdff64SToby Isaac     if (recurseCoarse) {
53588bdff64SToby Isaac       PetscInt coarseCount;
53688bdff64SToby Isaac 
5379566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount));
53888bdff64SToby Isaac       refct += coarseCount;
53988bdff64SToby Isaac     }
54088bdff64SToby Isaac   }
54188bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
54288bdff64SToby Isaac     refct--;
54388bdff64SToby Isaac     if (recurseFine) {
54488bdff64SToby Isaac       PetscInt fineCount;
54588bdff64SToby Isaac 
5469566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount));
54788bdff64SToby Isaac       refct += fineCount;
54888bdff64SToby Isaac     }
54988bdff64SToby Isaac   }
55088bdff64SToby Isaac   *ncrefct = refct;
55188bdff64SToby Isaac   PetscFunctionReturn(0);
55288bdff64SToby Isaac }
55388bdff64SToby Isaac 
554f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
555354557abSToby Isaac {
5565d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
557354557abSToby Isaac 
558354557abSToby Isaac   PetscFunctionBegin;
559354557abSToby Isaac   /* destroy the labels */
560354557abSToby Isaac   while (next) {
561354557abSToby Isaac     DMLabelLink tmp = next->next;
562354557abSToby Isaac 
5635d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
564ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
5659566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
5669566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
567354557abSToby Isaac     next = tmp;
568354557abSToby Isaac   }
5695d80c0bfSVaclav Hapla   dm->labels = NULL;
570354557abSToby Isaac   PetscFunctionReturn(0);
571354557abSToby Isaac }
572354557abSToby Isaac 
5731fb7b255SJunchao Zhang /*@C
5748472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
57547c6ae99SBarry Smith 
576d083f849SBarry Smith     Collective on dm
57747c6ae99SBarry Smith 
57847c6ae99SBarry Smith     Input Parameter:
57947c6ae99SBarry Smith .   dm - the DM object to destroy
58047c6ae99SBarry Smith 
58147c6ae99SBarry Smith     Level: developer
58247c6ae99SBarry Smith 
583db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
58447c6ae99SBarry Smith 
58547c6ae99SBarry Smith @*/
586fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
58747c6ae99SBarry Smith {
5886eb26441SStefano Zampini   PetscInt       cnt;
589dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith   PetscFunctionBegin;
5926bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5936bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
59487e657c6SBarry Smith 
59588bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
5969566063dSJacob Faibussowitsch   PetscCall(DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt));
59788bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
598ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
5996bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6006bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6016eb26441SStefano Zampini 
6029566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6039566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6046eb26441SStefano Zampini 
605f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6060298fd71SBarry Smith   (*dm)->namedglobal = NULL;
607f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6082348bcf4SPeter Brune     nnext = nlink->next;
6097a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6109566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6119566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6129566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6132348bcf4SPeter Brune   }
614f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6150298fd71SBarry Smith   (*dm)->namedlocal = NULL;
616f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
617f490541aSPeter Brune     nnext = nlink->next;
6187a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6199566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6209566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6219566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
622f490541aSPeter Brune   }
6232348bcf4SPeter Brune 
624b17ce1afSJed Brown   /* Destroy the list of hooks */
625c833c3b5SJed Brown   {
626c833c3b5SJed Brown     DMCoarsenHookLink link,next;
627b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
628b17ce1afSJed Brown       next = link->next;
6299566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
630b17ce1afSJed Brown     }
6310298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
632c833c3b5SJed Brown   }
633c833c3b5SJed Brown   {
634c833c3b5SJed Brown     DMRefineHookLink link,next;
635c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
636c833c3b5SJed Brown       next = link->next;
6379566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
638c833c3b5SJed Brown     }
6390298fd71SBarry Smith     (*dm)->refinehook = NULL;
640c833c3b5SJed Brown   }
641be081cd6SPeter Brune   {
642be081cd6SPeter Brune     DMSubDomainHookLink link,next;
643be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
644be081cd6SPeter Brune       next = link->next;
6459566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
646be081cd6SPeter Brune     }
6470298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
648be081cd6SPeter Brune   }
649baf369e7SPeter Brune   {
650baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
651baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
652baf369e7SPeter Brune       next = link->next;
6539566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
654baf369e7SPeter Brune     }
6550298fd71SBarry Smith     (*dm)->gtolhook = NULL;
656baf369e7SPeter Brune   }
657d4d07f1eSToby Isaac   {
658d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
659d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
660d4d07f1eSToby Isaac       next = link->next;
6619566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
662d4d07f1eSToby Isaac     }
663d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
664d4d07f1eSToby Isaac   }
665aa1993deSMatthew G Knepley   /* Destroy the work arrays */
666aa1993deSMatthew G Knepley   {
667aa1993deSMatthew G Knepley     DMWorkLink link,next;
6687a8be351SBarry Smith     PetscCheck(!(*dm)->workout,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
669aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
670aa1993deSMatthew G Knepley       next = link->next;
6719566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
6729566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
673aa1993deSMatthew G Knepley     }
6740298fd71SBarry Smith     (*dm)->workin = NULL;
675aa1993deSMatthew G Knepley   }
676c58f1c22SToby Isaac   /* destroy the labels */
6779566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
678f4cdcedcSVaclav Hapla   /* destroy the fields */
6799566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
680f4cdcedcSVaclav Hapla   /* destroy the boundaries */
681e6f8dbb6SToby Isaac   {
682e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
683e6f8dbb6SToby Isaac     while (next) {
684e6f8dbb6SToby Isaac       DMBoundary b = next;
685e6f8dbb6SToby Isaac 
686e6f8dbb6SToby Isaac       next = b->next;
6879566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
688e6f8dbb6SToby Isaac     }
689e6f8dbb6SToby Isaac   }
690b17ce1afSJed Brown 
6919566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
6929566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
6939566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
69452536dc3SBarry Smith 
6951a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6969566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
6971a266240SBarry Smith   }
6989566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
6999566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7009566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7019566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
70288ed4aceSMatthew G Knepley 
7039566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7049566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7059566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7069566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7079566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7089566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7099566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
710736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
711736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7129566063dSJacob Faibussowitsch       PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
713736995cdSBlaise Bourdin     }
7149566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) (*dm)->sfMigration));
715736995cdSBlaise Bourdin   }
7169a2a23afSMatthew G. Knepley   {
7179a2a23afSMatthew G. Knepley     Vec     *auxData;
7189a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7199a2a23afSMatthew G. Knepley 
7209566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7229566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7239566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7249566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7259566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7269a2a23afSMatthew G. Knepley   }
72788bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
7289566063dSJacob Faibussowitsch     PetscCall(DMSetFineDM((*dm)->coarseMesh,NULL));
72988bdff64SToby Isaac   }
7306eb26441SStefano Zampini 
7319566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
73288bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
7339566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM((*dm)->fineMesh,NULL));
73488bdff64SToby Isaac   }
7359566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7369566063dSJacob Faibussowitsch   PetscCall(DMFieldDestroy(&(*dm)->coordinateField));
7379566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coordinateDM));
7389566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->coordinates));
7399566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->coordinatesLocal));
7409566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7419566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7429566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->bdtype));
7439566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7449566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7459566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7466636e97aSMatthew G Knepley 
7479566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7489566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
749e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7509566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
751732e2eb9SMatthew G Knepley 
752ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7539566063dSJacob Faibussowitsch     PetscCall((*(*dm)->ops->destroy)(*dm));
754ed3c66a1SDave May   }
7559566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
756f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7579566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7589566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
759f918ec44SMatthew G. Knepley #endif
760435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7619566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
76247c6ae99SBarry Smith   PetscFunctionReturn(0);
76347c6ae99SBarry Smith }
76447c6ae99SBarry Smith 
765d7bf68aeSBarry Smith /*@
766d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
767d7bf68aeSBarry Smith 
768d083f849SBarry Smith     Collective on dm
769d7bf68aeSBarry Smith 
770d7bf68aeSBarry Smith     Input Parameter:
771d7bf68aeSBarry Smith .   dm - the DM object to setup
772d7bf68aeSBarry Smith 
773d7bf68aeSBarry Smith     Level: developer
774d7bf68aeSBarry Smith 
775db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
776d7bf68aeSBarry Smith 
777d7bf68aeSBarry Smith @*/
7787087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
779d7bf68aeSBarry Smith {
780d7bf68aeSBarry Smith   PetscFunctionBegin;
781171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7828387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
783*1baa6e33SBarry Smith   if (dm->ops->setup) PetscCall((*dm->ops->setup)(dm));
7848387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
785d7bf68aeSBarry Smith   PetscFunctionReturn(0);
786d7bf68aeSBarry Smith }
787d7bf68aeSBarry Smith 
788d7bf68aeSBarry Smith /*@
789d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
790d7bf68aeSBarry Smith 
791d083f849SBarry Smith     Collective on dm
792d7bf68aeSBarry Smith 
793d7bf68aeSBarry Smith     Input Parameter:
794d7bf68aeSBarry Smith .   dm - the DM object to set options for
795d7bf68aeSBarry Smith 
796732e2eb9SMatthew G Knepley     Options Database:
7975b8ffe73SMark Adams +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix() and DMCreateMassMatrix(), but do not fill it with zeros
7984164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7994164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
800a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
801a4ea9b21SRichard 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
802732e2eb9SMatthew G Knepley 
8039318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8049318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8059318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
806cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8079318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8089318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8099318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8109318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8119318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8129318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8139318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8149318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8159318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8169318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8179318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8189318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8199318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8209318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8219318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
822bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8239318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8249318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8259318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8269318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
827bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
828d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
829d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
830d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
831d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
832d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
833909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
834909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8359318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8369318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8379318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8389318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8399318fe57SMatthew G. Knepley 
840384a6580SVaclav Hapla     DMPLEX Specific Checks
841384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
842384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
843384a6580SVaclav 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()
844384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
845384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
846384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
847384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
848d7bf68aeSBarry Smith 
84995eb5ee5SVaclav Hapla     Level: intermediate
85095eb5ee5SVaclav Hapla 
851db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
852db781477SPatrick Sanan          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`
853d7bf68aeSBarry Smith 
854d7bf68aeSBarry Smith @*/
8557087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
856d7bf68aeSBarry Smith {
8577781c08eSBarry Smith   char           typeName[256];
858ca266f36SBarry Smith   PetscBool      flg;
859d7bf68aeSBarry Smith 
860d7bf68aeSBarry Smith   PetscFunctionBegin;
861171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86249be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8639566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8649566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
865d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8669566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL));
8679566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg));
868*1baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm,typeName));
8699566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg));
870*1baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm,typeName));
8719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL));
8729566063dSJacob 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));
873*1baa6e33SBarry Smith   if (dm->ops->setfromoptions) PetscCall((*dm->ops->setfromoptions)(PetscOptionsObject,dm));
874f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8759566063dSJacob Faibussowitsch   PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm));
876d0609cedSBarry Smith   PetscOptionsEnd();
877d7bf68aeSBarry Smith   PetscFunctionReturn(0);
878d7bf68aeSBarry Smith }
879d7bf68aeSBarry Smith 
880fc9bc008SSatish Balay /*@C
881fe2efc57SMark    DMViewFromOptions - View from Options
882fe2efc57SMark 
883fe2efc57SMark    Collective on DM
884fe2efc57SMark 
885fe2efc57SMark    Input Parameters:
886fe2efc57SMark +  dm - the DM object
887736c3998SJose E. Roman .  obj - Optional object
888736c3998SJose E. Roman -  name - command line option
889fe2efc57SMark 
890fe2efc57SMark    Level: intermediate
891db781477SPatrick Sanan .seealso: `DM`, `DMView`, `PetscObjectViewFromOptions()`, `DMCreate()`
892fe2efc57SMark @*/
893fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
894fe2efc57SMark {
895fe2efc57SMark   PetscFunctionBegin;
896fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8979566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm,obj,name));
898fe2efc57SMark   PetscFunctionReturn(0);
899fe2efc57SMark }
900fe2efc57SMark 
901fe2efc57SMark /*@C
902224748a4SBarry Smith     DMView - Views a DM
90347c6ae99SBarry Smith 
904d083f849SBarry Smith     Collective on dm
90547c6ae99SBarry Smith 
906d8d19677SJose E. Roman     Input Parameters:
90747c6ae99SBarry Smith +   dm - the DM object to view
90847c6ae99SBarry Smith -   v - the viewer
90947c6ae99SBarry Smith 
910cd7e8a5eSksagiyam     Notes:
911cd7e8a5eSksagiyam     Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
912cd7e8a5eSksagiyam     meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
913cd7e8a5eSksagiyam     before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
914cd7e8a5eSksagiyam 
915224748a4SBarry Smith     Level: beginner
91647c6ae99SBarry Smith 
917db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
91847c6ae99SBarry Smith 
91947c6ae99SBarry Smith @*/
9207087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
92147c6ae99SBarry Smith {
92232c0f0efSBarry Smith   PetscBool         isbinary;
92376a8abe0SBarry Smith   PetscMPIInt       size;
92476a8abe0SBarry Smith   PetscViewerFormat format;
92547c6ae99SBarry Smith 
92647c6ae99SBarry Smith   PetscFunctionBegin;
927171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9283014e516SBarry Smith   if (!v) {
9299566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v));
9303014e516SBarry Smith   }
931b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
93274903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
93374903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
93474903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
93574903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
93674903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
93774903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
93874903a4fSStefano Zampini      in an error here */
93974903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9409566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
941b1b135c8SBarry Smith 
9429566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v,&format));
9439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size));
94476a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9459566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm,v));
9469566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary));
94732c0f0efSBarry Smith   if (isbinary) {
94855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
94932c0f0efSBarry Smith     char     type[256];
95032c0f0efSBarry Smith 
9519566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,&classid,1,PETSC_INT));
9529566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type,((PetscObject)dm)->type_name,256));
9539566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v,type,256,PETSC_CHAR));
95432c0f0efSBarry Smith   }
955*1baa6e33SBarry Smith   if (dm->ops->view) PetscCall((*dm->ops->view)(dm,v));
95647c6ae99SBarry Smith   PetscFunctionReturn(0);
95747c6ae99SBarry Smith }
95847c6ae99SBarry Smith 
95947c6ae99SBarry Smith /*@
9608472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
96147c6ae99SBarry Smith 
962d083f849SBarry Smith     Collective on dm
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith     Input Parameter:
96547c6ae99SBarry Smith .   dm - the DM object
96647c6ae99SBarry Smith 
96747c6ae99SBarry Smith     Output Parameter:
96847c6ae99SBarry Smith .   vec - the global vector
96947c6ae99SBarry Smith 
970073dac72SJed Brown     Level: beginner
97147c6ae99SBarry Smith 
972db781477SPatrick Sanan .seealso `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith @*/
9757087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
97647c6ae99SBarry Smith {
97747c6ae99SBarry Smith   PetscFunctionBegin;
978171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
979b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
9807a8be351SBarry Smith   PetscCheck(dm->ops->createglobalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
9819566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createglobalvector)(dm,vec));
98276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
983c6b011d8SStefano Zampini     DM vdm;
984c6b011d8SStefano Zampini 
9859566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec,&vdm));
9867a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
987c6b011d8SStefano Zampini   }
98847c6ae99SBarry Smith   PetscFunctionReturn(0);
98947c6ae99SBarry Smith }
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith /*@
9928472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith     Not Collective
99547c6ae99SBarry Smith 
99647c6ae99SBarry Smith     Input Parameter:
99747c6ae99SBarry Smith .   dm - the DM object
99847c6ae99SBarry Smith 
99947c6ae99SBarry Smith     Output Parameter:
100047c6ae99SBarry Smith .   vec - the local vector
100147c6ae99SBarry Smith 
1002073dac72SJed Brown     Level: beginner
100347c6ae99SBarry Smith 
1004db781477SPatrick Sanan .seealso `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith @*/
10077087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
100847c6ae99SBarry Smith {
100947c6ae99SBarry Smith   PetscFunctionBegin;
1010171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1011b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
10127a8be351SBarry Smith   PetscCheck(dm->ops->createlocalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
10139566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createlocalvector)(dm,vec));
101476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1015c6b011d8SStefano Zampini     DM vdm;
1016c6b011d8SStefano Zampini 
10179566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec,&vdm));
10187a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1019c6b011d8SStefano Zampini   }
102047c6ae99SBarry Smith   PetscFunctionReturn(0);
102147c6ae99SBarry Smith }
102247c6ae99SBarry Smith 
10231411c6eeSJed Brown /*@
10241411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10251411c6eeSJed Brown 
1026d083f849SBarry Smith    Collective on dm
10271411c6eeSJed Brown 
10281411c6eeSJed Brown    Input Parameter:
10291411c6eeSJed Brown .  dm - the DM that provides the mapping
10301411c6eeSJed Brown 
10311411c6eeSJed Brown    Output Parameter:
10321411c6eeSJed Brown .  ltog - the mapping
10331411c6eeSJed Brown 
10341411c6eeSJed Brown    Level: intermediate
10351411c6eeSJed Brown 
10361411c6eeSJed Brown    Notes:
10371411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10381411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10391411c6eeSJed Brown 
1040db781477SPatrick Sanan .seealso: `DMCreateLocalVector()`
10411411c6eeSJed Brown @*/
10427087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10431411c6eeSJed Brown {
10440be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10451411c6eeSJed Brown 
10461411c6eeSJed Brown   PetscFunctionBegin;
10471411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10481411c6eeSJed Brown   PetscValidPointer(ltog,2);
10491411c6eeSJed Brown   if (!dm->ltogmap) {
105037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
105137d0c07bSMatthew G Knepley 
10529566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
105337d0c07bSMatthew G Knepley     if (section) {
1054a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
105537d0c07bSMatthew G Knepley       PetscInt       *ltog;
1056ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
105737d0c07bSMatthew G Knepley 
10589566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
10599566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
10609566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
10619566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
106237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1063e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
106437d0c07bSMatthew G Knepley 
106537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
10669566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
10679566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
10689566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
10699566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
10701a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10711a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10721a7dc684SMatthew G. Knepley         if (dof) {
10735b8243e1SJed Brown           bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
10741a7dc684SMatthew G. Knepley         }
10755227eafbSStefano Zampini 
1076e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
10775227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1078e6befd46SJed Brown             ltog[l] = off < 0 ? off-c : -(off+c+1);
1079e6befd46SJed Brown             cind++;
1080e6befd46SJed Brown           } else {
10815227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off+1) : off) + c - cind;
1082e6befd46SJed Brown           }
108337d0c07bSMatthew G Knepley         }
108437d0c07bSMatthew G Knepley       }
1085bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10860be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10879566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax));
10880be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10890be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10907591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10917591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1092ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1093ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1094ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1095ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l]/bs : -(-(ltog[l]+1)/bs + 1);
1096ca469d19SJed Brown         }
1097ccf3bd66SMatthew G. Knepley         n /= bs;
1098ccf3bd66SMatthew G. Knepley       }
10999566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
11009566063dSJacob Faibussowitsch       PetscCall(PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap));
110137d0c07bSMatthew G Knepley     } else {
11027a8be351SBarry Smith       PetscCheck(dm->ops->getlocaltoglobalmapping,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
11039566063dSJacob Faibussowitsch       PetscCall((*dm->ops->getlocaltoglobalmapping)(dm));
11041411c6eeSJed Brown     }
110537d0c07bSMatthew G Knepley   }
11061411c6eeSJed Brown   *ltog = dm->ltogmap;
11071411c6eeSJed Brown   PetscFunctionReturn(0);
11081411c6eeSJed Brown }
11091411c6eeSJed Brown 
11101411c6eeSJed Brown /*@
11111411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11121411c6eeSJed Brown 
11131411c6eeSJed Brown    Not Collective
11141411c6eeSJed Brown 
11151411c6eeSJed Brown    Input Parameter:
11161411c6eeSJed Brown .  dm - the DM with block structure
11171411c6eeSJed Brown 
11181411c6eeSJed Brown    Output Parameter:
11191411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11201411c6eeSJed Brown 
11211411c6eeSJed Brown    Level: intermediate
11221411c6eeSJed Brown 
1123db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11241411c6eeSJed Brown @*/
11257087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11261411c6eeSJed Brown {
11271411c6eeSJed Brown   PetscFunctionBegin;
11281411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1129534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11307a8be351SBarry Smith   PetscCheck(dm->bs >= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11311411c6eeSJed Brown   *bs = dm->bs;
11321411c6eeSJed Brown   PetscFunctionReturn(0);
11331411c6eeSJed Brown }
11341411c6eeSJed Brown 
113548eeb7c8SBarry Smith /*@C
11368472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
113747c6ae99SBarry Smith 
1138a5bc1bf3SBarry Smith     Collective on dmc
113947c6ae99SBarry Smith 
1140d8d19677SJose E. Roman     Input Parameters:
1141a5bc1bf3SBarry Smith +   dmc - the DM object
1142a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
114347c6ae99SBarry Smith 
1144d8d19677SJose E. Roman     Output Parameters:
114547c6ae99SBarry Smith +  mat - the interpolation
114647c6ae99SBarry Smith -  vec - the scaling (optional)
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith     Level: developer
114947c6ae99SBarry Smith 
115095452b02SPatrick Sanan     Notes:
115195452b02SPatrick 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
115285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1153d52bd9f3SBarry Smith 
11541f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1155d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
115685afcc9aSBarry Smith 
1157db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
115847c6ae99SBarry Smith 
115947c6ae99SBarry Smith @*/
1160a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
116147c6ae99SBarry Smith {
116247c6ae99SBarry Smith   PetscFunctionBegin;
1163a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1164a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1165c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
11667a8be351SBarry Smith   PetscCheck(dmc->ops->createinterpolation,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
11679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0));
11689566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createinterpolation)(dmc,dmf,mat,vec));
11699566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0));
117047c6ae99SBarry Smith   PetscFunctionReturn(0);
117147c6ae99SBarry Smith }
117247c6ae99SBarry Smith 
11733ad4599aSBarry Smith /*@
1174d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11752ed6491fSPatrick Sanan 
11762ed6491fSPatrick Sanan   Input Parameters:
11772ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11782ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11792ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11802ed6491fSPatrick Sanan 
11812ed6491fSPatrick Sanan   Output Parameter:
11822ed6491fSPatrick Sanan .    scale - the scaled vector
11832ed6491fSPatrick Sanan 
11842ed6491fSPatrick Sanan   Level: developer
11852ed6491fSPatrick Sanan 
1186e9c74fd6SRichard Tran Mills   Developer Notes:
1187e9c74fd6SRichard Tran Mills   If the fine-scale DMDA has the -dm_bind_below option set to true, then DMCreateInterpolationScale() calls MatSetBindingPropagates()
1188e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1189e9c74fd6SRichard Tran Mills 
1190db781477SPatrick Sanan .seealso: `DMCreateInterpolation()`
11912ed6491fSPatrick Sanan 
11922ed6491fSPatrick Sanan @*/
11932ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11942ed6491fSPatrick Sanan {
11952ed6491fSPatrick Sanan   Vec            fine;
11962ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11979704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1198e9c74fd6SRichard Tran Mills   PetscBool      bindingpropagates,isbound;
11999704db99SRichard Tran Mills #endif
12002ed6491fSPatrick Sanan 
12012ed6491fSPatrick Sanan   PetscFunctionBegin;
12029566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf,&fine));
12039566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac,scale));
12049566063dSJacob Faibussowitsch   PetscCall(VecSet(fine,one));
12059704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12069704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12079704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12089704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12099566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine,&bindingpropagates));
1210e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12119566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat,PETSC_TRUE));
12129566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine,&isbound));
12139566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat,isbound));
121483aa49f4SRichard Tran Mills   }
12159704db99SRichard Tran Mills #endif
12169566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat,fine,*scale));
12179566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12189566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12192ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12202ed6491fSPatrick Sanan }
12212ed6491fSPatrick Sanan 
12222ed6491fSPatrick Sanan /*@
12233ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12243ad4599aSBarry Smith 
1225a5bc1bf3SBarry Smith     Collective on dmc
12263ad4599aSBarry Smith 
1227d8d19677SJose E. Roman     Input Parameters:
1228a5bc1bf3SBarry Smith +   dmc - the DM object
1229a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12303ad4599aSBarry Smith 
12313ad4599aSBarry Smith     Output Parameter:
12323ad4599aSBarry Smith .  mat - the restriction
12333ad4599aSBarry Smith 
12343ad4599aSBarry Smith     Level: developer
12353ad4599aSBarry Smith 
123695452b02SPatrick Sanan     Notes:
123795452b02SPatrick 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
12383ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12393ad4599aSBarry Smith 
1240db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12413ad4599aSBarry Smith 
12423ad4599aSBarry Smith @*/
1243a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12443ad4599aSBarry Smith {
12453ad4599aSBarry Smith   PetscFunctionBegin;
1246a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1247a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12485a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
12497a8be351SBarry Smith   PetscCheck(dmc->ops->createrestriction,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
12509566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0));
12519566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createrestriction)(dmc,dmf,mat));
12529566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0));
12533ad4599aSBarry Smith   PetscFunctionReturn(0);
12543ad4599aSBarry Smith }
12553ad4599aSBarry Smith 
125647c6ae99SBarry Smith /*@
12578472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
125847c6ae99SBarry Smith 
1259a5bc1bf3SBarry Smith     Collective on dac
126047c6ae99SBarry Smith 
1261d8d19677SJose E. Roman     Input Parameters:
1262a5bc1bf3SBarry Smith +   dac - the DM object
1263a5bc1bf3SBarry Smith -   daf - the second, finer DM object
126447c6ae99SBarry Smith 
126547c6ae99SBarry Smith     Output Parameter:
12666dbf9973SLawrence Mitchell .   mat - the injection
126747c6ae99SBarry Smith 
126847c6ae99SBarry Smith     Level: developer
126947c6ae99SBarry Smith 
127095452b02SPatrick Sanan    Notes:
127195452b02SPatrick 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
127285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
127385afcc9aSBarry Smith 
1274db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`
127547c6ae99SBarry Smith 
127647c6ae99SBarry Smith @*/
1277a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
127847c6ae99SBarry Smith {
127947c6ae99SBarry Smith   PetscFunctionBegin;
1280a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1281a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12825a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
12837a8be351SBarry Smith   PetscCheck(dac->ops->createinjection,PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
12849566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0));
12859566063dSJacob Faibussowitsch   PetscCall((*dac->ops->createinjection)(dac,daf,mat));
12869566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0));
128747c6ae99SBarry Smith   PetscFunctionReturn(0);
128847c6ae99SBarry Smith }
128947c6ae99SBarry Smith 
1290b412c318SBarry Smith /*@
1291bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1292bd041c0cSMatthew G. Knepley 
1293a5bc1bf3SBarry Smith   Collective on dac
1294bd041c0cSMatthew G. Knepley 
1295d8d19677SJose E. Roman   Input Parameters:
1296b4937a87SMatthew G. Knepley + dmc - the target DM object
1297b4937a87SMatthew G. Knepley - dmf - the source DM object
1298bd041c0cSMatthew G. Knepley 
1299bd041c0cSMatthew G. Knepley   Output Parameter:
1300b4937a87SMatthew G. Knepley . mat - the mass matrix
1301bd041c0cSMatthew G. Knepley 
1302bd041c0cSMatthew G. Knepley   Level: developer
1303bd041c0cSMatthew G. Knepley 
1304db781477SPatrick Sanan .seealso `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1305bd041c0cSMatthew G. Knepley @*/
1306b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1307bd041c0cSMatthew G. Knepley {
1308bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1309b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1310b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13115a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13122c71b3e2SJacob Faibussowitsch   PetscCheck(dmc->ops->createmassmatrix,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dmc)->type_name);
13135b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix,0,0,0,0));
13149566063dSJacob Faibussowitsch   PetscCall((*dmc->ops->createmassmatrix)(dmc, dmf, mat));
13155b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix,0,0,0,0));
1316b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1317b4937a87SMatthew G. Knepley }
1318b4937a87SMatthew G. Knepley 
1319b4937a87SMatthew G. Knepley /*@
1320b4937a87SMatthew G. Knepley   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given DM
1321b4937a87SMatthew G. Knepley 
1322b4937a87SMatthew G. Knepley   Collective on dm
1323b4937a87SMatthew G. Knepley 
1324b4937a87SMatthew G. Knepley   Input Parameter:
1325b4937a87SMatthew G. Knepley . dm - the DM object
1326b4937a87SMatthew G. Knepley 
1327b4937a87SMatthew G. Knepley   Output Parameter:
1328b4937a87SMatthew G. Knepley . lm - the lumped mass matrix
1329b4937a87SMatthew G. Knepley 
1330b4937a87SMatthew G. Knepley   Level: developer
1331b4937a87SMatthew G. Knepley 
1332db781477SPatrick Sanan .seealso `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1333b4937a87SMatthew G. Knepley @*/
1334b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1335b4937a87SMatthew G. Knepley {
1336b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1337b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1338b4937a87SMatthew G. Knepley   PetscValidPointer(lm,2);
13392c71b3e2SJacob Faibussowitsch   PetscCheck(dm->ops->createmassmatrixlumped,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrixLumped",((PetscObject)dm)->type_name);
13409566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createmassmatrixlumped)(dm, lm));
1341bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1342bd041c0cSMatthew G. Knepley }
1343bd041c0cSMatthew G. Knepley 
1344bd041c0cSMatthew G. Knepley /*@
1345b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
134647c6ae99SBarry Smith 
1347d083f849SBarry Smith     Collective on dm
134847c6ae99SBarry Smith 
1349d8d19677SJose E. Roman     Input Parameters:
135047c6ae99SBarry Smith +   dm - the DM object
13515bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
135247c6ae99SBarry Smith 
135347c6ae99SBarry Smith     Output Parameter:
135447c6ae99SBarry Smith .   coloring - the coloring
135547c6ae99SBarry Smith 
1356ec5066bdSBarry Smith     Notes:
1357ec5066bdSBarry 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
1358ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1359ec5066bdSBarry Smith 
1360ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1361ec5066bdSBarry Smith 
136247c6ae99SBarry Smith     Level: developer
136347c6ae99SBarry Smith 
1364db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
136547c6ae99SBarry Smith 
1366aab9d709SJed Brown @*/
1367b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
136847c6ae99SBarry Smith {
136947c6ae99SBarry Smith   PetscFunctionBegin;
1370171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13715a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
13727a8be351SBarry Smith   PetscCheck(dm->ops->getcoloring,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
13739566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getcoloring)(dm,ctype,coloring));
137447c6ae99SBarry Smith   PetscFunctionReturn(0);
137547c6ae99SBarry Smith }
137647c6ae99SBarry Smith 
1377b412c318SBarry Smith /*@
13788472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
137947c6ae99SBarry Smith 
1380d083f849SBarry Smith     Collective on dm
138147c6ae99SBarry Smith 
138247c6ae99SBarry Smith     Input Parameter:
1383b412c318SBarry Smith .   dm - the DM object
138447c6ae99SBarry Smith 
138547c6ae99SBarry Smith     Output Parameter:
138647c6ae99SBarry Smith .   mat - the empty Jacobian
138747c6ae99SBarry Smith 
1388073dac72SJed Brown     Level: beginner
138947c6ae99SBarry Smith 
1390f27dd7c6SMatthew G. Knepley     Options Database Keys:
13915b8ffe73SMark Adams . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix() and DMCreateMassMatrix(), but do not fill it with zeros
1392f27dd7c6SMatthew G. Knepley 
139395452b02SPatrick Sanan     Notes:
139495452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
139594013140SBarry Smith        do not need to do it yourself.
139694013140SBarry Smith 
139794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13987889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
139994013140SBarry Smith 
140094013140SBarry 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
140194013140SBarry Smith        internally by PETSc.
140294013140SBarry Smith 
140394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1404aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
140594013140SBarry Smith 
1406db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
140747c6ae99SBarry Smith 
1408aab9d709SJed Brown @*/
1409b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
141047c6ae99SBarry Smith {
141147c6ae99SBarry Smith   PetscFunctionBegin;
1412171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1413064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
14147a8be351SBarry Smith   PetscCheck(dm->ops->creatematrix,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14159566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14169566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix,0,0,0,0));
14179566063dSJacob Faibussowitsch   PetscCall((*dm->ops->creatematrix)(dm,mat));
141876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1419c6b011d8SStefano Zampini     DM mdm;
1420c6b011d8SStefano Zampini 
14219566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat,&mdm));
14227a8be351SBarry Smith     PetscCheck(mdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix",((PetscObject)dm)->type_name);
1423c6b011d8SStefano Zampini   }
1424e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1425e5e52638SMatthew G. Knepley   if (dm->Nf) {
1426e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1427649ef022SMatthew Knepley     PetscInt     Nf, f;
1428e571a35bSMatthew G. Knepley 
14299566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1430649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1431649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14329566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14339566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14349566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1435649ef022SMatthew Knepley         break;
1436e571a35bSMatthew G. Knepley       }
1437649ef022SMatthew Knepley     }
1438649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1439649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14409566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14419566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
14429566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1443e571a35bSMatthew G. Knepley       }
1444e571a35bSMatthew G. Knepley     }
1445e571a35bSMatthew G. Knepley   }
14469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix,0,0,0,0));
144747c6ae99SBarry Smith   PetscFunctionReturn(0);
144847c6ae99SBarry Smith }
144947c6ae99SBarry Smith 
1450732e2eb9SMatthew G Knepley /*@
1451aa0f6e3cSJed Brown   DMSetMatrixPreallocateSkip - When DMCreateMatrix() is called the matrix sizes and ISLocalToGlobalMapping will be
1452aa0f6e3cSJed Brown   properly set, but the entries will not be preallocated. This is most useful to reduce initialization costs when
1453aa0f6e3cSJed Brown   MatSetPreallocationCOO() and MatSetValuesCOO() will be used.
1454aa0f6e3cSJed Brown 
1455aa0f6e3cSJed Brown   Logically Collective on dm
1456aa0f6e3cSJed Brown 
1457aa0f6e3cSJed Brown   Input Parameters:
1458aa0f6e3cSJed Brown + dm - the DM
1459aa0f6e3cSJed Brown - skip - PETSC_TRUE to skip preallocation
1460aa0f6e3cSJed Brown 
1461aa0f6e3cSJed Brown   Level: developer
1462aa0f6e3cSJed Brown 
1463db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`
1464aa0f6e3cSJed Brown @*/
1465aa0f6e3cSJed Brown PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1466aa0f6e3cSJed Brown {
1467aa0f6e3cSJed Brown   PetscFunctionBegin;
1468aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1469aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1470aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1471aa0f6e3cSJed Brown }
1472aa0f6e3cSJed Brown 
1473aa0f6e3cSJed Brown /*@
1474950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1475732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1476732e2eb9SMatthew G Knepley 
1477d083f849SBarry Smith   Logically Collective on dm
1478732e2eb9SMatthew G Knepley 
1479d8d19677SJose E. Roman   Input Parameters:
1480732e2eb9SMatthew G Knepley + dm - the DM
1481732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1482732e2eb9SMatthew G Knepley 
1483732e2eb9SMatthew G Knepley   Level: developer
1484f27dd7c6SMatthew G. Knepley 
1485f27dd7c6SMatthew G. Knepley   Options Database Keys:
14865b8ffe73SMark Adams . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), DMCreateMassMatrix(), but do not fill it with zeros
1487f27dd7c6SMatthew G. Knepley 
1488db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`
1489732e2eb9SMatthew G Knepley @*/
1490732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1491732e2eb9SMatthew G Knepley {
1492732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1493732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1494732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1495732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1496732e2eb9SMatthew G Knepley }
1497732e2eb9SMatthew G Knepley 
1498b06ff27eSHong Zhang /*@
1499b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1500b06ff27eSHong Zhang     but the array for values will not be allocated.
1501b06ff27eSHong Zhang 
1502d083f849SBarry Smith   Logically Collective on dm
1503b06ff27eSHong Zhang 
1504d8d19677SJose E. Roman   Input Parameters:
1505b06ff27eSHong Zhang + dm - the DM
1506b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1507b06ff27eSHong Zhang 
1508b06ff27eSHong Zhang   Level: developer
1509db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`
1510b06ff27eSHong Zhang @*/
1511b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1512b06ff27eSHong Zhang {
1513b06ff27eSHong Zhang   PetscFunctionBegin;
1514b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1515b06ff27eSHong Zhang   dm->structure_only = only;
1516b06ff27eSHong Zhang   PetscFunctionReturn(0);
1517b06ff27eSHong Zhang }
1518b06ff27eSHong Zhang 
1519a89ea682SMatthew G Knepley /*@C
1520aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1521a89ea682SMatthew G Knepley 
1522a89ea682SMatthew G Knepley   Not Collective
1523a89ea682SMatthew G Knepley 
1524a89ea682SMatthew G Knepley   Input Parameters:
1525a89ea682SMatthew G Knepley + dm - the DM object
1526a5b23f4aSJose E. Roman . count - The minimum size
152769291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1528a89ea682SMatthew G Knepley 
1529a89ea682SMatthew G Knepley   Output Parameter:
1530a89ea682SMatthew G Knepley . array - the work array
1531a89ea682SMatthew G Knepley 
1532a89ea682SMatthew G Knepley   Level: developer
1533a89ea682SMatthew G Knepley 
1534db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreate()`
1535a89ea682SMatthew G Knepley @*/
153669291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1537a89ea682SMatthew G Knepley {
1538aa1993deSMatthew G Knepley   DMWorkLink     link;
153969291d52SBarry Smith   PetscMPIInt    dsize;
1540a89ea682SMatthew G Knepley 
1541a89ea682SMatthew G Knepley   PetscFunctionBegin;
1542a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1543aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1544aa1993deSMatthew G Knepley   if (dm->workin) {
1545aa1993deSMatthew G Knepley     link       = dm->workin;
1546aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1547aa1993deSMatthew G Knepley   } else {
15489566063dSJacob Faibussowitsch     PetscCall(PetscNewLog(dm,&link));
1549a89ea682SMatthew G Knepley   }
15509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype,&dsize));
15515056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
15529566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
15539566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize*count,&link->mem));
1554854ce69bSBarry Smith     link->bytes = dsize*count;
1555aa1993deSMatthew G Knepley   }
1556aa1993deSMatthew G Knepley   link->next   = dm->workout;
1557aa1993deSMatthew G Knepley   dm->workout  = link;
1558cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
155900d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
156000d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
156100d952a4SJed Brown #endif
1562aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1563a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1564a89ea682SMatthew G Knepley }
1565a89ea682SMatthew G Knepley 
1566aa1993deSMatthew G Knepley /*@C
1567aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1568aa1993deSMatthew G Knepley 
1569aa1993deSMatthew G Knepley   Not Collective
1570aa1993deSMatthew G Knepley 
1571aa1993deSMatthew G Knepley   Input Parameters:
1572aa1993deSMatthew G Knepley + dm - the DM object
1573a5b23f4aSJose E. Roman . count - The minimum size
157469291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1575aa1993deSMatthew G Knepley 
1576aa1993deSMatthew G Knepley   Output Parameter:
1577aa1993deSMatthew G Knepley . array - the work array
1578aa1993deSMatthew G Knepley 
1579aa1993deSMatthew G Knepley   Level: developer
1580aa1993deSMatthew G Knepley 
158195452b02SPatrick Sanan   Developer Notes:
158295452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1583147403d9SBarry Smith 
1584db781477SPatrick Sanan .seealso `DMDestroy()`, `DMCreate()`
1585aa1993deSMatthew G Knepley @*/
158669291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1587aa1993deSMatthew G Knepley {
1588aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1589aa1993deSMatthew G Knepley 
1590aa1993deSMatthew G Knepley   PetscFunctionBegin;
1591aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1592aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1593aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1594aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1595aa1993deSMatthew G Knepley       *p           = link->next;
1596aa1993deSMatthew G Knepley       link->next   = dm->workin;
1597aa1993deSMatthew G Knepley       dm->workin   = link;
15980298fd71SBarry Smith       *(void**)mem = NULL;
1599aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1600aa1993deSMatthew G Knepley     }
1601aa1993deSMatthew G Knepley   }
1602aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1603aa1993deSMatthew G Knepley }
1604e7c4fc90SDmitry Karpeev 
16058cda7954SMatthew G. Knepley /*@C
1606147403d9SBarry 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()
16078cda7954SMatthew G. Knepley 
16088cda7954SMatthew G. Knepley   Logically collective on DM
16098cda7954SMatthew G. Knepley 
16108cda7954SMatthew G. Knepley   Input Parameters:
16118cda7954SMatthew G. Knepley + dm     - The DM
16128cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16138cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16148cda7954SMatthew G. Knepley 
1615147403d9SBarry Smith   Calling sequence of nullsp:
1616147403d9SBarry Smith .vb
1617147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1618147403d9SBarry Smith .ve
1619147403d9SBarry Smith +  dm        - The present DM
1620147403d9SBarry Smith .  origField - The field number given above, in the original DM
1621147403d9SBarry Smith .  field     - The field number in dm
1622147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16238cda7954SMatthew G. Knepley 
16248cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16258cda7954SMatthew G. Knepley 
162649762cbcSSatish Balay    Level: intermediate
162749762cbcSSatish Balay 
1628db781477SPatrick Sanan .seealso: `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1629147403d9SBarry Smith @*/
1630147403d9SBarry Smith PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace*))
1631435a35e8SMatthew G Knepley {
1632435a35e8SMatthew G Knepley   PetscFunctionBegin;
1633435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16347a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1635435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1636435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1637435a35e8SMatthew G Knepley }
1638435a35e8SMatthew G Knepley 
16398cda7954SMatthew G. Knepley /*@C
16408cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16418cda7954SMatthew G. Knepley 
16428cda7954SMatthew G. Knepley   Not collective
16438cda7954SMatthew G. Knepley 
16448cda7954SMatthew G. Knepley   Input Parameters:
16458cda7954SMatthew G. Knepley + dm     - The DM
16468cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16478cda7954SMatthew G. Knepley 
16488cda7954SMatthew G. Knepley   Output Parameter:
16498cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16508cda7954SMatthew G. Knepley 
1651147403d9SBarry Smith   Calling sequence of nullsp:
1652147403d9SBarry Smith .vb
1653147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1654147403d9SBarry Smith .ve
1655147403d9SBarry Smith +  dm        - The present DM
1656147403d9SBarry Smith .  origField - The field number given above, in the original DM
1657147403d9SBarry Smith .  field     - The field number in dm
1658147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16598cda7954SMatthew G. Knepley 
16608cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16618cda7954SMatthew G. Knepley 
166249762cbcSSatish Balay    Level: intermediate
166349762cbcSSatish Balay 
1664db781477SPatrick Sanan .seealso: `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1665147403d9SBarry Smith @*/
1666147403d9SBarry Smith PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
16670a50eb56SMatthew G. Knepley {
16680a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16690a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1670f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16717a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
16720a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16730a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16740a50eb56SMatthew G. Knepley }
16750a50eb56SMatthew G. Knepley 
16768cda7954SMatthew G. Knepley /*@C
16778cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16788cda7954SMatthew G. Knepley 
16798cda7954SMatthew G. Knepley   Logically collective on DM
16808cda7954SMatthew G. Knepley 
16818cda7954SMatthew G. Knepley   Input Parameters:
16828cda7954SMatthew G. Knepley + dm     - The DM
16838cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16848cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
16858cda7954SMatthew G. Knepley 
1686147403d9SBarry Smith   Calling sequence of nullsp:
1687147403d9SBarry Smith .vb
1688147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1689147403d9SBarry Smith .ve
1690147403d9SBarry Smith +  dm        - The present DM
1691147403d9SBarry Smith .  origField - The field number given above, in the original DM
1692147403d9SBarry Smith .  field     - The field number in dm
1693147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16948cda7954SMatthew G. Knepley 
16958cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16968cda7954SMatthew G. Knepley 
169749762cbcSSatish Balay    Level: intermediate
169849762cbcSSatish Balay 
1699db781477SPatrick Sanan .seealso: `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1700147403d9SBarry Smith @*/
1701147403d9SBarry Smith PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1702f9d4088aSMatthew G. Knepley {
1703f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1704f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17057a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1706f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1707f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1708f9d4088aSMatthew G. Knepley }
1709f9d4088aSMatthew G. Knepley 
17108cda7954SMatthew G. Knepley /*@C
17118cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
17128cda7954SMatthew G. Knepley 
17138cda7954SMatthew G. Knepley   Not collective
17148cda7954SMatthew G. Knepley 
17158cda7954SMatthew G. Knepley   Input Parameters:
17168cda7954SMatthew G. Knepley + dm     - The DM
17178cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17188cda7954SMatthew G. Knepley 
17198cda7954SMatthew G. Knepley   Output Parameter:
17208cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17218cda7954SMatthew G. Knepley 
1722147403d9SBarry Smith   Calling sequence of nullsp:
1723147403d9SBarry Smith .vb
1724147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1725147403d9SBarry Smith .ve
1726147403d9SBarry Smith +  dm        - The present DM
1727147403d9SBarry Smith .  origField - The field number given above, in the original DM
1728147403d9SBarry Smith .  field     - The field number in dm
1729147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17308cda7954SMatthew G. Knepley 
17318cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17328cda7954SMatthew G. Knepley 
173349762cbcSSatish Balay    Level: intermediate
173449762cbcSSatish Balay 
1735db781477SPatrick Sanan .seealso: `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1736147403d9SBarry Smith @*/
1737147403d9SBarry Smith PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1738f9d4088aSMatthew G. Knepley {
1739f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1740f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1741f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17427a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1743f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1744f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1745f9d4088aSMatthew G. Knepley }
1746f9d4088aSMatthew G. Knepley 
17474f3b5142SJed Brown /*@C
17484d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17494d343eeaSMatthew G Knepley 
17504d343eeaSMatthew G Knepley   Not collective
17514d343eeaSMatthew G Knepley 
17524d343eeaSMatthew G Knepley   Input Parameter:
17534d343eeaSMatthew G Knepley . dm - the DM object
17544d343eeaSMatthew G Knepley 
17554d343eeaSMatthew G Knepley   Output Parameters:
17560298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17570298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17580298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17594d343eeaSMatthew G Knepley 
17604d343eeaSMatthew G Knepley   Level: intermediate
17614d343eeaSMatthew G Knepley 
176221c9b008SJed Brown   Notes:
176321c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
176421c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
176521c9b008SJed Brown   PetscFree().
176621c9b008SJed Brown 
1767db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
17684d343eeaSMatthew G Knepley @*/
176937d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17704d343eeaSMatthew G Knepley {
177137d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17724d343eeaSMatthew G Knepley 
17734d343eeaSMatthew G Knepley   PetscFunctionBegin;
17744d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
177569ca1f37SDmitry Karpeev   if (numFields) {
1776534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
177769ca1f37SDmitry Karpeev     *numFields = 0;
177869ca1f37SDmitry Karpeev   }
177937d0c07bSMatthew G Knepley   if (fieldNames) {
178037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17810298fd71SBarry Smith     *fieldNames = NULL;
178269ca1f37SDmitry Karpeev   }
178369ca1f37SDmitry Karpeev   if (fields) {
178469ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
17850298fd71SBarry Smith     *fields = NULL;
178669ca1f37SDmitry Karpeev   }
17879566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
178837d0c07bSMatthew G Knepley   if (section) {
17893a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
179037d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
179137d0c07bSMatthew G Knepley 
17929566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
17939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
17949566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices));
17959566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
179637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
179737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
17989566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
179937d0c07bSMatthew G Knepley     }
180037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
180137d0c07bSMatthew G Knepley       PetscInt gdof;
180237d0c07bSMatthew G Knepley 
18039566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
180437d0c07bSMatthew G Knepley       if (gdof > 0) {
180537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18063a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
180737d0c07bSMatthew G Knepley 
18089566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18099566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18103a544194SStefano Zampini           fpdof = fdof-fcdof;
18113a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18123a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18133a544194SStefano Zampini             fieldNc[f] = 1;
18143a544194SStefano Zampini           }
18153a544194SStefano Zampini           fieldSizes[f] += fpdof;
181637d0c07bSMatthew G Knepley         }
181737d0c07bSMatthew G Knepley       }
181837d0c07bSMatthew G Knepley     }
181937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
18209566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
182137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
182237d0c07bSMatthew G Knepley     }
182337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
182437d0c07bSMatthew G Knepley       PetscInt gdof, goff;
182537d0c07bSMatthew G Knepley 
18269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
182737d0c07bSMatthew G Knepley       if (gdof > 0) {
18289566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
182937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
183037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
183137d0c07bSMatthew G Knepley 
18329566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18339566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
183437d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
183537d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
183637d0c07bSMatthew G Knepley           }
183737d0c07bSMatthew G Knepley         }
183837d0c07bSMatthew G Knepley       }
183937d0c07bSMatthew G Knepley     }
18408865f1eaSKarl Rupp     if (numFields) *numFields = nF;
184137d0c07bSMatthew G Knepley     if (fieldNames) {
18429566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
184337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
184437d0c07bSMatthew G Knepley         const char *fieldName;
184537d0c07bSMatthew G Knepley 
18469566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
18479566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]));
184837d0c07bSMatthew G Knepley       }
184937d0c07bSMatthew G Knepley     }
185037d0c07bSMatthew G Knepley     if (fields) {
18519566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
185237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18533a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18543a544194SStefano Zampini 
18559566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
18563a544194SStefano Zampini         in[0] = -fieldNc[f];
18573a544194SStefano Zampini         in[1] = fieldNc[f];
18581c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
18593a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18609566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
186137d0c07bSMatthew G Knepley       }
186237d0c07bSMatthew G Knepley     }
18639566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes,fieldNc,fieldIndices));
1864*1baa6e33SBarry Smith   } else if (dm->ops->createfieldis) PetscCall((*dm->ops->createfieldis)(dm, numFields, fieldNames, fields));
18654d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18664d343eeaSMatthew G Knepley }
18674d343eeaSMatthew G Knepley 
186816621825SDmitry Karpeev /*@C
186916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
187016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
187116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1872e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1873e7c4fc90SDmitry Karpeev 
1874e7c4fc90SDmitry Karpeev   Not collective
1875e7c4fc90SDmitry Karpeev 
1876e7c4fc90SDmitry Karpeev   Input Parameter:
1877e7c4fc90SDmitry Karpeev . dm - the DM object
1878e7c4fc90SDmitry Karpeev 
1879e7c4fc90SDmitry Karpeev   Output Parameters:
18800298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18810298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18820298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
18830298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1884e7c4fc90SDmitry Karpeev 
1885e7c4fc90SDmitry Karpeev   Level: intermediate
1886e7c4fc90SDmitry Karpeev 
1887e7c4fc90SDmitry Karpeev   Notes:
1888e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1889e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1890e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1891e7c4fc90SDmitry Karpeev 
1892db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1893e7c4fc90SDmitry Karpeev @*/
189416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1895e7c4fc90SDmitry Karpeev {
1896e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1897e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18988865f1eaSKarl Rupp   if (len) {
1899534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
19008865f1eaSKarl Rupp     *len = 0;
19018865f1eaSKarl Rupp   }
19028865f1eaSKarl Rupp   if (namelist) {
19038865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1904ea78f98cSLisandro Dalcin     *namelist = NULL;
19058865f1eaSKarl Rupp   }
19068865f1eaSKarl Rupp   if (islist) {
19078865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1908ea78f98cSLisandro Dalcin     *islist = NULL;
19098865f1eaSKarl Rupp   }
19108865f1eaSKarl Rupp   if (dmlist) {
19118865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1912ea78f98cSLisandro Dalcin     *dmlist = NULL;
19138865f1eaSKarl Rupp   }
1914f3f0edfdSDmitry Karpeev   /*
1915f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1916f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1917f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1918f3f0edfdSDmitry Karpeev    */
19197a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
192016621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1921435a35e8SMatthew G Knepley     PetscSection section;
1922435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1923435a35e8SMatthew G Knepley 
19249566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
19259566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
1926435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1927f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
19289566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields,namelist));
19299566063dSJacob Faibussowitsch       if (islist)   PetscCall(PetscMalloc1(numFields,islist));
19309566063dSJacob Faibussowitsch       if (dmlist)   PetscCall(PetscMalloc1(numFields,dmlist));
1931435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1932435a35e8SMatthew G Knepley         const char *fieldName;
1933435a35e8SMatthew G Knepley 
19349566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
193503dc3394SMatthew G. Knepley         if (namelist) {
19369566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19379566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char**) &(*namelist)[f]));
1938435a35e8SMatthew G Knepley         }
193903dc3394SMatthew G. Knepley       }
1940435a35e8SMatthew G Knepley     } else {
19419566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
1942e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19430298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1944e7c4fc90SDmitry Karpeev     }
19458865f1eaSKarl Rupp   } else {
19469566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist));
194716621825SDmitry Karpeev   }
194816621825SDmitry Karpeev   PetscFunctionReturn(0);
194916621825SDmitry Karpeev }
195016621825SDmitry Karpeev 
1951564cec59SMatthew G. Knepley /*@
1952435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1953435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1954435a35e8SMatthew G Knepley 
1955435a35e8SMatthew G Knepley   Not collective
1956435a35e8SMatthew G Knepley 
1957435a35e8SMatthew G Knepley   Input Parameters:
19582adcc780SMatthew G. Knepley + dm        - The DM object
19592adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19602adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1961435a35e8SMatthew G Knepley 
1962435a35e8SMatthew G Knepley   Output Parameters:
19632adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19642adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1965435a35e8SMatthew G Knepley 
19665d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19675d3b26e6SMatthew G. Knepley 
1968435a35e8SMatthew G Knepley   Level: intermediate
1969435a35e8SMatthew G Knepley 
1970db781477SPatrick Sanan .seealso `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1971435a35e8SMatthew G Knepley @*/
197237bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1973435a35e8SMatthew G Knepley {
1974435a35e8SMatthew G Knepley   PetscFunctionBegin;
1975435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1976dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields,3);
19778865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19788865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
19797a8be351SBarry Smith   PetscCheck(dm->ops->createsubdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
19809566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createsubdm)(dm, numFields, fields, is, subdm));
1981435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1982435a35e8SMatthew G Knepley }
1983435a35e8SMatthew G Knepley 
19842adcc780SMatthew G. Knepley /*@C
19852adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
19862adcc780SMatthew G. Knepley 
19872adcc780SMatthew G. Knepley   Not collective
19882adcc780SMatthew G. Knepley 
1989d8d19677SJose E. Roman   Input Parameters:
19902adcc780SMatthew G. Knepley + dms - The DM objects
19912adcc780SMatthew G. Knepley - len - The number of DMs
19922adcc780SMatthew G. Knepley 
19932adcc780SMatthew G. Knepley   Output Parameters:
1994a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19952adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19962adcc780SMatthew G. Knepley 
19975d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19985d3b26e6SMatthew G. Knepley 
19992adcc780SMatthew G. Knepley   Level: intermediate
20002adcc780SMatthew G. Knepley 
2001db781477SPatrick Sanan .seealso `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
20022adcc780SMatthew G. Knepley @*/
20032adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
20042adcc780SMatthew G. Knepley {
20052adcc780SMatthew G. Knepley   PetscInt       i;
20062adcc780SMatthew G. Knepley 
20072adcc780SMatthew G. Knepley   PetscFunctionBegin;
20082adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
20092adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
20102adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2011a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
201263a3b9bcSJacob Faibussowitsch   PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, len);
20132adcc780SMatthew G. Knepley   if (len) {
2014b9d85ea2SLisandro Dalcin     DM dm = dms[0];
20157a8be351SBarry Smith     PetscCheck(dm->ops->createsuperdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
20169566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createsuperdm)(dms, len, is, superdm));
20172adcc780SMatthew G. Knepley   }
20182adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20192adcc780SMatthew G. Knepley }
20202adcc780SMatthew G. Knepley 
202116621825SDmitry Karpeev /*@C
20228d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
20238d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
20248d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
20258d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
20268d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
202716621825SDmitry Karpeev 
202816621825SDmitry Karpeev   Not collective
202916621825SDmitry Karpeev 
203016621825SDmitry Karpeev   Input Parameter:
203116621825SDmitry Karpeev . dm - the DM object
203216621825SDmitry Karpeev 
203316621825SDmitry Karpeev   Output Parameters:
20340298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20350298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20360298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20370298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20380298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
203916621825SDmitry Karpeev 
204016621825SDmitry Karpeev   Level: intermediate
204116621825SDmitry Karpeev 
204216621825SDmitry Karpeev   Notes:
204316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
204416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
204516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
204616621825SDmitry Karpeev 
2047db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
204816621825SDmitry Karpeev @*/
20498d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
205016621825SDmitry Karpeev {
2051be081cd6SPeter Brune   DMSubDomainHookLink link;
2052be081cd6SPeter Brune   PetscInt            i,l;
205316621825SDmitry Karpeev 
205416621825SDmitry Karpeev   PetscFunctionBegin;
205516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2056dadcf809SJacob Faibussowitsch   if (len)           {PetscValidIntPointer(len,2);            *len         = 0;}
20570298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20580298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20590298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20600298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2061f3f0edfdSDmitry Karpeev   /*
2062f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2063f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2064f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2065f3f0edfdSDmitry Karpeev    */
20667a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
206716621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
20689566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist));
206914a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2070f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2071be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2072be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
20739566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm,(*dmlist)[i],link->ctx));
2074be081cd6SPeter Brune         }
2075648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2076e7c4fc90SDmitry Karpeev       }
207714a18fd3SPeter Brune     }
207814a18fd3SPeter Brune     if (len) *len = l;
207914a18fd3SPeter Brune   }
2080e30e807fSPeter Brune   PetscFunctionReturn(0);
2081e30e807fSPeter Brune }
2082e30e807fSPeter Brune 
2083e30e807fSPeter Brune /*@C
2084e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2085e30e807fSPeter Brune 
2086e30e807fSPeter Brune   Not collective
2087e30e807fSPeter Brune 
2088e30e807fSPeter Brune   Input Parameters:
2089e30e807fSPeter Brune + dm - the DM object
2090e30e807fSPeter Brune . n  - the number of subdomain scatters
2091e30e807fSPeter Brune - subdms - the local subdomains
2092e30e807fSPeter Brune 
2093e30e807fSPeter Brune   Output Parameters:
20946b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2095e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2096e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2097e30e807fSPeter Brune 
209895452b02SPatrick Sanan   Notes:
209995452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2100e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2101e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2102e30e807fSPeter Brune   solution and residual data.
2103e30e807fSPeter Brune 
2104e30e807fSPeter Brune   Level: developer
2105e30e807fSPeter Brune 
2106db781477SPatrick Sanan .seealso `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2107e30e807fSPeter Brune @*/
2108e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2109e30e807fSPeter Brune {
2110e30e807fSPeter Brune   PetscFunctionBegin;
2111e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2112e30e807fSPeter Brune   PetscValidPointer(subdms,3);
21137a8be351SBarry Smith   PetscCheck(dm->ops->createddscatters,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
21149566063dSJacob Faibussowitsch   PetscCall((*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat));
2115e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2116e7c4fc90SDmitry Karpeev }
2117e7c4fc90SDmitry Karpeev 
211847c6ae99SBarry Smith /*@
211947c6ae99SBarry Smith   DMRefine - Refines a DM object
212047c6ae99SBarry Smith 
2121d083f849SBarry Smith   Collective on dm
212247c6ae99SBarry Smith 
2123d8d19677SJose E. Roman   Input Parameters:
212447c6ae99SBarry Smith + dm   - the DM object
212591d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
212647c6ae99SBarry Smith 
212747c6ae99SBarry Smith   Output Parameter:
21280298fd71SBarry Smith . dmf - the refined DM, or NULL
2129ae0a1c52SMatthew G Knepley 
2130f27dd7c6SMatthew G. Knepley   Options Database Keys:
2131412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2132412e9a14SMatthew G. Knepley 
21330298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
213447c6ae99SBarry Smith 
213547c6ae99SBarry Smith   Level: developer
213647c6ae99SBarry Smith 
2137db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
213847c6ae99SBarry Smith @*/
21397087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
214047c6ae99SBarry Smith {
2141c833c3b5SJed Brown   DMRefineHookLink link;
214247c6ae99SBarry Smith 
214347c6ae99SBarry Smith   PetscFunctionBegin;
2144732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21457a8be351SBarry Smith   PetscCheck(dm->ops->refine,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine,dm,0,0,0));
21479566063dSJacob Faibussowitsch   PetscCall((*dm->ops->refine)(dm,comm,dmf));
21484057135bSMatthew G Knepley   if (*dmf) {
214943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21508865f1eaSKarl Rupp 
21519566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf));
21528865f1eaSKarl Rupp 
2153644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21540598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2155656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21568865f1eaSKarl Rupp 
21579566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf,dm->mattype));
2158c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
2159*1baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm,*dmf,link->ctx));
2160c833c3b5SJed Brown     }
2161c833c3b5SJed Brown   }
21629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine,dm,0,0,0));
2163c833c3b5SJed Brown   PetscFunctionReturn(0);
2164c833c3b5SJed Brown }
2165c833c3b5SJed Brown 
2166bb9467b5SJed Brown /*@C
2167c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2168c833c3b5SJed Brown 
2169c833c3b5SJed Brown    Logically Collective
2170c833c3b5SJed Brown 
21714165533cSJose E. Roman    Input Parameters:
2172c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2173c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2174c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21750298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2176c833c3b5SJed Brown 
2177c833c3b5SJed Brown    Calling sequence of refinehook:
2178c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2179c833c3b5SJed Brown 
2180c833c3b5SJed Brown +  coarse - coarse level DM
2181c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2182c833c3b5SJed Brown -  ctx - optional user-defined function context
2183c833c3b5SJed Brown 
2184c833c3b5SJed Brown    Calling sequence for interphook:
2185c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2186c833c3b5SJed Brown 
2187c833c3b5SJed Brown +  coarse - coarse level DM
2188c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2189c833c3b5SJed Brown .  fine - fine level DM to update
2190c833c3b5SJed Brown -  ctx - optional user-defined function context
2191c833c3b5SJed Brown 
2192c833c3b5SJed Brown    Level: advanced
2193c833c3b5SJed Brown 
2194c833c3b5SJed Brown    Notes:
2195c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2196c833c3b5SJed Brown 
2197c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2198c833c3b5SJed Brown 
2199bb9467b5SJed Brown    This function is currently not available from Fortran.
2200bb9467b5SJed Brown 
2201db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2202c833c3b5SJed Brown @*/
2203c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2204c833c3b5SJed Brown {
2205c833c3b5SJed Brown   DMRefineHookLink link,*p;
2206c833c3b5SJed Brown 
2207c833c3b5SJed Brown   PetscFunctionBegin;
2208c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22093d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
22103d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22113d8e3701SJed Brown   }
22129566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2213c833c3b5SJed Brown   link->refinehook = refinehook;
2214c833c3b5SJed Brown   link->interphook = interphook;
2215c833c3b5SJed Brown   link->ctx        = ctx;
22160298fd71SBarry Smith   link->next       = NULL;
2217c833c3b5SJed Brown   *p               = link;
2218c833c3b5SJed Brown   PetscFunctionReturn(0);
2219c833c3b5SJed Brown }
2220c833c3b5SJed Brown 
22213d8e3701SJed Brown /*@C
22223d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22233d8e3701SJed Brown 
22243d8e3701SJed Brown    Logically Collective
22253d8e3701SJed Brown 
22264165533cSJose E. Roman    Input Parameters:
22273d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22283d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22293d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22303d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22313d8e3701SJed Brown 
22323d8e3701SJed Brown    Level: advanced
22333d8e3701SJed Brown 
22343d8e3701SJed Brown    Notes:
22353d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22363d8e3701SJed Brown 
22373d8e3701SJed Brown    This function is currently not available from Fortran.
22383d8e3701SJed Brown 
2239db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
22403d8e3701SJed Brown @*/
22413d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22423d8e3701SJed Brown {
22433d8e3701SJed Brown   DMRefineHookLink link,*p;
22443d8e3701SJed Brown 
22453d8e3701SJed Brown   PetscFunctionBegin;
22463d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22473d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22483d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22493d8e3701SJed Brown       link = *p;
22503d8e3701SJed Brown       *p = link->next;
22519566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
22523d8e3701SJed Brown       break;
22533d8e3701SJed Brown     }
22543d8e3701SJed Brown   }
22553d8e3701SJed Brown   PetscFunctionReturn(0);
22563d8e3701SJed Brown }
22573d8e3701SJed Brown 
2258c833c3b5SJed Brown /*@
2259c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2260c833c3b5SJed Brown 
2261c833c3b5SJed Brown    Collective if any hooks are
2262c833c3b5SJed Brown 
22634165533cSJose E. Roman    Input Parameters:
2264c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2265e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2266c833c3b5SJed Brown -  fine - finer DM to update
2267c833c3b5SJed Brown 
2268c833c3b5SJed Brown    Level: developer
2269c833c3b5SJed Brown 
2270db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `MatInterpolate()`
2271c833c3b5SJed Brown @*/
2272c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2273c833c3b5SJed Brown {
2274c833c3b5SJed Brown   DMRefineHookLink link;
2275c833c3b5SJed Brown 
2276c833c3b5SJed Brown   PetscFunctionBegin;
2277c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
2278*1baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse,interp,fine,link->ctx));
22794057135bSMatthew G Knepley   }
228047c6ae99SBarry Smith   PetscFunctionReturn(0);
228147c6ae99SBarry Smith }
228247c6ae99SBarry Smith 
2283eb3f98d2SBarry Smith /*@
22841f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
22851f3379b2SToby Isaac 
22861f3379b2SToby Isaac    Collective on DM
22871f3379b2SToby Isaac 
22884165533cSJose E. Roman    Input Parameters:
22891f3379b2SToby Isaac +  coarse - coarse DM
22901f3379b2SToby Isaac .  fine   - fine DM
22911f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
22921f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
22931f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
22941f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
22951f3379b2SToby Isaac 
22964165533cSJose E. Roman    Output Parameter:
22971f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
22981f3379b2SToby Isaac 
22991f3379b2SToby Isaac    Level: developer
23001f3379b2SToby Isaac 
23011f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
23021f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23031f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23041f3379b2SToby Isaac    slope-limiting reconstruction.
23051f3379b2SToby Isaac 
2306db781477SPatrick Sanan .seealso `DMInterpolate()`, `DMCreateInterpolation()`
23071f3379b2SToby Isaac @*/
23081f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
23091f3379b2SToby Isaac {
23101f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
23111f3379b2SToby Isaac 
23121f3379b2SToby Isaac   PetscFunctionBegin;
23131f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23141f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23151f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23161f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23171f3379b2SToby Isaac 
23189566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol));
23191f3379b2SToby Isaac   if (interpsol) {
23209566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
23211f3379b2SToby Isaac   } else if (interp) {
23229566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
232398921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23241f3379b2SToby Isaac   PetscFunctionReturn(0);
23251f3379b2SToby Isaac }
23261f3379b2SToby Isaac 
23271f3379b2SToby Isaac /*@
2328aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2329eb3f98d2SBarry Smith 
2330eb3f98d2SBarry Smith     Not Collective
2331eb3f98d2SBarry Smith 
2332eb3f98d2SBarry Smith     Input Parameter:
2333eb3f98d2SBarry Smith .   dm - the DM object
2334eb3f98d2SBarry Smith 
2335eb3f98d2SBarry Smith     Output Parameter:
2336eb3f98d2SBarry Smith .   level - number of refinements
2337eb3f98d2SBarry Smith 
2338eb3f98d2SBarry Smith     Level: developer
2339eb3f98d2SBarry Smith 
2340db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2341eb3f98d2SBarry Smith 
2342eb3f98d2SBarry Smith @*/
2343eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2344eb3f98d2SBarry Smith {
2345eb3f98d2SBarry Smith   PetscFunctionBegin;
2346eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2347eb3f98d2SBarry Smith   *level = dm->levelup;
2348eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2349eb3f98d2SBarry Smith }
2350eb3f98d2SBarry Smith 
2351fef3a512SBarry Smith /*@
2352aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2353fef3a512SBarry Smith 
2354fef3a512SBarry Smith     Not Collective
2355fef3a512SBarry Smith 
2356d8d19677SJose E. Roman     Input Parameters:
2357fef3a512SBarry Smith +   dm - the DM object
2358fef3a512SBarry Smith -   level - number of refinements
2359fef3a512SBarry Smith 
2360fef3a512SBarry Smith     Level: advanced
2361fef3a512SBarry Smith 
236295452b02SPatrick Sanan     Notes:
236395452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2364fef3a512SBarry Smith 
2365db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2366fef3a512SBarry Smith 
2367fef3a512SBarry Smith @*/
2368fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2369fef3a512SBarry Smith {
2370fef3a512SBarry Smith   PetscFunctionBegin;
2371fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2372fef3a512SBarry Smith   dm->levelup = level;
2373fef3a512SBarry Smith   PetscFunctionReturn(0);
2374fef3a512SBarry Smith }
2375fef3a512SBarry Smith 
2376d410b0cfSMatthew G. Knepley /*@
2377d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2378d410b0cfSMatthew G. Knepley 
2379d410b0cfSMatthew G. Knepley   Collective on dm
2380d410b0cfSMatthew G. Knepley 
2381f1a722f8SMatthew G. Knepley   Input Parameters:
2382d410b0cfSMatthew G. Knepley + dm     - the DM object
2383d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2384d410b0cfSMatthew G. Knepley 
2385d410b0cfSMatthew G. Knepley   Output Parameter:
2386d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2387d410b0cfSMatthew G. Knepley 
2388d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2389d410b0cfSMatthew G. Knepley 
2390d410b0cfSMatthew G. Knepley   Level: developer
2391d410b0cfSMatthew G. Knepley 
2392db781477SPatrick Sanan .seealso `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2393d410b0cfSMatthew G. Knepley @*/
2394d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2395d410b0cfSMatthew G. Knepley {
2396d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2397d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23987a8be351SBarry Smith   PetscCheck(dm->ops->extrude,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
23999566063dSJacob Faibussowitsch   PetscCall((*dm->ops->extrude)(dm, layers, dme));
2400d410b0cfSMatthew G. Knepley   if (*dme) {
2401d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
24029566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme));
2403d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
24049566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2405d410b0cfSMatthew G. Knepley   }
2406d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2407d410b0cfSMatthew G. Knepley }
2408d410b0cfSMatthew G. Knepley 
2409ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2410ca3d3a14SMatthew G. Knepley {
2411ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2412ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2413ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2414ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2415ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2416ca3d3a14SMatthew G. Knepley }
2417ca3d3a14SMatthew G. Knepley 
2418ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2419ca3d3a14SMatthew G. Knepley {
2420ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2421ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2422ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2423ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2424ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2425ca3d3a14SMatthew G. Knepley }
2426ca3d3a14SMatthew G. Knepley 
2427ca3d3a14SMatthew G. Knepley /*@
2428c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2429ca3d3a14SMatthew G. Knepley 
2430ca3d3a14SMatthew G. Knepley   Input Parameter:
2431ca3d3a14SMatthew G. Knepley . dm - The DM
2432ca3d3a14SMatthew G. Knepley 
2433ca3d3a14SMatthew G. Knepley   Output Parameter:
2434ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2435ca3d3a14SMatthew G. Knepley 
2436ca3d3a14SMatthew G. Knepley   Level: developer
2437ca3d3a14SMatthew G. Knepley 
2438db781477SPatrick Sanan .seealso: `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2439ca3d3a14SMatthew G. Knepley @*/
2440ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2441ca3d3a14SMatthew G. Knepley {
2442ca3d3a14SMatthew G. Knepley   Vec            tv;
2443ca3d3a14SMatthew G. Knepley 
2444ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2445ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2446534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
24479566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2448ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2449ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2450ca3d3a14SMatthew G. Knepley }
2451ca3d3a14SMatthew G. Knepley 
2452ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2453ca3d3a14SMatthew G. Knepley {
2454ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2455ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2456ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2457ca3d3a14SMatthew G. Knepley 
2458ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
24599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
24609566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
24619566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
24629566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
24639566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
24649566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
24659566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
24669566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2467ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
24689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2469ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2470ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2471ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
24729566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2473ca3d3a14SMatthew G. Knepley       if (!dof) continue;
24749566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
24759566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2476ca3d3a14SMatthew G. Knepley     }
2477ca3d3a14SMatthew G. Knepley   }
24789566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
24799566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
24809566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2481ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2482ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
24839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2484ca3d3a14SMatthew G. Knepley       if (dof) {
2485ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2486ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2487ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2488ca3d3a14SMatthew G. Knepley 
2489ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
24909566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
24919566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva));
24929566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2493ca3d3a14SMatthew G. Knepley       }
2494ca3d3a14SMatthew G. Knepley     }
2495ca3d3a14SMatthew G. Knepley   }
24969566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2497ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2498ca3d3a14SMatthew G. Knepley }
2499ca3d3a14SMatthew G. Knepley 
2500ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2501ca3d3a14SMatthew G. Knepley {
2502ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2503ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2504ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2505ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2506ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2507ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2508ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
25099566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2510ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2511ca3d3a14SMatthew G. Knepley }
2512ca3d3a14SMatthew G. Knepley 
2513bb9467b5SJed Brown /*@C
2514baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2515baf369e7SPeter Brune 
2516baf369e7SPeter Brune    Logically Collective
2517baf369e7SPeter Brune 
25184165533cSJose E. Roman    Input Parameters:
2519baf369e7SPeter Brune +  dm - the DM
2520baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2521baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25220298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2523baf369e7SPeter Brune 
2524baf369e7SPeter Brune    Calling sequence for beginhook:
2525baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2526baf369e7SPeter Brune 
2527baf369e7SPeter Brune +  dm - global DM
2528baf369e7SPeter Brune .  g - global vector
2529baf369e7SPeter Brune .  mode - mode
2530baf369e7SPeter Brune .  l - local vector
2531baf369e7SPeter Brune -  ctx - optional user-defined function context
2532baf369e7SPeter Brune 
2533baf369e7SPeter Brune    Calling sequence for endhook:
2534ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2535baf369e7SPeter Brune 
2536baf369e7SPeter Brune +  global - global DM
2537baf369e7SPeter Brune -  ctx - optional user-defined function context
2538baf369e7SPeter Brune 
2539baf369e7SPeter Brune    Level: advanced
2540baf369e7SPeter Brune 
2541db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2542baf369e7SPeter Brune @*/
2543baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2544baf369e7SPeter Brune {
2545baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2546baf369e7SPeter Brune 
2547baf369e7SPeter Brune   PetscFunctionBegin;
2548baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2549baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
25509566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2551baf369e7SPeter Brune   link->beginhook = beginhook;
2552baf369e7SPeter Brune   link->endhook   = endhook;
2553baf369e7SPeter Brune   link->ctx       = ctx;
25540298fd71SBarry Smith   link->next      = NULL;
2555baf369e7SPeter Brune   *p              = link;
2556baf369e7SPeter Brune   PetscFunctionReturn(0);
2557baf369e7SPeter Brune }
2558baf369e7SPeter Brune 
25594c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25604c274da1SToby Isaac {
25614c274da1SToby Isaac   Mat cMat;
256279769bd5SJed Brown   Vec cVec, cBias;
25634c274da1SToby Isaac   PetscSection section, cSec;
25644c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25654c274da1SToby Isaac 
25664c274da1SToby Isaac   PetscFunctionBegin;
25674c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25689566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,&cBias));
25694c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25705db9a05bSToby Isaac     PetscInt nRows;
25715db9a05bSToby Isaac 
25729566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
25735db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
25749566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
25759566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
25769566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat,l,cVec));
25779566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec,1.,cBias));
25789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
25794c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25809566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
25814c274da1SToby Isaac       if (dof) {
25824c274da1SToby Isaac         PetscScalar *vals;
25839566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec,cSec,p,&vals));
25849566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES));
25854c274da1SToby Isaac       }
25864c274da1SToby Isaac     }
25879566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
25884c274da1SToby Isaac   }
25894c274da1SToby Isaac   PetscFunctionReturn(0);
25904c274da1SToby Isaac }
25914c274da1SToby Isaac 
259247c6ae99SBarry Smith /*@
259301729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
259401729b5cSPatrick Sanan 
2595d083f849SBarry Smith     Neighbor-wise Collective on dm
259601729b5cSPatrick Sanan 
259701729b5cSPatrick Sanan     Input Parameters:
259801729b5cSPatrick Sanan +   dm - the DM object
259901729b5cSPatrick Sanan .   g - the global vector
260001729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
260101729b5cSPatrick Sanan -   l - the local vector
260201729b5cSPatrick Sanan 
260301729b5cSPatrick Sanan     Notes:
260401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
260501729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
260601729b5cSPatrick Sanan 
260701729b5cSPatrick Sanan     Level: beginner
260801729b5cSPatrick Sanan 
2609db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
261001729b5cSPatrick Sanan 
261101729b5cSPatrick Sanan @*/
261201729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
261301729b5cSPatrick Sanan {
261401729b5cSPatrick Sanan   PetscFunctionBegin;
26159566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm,g,mode,l));
26169566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm,g,mode,l));
261701729b5cSPatrick Sanan   PetscFunctionReturn(0);
261801729b5cSPatrick Sanan }
261901729b5cSPatrick Sanan 
262001729b5cSPatrick Sanan /*@
262147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
262247c6ae99SBarry Smith 
2623d083f849SBarry Smith     Neighbor-wise Collective on dm
262447c6ae99SBarry Smith 
262547c6ae99SBarry Smith     Input Parameters:
262647c6ae99SBarry Smith +   dm - the DM object
262747c6ae99SBarry Smith .   g - the global vector
262847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
262947c6ae99SBarry Smith -   l - the local vector
263047c6ae99SBarry Smith 
263101729b5cSPatrick Sanan     Level: intermediate
263247c6ae99SBarry Smith 
2633db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
263447c6ae99SBarry Smith 
263547c6ae99SBarry Smith @*/
26367087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
263747c6ae99SBarry Smith {
26387128ae9fSMatthew G Knepley   PetscSF                 sf;
2639baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
264047c6ae99SBarry Smith 
264147c6ae99SBarry Smith   PetscFunctionBegin;
2642171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2643baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2644*1baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm,g,mode,l,link->ctx));
2645baf369e7SPeter Brune   }
26469566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
26477128ae9fSMatthew G Knepley   if (sf) {
2648ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2649ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2650d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26517128ae9fSMatthew G Knepley 
26527a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
26539566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
26549566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
26559566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
26569566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
26579566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
26587128ae9fSMatthew G Knepley   } else {
26597a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
26609566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
26617128ae9fSMatthew G Knepley   }
266247c6ae99SBarry Smith   PetscFunctionReturn(0);
266347c6ae99SBarry Smith }
266447c6ae99SBarry Smith 
266547c6ae99SBarry Smith /*@
266647c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
266747c6ae99SBarry Smith 
2668d083f849SBarry Smith     Neighbor-wise Collective on dm
266947c6ae99SBarry Smith 
267047c6ae99SBarry Smith     Input Parameters:
267147c6ae99SBarry Smith +   dm - the DM object
267247c6ae99SBarry Smith .   g - the global vector
267347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
267447c6ae99SBarry Smith -   l - the local vector
267547c6ae99SBarry Smith 
267601729b5cSPatrick Sanan     Level: intermediate
267747c6ae99SBarry Smith 
2678db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
267947c6ae99SBarry Smith 
268047c6ae99SBarry Smith @*/
26817087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
268247c6ae99SBarry Smith {
26837128ae9fSMatthew G Knepley   PetscSF                 sf;
2684ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2685ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2686ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2687baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2688d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
268947c6ae99SBarry Smith 
269047c6ae99SBarry Smith   PetscFunctionBegin;
2691171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26929566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
26939566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
26947128ae9fSMatthew G Knepley   if (sf) {
26957a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
26967128ae9fSMatthew G Knepley 
26979566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
26989566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
26999566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE));
27009566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27019566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27029566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
27037128ae9fSMatthew G Knepley   } else {
27047a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
27059566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
27067128ae9fSMatthew G Knepley   }
27079566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL));
2708baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
27099566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
2710baf369e7SPeter Brune   }
271147c6ae99SBarry Smith   PetscFunctionReturn(0);
271247c6ae99SBarry Smith }
271347c6ae99SBarry Smith 
2714d4d07f1eSToby Isaac /*@C
2715d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2716d4d07f1eSToby Isaac 
2717d4d07f1eSToby Isaac    Logically Collective
2718d4d07f1eSToby Isaac 
27194165533cSJose E. Roman    Input Parameters:
2720d4d07f1eSToby Isaac +  dm - the DM
2721d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2722d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2723d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2724d4d07f1eSToby Isaac 
2725d4d07f1eSToby Isaac    Calling sequence for beginhook:
2726d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2727d4d07f1eSToby Isaac 
2728d4d07f1eSToby Isaac +  dm - global DM
2729d4d07f1eSToby Isaac .  l - local vector
2730d4d07f1eSToby Isaac .  mode - mode
2731d4d07f1eSToby Isaac .  g - global vector
2732d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2733d4d07f1eSToby Isaac 
2734d4d07f1eSToby Isaac    Calling sequence for endhook:
2735d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2736d4d07f1eSToby Isaac 
2737d4d07f1eSToby Isaac +  global - global DM
2738d4d07f1eSToby Isaac .  l - local vector
2739d4d07f1eSToby Isaac .  mode - mode
2740d4d07f1eSToby Isaac .  g - global vector
2741d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2742d4d07f1eSToby Isaac 
2743d4d07f1eSToby Isaac    Level: advanced
2744d4d07f1eSToby Isaac 
2745db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2746d4d07f1eSToby Isaac @*/
2747d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2748d4d07f1eSToby Isaac {
2749d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2750d4d07f1eSToby Isaac 
2751d4d07f1eSToby Isaac   PetscFunctionBegin;
2752d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2753d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
27549566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2755d4d07f1eSToby Isaac   link->beginhook = beginhook;
2756d4d07f1eSToby Isaac   link->endhook   = endhook;
2757d4d07f1eSToby Isaac   link->ctx       = ctx;
2758d4d07f1eSToby Isaac   link->next      = NULL;
2759d4d07f1eSToby Isaac   *p              = link;
2760d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2761d4d07f1eSToby Isaac }
2762d4d07f1eSToby Isaac 
27634c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27644c274da1SToby Isaac {
27654c274da1SToby Isaac   Mat cMat;
27664c274da1SToby Isaac   Vec cVec;
27674c274da1SToby Isaac   PetscSection section, cSec;
27684c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27694c274da1SToby Isaac 
27704c274da1SToby Isaac   PetscFunctionBegin;
27714c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27729566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm,&cSec,&cMat,NULL));
27734c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27745db9a05bSToby Isaac     PetscInt nRows;
27755db9a05bSToby Isaac 
27769566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat,&nRows,NULL));
27775db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
27789566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm,&section));
27799566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat,NULL,&cVec));
27809566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec,&pStart,&pEnd));
27814c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec,p,&dof));
27834c274da1SToby Isaac       if (dof) {
27844c274da1SToby Isaac         PetscInt d;
27854c274da1SToby Isaac         PetscScalar *vals;
27869566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l,section,p,&vals));
27879566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec,cSec,p,vals,mode));
27884c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
27894c274da1SToby Isaac          * we just extracted */
27904c274da1SToby Isaac         for (d = 0; d < dof; d++) {
27914c274da1SToby Isaac           vals[d] = 0.;
27924c274da1SToby Isaac         }
27934c274da1SToby Isaac       }
27944c274da1SToby Isaac     }
27959566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat,cVec,l,l));
27969566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27974c274da1SToby Isaac   }
27984c274da1SToby Isaac   PetscFunctionReturn(0);
27994c274da1SToby Isaac }
280001729b5cSPatrick Sanan /*@
280101729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
280201729b5cSPatrick Sanan 
2803d083f849SBarry Smith     Neighbor-wise Collective on dm
280401729b5cSPatrick Sanan 
280501729b5cSPatrick Sanan     Input Parameters:
280601729b5cSPatrick Sanan +   dm - the DM object
280701729b5cSPatrick Sanan .   l - the local vector
280801729b5cSPatrick 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.
280901729b5cSPatrick Sanan -   g - the global vector
281001729b5cSPatrick Sanan 
281101729b5cSPatrick Sanan     Notes:
281201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
281301729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
281401729b5cSPatrick Sanan 
281501729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
281601729b5cSPatrick 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.
281701729b5cSPatrick Sanan 
281801729b5cSPatrick Sanan     Level: beginner
281901729b5cSPatrick Sanan 
2820db781477SPatrick Sanan .seealso `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
282101729b5cSPatrick Sanan 
282201729b5cSPatrick Sanan @*/
282301729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
282401729b5cSPatrick Sanan {
282501729b5cSPatrick Sanan   PetscFunctionBegin;
28269566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm,l,mode,g));
28279566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm,l,mode,g));
282801729b5cSPatrick Sanan   PetscFunctionReturn(0);
282901729b5cSPatrick Sanan }
28304c274da1SToby Isaac 
283147c6ae99SBarry Smith /*@
283201729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28339a42bb27SBarry Smith 
2834d083f849SBarry Smith     Neighbor-wise Collective on dm
28359a42bb27SBarry Smith 
28369a42bb27SBarry Smith     Input Parameters:
28379a42bb27SBarry Smith +   dm - the DM object
2838f6813fd5SJed Brown .   l - the local vector
28391eb28f2eSBarry 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.
28401eb28f2eSBarry Smith -   g - the global vector
28419a42bb27SBarry Smith 
284295452b02SPatrick Sanan     Notes:
284395452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
284484330215SMatthew 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.
28459a42bb27SBarry Smith 
284601729b5cSPatrick Sanan     Level: intermediate
28479a42bb27SBarry Smith 
2848db781477SPatrick Sanan .seealso `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
28499a42bb27SBarry Smith 
28509a42bb27SBarry Smith @*/
28517087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28529a42bb27SBarry Smith {
28537128ae9fSMatthew G Knepley   PetscSF                 sf;
285484330215SMatthew G. Knepley   PetscSection            s, gs;
2855d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2856ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2857ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2858ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2859fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
2860d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28619a42bb27SBarry Smith 
28629a42bb27SBarry Smith   PetscFunctionBegin;
2863171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2864d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2865*1baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm,l,mode,g,link->ctx));
2866d4d07f1eSToby Isaac   }
28679566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL));
28689566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28699566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
28707128ae9fSMatthew G Knepley   switch (mode) {
28717128ae9fSMatthew G Knepley   case INSERT_VALUES:
28727128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2873304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
287484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28757128ae9fSMatthew G Knepley   case ADD_VALUES:
28767128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2877304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
287884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28797128ae9fSMatthew G Knepley   default:
288063a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
28817128ae9fSMatthew G Knepley   }
2882ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
28839566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
2884ca3d3a14SMatthew G. Knepley     if (transform) {
28859566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
28869566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
28879566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
28889566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
2889fa88e482SJed Brown     } else if (isInsert) {
28909566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
2891fa88e482SJed Brown     } else {
28929566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
2893fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2894ca3d3a14SMatthew G. Knepley     }
2895fa88e482SJed Brown     if (s && isInsert) {
28969566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
2897fa88e482SJed Brown     } else {
28989566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
2899fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2900fa88e482SJed Brown     }
2901ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
29029566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
290384330215SMatthew G. Knepley     } else if (s && isInsert) {
290484330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
290584330215SMatthew G. Knepley 
29069566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
29079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
29089566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
290984330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2910b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
291184330215SMatthew G. Knepley 
29129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
29139566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
29149566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
29159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
29169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
29179566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
2918b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
291903442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
29207a8be351SBarry 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);
2921b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2922b3b16f48SMatthew G. Knepley         if (!gcdof) {
292384330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2924b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2925b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
292684330215SMatthew G. Knepley           const PetscInt *cdofs;
292784330215SMatthew G. Knepley           PetscInt        cind = 0;
292884330215SMatthew G. Knepley 
29299566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
2930b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
293184330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2932b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
293384330215SMatthew G. Knepley           }
29347a8be351SBarry 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);
293584330215SMatthew G. Knepley       }
2936ca3d3a14SMatthew G. Knepley     }
2937fa88e482SJed Brown     if (g_inplace) {
29389566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
2939fa88e482SJed Brown     } else {
29409566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
2941fa88e482SJed Brown     }
2942ca3d3a14SMatthew G. Knepley     if (transform) {
29439566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
29449566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
2945fa88e482SJed Brown     } else if (l_inplace) {
29469566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
2947ca3d3a14SMatthew G. Knepley     } else {
29489566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
2949ca3d3a14SMatthew G. Knepley     }
29507128ae9fSMatthew G Knepley   } else {
29517a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
29529566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
29537128ae9fSMatthew G Knepley   }
29549a42bb27SBarry Smith   PetscFunctionReturn(0);
29559a42bb27SBarry Smith }
29569a42bb27SBarry Smith 
29579a42bb27SBarry Smith /*@
29589a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
295947c6ae99SBarry Smith 
2960d083f849SBarry Smith     Neighbor-wise Collective on dm
296147c6ae99SBarry Smith 
296247c6ae99SBarry Smith     Input Parameters:
296347c6ae99SBarry Smith +   dm - the DM object
2964f6813fd5SJed Brown .   l - the local vector
296547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2966f6813fd5SJed Brown -   g - the global vector
296747c6ae99SBarry Smith 
296801729b5cSPatrick Sanan     Level: intermediate
296947c6ae99SBarry Smith 
2970db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
297147c6ae99SBarry Smith 
297247c6ae99SBarry Smith @*/
29737087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
297447c6ae99SBarry Smith {
29757128ae9fSMatthew G Knepley   PetscSF                 sf;
297684330215SMatthew G. Knepley   PetscSection            s;
2977d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2978ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
297947c6ae99SBarry Smith 
298047c6ae99SBarry Smith   PetscFunctionBegin;
2981171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29829566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
29839566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
29847128ae9fSMatthew G Knepley   switch (mode) {
29857128ae9fSMatthew G Knepley   case INSERT_VALUES:
29867128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
298784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
29887128ae9fSMatthew G Knepley   case ADD_VALUES:
29897128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
299084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29917128ae9fSMatthew G Knepley   default:
299263a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
29937128ae9fSMatthew G Knepley   }
299484330215SMatthew G. Knepley   if (sf && !isInsert) {
2995ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2996ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2997ca3d3a14SMatthew G. Knepley     Vec                tmpl;
299884330215SMatthew G. Knepley 
29999566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3000ca3d3a14SMatthew G. Knepley     if (transform) {
30019566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30029566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3003ca3d3a14SMatthew G. Knepley     } else {
30049566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3005ca3d3a14SMatthew G. Knepley     }
30069566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
30079566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3008ca3d3a14SMatthew G. Knepley     if (transform) {
30099566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30109566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3011ca3d3a14SMatthew G. Knepley     } else {
30129566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3013ca3d3a14SMatthew G. Knepley     }
30149566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
301584330215SMatthew G. Knepley   } else if (s && isInsert) {
30167128ae9fSMatthew G Knepley   } else {
30177a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
30189566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
30197128ae9fSMatthew G Knepley   }
3020d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
30219566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm,g,mode,l,link->ctx));
3022d4d07f1eSToby Isaac   }
302347c6ae99SBarry Smith   PetscFunctionReturn(0);
302447c6ae99SBarry Smith }
302547c6ae99SBarry Smith 
3026f089877aSRichard Tran Mills /*@
3027bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3028bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3029d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3030f089877aSRichard Tran Mills 
3031d083f849SBarry Smith    Neighbor-wise Collective on dm
3032f089877aSRichard Tran Mills 
3033f089877aSRichard Tran Mills    Input Parameters:
3034f089877aSRichard Tran Mills +  dm - the DM object
3035bc0a1609SRichard Tran Mills .  g - the original local vector
3036bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3037f089877aSRichard Tran Mills 
3038bc0a1609SRichard Tran Mills    Output Parameter:
3039bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3040f089877aSRichard Tran Mills 
3041f089877aSRichard Tran Mills    Level: intermediate
3042f089877aSRichard Tran Mills 
3043bc0a1609SRichard Tran Mills    Notes:
3044bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3045bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3046bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3047bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3048bc0a1609SRichard Tran Mills 
3049db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3050f089877aSRichard Tran Mills 
3051f089877aSRichard Tran Mills @*/
3052f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3053f089877aSRichard Tran Mills {
3054f089877aSRichard Tran Mills   PetscFunctionBegin;
3055f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30567a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalbegin,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
30579566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3058f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3059f089877aSRichard Tran Mills }
3060f089877aSRichard Tran Mills 
3061f089877aSRichard Tran Mills /*@
3062bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3063bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3064d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3065f089877aSRichard Tran Mills 
3066d083f849SBarry Smith    Neighbor-wise Collective on dm
3067f089877aSRichard Tran Mills 
3068f089877aSRichard Tran Mills    Input Parameters:
3069bc0a1609SRichard Tran Mills +  da - the DM object
3070bc0a1609SRichard Tran Mills .  g - the original local vector
3071bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3072f089877aSRichard Tran Mills 
3073bc0a1609SRichard Tran Mills    Output Parameter:
3074bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3075f089877aSRichard Tran Mills 
3076f089877aSRichard Tran Mills    Level: intermediate
3077f089877aSRichard Tran Mills 
3078bc0a1609SRichard Tran Mills    Notes:
3079bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3080bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3081bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3082bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3083bc0a1609SRichard Tran Mills 
3084db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3085f089877aSRichard Tran Mills 
3086f089877aSRichard Tran Mills @*/
3087f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3088f089877aSRichard Tran Mills {
3089f089877aSRichard Tran Mills   PetscFunctionBegin;
3090f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30917a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalend,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
30929566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3093f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3094f089877aSRichard Tran Mills }
3095f089877aSRichard Tran Mills 
309647c6ae99SBarry Smith /*@
309747c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
309847c6ae99SBarry Smith 
3099d083f849SBarry Smith     Collective on dm
310047c6ae99SBarry Smith 
3101d8d19677SJose E. Roman     Input Parameters:
310247c6ae99SBarry Smith +   dm - the DM object
310391d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
310447c6ae99SBarry Smith 
310547c6ae99SBarry Smith     Output Parameter:
310647c6ae99SBarry Smith .   dmc - the coarsened DM
310747c6ae99SBarry Smith 
310847c6ae99SBarry Smith     Level: developer
310947c6ae99SBarry Smith 
3110db781477SPatrick Sanan .seealso `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
311147c6ae99SBarry Smith 
311247c6ae99SBarry Smith @*/
31137087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
311447c6ae99SBarry Smith {
3115b17ce1afSJed Brown   DMCoarsenHookLink link;
311647c6ae99SBarry Smith 
311747c6ae99SBarry Smith   PetscFunctionBegin;
3118171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31197a8be351SBarry Smith   PetscCheck(dm->ops->coarsen,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
31209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen,dm,0,0,0));
31219566063dSJacob Faibussowitsch   PetscCall((*dm->ops->coarsen)(dm, comm, dmc));
3122b9d85ea2SLisandro Dalcin   if (*dmc) {
3123a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
31249566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm,*dmc));
312543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31269566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc));
3127644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31280598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3129656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
31309566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc,dm->mattype));
3131b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
31329566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm,*dmc,link->ctx));
3133b17ce1afSJed Brown     }
3134b9d85ea2SLisandro Dalcin   }
31359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen,dm,0,0,0));
31367a8be351SBarry Smith   PetscCheck(*dmc,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3137b17ce1afSJed Brown   PetscFunctionReturn(0);
3138b17ce1afSJed Brown }
3139b17ce1afSJed Brown 
3140bb9467b5SJed Brown /*@C
3141b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3142b17ce1afSJed Brown 
3143b17ce1afSJed Brown    Logically Collective
3144b17ce1afSJed Brown 
31454165533cSJose E. Roman    Input Parameters:
3146b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3147b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3148b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31490298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3150b17ce1afSJed Brown 
3151b17ce1afSJed Brown    Calling sequence of coarsenhook:
3152b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3153b17ce1afSJed Brown 
3154b17ce1afSJed Brown +  fine - fine level DM
3155b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3156b17ce1afSJed Brown -  ctx - optional user-defined function context
3157b17ce1afSJed Brown 
3158b17ce1afSJed Brown    Calling sequence for restricthook:
3159c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3160b17ce1afSJed Brown 
3161b17ce1afSJed Brown +  fine - fine level DM
3162b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3163c833c3b5SJed Brown .  rscale - scaling vector for restriction
3164c833c3b5SJed Brown .  inject - matrix restricting by injection
3165b17ce1afSJed Brown .  coarse - coarse level DM to update
3166b17ce1afSJed Brown -  ctx - optional user-defined function context
3167b17ce1afSJed Brown 
3168b17ce1afSJed Brown    Level: advanced
3169b17ce1afSJed Brown 
3170b17ce1afSJed Brown    Notes:
3171b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3172b17ce1afSJed Brown 
3173b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3174b17ce1afSJed Brown 
3175b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3176b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3177b17ce1afSJed Brown 
3178bb9467b5SJed Brown    This function is currently not available from Fortran.
3179bb9467b5SJed Brown 
3180db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3181b17ce1afSJed Brown @*/
3182b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3183b17ce1afSJed Brown {
3184b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3185b17ce1afSJed Brown 
3186b17ce1afSJed Brown   PetscFunctionBegin;
3187b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
31881e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
31891e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
31901e3d8eccSJed Brown   }
31919566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3192b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3193b17ce1afSJed Brown   link->restricthook = restricthook;
3194b17ce1afSJed Brown   link->ctx          = ctx;
31950298fd71SBarry Smith   link->next         = NULL;
3196b17ce1afSJed Brown   *p                 = link;
3197b17ce1afSJed Brown   PetscFunctionReturn(0);
3198b17ce1afSJed Brown }
3199b17ce1afSJed Brown 
3200dc822a44SJed Brown /*@C
3201dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3202dc822a44SJed Brown 
3203dc822a44SJed Brown    Logically Collective
3204dc822a44SJed Brown 
32054165533cSJose E. Roman    Input Parameters:
3206dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3207dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3208dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3209dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3210dc822a44SJed Brown 
3211dc822a44SJed Brown    Level: advanced
3212dc822a44SJed Brown 
3213dc822a44SJed Brown    Notes:
3214dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3215dc822a44SJed Brown 
3216dc822a44SJed Brown    This function is currently not available from Fortran.
3217dc822a44SJed Brown 
3218db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3219dc822a44SJed Brown @*/
3220dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3221dc822a44SJed Brown {
3222dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3223dc822a44SJed Brown 
3224dc822a44SJed Brown   PetscFunctionBegin;
3225dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3226dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3227dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3228dc822a44SJed Brown       link = *p;
3229dc822a44SJed Brown       *p = link->next;
32309566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3231dc822a44SJed Brown       break;
3232dc822a44SJed Brown     }
3233dc822a44SJed Brown   }
3234dc822a44SJed Brown   PetscFunctionReturn(0);
3235dc822a44SJed Brown }
3236dc822a44SJed Brown 
3237b17ce1afSJed Brown /*@
3238b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3239b17ce1afSJed Brown 
3240b17ce1afSJed Brown    Collective if any hooks are
3241b17ce1afSJed Brown 
32424165533cSJose E. Roman    Input Parameters:
3243b17ce1afSJed Brown +  fine - finer DM to use as a base
3244b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3245e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3246b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3247e91eccc2SStefano Zampini -  coarse - coarser DM to update
3248b17ce1afSJed Brown 
3249b17ce1afSJed Brown    Level: developer
3250b17ce1afSJed Brown 
3251db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
3252b17ce1afSJed Brown @*/
3253b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3254b17ce1afSJed Brown {
3255b17ce1afSJed Brown   DMCoarsenHookLink link;
3256b17ce1afSJed Brown 
3257b17ce1afSJed Brown   PetscFunctionBegin;
3258b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
3259*1baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx));
3260b17ce1afSJed Brown   }
326147c6ae99SBarry Smith   PetscFunctionReturn(0);
326247c6ae99SBarry Smith }
326347c6ae99SBarry Smith 
3264bb9467b5SJed Brown /*@C
3265be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32665dbd56e3SPeter Brune 
3267d083f849SBarry Smith    Logically Collective on global
32685dbd56e3SPeter Brune 
32694165533cSJose E. Roman    Input Parameters:
32705dbd56e3SPeter Brune +  global - global DM
3271ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
32725dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
32730298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
32745dbd56e3SPeter Brune 
3275ec4806b8SPeter Brune    Calling sequence for ddhook:
3276ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3277ec4806b8SPeter Brune 
3278ec4806b8SPeter Brune +  global - global DM
3279ec4806b8SPeter Brune .  block  - block DM
3280ec4806b8SPeter Brune -  ctx - optional user-defined function context
3281ec4806b8SPeter Brune 
32825dbd56e3SPeter Brune    Calling sequence for restricthook:
3283ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
32845dbd56e3SPeter Brune 
32855dbd56e3SPeter Brune +  global - global DM
32865dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
32875dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3288ec4806b8SPeter Brune .  block  - block DM
32895dbd56e3SPeter Brune -  ctx - optional user-defined function context
32905dbd56e3SPeter Brune 
32915dbd56e3SPeter Brune    Level: advanced
32925dbd56e3SPeter Brune 
32935dbd56e3SPeter Brune    Notes:
3294ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
32955dbd56e3SPeter Brune 
32965dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
32975dbd56e3SPeter Brune 
32985dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3299ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33005dbd56e3SPeter Brune 
3301bb9467b5SJed Brown    This function is currently not available from Fortran.
3302bb9467b5SJed Brown 
3303db781477SPatrick Sanan .seealso: `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
33045dbd56e3SPeter Brune @*/
3305be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33065dbd56e3SPeter Brune {
3307be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33085dbd56e3SPeter Brune 
33095dbd56e3SPeter Brune   PetscFunctionBegin;
33105dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3311b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3312b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3313b3a6b972SJed Brown   }
33149566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
33155dbd56e3SPeter Brune   link->restricthook = restricthook;
3316be081cd6SPeter Brune   link->ddhook       = ddhook;
33175dbd56e3SPeter Brune   link->ctx          = ctx;
33180298fd71SBarry Smith   link->next         = NULL;
33195dbd56e3SPeter Brune   *p                 = link;
33205dbd56e3SPeter Brune   PetscFunctionReturn(0);
33215dbd56e3SPeter Brune }
33225dbd56e3SPeter Brune 
3323b3a6b972SJed Brown /*@C
3324b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3325b3a6b972SJed Brown 
3326b3a6b972SJed Brown    Logically Collective
3327b3a6b972SJed Brown 
33284165533cSJose E. Roman    Input Parameters:
3329b3a6b972SJed Brown +  global - global DM
3330b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3331b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3332b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3333b3a6b972SJed Brown 
3334b3a6b972SJed Brown    Level: advanced
3335b3a6b972SJed Brown 
3336b3a6b972SJed Brown    Notes:
3337b3a6b972SJed Brown 
3338b3a6b972SJed Brown    This function is currently not available from Fortran.
3339b3a6b972SJed Brown 
3340db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3341b3a6b972SJed Brown @*/
3342b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3343b3a6b972SJed Brown {
3344b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3345b3a6b972SJed Brown 
3346b3a6b972SJed Brown   PetscFunctionBegin;
3347b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3348b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3349b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3350b3a6b972SJed Brown       link = *p;
3351b3a6b972SJed Brown       *p = link->next;
33529566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3353b3a6b972SJed Brown       break;
3354b3a6b972SJed Brown     }
3355b3a6b972SJed Brown   }
3356b3a6b972SJed Brown   PetscFunctionReturn(0);
3357b3a6b972SJed Brown }
3358b3a6b972SJed Brown 
33595dbd56e3SPeter Brune /*@
3360be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33615dbd56e3SPeter Brune 
33625dbd56e3SPeter Brune    Collective if any hooks are
33635dbd56e3SPeter Brune 
33644165533cSJose E. Roman    Input Parameters:
33655dbd56e3SPeter Brune +  fine - finer DM to use as a base
3366be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3367be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33685dbd56e3SPeter Brune -  coarse - coarer DM to update
33695dbd56e3SPeter Brune 
33705dbd56e3SPeter Brune    Level: developer
33715dbd56e3SPeter Brune 
3372db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
33735dbd56e3SPeter Brune @*/
3374be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
33755dbd56e3SPeter Brune {
3376be081cd6SPeter Brune   DMSubDomainHookLink link;
33775dbd56e3SPeter Brune 
33785dbd56e3SPeter Brune   PetscFunctionBegin;
3379be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
3380*1baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx));
33815dbd56e3SPeter Brune   }
33825dbd56e3SPeter Brune   PetscFunctionReturn(0);
33835dbd56e3SPeter Brune }
33845dbd56e3SPeter Brune 
33855fe1f584SPeter Brune /*@
33866a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
33875fe1f584SPeter Brune 
33885fe1f584SPeter Brune     Not Collective
33895fe1f584SPeter Brune 
33905fe1f584SPeter Brune     Input Parameter:
33915fe1f584SPeter Brune .   dm - the DM object
33925fe1f584SPeter Brune 
33935fe1f584SPeter Brune     Output Parameter:
33946a7d9d85SPeter Brune .   level - number of coarsenings
33955fe1f584SPeter Brune 
33965fe1f584SPeter Brune     Level: developer
33975fe1f584SPeter Brune 
3398db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
33995fe1f584SPeter Brune 
34005fe1f584SPeter Brune @*/
34015fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34025fe1f584SPeter Brune {
34035fe1f584SPeter Brune   PetscFunctionBegin;
34045fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3405b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34065fe1f584SPeter Brune   *level = dm->leveldown;
34075fe1f584SPeter Brune   PetscFunctionReturn(0);
34085fe1f584SPeter Brune }
34095fe1f584SPeter Brune 
34109a64c4a8SMatthew G. Knepley /*@
34119a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34129a64c4a8SMatthew G. Knepley 
34139a64c4a8SMatthew G. Knepley     Not Collective
34149a64c4a8SMatthew G. Knepley 
34159a64c4a8SMatthew G. Knepley     Input Parameters:
34169a64c4a8SMatthew G. Knepley +   dm - the DM object
34179a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34189a64c4a8SMatthew G. Knepley 
34199a64c4a8SMatthew G. Knepley     Level: developer
34209a64c4a8SMatthew G. Knepley 
3421db781477SPatrick Sanan .seealso `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34229a64c4a8SMatthew G. Knepley @*/
34239a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34249a64c4a8SMatthew G. Knepley {
34259a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34269a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34279a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34289a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34299a64c4a8SMatthew G. Knepley }
34309a64c4a8SMatthew G. Knepley 
343147c6ae99SBarry Smith /*@C
343247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
343347c6ae99SBarry Smith 
3434d083f849SBarry Smith     Collective on dm
343547c6ae99SBarry Smith 
3436d8d19677SJose E. Roman     Input Parameters:
343747c6ae99SBarry Smith +   dm - the DM object
343847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
343947c6ae99SBarry Smith 
344047c6ae99SBarry Smith     Output Parameter:
344147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
344247c6ae99SBarry Smith 
344347c6ae99SBarry Smith     Level: developer
344447c6ae99SBarry Smith 
3445db781477SPatrick Sanan .seealso `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
344647c6ae99SBarry Smith 
344747c6ae99SBarry Smith @*/
34487087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
344947c6ae99SBarry Smith {
345047c6ae99SBarry Smith   PetscFunctionBegin;
3451171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34527a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
345347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3454b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
345547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
34569566063dSJacob Faibussowitsch     PetscCall((*dm->ops->refinehierarchy)(dm,nlevels,dmf));
345747c6ae99SBarry Smith   } else if (dm->ops->refine) {
345847c6ae99SBarry Smith     PetscInt i;
345947c6ae99SBarry Smith 
34609566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]));
346147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
34629566063dSJacob Faibussowitsch       PetscCall(DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]));
346347c6ae99SBarry Smith     }
3464ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
346547c6ae99SBarry Smith   PetscFunctionReturn(0);
346647c6ae99SBarry Smith }
346747c6ae99SBarry Smith 
346847c6ae99SBarry Smith /*@C
346947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
347047c6ae99SBarry Smith 
3471d083f849SBarry Smith     Collective on dm
347247c6ae99SBarry Smith 
3473d8d19677SJose E. Roman     Input Parameters:
347447c6ae99SBarry Smith +   dm - the DM object
347547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
347647c6ae99SBarry Smith 
347747c6ae99SBarry Smith     Output Parameter:
347847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
347947c6ae99SBarry Smith 
348047c6ae99SBarry Smith     Level: developer
348147c6ae99SBarry Smith 
3482db781477SPatrick Sanan .seealso `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
348347c6ae99SBarry Smith 
348447c6ae99SBarry Smith @*/
34857087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
348647c6ae99SBarry Smith {
348747c6ae99SBarry Smith   PetscFunctionBegin;
3488171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34897a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
349047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
349147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
349247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
34939566063dSJacob Faibussowitsch     PetscCall((*dm->ops->coarsenhierarchy)(dm, nlevels, dmc));
349447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
349547c6ae99SBarry Smith     PetscInt i;
349647c6ae99SBarry Smith 
34979566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]));
349847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
34999566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]));
350047c6ae99SBarry Smith     }
3501ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
350247c6ae99SBarry Smith   PetscFunctionReturn(0);
350347c6ae99SBarry Smith }
350447c6ae99SBarry Smith 
35051a266240SBarry Smith /*@C
35061a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35071a266240SBarry Smith 
35081a266240SBarry Smith     Not Collective
35091a266240SBarry Smith 
35101a266240SBarry Smith     Input Parameters:
35111a266240SBarry Smith +   dm - the DM object
35121a266240SBarry Smith -   destroy - the destroy function
35131a266240SBarry Smith 
35141a266240SBarry Smith     Level: intermediate
35151a266240SBarry Smith 
3516db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
35171a266240SBarry Smith 
3518f07f9ceaSJed Brown @*/
35191a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35201a266240SBarry Smith {
35211a266240SBarry Smith   PetscFunctionBegin;
3522171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35231a266240SBarry Smith   dm->ctxdestroy = destroy;
35241a266240SBarry Smith   PetscFunctionReturn(0);
35251a266240SBarry Smith }
35261a266240SBarry Smith 
3527b07ff414SBarry Smith /*@
35281b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
352947c6ae99SBarry Smith 
353047c6ae99SBarry Smith     Not Collective
353147c6ae99SBarry Smith 
353247c6ae99SBarry Smith     Input Parameters:
353347c6ae99SBarry Smith +   dm - the DM object
353447c6ae99SBarry Smith -   ctx - the user context
353547c6ae99SBarry Smith 
353647c6ae99SBarry Smith     Level: intermediate
353747c6ae99SBarry Smith 
3538db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
353947c6ae99SBarry Smith 
354047c6ae99SBarry Smith @*/
35411b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
354247c6ae99SBarry Smith {
354347c6ae99SBarry Smith   PetscFunctionBegin;
3544171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
354547c6ae99SBarry Smith   dm->ctx = ctx;
354647c6ae99SBarry Smith   PetscFunctionReturn(0);
354747c6ae99SBarry Smith }
354847c6ae99SBarry Smith 
354947c6ae99SBarry Smith /*@
35501b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
355147c6ae99SBarry Smith 
355247c6ae99SBarry Smith     Not Collective
355347c6ae99SBarry Smith 
355447c6ae99SBarry Smith     Input Parameter:
355547c6ae99SBarry Smith .   dm - the DM object
355647c6ae99SBarry Smith 
355747c6ae99SBarry Smith     Output Parameter:
355847c6ae99SBarry Smith .   ctx - the user context
355947c6ae99SBarry Smith 
356047c6ae99SBarry Smith     Level: intermediate
356147c6ae99SBarry Smith 
3562db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
356347c6ae99SBarry Smith 
356447c6ae99SBarry Smith @*/
35651b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
356647c6ae99SBarry Smith {
356747c6ae99SBarry Smith   PetscFunctionBegin;
3568171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35691b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
357047c6ae99SBarry Smith   PetscFunctionReturn(0);
357147c6ae99SBarry Smith }
357247c6ae99SBarry Smith 
357308da532bSDmitry Karpeev /*@C
3574df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
357508da532bSDmitry Karpeev 
3576d083f849SBarry Smith     Logically Collective on dm
357708da532bSDmitry Karpeev 
3578d8d19677SJose E. Roman     Input Parameters:
357908da532bSDmitry Karpeev +   dm - the DM object
35800298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
358108da532bSDmitry Karpeev 
358208da532bSDmitry Karpeev     Level: intermediate
358308da532bSDmitry Karpeev 
3584db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3585db781477SPatrick Sanan          `DMSetJacobian()`
358608da532bSDmitry Karpeev 
358708da532bSDmitry Karpeev @*/
358808da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
358908da532bSDmitry Karpeev {
359008da532bSDmitry Karpeev   PetscFunctionBegin;
35915a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
359208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
359308da532bSDmitry Karpeev   PetscFunctionReturn(0);
359408da532bSDmitry Karpeev }
359508da532bSDmitry Karpeev 
359608da532bSDmitry Karpeev /*@
359708da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
359808da532bSDmitry Karpeev 
359908da532bSDmitry Karpeev     Not Collective
360008da532bSDmitry Karpeev 
360108da532bSDmitry Karpeev     Input Parameter:
360208da532bSDmitry Karpeev .   dm - the DM object to destroy
360308da532bSDmitry Karpeev 
360408da532bSDmitry Karpeev     Output Parameter:
360508da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
360608da532bSDmitry Karpeev 
360708da532bSDmitry Karpeev     Level: developer
360808da532bSDmitry Karpeev 
3609db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
361008da532bSDmitry Karpeev 
361108da532bSDmitry Karpeev @*/
361208da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
361308da532bSDmitry Karpeev {
361408da532bSDmitry Karpeev   PetscFunctionBegin;
36155a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3616534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
361708da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
361808da532bSDmitry Karpeev   PetscFunctionReturn(0);
361908da532bSDmitry Karpeev }
362008da532bSDmitry Karpeev 
362108da532bSDmitry Karpeev /*@C
362208da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
362308da532bSDmitry Karpeev 
3624d083f849SBarry Smith     Logically Collective on dm
362508da532bSDmitry Karpeev 
3626f899ff85SJose E. Roman     Input Parameter:
3627907376e6SBarry Smith .   dm - the DM object
362808da532bSDmitry Karpeev 
362908da532bSDmitry Karpeev     Output parameters:
363008da532bSDmitry Karpeev +   xl - lower bound
363108da532bSDmitry Karpeev -   xu - upper bound
363208da532bSDmitry Karpeev 
3633907376e6SBarry Smith     Level: advanced
3634907376e6SBarry Smith 
363595452b02SPatrick Sanan     Notes:
363695452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
363708da532bSDmitry Karpeev 
3638db781477SPatrick Sanan .seealso `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
363908da532bSDmitry Karpeev 
364008da532bSDmitry Karpeev @*/
364108da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
364208da532bSDmitry Karpeev {
364308da532bSDmitry Karpeev   PetscFunctionBegin;
36445a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
364508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36465a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
36477a8be351SBarry Smith   PetscCheck(dm->ops->computevariablebounds,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
36489566063dSJacob Faibussowitsch   PetscCall((*dm->ops->computevariablebounds)(dm, xl,xu));
364908da532bSDmitry Karpeev   PetscFunctionReturn(0);
365008da532bSDmitry Karpeev }
365108da532bSDmitry Karpeev 
3652b0ae01b7SPeter Brune /*@
3653b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3654b0ae01b7SPeter Brune 
3655b0ae01b7SPeter Brune     Not Collective
3656b0ae01b7SPeter Brune 
3657b0ae01b7SPeter Brune     Input Parameter:
3658b0ae01b7SPeter Brune .   dm - the DM object
3659b0ae01b7SPeter Brune 
3660b0ae01b7SPeter Brune     Output Parameter:
3661b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3662b0ae01b7SPeter Brune 
3663b0ae01b7SPeter Brune     Level: developer
3664b0ae01b7SPeter Brune 
3665db781477SPatrick Sanan .seealso `DMCreateColoring()`
3666b0ae01b7SPeter Brune 
3667b0ae01b7SPeter Brune @*/
3668b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3669b0ae01b7SPeter Brune {
3670b0ae01b7SPeter Brune   PetscFunctionBegin;
36715a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3672534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3673b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3674b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3675b0ae01b7SPeter Brune }
3676b0ae01b7SPeter Brune 
36773ad4599aSBarry Smith /*@
36783ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
36793ad4599aSBarry Smith 
36803ad4599aSBarry Smith     Not Collective
36813ad4599aSBarry Smith 
36823ad4599aSBarry Smith     Input Parameter:
36833ad4599aSBarry Smith .   dm - the DM object
36843ad4599aSBarry Smith 
36853ad4599aSBarry Smith     Output Parameter:
36863ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
36873ad4599aSBarry Smith 
36883ad4599aSBarry Smith     Level: developer
36893ad4599aSBarry Smith 
3690db781477SPatrick Sanan .seealso `DMCreateRestriction()`
36913ad4599aSBarry Smith 
36923ad4599aSBarry Smith @*/
36933ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
36943ad4599aSBarry Smith {
36953ad4599aSBarry Smith   PetscFunctionBegin;
36965a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3697534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
36983ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
36993ad4599aSBarry Smith   PetscFunctionReturn(0);
37003ad4599aSBarry Smith }
37013ad4599aSBarry Smith 
3702a7058e45SLawrence Mitchell /*@
3703a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3704a7058e45SLawrence Mitchell 
3705a7058e45SLawrence Mitchell     Not Collective
3706a7058e45SLawrence Mitchell 
3707a7058e45SLawrence Mitchell     Input Parameter:
3708a7058e45SLawrence Mitchell .   dm - the DM object
3709a7058e45SLawrence Mitchell 
3710a7058e45SLawrence Mitchell     Output Parameter:
3711a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3712a7058e45SLawrence Mitchell 
3713a7058e45SLawrence Mitchell     Level: developer
3714a7058e45SLawrence Mitchell 
3715db781477SPatrick Sanan .seealso `DMCreateInjection()`
3716a7058e45SLawrence Mitchell 
3717a7058e45SLawrence Mitchell @*/
3718a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3719a7058e45SLawrence Mitchell {
3720a7058e45SLawrence Mitchell   PetscFunctionBegin;
37215a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3722534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37235a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37249566063dSJacob Faibussowitsch     PetscCall((*dm->ops->hascreateinjection)(dm,flg));
37255a84ad33SLisandro Dalcin   } else {
37265a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37275a84ad33SLisandro Dalcin   }
3728a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3729a7058e45SLawrence Mitchell }
3730a7058e45SLawrence Mitchell 
37310298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3732264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3733264ace61SBarry Smith 
3734264ace61SBarry Smith /*@C
3735264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3736264ace61SBarry Smith 
3737d083f849SBarry Smith   Collective on dm
3738264ace61SBarry Smith 
3739264ace61SBarry Smith   Input Parameters:
3740264ace61SBarry Smith + dm     - The DM object
3741264ace61SBarry Smith - method - The name of the DM type
3742264ace61SBarry Smith 
3743264ace61SBarry Smith   Options Database Key:
3744264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3745264ace61SBarry Smith 
3746264ace61SBarry Smith   Notes:
3747e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3748264ace61SBarry Smith 
3749264ace61SBarry Smith   Level: intermediate
3750264ace61SBarry Smith 
3751db781477SPatrick Sanan .seealso: `DMGetType()`, `DMCreate()`
3752264ace61SBarry Smith @*/
375319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3754264ace61SBarry Smith {
3755264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3756264ace61SBarry Smith   PetscBool      match;
3757264ace61SBarry Smith 
3758264ace61SBarry Smith   PetscFunctionBegin;
3759264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
37609566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, method, &match));
3761264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3762264ace61SBarry Smith 
37639566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
37649566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList,method,&r));
37657a8be351SBarry Smith   PetscCheck(r,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3766264ace61SBarry Smith 
3767*1baa6e33SBarry Smith   if (dm->ops->destroy) PetscCall((*dm->ops->destroy)(dm));
37689566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops,sizeof(*dm->ops)));
37699566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm,method));
37709566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3771264ace61SBarry Smith   PetscFunctionReturn(0);
3772264ace61SBarry Smith }
3773264ace61SBarry Smith 
3774264ace61SBarry Smith /*@C
3775264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3776264ace61SBarry Smith 
3777264ace61SBarry Smith   Not Collective
3778264ace61SBarry Smith 
3779264ace61SBarry Smith   Input Parameter:
3780264ace61SBarry Smith . dm  - The DM
3781264ace61SBarry Smith 
3782264ace61SBarry Smith   Output Parameter:
3783264ace61SBarry Smith . type - The DM type name
3784264ace61SBarry Smith 
3785264ace61SBarry Smith   Level: intermediate
3786264ace61SBarry Smith 
3787db781477SPatrick Sanan .seealso: `DMSetType()`, `DMCreate()`
3788264ace61SBarry Smith @*/
378919fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3790264ace61SBarry Smith {
3791264ace61SBarry Smith   PetscFunctionBegin;
3792264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3793c959eef4SJed Brown   PetscValidPointer(type,2);
37949566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3795264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3796264ace61SBarry Smith   PetscFunctionReturn(0);
3797264ace61SBarry Smith }
3798264ace61SBarry Smith 
379967a56275SMatthew G Knepley /*@C
380067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
380167a56275SMatthew G Knepley 
3802d083f849SBarry Smith   Collective on dm
380367a56275SMatthew G Knepley 
380467a56275SMatthew G Knepley   Input Parameters:
380567a56275SMatthew G Knepley + dm - the DM
380667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
380767a56275SMatthew G Knepley 
380867a56275SMatthew G Knepley   Output Parameter:
380967a56275SMatthew G Knepley . M - pointer to new DM
381067a56275SMatthew G Knepley 
381167a56275SMatthew G Knepley   Notes:
381267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
381367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
381467a56275SMatthew G Knepley   of the input DM.
381567a56275SMatthew G Knepley 
381667a56275SMatthew G Knepley   Level: intermediate
381767a56275SMatthew G Knepley 
3818db781477SPatrick Sanan .seealso: `DMCreate()`
381967a56275SMatthew G Knepley @*/
382019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
382167a56275SMatthew G Knepley {
382267a56275SMatthew G Knepley   DM             B;
382367a56275SMatthew G Knepley   char           convname[256];
3824c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
382567a56275SMatthew G Knepley 
382667a56275SMatthew G Knepley   PetscFunctionBegin;
382767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
382867a56275SMatthew G Knepley   PetscValidType(dm,1);
382967a56275SMatthew G Knepley   PetscValidPointer(M,3);
38309566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype));
38319566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3832c067b6caSMatthew G. Knepley   if (sametype) {
3833c067b6caSMatthew G. Knepley     *M   = dm;
38349566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) dm));
3835c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3836c067b6caSMatthew G. Knepley   } else {
38370298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
383867a56275SMatthew G Knepley 
383967a56275SMatthew G Knepley     /*
384067a56275SMatthew G Knepley        Order of precedence:
384167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
384267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
384367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
384467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
384567a56275SMatthew G Knepley        5) Use a really basic converter.
384667a56275SMatthew G Knepley     */
384767a56275SMatthew G Knepley 
384867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
38499566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
38509566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
38519566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
38529566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
38539566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
38549566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm,convname,&conv));
385567a56275SMatthew G Knepley     if (conv) goto foundconv;
385667a56275SMatthew G Knepley 
385767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
38589566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
38599566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
38609566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
38619566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
38629566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_",sizeof(convname)));
38639566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,newtype,sizeof(convname)));
38649566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname,"_C",sizeof(convname)));
38659566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B,convname,&conv));
386667a56275SMatthew G Knepley     if (conv) {
38679566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
386867a56275SMatthew G Knepley       goto foundconv;
386967a56275SMatthew G Knepley     }
387067a56275SMatthew G Knepley 
387167a56275SMatthew G Knepley #if 0
387267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
387367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
38749566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
387567a56275SMatthew G Knepley     if (conv) goto foundconv;
387667a56275SMatthew G Knepley 
387767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
387867a56275SMatthew G Knepley     if (dm->ops->convert) {
387967a56275SMatthew G Knepley       conv = dm->ops->convert;
388067a56275SMatthew G Knepley     }
388167a56275SMatthew G Knepley     if (conv) goto foundconv;
388267a56275SMatthew G Knepley #endif
388367a56275SMatthew G Knepley 
388467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
388598921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
388667a56275SMatthew G Knepley 
388767a56275SMatthew G Knepley foundconv:
38889566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert,dm,0,0,0));
38899566063dSJacob Faibussowitsch     PetscCall((*conv)(dm,newtype,M));
389012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
389190b157c4SStefano Zampini     {
389290b157c4SStefano Zampini       PetscBool             isper;
389312fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
389412fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
38959566063dSJacob Faibussowitsch       PetscCall(DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd));
38969566063dSJacob Faibussowitsch       PetscCall(DMSetPeriodicity(*M, isper, maxCell,  L,  bd));
3897c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
38989566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
38999566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype));
39009566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
39019566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype));
390212fa691eSMatthew G. Knepley     }
39039566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert,dm,0,0,0));
390467a56275SMatthew G Knepley   }
39059566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject) *M));
390667a56275SMatthew G Knepley   PetscFunctionReturn(0);
390767a56275SMatthew G Knepley }
3908264ace61SBarry Smith 
3909264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3910264ace61SBarry Smith 
3911264ace61SBarry Smith /*@C
39121c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39131c84c290SBarry Smith 
39141c84c290SBarry Smith   Not Collective
39151c84c290SBarry Smith 
39161c84c290SBarry Smith   Input Parameters:
39171c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39181c84c290SBarry Smith - create_func - The creation routine itself
39191c84c290SBarry Smith 
39201c84c290SBarry Smith   Notes:
39211c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39221c84c290SBarry Smith 
39231c84c290SBarry Smith   Sample usage:
39241c84c290SBarry Smith .vb
3925bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39261c84c290SBarry Smith .ve
39271c84c290SBarry Smith 
39281c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39291c84c290SBarry Smith .vb
39301c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39311c84c290SBarry Smith     DMSetType(DM,"my_da");
39321c84c290SBarry Smith .ve
39331c84c290SBarry Smith    or at runtime via the option
39341c84c290SBarry Smith .vb
39351c84c290SBarry Smith     -da_type my_da
39361c84c290SBarry Smith .ve
3937264ace61SBarry Smith 
3938264ace61SBarry Smith   Level: advanced
39391c84c290SBarry Smith 
3940db781477SPatrick Sanan .seealso: `DMRegisterAll()`, `DMRegisterDestroy()`
39411c84c290SBarry Smith 
3942264ace61SBarry Smith @*/
3943bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3944264ace61SBarry Smith {
3945264ace61SBarry Smith   PetscFunctionBegin;
39469566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
39479566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList,sname,function));
3948264ace61SBarry Smith   PetscFunctionReturn(0);
3949264ace61SBarry Smith }
3950264ace61SBarry Smith 
3951b859378eSBarry Smith /*@C
395255849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3953b859378eSBarry Smith 
3954d083f849SBarry Smith   Collective on viewer
3955b859378eSBarry Smith 
3956b859378eSBarry Smith   Input Parameters:
3957b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3958b859378eSBarry Smith            some related function before a call to DMLoad().
3959b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3960b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3961b859378eSBarry Smith 
3962b859378eSBarry Smith    Level: intermediate
3963b859378eSBarry Smith 
3964b859378eSBarry Smith   Notes:
396555849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
3966b859378eSBarry Smith 
3967cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
3968cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
3969cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
3970cd7e8a5eSksagiyam 
3971b859378eSBarry Smith   Notes for advanced users:
3972b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3973b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3974b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3975b859378eSBarry Smith   format is
3976b859378eSBarry Smith .vb
3977b859378eSBarry Smith      has not yet been determined
3978b859378eSBarry Smith .ve
3979b859378eSBarry Smith 
3980db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
3981b859378eSBarry Smith @*/
3982b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3983b859378eSBarry Smith {
39849331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3985b859378eSBarry Smith 
3986b859378eSBarry Smith   PetscFunctionBegin;
3987b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3988b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
39899566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
39909566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
39919566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5));
39929566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load,viewer,0,0,0));
39939331c7a4SMatthew G. Knepley   if (isbinary) {
39949331c7a4SMatthew G. Knepley     PetscInt classid;
39959331c7a4SMatthew G. Knepley     char     type[256];
3996b859378eSBarry Smith 
39979566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT));
39987a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID,PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
39999566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR));
40009566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
40019566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
40029331c7a4SMatthew G. Knepley   } else if (ishdf5) {
40039566063dSJacob Faibussowitsch     if (newdm->ops->load) PetscCall((*newdm->ops->load)(newdm,viewer));
40049331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
40059566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load,viewer,0,0,0));
4006b859378eSBarry Smith   PetscFunctionReturn(0);
4007b859378eSBarry Smith }
4008b859378eSBarry Smith 
4009b2e4378dSMatthew G. Knepley /*@
4010b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4011b2e4378dSMatthew G. Knepley 
4012b2e4378dSMatthew G. Knepley   Not collective
4013b2e4378dSMatthew G. Knepley 
4014b2e4378dSMatthew G. Knepley   Input Parameter:
4015b2e4378dSMatthew G. Knepley . dm - the DM
4016b2e4378dSMatthew G. Knepley 
4017b2e4378dSMatthew G. Knepley   Output Parameters:
4018b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4019b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4020b2e4378dSMatthew G. Knepley 
4021b2e4378dSMatthew G. Knepley   Level: beginner
4022b2e4378dSMatthew G. Knepley 
4023b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4024b2e4378dSMatthew G. Knepley 
4025db781477SPatrick Sanan .seealso: `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetBoundingBox()`
4026b2e4378dSMatthew G. Knepley @*/
4027b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4028b2e4378dSMatthew G. Knepley {
4029b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4030b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4031b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4032b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4033b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4034b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4035b2e4378dSMatthew G. Knepley 
4036b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4037b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
40399566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm, &coords));
4040b2e4378dSMatthew G. Knepley   if (coords) {
40419566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(coords, &local_coords));
40429566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(coords, &N));
4043b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4044b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4045b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4046b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4047b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4048b2e4378dSMatthew G. Knepley       }
4049b2e4378dSMatthew G. Knepley     }
40509566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(coords, &local_coords));
4051b2e4378dSMatthew G. Knepley   } else {
4052b2e4378dSMatthew G. Knepley     PetscBool isda;
4053b2e4378dSMatthew G. Knepley 
40549566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda));
40559566063dSJacob Faibussowitsch     if (isda) PetscCall(DMGetLocalBoundingIndices_DMDA(dm, min, max));
4056b2e4378dSMatthew G. Knepley   }
40579566063dSJacob Faibussowitsch   if (lmin) PetscCall(PetscArraycpy(lmin, min, cdim));
40589566063dSJacob Faibussowitsch   if (lmax) PetscCall(PetscArraycpy(lmax, max, cdim));
4059b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4060b2e4378dSMatthew G. Knepley }
4061b2e4378dSMatthew G. Knepley 
4062b2e4378dSMatthew G. Knepley /*@
4063b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4064b2e4378dSMatthew G. Knepley 
4065b2e4378dSMatthew G. Knepley   Collective
4066b2e4378dSMatthew G. Knepley 
4067b2e4378dSMatthew G. Knepley   Input Parameter:
4068b2e4378dSMatthew G. Knepley . dm - the DM
4069b2e4378dSMatthew G. Knepley 
4070b2e4378dSMatthew G. Knepley   Output Parameters:
4071b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4072b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4073b2e4378dSMatthew G. Knepley 
4074b2e4378dSMatthew G. Knepley   Level: beginner
4075b2e4378dSMatthew G. Knepley 
4076db781477SPatrick Sanan .seealso: `DMGetLocalBoundingBox()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`
4077b2e4378dSMatthew G. Knepley @*/
4078b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4079b2e4378dSMatthew G. Knepley {
4080b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
40816ce308c4SMatthew G. Knepley   PetscInt       cdim;
40826ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4083b2e4378dSMatthew G. Knepley 
4084b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4085b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40869566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
40879566063dSJacob Faibussowitsch   PetscCall(PetscMPIIntCast(cdim, &count));
40889566063dSJacob Faibussowitsch   PetscCall(DMGetLocalBoundingBox(dm, lmin, lmax));
40891c2dc1cbSBarry Smith   if (gmin) PetscCall(MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm)));
40901c2dc1cbSBarry Smith   if (gmax) PetscCall(MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm)));
4091b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4092b2e4378dSMatthew G. Knepley }
4093b2e4378dSMatthew G. Knepley 
40947da65231SMatthew G Knepley /******************************** FEM Support **********************************/
40957da65231SMatthew G Knepley 
4096a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4097a6dfd86eSKarl Rupp {
40981d47ebbbSSatish Balay   PetscInt       f;
40991b30c384SMatthew G Knepley 
41007da65231SMatthew G Knepley   PetscFunctionBegin;
410163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41021d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
41039566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41047da65231SMatthew G Knepley   }
41057da65231SMatthew G Knepley   PetscFunctionReturn(0);
41067da65231SMatthew G Knepley }
41077da65231SMatthew G Knepley 
4108a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4109a6dfd86eSKarl Rupp {
41101b30c384SMatthew G Knepley   PetscInt       f, g;
41117da65231SMatthew G Knepley 
41127da65231SMatthew G Knepley   PetscFunctionBegin;
411363a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41141d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41159566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
41161d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
411763a3b9bcSJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f*cols+g])));
41187da65231SMatthew G Knepley     }
41199566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41207da65231SMatthew G Knepley   }
41217da65231SMatthew G Knepley   PetscFunctionReturn(0);
41227da65231SMatthew G Knepley }
4123e7c4fc90SDmitry Karpeev 
41246113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4125e759306cSMatthew G. Knepley {
41260c5b8624SToby Isaac   PetscInt          localSize, bs;
41270c5b8624SToby Isaac   PetscMPIInt       size;
41280c5b8624SToby Isaac   Vec               x, xglob;
41290c5b8624SToby Isaac   const PetscScalar *xarray;
4130e759306cSMatthew G. Knepley 
4131e759306cSMatthew G. Knepley   PetscFunctionBegin;
41329566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size));
41339566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41349566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
41359566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
41369566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name));
41370c5b8624SToby Isaac   if (size > 1) {
41389566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x,&localSize));
41399566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x,&xarray));
41409566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x,&bs));
41419566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob));
41420c5b8624SToby Isaac   } else {
41430c5b8624SToby Isaac     xglob = x;
41440c5b8624SToby Isaac   }
41459566063dSJacob Faibussowitsch   PetscCall(VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm))));
41460c5b8624SToby Isaac   if (size > 1) {
41479566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
41489566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x,&xarray));
41490c5b8624SToby Isaac   }
41509566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4151e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4152e759306cSMatthew G. Knepley }
4153e759306cSMatthew G. Knepley 
415488ed4aceSMatthew G Knepley /*@
41551bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4156061576a5SJed Brown 
4157061576a5SJed Brown   Input Parameter:
4158061576a5SJed Brown . dm - The DM
4159061576a5SJed Brown 
4160061576a5SJed Brown   Output Parameter:
4161061576a5SJed Brown . section - The PetscSection
4162061576a5SJed Brown 
4163061576a5SJed Brown   Options Database Keys:
4164061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4165061576a5SJed Brown 
4166061576a5SJed Brown   Level: advanced
4167061576a5SJed Brown 
4168061576a5SJed Brown   Notes:
4169061576a5SJed Brown   Use DMGetLocalSection() in new code.
4170061576a5SJed Brown 
4171061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4172061576a5SJed Brown 
4173db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4174061576a5SJed Brown @*/
4175061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4176061576a5SJed Brown {
4177061576a5SJed Brown   PetscFunctionBegin;
41789566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm,section));
4179061576a5SJed Brown   PetscFunctionReturn(0);
4180061576a5SJed Brown }
4181061576a5SJed Brown 
4182061576a5SJed Brown /*@
4183061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
418488ed4aceSMatthew G Knepley 
418588ed4aceSMatthew G Knepley   Input Parameter:
418688ed4aceSMatthew G Knepley . dm - The DM
418788ed4aceSMatthew G Knepley 
418888ed4aceSMatthew G Knepley   Output Parameter:
418988ed4aceSMatthew G Knepley . section - The PetscSection
419088ed4aceSMatthew G Knepley 
4191e5893cccSMatthew G. Knepley   Options Database Keys:
4192e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4193e5893cccSMatthew G. Knepley 
419488ed4aceSMatthew G Knepley   Level: intermediate
419588ed4aceSMatthew G Knepley 
419688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
419788ed4aceSMatthew G Knepley 
4198db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
419988ed4aceSMatthew G Knepley @*/
4200061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42010adebc6cSBarry Smith {
420288ed4aceSMatthew G Knepley   PetscFunctionBegin;
420388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
420488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42051bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4206e5e52638SMatthew G. Knepley     PetscInt d;
4207e5e52638SMatthew G. Knepley 
420845480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
420945480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
421045480ffeSMatthew G. Knepley       PetscViewer       viewer;
421145480ffeSMatthew G. Knepley       PetscViewerFormat format;
421245480ffeSMatthew G. Knepley       PetscBool         flg;
421345480ffeSMatthew G. Knepley 
42149566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42159566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
421645480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42179566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42189566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
421945480ffeSMatthew G. Knepley       }
422045480ffeSMatthew G. Knepley       if (flg) {
42219566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42229566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42239566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
422445480ffeSMatthew G. Knepley       }
422545480ffeSMatthew G. Knepley     }
42269566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createlocalsection)(dm));
42279566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view"));
42282f0f8703SMatthew G. Knepley   }
42291bb6d2a8SBarry Smith   *section = dm->localSection;
423088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
423188ed4aceSMatthew G Knepley }
423288ed4aceSMatthew G Knepley 
423388ed4aceSMatthew G Knepley /*@
42341bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4235061576a5SJed Brown 
4236061576a5SJed Brown   Input Parameters:
4237061576a5SJed Brown + dm - The DM
4238061576a5SJed Brown - section - The PetscSection
4239061576a5SJed Brown 
4240061576a5SJed Brown   Level: advanced
4241061576a5SJed Brown 
4242061576a5SJed Brown   Notes:
4243061576a5SJed Brown   Use DMSetLocalSection() in new code.
4244061576a5SJed Brown 
4245061576a5SJed Brown   Any existing Section will be destroyed
4246061576a5SJed Brown 
4247db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4248061576a5SJed Brown @*/
4249061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4250061576a5SJed Brown {
4251061576a5SJed Brown   PetscFunctionBegin;
42529566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm,section));
4253061576a5SJed Brown   PetscFunctionReturn(0);
4254061576a5SJed Brown }
4255061576a5SJed Brown 
4256061576a5SJed Brown /*@
4257061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
425888ed4aceSMatthew G Knepley 
425988ed4aceSMatthew G Knepley   Input Parameters:
426088ed4aceSMatthew G Knepley + dm - The DM
426188ed4aceSMatthew G Knepley - section - The PetscSection
426288ed4aceSMatthew G Knepley 
426388ed4aceSMatthew G Knepley   Level: intermediate
426488ed4aceSMatthew G Knepley 
426588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
426688ed4aceSMatthew G Knepley 
4267db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetGlobalSection()`
426888ed4aceSMatthew G Knepley @*/
4269061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
42700adebc6cSBarry Smith {
4271c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4272af122d2aSMatthew G Knepley   PetscInt       f;
427388ed4aceSMatthew G Knepley 
427488ed4aceSMatthew G Knepley   PetscFunctionBegin;
427588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4276b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
42789566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
42791bb6d2a8SBarry Smith   dm->localSection = section;
42809566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4281af122d2aSMatthew G Knepley   if (numFields) {
42829566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4283af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
42840f21e855SMatthew G. Knepley       PetscObject disc;
4285af122d2aSMatthew G Knepley       const char *name;
4286af122d2aSMatthew G Knepley 
42879566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
42889566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
42899566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4290af122d2aSMatthew G Knepley     }
4291af122d2aSMatthew G Knepley   }
4292e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
42939566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
429488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
429588ed4aceSMatthew G Knepley }
429688ed4aceSMatthew G Knepley 
42979435951eSToby Isaac /*@
4298b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
42999435951eSToby Isaac 
4300e228b242SToby Isaac   not collective
4301e228b242SToby Isaac 
43029435951eSToby Isaac   Input Parameter:
43039435951eSToby Isaac . dm - The DM
43049435951eSToby Isaac 
4305d8d19677SJose E. Roman   Output Parameters:
43069435951eSToby 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.
430779769bd5SJed 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.
430879769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43099435951eSToby Isaac 
43109435951eSToby Isaac   Level: advanced
43119435951eSToby Isaac 
431279769bd5SJed Brown   Note: This gets borrowed references, so the user should not destroy the PetscSection, Mat, or Vec.
43139435951eSToby Isaac 
4314db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
43159435951eSToby Isaac @*/
431679769bd5SJed Brown PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
43179435951eSToby Isaac {
43189435951eSToby Isaac   PetscFunctionBegin;
43199435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43209566063dSJacob Faibussowitsch   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscCall((*dm->ops->createdefaultconstraints)(dm));
43213b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43223b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
432379769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43249435951eSToby Isaac   PetscFunctionReturn(0);
43259435951eSToby Isaac }
43269435951eSToby Isaac 
43279435951eSToby Isaac /*@
4328b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
43299435951eSToby Isaac 
433079769bd5SJed 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().
43319435951eSToby Isaac 
433279769bd5SJed 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.
43339435951eSToby Isaac 
4334e228b242SToby Isaac   collective on dm
4335e228b242SToby Isaac 
43369435951eSToby Isaac   Input Parameters:
43379435951eSToby Isaac + dm - The DM
433879769bd5SJed 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).
433979769bd5SJed 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).
434079769bd5SJed 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).
43419435951eSToby Isaac 
43429435951eSToby Isaac   Level: advanced
43439435951eSToby Isaac 
434479769bd5SJed Brown   Note: This increments the references of the PetscSection, Mat, and Vec, so they user can destroy them.
43459435951eSToby Isaac 
4346db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
43479435951eSToby Isaac @*/
434879769bd5SJed Brown PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
43499435951eSToby Isaac {
4350e228b242SToby Isaac   PetscMPIInt result;
43519435951eSToby Isaac 
43529435951eSToby Isaac   PetscFunctionBegin;
43539435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4354e228b242SToby Isaac   if (section) {
4355e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43569566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result));
43577a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4358e228b242SToby Isaac   }
4359e228b242SToby Isaac   if (mat) {
4360e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
43619566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result));
43627a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4363e228b242SToby Isaac   }
436479769bd5SJed Brown   if (bias) {
436579769bd5SJed Brown     PetscValidHeaderSpecific(bias,VEC_CLASSID,4);
43669566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)bias),&result));
436779769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint bias must have local communicator");
436879769bd5SJed Brown   }
43699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43709566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
43713b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
43729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
43739566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
43743b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
43759566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
43769566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
437779769bd5SJed Brown   dm->defaultConstraint.bias = bias;
43789435951eSToby Isaac   PetscFunctionReturn(0);
43799435951eSToby Isaac }
43809435951eSToby Isaac 
4381497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4382507e4973SMatthew G. Knepley /*
4383507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4384507e4973SMatthew G. Knepley 
4385507e4973SMatthew G. Knepley   Input Parameters:
4386507e4973SMatthew G. Knepley + dm - The DM
4387507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4388507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4389507e4973SMatthew G. Knepley 
4390507e4973SMatthew G. Knepley   Level: intermediate
4391507e4973SMatthew G. Knepley 
4392db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4393507e4973SMatthew G. Knepley */
4394f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4395507e4973SMatthew G. Knepley {
4396507e4973SMatthew G. Knepley   MPI_Comm        comm;
4397507e4973SMatthew G. Knepley   PetscLayout     layout;
4398507e4973SMatthew G. Knepley   const PetscInt *ranges;
4399507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4400507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4401507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4402507e4973SMatthew G. Knepley 
4403507e4973SMatthew G. Knepley   PetscFunctionBegin;
44049566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
4405507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44069566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44079566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44089566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44099566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44109566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44119566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44129566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44139566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44149566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4415507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4416f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4417507e4973SMatthew G. Knepley 
44189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44209566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44219566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4424507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
442563a3b9bcSJacob 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;}
442663a3b9bcSJacob 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;}
4427507e4973SMatthew G. Knepley     if (gdof < 0) {
4428507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4429507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4430507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4431507e4973SMatthew G. Knepley 
44329566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset,size+1,ranges,&r));
4433507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
443463a3b9bcSJacob 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;}
4435507e4973SMatthew G. Knepley       }
4436507e4973SMatthew G. Knepley     }
4437507e4973SMatthew G. Knepley   }
44389566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
44399566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
44401c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4441507e4973SMatthew G. Knepley   if (!gvalid) {
44429566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4443507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4444507e4973SMatthew G. Knepley   }
4445507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4446507e4973SMatthew G. Knepley }
4447f741bcd2SMatthew G. Knepley #endif
4448507e4973SMatthew G. Knepley 
444988ed4aceSMatthew G Knepley /*@
4450e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
445188ed4aceSMatthew G Knepley 
4452d083f849SBarry Smith   Collective on dm
44538b1ab98fSJed Brown 
445488ed4aceSMatthew G Knepley   Input Parameter:
445588ed4aceSMatthew G Knepley . dm - The DM
445688ed4aceSMatthew G Knepley 
445788ed4aceSMatthew G Knepley   Output Parameter:
445888ed4aceSMatthew G Knepley . section - The PetscSection
445988ed4aceSMatthew G Knepley 
446088ed4aceSMatthew G Knepley   Level: intermediate
446188ed4aceSMatthew G Knepley 
446288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
446388ed4aceSMatthew G Knepley 
4464db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
446588ed4aceSMatthew G Knepley @*/
4466e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
44670adebc6cSBarry Smith {
446888ed4aceSMatthew G Knepley   PetscFunctionBegin;
446988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
447088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
44711bb6d2a8SBarry Smith   if (!dm->globalSection) {
4472fd59a867SMatthew G. Knepley     PetscSection s;
4473fd59a867SMatthew G. Knepley 
44749566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
44757a8be351SBarry Smith     PetscCheck(s,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
44767a8be351SBarry Smith     PetscCheck(dm->sf,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
44779566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
44789566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
44799566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
44809566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
448188ed4aceSMatthew G Knepley   }
44821bb6d2a8SBarry Smith   *section = dm->globalSection;
448388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
448488ed4aceSMatthew G Knepley }
448588ed4aceSMatthew G Knepley 
4486b21d0597SMatthew G Knepley /*@
4487e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4488b21d0597SMatthew G Knepley 
4489b21d0597SMatthew G Knepley   Input Parameters:
4490b21d0597SMatthew G Knepley + dm - The DM
44915080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4492b21d0597SMatthew G Knepley 
4493b21d0597SMatthew G Knepley   Level: intermediate
4494b21d0597SMatthew G Knepley 
4495b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4496b21d0597SMatthew G Knepley 
4497db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4498b21d0597SMatthew G Knepley @*/
4499e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45000adebc6cSBarry Smith {
4501b21d0597SMatthew G Knepley   PetscFunctionBegin;
4502b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45035080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
45049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45059566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45061bb6d2a8SBarry Smith   dm->globalSection = section;
4507497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45089566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4509507e4973SMatthew G. Knepley #endif
4510b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4511b21d0597SMatthew G Knepley }
4512b21d0597SMatthew G Knepley 
451388ed4aceSMatthew G Knepley /*@
45141bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
451588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
451688ed4aceSMatthew G Knepley 
451788ed4aceSMatthew G Knepley   Input Parameter:
451888ed4aceSMatthew G Knepley . dm - The DM
451988ed4aceSMatthew G Knepley 
452088ed4aceSMatthew G Knepley   Output Parameter:
452188ed4aceSMatthew G Knepley . sf - The PetscSF
452288ed4aceSMatthew G Knepley 
452388ed4aceSMatthew G Knepley   Level: intermediate
452488ed4aceSMatthew G Knepley 
452588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
452688ed4aceSMatthew G Knepley 
4527db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
452888ed4aceSMatthew G Knepley @*/
45291bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
45300adebc6cSBarry Smith {
453188ed4aceSMatthew G Knepley   PetscInt       nroots;
453288ed4aceSMatthew G Knepley 
453388ed4aceSMatthew G Knepley   PetscFunctionBegin;
453488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
453588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
45361bb6d2a8SBarry Smith   if (!dm->sectionSF) {
45379566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF));
453833907cc2SStefano Zampini   }
45399566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
454088ed4aceSMatthew G Knepley   if (nroots < 0) {
454188ed4aceSMatthew G Knepley     PetscSection section, gSection;
454288ed4aceSMatthew G Knepley 
45439566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
454431ea6d37SMatthew G Knepley     if (section) {
45459566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
45469566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
454731ea6d37SMatthew G Knepley     } else {
45480298fd71SBarry Smith       *sf = NULL;
454931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
455031ea6d37SMatthew G Knepley     }
455188ed4aceSMatthew G Knepley   }
45521bb6d2a8SBarry Smith   *sf = dm->sectionSF;
455388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
455488ed4aceSMatthew G Knepley }
455588ed4aceSMatthew G Knepley 
455688ed4aceSMatthew G Knepley /*@
45571bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
455888ed4aceSMatthew G Knepley 
455988ed4aceSMatthew G Knepley   Input Parameters:
456088ed4aceSMatthew G Knepley + dm - The DM
456188ed4aceSMatthew G Knepley - sf - The PetscSF
456288ed4aceSMatthew G Knepley 
456388ed4aceSMatthew G Knepley   Level: intermediate
456488ed4aceSMatthew G Knepley 
456588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
456688ed4aceSMatthew G Knepley 
4567db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
456888ed4aceSMatthew G Knepley @*/
45691bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
45700adebc6cSBarry Smith {
457188ed4aceSMatthew G Knepley   PetscFunctionBegin;
457288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4573b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
45749566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
45759566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
45761bb6d2a8SBarry Smith   dm->sectionSF = sf;
457788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
457888ed4aceSMatthew G Knepley }
457988ed4aceSMatthew G Knepley 
458088ed4aceSMatthew G Knepley /*@C
45811bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
458288ed4aceSMatthew G Knepley   describing the data layout.
458388ed4aceSMatthew G Knepley 
458488ed4aceSMatthew G Knepley   Input Parameters:
458588ed4aceSMatthew G Knepley + dm - The DM
458688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
458788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
458888ed4aceSMatthew G Knepley 
45891bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
459088ed4aceSMatthew G Knepley 
45911bb6d2a8SBarry Smith   Level: developer
45921bb6d2a8SBarry Smith 
45931bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
45941bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
45951bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
45961bb6d2a8SBarry Smith 
4597db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
459888ed4aceSMatthew G Knepley @*/
45991bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
460088ed4aceSMatthew G Knepley {
460188ed4aceSMatthew G Knepley   PetscFunctionBegin;
460288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46039566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
460488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
460588ed4aceSMatthew G Knepley }
4606af122d2aSMatthew G Knepley 
4607b21d0597SMatthew G Knepley /*@
4608b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4609b21d0597SMatthew G Knepley 
4610b21d0597SMatthew G Knepley   Input Parameter:
4611b21d0597SMatthew G Knepley . dm - The DM
4612b21d0597SMatthew G Knepley 
4613b21d0597SMatthew G Knepley   Output Parameter:
4614b21d0597SMatthew G Knepley . sf - The PetscSF
4615b21d0597SMatthew G Knepley 
4616b21d0597SMatthew G Knepley   Level: intermediate
4617b21d0597SMatthew G Knepley 
4618b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4619b21d0597SMatthew G Knepley 
4620db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4621b21d0597SMatthew G Knepley @*/
46220adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
46230adebc6cSBarry Smith {
4624b21d0597SMatthew G Knepley   PetscFunctionBegin;
4625b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4626b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4627b21d0597SMatthew G Knepley   *sf = dm->sf;
4628b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4629b21d0597SMatthew G Knepley }
4630b21d0597SMatthew G Knepley 
4631057b4bcdSMatthew G Knepley /*@
4632057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4633057b4bcdSMatthew G Knepley 
4634057b4bcdSMatthew G Knepley   Input Parameters:
4635057b4bcdSMatthew G Knepley + dm - The DM
4636057b4bcdSMatthew G Knepley - sf - The PetscSF
4637057b4bcdSMatthew G Knepley 
4638057b4bcdSMatthew G Knepley   Level: intermediate
4639057b4bcdSMatthew G Knepley 
4640db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4641057b4bcdSMatthew G Knepley @*/
46420adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46430adebc6cSBarry Smith {
4644057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4645057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4646b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46479566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
46489566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4649057b4bcdSMatthew G Knepley   dm->sf = sf;
4650057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4651057b4bcdSMatthew G Knepley }
4652057b4bcdSMatthew G Knepley 
46534f37162bSMatthew G. Knepley /*@
46544f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
46554f37162bSMatthew G. Knepley 
46564f37162bSMatthew G. Knepley   Input Parameter:
46574f37162bSMatthew G. Knepley . dm - The DM
46584f37162bSMatthew G. Knepley 
46594f37162bSMatthew G. Knepley   Output Parameter:
46604f37162bSMatthew G. Knepley . sf - The PetscSF
46614f37162bSMatthew G. Knepley 
46624f37162bSMatthew G. Knepley   Level: intermediate
46634f37162bSMatthew G. Knepley 
46644f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
46654f37162bSMatthew G. Knepley 
4666db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46674f37162bSMatthew G. Knepley @*/
46684f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
46694f37162bSMatthew G. Knepley {
46704f37162bSMatthew G. Knepley   PetscFunctionBegin;
46714f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46724f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
46734f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
46744f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46754f37162bSMatthew G. Knepley }
46764f37162bSMatthew G. Knepley 
46774f37162bSMatthew G. Knepley /*@
46784f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
46794f37162bSMatthew G. Knepley 
46804f37162bSMatthew G. Knepley   Input Parameters:
46814f37162bSMatthew G. Knepley + dm - The DM
46824f37162bSMatthew G. Knepley - sf - The PetscSF
46834f37162bSMatthew G. Knepley 
46844f37162bSMatthew G. Knepley   Level: intermediate
46854f37162bSMatthew G. Knepley 
4686db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46874f37162bSMatthew G. Knepley @*/
46884f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
46894f37162bSMatthew G. Knepley {
46904f37162bSMatthew G. Knepley   PetscFunctionBegin;
46914f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46924f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46939566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sf));
46949566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
46954f37162bSMatthew G. Knepley   dm->sfNatural = sf;
46964f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46974f37162bSMatthew G. Knepley }
46984f37162bSMatthew G. Knepley 
469934aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
470034aa8a36SMatthew G. Knepley {
470134aa8a36SMatthew G. Knepley   PetscClassId   id;
470234aa8a36SMatthew G. Knepley 
470334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
47049566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
470534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
47069566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
470734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
47089566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
470917c1d62eSMatthew G. Knepley   } else {
47109566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
471134aa8a36SMatthew G. Knepley   }
471234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
471334aa8a36SMatthew G. Knepley }
471434aa8a36SMatthew G. Knepley 
471544a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
471644a7f3ddSMatthew G. Knepley {
471744a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
471844a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
471944a7f3ddSMatthew G. Knepley 
472044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
472144a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
47229566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
472344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4724e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
47259566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
472644a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
472744a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
472844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
472944a7f3ddSMatthew G. Knepley }
473044a7f3ddSMatthew G. Knepley 
473144a7f3ddSMatthew G. Knepley /*@
473244a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
473344a7f3ddSMatthew G. Knepley 
4734d083f849SBarry Smith   Logically collective on dm
473544a7f3ddSMatthew G. Knepley 
473644a7f3ddSMatthew G. Knepley   Input Parameter:
473744a7f3ddSMatthew G. Knepley . dm - The DM
473844a7f3ddSMatthew G. Knepley 
473944a7f3ddSMatthew G. Knepley   Level: intermediate
474044a7f3ddSMatthew G. Knepley 
4741db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
474244a7f3ddSMatthew G. Knepley @*/
474344a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
474444a7f3ddSMatthew G. Knepley {
474544a7f3ddSMatthew G. Knepley   PetscInt       f;
474644a7f3ddSMatthew G. Knepley 
474744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
474844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
474944a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
47509566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
47519566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
475244a7f3ddSMatthew G. Knepley   }
47539566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
475444a7f3ddSMatthew G. Knepley   dm->fields = NULL;
475544a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
475644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
475744a7f3ddSMatthew G. Knepley }
475844a7f3ddSMatthew G. Knepley 
4759689b5837SMatthew G. Knepley /*@
4760689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4761689b5837SMatthew G. Knepley 
4762689b5837SMatthew G. Knepley   Not collective
4763689b5837SMatthew G. Knepley 
4764689b5837SMatthew G. Knepley   Input Parameter:
4765689b5837SMatthew G. Knepley . dm - The DM
4766689b5837SMatthew G. Knepley 
4767689b5837SMatthew G. Knepley   Output Parameter:
4768689b5837SMatthew G. Knepley . Nf - The number of fields
4769689b5837SMatthew G. Knepley 
4770689b5837SMatthew G. Knepley   Level: intermediate
4771689b5837SMatthew G. Knepley 
4772db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4773689b5837SMatthew G. Knepley @*/
47740f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
47750f21e855SMatthew G. Knepley {
47760f21e855SMatthew G. Knepley   PetscFunctionBegin;
47770f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4778534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
477944a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4780af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4781af122d2aSMatthew G Knepley }
4782af122d2aSMatthew G Knepley 
4783689b5837SMatthew G. Knepley /*@
4784689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4785689b5837SMatthew G. Knepley 
4786d083f849SBarry Smith   Logically collective on dm
4787689b5837SMatthew G. Knepley 
4788689b5837SMatthew G. Knepley   Input Parameters:
4789689b5837SMatthew G. Knepley + dm - The DM
4790689b5837SMatthew G. Knepley - Nf - The number of fields
4791689b5837SMatthew G. Knepley 
4792689b5837SMatthew G. Knepley   Level: intermediate
4793689b5837SMatthew G. Knepley 
4794db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4795689b5837SMatthew G. Knepley @*/
4796af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4797af122d2aSMatthew G Knepley {
47980f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4799af122d2aSMatthew G Knepley 
4800af122d2aSMatthew G Knepley   PetscFunctionBegin;
4801af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48029566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
48030f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48040f21e855SMatthew G. Knepley     PetscContainer obj;
48050f21e855SMatthew G. Knepley 
48069566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj));
48079566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject) obj));
48089566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4809af122d2aSMatthew G Knepley   }
4810af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4811af122d2aSMatthew G Knepley }
4812af122d2aSMatthew G Knepley 
4813c1929be8SMatthew G. Knepley /*@
4814c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4815c1929be8SMatthew G. Knepley 
4816c1929be8SMatthew G. Knepley   Not collective
4817c1929be8SMatthew G. Knepley 
4818c1929be8SMatthew G. Knepley   Input Parameters:
4819c1929be8SMatthew G. Knepley + dm - The DM
4820c1929be8SMatthew G. Knepley - f  - The field number
4821c1929be8SMatthew G. Knepley 
482244a7f3ddSMatthew G. Knepley   Output Parameters:
482344a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
482444a7f3ddSMatthew G. Knepley - field - The discretization object
4825c1929be8SMatthew G. Knepley 
482644a7f3ddSMatthew G. Knepley   Level: intermediate
4827c1929be8SMatthew G. Knepley 
4828db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4829c1929be8SMatthew G. Knepley @*/
483044a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4831af122d2aSMatthew G Knepley {
4832af122d2aSMatthew G Knepley   PetscFunctionBegin;
4833af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4834064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
48357a8be351SBarry 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);
483644a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
483744a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4838decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4839decb47aaSMatthew G. Knepley }
4840decb47aaSMatthew G. Knepley 
4841083401c6SMatthew G. Knepley /* Does not clear the DS */
4842083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4843083401c6SMatthew G. Knepley {
4844083401c6SMatthew G. Knepley   PetscFunctionBegin;
48459566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f+1));
48469566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
48479566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4848083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4849083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
48509566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
48519566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) field));
4852083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4853083401c6SMatthew G. Knepley }
4854083401c6SMatthew G. Knepley 
4855c1929be8SMatthew G. Knepley /*@
4856c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4857c1929be8SMatthew G. Knepley 
4858d083f849SBarry Smith   Logically collective on dm
4859c1929be8SMatthew G. Knepley 
4860c1929be8SMatthew G. Knepley   Input Parameters:
4861c1929be8SMatthew G. Knepley + dm    - The DM
4862c1929be8SMatthew G. Knepley . f     - The field number
486344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4864c1929be8SMatthew G. Knepley - field - The discretization object
4865c1929be8SMatthew G. Knepley 
486644a7f3ddSMatthew G. Knepley   Level: intermediate
4867c1929be8SMatthew G. Knepley 
4868db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4869c1929be8SMatthew G. Knepley @*/
487044a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4871decb47aaSMatthew G. Knepley {
4872decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4873decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4874e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
487544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
48767a8be351SBarry Smith   PetscCheck(f >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
48779566063dSJacob Faibussowitsch   PetscCall(DMSetField_Internal(dm, f, label, field));
48789566063dSJacob Faibussowitsch   PetscCall(DMSetDefaultAdjacency_Private(dm, f, field));
48799566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
488044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
488144a7f3ddSMatthew G. Knepley }
488244a7f3ddSMatthew G. Knepley 
488344a7f3ddSMatthew G. Knepley /*@
488444a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
488544a7f3ddSMatthew G. Knepley 
4886d083f849SBarry Smith   Logically collective on dm
488744a7f3ddSMatthew G. Knepley 
488844a7f3ddSMatthew G. Knepley   Input Parameters:
488944a7f3ddSMatthew G. Knepley + dm    - The DM
489044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
489144a7f3ddSMatthew G. Knepley - field - The discretization object
489244a7f3ddSMatthew G. Knepley 
489344a7f3ddSMatthew G. Knepley   Level: intermediate
489444a7f3ddSMatthew G. Knepley 
4895db781477SPatrick Sanan .seealso: `DMSetField()`, `DMGetField()`
489644a7f3ddSMatthew G. Knepley @*/
489744a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
489844a7f3ddSMatthew G. Knepley {
489944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
490044a7f3ddSMatthew G. Knepley 
490144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
490244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4903064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
490444a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
49059566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf+1));
490644a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
490744a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
49089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
49099566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) field));
49109566063dSJacob Faibussowitsch   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, field));
49119566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
4912af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4913af122d2aSMatthew G Knepley }
49146636e97aSMatthew G Knepley 
4915e5e52638SMatthew G. Knepley /*@
4916e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4917e0b68406SMatthew Knepley 
4918e0b68406SMatthew Knepley   Logically collective on dm
4919e0b68406SMatthew Knepley 
4920e0b68406SMatthew Knepley   Input Parameters:
4921e0b68406SMatthew Knepley + dm          - The DM
4922e0b68406SMatthew Knepley . f           - The field index
4923e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4924e0b68406SMatthew Knepley 
4925e0b68406SMatthew Knepley   Level: intermediate
4926e0b68406SMatthew Knepley 
4927db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4928e0b68406SMatthew Knepley @*/
4929e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4930e0b68406SMatthew Knepley {
4931e0b68406SMatthew Knepley   PetscFunctionBegin;
493263a3b9bcSJacob 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);
4933e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4934e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4935e0b68406SMatthew Knepley }
4936e0b68406SMatthew Knepley 
4937e0b68406SMatthew Knepley /*@
4938e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4939e0b68406SMatthew Knepley 
4940e0b68406SMatthew Knepley   Logically collective on dm
4941e0b68406SMatthew Knepley 
4942e0b68406SMatthew Knepley   Input Parameters:
4943e0b68406SMatthew Knepley + dm          - The DM
4944e0b68406SMatthew Knepley - f           - The field index
4945e0b68406SMatthew Knepley 
4946e0b68406SMatthew Knepley   Output Parameter:
4947e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4948e0b68406SMatthew Knepley 
4949e0b68406SMatthew Knepley   Level: intermediate
4950e0b68406SMatthew Knepley 
4951db781477SPatrick Sanan .seealso: `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4952e0b68406SMatthew Knepley @*/
4953e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4954e0b68406SMatthew Knepley {
4955e0b68406SMatthew Knepley   PetscFunctionBegin;
495663a3b9bcSJacob 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);
4957e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4958e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4959e0b68406SMatthew Knepley }
4960e0b68406SMatthew Knepley 
4961e0b68406SMatthew Knepley /*@
4962e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4963e5e52638SMatthew G. Knepley 
4964d083f849SBarry Smith   Collective on dm
4965e5e52638SMatthew G. Knepley 
4966e5e52638SMatthew G. Knepley   Input Parameter:
4967e5e52638SMatthew G. Knepley . dm - The DM
4968e5e52638SMatthew G. Knepley 
4969e5e52638SMatthew G. Knepley   Output Parameter:
4970e5e52638SMatthew G. Knepley . newdm - The DM
4971e5e52638SMatthew G. Knepley 
4972e5e52638SMatthew G. Knepley   Level: advanced
4973e5e52638SMatthew G. Knepley 
4974db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
4975e5e52638SMatthew G. Knepley @*/
4976e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4977e5e52638SMatthew G. Knepley {
4978e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4979e5e52638SMatthew G. Knepley 
4980e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4981e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
49829566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49839566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
4984e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4985e5e52638SMatthew G. Knepley     DMLabel     label;
4986e5e52638SMatthew G. Knepley     PetscObject field;
498734aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4988e5e52638SMatthew G. Knepley 
49899566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
49909566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
49919566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
49929566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
499334aa8a36SMatthew G. Knepley   }
499434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
499534aa8a36SMatthew G. Knepley }
499634aa8a36SMatthew G. Knepley 
499734aa8a36SMatthew G. Knepley /*@
499834aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
499934aa8a36SMatthew G. Knepley 
500034aa8a36SMatthew G. Knepley   Not collective
500134aa8a36SMatthew G. Knepley 
500234aa8a36SMatthew G. Knepley   Input Parameters:
500334aa8a36SMatthew G. Knepley + dm - The DM object
500434aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
500534aa8a36SMatthew G. Knepley 
5006d8d19677SJose E. Roman   Output Parameters:
500734aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
500834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
500934aa8a36SMatthew G. Knepley 
501034aa8a36SMatthew G. Knepley   Notes:
501134aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
501234aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
501334aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5014979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
501534aa8a36SMatthew G. Knepley 
501634aa8a36SMatthew G. Knepley   Level: developer
501734aa8a36SMatthew G. Knepley 
5018db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
501934aa8a36SMatthew G. Knepley @*/
502034aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
502134aa8a36SMatthew G. Knepley {
502234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
502334aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5024534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5025534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
502634aa8a36SMatthew G. Knepley   if (f < 0) {
502734aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
502834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
502934aa8a36SMatthew G. Knepley   } else {
503034aa8a36SMatthew G. Knepley     PetscInt       Nf;
503134aa8a36SMatthew G. Knepley 
50329566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
50337a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
503434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
503534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
503634aa8a36SMatthew G. Knepley   }
503734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
503834aa8a36SMatthew G. Knepley }
503934aa8a36SMatthew G. Knepley 
504034aa8a36SMatthew G. Knepley /*@
504134aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
504234aa8a36SMatthew G. Knepley 
504334aa8a36SMatthew G. Knepley   Not collective
504434aa8a36SMatthew G. Knepley 
504534aa8a36SMatthew G. Knepley   Input Parameters:
504634aa8a36SMatthew G. Knepley + dm         - The DM object
504734aa8a36SMatthew G. Knepley . f          - The field number
504834aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
504934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
505034aa8a36SMatthew G. Knepley 
505134aa8a36SMatthew G. Knepley   Notes:
505234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
505334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
505434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5055979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
505634aa8a36SMatthew G. Knepley 
505734aa8a36SMatthew G. Knepley   Level: developer
505834aa8a36SMatthew G. Knepley 
5059db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
506034aa8a36SMatthew G. Knepley @*/
506134aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
506234aa8a36SMatthew G. Knepley {
506334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
506434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
506534aa8a36SMatthew G. Knepley   if (f < 0) {
506634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
506734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
506834aa8a36SMatthew G. Knepley   } else {
506934aa8a36SMatthew G. Knepley     PetscInt       Nf;
507034aa8a36SMatthew G. Knepley 
50719566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
50727a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
507334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
507434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5075e5e52638SMatthew G. Knepley   }
5076e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5077e5e52638SMatthew G. Knepley }
5078e5e52638SMatthew G. Knepley 
5079b0441da4SMatthew G. Knepley /*@
5080b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5081b0441da4SMatthew G. Knepley 
5082b0441da4SMatthew G. Knepley   Not collective
5083b0441da4SMatthew G. Knepley 
5084f899ff85SJose E. Roman   Input Parameter:
5085b0441da4SMatthew G. Knepley . dm - The DM object
5086b0441da4SMatthew G. Knepley 
5087d8d19677SJose E. Roman   Output Parameters:
5088b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5089b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5090b0441da4SMatthew G. Knepley 
5091b0441da4SMatthew G. Knepley   Notes:
5092b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5093b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5094b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5095b0441da4SMatthew G. Knepley 
5096b0441da4SMatthew G. Knepley   Level: developer
5097b0441da4SMatthew G. Knepley 
5098db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5099b0441da4SMatthew G. Knepley @*/
5100b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5101b0441da4SMatthew G. Knepley {
5102b0441da4SMatthew G. Knepley   PetscInt       Nf;
5103b0441da4SMatthew G. Knepley 
5104b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5105b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5106064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5107064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
51089566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5109b0441da4SMatthew G. Knepley   if (!Nf) {
51109566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5111b0441da4SMatthew G. Knepley   } else {
51129566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5113b0441da4SMatthew G. Knepley   }
5114b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5115b0441da4SMatthew G. Knepley }
5116b0441da4SMatthew G. Knepley 
5117b0441da4SMatthew G. Knepley /*@
5118b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5119b0441da4SMatthew G. Knepley 
5120b0441da4SMatthew G. Knepley   Not collective
5121b0441da4SMatthew G. Knepley 
5122b0441da4SMatthew G. Knepley   Input Parameters:
5123b0441da4SMatthew G. Knepley + dm         - The DM object
5124b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5125b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5126b0441da4SMatthew G. Knepley 
5127b0441da4SMatthew G. Knepley   Notes:
5128b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5129b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5130b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5131b0441da4SMatthew G. Knepley 
5132b0441da4SMatthew G. Knepley   Level: developer
5133b0441da4SMatthew G. Knepley 
5134db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5135b0441da4SMatthew G. Knepley @*/
5136b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5137b0441da4SMatthew G. Knepley {
5138b0441da4SMatthew G. Knepley   PetscInt       Nf;
5139b0441da4SMatthew G. Knepley 
5140b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5141b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51429566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5143b0441da4SMatthew G. Knepley   if (!Nf) {
51449566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5145b0441da4SMatthew G. Knepley   } else {
51469566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5147e5e52638SMatthew G. Knepley   }
5148e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5149e5e52638SMatthew G. Knepley }
5150e5e52638SMatthew G. Knepley 
5151799db056SMatthew G. Knepley PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5152783e2ec8SMatthew G. Knepley {
5153799db056SMatthew G. Knepley   DM           plex;
5154799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5155799db056SMatthew G. Knepley   const char **names;
5156799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5157799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5158799db056SMatthew G. Knepley   size_t       len;
5159799db056SMatthew G. Knepley   MPI_Comm     comm;
5160799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5161783e2ec8SMatthew G. Knepley 
5162783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5163799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
5164799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5165799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5166799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5167799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5168799db056SMatthew G. Knepley     PetscDS  dsBC;
5169799db056SMatthew G. Knepley     PetscInt numBd;
5170799db056SMatthew G. Knepley 
5171799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5172799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5173799db056SMatthew G. Knepley     maxLabels += numBd;
5174799db056SMatthew G. Knepley   }
5175799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5176799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5177799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5178799db056SMatthew G. Knepley     PetscDS  dsBC;
5179799db056SMatthew G. Knepley     PetscInt numBd, bd;
5180799db056SMatthew G. Knepley 
5181799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5182799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5183799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5184799db056SMatthew G. Knepley       DMLabel      label;
5185799db056SMatthew G. Knepley       PetscInt     field;
5186799db056SMatthew G. Knepley       PetscObject  obj;
5187799db056SMatthew G. Knepley       PetscClassId id;
5188799db056SMatthew G. Knepley 
5189799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
51909566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
51919566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5192799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
5193799db056SMatthew G. Knepley       for (l = 0; l < Nl; ++l) if (labels[l] == label) break;
5194799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5195783e2ec8SMatthew G. Knepley     }
5196799db056SMatthew G. Knepley   }
5197799db056SMatthew G. Knepley   /* Get label names */
5198799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5199799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject) labels[l], &names[l]));
5200799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {PetscCall(PetscStrlen(names[l], &len)); maxLen = PetscMax(maxLen, (PetscInt) len+2);}
5201799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5202799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5203799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5204799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen*l], names[l]));
5205799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5206799db056SMatthew G. Knepley   /* Put all names on all processes */
5207799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size+1, &displs));
5208799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5209799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p+1] = displs[p] + counts[p];
5210799db056SMatthew G. Knepley   gNl = displs[size];
5211799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) {counts[p] *= gmaxLen; displs[p] *= gmaxLen;}
5212799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5213799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5214799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5215799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5216799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5217799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l*gmaxLen], &glabels[gl]));
5218799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l*gmaxLen], rank);
5219799db056SMatthew G. Knepley     for (m = 0; m < gl; ++m) if (glabels[m] == glabels[gl]) continue;
52209566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5221799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
52229566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5223799db056SMatthew G. Knepley     ++gl;
5224783e2ec8SMatthew G. Knepley   }
5225799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5226783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5227783e2ec8SMatthew G. Knepley }
5228783e2ec8SMatthew G. Knepley 
5229e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5230e5e52638SMatthew G. Knepley {
5231e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5232e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5233e5e52638SMatthew G. Knepley 
5234e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5235e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
52369566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5237e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5238b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
52399566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5240e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5241e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5242e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5243e5e52638SMatthew G. Knepley }
5244e5e52638SMatthew G. Knepley 
5245e5e52638SMatthew G. Knepley /*@
5246e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5247e5e52638SMatthew G. Knepley 
5248e5e52638SMatthew G. Knepley   Not collective
5249e5e52638SMatthew G. Knepley 
5250e5e52638SMatthew G. Knepley   Input Parameter:
5251e5e52638SMatthew G. Knepley . dm - The DM
5252e5e52638SMatthew G. Knepley 
5253e5e52638SMatthew G. Knepley   Output Parameter:
5254e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5255e5e52638SMatthew G. Knepley 
5256e5e52638SMatthew G. Knepley   Level: intermediate
5257e5e52638SMatthew G. Knepley 
5258db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5259e5e52638SMatthew G. Knepley @*/
5260e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5261e5e52638SMatthew G. Knepley {
5262e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5263e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5264534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5265e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5266e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5267e5e52638SMatthew G. Knepley }
5268e5e52638SMatthew G. Knepley 
5269e5e52638SMatthew G. Knepley /*@
5270e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5271e5e52638SMatthew G. Knepley 
5272d083f849SBarry Smith   Logically collective on dm
5273e5e52638SMatthew G. Knepley 
5274e5e52638SMatthew G. Knepley   Input Parameter:
5275e5e52638SMatthew G. Knepley . dm - The DM
5276e5e52638SMatthew G. Knepley 
5277e5e52638SMatthew G. Knepley   Level: intermediate
5278e5e52638SMatthew G. Knepley 
5279db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5280e5e52638SMatthew G. Knepley @*/
5281e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5282e5e52638SMatthew G. Knepley {
5283e5e52638SMatthew G. Knepley   PetscInt       s;
5284e5e52638SMatthew G. Knepley 
5285e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5286e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5287e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
52889566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
52899566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
52909566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5291e5e52638SMatthew G. Knepley   }
52929566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5293e5e52638SMatthew G. Knepley   dm->probs = NULL;
5294e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5295e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5296e5e52638SMatthew G. Knepley }
5297e5e52638SMatthew G. Knepley 
5298e5e52638SMatthew G. Knepley /*@
5299e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5300e5e52638SMatthew G. Knepley 
5301e5e52638SMatthew G. Knepley   Not collective
5302e5e52638SMatthew G. Knepley 
5303e5e52638SMatthew G. Knepley   Input Parameter:
5304e5e52638SMatthew G. Knepley . dm    - The DM
5305e5e52638SMatthew G. Knepley 
5306e5e52638SMatthew G. Knepley   Output Parameter:
5307e5e52638SMatthew G. Knepley . prob - The default PetscDS
5308e5e52638SMatthew G. Knepley 
5309e5e52638SMatthew G. Knepley   Level: intermediate
5310e5e52638SMatthew G. Knepley 
5311db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5312e5e52638SMatthew G. Knepley @*/
5313e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5314e5e52638SMatthew G. Knepley {
5315e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5316e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5317e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5318b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5319b0143b4dSMatthew G. Knepley     PetscDS ds;
5320b0143b4dSMatthew G. Knepley 
53219566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
53229566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
53239566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5324b0143b4dSMatthew G. Knepley   }
5325b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5326e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5327e5e52638SMatthew G. Knepley }
5328e5e52638SMatthew G. Knepley 
5329e5e52638SMatthew G. Knepley /*@
5330e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5331e5e52638SMatthew G. Knepley 
5332e5e52638SMatthew G. Knepley   Not collective
5333e5e52638SMatthew G. Knepley 
5334e5e52638SMatthew G. Knepley   Input Parameters:
5335e5e52638SMatthew G. Knepley + dm    - The DM
5336e5e52638SMatthew G. Knepley - point - Cell for the DS
5337e5e52638SMatthew G. Knepley 
5338e5e52638SMatthew G. Knepley   Output Parameter:
5339e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5340e5e52638SMatthew G. Knepley 
5341e5e52638SMatthew G. Knepley   Level: developer
5342e5e52638SMatthew G. Knepley 
5343db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5344e5e52638SMatthew G. Knepley @*/
5345e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5346e5e52638SMatthew G. Knepley {
5347e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5348e5e52638SMatthew G. Knepley   PetscInt       s;
5349e5e52638SMatthew G. Knepley 
5350e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5351e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5352e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
535363a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5354e5e52638SMatthew G. Knepley   *prob = NULL;
5355e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5356e5e52638SMatthew G. Knepley     PetscInt val;
5357e5e52638SMatthew G. Knepley 
5358e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5359e5e52638SMatthew G. Knepley     else {
53609566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
5361e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5362e5e52638SMatthew G. Knepley     }
5363e5e52638SMatthew G. Knepley   }
5364e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5365e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5366e5e52638SMatthew G. Knepley }
5367e5e52638SMatthew G. Knepley 
5368e5e52638SMatthew G. Knepley /*@
5369e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5370e5e52638SMatthew G. Knepley 
5371e5e52638SMatthew G. Knepley   Not collective
5372e5e52638SMatthew G. Knepley 
5373e5e52638SMatthew G. Knepley   Input Parameters:
5374e5e52638SMatthew G. Knepley + dm    - The DM
5375e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5376e5e52638SMatthew G. Knepley 
5377b3cf3223SMatthew G. Knepley   Output Parameters:
5378b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5379b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5380e5e52638SMatthew G. Knepley 
5381154ca461SJed Brown   Note:
5382154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5383154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5384154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5385e5e52638SMatthew G. Knepley 
5386e5e52638SMatthew G. Knepley   Level: advanced
5387e5e52638SMatthew G. Knepley 
5388db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5389e5e52638SMatthew G. Knepley @*/
5390b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5391e5e52638SMatthew G. Knepley {
5392e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5393e5e52638SMatthew G. Knepley 
5394e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5395e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5396e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5397b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5398b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5399e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5400154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5401b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5402b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5403154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5404b3cf3223SMatthew G. Knepley     }
5405e5e52638SMatthew G. Knepley   }
54062df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5407e5e52638SMatthew G. Knepley }
5408e5e52638SMatthew G. Knepley 
5409e5e52638SMatthew G. Knepley /*@
5410083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5411083401c6SMatthew G. Knepley 
5412083401c6SMatthew G. Knepley   Collective on dm
5413083401c6SMatthew G. Knepley 
5414083401c6SMatthew G. Knepley   Input Parameters:
5415083401c6SMatthew G. Knepley + dm     - The DM
5416083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5417083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5418083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5419083401c6SMatthew G. Knepley 
5420083401c6SMatthew 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,
5421083401c6SMatthew G. Knepley   the fields argument is ignored.
5422083401c6SMatthew G. Knepley 
5423083401c6SMatthew G. Knepley   Level: advanced
5424083401c6SMatthew G. Knepley 
5425db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5426083401c6SMatthew G. Knepley @*/
5427083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5428083401c6SMatthew G. Knepley {
5429083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5430083401c6SMatthew G. Knepley 
5431083401c6SMatthew G. Knepley   PetscFunctionBegin;
5432083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5433083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5434064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5435083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5436083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
54379566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5438083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5439083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5440083401c6SMatthew G. Knepley     }
5441083401c6SMatthew G. Knepley   }
54429566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds+1));
54439566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
54449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) fields));
54459566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) ds));
5446083401c6SMatthew G. Knepley   if (!label) {
5447083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5448083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5449083401c6SMatthew G. Knepley     Nds = 0;
5450083401c6SMatthew G. Knepley   }
5451083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5452083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5453083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5454083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5455083401c6SMatthew G. Knepley }
5456083401c6SMatthew G. Knepley 
5457083401c6SMatthew G. Knepley /*@
5458e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5459e5e52638SMatthew G. Knepley 
5460e5e52638SMatthew G. Knepley   Not collective
5461e5e52638SMatthew G. Knepley 
5462e5e52638SMatthew G. Knepley   Input Parameters:
5463e5e52638SMatthew G. Knepley + dm  - The DM
5464e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5465e5e52638SMatthew G. Knepley 
5466e5e52638SMatthew G. Knepley   Output Parameters:
5467b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5468b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5469083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5470e5e52638SMatthew G. Knepley 
5471e5e52638SMatthew G. Knepley   Level: advanced
5472e5e52638SMatthew G. Knepley 
5473db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5474e5e52638SMatthew G. Knepley @*/
5475b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5476e5e52638SMatthew G. Knepley {
5477e5e52638SMatthew G. Knepley   PetscInt       Nds;
5478e5e52638SMatthew G. Knepley 
5479e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5480e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54819566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
548263a3b9bcSJacob 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);
5483e5e52638SMatthew G. Knepley   if (label) {
5484e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5485e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5486e5e52638SMatthew G. Knepley   }
5487b3cf3223SMatthew G. Knepley   if (fields) {
5488b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5489b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5490b3cf3223SMatthew G. Knepley   }
5491e5e52638SMatthew G. Knepley   if (ds) {
5492b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5493e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5494e5e52638SMatthew G. Knepley   }
5495e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5496e5e52638SMatthew G. Knepley }
5497e5e52638SMatthew G. Knepley 
5498e5e52638SMatthew G. Knepley /*@
5499083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5500e5e52638SMatthew G. Knepley 
5501083401c6SMatthew G. Knepley   Not collective
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley   Input Parameters:
5504e5e52638SMatthew G. Knepley + dm     - The DM
5505083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5506083401c6SMatthew G. Knepley . label  - The region label, or NULL
5507083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5508083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5509e5e52638SMatthew G. Knepley 
5510e5e52638SMatthew G. Knepley   Level: advanced
5511e5e52638SMatthew G. Knepley 
5512db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5513e5e52638SMatthew G. Knepley @*/
5514083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5515e5e52638SMatthew G. Knepley {
5516083401c6SMatthew G. Knepley   PetscInt       Nds;
5517e5e52638SMatthew G. Knepley 
5518e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5519e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5520083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
55219566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
552263a3b9bcSJacob 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);
55239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) label));
55249566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5525083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5526083401c6SMatthew G. Knepley   if (fields) {
5527083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
55289566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) fields));
55299566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5530083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5531e5e52638SMatthew G. Knepley   }
5532083401c6SMatthew G. Knepley   if (ds) {
5533083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
55349566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject) ds));
55359566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5536083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5537083401c6SMatthew G. Knepley   }
5538e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5539e5e52638SMatthew G. Knepley }
5540e5e52638SMatthew G. Knepley 
5541e5e52638SMatthew G. Knepley /*@
55421d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
55431d3af9e0SMatthew G. Knepley 
55441d3af9e0SMatthew G. Knepley   Not collective
55451d3af9e0SMatthew G. Knepley 
55461d3af9e0SMatthew G. Knepley   Input Parameters:
55471d3af9e0SMatthew G. Knepley + dm  - The DM
55481d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
55491d3af9e0SMatthew G. Knepley 
55501d3af9e0SMatthew G. Knepley   Output Parameter:
55511d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
55521d3af9e0SMatthew G. Knepley 
55531d3af9e0SMatthew G. Knepley   Level: advanced
55541d3af9e0SMatthew G. Knepley 
5555db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
55561d3af9e0SMatthew G. Knepley @*/
55571d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
55581d3af9e0SMatthew G. Knepley {
55591d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
55601d3af9e0SMatthew G. Knepley 
55611d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55621d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55631d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5564dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
55659566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
55661d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
55671d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
55681d3af9e0SMatthew G. Knepley   else          *num = n;
55691d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55701d3af9e0SMatthew G. Knepley }
55711d3af9e0SMatthew G. Knepley 
55722df84da0SMatthew G. Knepley /*@C
55732df84da0SMatthew G. Knepley   DMCreateFEDefault - Create a PetscFE based on the celltype for the mesh
55742df84da0SMatthew G. Knepley 
55752df84da0SMatthew G. Knepley   Not collective
55762df84da0SMatthew G. Knepley 
5577f1a722f8SMatthew G. Knepley   Input Parameters:
55782df84da0SMatthew G. Knepley + dm     - The DM
55792df84da0SMatthew G. Knepley . Nc     - The number of components for the field
55802df84da0SMatthew G. Knepley . prefix - The options prefix for the output PetscFE, or NULL
55812df84da0SMatthew G. Knepley - qorder - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
55822df84da0SMatthew G. Knepley 
55832df84da0SMatthew G. Knepley   Output Parameter:
55842df84da0SMatthew G. Knepley . fem - The PetscFE
55852df84da0SMatthew G. Knepley 
55862df84da0SMatthew G. Knepley   Note: This is a convenience method that just calls PetscFECreateByCell() underneath.
55872df84da0SMatthew G. Knepley 
55882df84da0SMatthew G. Knepley   Level: intermediate
55892df84da0SMatthew G. Knepley 
5590db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
55912df84da0SMatthew G. Knepley @*/
55922df84da0SMatthew G. Knepley PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
55932df84da0SMatthew G. Knepley {
55942df84da0SMatthew G. Knepley   DMPolytopeType ct;
55952df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
55962df84da0SMatthew G. Knepley 
55972df84da0SMatthew G. Knepley   PetscFunctionBegin;
55982df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55992df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
56002df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
56012df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
56022df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
56039566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
56049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
56059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
56069566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
56072df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
56082df84da0SMatthew G. Knepley }
56092df84da0SMatthew G. Knepley 
56101d3af9e0SMatthew G. Knepley /*@
5611e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5612e5e52638SMatthew G. Knepley 
5613d083f849SBarry Smith   Collective on dm
5614e5e52638SMatthew G. Knepley 
5615e5e52638SMatthew G. Knepley   Input Parameter:
5616e5e52638SMatthew G. Knepley . dm - The DM
5617e5e52638SMatthew G. Knepley 
561845480ffeSMatthew G. Knepley   Options Database Keys:
561945480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
562045480ffeSMatthew G. Knepley 
5621e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5622e5e52638SMatthew G. Knepley 
5623e5e52638SMatthew G. Knepley   Level: intermediate
5624e5e52638SMatthew G. Knepley 
5625db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5626e5e52638SMatthew G. Knepley @*/
5627e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5628e5e52638SMatthew G. Knepley {
5629e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5630083401c6SMatthew G. Knepley   PetscDS        dsDef;
5631083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5632f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5633f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5634e5e52638SMatthew G. Knepley 
5635e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5636e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5637e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
56389566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
56399566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5640083401c6SMatthew G. Knepley   /* Determine how many regions we have */
56419566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5642083401c6SMatthew G. Knepley   Nl   = 0;
5643083401c6SMatthew G. Knepley   Ndef = 0;
5644083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5645083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5646083401c6SMatthew G. Knepley     PetscInt l;
5647083401c6SMatthew G. Knepley 
5648f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5649f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5650f918ec44SMatthew G. Knepley     {
5651f918ec44SMatthew G. Knepley       PetscClassId id;
5652f918ec44SMatthew G. Knepley 
56539566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5654f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5655f918ec44SMatthew G. Knepley         Ceed ceed;
5656f918ec44SMatthew G. Knepley 
56579566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
56589566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed));
5659f918ec44SMatthew G. Knepley       }
5660f918ec44SMatthew G. Knepley     }
5661f918ec44SMatthew G. Knepley #endif
5662083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5663083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5664083401c6SMatthew G. Knepley     if (l < Nl) continue;
5665083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5666083401c6SMatthew G. Knepley   }
5667083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
56689566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5669083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5670b3cf3223SMatthew G. Knepley     IS        fields;
5671b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5672b3cf3223SMatthew G. Knepley 
5673b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
56747a8be351SBarry Smith     PetscCheck(nf,comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
56759566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5676b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
56779566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
56789566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
56799566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
56809566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
568188f0c812SMatthew G. Knepley 
56829566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56839566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
56849566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
56859566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
56862df9ee95SMatthew G. Knepley   }
56879566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
56889566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5689083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5690083401c6SMatthew G. Knepley   if (Ndef && Nl) {
56910122748bSMatthew G. Knepley     DM              plex;
5692083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5693083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5694083401c6SMatthew G. Knepley     PetscInt       *fields;
5695083401c6SMatthew G. Knepley     const PetscInt *cells;
5696083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56970122748bSMatthew G. Knepley 
56989566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
56999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
57009566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
57019566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
57025fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5703083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5704083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5705083401c6SMatthew G. Knepley       IS      pointIS;
5706083401c6SMatthew G. Knepley 
57079566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
57089566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
57099566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
57109566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5711083401c6SMatthew G. Knepley     }
57129566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5713083401c6SMatthew G. Knepley 
57149566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
57159566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
57169566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
57179566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
57189566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
57199566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
57209566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5721083401c6SMatthew G. Knepley 
57229566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
5723083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
57249566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
57259566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_"));
57269566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
57279566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5728083401c6SMatthew G. Knepley 
57299566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
57309566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
57319566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
57329566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
57339566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
57349566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
57359566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5736083401c6SMatthew G. Knepley   }
5737083401c6SMatthew G. Knepley   /* Create label DSes
5738083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5739083401c6SMatthew G. Knepley   */
5740083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5741083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5742083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5743083401c6SMatthew G. Knepley     PetscDS   ds;
5744083401c6SMatthew G. Knepley     IS        fields;
5745083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5746083401c6SMatthew G. Knepley 
57479566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
5748083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
57499566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
5750083401c6SMatthew G. Knepley     for (f = 0, nf  = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
57519566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
57529566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
57539566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
57549566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
57559566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
57569566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
57579566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5758083401c6SMatthew G. Knepley     {
5759083401c6SMatthew G. Knepley       DMPolytopeType ct;
5760083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
57615fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
57620122748bSMatthew G. Knepley 
57639566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5764665f567fSMatthew G. Knepley       if (lStart >= 0) {
57659566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5766412e9a14SMatthew G. Knepley         switch (ct) {
5767412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5768412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5769412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5770412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
57715fedec97SMatthew G. Knepley             isCohesiveLocal = PETSC_TRUE;break;
5772083401c6SMatthew G. Knepley           default: break;
5773412e9a14SMatthew G. Knepley         }
5774665f567fSMatthew G. Knepley       }
57759566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
57765fedec97SMatthew G. Knepley       for (f = 0, nf  = 0; f < Nf; ++f) {
57775fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
57785fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
57799566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
57809566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
57815fedec97SMatthew G. Knepley           }
57825fedec97SMatthew G. Knepley           ++nf;
57835fedec97SMatthew G. Knepley         }
57845fedec97SMatthew G. Knepley       }
5785e5e52638SMatthew G. Knepley     }
57869566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5787e5e52638SMatthew G. Knepley   }
57889566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5789e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5790083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5791083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5792083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5793083401c6SMatthew G. Knepley     const PetscInt *fld;
57945fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
57955fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5796e5e52638SMatthew G. Knepley 
57979566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
57989566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
57999566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
58009566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5801083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5802083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
58035fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5804e5e52638SMatthew G. Knepley       PetscClassId id;
5805e5e52638SMatthew G. Knepley 
58065fedec97SMatthew G. Knepley       /* Handle DS with no fields */
58079566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
58085fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
58099566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc));
58109566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5811083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
58129566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5813e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5814e5e52638SMatthew G. Knepley     }
58159566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5816e5e52638SMatthew G. Knepley   }
5817f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
58189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5819f9244615SMatthew G. Knepley   if (flg) {
58203b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
58213b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
58223b4aee56SMatthew G. Knepley       PetscInt Nf, f;
58233b4aee56SMatthew G. Knepley 
58249566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
58259566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
58263b4aee56SMatthew G. Knepley     }
5827f9244615SMatthew G. Knepley   }
5828e5e52638SMatthew G. Knepley   /* Setup DSes */
5829e5e52638SMatthew G. Knepley   if (doSetup) {
58309566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5831e5e52638SMatthew G. Knepley   }
5832e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5833e5e52638SMatthew G. Knepley }
5834e5e52638SMatthew G. Knepley 
5835e5e52638SMatthew G. Knepley /*@
58367f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
58377f96f943SMatthew G. Knepley 
5838f2cacb80SMatthew G. Knepley   Collective on DM
5839f2cacb80SMatthew G. Knepley 
58407f96f943SMatthew G. Knepley   Input Parameters:
58417f96f943SMatthew G. Knepley + dm   - The DM
58427f96f943SMatthew G. Knepley - time - The time
58437f96f943SMatthew G. Knepley 
58447f96f943SMatthew G. Knepley   Output Parameters:
5845f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5846f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
58477f96f943SMatthew G. Knepley 
58487f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
58497f96f943SMatthew G. Knepley 
58507f96f943SMatthew G. Knepley   Level: developer
58517f96f943SMatthew G. Knepley 
5852db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
58537f96f943SMatthew G. Knepley @*/
5854f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
58557f96f943SMatthew G. Knepley {
58567f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
58577f96f943SMatthew G. Knepley   void            **ectxs;
58587f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
58597f96f943SMatthew G. Knepley 
58607f96f943SMatthew G. Knepley   PetscFunctionBegin;
5861f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5862f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5863f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
58649566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
58659566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
58669566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58677f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
58687f96f943SMatthew G. Knepley     PetscDS         ds;
58697f96f943SMatthew G. Knepley     DMLabel         label;
58707f96f943SMatthew G. Knepley     IS              fieldIS;
58717f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58727f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58737f96f943SMatthew G. Knepley 
58749566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
58759566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
58769566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
58779566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
58789566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
5879f2cacb80SMatthew G. Knepley     if (u) {
58807f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58817f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
58829566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
58837f96f943SMatthew G. Knepley       }
58849566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
58857f96f943SMatthew G. Knepley       if (label) {
58869566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
58877f96f943SMatthew G. Knepley       } else {
58889566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
58897f96f943SMatthew G. Knepley       }
58907f96f943SMatthew G. Knepley     }
5891f2cacb80SMatthew G. Knepley     if (u_t) {
58929566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
58939566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
5894f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5895f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
58969566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
5897f2cacb80SMatthew G. Knepley       }
58989566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
5899f2cacb80SMatthew G. Knepley       if (label) {
59009566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5901f2cacb80SMatthew G. Knepley       } else {
59029566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5903f2cacb80SMatthew G. Knepley       }
5904f2cacb80SMatthew G. Knepley     }
5905f2cacb80SMatthew G. Knepley   }
5906f2cacb80SMatthew G. Knepley   if (u) {
59079566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution"));
59089566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u, "exact_"));
5909f2cacb80SMatthew G. Knepley   }
5910f2cacb80SMatthew G. Knepley   if (u_t) {
59119566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative"));
59129566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_"));
5913f2cacb80SMatthew G. Knepley   }
59149566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
59157f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
59167f96f943SMatthew G. Knepley }
59177f96f943SMatthew G. Knepley 
591845480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
591945480ffeSMatthew G. Knepley {
592045480ffeSMatthew G. Knepley   PetscDS        dsNew;
592145480ffeSMatthew G. Knepley   DSBoundary     b;
59226a02485aSMatthew G. Knepley   PetscInt       cdim, Nf, f, d;
59235fedec97SMatthew G. Knepley   PetscBool      isCohesive;
592445480ffeSMatthew G. Knepley   void          *ctx;
592545480ffeSMatthew G. Knepley 
592645480ffeSMatthew G. Knepley   PetscFunctionBegin;
59279566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew));
59289566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
59299566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
59309566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
59319566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
59329566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
593345480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
59349566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
59359566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
59369566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
59379566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
59386a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
59396a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
594045480ffeSMatthew G. Knepley   }
594145480ffeSMatthew G. Knepley   if (Nf) {
59429566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
59439566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
594445480ffeSMatthew G. Knepley   }
59459566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
594645480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
59479566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
594845480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
59497a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
595045480ffeSMatthew G. Knepley   }
595145480ffeSMatthew G. Knepley 
59529566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
59539566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
595445480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
595545480ffeSMatthew G. Knepley }
595645480ffeSMatthew G. Knepley 
59577f96f943SMatthew G. Knepley /*@
5958e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5959e5e52638SMatthew G. Knepley 
5960d083f849SBarry Smith   Collective on dm
5961e5e52638SMatthew G. Knepley 
5962e5e52638SMatthew G. Knepley   Input Parameter:
5963e5e52638SMatthew G. Knepley . dm - The DM
5964e5e52638SMatthew G. Knepley 
5965e5e52638SMatthew G. Knepley   Output Parameter:
5966e5e52638SMatthew G. Knepley . newdm - The DM
5967e5e52638SMatthew G. Knepley 
5968e5e52638SMatthew G. Knepley   Level: advanced
5969e5e52638SMatthew G. Knepley 
5970db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5971e5e52638SMatthew G. Knepley @*/
5972e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5973e5e52638SMatthew G. Knepley {
5974e5e52638SMatthew G. Knepley   PetscInt Nds, s;
5975e5e52638SMatthew G. Knepley 
5976e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5977e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
59789566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
59799566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
5980e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5981e5e52638SMatthew G. Knepley     DMLabel  label;
5982b3cf3223SMatthew G. Knepley     IS       fields;
598345480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5984783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5985e5e52638SMatthew G. Knepley 
59869566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
5987b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
59889566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
598945480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
59909566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
59919566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
5992783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5993b8025e53SMatthew G. Knepley       PetscWeakForm wf;
599445480ffeSMatthew G. Knepley       DMLabel       label;
5995783e2ec8SMatthew G. Knepley       PetscInt      field;
5996783e2ec8SMatthew G. Knepley 
59979566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
59989566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
5999783e2ec8SMatthew G. Knepley     }
6000e5e52638SMatthew G. Knepley   }
6001799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
6002e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6003e5e52638SMatthew G. Knepley }
6004e5e52638SMatthew G. Knepley 
6005e5e52638SMatthew G. Knepley /*@
6006e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
6007e5e52638SMatthew G. Knepley 
6008d083f849SBarry Smith   Collective on dm
6009e5e52638SMatthew G. Knepley 
6010e5e52638SMatthew G. Knepley   Input Parameter:
6011e5e52638SMatthew G. Knepley . dm - The DM
6012e5e52638SMatthew G. Knepley 
6013e5e52638SMatthew G. Knepley   Output Parameter:
6014e5e52638SMatthew G. Knepley . newdm - The DM
6015e5e52638SMatthew G. Knepley 
6016e5e52638SMatthew G. Knepley   Level: advanced
6017e5e52638SMatthew G. Knepley 
6018db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
6019e5e52638SMatthew G. Knepley @*/
6020e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6021e5e52638SMatthew G. Knepley {
6022e5e52638SMatthew G. Knepley   PetscFunctionBegin;
60239566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
60249566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
6025e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6026e5e52638SMatthew G. Knepley }
6027e5e52638SMatthew G. Knepley 
6028b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
6029b64e0483SPeter Brune {
6030b64e0483SPeter Brune   DM dm_coord,dmc_coord;
6031b64e0483SPeter Brune   Vec coords,ccoords;
60326dbf9973SLawrence Mitchell   Mat inject;
6033b64e0483SPeter Brune   PetscFunctionBegin;
60349566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm,&dm_coord));
60359566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmc,&dmc_coord));
60369566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm,&coords));
60379566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dmc,&ccoords));
6038b64e0483SPeter Brune   if (coords && !ccoords) {
60399566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(dmc_coord,&ccoords));
60409566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)ccoords,"coordinates"));
60419566063dSJacob Faibussowitsch     PetscCall(DMCreateInjection(dmc_coord,dm_coord,&inject));
60429566063dSJacob Faibussowitsch     PetscCall(MatRestrict(inject,coords,ccoords));
60439566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&inject));
60449566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinates(dmc,ccoords));
60459566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ccoords));
6046b64e0483SPeter Brune   }
6047b64e0483SPeter Brune   PetscFunctionReturn(0);
6048b64e0483SPeter Brune }
6049b64e0483SPeter Brune 
605003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
605103dadc2fSPeter Brune {
605203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
605303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
605403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
605503dadc2fSPeter Brune   PetscFunctionBegin;
60569566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm,&dm_coord));
60579566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(subdm,&subdm_coord));
60589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm,&coords));
60599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(subdm,&ccoords));
606003dadc2fSPeter Brune   if (coords && !ccoords) {
60619566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(subdm_coord,&ccoords));
60629566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)ccoords,"coordinates"));
60639566063dSJacob Faibussowitsch     PetscCall(DMCreateLocalVector(subdm_coord,&clcoords));
60649566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)clcoords,"coordinates"));
60659566063dSJacob Faibussowitsch     PetscCall(DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g));
60669566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD));
60679566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD));
60689566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD));
60699566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD));
60709566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinates(subdm,ccoords));
60719566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm,clcoords));
60729566063dSJacob Faibussowitsch     PetscCall(VecScatterDestroy(&scat_i[0]));
60739566063dSJacob Faibussowitsch     PetscCall(VecScatterDestroy(&scat_g[0]));
60749566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ccoords));
60759566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&clcoords));
60769566063dSJacob Faibussowitsch     PetscCall(PetscFree(scat_i));
60779566063dSJacob Faibussowitsch     PetscCall(PetscFree(scat_g));
607803dadc2fSPeter Brune   }
607903dadc2fSPeter Brune   PetscFunctionReturn(0);
608003dadc2fSPeter Brune }
608103dadc2fSPeter Brune 
6082c73cfb54SMatthew G. Knepley /*@
6083c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
6084c73cfb54SMatthew G. Knepley 
6085c73cfb54SMatthew G. Knepley   Not collective
6086c73cfb54SMatthew G. Knepley 
6087c73cfb54SMatthew G. Knepley   Input Parameter:
6088c73cfb54SMatthew G. Knepley . dm - The DM
6089c73cfb54SMatthew G. Knepley 
6090c73cfb54SMatthew G. Knepley   Output Parameter:
6091c73cfb54SMatthew G. Knepley . dim - The topological dimension
6092c73cfb54SMatthew G. Knepley 
6093c73cfb54SMatthew G. Knepley   Level: beginner
6094c73cfb54SMatthew G. Knepley 
6095db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6096c73cfb54SMatthew G. Knepley @*/
6097c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6098c73cfb54SMatthew G. Knepley {
6099c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6100c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6101534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6102c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6103c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6104c73cfb54SMatthew G. Knepley }
6105c73cfb54SMatthew G. Knepley 
6106c73cfb54SMatthew G. Knepley /*@
6107c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6108c73cfb54SMatthew G. Knepley 
6109c73cfb54SMatthew G. Knepley   Collective on dm
6110c73cfb54SMatthew G. Knepley 
6111c73cfb54SMatthew G. Knepley   Input Parameters:
6112c73cfb54SMatthew G. Knepley + dm - The DM
6113c73cfb54SMatthew G. Knepley - dim - The topological dimension
6114c73cfb54SMatthew G. Knepley 
6115c73cfb54SMatthew G. Knepley   Level: beginner
6116c73cfb54SMatthew G. Knepley 
6117db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6118c73cfb54SMatthew G. Knepley @*/
6119c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6120c73cfb54SMatthew G. Knepley {
6121e5e52638SMatthew G. Knepley   PetscDS        ds;
612245480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6123f17e8794SMatthew G. Knepley 
6124c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6125c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6126c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6127c73cfb54SMatthew G. Knepley   dm->dim = dim;
6128d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
61299566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
613045480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
61319566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
61329566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
613345480ffeSMatthew G. Knepley     }
6134d17bd122SMatthew G. Knepley   }
6135c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6136c73cfb54SMatthew G. Knepley }
6137c73cfb54SMatthew G. Knepley 
6138793f3fe5SMatthew G. Knepley /*@
6139793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6140793f3fe5SMatthew G. Knepley 
6141d083f849SBarry Smith   Collective on dm
6142793f3fe5SMatthew G. Knepley 
6143793f3fe5SMatthew G. Knepley   Input Parameters:
6144793f3fe5SMatthew G. Knepley + dm - the DM
6145793f3fe5SMatthew G. Knepley - dim - the dimension
6146793f3fe5SMatthew G. Knepley 
6147793f3fe5SMatthew G. Knepley   Output Parameters:
6148793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6149aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6150793f3fe5SMatthew G. Knepley 
6151793f3fe5SMatthew G. Knepley   Note:
6152793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6153a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6154793f3fe5SMatthew G. Knepley   then the interval is empty.
6155793f3fe5SMatthew G. Knepley 
6156793f3fe5SMatthew G. Knepley   Level: intermediate
6157793f3fe5SMatthew G. Knepley 
6158db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6159793f3fe5SMatthew G. Knepley @*/
6160793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6161793f3fe5SMatthew G. Knepley {
6162793f3fe5SMatthew G. Knepley   PetscInt       d;
6163793f3fe5SMatthew G. Knepley 
6164793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6165793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
61677a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
61687a8be351SBarry Smith   PetscCheck(dm->ops->getdimpoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
61699566063dSJacob Faibussowitsch   PetscCall((*dm->ops->getdimpoints)(dm, dim, pStart, pEnd));
6170793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6171793f3fe5SMatthew G. Knepley }
6172793f3fe5SMatthew G. Knepley 
61736636e97aSMatthew G Knepley /*@
61746636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
61756636e97aSMatthew G Knepley 
6176d083f849SBarry Smith   Collective on dm
61776636e97aSMatthew G Knepley 
61786636e97aSMatthew G Knepley   Input Parameters:
61796636e97aSMatthew G Knepley + dm - the DM
61806636e97aSMatthew G Knepley - c - coordinate vector
61816636e97aSMatthew G Knepley 
61822dd40e9bSPatrick Sanan   Notes:
61832dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
61842dd40e9bSPatrick Sanan 
61852dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61866636e97aSMatthew G Knepley 
61876636e97aSMatthew G Knepley   Level: intermediate
61886636e97aSMatthew G Knepley 
6189db781477SPatrick Sanan .seealso: `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMDASetUniformCoordinates()`
61906636e97aSMatthew G Knepley @*/
61916636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
61926636e97aSMatthew G Knepley {
61936636e97aSMatthew G Knepley   PetscFunctionBegin;
61946636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
619510f43869SStefano Zampini   if (c) PetscValidHeaderSpecific(c,VEC_CLASSID,2);
61969566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) c));
61979566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->coordinates));
61986636e97aSMatthew G Knepley   dm->coordinates = c;
61999566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->coordinatesLocal));
62009566063dSJacob Faibussowitsch   PetscCall(DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL));
62019566063dSJacob Faibussowitsch   PetscCall(DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL));
62026636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62036636e97aSMatthew G Knepley }
62046636e97aSMatthew G Knepley 
62056636e97aSMatthew G Knepley /*@
62066636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
62076636e97aSMatthew G Knepley 
62087058e716SVaclav Hapla   Not collective
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:
62156636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
62166636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
62176636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
62186636e97aSMatthew G Knepley 
62192dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
62202dd40e9bSPatrick Sanan 
62216636e97aSMatthew G Knepley   Level: intermediate
62226636e97aSMatthew G Knepley 
6223db781477SPatrick Sanan .seealso: `DMGetCoordinatesLocal()`, `DMSetCoordinates()`, `DMGetCoordinates()`, `DMGetCoordinateDM()`
62246636e97aSMatthew G Knepley @*/
62256636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
62266636e97aSMatthew G Knepley {
62276636e97aSMatthew G Knepley   PetscFunctionBegin;
62286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6229b7f5c055SJed Brown   if (c) PetscValidHeaderSpecific(c,VEC_CLASSID,2);
62309566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) c));
62319566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->coordinatesLocal));
62328865f1eaSKarl Rupp 
62336636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
62348865f1eaSKarl Rupp 
62359566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->coordinates));
62366636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62376636e97aSMatthew G Knepley }
62386636e97aSMatthew G Knepley 
62396636e97aSMatthew G Knepley /*@
62406636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
62416636e97aSMatthew G Knepley 
6242d083f849SBarry Smith   Collective on dm
62436636e97aSMatthew G Knepley 
62446636e97aSMatthew G Knepley   Input Parameter:
62456636e97aSMatthew G Knepley . dm - the DM
62466636e97aSMatthew G Knepley 
62476636e97aSMatthew G Knepley   Output Parameter:
62486636e97aSMatthew G Knepley . c - global coordinate vector
62496636e97aSMatthew G Knepley 
62506636e97aSMatthew G Knepley   Note:
625160c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
625260c22052SBarry Smith   destroyed the array will no longer be valid.
62536636e97aSMatthew G Knepley 
6254959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
62556636e97aSMatthew G Knepley 
62566636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62576636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62586636e97aSMatthew G Knepley 
62596636e97aSMatthew G Knepley   Level: intermediate
62606636e97aSMatthew G Knepley 
6261db781477SPatrick Sanan .seealso: `DMSetCoordinates()`, `DMGetCoordinatesLocal()`, `DMGetCoordinateDM()`, `DMDASetUniformCoordinates()`
62626636e97aSMatthew G Knepley @*/
62636636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
62646636e97aSMatthew G Knepley {
62656636e97aSMatthew G Knepley   PetscFunctionBegin;
62666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62676636e97aSMatthew G Knepley   PetscValidPointer(c,2);
62681f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
62690298fd71SBarry Smith     DM        cdm = NULL;
62701970a576SMatthew G. Knepley     PetscBool localized;
62716636e97aSMatthew G Knepley 
62729566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
62739566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(cdm, &dm->coordinates));
62749566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocalized(dm, &localized));
62751970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
62761970a576SMatthew G. Knepley     if (localized) {
62771970a576SMatthew G. Knepley       PetscInt cdim;
62781970a576SMatthew G. Knepley 
62799566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDim(dm, &cdim));
62809566063dSJacob Faibussowitsch       PetscCall(VecSetBlockSize(dm->coordinates, cdim));
62811970a576SMatthew G. Knepley     }
62829566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) dm->coordinates, "coordinates"));
62839566063dSJacob Faibussowitsch     PetscCall(DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates));
62849566063dSJacob Faibussowitsch     PetscCall(DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates));
62856636e97aSMatthew G Knepley   }
62866636e97aSMatthew G Knepley   *c = dm->coordinates;
62876636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62886636e97aSMatthew G Knepley }
62896636e97aSMatthew G Knepley 
62906636e97aSMatthew G Knepley /*@
629181e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
629281e9a530SVaclav Hapla 
6293d083f849SBarry Smith   Collective on dm
629481e9a530SVaclav Hapla 
629581e9a530SVaclav Hapla   Input Parameter:
629681e9a530SVaclav Hapla . dm - the DM
629781e9a530SVaclav Hapla 
629881e9a530SVaclav Hapla   Level: advanced
629981e9a530SVaclav Hapla 
6300db781477SPatrick Sanan .seealso: `DMGetCoordinatesLocalNoncollective()`
630181e9a530SVaclav Hapla @*/
630281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
630381e9a530SVaclav Hapla {
630481e9a530SVaclav Hapla   PetscFunctionBegin;
630581e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
630681e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
63071970a576SMatthew G. Knepley     DM        cdm = NULL;
63081970a576SMatthew G. Knepley     PetscBool localized;
63091970a576SMatthew G. Knepley 
63109566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
63119566063dSJacob Faibussowitsch     PetscCall(DMCreateLocalVector(cdm, &dm->coordinatesLocal));
63129566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocalized(dm, &localized));
63131970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
63141970a576SMatthew G. Knepley     if (localized) {
63151970a576SMatthew G. Knepley       PetscInt cdim;
63161970a576SMatthew G. Knepley 
63179566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDim(dm, &cdim));
63189566063dSJacob Faibussowitsch       PetscCall(VecSetBlockSize(dm->coordinates, cdim));
63191970a576SMatthew G. Knepley     }
63209566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates"));
63219566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal));
63229566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal));
632381e9a530SVaclav Hapla   }
632481e9a530SVaclav Hapla   PetscFunctionReturn(0);
632581e9a530SVaclav Hapla }
632681e9a530SVaclav Hapla 
632781e9a530SVaclav Hapla /*@
63286636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
63296636e97aSMatthew G Knepley 
6330d083f849SBarry Smith   Collective on dm
63316636e97aSMatthew G Knepley 
63326636e97aSMatthew G Knepley   Input Parameter:
63336636e97aSMatthew G Knepley . dm - the DM
63346636e97aSMatthew G Knepley 
63356636e97aSMatthew G Knepley   Output Parameter:
63366636e97aSMatthew G Knepley . c - coordinate vector
63376636e97aSMatthew G Knepley 
63386636e97aSMatthew G Knepley   Note:
63396636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
63406636e97aSMatthew G Knepley 
63416636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
63426636e97aSMatthew G Knepley 
63436636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63446636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63456636e97aSMatthew G Knepley 
63466636e97aSMatthew G Knepley   Level: intermediate
63476636e97aSMatthew G Knepley 
6348db781477SPatrick Sanan .seealso: `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMSetCoordinates()`, `DMGetCoordinateDM()`, `DMGetCoordinatesLocalNoncollective()`
63496636e97aSMatthew G Knepley @*/
63506636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
63516636e97aSMatthew G Knepley {
63526636e97aSMatthew G Knepley   PetscFunctionBegin;
63536636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63546636e97aSMatthew G Knepley   PetscValidPointer(c,2);
63559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocalSetUp(dm));
63566636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63576636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63586636e97aSMatthew G Knepley }
63596636e97aSMatthew G Knepley 
636081e9a530SVaclav Hapla /*@
636181e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
636281e9a530SVaclav Hapla 
636381e9a530SVaclav Hapla   Not collective
636481e9a530SVaclav Hapla 
636581e9a530SVaclav Hapla   Input Parameter:
636681e9a530SVaclav Hapla . dm - the DM
636781e9a530SVaclav Hapla 
636881e9a530SVaclav Hapla   Output Parameter:
636981e9a530SVaclav Hapla . c - coordinate vector
637081e9a530SVaclav Hapla 
637181e9a530SVaclav Hapla   Level: advanced
637281e9a530SVaclav Hapla 
6373db781477SPatrick Sanan .seealso: `DMGetCoordinatesLocalSetUp()`, `DMGetCoordinatesLocal()`, `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMSetCoordinates()`, `DMGetCoordinateDM()`
637481e9a530SVaclav Hapla @*/
637581e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
637681e9a530SVaclav Hapla {
637781e9a530SVaclav Hapla   PetscFunctionBegin;
637881e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
637981e9a530SVaclav Hapla   PetscValidPointer(c,2);
63807a8be351SBarry Smith   PetscCheck(dm->coordinatesLocal || !dm->coordinates,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
63816636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63826636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63836636e97aSMatthew G Knepley }
63846636e97aSMatthew G Knepley 
63852db98f8dSVaclav Hapla /*@
63862db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
63872db98f8dSVaclav Hapla 
63882db98f8dSVaclav Hapla   Not collective
63892db98f8dSVaclav Hapla 
6390d8d19677SJose E. Roman   Input Parameters:
63912db98f8dSVaclav Hapla + dm - the DM
63922db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
63932db98f8dSVaclav Hapla 
6394d8d19677SJose E. Roman   Output Parameters:
63952db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
63962db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
63972db98f8dSVaclav Hapla 
63982db98f8dSVaclav Hapla   Note:
63992db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
64002db98f8dSVaclav Hapla 
64012db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
64022db98f8dSVaclav Hapla 
64032db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
64042db98f8dSVaclav Hapla 
64052db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
64062db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
64072db98f8dSVaclav Hapla 
64082db98f8dSVaclav Hapla   Level: advanced
64092db98f8dSVaclav Hapla 
6410db781477SPatrick Sanan .seealso: `DMSetCoordinatesLocal()`, `DMGetCoordinatesLocal()`, `DMGetCoordinatesLocalNoncollective()`, `DMGetCoordinatesLocalSetUp()`, `DMGetCoordinates()`, `DMSetCoordinates()`, `DMGetCoordinateDM()`
64112db98f8dSVaclav Hapla @*/
64122db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
64132db98f8dSVaclav Hapla {
64142db98f8dSVaclav Hapla   PetscSection        cs, newcs;
64152db98f8dSVaclav Hapla   Vec                 coords;
64162db98f8dSVaclav Hapla   const PetscScalar   *arr;
64172db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
64182db98f8dSVaclav Hapla   PetscInt            n;
64192db98f8dSVaclav Hapla 
64202db98f8dSVaclav Hapla   PetscFunctionBegin;
64212db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64222db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
64232db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
64242db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
64257a8be351SBarry Smith   PetscCheck(dm->coordinatesLocal,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
64267a8be351SBarry Smith   PetscCheck(dm->coordinateDM && dm->coordinateDM->localSection,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
64271bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
64282db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
64299566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coords, &arr));
64309566063dSJacob Faibussowitsch   PetscCall(PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL));
64319566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coords, &arr));
64322db98f8dSVaclav Hapla   if (pCoord) {
64339566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(newcs, &n));
64342db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
64359566063dSJacob Faibussowitsch     PetscCall(VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord));
64369566063dSJacob Faibussowitsch     PetscCall(VecReplaceArray(*pCoord, newarr));
6437ad9ac99dSVaclav Hapla   } else {
64389566063dSJacob Faibussowitsch     PetscCall(PetscFree(newarr));
64392db98f8dSVaclav Hapla   }
6440ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
64419566063dSJacob Faibussowitsch   else               PetscCall(PetscSectionDestroy(&newcs));
64422db98f8dSVaclav Hapla   PetscFunctionReturn(0);
64432db98f8dSVaclav Hapla }
64442db98f8dSVaclav Hapla 
6445f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6446f19dbd58SToby Isaac {
6447f19dbd58SToby Isaac   PetscFunctionBegin;
6448f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6449f19dbd58SToby Isaac   PetscValidPointer(field,2);
6450f19dbd58SToby Isaac   if (!dm->coordinateField) {
6451f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
64529566063dSJacob Faibussowitsch       PetscCall((*dm->ops->createcoordinatefield)(dm,&dm->coordinateField));
6453f19dbd58SToby Isaac     }
6454f19dbd58SToby Isaac   }
6455f19dbd58SToby Isaac   *field = dm->coordinateField;
6456f19dbd58SToby Isaac   PetscFunctionReturn(0);
6457f19dbd58SToby Isaac }
6458f19dbd58SToby Isaac 
6459f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6460f19dbd58SToby Isaac {
6461f19dbd58SToby Isaac   PetscFunctionBegin;
6462f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6463f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
64649566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)field));
64659566063dSJacob Faibussowitsch   PetscCall(DMFieldDestroy(&dm->coordinateField));
6466f19dbd58SToby Isaac   dm->coordinateField = field;
6467f19dbd58SToby Isaac   PetscFunctionReturn(0);
6468f19dbd58SToby Isaac }
6469f19dbd58SToby Isaac 
64706636e97aSMatthew G Knepley /*@
64711cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
64726636e97aSMatthew G Knepley 
6473d083f849SBarry Smith   Collective on dm
64746636e97aSMatthew G Knepley 
64756636e97aSMatthew G Knepley   Input Parameter:
64766636e97aSMatthew G Knepley . dm - the DM
64776636e97aSMatthew G Knepley 
64786636e97aSMatthew G Knepley   Output Parameter:
64796636e97aSMatthew G Knepley . cdm - coordinate DM
64806636e97aSMatthew G Knepley 
64816636e97aSMatthew G Knepley   Level: intermediate
64826636e97aSMatthew G Knepley 
6483db781477SPatrick Sanan .seealso: `DMSetCoordinateDM()`, `DMSetCoordinates()`, `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`
64846636e97aSMatthew G Knepley @*/
64856636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
64866636e97aSMatthew G Knepley {
64876636e97aSMatthew G Knepley   PetscFunctionBegin;
64886636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64896636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
64906636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6491308f8a94SToby Isaac     DM cdm;
6492308f8a94SToby Isaac 
64937a8be351SBarry Smith     PetscCheck(dm->ops->createcoordinatedm,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
64949566063dSJacob Faibussowitsch     PetscCall((*dm->ops->createcoordinatedm)(dm, &cdm));
64959566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)cdm, "coordinateDM"));
6496308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6497308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
64989566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&dm->coordinateDM));
6499308f8a94SToby Isaac     dm->coordinateDM = cdm;
65006636e97aSMatthew G Knepley   }
65016636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
65026636e97aSMatthew G Knepley   PetscFunctionReturn(0);
65036636e97aSMatthew G Knepley }
6504e87bb0d3SMatthew G Knepley 
65051cfe2091SMatthew G. Knepley /*@
65061cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
65071cfe2091SMatthew G. Knepley 
6508d083f849SBarry Smith   Logically Collective on dm
65091cfe2091SMatthew G. Knepley 
65101cfe2091SMatthew G. Knepley   Input Parameters:
65111cfe2091SMatthew G. Knepley + dm - the DM
65121cfe2091SMatthew G. Knepley - cdm - coordinate DM
65131cfe2091SMatthew G. Knepley 
65141cfe2091SMatthew G. Knepley   Level: intermediate
65151cfe2091SMatthew G. Knepley 
6516db781477SPatrick Sanan .seealso: `DMGetCoordinateDM()`, `DMSetCoordinates()`, `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`
65171cfe2091SMatthew G. Knepley @*/
65181cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
65191cfe2091SMatthew G. Knepley {
65201cfe2091SMatthew G. Knepley   PetscFunctionBegin;
65211cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65221cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
65239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
65249566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coordinateDM));
65251cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
65261cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
65271cfe2091SMatthew G. Knepley }
65281cfe2091SMatthew G. Knepley 
652946e270d4SMatthew G. Knepley /*@
653046e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
653146e270d4SMatthew G. Knepley 
653246e270d4SMatthew G. Knepley   Not Collective
653346e270d4SMatthew G. Knepley 
653446e270d4SMatthew G. Knepley   Input Parameter:
653546e270d4SMatthew G. Knepley . dm - The DM object
653646e270d4SMatthew G. Knepley 
653746e270d4SMatthew G. Knepley   Output Parameter:
653846e270d4SMatthew G. Knepley . dim - The embedding dimension
653946e270d4SMatthew G. Knepley 
654046e270d4SMatthew G. Knepley   Level: intermediate
654146e270d4SMatthew G. Knepley 
6542db781477SPatrick Sanan .seealso: `DMSetCoordinateDim()`, `DMGetCoordinateSection()`, `DMGetCoordinateDM()`, `DMGetLocalSection()`, `DMSetLocalSection()`
654346e270d4SMatthew G. Knepley @*/
654446e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
654546e270d4SMatthew G. Knepley {
654646e270d4SMatthew G. Knepley   PetscFunctionBegin;
654746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6548534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
65499a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
65509a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
65519a9a41abSToby Isaac   }
655246e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
655346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
655446e270d4SMatthew G. Knepley }
655546e270d4SMatthew G. Knepley 
655646e270d4SMatthew G. Knepley /*@
655746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
655846e270d4SMatthew G. Knepley 
655946e270d4SMatthew G. Knepley   Not Collective
656046e270d4SMatthew G. Knepley 
656146e270d4SMatthew G. Knepley   Input Parameters:
656246e270d4SMatthew G. Knepley + dm  - The DM object
656346e270d4SMatthew G. Knepley - dim - The embedding dimension
656446e270d4SMatthew G. Knepley 
656546e270d4SMatthew G. Knepley   Level: intermediate
656646e270d4SMatthew G. Knepley 
6567db781477SPatrick Sanan .seealso: `DMGetCoordinateDim()`, `DMSetCoordinateSection()`, `DMGetCoordinateSection()`, `DMGetLocalSection()`, `DMSetLocalSection()`
656846e270d4SMatthew G. Knepley @*/
656946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
657046e270d4SMatthew G. Knepley {
6571e5e52638SMatthew G. Knepley   PetscDS        ds;
657245480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6573f17e8794SMatthew G. Knepley 
657446e270d4SMatthew G. Knepley   PetscFunctionBegin;
657546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
657646e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6577d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
65789566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
657945480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
65809566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
65819566063dSJacob Faibussowitsch       PetscCall(PetscDSSetCoordinateDimension(ds, dim));
658245480ffeSMatthew G. Knepley     }
6583d17bd122SMatthew G. Knepley   }
658446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
658546e270d4SMatthew G. Knepley }
658646e270d4SMatthew G. Knepley 
6587e8abe2deSMatthew G. Knepley /*@
6588e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6589e8abe2deSMatthew G. Knepley 
6590d083f849SBarry Smith   Collective on dm
6591e8abe2deSMatthew G. Knepley 
6592e8abe2deSMatthew G. Knepley   Input Parameter:
6593e8abe2deSMatthew G. Knepley . dm - The DM object
6594e8abe2deSMatthew G. Knepley 
6595e8abe2deSMatthew G. Knepley   Output Parameter:
6596e8abe2deSMatthew G. Knepley . section - The PetscSection object
6597e8abe2deSMatthew G. Knepley 
6598e8abe2deSMatthew G. Knepley   Level: intermediate
6599e8abe2deSMatthew G. Knepley 
6600db781477SPatrick Sanan .seealso: `DMGetCoordinateDM()`, `DMGetLocalSection()`, `DMSetLocalSection()`
6601e8abe2deSMatthew G. Knepley @*/
6602e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6603e8abe2deSMatthew G. Knepley {
6604e8abe2deSMatthew G. Knepley   DM             cdm;
6605e8abe2deSMatthew G. Knepley 
6606e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6607e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6608e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
66099566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
66109566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(cdm, section));
6611e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6612e8abe2deSMatthew G. Knepley }
6613e8abe2deSMatthew G. Knepley 
6614e8abe2deSMatthew G. Knepley /*@
6615e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6616e8abe2deSMatthew G. Knepley 
6617e8abe2deSMatthew G. Knepley   Not Collective
6618e8abe2deSMatthew G. Knepley 
6619e8abe2deSMatthew G. Knepley   Input Parameters:
6620e8abe2deSMatthew G. Knepley + dm      - The DM object
662146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6622e8abe2deSMatthew G. Knepley - section - The PetscSection object
6623e8abe2deSMatthew G. Knepley 
6624e8abe2deSMatthew G. Knepley   Level: intermediate
6625e8abe2deSMatthew G. Knepley 
6626db781477SPatrick Sanan .seealso: `DMGetCoordinateSection()`, `DMGetLocalSection()`, `DMSetLocalSection()`
6627e8abe2deSMatthew G. Knepley @*/
662846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6629e8abe2deSMatthew G. Knepley {
6630e8abe2deSMatthew G. Knepley   DM             cdm;
6631e8abe2deSMatthew G. Knepley 
6632e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6633e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
663446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
66359566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
66369566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(cdm, section));
663746e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
66384c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
663946e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
664046e270d4SMatthew G. Knepley 
66419566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
66429566063dSJacob Faibussowitsch     PetscCall(DMGetDimPoints(dm, 0, &vStart, &vEnd));
664346e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
664446e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
664546e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
66469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(section, v, &dd));
664746e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
664846e270d4SMatthew G. Knepley     }
66499566063dSJacob Faibussowitsch     if (d >= 0) PetscCall(DMSetCoordinateDim(dm, d));
665046e270d4SMatthew G. Knepley   }
6651e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6652e8abe2deSMatthew G. Knepley }
6653e8abe2deSMatthew G. Knepley 
6654d864a3eaSLisandro Dalcin /*@
6655d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6656d864a3eaSLisandro Dalcin 
6657d864a3eaSLisandro Dalcin   Input Parameters:
6658d864a3eaSLisandro Dalcin + dm      - The DM object
66594f9ab2b4SJed Brown - disc    - The new coordinate discretization or NULL to ensure a coordinate discretization exists
6660d864a3eaSLisandro Dalcin 
6661d864a3eaSLisandro Dalcin   Level: intermediate
6662d864a3eaSLisandro Dalcin 
6663db781477SPatrick Sanan .seealso: `DMGetCoordinateField()`
6664d864a3eaSLisandro Dalcin @*/
6665d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6666d864a3eaSLisandro Dalcin {
66674f9ab2b4SJed Brown   PetscFE        discOld;
6668d864a3eaSLisandro Dalcin   PetscClassId   classid;
6669d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6670d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6671d864a3eaSLisandro Dalcin   Mat            matInterp;
66724f9ab2b4SJed Brown   PetscBool      same_space = PETSC_TRUE;
6673d864a3eaSLisandro Dalcin 
6674d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6675d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66764f9ab2b4SJed Brown   if (disc) PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6677d864a3eaSLisandro Dalcin 
66789566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdmOld));
6679d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
66809566063dSJacob Faibussowitsch   PetscCall(DMGetField(cdmOld, 0, NULL, (PetscObject*)&discOld));
66819566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId((PetscObject)discOld, &classid));
668229ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
668329ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
668429ad44c5SMatthew G. Knepley       PetscFE        feLinear;
668529ad44c5SMatthew G. Knepley       DMPolytopeType ct;
66868c4475acSStefano Zampini       PetscInt       dim, dE, cStart, cEnd;
668729ad44c5SMatthew G. Knepley       PetscBool      simplex;
668829ad44c5SMatthew G. Knepley 
668929ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
66909566063dSJacob Faibussowitsch       PetscCall(DMGetDimension(dm, &dim));
66919566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDim(dm, &dE));
66928c4475acSStefano Zampini       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
66938c4475acSStefano Zampini       if (cEnd > cStart) {
66949566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, cStart, &ct));
669529ad44c5SMatthew G. Knepley         switch (ct) {
669629ad44c5SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM:
669729ad44c5SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
669829ad44c5SMatthew G. Knepley             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
669929ad44c5SMatthew G. Knepley           default: break;
670029ad44c5SMatthew G. Knepley         }
67018c4475acSStefano Zampini       }
67028c4475acSStefano Zampini       PetscCall(DMPlexIsSimplex(dm, &simplex));
67039566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear));
67049566063dSJacob Faibussowitsch       PetscCall(DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear));
67059566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&feLinear));
67069566063dSJacob Faibussowitsch       PetscCall(DMCreateDS(cdmOld));
67079566063dSJacob Faibussowitsch       PetscCall(DMGetField(cdmOld, 0, NULL, (PetscObject*)&discOld));
670829ad44c5SMatthew G. Knepley     } else {
670929ad44c5SMatthew G. Knepley       const char *discname;
671029ad44c5SMatthew G. Knepley 
67119566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetType((PetscObject)discOld, &discname));
67124f9ab2b4SJed Brown       SETERRQ(PetscObjectComm((PetscObject)discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
671329ad44c5SMatthew G. Knepley     }
671429ad44c5SMatthew G. Knepley   }
67154f9ab2b4SJed Brown   if (!disc) PetscFunctionReturn(0);
67164f9ab2b4SJed Brown   { // Check if the new space is the same as the old modulo quadrature
67174f9ab2b4SJed Brown     PetscDualSpace dsOld, ds;
67189566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(discOld, &dsOld));
67199566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(disc, &ds));
67209566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceEqual(dsOld, ds, &same_space));
67214f9ab2b4SJed Brown   }
6722d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
67239566063dSJacob Faibussowitsch   PetscCall(DMClone(cdmOld, &cdmNew));
67249566063dSJacob Faibussowitsch   PetscCall(DMSetField(cdmNew, 0, NULL, (PetscObject) disc));
67259566063dSJacob Faibussowitsch   PetscCall(DMCreateDS(cdmNew));
67269566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm, &coordsOld));
67274f9ab2b4SJed Brown   if (same_space) {
67289566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coordsOld));
67294f9ab2b4SJed Brown     coordsNew = coordsOld;
67304f9ab2b4SJed Brown   } else { // Project the coordinate vector from old to new space
67319566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(cdmNew, &coordsNew));
67329566063dSJacob Faibussowitsch     PetscCall(DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL));
67339566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(matInterp, coordsOld, coordsNew));
67349566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&matInterp));
67354f9ab2b4SJed Brown   }
6736d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
67379566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateField(dm, NULL));
67389566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, cdmNew));
67399566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinates(dm, coordsNew));
67409566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordsNew));
67419566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&cdmNew));
6742d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6743d864a3eaSLisandro Dalcin }
6744d864a3eaSLisandro Dalcin 
67455dc8c3f7SMatthew G. Knepley /*@C
674690b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
67475dc8c3f7SMatthew G. Knepley 
6748f899ff85SJose E. Roman   Input Parameter:
674990b157c4SStefano Zampini . dm      - The DM object
675090b157c4SStefano Zampini 
675190b157c4SStefano Zampini   Output Parameters:
675290b157c4SStefano Zampini + per     - Whether the DM is periodic or not
67535dc8c3f7SMatthew 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
67545dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67555dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67565dc8c3f7SMatthew G. Knepley 
67575dc8c3f7SMatthew G. Knepley   Level: developer
67585dc8c3f7SMatthew G. Knepley 
6759db781477SPatrick Sanan .seealso: `DMGetPeriodicity()`
67605dc8c3f7SMatthew G. Knepley @*/
676190b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6762c6b900c6SMatthew G. Knepley {
6763c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6764c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
676590b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6766c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6767c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
67685dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6769c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6770c6b900c6SMatthew G. Knepley }
6771c6b900c6SMatthew G. Knepley 
67725dc8c3f7SMatthew G. Knepley /*@C
67735dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
67745dc8c3f7SMatthew G. Knepley 
67755dc8c3f7SMatthew G. Knepley   Input Parameters:
67765dc8c3f7SMatthew G. Knepley + dm      - The DM object
6777db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6778db2bf62eSStefano 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.
67795dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67805dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67815dc8c3f7SMatthew G. Knepley 
6782db2bf62eSStefano 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.
6783db2bf62eSStefano Zampini 
67845dc8c3f7SMatthew G. Knepley   Level: developer
67855dc8c3f7SMatthew G. Knepley 
6786db781477SPatrick Sanan .seealso: `DMGetPeriodicity()`
67875dc8c3f7SMatthew G. Knepley @*/
678890b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6789c6b900c6SMatthew G. Knepley {
6790c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6791c6b900c6SMatthew G. Knepley 
6792c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6793c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
679490b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6795412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6796412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6797412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
67989566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
679990b157c4SStefano Zampini   if (maxCell) {
68009566063dSJacob Faibussowitsch     if (!dm->maxCell) PetscCall(PetscMalloc1(dim, &dm->maxCell));
6801412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6802db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
68039566063dSJacob Faibussowitsch     PetscCall(PetscFree(dm->maxCell));
6804412e9a14SMatthew G. Knepley   }
6805db2bf62eSStefano Zampini 
6806412e9a14SMatthew G. Knepley   if (L) {
68079566063dSJacob Faibussowitsch     if (!dm->L) PetscCall(PetscMalloc1(dim, &dm->L));
6808412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6809412e9a14SMatthew G. Knepley   }
6810412e9a14SMatthew G. Knepley   if (bd) {
68119566063dSJacob Faibussowitsch     if (!dm->bdtype) PetscCall(PetscMalloc1(dim, &dm->bdtype));
6812412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
681390b157c4SStefano Zampini   }
6814072d7d67SStefano Zampini   dm->periodic = per;
6815c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6816c6b900c6SMatthew G. Knepley }
6817c6b900c6SMatthew G. Knepley 
68182e17dfb7SMatthew G. Knepley /*@
68192e17dfb7SMatthew 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.
68202e17dfb7SMatthew G. Knepley 
68212e17dfb7SMatthew G. Knepley   Input Parameters:
68222e17dfb7SMatthew G. Knepley + dm     - The DM
682365da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
682465da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
68252e17dfb7SMatthew G. Knepley 
68262e17dfb7SMatthew G. Knepley   Output Parameter:
68272e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68282e17dfb7SMatthew G. Knepley 
68292e17dfb7SMatthew G. Knepley   Level: developer
68302e17dfb7SMatthew G. Knepley 
6831db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMLocalizeAddCoordinate()`
68322e17dfb7SMatthew G. Knepley @*/
683365da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
68342e17dfb7SMatthew G. Knepley {
68352e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
68362e17dfb7SMatthew G. Knepley 
68372e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
68392e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68402e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68412e17dfb7SMatthew G. Knepley   } else {
684265da65dcSMatthew G. Knepley     if (endpoint) {
684365da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6844da3333bfSMatthew 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)) {
6845da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
684665da65dcSMatthew G. Knepley         } else {
6847da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
684865da65dcSMatthew G. Knepley         }
684965da65dcSMatthew G. Knepley       }
685065da65dcSMatthew G. Knepley     } else {
68512e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
68521118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
68532e17dfb7SMatthew G. Knepley       }
68542e17dfb7SMatthew G. Knepley     }
685565da65dcSMatthew G. Knepley   }
68562e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68572e17dfb7SMatthew G. Knepley }
68582e17dfb7SMatthew G. Knepley 
68592e17dfb7SMatthew G. Knepley /*
68602e17dfb7SMatthew 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.
68612e17dfb7SMatthew G. Knepley 
68622e17dfb7SMatthew G. Knepley   Input Parameters:
68632e17dfb7SMatthew G. Knepley + dm     - The DM
68642e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68652e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68662e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
68672e17dfb7SMatthew G. Knepley 
68682e17dfb7SMatthew G. Knepley   Output Parameter:
68692e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68702e17dfb7SMatthew G. Knepley 
68712e17dfb7SMatthew G. Knepley   Level: developer
68722e17dfb7SMatthew G. Knepley 
68732e17dfb7SMatthew 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
68742e17dfb7SMatthew G. Knepley 
6875db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMLocalizeAddCoordinate()`
68762e17dfb7SMatthew G. Knepley */
68772e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
68782e17dfb7SMatthew G. Knepley {
68792e17dfb7SMatthew G. Knepley   PetscInt d;
68802e17dfb7SMatthew G. Knepley 
68812e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68822e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68832e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68842e17dfb7SMatthew G. Knepley   } else {
68852e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6886908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
68872e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
68882e17dfb7SMatthew G. Knepley       } else {
68892e17dfb7SMatthew G. Knepley         out[d] = in[d];
68902e17dfb7SMatthew G. Knepley       }
68912e17dfb7SMatthew G. Knepley     }
68922e17dfb7SMatthew G. Knepley   }
68932e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68942e17dfb7SMatthew G. Knepley }
6895a5801f52SStefano Zampini 
68962e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
68972e17dfb7SMatthew G. Knepley {
68982e17dfb7SMatthew G. Knepley   PetscInt d;
68992e17dfb7SMatthew G. Knepley 
69002e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69012e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69022e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
69032e17dfb7SMatthew G. Knepley   } else {
69042e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6905908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
69062e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
69072e17dfb7SMatthew G. Knepley       } else {
69082e17dfb7SMatthew G. Knepley         out[d] = in[d];
69092e17dfb7SMatthew G. Knepley       }
69102e17dfb7SMatthew G. Knepley     }
69112e17dfb7SMatthew G. Knepley   }
69122e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69132e17dfb7SMatthew G. Knepley }
69142e17dfb7SMatthew G. Knepley 
69152e17dfb7SMatthew G. Knepley /*
69162e17dfb7SMatthew 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.
69172e17dfb7SMatthew G. Knepley 
69182e17dfb7SMatthew G. Knepley   Input Parameters:
69192e17dfb7SMatthew G. Knepley + dm     - The DM
69202e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
69212e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
69222e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
69232e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
69242e17dfb7SMatthew G. Knepley 
69252e17dfb7SMatthew G. Knepley   Output Parameter:
69262e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
69272e17dfb7SMatthew G. Knepley 
69282e17dfb7SMatthew G. Knepley   Level: developer
69292e17dfb7SMatthew G. Knepley 
69302e17dfb7SMatthew 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
69312e17dfb7SMatthew G. Knepley 
6932db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMLocalizeCoordinate()`
69332e17dfb7SMatthew G. Knepley */
69342e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
69352e17dfb7SMatthew G. Knepley {
69362e17dfb7SMatthew G. Knepley   PetscInt d;
69372e17dfb7SMatthew G. Knepley 
69382e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69392e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69402e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
69412e17dfb7SMatthew G. Knepley   } else {
69422e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6943412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6944412e9a14SMatthew G. Knepley 
6945412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6946412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6947412e9a14SMatthew G. Knepley 
6948412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
694963a3b9bcSJacob 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]));
6950412e9a14SMatthew G. Knepley         out[d] += newCoord;
69512e17dfb7SMatthew G. Knepley       } else {
69522e17dfb7SMatthew G. Knepley         out[d] += in[d];
69532e17dfb7SMatthew G. Knepley       }
69542e17dfb7SMatthew G. Knepley     }
69552e17dfb7SMatthew G. Knepley   }
69562e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69572e17dfb7SMatthew G. Knepley }
69582e17dfb7SMatthew G. Knepley 
695936447a5eSToby Isaac /*@
69608f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
69618f700142SStefano Zampini 
69628f700142SStefano Zampini   Not collective
696336447a5eSToby Isaac 
696436447a5eSToby Isaac   Input Parameter:
696536447a5eSToby Isaac . dm - The DM
696636447a5eSToby Isaac 
696736447a5eSToby Isaac   Output Parameter:
696836447a5eSToby Isaac   areLocalized - True if localized
696936447a5eSToby Isaac 
697036447a5eSToby Isaac   Level: developer
697136447a5eSToby Isaac 
6972db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMGetCoordinatesLocalized()`, `DMSetPeriodicity()`
697336447a5eSToby Isaac @*/
69748f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
697536447a5eSToby Isaac {
697636447a5eSToby Isaac   DM             cdm;
697736447a5eSToby Isaac   PetscSection   coordSection;
69782ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
697946a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
698036447a5eSToby Isaac 
698136447a5eSToby Isaac   PetscFunctionBegin;
698236447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6983534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
69848b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
698546a3a80fSLisandro Dalcin 
698636447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
69879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
69889566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex));
69899f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
69909566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(cdm, &depth));
69912ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
699246a3a80fSLisandro Dalcin 
69939566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
69949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd));
69959566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
699636447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
699746a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
699846a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
69999566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, c, &dof));
700046a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
700136447a5eSToby Isaac   }
70028f700142SStefano Zampini   *areLocalized = alreadyLocalized;
700336447a5eSToby Isaac   PetscFunctionReturn(0);
700436447a5eSToby Isaac }
700536447a5eSToby Isaac 
70068f700142SStefano Zampini /*@
70078f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
70088f700142SStefano Zampini 
70098f700142SStefano Zampini   Collective on dm
70108f700142SStefano Zampini 
70118f700142SStefano Zampini   Input Parameter:
70128f700142SStefano Zampini . dm - The DM
70138f700142SStefano Zampini 
70148f700142SStefano Zampini   Output Parameter:
70158f700142SStefano Zampini   areLocalized - True if localized
70168f700142SStefano Zampini 
70178f700142SStefano Zampini   Level: developer
70188f700142SStefano Zampini 
7019db781477SPatrick Sanan .seealso: `DMLocalizeCoordinates()`, `DMSetPeriodicity()`, `DMGetCoordinatesLocalizedLocal()`
70208f700142SStefano Zampini @*/
70218f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
70228f700142SStefano Zampini {
70238f700142SStefano Zampini   PetscBool      localized;
70248f700142SStefano Zampini 
70258f700142SStefano Zampini   PetscFunctionBegin;
70268f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7027534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
70289566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocalizedLocal(dm,&localized));
70291c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm)));
70308f700142SStefano Zampini   PetscFunctionReturn(0);
70318f700142SStefano Zampini }
703236447a5eSToby Isaac 
70332e17dfb7SMatthew G. Knepley /*@
7034492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
70352e17dfb7SMatthew G. Knepley 
70368f700142SStefano Zampini   Collective on dm
70378f700142SStefano Zampini 
70382e17dfb7SMatthew G. Knepley   Input Parameter:
70392e17dfb7SMatthew G. Knepley . dm - The DM
70402e17dfb7SMatthew G. Knepley 
70412e17dfb7SMatthew G. Knepley   Level: developer
70422e17dfb7SMatthew G. Knepley 
7043db781477SPatrick Sanan .seealso: `DMSetPeriodicity()`, `DMLocalizeCoordinate()`, `DMLocalizeAddCoordinate()`
70442e17dfb7SMatthew G. Knepley @*/
70452e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
70462e17dfb7SMatthew G. Knepley {
70472e17dfb7SMatthew G. Knepley   DM             cdm;
70482e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
70492e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
70503e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
70513e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
7052e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
70533e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
70543e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
70552e17dfb7SMatthew G. Knepley 
70562e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70572e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
705892c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
70599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocalized(dm, &alreadyLocalized));
7060f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
7061f7cbd40bSStefano Zampini 
70622e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
70639566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
70642e17dfb7SMatthew G. Knepley   {
70652e17dfb7SMatthew G. Knepley     PetscBool isplex;
70662e17dfb7SMatthew G. Knepley 
70679566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex));
70682e17dfb7SMatthew G. Knepley     if (isplex) {
70699566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd));
70709566063dSJacob Faibussowitsch       PetscCall(DMPlexGetMaxProjectionHeight(cdm,&maxHeight));
70719566063dSJacob Faibussowitsch       PetscCall(DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
70723e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
70733e922f36SToby Isaac       newStart = vStart;
70743e922f36SToby Isaac       newEnd   = vEnd;
70753e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
70769566063dSJacob Faibussowitsch         PetscCall(DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]));
70773e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
70783e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
70793e922f36SToby Isaac       }
70802e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
70812e17dfb7SMatthew G. Knepley   }
70829566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
70837a8be351SBarry Smith   PetscCheck(coordinates,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
70849566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
70859566063dSJacob Faibussowitsch   PetscCall(VecGetBlockSize(coordinates, &bs));
70869566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection,&sStart,&sEnd));
70873e922f36SToby Isaac 
70889566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection));
70899566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(cSection, 1));
70909566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &Nc));
70919566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(cSection, 0, Nc));
70929566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(cSection, newStart, newEnd));
70933e922f36SToby Isaac 
70949566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
70953e922f36SToby Isaac   localized = &anchor[bs];
70963e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
70973e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
70983e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
70993e922f36SToby Isaac 
71003e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
71013e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
71023e922f36SToby Isaac       PetscInt     b;
71033e922f36SToby Isaac 
71043e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
71059566063dSJacob Faibussowitsch       PetscCall(DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71063e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71073e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
71089566063dSJacob Faibussowitsch         PetscCall(DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized));
71093e922f36SToby Isaac         for (b = 0; b < bs; b++) {
71103e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
71113e922f36SToby Isaac         }
71123e922f36SToby Isaac         if (b < bs) break;
71133e922f36SToby Isaac       }
71143e922f36SToby Isaac       if (d < dof/bs) {
71153e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
71163e922f36SToby Isaac           PetscInt cdof;
71173e922f36SToby Isaac 
71189566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(coordSection, c, &cdof));
71193e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
71203e922f36SToby Isaac         }
71219566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetDof(cSection, c, dof));
71229566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldDof(cSection, c, 0, dof));
71233e922f36SToby Isaac       }
71249566063dSJacob Faibussowitsch       PetscCall(DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71253e922f36SToby Isaac     }
71263e922f36SToby Isaac   }
71279566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm)));
71283e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
71299566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
71309566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&cSection));
71319566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
71323e922f36SToby Isaac     PetscFunctionReturn(0);
71333e922f36SToby Isaac   }
71342e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
71359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
71369566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(cSection, v, dof));
71379566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(cSection, v, 0, dof));
71382e17dfb7SMatthew G. Knepley   }
71399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(cSection));
71409566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(cSection, &coordSize));
71419566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &cVec));
71429566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)cVec,"coordinates"));
71439566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(cVec, bs));
71449566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(cVec, coordSize, PETSC_DETERMINE));
71459566063dSJacob Faibussowitsch   PetscCall(VecSetType(cVec, VECSTANDARD));
71469566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(coordinates, (const PetscScalar**)&coords));
71479566063dSJacob Faibussowitsch   PetscCall(VecGetArray(cVec, &coords2));
71482e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
71499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
71509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
71519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cSection,     v, &off2));
71522e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
71532e17dfb7SMatthew G. Knepley   }
71543e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71553e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71563e922f36SToby Isaac 
71572e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
71582e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
71593e922f36SToby Isaac       PetscInt     b, cdof;
71602e17dfb7SMatthew G. Knepley 
71619566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSection,c,&cdof));
71623e922f36SToby Isaac       if (!cdof) continue;
71639566063dSJacob Faibussowitsch       PetscCall(DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71649566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(cSection, c, &off2));
71652e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71669566063dSJacob Faibussowitsch       for (d = 0; d < dof/bs; ++d) PetscCall(DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]));
71679566063dSJacob Faibussowitsch       PetscCall(DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71682e17dfb7SMatthew G. Knepley     }
71693e922f36SToby Isaac   }
71709566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
71719566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
71729566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords));
71739566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(cVec, &coords2));
71749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection));
71759566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, cVec));
71769566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&cVec));
71779566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&cSection));
71782e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
71792e17dfb7SMatthew G. Knepley }
71802e17dfb7SMatthew G. Knepley 
7181e87bb0d3SMatthew G Knepley /*@
71823a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7183e87bb0d3SMatthew G Knepley 
7184d083f849SBarry Smith   Collective on v (see explanation below)
7185e87bb0d3SMatthew G Knepley 
7186e87bb0d3SMatthew G Knepley   Input Parameters:
7187e87bb0d3SMatthew G Knepley + dm - The DM
71886b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7189e87bb0d3SMatthew G Knepley 
71906b867d5aSJose E. Roman   Input/Output Parameters:
71916b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
71926b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
71936b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
71943a93e3b7SToby Isaac 
7195e87bb0d3SMatthew G Knepley   Level: developer
719661e3bb9bSMatthew G Knepley 
719762a38674SMatthew G. Knepley   Notes:
71983a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
719962a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
72003a93e3b7SToby Isaac 
72013a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
720262a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
72033a93e3b7SToby Isaac 
72043a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
72053a93e3b7SToby Isaac 
720662a38674SMatthew G. Knepley $    const PetscSFNode *cells;
720762a38674SMatthew G. Knepley $    PetscInt           nFound;
7208a6216909SToby Isaac $    const PetscInt    *found;
720962a38674SMatthew G. Knepley $
7210a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
72113a93e3b7SToby Isaac 
72123a93e3b7SToby 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
72133a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
72143a93e3b7SToby Isaac 
7215db781477SPatrick Sanan .seealso: `DMSetCoordinates()`, `DMSetCoordinatesLocal()`, `DMGetCoordinates()`, `DMGetCoordinatesLocal()`, `DMPointLocationType`
721661e3bb9bSMatthew G Knepley @*/
721762a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7218e87bb0d3SMatthew G Knepley {
7219e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7220e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7221e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7222e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
72233a93e3b7SToby Isaac   if (*cellSF) {
72243a93e3b7SToby Isaac     PetscMPIInt result;
72253a93e3b7SToby Isaac 
7226e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
72279566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result));
72287a8be351SBarry Smith     PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
7229e0fc9d1bSMatthew G. Knepley   } else {
72309566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF));
72313a93e3b7SToby Isaac   }
72327a8be351SBarry Smith   PetscCheck(dm->ops->locatepoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
72339566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_LocatePoints,dm,0,0,0));
72349566063dSJacob Faibussowitsch   PetscCall((*dm->ops->locatepoints)(dm,v,ltype,*cellSF));
72359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_LocatePoints,dm,0,0,0));
7236e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7237e87bb0d3SMatthew G Knepley }
723814f150ffSMatthew G. Knepley 
7239f4d763aaSMatthew G. Knepley /*@
7240f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7241f4d763aaSMatthew G. Knepley 
72428f700142SStefano Zampini   Collective on dm
72438f700142SStefano Zampini 
7244f4d763aaSMatthew G. Knepley   Input Parameter:
7245f4d763aaSMatthew G. Knepley . dm - The original DM
7246f4d763aaSMatthew G. Knepley 
7247f4d763aaSMatthew G. Knepley   Output Parameter:
7248f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7249f4d763aaSMatthew G. Knepley 
7250f4d763aaSMatthew G. Knepley   Level: intermediate
7251f4d763aaSMatthew G. Knepley 
7252db781477SPatrick Sanan .seealso: `VecView()`, `DMGetGlobalSection()`
7253f4d763aaSMatthew G. Knepley @*/
725414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
725514f150ffSMatthew G. Knepley {
7256c26acbdeSMatthew G. Knepley   PetscSection   section;
72572d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
725814f150ffSMatthew G. Knepley 
725914f150ffSMatthew G. Knepley   PetscFunctionBegin;
726014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
726114f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
72629566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
72639566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
72649566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm)));
72652d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7266c26acbdeSMatthew G. Knepley     *odm = dm;
7267c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7268c26acbdeSMatthew G. Knepley   }
726914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7270c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
727114f150ffSMatthew G. Knepley     PetscSF      sf;
727214f150ffSMatthew G. Knepley 
72739566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
72749566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
72759566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
72769566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
72779566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
72789566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
72799566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
72809566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
72819566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
728214f150ffSMatthew G. Knepley   }
728314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
728414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
728514f150ffSMatthew G. Knepley }
7286f4d763aaSMatthew G. Knepley 
7287f4d763aaSMatthew G. Knepley /*@
7288cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7289f4d763aaSMatthew G. Knepley 
7290f4d763aaSMatthew G. Knepley   Input Parameter:
7291f4d763aaSMatthew G. Knepley . dm - The original DM
7292f4d763aaSMatthew G. Knepley 
7293cdb7a50dSMatthew G. Knepley   Output Parameters:
7294cdb7a50dSMatthew G. Knepley + num - The output sequence number
7295cdb7a50dSMatthew G. Knepley - val - The output sequence value
7296f4d763aaSMatthew G. Knepley 
7297f4d763aaSMatthew G. Knepley   Level: intermediate
7298f4d763aaSMatthew G. Knepley 
7299f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7300f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7301f4d763aaSMatthew G. Knepley 
7302db781477SPatrick Sanan .seealso: `VecView()`
7303f4d763aaSMatthew G. Knepley @*/
7304cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7305f4d763aaSMatthew G. Knepley {
7306f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7307f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7308534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7309534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7310f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7311f4d763aaSMatthew G. Knepley }
7312f4d763aaSMatthew G. Knepley 
7313f4d763aaSMatthew G. Knepley /*@
7314cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7315f4d763aaSMatthew G. Knepley 
7316f4d763aaSMatthew G. Knepley   Input Parameters:
7317f4d763aaSMatthew G. Knepley + dm - The original DM
7318cdb7a50dSMatthew G. Knepley . num - The output sequence number
7319cdb7a50dSMatthew G. Knepley - val - The output sequence value
7320f4d763aaSMatthew G. Knepley 
7321f4d763aaSMatthew G. Knepley   Level: intermediate
7322f4d763aaSMatthew G. Knepley 
7323f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7324f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7325f4d763aaSMatthew G. Knepley 
7326db781477SPatrick Sanan .seealso: `VecView()`
7327f4d763aaSMatthew G. Knepley @*/
7328cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7329f4d763aaSMatthew G. Knepley {
7330f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7331f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7332f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7333cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7334cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7335cdb7a50dSMatthew G. Knepley }
7336cdb7a50dSMatthew G. Knepley 
7337cdb7a50dSMatthew G. Knepley /*@C
7338cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7339cdb7a50dSMatthew G. Knepley 
7340cdb7a50dSMatthew G. Knepley   Input Parameters:
7341cdb7a50dSMatthew G. Knepley + dm   - The original DM
7342cdb7a50dSMatthew G. Knepley . name - The sequence name
7343cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7344cdb7a50dSMatthew G. Knepley 
7345cdb7a50dSMatthew G. Knepley   Output Parameter:
7346cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7347cdb7a50dSMatthew G. Knepley 
7348cdb7a50dSMatthew G. Knepley   Level: intermediate
7349cdb7a50dSMatthew G. Knepley 
7350cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7351cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7352cdb7a50dSMatthew G. Knepley 
7353db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
7354cdb7a50dSMatthew G. Knepley @*/
7355cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7356cdb7a50dSMatthew G. Knepley {
7357cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7358cdb7a50dSMatthew G. Knepley 
7359cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7360cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7361cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7362064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
73639566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5));
7364cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7365cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7366cdb7a50dSMatthew G. Knepley     PetscScalar value;
7367cdb7a50dSMatthew G. Knepley 
73689566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
73694aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7370cdb7a50dSMatthew G. Knepley #endif
7371cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7372f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7373f4d763aaSMatthew G. Knepley }
73748e4ac7eaSMatthew G. Knepley 
73758e4ac7eaSMatthew G. Knepley /*@
73768e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
73778e4ac7eaSMatthew G. Knepley 
73788e4ac7eaSMatthew G. Knepley   Not collective
73798e4ac7eaSMatthew G. Knepley 
73808e4ac7eaSMatthew G. Knepley   Input Parameter:
73818e4ac7eaSMatthew G. Knepley . dm - The DM
73828e4ac7eaSMatthew G. Knepley 
73838e4ac7eaSMatthew G. Knepley   Output Parameter:
73848e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
73858e4ac7eaSMatthew G. Knepley 
73868e4ac7eaSMatthew G. Knepley   Level: beginner
73878e4ac7eaSMatthew G. Knepley 
7388db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
73898e4ac7eaSMatthew G. Knepley @*/
73908e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
73918e4ac7eaSMatthew G. Knepley {
73928e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73938e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7394534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
73958e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
73968e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73978e4ac7eaSMatthew G. Knepley }
73988e4ac7eaSMatthew G. Knepley 
73998e4ac7eaSMatthew G. Knepley /*@
74005d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
74018e4ac7eaSMatthew G. Knepley 
74028e4ac7eaSMatthew G. Knepley   Collective on dm
74038e4ac7eaSMatthew G. Knepley 
74048e4ac7eaSMatthew G. Knepley   Input Parameters:
74058e4ac7eaSMatthew G. Knepley + dm - The DM
74068e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
74078e4ac7eaSMatthew G. Knepley 
74085d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
74095d3b26e6SMatthew G. Knepley 
74108e4ac7eaSMatthew G. Knepley   Level: beginner
74118e4ac7eaSMatthew G. Knepley 
7412db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
74138e4ac7eaSMatthew G. Knepley @*/
74148e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
74158e4ac7eaSMatthew G. Knepley {
74168e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
74178e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74188833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
74198e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
74208e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
74218e4ac7eaSMatthew G. Knepley }
7422c58f1c22SToby Isaac 
7423c58f1c22SToby Isaac /*@C
7424c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7425c58f1c22SToby Isaac 
7426c58f1c22SToby Isaac   Not Collective
7427c58f1c22SToby Isaac 
7428c58f1c22SToby Isaac   Input Parameters:
7429c58f1c22SToby Isaac + dm   - The DM object
7430c58f1c22SToby Isaac - name - The label name
7431c58f1c22SToby Isaac 
7432c58f1c22SToby Isaac   Level: intermediate
7433c58f1c22SToby Isaac 
7434db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7435c58f1c22SToby Isaac @*/
7436c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7437c58f1c22SToby Isaac {
74385d80c0bfSVaclav Hapla   PetscBool      flg;
74395d80c0bfSVaclav Hapla   DMLabel        label;
7440c58f1c22SToby Isaac 
7441c58f1c22SToby Isaac   PetscFunctionBegin;
7442c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7443c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
74449566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
7445c58f1c22SToby Isaac   if (!flg) {
74469566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
74479566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
74489566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
7449c58f1c22SToby Isaac   }
7450c58f1c22SToby Isaac   PetscFunctionReturn(0);
7451c58f1c22SToby Isaac }
7452c58f1c22SToby Isaac 
7453c58f1c22SToby Isaac /*@C
74547cf350d0SPierre Jolivet   DMCreateLabelAtIndex - Create a label of the given name at the given index. If it already exists, move it to this index.
74550fdc7489SMatthew Knepley 
74560fdc7489SMatthew Knepley   Not Collective
74570fdc7489SMatthew Knepley 
74580fdc7489SMatthew Knepley   Input Parameters:
74590fdc7489SMatthew Knepley + dm   - The DM object
74600fdc7489SMatthew Knepley . l    - The index for the label
74610fdc7489SMatthew Knepley - name - The label name
74620fdc7489SMatthew Knepley 
74630fdc7489SMatthew Knepley   Level: intermediate
74640fdc7489SMatthew Knepley 
7465db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
74660fdc7489SMatthew Knepley @*/
74670fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
74680fdc7489SMatthew Knepley {
74690fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
74700fdc7489SMatthew Knepley   DMLabel        label;
74710fdc7489SMatthew Knepley   PetscInt       Nl, m;
74720fdc7489SMatthew Knepley   PetscBool      flg, match;
74730fdc7489SMatthew Knepley   const char    *lname;
74740fdc7489SMatthew Knepley 
74750fdc7489SMatthew Knepley   PetscFunctionBegin;
74760fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7477064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
74789566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
74790fdc7489SMatthew Knepley   if (!flg) {
74809566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
74819566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
74829566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
74830fdc7489SMatthew Knepley   }
74849566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
748563a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
74860fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
74879566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) orig->label, &lname));
74889566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
74890fdc7489SMatthew Knepley     if (match) break;
74900fdc7489SMatthew Knepley   }
74910fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
74920fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
74930fdc7489SMatthew Knepley   else    prev->next = orig->next;
74940fdc7489SMatthew Knepley   if (!l) {
74950fdc7489SMatthew Knepley     orig->next = dm->labels;
74960fdc7489SMatthew Knepley     dm->labels = orig;
74970fdc7489SMatthew Knepley   } else {
74980fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
74990fdc7489SMatthew Knepley     orig->next = prev->next;
75000fdc7489SMatthew Knepley     prev->next = orig;
75010fdc7489SMatthew Knepley   }
75020fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75030fdc7489SMatthew Knepley }
75040fdc7489SMatthew Knepley 
75050fdc7489SMatthew Knepley /*@C
75067cf350d0SPierre Jolivet   DMGetLabelValue - Get the value in a DMLabel for the given point, with -1 as the default
7507c58f1c22SToby Isaac 
7508c58f1c22SToby Isaac   Not Collective
7509c58f1c22SToby Isaac 
7510c58f1c22SToby Isaac   Input Parameters:
7511c58f1c22SToby Isaac + dm   - The DM object
7512c58f1c22SToby Isaac . name - The label name
7513c58f1c22SToby Isaac - point - The mesh point
7514c58f1c22SToby Isaac 
7515c58f1c22SToby Isaac   Output Parameter:
7516c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7517c58f1c22SToby Isaac 
7518c58f1c22SToby Isaac   Level: beginner
7519c58f1c22SToby Isaac 
7520db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7521c58f1c22SToby Isaac @*/
7522c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7523c58f1c22SToby Isaac {
7524c58f1c22SToby Isaac   DMLabel        label;
7525c58f1c22SToby Isaac 
7526c58f1c22SToby Isaac   PetscFunctionBegin;
7527c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7528c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
75299566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
75307a8be351SBarry Smith   PetscCheck(label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
75319566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
7532c58f1c22SToby Isaac   PetscFunctionReturn(0);
7533c58f1c22SToby Isaac }
7534c58f1c22SToby Isaac 
7535c58f1c22SToby Isaac /*@C
75367cf350d0SPierre Jolivet   DMSetLabelValue - Add a point to a DMLabel with given value
7537c58f1c22SToby Isaac 
7538c58f1c22SToby Isaac   Not Collective
7539c58f1c22SToby Isaac 
7540c58f1c22SToby Isaac   Input Parameters:
7541c58f1c22SToby Isaac + dm   - The DM object
7542c58f1c22SToby Isaac . name - The label name
7543c58f1c22SToby Isaac . point - The mesh point
7544c58f1c22SToby Isaac - value - The label value for this point
7545c58f1c22SToby Isaac 
7546c58f1c22SToby Isaac   Output Parameter:
7547c58f1c22SToby Isaac 
7548c58f1c22SToby Isaac   Level: beginner
7549c58f1c22SToby Isaac 
7550db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7551c58f1c22SToby Isaac @*/
7552c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7553c58f1c22SToby Isaac {
7554c58f1c22SToby Isaac   DMLabel        label;
7555c58f1c22SToby Isaac 
7556c58f1c22SToby Isaac   PetscFunctionBegin;
7557c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7558c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
75599566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7560c58f1c22SToby Isaac   if (!label) {
75619566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
75629566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
7563c58f1c22SToby Isaac   }
75649566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
7565c58f1c22SToby Isaac   PetscFunctionReturn(0);
7566c58f1c22SToby Isaac }
7567c58f1c22SToby Isaac 
7568c58f1c22SToby Isaac /*@C
75697cf350d0SPierre Jolivet   DMClearLabelValue - Remove a point from a DMLabel with given value
7570c58f1c22SToby Isaac 
7571c58f1c22SToby Isaac   Not Collective
7572c58f1c22SToby Isaac 
7573c58f1c22SToby Isaac   Input Parameters:
7574c58f1c22SToby Isaac + dm   - The DM object
7575c58f1c22SToby Isaac . name - The label name
7576c58f1c22SToby Isaac . point - The mesh point
7577c58f1c22SToby Isaac - value - The label value for this point
7578c58f1c22SToby Isaac 
7579c58f1c22SToby Isaac   Output Parameter:
7580c58f1c22SToby Isaac 
7581c58f1c22SToby Isaac   Level: beginner
7582c58f1c22SToby Isaac 
7583db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7584c58f1c22SToby Isaac @*/
7585c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7586c58f1c22SToby Isaac {
7587c58f1c22SToby Isaac   DMLabel        label;
7588c58f1c22SToby Isaac 
7589c58f1c22SToby Isaac   PetscFunctionBegin;
7590c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7591c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
75929566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7593c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
75949566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
7595c58f1c22SToby Isaac   PetscFunctionReturn(0);
7596c58f1c22SToby Isaac }
7597c58f1c22SToby Isaac 
7598c58f1c22SToby Isaac /*@C
7599c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7600c58f1c22SToby Isaac 
7601c58f1c22SToby Isaac   Not Collective
7602c58f1c22SToby Isaac 
7603c58f1c22SToby Isaac   Input Parameters:
7604c58f1c22SToby Isaac + dm   - The DM object
7605c58f1c22SToby Isaac - name - The label name
7606c58f1c22SToby Isaac 
7607c58f1c22SToby Isaac   Output Parameter:
7608c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7609c58f1c22SToby Isaac 
7610c58f1c22SToby Isaac   Level: beginner
7611c58f1c22SToby Isaac 
7612db781477SPatrick Sanan .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`
7613c58f1c22SToby Isaac @*/
7614c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7615c58f1c22SToby Isaac {
7616c58f1c22SToby Isaac   DMLabel        label;
7617c58f1c22SToby Isaac 
7618c58f1c22SToby Isaac   PetscFunctionBegin;
7619c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7620c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7621534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
76229566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7623c58f1c22SToby Isaac   *size = 0;
7624c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
76259566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
7626c58f1c22SToby Isaac   PetscFunctionReturn(0);
7627c58f1c22SToby Isaac }
7628c58f1c22SToby Isaac 
7629c58f1c22SToby Isaac /*@C
7630c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7631c58f1c22SToby Isaac 
7632c58f1c22SToby Isaac   Not Collective
7633c58f1c22SToby Isaac 
7634c58f1c22SToby Isaac   Input Parameters:
7635c58f1c22SToby Isaac + mesh - The DM object
7636c58f1c22SToby Isaac - name - The label name
7637c58f1c22SToby Isaac 
7638c58f1c22SToby Isaac   Output Parameter:
7639c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7640c58f1c22SToby Isaac 
7641c58f1c22SToby Isaac   Level: beginner
7642c58f1c22SToby Isaac 
7643db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
7644c58f1c22SToby Isaac @*/
7645c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7646c58f1c22SToby Isaac {
7647c58f1c22SToby Isaac   DMLabel        label;
7648c58f1c22SToby Isaac 
7649c58f1c22SToby Isaac   PetscFunctionBegin;
7650c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7651c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7652c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
76539566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7654c58f1c22SToby Isaac   *ids = NULL;
7655dab2e251SBlaise Bourdin   if (label) {
76569566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
7657dab2e251SBlaise Bourdin   } else {
7658dab2e251SBlaise Bourdin     /* returning an empty IS */
76599566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids));
7660dab2e251SBlaise Bourdin   }
7661c58f1c22SToby Isaac   PetscFunctionReturn(0);
7662c58f1c22SToby Isaac }
7663c58f1c22SToby Isaac 
7664c58f1c22SToby Isaac /*@C
7665c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7666c58f1c22SToby Isaac 
7667c58f1c22SToby Isaac   Not Collective
7668c58f1c22SToby Isaac 
7669c58f1c22SToby Isaac   Input Parameters:
7670c58f1c22SToby Isaac + dm - The DM object
7671c58f1c22SToby Isaac . name - The label name
7672c58f1c22SToby Isaac - value - The stratum value
7673c58f1c22SToby Isaac 
7674c58f1c22SToby Isaac   Output Parameter:
7675c58f1c22SToby Isaac . size - The stratum size
7676c58f1c22SToby Isaac 
7677c58f1c22SToby Isaac   Level: beginner
7678c58f1c22SToby Isaac 
7679db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
7680c58f1c22SToby Isaac @*/
7681c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7682c58f1c22SToby Isaac {
7683c58f1c22SToby Isaac   DMLabel        label;
7684c58f1c22SToby Isaac 
7685c58f1c22SToby Isaac   PetscFunctionBegin;
7686c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7687c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7688534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
76899566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7690c58f1c22SToby Isaac   *size = 0;
7691c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
76929566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
7693c58f1c22SToby Isaac   PetscFunctionReturn(0);
7694c58f1c22SToby Isaac }
7695c58f1c22SToby Isaac 
7696c58f1c22SToby Isaac /*@C
7697c58f1c22SToby Isaac   DMGetStratumIS - Get the 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 . points - The stratum points, or NULL if the label does not exist or does not have that value
7708c58f1c22SToby Isaac 
7709c58f1c22SToby Isaac   Level: beginner
7710c58f1c22SToby Isaac 
7711db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
7712c58f1c22SToby Isaac @*/
7713c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7714c58f1c22SToby Isaac {
7715c58f1c22SToby Isaac   DMLabel        label;
7716c58f1c22SToby Isaac 
7717c58f1c22SToby Isaac   PetscFunctionBegin;
7718c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7719c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7720c58f1c22SToby Isaac   PetscValidPointer(points, 4);
77219566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7722c58f1c22SToby Isaac   *points = NULL;
7723c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
77249566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
7725c58f1c22SToby Isaac   PetscFunctionReturn(0);
7726c58f1c22SToby Isaac }
7727c58f1c22SToby Isaac 
77284de306b1SToby Isaac /*@C
77299044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
77304de306b1SToby Isaac 
77314de306b1SToby Isaac   Not Collective
77324de306b1SToby Isaac 
77334de306b1SToby Isaac   Input Parameters:
77344de306b1SToby Isaac + dm - The DM object
77354de306b1SToby Isaac . name - The label name
77364de306b1SToby Isaac . value - The stratum value
77374de306b1SToby Isaac - points - The stratum points
77384de306b1SToby Isaac 
77394de306b1SToby Isaac   Level: beginner
77404de306b1SToby Isaac 
7741db781477SPatrick Sanan .seealso: `DMLabelSetStratumIS()`, `DMGetStratumSize()`
77424de306b1SToby Isaac @*/
77434de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
77444de306b1SToby Isaac {
77454de306b1SToby Isaac   DMLabel        label;
77464de306b1SToby Isaac 
77474de306b1SToby Isaac   PetscFunctionBegin;
77484de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77494de306b1SToby Isaac   PetscValidCharPointer(name, 2);
77504de306b1SToby Isaac   PetscValidPointer(points, 4);
77519566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
77524de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
77539566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
77544de306b1SToby Isaac   PetscFunctionReturn(0);
77554de306b1SToby Isaac }
77564de306b1SToby Isaac 
7757c58f1c22SToby Isaac /*@C
77587cf350d0SPierre Jolivet   DMClearLabelStratum - Remove all points from a stratum from a DMLabel
7759c58f1c22SToby Isaac 
7760c58f1c22SToby Isaac   Not Collective
7761c58f1c22SToby Isaac 
7762c58f1c22SToby Isaac   Input Parameters:
7763c58f1c22SToby Isaac + dm   - The DM object
7764c58f1c22SToby Isaac . name - The label name
7765c58f1c22SToby Isaac - value - The label value for this point
7766c58f1c22SToby Isaac 
7767c58f1c22SToby Isaac   Output Parameter:
7768c58f1c22SToby Isaac 
7769c58f1c22SToby Isaac   Level: beginner
7770c58f1c22SToby Isaac 
7771db781477SPatrick Sanan .seealso: `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7772c58f1c22SToby Isaac @*/
7773c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7774c58f1c22SToby Isaac {
7775c58f1c22SToby Isaac   DMLabel        label;
7776c58f1c22SToby Isaac 
7777c58f1c22SToby Isaac   PetscFunctionBegin;
7778c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7779c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
77809566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7781c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
77829566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
7783c58f1c22SToby Isaac   PetscFunctionReturn(0);
7784c58f1c22SToby Isaac }
7785c58f1c22SToby Isaac 
7786c58f1c22SToby Isaac /*@
7787c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7788c58f1c22SToby Isaac 
7789c58f1c22SToby Isaac   Not Collective
7790c58f1c22SToby Isaac 
7791c58f1c22SToby Isaac   Input Parameter:
7792c58f1c22SToby Isaac . dm   - The DM object
7793c58f1c22SToby Isaac 
7794c58f1c22SToby Isaac   Output Parameter:
7795c58f1c22SToby Isaac . numLabels - the number of Labels
7796c58f1c22SToby Isaac 
7797c58f1c22SToby Isaac   Level: intermediate
7798c58f1c22SToby Isaac 
7799db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7800c58f1c22SToby Isaac @*/
7801c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7802c58f1c22SToby Isaac {
78035d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7804c58f1c22SToby Isaac   PetscInt  n    = 0;
7805c58f1c22SToby Isaac 
7806c58f1c22SToby Isaac   PetscFunctionBegin;
7807c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7808534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7809c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7810c58f1c22SToby Isaac   *numLabels = n;
7811c58f1c22SToby Isaac   PetscFunctionReturn(0);
7812c58f1c22SToby Isaac }
7813c58f1c22SToby Isaac 
7814c58f1c22SToby Isaac /*@C
7815c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7816c58f1c22SToby Isaac 
7817c58f1c22SToby Isaac   Not Collective
7818c58f1c22SToby Isaac 
7819c58f1c22SToby Isaac   Input Parameters:
7820c58f1c22SToby Isaac + dm - The DM object
7821c58f1c22SToby Isaac - n  - the label number
7822c58f1c22SToby Isaac 
7823c58f1c22SToby Isaac   Output Parameter:
7824c58f1c22SToby Isaac . name - the label name
7825c58f1c22SToby Isaac 
7826c58f1c22SToby Isaac   Level: intermediate
7827c58f1c22SToby Isaac 
7828db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7829c58f1c22SToby Isaac @*/
7830c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7831c58f1c22SToby Isaac {
78325d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7833c58f1c22SToby Isaac   PetscInt       l    = 0;
7834c58f1c22SToby Isaac 
7835c58f1c22SToby Isaac   PetscFunctionBegin;
7836c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7837c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7838c58f1c22SToby Isaac   while (next) {
7839c58f1c22SToby Isaac     if (l == n) {
78409566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) next->label, name));
7841c58f1c22SToby Isaac       PetscFunctionReturn(0);
7842c58f1c22SToby Isaac     }
7843c58f1c22SToby Isaac     ++l;
7844c58f1c22SToby Isaac     next = next->next;
7845c58f1c22SToby Isaac   }
784663a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7847c58f1c22SToby Isaac }
7848c58f1c22SToby Isaac 
7849c58f1c22SToby Isaac /*@C
7850c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7851c58f1c22SToby Isaac 
7852c58f1c22SToby Isaac   Not Collective
7853c58f1c22SToby Isaac 
7854c58f1c22SToby Isaac   Input Parameters:
7855c58f1c22SToby Isaac + dm   - The DM object
7856c58f1c22SToby Isaac - name - The label name
7857c58f1c22SToby Isaac 
7858c58f1c22SToby Isaac   Output Parameter:
7859c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7860c58f1c22SToby Isaac 
7861c58f1c22SToby Isaac   Level: intermediate
7862c58f1c22SToby Isaac 
7863db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7864c58f1c22SToby Isaac @*/
7865c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7866c58f1c22SToby Isaac {
78675d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7868d67d17b1SMatthew G. Knepley   const char    *lname;
7869c58f1c22SToby Isaac 
7870c58f1c22SToby Isaac   PetscFunctionBegin;
7871c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7872c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7873534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7874c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7875c58f1c22SToby Isaac   while (next) {
78769566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
78779566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7878c58f1c22SToby Isaac     if (*hasLabel) break;
7879c58f1c22SToby Isaac     next = next->next;
7880c58f1c22SToby Isaac   }
7881c58f1c22SToby Isaac   PetscFunctionReturn(0);
7882c58f1c22SToby Isaac }
7883c58f1c22SToby Isaac 
7884c58f1c22SToby Isaac /*@C
7885c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7886c58f1c22SToby Isaac 
7887c58f1c22SToby Isaac   Not Collective
7888c58f1c22SToby Isaac 
7889c58f1c22SToby Isaac   Input Parameters:
7890c58f1c22SToby Isaac + dm   - The DM object
7891c58f1c22SToby Isaac - name - The label name
7892c58f1c22SToby Isaac 
7893c58f1c22SToby Isaac   Output Parameter:
7894c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7895c58f1c22SToby Isaac 
78966d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
78976d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
78986d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
78996d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
79006d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
79016d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
79026d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
79036d7c9049SMatthew G. Knepley 
7904c58f1c22SToby Isaac   Level: intermediate
7905c58f1c22SToby Isaac 
7906db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7907c58f1c22SToby Isaac @*/
7908c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7909c58f1c22SToby Isaac {
79105d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7911c58f1c22SToby Isaac   PetscBool      hasLabel;
7912d67d17b1SMatthew G. Knepley   const char    *lname;
7913c58f1c22SToby Isaac 
7914c58f1c22SToby Isaac   PetscFunctionBegin;
7915c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7916c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7917c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7918c58f1c22SToby Isaac   *label = NULL;
7919c58f1c22SToby Isaac   while (next) {
79209566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
79219566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7922c58f1c22SToby Isaac     if (hasLabel) {
7923c58f1c22SToby Isaac       *label = next->label;
7924c58f1c22SToby Isaac       break;
7925c58f1c22SToby Isaac     }
7926c58f1c22SToby Isaac     next = next->next;
7927c58f1c22SToby Isaac   }
7928c58f1c22SToby Isaac   PetscFunctionReturn(0);
7929c58f1c22SToby Isaac }
7930c58f1c22SToby Isaac 
7931c58f1c22SToby Isaac /*@C
7932c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7933c58f1c22SToby Isaac 
7934c58f1c22SToby Isaac   Not Collective
7935c58f1c22SToby Isaac 
7936c58f1c22SToby Isaac   Input Parameters:
7937c58f1c22SToby Isaac + dm - The DM object
7938c58f1c22SToby Isaac - n  - the label number
7939c58f1c22SToby Isaac 
7940c58f1c22SToby Isaac   Output Parameter:
7941c58f1c22SToby Isaac . label - the label
7942c58f1c22SToby Isaac 
7943c58f1c22SToby Isaac   Level: intermediate
7944c58f1c22SToby Isaac 
7945db781477SPatrick Sanan .seealso: `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7946c58f1c22SToby Isaac @*/
7947c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7948c58f1c22SToby Isaac {
79495d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7950c58f1c22SToby Isaac   PetscInt    l    = 0;
7951c58f1c22SToby Isaac 
7952c58f1c22SToby Isaac   PetscFunctionBegin;
7953c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7954c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7955c58f1c22SToby Isaac   while (next) {
7956c58f1c22SToby Isaac     if (l == n) {
7957c58f1c22SToby Isaac       *label = next->label;
7958c58f1c22SToby Isaac       PetscFunctionReturn(0);
7959c58f1c22SToby Isaac     }
7960c58f1c22SToby Isaac     ++l;
7961c58f1c22SToby Isaac     next = next->next;
7962c58f1c22SToby Isaac   }
796363a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7964c58f1c22SToby Isaac }
7965c58f1c22SToby Isaac 
7966c58f1c22SToby Isaac /*@C
7967c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7968c58f1c22SToby Isaac 
7969c58f1c22SToby Isaac   Not Collective
7970c58f1c22SToby Isaac 
7971c58f1c22SToby Isaac   Input Parameters:
7972c58f1c22SToby Isaac + dm   - The DM object
7973c58f1c22SToby Isaac - label - The DMLabel
7974c58f1c22SToby Isaac 
7975c58f1c22SToby Isaac   Level: developer
7976c58f1c22SToby Isaac 
7977db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7978c58f1c22SToby Isaac @*/
7979c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7980c58f1c22SToby Isaac {
79815d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7982c58f1c22SToby Isaac   PetscBool      hasLabel;
7983d67d17b1SMatthew G. Knepley   const char    *lname;
79845d80c0bfSVaclav Hapla   PetscBool      flg;
7985c58f1c22SToby Isaac 
7986c58f1c22SToby Isaac   PetscFunctionBegin;
7987c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79889566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &lname));
79899566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
79907a8be351SBarry Smith   PetscCheck(!hasLabel,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
79919566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7992c58f1c22SToby Isaac   tmpLabel->label  = label;
7993c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
79945d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
79955d80c0bfSVaclav Hapla   *p = tmpLabel;
79969566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
79979566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
79985d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
79999566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
8000ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
8001c58f1c22SToby Isaac   PetscFunctionReturn(0);
8002c58f1c22SToby Isaac }
8003c58f1c22SToby Isaac 
8004c58f1c22SToby Isaac /*@C
80054a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
80064a7ee7d0SMatthew G. Knepley 
80074a7ee7d0SMatthew G. Knepley   Not Collective
80084a7ee7d0SMatthew G. Knepley 
80094a7ee7d0SMatthew G. Knepley   Input Parameters:
80104a7ee7d0SMatthew G. Knepley + dm    - The DM object
80114a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
80124a7ee7d0SMatthew G. Knepley 
80134a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
80144a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
80154a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
80164a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
80174a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
80184a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
80194a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
80204a7ee7d0SMatthew G. Knepley 
80214a7ee7d0SMatthew G. Knepley   Level: intermediate
80224a7ee7d0SMatthew G. Knepley 
8023db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
80244a7ee7d0SMatthew G. Knepley @*/
80254a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
80264a7ee7d0SMatthew G. Knepley {
80274a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
80284a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
80294a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
80304a7ee7d0SMatthew G. Knepley 
80314a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
80324a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80334a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
80349566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject) label, &name));
80354a7ee7d0SMatthew G. Knepley   while (next) {
80369566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
80379566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
80384a7ee7d0SMatthew G. Knepley     if (hasLabel) {
80399566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject) label));
80409566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
80414a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
80429566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
80434a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
80449566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
80454a7ee7d0SMatthew G. Knepley       next->label = label;
80464a7ee7d0SMatthew G. Knepley       break;
80474a7ee7d0SMatthew G. Knepley     }
80484a7ee7d0SMatthew G. Knepley     next = next->next;
80494a7ee7d0SMatthew G. Knepley   }
80504a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
80514a7ee7d0SMatthew G. Knepley }
80524a7ee7d0SMatthew G. Knepley 
80534a7ee7d0SMatthew G. Knepley /*@C
8054e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
8055c58f1c22SToby Isaac 
8056c58f1c22SToby Isaac   Not Collective
8057c58f1c22SToby Isaac 
8058c58f1c22SToby Isaac   Input Parameters:
8059c58f1c22SToby Isaac + dm   - The DM object
8060c58f1c22SToby Isaac - name - The label name
8061c58f1c22SToby Isaac 
8062c58f1c22SToby Isaac   Output Parameter:
8063c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8064c58f1c22SToby Isaac 
8065c58f1c22SToby Isaac   Level: developer
8066c58f1c22SToby Isaac 
8067e5472504SVaclav Hapla   Notes:
8068e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
8069e5472504SVaclav Hapla   DMLabelDestroy() on the label.
8070e5472504SVaclav Hapla 
8071e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8072e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8073e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8074e5472504SVaclav Hapla 
8075db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
8076c58f1c22SToby Isaac @*/
8077c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8078c58f1c22SToby Isaac {
807995d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8080c58f1c22SToby Isaac   PetscBool      hasLabel;
8081d67d17b1SMatthew G. Knepley   const char    *lname;
8082c58f1c22SToby Isaac 
8083c58f1c22SToby Isaac   PetscFunctionBegin;
8084c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8085e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8086e5472504SVaclav Hapla   if (label) {
8087e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8088c58f1c22SToby Isaac     *label = NULL;
8089e5472504SVaclav Hapla   }
80905d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
80919566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) link->label, &lname));
80929566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
8093c58f1c22SToby Isaac     if (hasLabel) {
809495d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
80959566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
809695d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
80979566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
8098ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
809995d578d6SVaclav Hapla       if (label) *label = link->label;
81009566063dSJacob Faibussowitsch       else       PetscCall(DMLabelDestroy(&link->label));
81019566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
8102c58f1c22SToby Isaac       break;
8103c58f1c22SToby Isaac     }
8104c58f1c22SToby Isaac   }
8105c58f1c22SToby Isaac   PetscFunctionReturn(0);
8106c58f1c22SToby Isaac }
8107c58f1c22SToby Isaac 
8108306894acSVaclav Hapla /*@
8109306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8110306894acSVaclav Hapla 
8111306894acSVaclav Hapla   Not Collective
8112306894acSVaclav Hapla 
8113306894acSVaclav Hapla   Input Parameters:
8114306894acSVaclav Hapla + dm   - The DM object
8115876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
8116306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8117306894acSVaclav Hapla 
8118306894acSVaclav Hapla   Level: developer
8119306894acSVaclav Hapla 
8120306894acSVaclav Hapla   Notes:
8121306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8122306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8123306894acSVaclav Hapla   *label nullified.
8124306894acSVaclav Hapla 
8125db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
8126306894acSVaclav Hapla @*/
8127306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8128306894acSVaclav Hapla {
812943e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8130306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8131306894acSVaclav Hapla 
8132306894acSVaclav Hapla   PetscFunctionBegin;
8133306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8134306894acSVaclav Hapla   PetscValidPointer(label, 2);
8135f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8136306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8137306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
81385d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
813943e45a93SVaclav Hapla     if (*label == link->label) {
8140306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
814143e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8142306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8143ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
814443e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
81459566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
81469566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
8147306894acSVaclav Hapla       break;
8148306894acSVaclav Hapla     }
8149306894acSVaclav Hapla   }
81507a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8151306894acSVaclav Hapla   PetscFunctionReturn(0);
8152306894acSVaclav Hapla }
8153306894acSVaclav Hapla 
8154c58f1c22SToby Isaac /*@C
8155c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8156c58f1c22SToby Isaac 
8157c58f1c22SToby Isaac   Not Collective
8158c58f1c22SToby Isaac 
8159c58f1c22SToby Isaac   Input Parameters:
8160c58f1c22SToby Isaac + dm   - The DM object
8161c58f1c22SToby Isaac - name - The label name
8162c58f1c22SToby Isaac 
8163c58f1c22SToby Isaac   Output Parameter:
8164c58f1c22SToby Isaac . output - The flag for output
8165c58f1c22SToby Isaac 
8166c58f1c22SToby Isaac   Level: developer
8167c58f1c22SToby Isaac 
8168db781477SPatrick Sanan .seealso: `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
8169c58f1c22SToby Isaac @*/
8170c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8171c58f1c22SToby Isaac {
81725d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8173d67d17b1SMatthew G. Knepley   const char    *lname;
8174c58f1c22SToby Isaac 
8175c58f1c22SToby Isaac   PetscFunctionBegin;
8176c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8177dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
8178dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
8179c58f1c22SToby Isaac   while (next) {
8180c58f1c22SToby Isaac     PetscBool flg;
8181c58f1c22SToby Isaac 
81829566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
81839566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
8184c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8185c58f1c22SToby Isaac     next = next->next;
8186c58f1c22SToby Isaac   }
818798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8188c58f1c22SToby Isaac }
8189c58f1c22SToby Isaac 
8190c58f1c22SToby Isaac /*@C
8191c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8192c58f1c22SToby Isaac 
8193c58f1c22SToby Isaac   Not Collective
8194c58f1c22SToby Isaac 
8195c58f1c22SToby Isaac   Input Parameters:
8196c58f1c22SToby Isaac + dm     - The DM object
8197c58f1c22SToby Isaac . name   - The label name
8198c58f1c22SToby Isaac - output - The flag for output
8199c58f1c22SToby Isaac 
8200c58f1c22SToby Isaac   Level: developer
8201c58f1c22SToby Isaac 
8202db781477SPatrick Sanan .seealso: `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
8203c58f1c22SToby Isaac @*/
8204c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8205c58f1c22SToby Isaac {
82065d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8207d67d17b1SMatthew G. Knepley   const char    *lname;
8208c58f1c22SToby Isaac 
8209c58f1c22SToby Isaac   PetscFunctionBegin;
8210c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8211534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8212c58f1c22SToby Isaac   while (next) {
8213c58f1c22SToby Isaac     PetscBool flg;
8214c58f1c22SToby Isaac 
82159566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) next->label, &lname));
82169566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
8217c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8218c58f1c22SToby Isaac     next = next->next;
8219c58f1c22SToby Isaac   }
822098921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8221c58f1c22SToby Isaac }
8222c58f1c22SToby Isaac 
8223c58f1c22SToby Isaac /*@
8224c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8225c58f1c22SToby Isaac 
8226d083f849SBarry Smith   Collective on dmA
8227c58f1c22SToby Isaac 
8228d8d19677SJose E. Roman   Input Parameters:
82295d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
82302cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
82315d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
82322cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
82332cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
8234c58f1c22SToby Isaac 
8235c58f1c22SToby Isaac   Level: intermediate
8236c58f1c22SToby Isaac 
82372cbb9b06SVaclav Hapla   Notes:
82382cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
8239c58f1c22SToby Isaac 
8240db781477SPatrick Sanan .seealso: `DMAddLabel()`, `DMCopyLabelsMode`
8241c58f1c22SToby Isaac @*/
82422cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
8243c58f1c22SToby Isaac {
82442cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
8245c58f1c22SToby Isaac   const char    *name;
8246c58f1c22SToby Isaac   PetscBool      flg;
82475d80c0bfSVaclav Hapla   DMLabelLink    link;
8248c58f1c22SToby Isaac 
82495d80c0bfSVaclav Hapla   PetscFunctionBegin;
82505d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
82515d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
82525d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
82535d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
82547a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER,PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
82555d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
82565d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
82575d80c0bfSVaclav Hapla     label=link->label;
82589566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
82595d80c0bfSVaclav Hapla     if (!all) {
82609566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
8261c58f1c22SToby Isaac       if (flg) continue;
82629566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
82637d5acc75SStefano Zampini       if (flg) continue;
82649566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
8265ba2698f1SMatthew G. Knepley       if (flg) continue;
82665d80c0bfSVaclav Hapla     }
82679566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
82682cbb9b06SVaclav Hapla     if (labelOld) {
82692cbb9b06SVaclav Hapla       switch (emode) {
82702cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
82712cbb9b06SVaclav Hapla           continue;
82722cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
82739566063dSJacob Faibussowitsch           PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
82742cbb9b06SVaclav Hapla           break;
82752cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
827698921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
82772cbb9b06SVaclav Hapla         default:
82787a8be351SBarry Smith           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
82792cbb9b06SVaclav Hapla       }
82802cbb9b06SVaclav Hapla     }
82815d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
82829566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
82835d80c0bfSVaclav Hapla     } else {
82845d80c0bfSVaclav Hapla       labelNew = label;
82855d80c0bfSVaclav Hapla     }
82869566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
82879566063dSJacob Faibussowitsch     if (mode==PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
8288c58f1c22SToby Isaac   }
8289c58f1c22SToby Isaac   PetscFunctionReturn(0);
8290c58f1c22SToby Isaac }
8291461a15a0SLisandro Dalcin 
8292609dae6eSVaclav Hapla /*@C
8293609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
8294609dae6eSVaclav Hapla 
82955efe38ccSVaclav Hapla   Collective
8296609dae6eSVaclav Hapla 
8297609dae6eSVaclav Hapla   Input Parameters:
8298609dae6eSVaclav Hapla + dm0 - First DM object
8299609dae6eSVaclav Hapla - dm1 - Second DM object
8300609dae6eSVaclav Hapla 
8301609dae6eSVaclav Hapla   Output Parameters
83025efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
8303609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
8304609dae6eSVaclav Hapla 
8305609dae6eSVaclav Hapla   Level: intermediate
8306609dae6eSVaclav Hapla 
8307609dae6eSVaclav Hapla   Notes:
83085efe38ccSVaclav Hapla   The output flag equal is the same on all processes.
83095efe38ccSVaclav Hapla   If it is passed as NULL and difference is found, an error is thrown on all processes.
83105efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8311609dae6eSVaclav Hapla 
83125efe38ccSVaclav Hapla   The output message is set independently on each rank.
83135efe38ccSVaclav Hapla   It is set to NULL if no difference was found on the current rank. It must be freed by user.
83145efe38ccSVaclav Hapla   If message is passed as NULL and difference is found, the difference description is printed to stderr in synchronized manner.
83155efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8316609dae6eSVaclav Hapla 
8317609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
8318609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
8319609dae6eSVaclav Hapla 
8320609dae6eSVaclav Hapla   Fortran Notes:
8321609dae6eSVaclav Hapla   This function is currently not available from Fortran.
8322609dae6eSVaclav Hapla 
8323db781477SPatrick Sanan .seealso: `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
8324609dae6eSVaclav Hapla @*/
83255efe38ccSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
8326609dae6eSVaclav Hapla {
83275efe38ccSVaclav Hapla   PetscInt        n, i;
8328609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
83295efe38ccSVaclav Hapla   PetscBool       eq;
8330609dae6eSVaclav Hapla   MPI_Comm        comm;
83315efe38ccSVaclav Hapla   PetscMPIInt     rank;
8332609dae6eSVaclav Hapla 
8333609dae6eSVaclav Hapla   PetscFunctionBegin;
8334609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
8335609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
8336609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
83375efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal,3);
8338609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
83399566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
83409566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
83415efe38ccSVaclav Hapla   {
83425efe38ccSVaclav Hapla     PetscInt n1;
83435efe38ccSVaclav Hapla 
83449566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
83459566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
83465efe38ccSVaclav Hapla     eq = (PetscBool) (n == n1);
83475efe38ccSVaclav Hapla     if (!eq) {
834863a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
8349609dae6eSVaclav Hapla     }
83509566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
83515efe38ccSVaclav Hapla     if (!eq) goto finish;
83525efe38ccSVaclav Hapla   }
83535efe38ccSVaclav Hapla   for (i=0; i<n; i++) {
8354609dae6eSVaclav Hapla     DMLabel     l0, l1;
8355609dae6eSVaclav Hapla     const char *name;
8356609dae6eSVaclav Hapla     char       *msgInner;
8357609dae6eSVaclav Hapla 
8358609dae6eSVaclav Hapla     /* Ignore label order */
83599566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
83609566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
83619566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
8362609dae6eSVaclav Hapla     if (!l1) {
836363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
83645efe38ccSVaclav Hapla       eq = PETSC_FALSE;
83655efe38ccSVaclav Hapla       break;
8366609dae6eSVaclav Hapla     }
83679566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
83689566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
83699566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
83705efe38ccSVaclav Hapla     if (!eq) break;
8371609dae6eSVaclav Hapla   }
83729566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
8373609dae6eSVaclav Hapla finish:
83745efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
8375609dae6eSVaclav Hapla   if (message) {
8376609dae6eSVaclav Hapla     *message = NULL;
8377609dae6eSVaclav Hapla     if (msg[0]) {
83789566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(msg, message));
8379609dae6eSVaclav Hapla     }
83805efe38ccSVaclav Hapla   } else {
83815efe38ccSVaclav Hapla     if (msg[0]) {
83829566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
8383609dae6eSVaclav Hapla     }
83849566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
83855efe38ccSVaclav Hapla   }
83865efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
83875efe38ccSVaclav Hapla   if (equal) *equal = eq;
83887a8be351SBarry Smith   else PetscCheck(eq,comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
8389609dae6eSVaclav Hapla   PetscFunctionReturn(0);
8390609dae6eSVaclav Hapla }
8391609dae6eSVaclav Hapla 
8392461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8393461a15a0SLisandro Dalcin {
8394461a15a0SLisandro Dalcin   PetscFunctionBegin;
8395461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8396461a15a0SLisandro Dalcin   if (!*label) {
83979566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
83989566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
8399461a15a0SLisandro Dalcin   }
84009566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
8401461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8402461a15a0SLisandro Dalcin }
8403461a15a0SLisandro Dalcin 
84040fdc7489SMatthew Knepley /*
84050fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
84060fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
84070fdc7489SMatthew Knepley   (label, id) pair in the DM.
84080fdc7489SMatthew Knepley 
84090fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
84100fdc7489SMatthew Knepley   each label.
84110fdc7489SMatthew Knepley */
84120fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
84130fdc7489SMatthew Knepley {
84140fdc7489SMatthew Knepley   DMUniversalLabel ul;
84150fdc7489SMatthew Knepley   PetscBool       *active;
84160fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
84170fdc7489SMatthew Knepley 
84180fdc7489SMatthew Knepley   PetscFunctionBegin;
84199566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
84209566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
84219566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
84229566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
84230fdc7489SMatthew Knepley   ul->Nl = 0;
84240fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
84250fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
84260fdc7489SMatthew Knepley     const char *name;
84270fdc7489SMatthew Knepley 
84289566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
84299566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
84309566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
84310fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
84320fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
84330fdc7489SMatthew Knepley   }
84349566063dSJacob 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));
84350fdc7489SMatthew Knepley   ul->Nv = 0;
84360fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84370fdc7489SMatthew Knepley     DMLabel     label;
84380fdc7489SMatthew Knepley     PetscInt    nv;
84390fdc7489SMatthew Knepley     const char *name;
84400fdc7489SMatthew Knepley 
84410fdc7489SMatthew Knepley     if (!active[l]) continue;
84429566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
84439566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
84449566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
84459566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
84460fdc7489SMatthew Knepley     ul->indices[m]   = l;
84470fdc7489SMatthew Knepley     ul->Nv          += nv;
84480fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
84490fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
84500fdc7489SMatthew Knepley     ++m;
84510fdc7489SMatthew Knepley   }
84520fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
84530fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
84540fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
84550fdc7489SMatthew Knepley   }
84560fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
84570fdc7489SMatthew Knepley     PetscInt b;
84580fdc7489SMatthew Knepley 
84590fdc7489SMatthew Knepley     ul->masks[l] = 0;
84600fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
84610fdc7489SMatthew Knepley   }
84629566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
84630fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84640fdc7489SMatthew Knepley     DMLabel         label;
84650fdc7489SMatthew Knepley     IS              valueIS;
84660fdc7489SMatthew Knepley     const PetscInt *varr;
84670fdc7489SMatthew Knepley     PetscInt        nv, v;
84680fdc7489SMatthew Knepley 
84690fdc7489SMatthew Knepley     if (!active[l]) continue;
84709566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
84719566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
84729566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
84739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
84740fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
84750fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
84760fdc7489SMatthew Knepley     }
84779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
84789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
84799566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
84800fdc7489SMatthew Knepley     ++m;
84810fdc7489SMatthew Knepley   }
84829566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
84830fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
84840fdc7489SMatthew Knepley     PetscInt  uval = 0;
84850fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
84860fdc7489SMatthew Knepley 
84870fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
84880fdc7489SMatthew Knepley       DMLabel  label;
84890649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
84900fdc7489SMatthew Knepley 
84910fdc7489SMatthew Knepley       if (!active[l]) continue;
84929566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
84939566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
84949566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
84950fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
84960649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
84970fdc7489SMatthew Knepley       marked = PETSC_TRUE;
84989566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
849963a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
85000fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
85010fdc7489SMatthew Knepley       ++m;
85020fdc7489SMatthew Knepley     }
85039566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
85040fdc7489SMatthew Knepley   }
85059566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
85060fdc7489SMatthew Knepley   *universal = ul;
85070fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85080fdc7489SMatthew Knepley }
85090fdc7489SMatthew Knepley 
85100fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
85110fdc7489SMatthew Knepley {
85120fdc7489SMatthew Knepley   PetscInt       l;
85130fdc7489SMatthew Knepley 
85140fdc7489SMatthew Knepley   PetscFunctionBegin;
85159566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
85169566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
85179566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
85189566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
85199566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
85200fdc7489SMatthew Knepley   *universal = NULL;
85210fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85220fdc7489SMatthew Knepley }
85230fdc7489SMatthew Knepley 
85240fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
85250fdc7489SMatthew Knepley {
85260fdc7489SMatthew Knepley   PetscFunctionBegin;
85270fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
85280fdc7489SMatthew Knepley   *ulabel = ul->label;
85290fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85300fdc7489SMatthew Knepley }
85310fdc7489SMatthew Knepley 
85320fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
85330fdc7489SMatthew Knepley {
85340fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
85350fdc7489SMatthew Knepley 
85360fdc7489SMatthew Knepley   PetscFunctionBegin;
8537064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
85380fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
85399566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
85409566063dSJacob Faibussowitsch     else               PetscCall(DMCreateLabel(dm, ul->names[l]));
85410fdc7489SMatthew Knepley   }
85420fdc7489SMatthew Knepley   if (preserveOrder) {
85430fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
85440fdc7489SMatthew Knepley       const char *name;
85450fdc7489SMatthew Knepley       PetscBool   match;
85460fdc7489SMatthew Knepley 
85479566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
85489566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
854963a3b9bcSJacob 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]);
85500fdc7489SMatthew Knepley     }
85510fdc7489SMatthew Knepley   }
85520fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85530fdc7489SMatthew Knepley }
85540fdc7489SMatthew Knepley 
85550fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
85560fdc7489SMatthew Knepley {
85570fdc7489SMatthew Knepley   PetscInt       l;
85580fdc7489SMatthew Knepley 
85590fdc7489SMatthew Knepley   PetscFunctionBegin;
85600fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
85610fdc7489SMatthew Knepley     DMLabel  label;
85620fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
85630fdc7489SMatthew Knepley 
85640fdc7489SMatthew Knepley     if (lval) {
85659566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
85669566063dSJacob Faibussowitsch       else          PetscCall(DMGetLabel(dm, ul->names[l], &label));
85679566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]));
85680fdc7489SMatthew Knepley     }
85690fdc7489SMatthew Knepley   }
85700fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85710fdc7489SMatthew Knepley }
8572a8fb8f29SToby Isaac 
8573a8fb8f29SToby Isaac /*@
8574a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8575a8fb8f29SToby Isaac 
8576a8fb8f29SToby Isaac   Input Parameter:
8577a8fb8f29SToby Isaac . dm - The DM object
8578a8fb8f29SToby Isaac 
8579a8fb8f29SToby Isaac   Output Parameter:
8580a8fb8f29SToby Isaac . cdm - The coarse DM
8581a8fb8f29SToby Isaac 
8582a8fb8f29SToby Isaac   Level: intermediate
8583a8fb8f29SToby Isaac 
8584db781477SPatrick Sanan .seealso: `DMSetCoarseDM()`
8585a8fb8f29SToby Isaac @*/
8586a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8587a8fb8f29SToby Isaac {
8588a8fb8f29SToby Isaac   PetscFunctionBegin;
8589a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8590a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8591a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8592a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8593a8fb8f29SToby Isaac }
8594a8fb8f29SToby Isaac 
8595a8fb8f29SToby Isaac /*@
8596a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8597a8fb8f29SToby Isaac 
8598a8fb8f29SToby Isaac   Input Parameters:
8599a8fb8f29SToby Isaac + dm - The DM object
8600a8fb8f29SToby Isaac - cdm - The coarse DM
8601a8fb8f29SToby Isaac 
8602a8fb8f29SToby Isaac   Level: intermediate
8603a8fb8f29SToby Isaac 
8604db781477SPatrick Sanan .seealso: `DMGetCoarseDM()`
8605a8fb8f29SToby Isaac @*/
8606a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8607a8fb8f29SToby Isaac {
8608a8fb8f29SToby Isaac   PetscFunctionBegin;
8609a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8610a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
86119566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
86129566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
8613a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8614a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8615a8fb8f29SToby Isaac }
8616a8fb8f29SToby Isaac 
861788bdff64SToby Isaac /*@
861888bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
861988bdff64SToby Isaac 
862088bdff64SToby Isaac   Input Parameter:
862188bdff64SToby Isaac . dm - The DM object
862288bdff64SToby Isaac 
862388bdff64SToby Isaac   Output Parameter:
862488bdff64SToby Isaac . fdm - The fine DM
862588bdff64SToby Isaac 
862688bdff64SToby Isaac   Level: intermediate
862788bdff64SToby Isaac 
8628db781477SPatrick Sanan .seealso: `DMSetFineDM()`
862988bdff64SToby Isaac @*/
863088bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
863188bdff64SToby Isaac {
863288bdff64SToby Isaac   PetscFunctionBegin;
863388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
863488bdff64SToby Isaac   PetscValidPointer(fdm, 2);
863588bdff64SToby Isaac   *fdm = dm->fineMesh;
863688bdff64SToby Isaac   PetscFunctionReturn(0);
863788bdff64SToby Isaac }
863888bdff64SToby Isaac 
863988bdff64SToby Isaac /*@
864088bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
864188bdff64SToby Isaac 
864288bdff64SToby Isaac   Input Parameters:
864388bdff64SToby Isaac + dm - The DM object
864488bdff64SToby Isaac - fdm - The fine DM
864588bdff64SToby Isaac 
864688bdff64SToby Isaac   Level: intermediate
864788bdff64SToby Isaac 
8648db781477SPatrick Sanan .seealso: `DMGetFineDM()`
864988bdff64SToby Isaac @*/
865088bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
865188bdff64SToby Isaac {
865288bdff64SToby Isaac   PetscFunctionBegin;
865388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
865488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
86559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
86569566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
865788bdff64SToby Isaac   dm->fineMesh = fdm;
865888bdff64SToby Isaac   PetscFunctionReturn(0);
865988bdff64SToby Isaac }
866088bdff64SToby Isaac 
8661a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8662a6ba4734SToby Isaac 
8663a6ba4734SToby Isaac /*@C
8664a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8665a6ba4734SToby Isaac 
8666783e2ec8SMatthew G. Knepley   Collective on dm
8667783e2ec8SMatthew G. Knepley 
8668a6ba4734SToby Isaac   Input Parameters:
86694c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8670f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8671a6ba4734SToby Isaac . name     - The BC name
867245480ffeSMatthew G. Knepley . label    - The label defining constrained points
867345480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
867445480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8675a6ba4734SToby Isaac . field    - The field to constrain
867645480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8677a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8678a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
867956cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8680a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8681a6ba4734SToby Isaac 
868245480ffeSMatthew G. Knepley   Output Parameter:
868345480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
868445480ffeSMatthew G. Knepley 
8685a6ba4734SToby Isaac   Options Database Keys:
8686a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8687a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8688a6ba4734SToby Isaac 
868956cf3b9cSMatthew G. Knepley   Note:
869056cf3b9cSMatthew 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:
869156cf3b9cSMatthew G. Knepley 
869256cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
869356cf3b9cSMatthew G. Knepley 
869456cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
869556cf3b9cSMatthew G. Knepley 
869656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
869756cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
869856cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
869956cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
870056cf3b9cSMatthew G. Knepley 
870156cf3b9cSMatthew G. Knepley + dim - the spatial dimension
870256cf3b9cSMatthew G. Knepley . Nf - the number of fields
870356cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
870456cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
870556cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
870656cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
870756cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
870856cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
870956cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
871056cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
871156cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
871256cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
871356cf3b9cSMatthew G. Knepley . t - current time
871456cf3b9cSMatthew G. Knepley . x - coordinates of the current point
871556cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
871656cf3b9cSMatthew G. Knepley . constants - constant parameters
871756cf3b9cSMatthew G. Knepley - bcval - output values at the current point
871856cf3b9cSMatthew G. Knepley 
8719ed808b8fSJed Brown   Level: intermediate
8720a6ba4734SToby Isaac 
8721db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
8722a6ba4734SToby Isaac @*/
872345480ffeSMatthew 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)
8724a6ba4734SToby Isaac {
8725e5e52638SMatthew G. Knepley   PetscDS        ds;
8726a6ba4734SToby Isaac 
8727a6ba4734SToby Isaac   PetscFunctionBegin;
8728a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8729783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
873045480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
873145480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
873245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
873345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
873401a5d20dSJed Brown   PetscCheck(!dm->localSection,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot add boundary to DM after creating local section");
87359566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8736799db056SMatthew G. Knepley   /* Complete label */
8737799db056SMatthew G. Knepley   if (label) {
8738799db056SMatthew G. Knepley     PetscObject  obj;
8739799db056SMatthew G. Knepley     PetscClassId id;
8740799db056SMatthew G. Knepley 
8741799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8742799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8743799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8744799db056SMatthew G. Knepley       DM plex;
8745799db056SMatthew G. Knepley 
8746799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8747799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8748799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8749799db056SMatthew G. Knepley     }
8750799db056SMatthew G. Knepley   }
87519566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
8752a6ba4734SToby Isaac   PetscFunctionReturn(0);
8753a6ba4734SToby Isaac }
8754a6ba4734SToby Isaac 
875545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8756e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8757e6f8dbb6SToby Isaac {
8758e5e52638SMatthew G. Knepley   PetscDS        ds;
8759dff059c6SToby Isaac   DMBoundary    *lastnext;
8760e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8761e6f8dbb6SToby Isaac 
8762e6f8dbb6SToby Isaac   PetscFunctionBegin;
87639566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8764e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
876547a1f5adSToby Isaac   if (dm->boundary) {
876647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
876747a1f5adSToby Isaac 
876847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
876947a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
877047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
877147a1f5adSToby Isaac     while (next) {
877247a1f5adSToby Isaac       DMBoundary b = next;
877347a1f5adSToby Isaac 
877447a1f5adSToby Isaac       next = b->next;
87759566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8776a6ba4734SToby Isaac     }
877747a1f5adSToby Isaac     dm->boundary = NULL;
8778a6ba4734SToby Isaac   }
877947a1f5adSToby Isaac 
8780dff059c6SToby Isaac   lastnext = &(dm->boundary);
8781e6f8dbb6SToby Isaac   while (dsbound) {
8782e6f8dbb6SToby Isaac     DMBoundary dmbound;
8783e6f8dbb6SToby Isaac 
87849566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8785e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
878645480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
878747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8788dff059c6SToby Isaac     *lastnext = dmbound;
8789dff059c6SToby Isaac     lastnext = &(dmbound->next);
8790dff059c6SToby Isaac     dsbound = dsbound->next;
8791a6ba4734SToby Isaac   }
8792a6ba4734SToby Isaac   PetscFunctionReturn(0);
8793a6ba4734SToby Isaac }
8794a6ba4734SToby Isaac 
8795a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8796a6ba4734SToby Isaac {
8797b95f2879SToby Isaac   DMBoundary     b;
8798a6ba4734SToby Isaac 
8799a6ba4734SToby Isaac   PetscFunctionBegin;
8800a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8801534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8802a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
88039566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8804b95f2879SToby Isaac   b = dm->boundary;
8805a6ba4734SToby Isaac   while (b && !(*isBd)) {
8806e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8807e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8808a6ba4734SToby Isaac     PetscInt   i;
8809a6ba4734SToby Isaac 
881045480ffeSMatthew G. Knepley     if (label) {
88119566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8812a6ba4734SToby Isaac     }
8813a6ba4734SToby Isaac     b = b->next;
8814a6ba4734SToby Isaac   }
8815a6ba4734SToby Isaac   PetscFunctionReturn(0);
8816a6ba4734SToby Isaac }
88174d6f44ffSToby Isaac 
88184d6f44ffSToby Isaac /*@C
8819a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8820a6e0b375SMatthew G. Knepley 
8821a6e0b375SMatthew G. Knepley   Collective on DM
88224d6f44ffSToby Isaac 
88234d6f44ffSToby Isaac   Input Parameters:
88244d6f44ffSToby Isaac + dm      - The DM
88250709b2feSToby Isaac . time    - The time
88264d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
88274d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
88284d6f44ffSToby Isaac - mode    - The insertion mode for values
88294d6f44ffSToby Isaac 
88304d6f44ffSToby Isaac   Output Parameter:
88314d6f44ffSToby Isaac . X - vector
88324d6f44ffSToby Isaac 
88334d6f44ffSToby Isaac    Calling sequence of func:
883477b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
88354d6f44ffSToby Isaac 
88364d6f44ffSToby Isaac +  dim - The spatial dimension
88378ec8862eSJed Brown .  time - The time at which to sample
88384d6f44ffSToby Isaac .  x   - The coordinates
883977b739a6SMatthew Knepley .  Nc  - The number of components
88404d6f44ffSToby Isaac .  u   - The output field values
88414d6f44ffSToby Isaac -  ctx - optional user-defined function context
88424d6f44ffSToby Isaac 
88434d6f44ffSToby Isaac   Level: developer
88444d6f44ffSToby Isaac 
8845db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
88464d6f44ffSToby Isaac @*/
88470709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
88484d6f44ffSToby Isaac {
88494d6f44ffSToby Isaac   Vec            localX;
88504d6f44ffSToby Isaac 
88514d6f44ffSToby Isaac   PetscFunctionBegin;
88524d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88539566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
88549566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
88559566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
88569566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
88579566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
88584d6f44ffSToby Isaac   PetscFunctionReturn(0);
88594d6f44ffSToby Isaac }
88604d6f44ffSToby Isaac 
8861a6e0b375SMatthew G. Knepley /*@C
8862a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8863a6e0b375SMatthew G. Knepley 
8864a6e0b375SMatthew G. Knepley   Not collective
8865a6e0b375SMatthew G. Knepley 
8866a6e0b375SMatthew G. Knepley   Input Parameters:
8867a6e0b375SMatthew G. Knepley + dm      - The DM
8868a6e0b375SMatthew G. Knepley . time    - The time
8869a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8870a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8871a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8872a6e0b375SMatthew G. Knepley 
8873a6e0b375SMatthew G. Knepley   Output Parameter:
8874a6e0b375SMatthew G. Knepley . localX - vector
8875a6e0b375SMatthew G. Knepley 
8876a6e0b375SMatthew G. Knepley    Calling sequence of func:
887777b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8878a6e0b375SMatthew G. Knepley 
8879a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8880a6e0b375SMatthew G. Knepley .  x   - The coordinates
888177b739a6SMatthew Knepley .  Nc  - The number of components
8882a6e0b375SMatthew G. Knepley .  u   - The output field values
8883a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8884a6e0b375SMatthew G. Knepley 
8885a6e0b375SMatthew G. Knepley   Level: developer
8886a6e0b375SMatthew G. Knepley 
8887db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8888a6e0b375SMatthew G. Knepley @*/
88890709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
88904d6f44ffSToby Isaac {
88914d6f44ffSToby Isaac   PetscFunctionBegin;
88924d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8893064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
88947a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
88959566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX));
88964d6f44ffSToby Isaac   PetscFunctionReturn(0);
88974d6f44ffSToby Isaac }
88984d6f44ffSToby Isaac 
8899a6e0b375SMatthew G. Knepley /*@C
8900a6e0b375SMatthew 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.
8901a6e0b375SMatthew G. Knepley 
8902a6e0b375SMatthew G. Knepley   Collective on DM
8903a6e0b375SMatthew G. Knepley 
8904a6e0b375SMatthew G. Knepley   Input Parameters:
8905a6e0b375SMatthew G. Knepley + dm      - The DM
8906a6e0b375SMatthew G. Knepley . time    - The time
8907a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8908a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8909a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8910a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8911a6e0b375SMatthew G. Knepley 
8912a6e0b375SMatthew G. Knepley   Output Parameter:
8913a6e0b375SMatthew G. Knepley . X - vector
8914a6e0b375SMatthew G. Knepley 
8915a6e0b375SMatthew G. Knepley    Calling sequence of func:
891677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8917a6e0b375SMatthew G. Knepley 
8918a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8919a6e0b375SMatthew G. Knepley .  x   - The coordinates
892077b739a6SMatthew Knepley .  Nc  - The number of components
8921a6e0b375SMatthew G. Knepley .  u   - The output field values
8922a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8923a6e0b375SMatthew G. Knepley 
8924a6e0b375SMatthew G. Knepley   Level: developer
8925a6e0b375SMatthew G. Knepley 
8926db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8927a6e0b375SMatthew G. Knepley @*/
89282c53366bSMatthew 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)
89292c53366bSMatthew G. Knepley {
89302c53366bSMatthew G. Knepley   Vec            localX;
89312c53366bSMatthew G. Knepley 
89322c53366bSMatthew G. Knepley   PetscFunctionBegin;
89332c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89349566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
89359566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
89369566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
89379566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
89389566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
89392c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
89402c53366bSMatthew G. Knepley }
89412c53366bSMatthew G. Knepley 
8942a6e0b375SMatthew G. Knepley /*@C
8943a6e0b375SMatthew 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.
8944a6e0b375SMatthew G. Knepley 
8945a6e0b375SMatthew G. Knepley   Not collective
8946a6e0b375SMatthew G. Knepley 
8947a6e0b375SMatthew G. Knepley   Input Parameters:
8948a6e0b375SMatthew G. Knepley + dm      - The DM
8949a6e0b375SMatthew G. Knepley . time    - The time
8950a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8951a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8952a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8953a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8954a6e0b375SMatthew G. Knepley 
8955a6e0b375SMatthew G. Knepley   Output Parameter:
8956a6e0b375SMatthew G. Knepley . localX - vector
8957a6e0b375SMatthew G. Knepley 
8958a6e0b375SMatthew G. Knepley    Calling sequence of func:
895977b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8960a6e0b375SMatthew G. Knepley 
8961a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8962a6e0b375SMatthew G. Knepley .  x   - The coordinates
896377b739a6SMatthew Knepley .  Nc  - The number of components
8964a6e0b375SMatthew G. Knepley .  u   - The output field values
8965a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8966a6e0b375SMatthew G. Knepley 
8967a6e0b375SMatthew G. Knepley   Level: developer
8968a6e0b375SMatthew G. Knepley 
8969db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8970a6e0b375SMatthew G. Knepley @*/
89711c531cf8SMatthew 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)
89724d6f44ffSToby Isaac {
89734d6f44ffSToby Isaac   PetscFunctionBegin;
89744d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8975064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
89767a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
89779566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
89784d6f44ffSToby Isaac   PetscFunctionReturn(0);
89794d6f44ffSToby Isaac }
89802716604bSToby Isaac 
8981a6e0b375SMatthew G. Knepley /*@C
8982a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8983a6e0b375SMatthew G. Knepley 
8984a6e0b375SMatthew G. Knepley   Not collective
8985a6e0b375SMatthew G. Knepley 
8986a6e0b375SMatthew G. Knepley   Input Parameters:
8987a6e0b375SMatthew G. Knepley + dm      - The DM
8988a6e0b375SMatthew G. Knepley . time    - The time
8989a6e0b375SMatthew G. Knepley . localU  - The input field vector
8990a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8991a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8992a6e0b375SMatthew G. Knepley 
8993a6e0b375SMatthew G. Knepley   Output Parameter:
8994a6e0b375SMatthew G. Knepley . localX  - The output vector
8995a6e0b375SMatthew G. Knepley 
8996a6e0b375SMatthew G. Knepley    Calling sequence of func:
8997a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8998a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8999a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9000a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9001a6e0b375SMatthew G. Knepley 
9002a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9003a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9004a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9005a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9006a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9007a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9008a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9009a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9010a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9011a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9012a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9013a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9014a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9015a6e0b375SMatthew G. Knepley .  t            - The current time
9016a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9017a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9018a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9019a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9020a6e0b375SMatthew G. Knepley 
9021a6e0b375SMatthew 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.
9022a6e0b375SMatthew 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
9023a6e0b375SMatthew 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
9024a6e0b375SMatthew 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.
9025a6e0b375SMatthew G. Knepley 
9026a6e0b375SMatthew G. Knepley   Level: intermediate
9027a6e0b375SMatthew G. Knepley 
9028db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
9029a6e0b375SMatthew G. Knepley @*/
90308c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
90318c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
90328c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
90338c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9034191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
90358c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
90368c6c5593SMatthew G. Knepley {
90378c6c5593SMatthew G. Knepley   PetscFunctionBegin;
90388c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90398c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
90408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
90417a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
90429566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX));
90438c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
90448c6c5593SMatthew G. Knepley }
90458c6c5593SMatthew G. Knepley 
9046a6e0b375SMatthew G. Knepley /*@C
9047a6e0b375SMatthew 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.
9048a6e0b375SMatthew G. Knepley 
9049a6e0b375SMatthew G. Knepley   Not collective
9050a6e0b375SMatthew G. Knepley 
9051a6e0b375SMatthew G. Knepley   Input Parameters:
9052a6e0b375SMatthew G. Knepley + dm      - The DM
9053a6e0b375SMatthew G. Knepley . time    - The time
9054a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
9055a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
9056a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
9057a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9058a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9059a6e0b375SMatthew G. Knepley . localU  - The input field vector
9060a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9061a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9062a6e0b375SMatthew G. Knepley 
9063a6e0b375SMatthew G. Knepley   Output Parameter:
9064a6e0b375SMatthew G. Knepley . localX  - The output vector
9065a6e0b375SMatthew G. Knepley 
9066a6e0b375SMatthew G. Knepley    Calling sequence of func:
9067a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9068a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9069a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9070a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9071a6e0b375SMatthew G. Knepley 
9072a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9073a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9074a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9075a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9076a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9077a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9078a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9079a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9080a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9081a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9082a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9083a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9084a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9085a6e0b375SMatthew G. Knepley .  t            - The current time
9086a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9087a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9088a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9089a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9090a6e0b375SMatthew G. Knepley 
9091a6e0b375SMatthew 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.
9092a6e0b375SMatthew 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
9093a6e0b375SMatthew 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
9094a6e0b375SMatthew 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.
9095a6e0b375SMatthew G. Knepley 
9096a6e0b375SMatthew G. Knepley   Level: intermediate
9097a6e0b375SMatthew G. Knepley 
9098db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
9099a6e0b375SMatthew G. Knepley @*/
91001c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
91018c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
91028c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
91038c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9104191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
91058c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
91068c6c5593SMatthew G. Knepley {
91078c6c5593SMatthew G. Knepley   PetscFunctionBegin;
91088c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9109064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9110064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
91117a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
91129566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
91138c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
91148c6c5593SMatthew G. Knepley }
91158c6c5593SMatthew G. Knepley 
91162716604bSToby Isaac /*@C
9117ece3a9fcSMatthew 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.
9118ece3a9fcSMatthew G. Knepley 
9119ece3a9fcSMatthew G. Knepley   Not collective
9120ece3a9fcSMatthew G. Knepley 
9121ece3a9fcSMatthew G. Knepley   Input Parameters:
9122ece3a9fcSMatthew G. Knepley + dm      - The DM
9123ece3a9fcSMatthew G. Knepley . time    - The time
9124ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
9125ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
9126ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
9127ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9128ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9129ece3a9fcSMatthew G. Knepley . localU  - The input field vector
9130ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9131ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
9132ece3a9fcSMatthew G. Knepley 
9133ece3a9fcSMatthew G. Knepley   Output Parameter:
9134ece3a9fcSMatthew G. Knepley . localX  - The output vector
9135ece3a9fcSMatthew G. Knepley 
9136ece3a9fcSMatthew G. Knepley    Calling sequence of func:
9137ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9138ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9139ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9140ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9141ece3a9fcSMatthew G. Knepley 
9142ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9143ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9144ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9145ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9146ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9147ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9148ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9149ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9150ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9151ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9152ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9153ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9154ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9155ece3a9fcSMatthew G. Knepley .  t            - The current time
9156ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9157ece3a9fcSMatthew G. Knepley .  n            - The face normal
9158ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9159ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9160ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9161ece3a9fcSMatthew G. Knepley 
9162ece3a9fcSMatthew G. Knepley   Note:
9163ece3a9fcSMatthew 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.
9164ece3a9fcSMatthew 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
9165ece3a9fcSMatthew 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
9166ece3a9fcSMatthew 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.
9167ece3a9fcSMatthew G. Knepley 
9168ece3a9fcSMatthew G. Knepley   Level: intermediate
9169ece3a9fcSMatthew G. Knepley 
9170db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
9171ece3a9fcSMatthew G. Knepley @*/
9172ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9173ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9174ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9175ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9176ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9177ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9178ece3a9fcSMatthew G. Knepley {
9179ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9180ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9181064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9182064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
91837a8be351SBarry Smith   PetscCheck(dm->ops->projectbdfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
91849566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
9185ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9186ece3a9fcSMatthew G. Knepley }
9187ece3a9fcSMatthew G. Knepley 
9188ece3a9fcSMatthew G. Knepley /*@C
91892716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
91902716604bSToby Isaac 
91912716604bSToby Isaac   Input Parameters:
91922716604bSToby Isaac + dm    - The DM
91930709b2feSToby Isaac . time  - The time
91942716604bSToby Isaac . funcs - The functions to evaluate for each field component
91952716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9196574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
91972716604bSToby Isaac 
91982716604bSToby Isaac   Output Parameter:
91992716604bSToby Isaac . diff - The diff ||u - u_h||_2
92002716604bSToby Isaac 
92012716604bSToby Isaac   Level: developer
92022716604bSToby Isaac 
9203db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
92042716604bSToby Isaac @*/
92050709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
92062716604bSToby Isaac {
92072716604bSToby Isaac   PetscFunctionBegin;
92082716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9209b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92107a8be351SBarry Smith   PetscCheck(dm->ops->computel2diff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
92119566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff));
92122716604bSToby Isaac   PetscFunctionReturn(0);
92132716604bSToby Isaac }
9214b698f381SToby Isaac 
9215b698f381SToby Isaac /*@C
9216b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9217b698f381SToby Isaac 
9218d083f849SBarry Smith   Collective on dm
9219d083f849SBarry Smith 
9220b698f381SToby Isaac   Input Parameters:
9221b698f381SToby Isaac + dm    - The DM
9222b698f381SToby Isaac , time  - The time
9223b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9224b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9225574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9226b698f381SToby Isaac - n     - The vector to project along
9227b698f381SToby Isaac 
9228b698f381SToby Isaac   Output Parameter:
9229b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9230b698f381SToby Isaac 
9231b698f381SToby Isaac   Level: developer
9232b698f381SToby Isaac 
9233db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`
9234b698f381SToby Isaac @*/
9235b698f381SToby 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)
9236b698f381SToby Isaac {
9237b698f381SToby Isaac   PetscFunctionBegin;
9238b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9239b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92407a8be351SBarry Smith   PetscCheck(dm->ops->computel2gradientdiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
92419566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff));
9242b698f381SToby Isaac   PetscFunctionReturn(0);
9243b698f381SToby Isaac }
9244b698f381SToby Isaac 
92452a16baeaSToby Isaac /*@C
92462a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
92472a16baeaSToby Isaac 
9248d083f849SBarry Smith   Collective on dm
9249d083f849SBarry Smith 
92502a16baeaSToby Isaac   Input Parameters:
92512a16baeaSToby Isaac + dm    - The DM
92522a16baeaSToby Isaac . time  - The time
92532a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
92542a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9255574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
92562a16baeaSToby Isaac 
92572a16baeaSToby Isaac   Output Parameter:
92582a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
92592a16baeaSToby Isaac 
92602a16baeaSToby Isaac   Level: developer
92612a16baeaSToby Isaac 
9262db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
92632a16baeaSToby Isaac @*/
92641189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
92652a16baeaSToby Isaac {
92662a16baeaSToby Isaac   PetscFunctionBegin;
92672a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92682a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92697a8be351SBarry Smith   PetscCheck(dm->ops->computel2fielddiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
92709566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff));
92712a16baeaSToby Isaac   PetscFunctionReturn(0);
92722a16baeaSToby Isaac }
92732a16baeaSToby Isaac 
9274df0b854cSToby Isaac /*@C
9275502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9276502a2867SDave May 
9277502a2867SDave May  Not Collective
9278502a2867SDave May 
9279502a2867SDave May  Input Parameter:
9280502a2867SDave May .  dm    - The DM
9281502a2867SDave May 
92820a19bb7dSprj-  Output Parameters:
92830a19bb7dSprj- +  nranks - the number of neighbours
92840a19bb7dSprj- -  ranks - the neighbors ranks
9285502a2867SDave May 
9286502a2867SDave May  Notes:
9287502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9288502a2867SDave May 
9289502a2867SDave May  Level: beginner
9290502a2867SDave May 
9291db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
9292502a2867SDave May @*/
9293502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9294502a2867SDave May {
9295502a2867SDave May   PetscFunctionBegin;
9296502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92977a8be351SBarry Smith   PetscCheck(dm->ops->getneighbors,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
92989566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm,nranks,ranks));
9299502a2867SDave May   PetscFunctionReturn(0);
9300502a2867SDave May }
9301502a2867SDave May 
9302531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9303531c7667SBarry Smith 
9304531c7667SBarry Smith /*
9305531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9306531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9307531c7667SBarry Smith */
9308531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9309531c7667SBarry Smith {
9310531c7667SBarry Smith   PetscFunctionBegin;
9311531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9312531c7667SBarry Smith     Vec x1local;
9313531c7667SBarry Smith     DM  dm;
93149566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
93157a8be351SBarry Smith     PetscCheck(dm,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
93169566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm,&x1local));
93179566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local));
93189566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local));
9319531c7667SBarry Smith     x1   = x1local;
9320531c7667SBarry Smith   }
93219566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J,coloring,x1,sctx));
9322531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9323531c7667SBarry Smith     DM  dm;
93249566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J,&dm));
93259566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm,&x1));
9326531c7667SBarry Smith   }
9327531c7667SBarry Smith   PetscFunctionReturn(0);
9328531c7667SBarry Smith }
9329531c7667SBarry Smith 
9330531c7667SBarry Smith /*@
9331531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9332531c7667SBarry Smith 
9333531c7667SBarry Smith     Input Parameter:
9334531c7667SBarry Smith .    coloring - the MatFDColoring object
9335531c7667SBarry Smith 
933695452b02SPatrick Sanan     Developer Notes:
933795452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9338531c7667SBarry Smith 
93391b266c99SBarry Smith     Level: advanced
93401b266c99SBarry Smith 
9341db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
9342531c7667SBarry Smith @*/
9343531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9344531c7667SBarry Smith {
9345531c7667SBarry Smith   PetscFunctionBegin;
9346531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9347531c7667SBarry Smith   PetscFunctionReturn(0);
9348531c7667SBarry Smith }
93498320bc6fSPatrick Sanan 
93508320bc6fSPatrick Sanan /*@
93518320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
93528320bc6fSPatrick Sanan 
93538320bc6fSPatrick Sanan     Collective
93548320bc6fSPatrick Sanan 
93558320bc6fSPatrick Sanan     Input Parameters:
9356a5bc1bf3SBarry Smith +    dm1 - the first DM
93578320bc6fSPatrick Sanan -    dm2 - the second DM
93588320bc6fSPatrick Sanan 
93598320bc6fSPatrick Sanan     Output Parameters:
93608320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
93618320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
93628320bc6fSPatrick Sanan 
93638320bc6fSPatrick Sanan     Notes:
93648320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
93653d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
93668320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
93673d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
93683d862458SPatrick Sanan     decomposition, but hold different data.
93698320bc6fSPatrick Sanan 
93708320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
93718320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
93728320bc6fSPatrick Sanan 
93738320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
93748320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
93758320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
93768320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
93778320bc6fSPatrick Sanan 
93788320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
93798320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
93808320bc6fSPatrick Sanan .vb
93818320bc6fSPatrick Sanan   ...
93829566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
93838320bc6fSPatrick Sanan   if (set && compatible)  {
93849566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
93859566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
93869566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
93878320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
93888320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
93898320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
93908320bc6fSPatrick Sanan       }
93918320bc6fSPatrick Sanan     }
93929566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
93939566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
93948320bc6fSPatrick Sanan   } else {
93958320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
93968320bc6fSPatrick Sanan   }
93978320bc6fSPatrick Sanan   ...
93988320bc6fSPatrick Sanan .ve
93998320bc6fSPatrick Sanan 
94008320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
94018320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
94028320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
94038320bc6fSPatrick Sanan     always check the "set" output parameter.
94048320bc6fSPatrick Sanan 
94058320bc6fSPatrick Sanan     A DM is always compatible with itself.
94068320bc6fSPatrick Sanan 
94078320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
94088320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
94098320bc6fSPatrick Sanan     incompatible.
94108320bc6fSPatrick Sanan 
94118320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
94128320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
94138320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
94148320bc6fSPatrick Sanan 
94158320bc6fSPatrick Sanan     Developer Notes:
94163d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
94173d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9418a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
94198320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
94208320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
94213d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
94223d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
94238320bc6fSPatrick Sanan 
94248320bc6fSPatrick Sanan     Level: advanced
94258320bc6fSPatrick Sanan 
9426db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
94278320bc6fSPatrick Sanan @*/
94288320bc6fSPatrick Sanan 
9429a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
94308320bc6fSPatrick Sanan {
94318320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
94328320bc6fSPatrick Sanan   DMType         type,type2;
94338320bc6fSPatrick Sanan   PetscBool      sameType;
94348320bc6fSPatrick Sanan 
94358320bc6fSPatrick Sanan   PetscFunctionBegin;
9436a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
94378320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
94388320bc6fSPatrick Sanan 
94398320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9440a5bc1bf3SBarry Smith   if (dm1 == dm2) {
94418320bc6fSPatrick Sanan     *set = PETSC_TRUE;
94428320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
94438320bc6fSPatrick Sanan     PetscFunctionReturn(0);
94448320bc6fSPatrick Sanan   }
94458320bc6fSPatrick Sanan 
94468320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
94478320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
94488320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
94498320bc6fSPatrick Sanan      determined by the implementation-specific logic */
94509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult));
94518320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
94528320bc6fSPatrick Sanan     *set = PETSC_TRUE;
94538320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
94548320bc6fSPatrick Sanan     PetscFunctionReturn(0);
94558320bc6fSPatrick Sanan   }
94568320bc6fSPatrick Sanan 
94578320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9458a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
94599566063dSJacob Faibussowitsch     PetscCall((*dm1->ops->getcompatibility)(dm1,dm2,compatible,set));
9460b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
94618320bc6fSPatrick Sanan   }
94628320bc6fSPatrick Sanan 
9463a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
94648320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
94659566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1,&type));
94669566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2,&type2));
94679566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type,type2,&sameType));
94688320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
94699566063dSJacob Faibussowitsch     PetscCall((*dm2->ops->getcompatibility)(dm2,dm1,compatible,set)); /* Note argument order */
94708320bc6fSPatrick Sanan   } else {
94718320bc6fSPatrick Sanan     *set = PETSC_FALSE;
94728320bc6fSPatrick Sanan   }
94738320bc6fSPatrick Sanan   PetscFunctionReturn(0);
94748320bc6fSPatrick Sanan }
9475c0f0dcc3SMatthew G. Knepley 
9476c0f0dcc3SMatthew G. Knepley /*@C
9477c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9478c0f0dcc3SMatthew G. Knepley 
9479c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9480c0f0dcc3SMatthew G. Knepley 
9481c0f0dcc3SMatthew G. Knepley   Input Parameters:
9482c0f0dcc3SMatthew G. Knepley + DM - the DM
9483c0f0dcc3SMatthew G. Knepley . f - the monitor function
9484c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9485c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9486c0f0dcc3SMatthew G. Knepley 
9487c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9488c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9489c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9490c0f0dcc3SMatthew G. Knepley 
9491c0f0dcc3SMatthew G. Knepley   Notes:
9492c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9493c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9494c0f0dcc3SMatthew G. Knepley   order in which they were set.
9495c0f0dcc3SMatthew G. Knepley 
9496c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9497c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9498c0f0dcc3SMatthew G. Knepley 
9499c0f0dcc3SMatthew G. Knepley   Level: intermediate
9500c0f0dcc3SMatthew G. Knepley 
9501db781477SPatrick Sanan .seealso: `DMMonitorCancel()`
9502c0f0dcc3SMatthew G. Knepley @*/
9503c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9504c0f0dcc3SMatthew G. Knepley {
9505c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9506c0f0dcc3SMatthew G. Knepley 
9507c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9508c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9509c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9510c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9511c0f0dcc3SMatthew G. Knepley 
95129566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
9513c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9514c0f0dcc3SMatthew G. Knepley   }
95157a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9516c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9517c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9518c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9519c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9520c0f0dcc3SMatthew G. Knepley }
9521c0f0dcc3SMatthew G. Knepley 
9522c0f0dcc3SMatthew G. Knepley /*@
9523c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9524c0f0dcc3SMatthew G. Knepley 
9525c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9526c0f0dcc3SMatthew G. Knepley 
9527c0f0dcc3SMatthew G. Knepley   Input Parameter:
9528c0f0dcc3SMatthew G. Knepley . dm - the DM
9529c0f0dcc3SMatthew G. Knepley 
9530c0f0dcc3SMatthew G. Knepley   Options Database Key:
9531c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9532c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9533c0f0dcc3SMatthew G. Knepley   set via the options database
9534c0f0dcc3SMatthew G. Knepley 
9535c0f0dcc3SMatthew G. Knepley   Notes:
9536c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9537c0f0dcc3SMatthew G. Knepley 
9538c0f0dcc3SMatthew G. Knepley   Level: intermediate
9539c0f0dcc3SMatthew G. Knepley 
9540db781477SPatrick Sanan .seealso: `DMMonitorSet()`
9541c0f0dcc3SMatthew G. Knepley @*/
9542c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9543c0f0dcc3SMatthew G. Knepley {
9544c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9545c0f0dcc3SMatthew G. Knepley 
9546c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9547c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9548c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
95499566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
9550c0f0dcc3SMatthew G. Knepley   }
9551c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9552c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9553c0f0dcc3SMatthew G. Knepley }
9554c0f0dcc3SMatthew G. Knepley 
9555c0f0dcc3SMatthew G. Knepley /*@C
9556c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9557c0f0dcc3SMatthew G. Knepley 
9558c0f0dcc3SMatthew G. Knepley   Collective on DM
9559c0f0dcc3SMatthew G. Knepley 
9560c0f0dcc3SMatthew G. Knepley   Input Parameters:
9561c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9562c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9563c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9564c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9565c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9566c0f0dcc3SMatthew 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
9567c0f0dcc3SMatthew G. Knepley 
9568c0f0dcc3SMatthew G. Knepley   Output Parameter:
9569c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9570c0f0dcc3SMatthew G. Knepley 
9571c0f0dcc3SMatthew G. Knepley   Level: developer
9572c0f0dcc3SMatthew G. Knepley 
9573db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
9574db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
9575db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
9576db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
9577c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
9578db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
9579db781477SPatrick Sanan           `PetscOptionsFList()`, `PetscOptionsEList()`
9580c0f0dcc3SMatthew G. Knepley @*/
9581c0f0dcc3SMatthew 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)
9582c0f0dcc3SMatthew G. Knepley {
9583c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9584c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9585c0f0dcc3SMatthew G. Knepley 
9586c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9587c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
95889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg));
9589c0f0dcc3SMatthew G. Knepley   if (*flg) {
9590c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9591c0f0dcc3SMatthew G. Knepley 
95929566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
95939566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) viewer));
95949566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
95959566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy));
9596c0f0dcc3SMatthew G. Knepley   }
9597c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9598c0f0dcc3SMatthew G. Knepley }
9599c0f0dcc3SMatthew G. Knepley 
9600c0f0dcc3SMatthew G. Knepley /*@
9601c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9602c0f0dcc3SMatthew G. Knepley 
9603c0f0dcc3SMatthew G. Knepley    Collective on DM
9604c0f0dcc3SMatthew G. Knepley 
9605c0f0dcc3SMatthew G. Knepley    Input Parameters:
9606c0f0dcc3SMatthew G. Knepley .  dm - The DM
9607c0f0dcc3SMatthew G. Knepley 
9608c0f0dcc3SMatthew G. Knepley    Level: developer
9609c0f0dcc3SMatthew G. Knepley 
9610db781477SPatrick Sanan .seealso: `DMMonitorSet()`
9611c0f0dcc3SMatthew G. Knepley @*/
9612c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9613c0f0dcc3SMatthew G. Knepley {
9614c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9615c0f0dcc3SMatthew G. Knepley 
9616c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9617c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9618c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9619c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
96209566063dSJacob Faibussowitsch     PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
9621c0f0dcc3SMatthew G. Knepley   }
9622c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9623c0f0dcc3SMatthew G. Knepley }
96242e4af2aeSMatthew G. Knepley 
96252e4af2aeSMatthew G. Knepley /*@
96262e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
96272e4af2aeSMatthew G. Knepley 
96282e4af2aeSMatthew G. Knepley   Collective on DM
96292e4af2aeSMatthew G. Knepley 
96302e4af2aeSMatthew G. Knepley   Input Parameters:
96312e4af2aeSMatthew G. Knepley + dm     - The DM
96326b867d5aSJose E. Roman - sol    - The solution vector
96332e4af2aeSMatthew G. Knepley 
96346b867d5aSJose E. Roman   Input/Output Parameter:
96356b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
96366b867d5aSJose E. Roman            contains the error in each field
96376b867d5aSJose E. Roman 
96386b867d5aSJose E. Roman   Output Parameter:
96396b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
96402e4af2aeSMatthew G. Knepley 
96412e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
96422e4af2aeSMatthew G. Knepley 
96432e4af2aeSMatthew G. Knepley   Level: developer
96442e4af2aeSMatthew G. Knepley 
9645db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
96462e4af2aeSMatthew G. Knepley @*/
96472e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
96482e4af2aeSMatthew G. Knepley {
96492e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
96502e4af2aeSMatthew G. Knepley   void            **ctxs;
96512e4af2aeSMatthew G. Knepley   PetscReal         time;
96522e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
96532e4af2aeSMatthew G. Knepley 
96542e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
96559566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
96569566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
96579566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
96582e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
96592e4af2aeSMatthew G. Knepley     PetscDS         ds;
96602e4af2aeSMatthew G. Knepley     DMLabel         label;
96612e4af2aeSMatthew G. Knepley     IS              fieldIS;
96622e4af2aeSMatthew G. Knepley     const PetscInt *fields;
96632e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
96642e4af2aeSMatthew G. Knepley 
96659566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
96669566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
96679566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
96682e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
96692e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
96709566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
96712e4af2aeSMatthew G. Knepley     }
96729566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
96732e4af2aeSMatthew G. Knepley   }
96742e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
967563a3b9bcSJacob 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);
96762e4af2aeSMatthew G. Knepley   }
96779566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
96789566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
96792e4af2aeSMatthew G. Knepley   if (errorVec) {
96802e4af2aeSMatthew G. Knepley     DM             edm;
96812e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
96822e4af2aeSMatthew G. Knepley     PetscBool      simplex;
96832e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
96842e4af2aeSMatthew G. Knepley 
96859566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
96869566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
96879566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
96889566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
96892e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
96909566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
96912e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
96922e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
96932e4af2aeSMatthew G. Knepley       PetscQuadrature q;
96942e4af2aeSMatthew G. Knepley       const char     *name;
96952e4af2aeSMatthew G. Knepley 
96969566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *) &fe));
96979566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
96989566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject) fe, &name));
96999566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject) efe, name));
97009566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
97019566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
97029566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject) efe));
97039566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
97042e4af2aeSMatthew G. Knepley     }
97059566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
97062e4af2aeSMatthew G. Knepley 
97079566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
97089566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) *errorVec, "Error"));
97099566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
97109566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
97112e4af2aeSMatthew G. Knepley   }
97129566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
97132e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
97142e4af2aeSMatthew G. Knepley }
97159a2a23afSMatthew G. Knepley 
97169a2a23afSMatthew G. Knepley /*@
97179a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
97189a2a23afSMatthew G. Knepley 
97199a2a23afSMatthew G. Knepley   Not collective
97209a2a23afSMatthew G. Knepley 
97219a2a23afSMatthew G. Knepley   Input Parameter:
97229a2a23afSMatthew G. Knepley . dm     - The DM
97239a2a23afSMatthew G. Knepley 
97249a2a23afSMatthew G. Knepley   Output Parameter:
9725a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
97269a2a23afSMatthew G. Knepley 
97279a2a23afSMatthew G. Knepley   Level: advanced
97289a2a23afSMatthew G. Knepley 
9729db781477SPatrick Sanan .seealso: `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
97309a2a23afSMatthew G. Knepley @*/
97319a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
97329a2a23afSMatthew G. Knepley {
97339a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97349a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97359566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
97369a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97379a2a23afSMatthew G. Knepley }
97389a2a23afSMatthew G. Knepley 
97399a2a23afSMatthew G. Knepley /*@
9740ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
97419a2a23afSMatthew G. Knepley 
97429a2a23afSMatthew G. Knepley   Not collective
97439a2a23afSMatthew G. Knepley 
97449a2a23afSMatthew G. Knepley   Input Parameters:
97459a2a23afSMatthew G. Knepley + dm     - The DM
97469a2a23afSMatthew G. Knepley . label  - The DMLabel
9747ac17215fSMatthew G. Knepley . value  - The label value indicating the region
9748ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
97499a2a23afSMatthew G. Knepley 
97509a2a23afSMatthew G. Knepley   Output Parameter:
97519a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
97529a2a23afSMatthew G. Knepley 
9753ac17215fSMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
975404c51a94SMatthew G. Knepley 
97559a2a23afSMatthew G. Knepley   Level: advanced
97569a2a23afSMatthew G. Knepley 
9757db781477SPatrick Sanan .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`
97589a2a23afSMatthew G. Knepley @*/
9759ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
97609a2a23afSMatthew G. Knepley {
9761ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
976204c51a94SMatthew G. Knepley   PetscBool       has;
97639a2a23afSMatthew G. Knepley 
97649a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97659a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97669a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
97679a2a23afSMatthew G. Knepley   key.label = label;
97689a2a23afSMatthew G. Knepley   key.value = value;
9769ac17215fSMatthew G. Knepley   key.part  = part;
97709566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
97719566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key,  aux));
97729566063dSJacob Faibussowitsch   else     PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
97739a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97749a2a23afSMatthew G. Knepley }
97759a2a23afSMatthew G. Knepley 
97769a2a23afSMatthew G. Knepley /*@
9777ac17215fSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value, and equation part
97789a2a23afSMatthew G. Knepley 
97799a2a23afSMatthew G. Knepley   Not collective
97809a2a23afSMatthew G. Knepley 
97819a2a23afSMatthew G. Knepley   Input Parameters:
97829a2a23afSMatthew G. Knepley + dm     - The DM
97839a2a23afSMatthew G. Knepley . label  - The DMLabel
97849a2a23afSMatthew G. Knepley . value  - The label value indicating the region
9785ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
97869a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
97879a2a23afSMatthew G. Knepley 
97889a2a23afSMatthew G. Knepley   Level: advanced
97899a2a23afSMatthew G. Knepley 
9790db781477SPatrick Sanan .seealso: `DMGetAuxiliaryVec()`
97919a2a23afSMatthew G. Knepley @*/
9792ac17215fSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
97939a2a23afSMatthew G. Knepley {
97949a2a23afSMatthew G. Knepley   Vec             old;
97959a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
97969a2a23afSMatthew G. Knepley 
97979a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97989a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97999a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
98009a2a23afSMatthew G. Knepley   key.label = label;
98019a2a23afSMatthew G. Knepley   key.value = value;
9802ac17215fSMatthew G. Knepley   key.part  = part;
98039566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
98049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) aux));
98059566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) old));
98069566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
98079566063dSJacob Faibussowitsch   else      PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
98089a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98099a2a23afSMatthew G. Knepley }
98109a2a23afSMatthew G. Knepley 
98119a2a23afSMatthew G. Knepley /*@C
9812ac17215fSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this DM
98139a2a23afSMatthew G. Knepley 
98149a2a23afSMatthew G. Knepley   Not collective
98159a2a23afSMatthew G. Knepley 
98169a2a23afSMatthew G. Knepley   Input Parameter:
98179a2a23afSMatthew G. Knepley . dm      - The DM
98189a2a23afSMatthew G. Knepley 
98199a2a23afSMatthew G. Knepley   Output Parameters:
98209a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
9821ac17215fSMatthew G. Knepley . values  - The label values for each Vec
9822ac17215fSMatthew G. Knepley - parts   - The equation parts for each Vec
98239a2a23afSMatthew G. Knepley 
98249a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
98259a2a23afSMatthew G. Knepley 
98269a2a23afSMatthew G. Knepley   Level: advanced
98279a2a23afSMatthew G. Knepley 
9828db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
98299a2a23afSMatthew G. Knepley @*/
9830ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
98319a2a23afSMatthew G. Knepley {
98329a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
98339a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
98349a2a23afSMatthew G. Knepley 
98359a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98369a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98379a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9838dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9839dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts,  4);
98409566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
98419566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
98429566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
9843ac17215fSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value; parts[i] = keys[i].part;}
98449566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
98459a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98469a2a23afSMatthew G. Knepley }
98479a2a23afSMatthew G. Knepley 
98489a2a23afSMatthew G. Knepley /*@
98499a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
98509a2a23afSMatthew G. Knepley 
98519a2a23afSMatthew G. Knepley   Not collective
98529a2a23afSMatthew G. Knepley 
98539a2a23afSMatthew G. Knepley   Input Parameter:
98549a2a23afSMatthew G. Knepley . dm    - The DM
98559a2a23afSMatthew G. Knepley 
98569a2a23afSMatthew G. Knepley   Output Parameter:
98579a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
98589a2a23afSMatthew G. Knepley 
98599a2a23afSMatthew G. Knepley   Level: advanced
98609a2a23afSMatthew G. Knepley 
9861db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
98629a2a23afSMatthew G. Knepley @*/
98639a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
98649a2a23afSMatthew G. Knepley {
98659a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98669a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98679566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
98689566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
98699a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98709a2a23afSMatthew G. Knepley }
9871b5a892a1SMatthew G. Knepley 
9872b5a892a1SMatthew G. Knepley /*@C
9873b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9874b5a892a1SMatthew G. Knepley 
9875b5a892a1SMatthew G. Knepley   Not collective
9876b5a892a1SMatthew G. Knepley 
9877b5a892a1SMatthew G. Knepley   Input Parameters:
9878b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9879b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9880b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9881b5a892a1SMatthew G. Knepley 
9882b5a892a1SMatthew G. Knepley   Output Parameters:
9883b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9884b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9885b5a892a1SMatthew G. Knepley 
9886b5a892a1SMatthew G. Knepley   Level: advanced
9887b5a892a1SMatthew G. Knepley 
9888db781477SPatrick Sanan .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`
9889b5a892a1SMatthew G. Knepley @*/
9890b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9891b5a892a1SMatthew G. Knepley {
9892b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9893b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9894b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9895b5a892a1SMatthew G. Knepley 
9896b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9897b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9898b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9899b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9900b5a892a1SMatthew G. Knepley 
9901b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
9902b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9903b5a892a1SMatthew G. Knepley   }
9904b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9905b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9906b5a892a1SMatthew G. Knepley }
9907b5a892a1SMatthew G. Knepley 
9908b5a892a1SMatthew G. Knepley /*@C
9909b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9910b5a892a1SMatthew G. Knepley 
9911b5a892a1SMatthew G. Knepley   Not collective
9912b5a892a1SMatthew G. Knepley 
9913b5a892a1SMatthew G. Knepley   Input Parameters:
9914b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9915b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9916b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9917b5a892a1SMatthew G. Knepley 
9918b5a892a1SMatthew G. Knepley   Output Parameters:
9919b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9920b5a892a1SMatthew G. Knepley 
9921b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9922b5a892a1SMatthew G. Knepley 
9923b5a892a1SMatthew G. Knepley   Level: advanced
9924b5a892a1SMatthew G. Knepley 
9925db781477SPatrick Sanan .seealso: `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`
9926b5a892a1SMatthew G. Knepley @*/
9927b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9928b5a892a1SMatthew G. Knepley {
9929b5a892a1SMatthew G. Knepley   PetscBool      found;
9930b5a892a1SMatthew G. Knepley 
9931b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
99329566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
99337a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9934b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9935b5a892a1SMatthew G. Knepley }
9936b5a892a1SMatthew G. Knepley 
9937b5a892a1SMatthew G. Knepley /*@C
9938b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9939b5a892a1SMatthew G. Knepley 
9940b5a892a1SMatthew G. Knepley   Not collective
9941b5a892a1SMatthew G. Knepley 
9942b5a892a1SMatthew G. Knepley   Input Parameters:
9943b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9944b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9945b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9946b5a892a1SMatthew G. Knepley 
9947b5a892a1SMatthew G. Knepley   Output Parameters:
9948b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9949b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9950b5a892a1SMatthew G. Knepley 
9951b5a892a1SMatthew G. Knepley   Level: advanced
9952b5a892a1SMatthew G. Knepley 
9953db781477SPatrick Sanan .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`
9954b5a892a1SMatthew G. Knepley @*/
9955b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9956b5a892a1SMatthew G. Knepley {
9957b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9958b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9959b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9960b5a892a1SMatthew G. Knepley 
9961b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9962b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9963b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9964b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9965b5a892a1SMatthew G. Knepley 
9966b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
9967b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9968b5a892a1SMatthew G. Knepley   }
9969b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9970b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9971b5a892a1SMatthew G. Knepley }
9972b5a892a1SMatthew G. Knepley 
9973b5a892a1SMatthew G. Knepley /*@C
9974b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9975b5a892a1SMatthew G. Knepley 
9976b5a892a1SMatthew G. Knepley   Not collective
9977b5a892a1SMatthew G. Knepley 
9978b5a892a1SMatthew G. Knepley   Input Parameters:
9979b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9980b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9981b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9982b5a892a1SMatthew G. Knepley 
9983b5a892a1SMatthew G. Knepley   Output Parameters:
9984b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9985b5a892a1SMatthew G. Knepley 
9986b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9987b5a892a1SMatthew G. Knepley 
9988b5a892a1SMatthew G. Knepley   Level: advanced
9989b5a892a1SMatthew G. Knepley 
9990db781477SPatrick Sanan .seealso: `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9991b5a892a1SMatthew G. Knepley @*/
9992b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9993b5a892a1SMatthew G. Knepley {
9994b5a892a1SMatthew G. Knepley   PetscBool      found;
9995b5a892a1SMatthew G. Knepley 
9996b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
99979566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
99987a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9999b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10000b5a892a1SMatthew G. Knepley }
10001012bc364SMatthew G. Knepley 
10002012bc364SMatthew G. Knepley /*@C
10003012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
10004012bc364SMatthew G. Knepley 
10005012bc364SMatthew G. Knepley   Not collective
10006012bc364SMatthew G. Knepley 
10007012bc364SMatthew G. Knepley   Input Parameters:
10008012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
10009012bc364SMatthew G. Knepley - point - Coordinates of the point
10010012bc364SMatthew G. Knepley 
10011012bc364SMatthew G. Knepley   Output Parameters:
10012012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
10013012bc364SMatthew G. Knepley 
10014012bc364SMatthew G. Knepley   Level: advanced
10015012bc364SMatthew G. Knepley 
10016db781477SPatrick Sanan .seealso: `DMLocatePoints()`
10017012bc364SMatthew G. Knepley @*/
10018012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
10019012bc364SMatthew G. Knepley {
10020012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
10021012bc364SMatthew G. Knepley   PetscInt  d;
10022012bc364SMatthew G. Knepley 
10023012bc364SMatthew G. Knepley   PetscFunctionBegin;
10024012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
10025012bc364SMatthew G. Knepley   switch (ct) {
10026012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
10027012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
10028012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
10029012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
10030012bc364SMatthew G. Knepley       sum += point[d];
10031012bc364SMatthew G. Knepley     }
10032012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10033012bc364SMatthew G. Knepley     break;
10034012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
10035012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
10036012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
10037012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10038012bc364SMatthew G. Knepley     break;
10039012bc364SMatthew G. Knepley   default:
1004098921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
10041012bc364SMatthew G. Knepley   }
10042012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
10043012bc364SMatthew G. Knepley }
10044