xref: /petsc/src/dm/interface/dm.c (revision 7f96f943dd8803859454688487e467215585f68e)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
900d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
1000d952a4SJed Brown #  include <valgrind/memcheck.h>
1100d952a4SJed Brown #endif
1200d952a4SJed Brown 
13732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
14d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
1558cd63d5SVaclav Hapla PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_Load;
1667a56275SMatthew G Knepley 
17e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
1840967b3bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_",0};
19412e9a14SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"vertex", "segment", "tensor_segment", "triangle", "quadrilateral", "tensor_quad", "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "FV_ghost_cell", "unknown", "DMPolytopeType", "DM_POLYTOPE_", 0};
20a4121054SBarry Smith /*@
21de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
22a4121054SBarry Smith 
23a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
24a4121054SBarry Smith    error when you try to use the vector.
25a4121054SBarry Smith 
26d083f849SBarry Smith   Collective
27a4121054SBarry Smith 
28a4121054SBarry Smith   Input Parameter:
29a4121054SBarry Smith . comm - The communicator for the DM object
30a4121054SBarry Smith 
31a4121054SBarry Smith   Output Parameter:
32a4121054SBarry Smith . dm - The DM object
33a4121054SBarry Smith 
34a4121054SBarry Smith   Level: beginner
35a4121054SBarry Smith 
368472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
37a4121054SBarry Smith @*/
387087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
39a4121054SBarry Smith {
40a4121054SBarry Smith   DM             v;
41e5e52638SMatthew G. Knepley   PetscDS        ds;
42a4121054SBarry Smith   PetscErrorCode ierr;
43a4121054SBarry Smith 
44a4121054SBarry Smith   PetscFunctionBegin;
451411c6eeSJed Brown   PetscValidPointer(dm,2);
460298fd71SBarry Smith   *dm = NULL;
47607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
48a4121054SBarry Smith 
4973107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
50e7c4fc90SDmitry Karpeev 
5149be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5249be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
530298fd71SBarry Smith   v->ltogmap                  = NULL;
541411c6eeSJed Brown   v->bs                       = 1;
55171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5688ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
571bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
58c58f1c22SToby Isaac   v->labels                   = NULL;
5934aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6034aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
61c58f1c22SToby Isaac   v->depthLabel               = NULL;
62ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
631bb6d2a8SBarry Smith   v->localSection             = NULL;
641bb6d2a8SBarry Smith   v->globalSection            = NULL;
65fba222abSToby Isaac   v->defaultConstraintSection = NULL;
66fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
67c6b900c6SMatthew G. Knepley   v->L                        = NULL;
68c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
695dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
709a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7196173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
72435a35e8SMatthew G Knepley   {
73435a35e8SMatthew G Knepley     PetscInt i;
74435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
750298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
76f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
77435a35e8SMatthew G Knepley     }
78435a35e8SMatthew G Knepley   }
79e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
80b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
81e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
8214f150ffSMatthew G. Knepley   v->dmBC = NULL;
83a8fb8f29SToby Isaac   v->coarseMesh = NULL;
84f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
85cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
86c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
87b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
884a7a4c06SLawrence Mitchell 
891411c6eeSJed Brown   *dm = v;
90a4121054SBarry Smith   PetscFunctionReturn(0);
91a4121054SBarry Smith }
92a4121054SBarry Smith 
9338221697SMatthew G. Knepley /*@
9438221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9538221697SMatthew G. Knepley 
96d083f849SBarry Smith   Collective
9738221697SMatthew G. Knepley 
9838221697SMatthew G. Knepley   Input Parameter:
9938221697SMatthew G. Knepley . dm - The original DM object
10038221697SMatthew G. Knepley 
10138221697SMatthew G. Knepley   Output Parameter:
10238221697SMatthew G. Knepley . newdm  - The new DM object
10338221697SMatthew G. Knepley 
10438221697SMatthew G. Knepley   Level: beginner
10538221697SMatthew G. Knepley 
1061cb8cacdSPatrick Sanan   Notes:
1071cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1081cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1091cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1101bb6d2a8SBarry Smith 
11189706ed2SPatrick Sanan   The clone is considered set up iff the original is.
11289706ed2SPatrick Sanan 
11392cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1141bb6d2a8SBarry Smith 
11538221697SMatthew G. Knepley @*/
11638221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11738221697SMatthew G. Knepley {
11838221697SMatthew G. Knepley   PetscSF        sf;
11938221697SMatthew G. Knepley   Vec            coords;
12038221697SMatthew G. Knepley   void          *ctx;
121a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12238221697SMatthew G. Knepley   PetscErrorCode ierr;
12338221697SMatthew G. Knepley 
12438221697SMatthew G. Knepley   PetscFunctionBegin;
12538221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12638221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12738221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1285d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
129ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
130ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1311de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1321de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13338221697SMatthew G. Knepley   if (dm->ops->clone) {
13438221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   }
1363f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13738221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13838221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
141be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
142be4c1c3eSMatthew G. Knepley     DM           ncdm;
143be4c1c3eSMatthew G. Knepley     PetscSection cs;
1445a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
145be4c1c3eSMatthew G. Knepley 
14692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1485a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1495a0206caSToby Isaac     if (pEndMax >= 0) {
150be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15183bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
15292fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
153a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
154be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
155be4c1c3eSMatthew G. Knepley     }
156be4c1c3eSMatthew G. Knepley   }
157a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
158a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15938221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16038221697SMatthew G. Knepley   if (coords) {
16138221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16238221697SMatthew G. Knepley   } else {
16338221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16438221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16538221697SMatthew G. Knepley   }
16690b157c4SStefano Zampini   {
16790b157c4SStefano Zampini     PetscBool             isper;
168c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1695dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17090b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17190b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
172c6b900c6SMatthew G. Knepley   }
17334aa8a36SMatthew G. Knepley   {
17434aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17534aa8a36SMatthew G. Knepley 
17634aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17834aa8a36SMatthew G. Knepley   }
17938221697SMatthew G. Knepley   PetscFunctionReturn(0);
18038221697SMatthew G. Knepley }
18138221697SMatthew G. Knepley 
1829a42bb27SBarry Smith /*@C
183564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1849a42bb27SBarry Smith 
185d083f849SBarry Smith    Logically Collective on da
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Input Parameter:
1889a42bb27SBarry Smith +  da - initial distributed array
189e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1909a42bb27SBarry Smith 
1919a42bb27SBarry Smith    Options Database:
192dd85299cSBarry Smith .   -dm_vec_type ctype
1939a42bb27SBarry Smith 
1949a42bb27SBarry Smith    Level: intermediate
1959a42bb27SBarry Smith 
196a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1979a42bb27SBarry Smith @*/
19819fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1999a42bb27SBarry Smith {
2009a42bb27SBarry Smith   PetscErrorCode ierr;
2019a42bb27SBarry Smith 
2029a42bb27SBarry Smith   PetscFunctionBegin;
2039a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2049a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2069a42bb27SBarry Smith   PetscFunctionReturn(0);
2079a42bb27SBarry Smith }
2089a42bb27SBarry Smith 
209c0dedaeaSBarry Smith /*@C
210c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
211c0dedaeaSBarry Smith 
212d083f849SBarry Smith    Logically Collective on da
213c0dedaeaSBarry Smith 
214c0dedaeaSBarry Smith    Input Parameter:
215c0dedaeaSBarry Smith .  da - initial distributed array
216c0dedaeaSBarry Smith 
217c0dedaeaSBarry Smith    Output Parameter:
218c0dedaeaSBarry Smith .  ctype - the vector type
219c0dedaeaSBarry Smith 
220c0dedaeaSBarry Smith    Level: intermediate
221c0dedaeaSBarry Smith 
222a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
223c0dedaeaSBarry Smith @*/
224c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
225c0dedaeaSBarry Smith {
226c0dedaeaSBarry Smith   PetscFunctionBegin;
227c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
228c0dedaeaSBarry Smith   *ctype = da->vectype;
229c0dedaeaSBarry Smith   PetscFunctionReturn(0);
230c0dedaeaSBarry Smith }
231c0dedaeaSBarry Smith 
2325f1ad066SMatthew G Knepley /*@
23334f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2345f1ad066SMatthew G Knepley 
2355f1ad066SMatthew G Knepley   Not collective
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley   Input Parameter:
2385f1ad066SMatthew G Knepley . v - The Vec
2395f1ad066SMatthew G Knepley 
2405f1ad066SMatthew G Knepley   Output Parameter:
2415f1ad066SMatthew G Knepley . dm - The DM
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   Level: intermediate
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2465f1ad066SMatthew G Knepley @*/
2475f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2485f1ad066SMatthew G Knepley {
2495f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   PetscFunctionBegin;
2525f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2535f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2545f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2555f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2565f1ad066SMatthew G Knepley }
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley /*@
259d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2605f1ad066SMatthew G Knepley 
2615f1ad066SMatthew G Knepley   Not collective
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley   Input Parameters:
2645f1ad066SMatthew G Knepley + v - The Vec
2655f1ad066SMatthew G Knepley - dm - The DM
2665f1ad066SMatthew G Knepley 
267d9805387SMatthew 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.
268d9805387SMatthew G. Knepley 
2695f1ad066SMatthew G Knepley   Level: intermediate
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2725f1ad066SMatthew G Knepley @*/
2735f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2745f1ad066SMatthew G Knepley {
2755f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2765f1ad066SMatthew G Knepley 
2775f1ad066SMatthew G Knepley   PetscFunctionBegin;
2785f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
279d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2805f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2815f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2825f1ad066SMatthew G Knepley }
2835f1ad066SMatthew G Knepley 
284521d9a4cSLisandro Dalcin /*@C
2858f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2868f1509bcSBarry Smith 
287d083f849SBarry Smith    Logically Collective on dm
2888f1509bcSBarry Smith 
2898f1509bcSBarry Smith    Input Parameters:
2908f1509bcSBarry Smith +  dm - the DM context
2918f1509bcSBarry Smith -  ctype - the matrix type
2928f1509bcSBarry Smith 
2938f1509bcSBarry Smith    Options Database:
2948f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2958f1509bcSBarry Smith 
2968f1509bcSBarry Smith    Level: intermediate
2978f1509bcSBarry Smith 
2988f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2998f1509bcSBarry Smith           DMGetISColoringType()
3008f1509bcSBarry Smith @*/
3018f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3028f1509bcSBarry Smith {
3038f1509bcSBarry Smith   PetscFunctionBegin;
3048f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3058f1509bcSBarry Smith   dm->coloringtype = ctype;
3068f1509bcSBarry Smith   PetscFunctionReturn(0);
3078f1509bcSBarry Smith }
3088f1509bcSBarry Smith 
3098f1509bcSBarry Smith /*@C
3108f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
311521d9a4cSLisandro Dalcin 
312d083f849SBarry Smith    Logically Collective on dm
313521d9a4cSLisandro Dalcin 
314521d9a4cSLisandro Dalcin    Input Parameter:
3158f1509bcSBarry Smith .  dm - the DM context
3168f1509bcSBarry Smith 
3178f1509bcSBarry Smith    Output Parameter:
3188f1509bcSBarry Smith .  ctype - the matrix type
3198f1509bcSBarry Smith 
3208f1509bcSBarry Smith    Options Database:
3218f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3228f1509bcSBarry Smith 
3238f1509bcSBarry Smith    Level: intermediate
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3268f1509bcSBarry Smith           DMGetISColoringType()
3278f1509bcSBarry Smith @*/
3288f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3298f1509bcSBarry Smith {
3308f1509bcSBarry Smith   PetscFunctionBegin;
3318f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3328f1509bcSBarry Smith   *ctype = dm->coloringtype;
3338f1509bcSBarry Smith   PetscFunctionReturn(0);
3348f1509bcSBarry Smith }
3358f1509bcSBarry Smith 
3368f1509bcSBarry Smith /*@C
3378f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3388f1509bcSBarry Smith 
339d083f849SBarry Smith    Logically Collective on dm
3408f1509bcSBarry Smith 
3418f1509bcSBarry Smith    Input Parameters:
342521d9a4cSLisandro Dalcin +  dm - the DM context
343a2b5a043SBarry Smith -  ctype - the matrix type
344521d9a4cSLisandro Dalcin 
345521d9a4cSLisandro Dalcin    Options Database:
346521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
347521d9a4cSLisandro Dalcin 
348521d9a4cSLisandro Dalcin    Level: intermediate
349521d9a4cSLisandro Dalcin 
350a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
351521d9a4cSLisandro Dalcin @*/
35219fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
353521d9a4cSLisandro Dalcin {
354521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35588f0584fSBarry Smith 
356521d9a4cSLisandro Dalcin   PetscFunctionBegin;
357521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
358521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
360521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
361521d9a4cSLisandro Dalcin }
362521d9a4cSLisandro Dalcin 
363c0dedaeaSBarry Smith /*@C
364c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
365c0dedaeaSBarry Smith 
366d083f849SBarry Smith    Logically Collective on dm
367c0dedaeaSBarry Smith 
368c0dedaeaSBarry Smith    Input Parameter:
369c0dedaeaSBarry Smith .  dm - the DM context
370c0dedaeaSBarry Smith 
371c0dedaeaSBarry Smith    Output Parameter:
372c0dedaeaSBarry Smith .  ctype - the matrix type
373c0dedaeaSBarry Smith 
374c0dedaeaSBarry Smith    Options Database:
375c0dedaeaSBarry Smith .   -dm_mat_type ctype
376c0dedaeaSBarry Smith 
377c0dedaeaSBarry Smith    Level: intermediate
378c0dedaeaSBarry Smith 
379a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
380c0dedaeaSBarry Smith @*/
381c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
382c0dedaeaSBarry Smith {
383c0dedaeaSBarry Smith   PetscFunctionBegin;
384c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
385c0dedaeaSBarry Smith   *ctype = dm->mattype;
386c0dedaeaSBarry Smith   PetscFunctionReturn(0);
387c0dedaeaSBarry Smith }
388c0dedaeaSBarry Smith 
389c688c046SMatthew G Knepley /*@
39034f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
391c688c046SMatthew G Knepley 
392c688c046SMatthew G Knepley   Not collective
393c688c046SMatthew G Knepley 
394c688c046SMatthew G Knepley   Input Parameter:
395c688c046SMatthew G Knepley . A - The Mat
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   Output Parameter:
398c688c046SMatthew G Knepley . dm - The DM
399c688c046SMatthew G Knepley 
400c688c046SMatthew G Knepley   Level: intermediate
401c688c046SMatthew G Knepley 
4028f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4038f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4048f1509bcSBarry Smith 
405c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
406c688c046SMatthew G Knepley @*/
407c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
408c688c046SMatthew G Knepley {
409c688c046SMatthew G Knepley   PetscErrorCode ierr;
410c688c046SMatthew G Knepley 
411c688c046SMatthew G Knepley   PetscFunctionBegin;
412c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
413c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
414c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
415c688c046SMatthew G Knepley   PetscFunctionReturn(0);
416c688c046SMatthew G Knepley }
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley /*@
419c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley   Not collective
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Input Parameters:
424c688c046SMatthew G Knepley + A - The Mat
425c688c046SMatthew G Knepley - dm - The DM
426c688c046SMatthew G Knepley 
427c688c046SMatthew G Knepley   Level: intermediate
428c688c046SMatthew G Knepley 
4298f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4308f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4318f1509bcSBarry Smith 
4328f1509bcSBarry Smith 
433c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
434c688c046SMatthew G Knepley @*/
435c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
436c688c046SMatthew G Knepley {
437c688c046SMatthew G Knepley   PetscErrorCode ierr;
438c688c046SMatthew G Knepley 
439c688c046SMatthew G Knepley   PetscFunctionBegin;
440c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4418865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
442c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
443c688c046SMatthew G Knepley   PetscFunctionReturn(0);
444c688c046SMatthew G Knepley }
445c688c046SMatthew G Knepley 
4469a42bb27SBarry Smith /*@C
4479a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4486757b960SDave May    DM options in the database.
4499a42bb27SBarry Smith 
450d083f849SBarry Smith    Logically Collective on dm
4519a42bb27SBarry Smith 
4529a42bb27SBarry Smith    Input Parameter:
4538353ddbbSDave May +  da - the DM context
4549a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith    Notes:
4579a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4589a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4599a42bb27SBarry Smith 
4609a42bb27SBarry Smith    Level: advanced
4619a42bb27SBarry Smith 
4629a42bb27SBarry Smith .seealso: DMSetFromOptions()
4639a42bb27SBarry Smith @*/
4647087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4659a42bb27SBarry Smith {
4669a42bb27SBarry Smith   PetscErrorCode ierr;
4679a42bb27SBarry Smith 
4689a42bb27SBarry Smith   PetscFunctionBegin;
4699a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4709a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
471691be533SLawrence Mitchell   if (dm->sf) {
472691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
473691be533SLawrence Mitchell   }
4741bb6d2a8SBarry Smith   if (dm->sectionSF) {
4751bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
476691be533SLawrence Mitchell   }
4779a42bb27SBarry Smith   PetscFunctionReturn(0);
4789a42bb27SBarry Smith }
4799a42bb27SBarry Smith 
48031697293SDave May /*@C
48131697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48231697293SDave May    DM options in the database.
48331697293SDave May 
484d083f849SBarry Smith    Logically Collective on dm
48531697293SDave May 
48631697293SDave May    Input Parameters:
48731697293SDave May +  dm - the DM context
48831697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48931697293SDave May 
49031697293SDave May    Notes:
49131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49331697293SDave May 
49431697293SDave May    Level: advanced
49531697293SDave May 
49631697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49731697293SDave May @*/
49831697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49931697293SDave May {
50031697293SDave May   PetscErrorCode ierr;
50131697293SDave May 
50231697293SDave May   PetscFunctionBegin;
50331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50431697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50531697293SDave May   PetscFunctionReturn(0);
50631697293SDave May }
50731697293SDave May 
50831697293SDave May /*@C
50931697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51031697293SDave May    DM options in the database.
51131697293SDave May 
51231697293SDave May    Not Collective
51331697293SDave May 
51431697293SDave May    Input Parameters:
51531697293SDave May .  dm - the DM context
51631697293SDave May 
51731697293SDave May    Output Parameters:
51831697293SDave May .  prefix - pointer to the prefix string used is returned
51931697293SDave May 
52095452b02SPatrick Sanan    Notes:
52195452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52231697293SDave May    sufficient length to hold the prefix.
52331697293SDave May 
52431697293SDave May    Level: advanced
52531697293SDave May 
52631697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52731697293SDave May @*/
52831697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52931697293SDave May {
53031697293SDave May   PetscErrorCode ierr;
53131697293SDave May 
53231697293SDave May   PetscFunctionBegin;
53331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53431697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53531697293SDave May   PetscFunctionReturn(0);
53631697293SDave May }
53731697293SDave May 
53888bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53988bdff64SToby Isaac {
5406eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
54188bdff64SToby Isaac   PetscErrorCode ierr;
54288bdff64SToby Isaac 
54388bdff64SToby Isaac   PetscFunctionBegin;
544aab5bcd8SJed Brown   *ncrefct = 0;
54588bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54688bdff64SToby Isaac     refct--;
54788bdff64SToby Isaac     if (recurseCoarse) {
54888bdff64SToby Isaac       PetscInt coarseCount;
54988bdff64SToby Isaac 
55088bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55188bdff64SToby Isaac       refct += coarseCount;
55288bdff64SToby Isaac     }
55388bdff64SToby Isaac   }
55488bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55588bdff64SToby Isaac     refct--;
55688bdff64SToby Isaac     if (recurseFine) {
55788bdff64SToby Isaac       PetscInt fineCount;
55888bdff64SToby Isaac 
55988bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56088bdff64SToby Isaac       refct += fineCount;
56188bdff64SToby Isaac     }
56288bdff64SToby Isaac   }
56388bdff64SToby Isaac   *ncrefct = refct;
56488bdff64SToby Isaac   PetscFunctionReturn(0);
56588bdff64SToby Isaac }
56688bdff64SToby Isaac 
567f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
568354557abSToby Isaac {
5695d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
570354557abSToby Isaac   PetscErrorCode ierr;
571354557abSToby Isaac 
572354557abSToby Isaac   PetscFunctionBegin;
573354557abSToby Isaac   /* destroy the labels */
574354557abSToby Isaac   while (next) {
575354557abSToby Isaac     DMLabelLink tmp = next->next;
576354557abSToby Isaac 
5775d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
578ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
579354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
580354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
581354557abSToby Isaac     next = tmp;
582354557abSToby Isaac   }
5835d80c0bfSVaclav Hapla   dm->labels = NULL;
584354557abSToby Isaac   PetscFunctionReturn(0);
585354557abSToby Isaac }
586354557abSToby Isaac 
58747c6ae99SBarry Smith /*@
5888472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
58947c6ae99SBarry Smith 
590d083f849SBarry Smith     Collective on dm
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith     Input Parameter:
59347c6ae99SBarry Smith .   dm - the DM object to destroy
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith     Level: developer
59647c6ae99SBarry Smith 
597e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith @*/
600fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60147c6ae99SBarry Smith {
6026eb26441SStefano Zampini   PetscInt       cnt;
603dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60447c6ae99SBarry Smith   PetscErrorCode ierr;
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith   PetscFunctionBegin;
6076bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6086bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
60987e657c6SBarry Smith 
61088bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61188bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61288bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
61388bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
6146bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6156bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6166eb26441SStefano Zampini 
6176eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6186eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6196eb26441SStefano Zampini 
620f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6210298fd71SBarry Smith   (*dm)->namedglobal = NULL;
622f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6232348bcf4SPeter Brune     nnext = nlink->next;
6242348bcf4SPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6252348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6262348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6272348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6282348bcf4SPeter Brune   }
629f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6300298fd71SBarry Smith   (*dm)->namedlocal = NULL;
631f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
632f490541aSPeter Brune     nnext = nlink->next;
633f490541aSPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
634f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
635f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
636f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
637f490541aSPeter Brune   }
6382348bcf4SPeter Brune 
639b17ce1afSJed Brown   /* Destroy the list of hooks */
640c833c3b5SJed Brown   {
641c833c3b5SJed Brown     DMCoarsenHookLink link,next;
642b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
643b17ce1afSJed Brown       next = link->next;
644b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
645b17ce1afSJed Brown     }
6460298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
647c833c3b5SJed Brown   }
648c833c3b5SJed Brown   {
649c833c3b5SJed Brown     DMRefineHookLink link,next;
650c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
651c833c3b5SJed Brown       next = link->next;
652c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
653c833c3b5SJed Brown     }
6540298fd71SBarry Smith     (*dm)->refinehook = NULL;
655c833c3b5SJed Brown   }
656be081cd6SPeter Brune   {
657be081cd6SPeter Brune     DMSubDomainHookLink link,next;
658be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
659be081cd6SPeter Brune       next = link->next;
660be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
661be081cd6SPeter Brune     }
6620298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
663be081cd6SPeter Brune   }
664baf369e7SPeter Brune   {
665baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
666baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
667baf369e7SPeter Brune       next = link->next;
668baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
669baf369e7SPeter Brune     }
6700298fd71SBarry Smith     (*dm)->gtolhook = NULL;
671baf369e7SPeter Brune   }
672d4d07f1eSToby Isaac   {
673d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
674d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
675d4d07f1eSToby Isaac       next = link->next;
676d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
677d4d07f1eSToby Isaac     }
678d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
679d4d07f1eSToby Isaac   }
680aa1993deSMatthew G Knepley   /* Destroy the work arrays */
681aa1993deSMatthew G Knepley   {
682aa1993deSMatthew G Knepley     DMWorkLink link,next;
683aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
684aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
685aa1993deSMatthew G Knepley       next = link->next;
686aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
687aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
688aa1993deSMatthew G Knepley     }
6890298fd71SBarry Smith     (*dm)->workin = NULL;
690aa1993deSMatthew G Knepley   }
691c58f1c22SToby Isaac   /* destroy the labels */
692f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
693f4cdcedcSVaclav Hapla   /* destroy the fields */
694e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
695f4cdcedcSVaclav Hapla   /* destroy the boundaries */
696e6f8dbb6SToby Isaac   {
697e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
698e6f8dbb6SToby Isaac     while (next) {
699e6f8dbb6SToby Isaac       DMBoundary b = next;
700e6f8dbb6SToby Isaac 
701e6f8dbb6SToby Isaac       next = b->next;
702e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
703e6f8dbb6SToby Isaac     }
704e6f8dbb6SToby Isaac   }
705b17ce1afSJed Brown 
70652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
70752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
70852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
70952536dc3SBarry Smith 
7101a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7111a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7121a266240SBarry Smith   }
71371cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7146bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7156bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
716073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
71788ed4aceSMatthew G Knepley 
7181bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7191bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7208b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
721fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
722fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
72388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7241bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
725736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
726736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7278e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
728736995cdSBlaise Bourdin     }
729736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
730736995cdSBlaise Bourdin   }
73188bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
73288bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
73388bdff64SToby Isaac   }
7346eb26441SStefano Zampini 
735a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
73688bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
73788bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
73888bdff64SToby Isaac   }
73988bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
740f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7416636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7426636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7436636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
744412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
745412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
746412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
747ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
748ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
749ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7506636e97aSMatthew G Knepley 
751e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
75214f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
753e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
754e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
755732e2eb9SMatthew G Knepley 
756ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7576bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
758ed3c66a1SDave May   }
759c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
760435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
761732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
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 
775e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
776d7bf68aeSBarry Smith 
777d7bf68aeSBarry Smith @*/
7787087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
779d7bf68aeSBarry Smith {
780d7bf68aeSBarry Smith   PetscErrorCode ierr;
781d7bf68aeSBarry Smith 
782d7bf68aeSBarry Smith   PetscFunctionBegin;
783171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7848387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
785d7bf68aeSBarry Smith   if (dm->ops->setup) {
786d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
787d7bf68aeSBarry Smith   }
7888387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
789d7bf68aeSBarry Smith   PetscFunctionReturn(0);
790d7bf68aeSBarry Smith }
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith /*@
793d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
794d7bf68aeSBarry Smith 
795d083f849SBarry Smith     Collective on dm
796d7bf68aeSBarry Smith 
797d7bf68aeSBarry Smith     Input Parameter:
798d7bf68aeSBarry Smith .   dm - the DM object to set options for
799d7bf68aeSBarry Smith 
800732e2eb9SMatthew G Knepley     Options Database:
8014164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8024164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8034164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8048f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
805732e2eb9SMatthew G Knepley 
806384a6580SVaclav Hapla     DMPLEX Specific Checks
807384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
808384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
809384a6580SVaclav 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()
810384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
811384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
812384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
813384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
814d7bf68aeSBarry Smith 
81595eb5ee5SVaclav Hapla     Level: intermediate
81695eb5ee5SVaclav Hapla 
81795eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
81895eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
819d7bf68aeSBarry Smith 
820d7bf68aeSBarry Smith @*/
8217087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
822d7bf68aeSBarry Smith {
8237781c08eSBarry Smith   char           typeName[256];
824ca266f36SBarry Smith   PetscBool      flg;
825d7bf68aeSBarry Smith   PetscErrorCode ierr;
826d7bf68aeSBarry Smith 
827d7bf68aeSBarry Smith   PetscFunctionBegin;
828171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
83049be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8311bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8323194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8330298fd71SBarry Smith   ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL);CHKERRQ(ierr);
834a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
835f9ba7244SBarry Smith   if (flg) {
836f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
837f9ba7244SBarry Smith   }
838a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
839073dac72SJed Brown   if (flg) {
840521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
841073dac72SJed Brown   }
8428f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
843f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
844e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
845f9ba7244SBarry Smith   }
846f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8470633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
84882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
849d7bf68aeSBarry Smith   PetscFunctionReturn(0);
850d7bf68aeSBarry Smith }
851d7bf68aeSBarry Smith 
852fc9bc008SSatish Balay /*@C
853fe2efc57SMark    DMViewFromOptions - View from Options
854fe2efc57SMark 
855fe2efc57SMark    Collective on DM
856fe2efc57SMark 
857fe2efc57SMark    Input Parameters:
858fe2efc57SMark +  dm - the DM object
859736c3998SJose E. Roman .  obj - Optional object
860736c3998SJose E. Roman -  name - command line option
861fe2efc57SMark 
862fe2efc57SMark    Level: intermediate
863fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
864fe2efc57SMark @*/
865fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
866fe2efc57SMark {
867fe2efc57SMark   PetscErrorCode ierr;
868fe2efc57SMark 
869fe2efc57SMark   PetscFunctionBegin;
870fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
871fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
872fe2efc57SMark   PetscFunctionReturn(0);
873fe2efc57SMark }
874fe2efc57SMark 
875fe2efc57SMark /*@C
876224748a4SBarry Smith     DMView - Views a DM
87747c6ae99SBarry Smith 
878d083f849SBarry Smith     Collective on dm
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Input Parameter:
88147c6ae99SBarry Smith +   dm - the DM object to view
88247c6ae99SBarry Smith -   v - the viewer
88347c6ae99SBarry Smith 
884224748a4SBarry Smith     Level: beginner
88547c6ae99SBarry Smith 
886e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith @*/
8897087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
89047c6ae99SBarry Smith {
89147c6ae99SBarry Smith   PetscErrorCode    ierr;
89232c0f0efSBarry Smith   PetscBool         isbinary;
89376a8abe0SBarry Smith   PetscMPIInt       size;
89476a8abe0SBarry Smith   PetscViewerFormat format;
89547c6ae99SBarry Smith 
89647c6ae99SBarry Smith   PetscFunctionBegin;
897171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8983014e516SBarry Smith   if (!v) {
899ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9003014e516SBarry Smith   }
901b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
90274903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
90374903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
90474903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
90574903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90674903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90774903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
90874903a4fSStefano Zampini      in an error here */
90974903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9108bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
911b1b135c8SBarry Smith 
91276a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
91376a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
91476a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
91598c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91732c0f0efSBarry Smith   if (isbinary) {
91855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
91932c0f0efSBarry Smith     char     type[256];
92032c0f0efSBarry Smith 
921f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
92232c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
923f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
92432c0f0efSBarry Smith   }
9250c010503SBarry Smith   if (dm->ops->view) {
9260c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92747c6ae99SBarry Smith   }
92847c6ae99SBarry Smith   PetscFunctionReturn(0);
92947c6ae99SBarry Smith }
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith /*@
9328472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
93347c6ae99SBarry Smith 
934d083f849SBarry Smith     Collective on dm
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith     Input Parameter:
93747c6ae99SBarry Smith .   dm - the DM object
93847c6ae99SBarry Smith 
93947c6ae99SBarry Smith     Output Parameter:
94047c6ae99SBarry Smith .   vec - the global vector
94147c6ae99SBarry Smith 
942073dac72SJed Brown     Level: beginner
94347c6ae99SBarry Smith 
944e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith @*/
9477087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94847c6ae99SBarry Smith {
94947c6ae99SBarry Smith   PetscErrorCode ierr;
95047c6ae99SBarry Smith 
95147c6ae99SBarry Smith   PetscFunctionBegin;
952171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
953b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
954b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
95547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
95676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
957c6b011d8SStefano Zampini     DM vdm;
958c6b011d8SStefano Zampini 
959c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
960c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
961c6b011d8SStefano Zampini   }
96247c6ae99SBarry Smith   PetscFunctionReturn(0);
96347c6ae99SBarry Smith }
96447c6ae99SBarry Smith 
96547c6ae99SBarry Smith /*@
9668472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith     Not Collective
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith     Input Parameter:
97147c6ae99SBarry Smith .   dm - the DM object
97247c6ae99SBarry Smith 
97347c6ae99SBarry Smith     Output Parameter:
97447c6ae99SBarry Smith .   vec - the local vector
97547c6ae99SBarry Smith 
976073dac72SJed Brown     Level: beginner
97747c6ae99SBarry Smith 
978e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith @*/
9817087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98247c6ae99SBarry Smith {
98347c6ae99SBarry Smith   PetscErrorCode ierr;
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith   PetscFunctionBegin;
986171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
987b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
988b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
98947c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
99076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
991c6b011d8SStefano Zampini     DM vdm;
992c6b011d8SStefano Zampini 
993c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
994c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
995c6b011d8SStefano Zampini   }
99647c6ae99SBarry Smith   PetscFunctionReturn(0);
99747c6ae99SBarry Smith }
99847c6ae99SBarry Smith 
9991411c6eeSJed Brown /*@
10001411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10011411c6eeSJed Brown 
1002d083f849SBarry Smith    Collective on dm
10031411c6eeSJed Brown 
10041411c6eeSJed Brown    Input Parameter:
10051411c6eeSJed Brown .  dm - the DM that provides the mapping
10061411c6eeSJed Brown 
10071411c6eeSJed Brown    Output Parameter:
10081411c6eeSJed Brown .  ltog - the mapping
10091411c6eeSJed Brown 
10101411c6eeSJed Brown    Level: intermediate
10111411c6eeSJed Brown 
10121411c6eeSJed Brown    Notes:
10131411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10141411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10151411c6eeSJed Brown 
1016fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10171411c6eeSJed Brown @*/
10187087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10191411c6eeSJed Brown {
10200be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10211411c6eeSJed Brown   PetscErrorCode ierr;
10221411c6eeSJed Brown 
10231411c6eeSJed Brown   PetscFunctionBegin;
10241411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10251411c6eeSJed Brown   PetscValidPointer(ltog,2);
10261411c6eeSJed Brown   if (!dm->ltogmap) {
102737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
102837d0c07bSMatthew G Knepley 
102992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
103037d0c07bSMatthew G Knepley     if (section) {
1031a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103237d0c07bSMatthew G Knepley       PetscInt       *ltog;
1033ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103437d0c07bSMatthew G Knepley 
1035e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1037ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1038ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
103937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1040a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
104137d0c07bSMatthew G Knepley 
104237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1044a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1045a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10471a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10481a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10491a7dc684SMatthew G. Knepley         if (dof) {
1050b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1051b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10521a7dc684SMatthew G. Knepley         }
105337d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1054a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1055a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105637d0c07bSMatthew G Knepley         }
105737d0c07bSMatthew G Knepley       }
1058bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10590be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10600be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10610be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10620be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10637591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10647591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1065ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1066ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1067ccf3bd66SMatthew G. Knepley         n /= bs;
1068ccf3bd66SMatthew G. Knepley       }
1069ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10703bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
107137d0c07bSMatthew G Knepley     } else {
1072b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1073184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10741411c6eeSJed Brown     }
107537d0c07bSMatthew G Knepley   }
10761411c6eeSJed Brown   *ltog = dm->ltogmap;
10771411c6eeSJed Brown   PetscFunctionReturn(0);
10781411c6eeSJed Brown }
10791411c6eeSJed Brown 
10801411c6eeSJed Brown /*@
10811411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10821411c6eeSJed Brown 
10831411c6eeSJed Brown    Not Collective
10841411c6eeSJed Brown 
10851411c6eeSJed Brown    Input Parameter:
10861411c6eeSJed Brown .  dm - the DM with block structure
10871411c6eeSJed Brown 
10881411c6eeSJed Brown    Output Parameter:
10891411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10901411c6eeSJed Brown 
10911411c6eeSJed Brown    Level: intermediate
10921411c6eeSJed Brown 
1093fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10941411c6eeSJed Brown @*/
10957087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10961411c6eeSJed Brown {
10971411c6eeSJed Brown   PetscFunctionBegin;
10981411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1099534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11001411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11011411c6eeSJed Brown   *bs = dm->bs;
11021411c6eeSJed Brown   PetscFunctionReturn(0);
11031411c6eeSJed Brown }
11041411c6eeSJed Brown 
110547c6ae99SBarry Smith /*@
11068472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110747c6ae99SBarry Smith 
1108a5bc1bf3SBarry Smith     Collective on dmc
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith     Input Parameter:
1111a5bc1bf3SBarry Smith +   dmc - the DM object
1112a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
111347c6ae99SBarry Smith 
111447c6ae99SBarry Smith     Output Parameter:
111547c6ae99SBarry Smith +  mat - the interpolation
111647c6ae99SBarry Smith -  vec - the scaling (optional)
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith     Level: developer
111947c6ae99SBarry Smith 
112095452b02SPatrick Sanan     Notes:
112195452b02SPatrick 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
112285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1123d52bd9f3SBarry Smith 
11241f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1125d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112685afcc9aSBarry Smith 
112785afcc9aSBarry Smith 
11282ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
112947c6ae99SBarry Smith 
113047c6ae99SBarry Smith @*/
1131a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
113247c6ae99SBarry Smith {
113347c6ae99SBarry Smith   PetscErrorCode ierr;
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith   PetscFunctionBegin;
1136a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1137a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1138c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1139a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1140a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1141a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1142a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
114347c6ae99SBarry Smith   PetscFunctionReturn(0);
114447c6ae99SBarry Smith }
114547c6ae99SBarry Smith 
11463ad4599aSBarry Smith /*@
1147d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11482ed6491fSPatrick Sanan 
11492ed6491fSPatrick Sanan   Input Parameters:
11502ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11512ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11522ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11532ed6491fSPatrick Sanan 
11542ed6491fSPatrick Sanan   Output Parameter:
11552ed6491fSPatrick Sanan .    scale - the scaled vector
11562ed6491fSPatrick Sanan 
11572ed6491fSPatrick Sanan   Level: developer
11582ed6491fSPatrick Sanan 
11592ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11602ed6491fSPatrick Sanan 
11612ed6491fSPatrick Sanan @*/
11622ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11632ed6491fSPatrick Sanan {
11642ed6491fSPatrick Sanan   PetscErrorCode ierr;
11652ed6491fSPatrick Sanan   Vec            fine;
11662ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11672ed6491fSPatrick Sanan 
11682ed6491fSPatrick Sanan   PetscFunctionBegin;
11692ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11702ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11712ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11722ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11732ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11742ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11752ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11762ed6491fSPatrick Sanan }
11772ed6491fSPatrick Sanan 
11782ed6491fSPatrick Sanan /*@
11793ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11803ad4599aSBarry Smith 
1181a5bc1bf3SBarry Smith     Collective on dmc
11823ad4599aSBarry Smith 
11833ad4599aSBarry Smith     Input Parameter:
1184a5bc1bf3SBarry Smith +   dmc - the DM object
1185a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
11863ad4599aSBarry Smith 
11873ad4599aSBarry Smith     Output Parameter:
11883ad4599aSBarry Smith .  mat - the restriction
11893ad4599aSBarry Smith 
11903ad4599aSBarry Smith 
11913ad4599aSBarry Smith     Level: developer
11923ad4599aSBarry Smith 
119395452b02SPatrick Sanan     Notes:
119495452b02SPatrick 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
11953ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11963ad4599aSBarry Smith 
11973ad4599aSBarry Smith 
11983ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11993ad4599aSBarry Smith 
12003ad4599aSBarry Smith @*/
1201a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12023ad4599aSBarry Smith {
12033ad4599aSBarry Smith   PetscErrorCode ierr;
12043ad4599aSBarry Smith 
12053ad4599aSBarry Smith   PetscFunctionBegin;
1206a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1207a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12085a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1209a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1210a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1211a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1212a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12133ad4599aSBarry Smith   PetscFunctionReturn(0);
12143ad4599aSBarry Smith }
12153ad4599aSBarry Smith 
121647c6ae99SBarry Smith /*@
12178472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
121847c6ae99SBarry Smith 
1219a5bc1bf3SBarry Smith     Collective on dac
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith     Input Parameter:
1222a5bc1bf3SBarry Smith +   dac - the DM object
1223a5bc1bf3SBarry Smith -   daf - the second, finer DM object
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith     Output Parameter:
12266dbf9973SLawrence Mitchell .   mat - the injection
122747c6ae99SBarry Smith 
122847c6ae99SBarry Smith     Level: developer
122947c6ae99SBarry Smith 
123095452b02SPatrick Sanan    Notes:
123195452b02SPatrick 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
123285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
123385afcc9aSBarry Smith 
1234e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith @*/
1237a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
123847c6ae99SBarry Smith {
123947c6ae99SBarry Smith   PetscErrorCode ierr;
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith   PetscFunctionBegin;
1242a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1243a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12445a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1245a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1246a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1247a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1248a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
124947c6ae99SBarry Smith   PetscFunctionReturn(0);
125047c6ae99SBarry Smith }
125147c6ae99SBarry Smith 
1252b412c318SBarry Smith /*@
1253bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1254bd041c0cSMatthew G. Knepley 
1255a5bc1bf3SBarry Smith   Collective on dac
1256bd041c0cSMatthew G. Knepley 
1257bd041c0cSMatthew G. Knepley   Input Parameter:
1258a5bc1bf3SBarry Smith + dac - the DM object
1259a5bc1bf3SBarry Smith - daf - the second, finer DM object
1260bd041c0cSMatthew G. Knepley 
1261bd041c0cSMatthew G. Knepley   Output Parameter:
1262bd041c0cSMatthew G. Knepley . mat - the interpolation
1263bd041c0cSMatthew G. Knepley 
1264bd041c0cSMatthew G. Knepley   Level: developer
1265bd041c0cSMatthew G. Knepley 
1266bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1267bd041c0cSMatthew G. Knepley @*/
1268a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1269bd041c0cSMatthew G. Knepley {
1270bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1271bd041c0cSMatthew G. Knepley 
1272bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1273a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1274a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
12755a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1276a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1277a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1278bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1279bd041c0cSMatthew G. Knepley }
1280bd041c0cSMatthew G. Knepley 
1281bd041c0cSMatthew G. Knepley /*@
1282b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
128347c6ae99SBarry Smith 
1284d083f849SBarry Smith     Collective on dm
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith     Input Parameter:
128747c6ae99SBarry Smith +   dm - the DM object
12885bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
128947c6ae99SBarry Smith 
129047c6ae99SBarry Smith     Output Parameter:
129147c6ae99SBarry Smith .   coloring - the coloring
129247c6ae99SBarry Smith 
1293ec5066bdSBarry Smith     Notes:
1294ec5066bdSBarry 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
1295ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1296ec5066bdSBarry Smith 
1297ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1298ec5066bdSBarry Smith 
129947c6ae99SBarry Smith     Level: developer
130047c6ae99SBarry Smith 
1301ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
130247c6ae99SBarry Smith 
1303aab9d709SJed Brown @*/
1304b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
130547c6ae99SBarry Smith {
130647c6ae99SBarry Smith   PetscErrorCode ierr;
130747c6ae99SBarry Smith 
130847c6ae99SBarry Smith   PetscFunctionBegin;
1309171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13105a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1311b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1312b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
131347c6ae99SBarry Smith   PetscFunctionReturn(0);
131447c6ae99SBarry Smith }
131547c6ae99SBarry Smith 
1316b412c318SBarry Smith /*@
13178472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
131847c6ae99SBarry Smith 
1319d083f849SBarry Smith     Collective on dm
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith     Input Parameter:
1322b412c318SBarry Smith .   dm - the DM object
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith     Output Parameter:
132547c6ae99SBarry Smith .   mat - the empty Jacobian
132647c6ae99SBarry Smith 
1327073dac72SJed Brown     Level: beginner
132847c6ae99SBarry Smith 
132995452b02SPatrick Sanan     Notes:
133095452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
133194013140SBarry Smith        do not need to do it yourself.
133294013140SBarry Smith 
133394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13347889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
133594013140SBarry Smith 
133694013140SBarry 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
133794013140SBarry Smith        internally by PETSc.
133894013140SBarry Smith 
133994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1340aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
134194013140SBarry Smith 
1342b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
134347c6ae99SBarry Smith 
1344aab9d709SJed Brown @*/
1345b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
134647c6ae99SBarry Smith {
134747c6ae99SBarry Smith   PetscErrorCode ierr;
134847c6ae99SBarry Smith 
134947c6ae99SBarry Smith   PetscFunctionBegin;
1350171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1351c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1352b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13535a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1354fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1355b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
135676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1357c6b011d8SStefano Zampini     DM mdm;
1358c6b011d8SStefano Zampini 
1359c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1360c6b011d8SStefano Zampini     if (!mdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix\n",((PetscObject)dm)->type_name);
1361c6b011d8SStefano Zampini   }
1362e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1363e5e52638SMatthew G. Knepley   if (dm->Nf) {
1364e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1365e571a35bSMatthew G. Knepley     PetscInt     Nf;
1366e571a35bSMatthew G. Knepley 
1367e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1368e571a35bSMatthew G. Knepley     if (Nf == 1) {
1369e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1370e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1371e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1372e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1373e571a35bSMatthew G. Knepley       }
1374e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1375e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1376e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1377e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1378e571a35bSMatthew G. Knepley       }
1379e571a35bSMatthew G. Knepley     }
1380e571a35bSMatthew G. Knepley   }
1381fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
138247c6ae99SBarry Smith   PetscFunctionReturn(0);
138347c6ae99SBarry Smith }
138447c6ae99SBarry Smith 
1385732e2eb9SMatthew G Knepley /*@
1386950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1387732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1388732e2eb9SMatthew G Knepley 
1389d083f849SBarry Smith   Logically Collective on dm
1390732e2eb9SMatthew G Knepley 
1391732e2eb9SMatthew G Knepley   Input Parameter:
1392732e2eb9SMatthew G Knepley + dm - the DM
1393732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1394732e2eb9SMatthew G Knepley 
1395732e2eb9SMatthew G Knepley   Level: developer
1396b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1397732e2eb9SMatthew G Knepley @*/
1398732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1399732e2eb9SMatthew G Knepley {
1400732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1401732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1402732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1403732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1404732e2eb9SMatthew G Knepley }
1405732e2eb9SMatthew G Knepley 
1406b06ff27eSHong Zhang /*@
1407b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1408b06ff27eSHong Zhang     but the array for values will not be allocated.
1409b06ff27eSHong Zhang 
1410d083f849SBarry Smith   Logically Collective on dm
1411b06ff27eSHong Zhang 
1412b06ff27eSHong Zhang   Input Parameter:
1413b06ff27eSHong Zhang + dm - the DM
1414b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1415b06ff27eSHong Zhang 
1416b06ff27eSHong Zhang   Level: developer
1417b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1418b06ff27eSHong Zhang @*/
1419b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1420b06ff27eSHong Zhang {
1421b06ff27eSHong Zhang   PetscFunctionBegin;
1422b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1423b06ff27eSHong Zhang   dm->structure_only = only;
1424b06ff27eSHong Zhang   PetscFunctionReturn(0);
1425b06ff27eSHong Zhang }
1426b06ff27eSHong Zhang 
1427a89ea682SMatthew G Knepley /*@C
1428aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1429a89ea682SMatthew G Knepley 
1430a89ea682SMatthew G Knepley   Not Collective
1431a89ea682SMatthew G Knepley 
1432a89ea682SMatthew G Knepley   Input Parameters:
1433a89ea682SMatthew G Knepley + dm - the DM object
1434aa1993deSMatthew G Knepley . count - The minium size
143569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1436a89ea682SMatthew G Knepley 
1437a89ea682SMatthew G Knepley   Output Parameter:
1438a89ea682SMatthew G Knepley . array - the work array
1439a89ea682SMatthew G Knepley 
1440a89ea682SMatthew G Knepley   Level: developer
1441a89ea682SMatthew G Knepley 
1442a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1443a89ea682SMatthew G Knepley @*/
144469291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1445a89ea682SMatthew G Knepley {
1446a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1447aa1993deSMatthew G Knepley   DMWorkLink     link;
144869291d52SBarry Smith   PetscMPIInt    dsize;
1449a89ea682SMatthew G Knepley 
1450a89ea682SMatthew G Knepley   PetscFunctionBegin;
1451a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1452aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1453aa1993deSMatthew G Knepley   if (dm->workin) {
1454aa1993deSMatthew G Knepley     link       = dm->workin;
1455aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1456aa1993deSMatthew G Knepley   } else {
1457b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1458a89ea682SMatthew G Knepley   }
145969291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14605056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1461aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1462854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1463854ce69bSBarry Smith     link->bytes = dsize*count;
1464aa1993deSMatthew G Knepley   }
1465aa1993deSMatthew G Knepley   link->next   = dm->workout;
1466aa1993deSMatthew G Knepley   dm->workout  = link;
146700d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
146800d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
146900d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
147000d952a4SJed Brown #endif
1471aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1472a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1473a89ea682SMatthew G Knepley }
1474a89ea682SMatthew G Knepley 
1475aa1993deSMatthew G Knepley /*@C
1476aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1477aa1993deSMatthew G Knepley 
1478aa1993deSMatthew G Knepley   Not Collective
1479aa1993deSMatthew G Knepley 
1480aa1993deSMatthew G Knepley   Input Parameters:
1481aa1993deSMatthew G Knepley + dm - the DM object
1482aa1993deSMatthew G Knepley . count - The minium size
148369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1484aa1993deSMatthew G Knepley 
1485aa1993deSMatthew G Knepley   Output Parameter:
1486aa1993deSMatthew G Knepley . array - the work array
1487aa1993deSMatthew G Knepley 
1488aa1993deSMatthew G Knepley   Level: developer
1489aa1993deSMatthew G Knepley 
149095452b02SPatrick Sanan   Developer Notes:
149195452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1492aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1493aa1993deSMatthew G Knepley @*/
149469291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1495aa1993deSMatthew G Knepley {
1496aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1497aa1993deSMatthew G Knepley 
1498aa1993deSMatthew G Knepley   PetscFunctionBegin;
1499aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1500aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1501aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1502aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1503aa1993deSMatthew G Knepley       *p           = link->next;
1504aa1993deSMatthew G Knepley       link->next   = dm->workin;
1505aa1993deSMatthew G Knepley       dm->workin   = link;
15060298fd71SBarry Smith       *(void**)mem = NULL;
1507aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1508aa1993deSMatthew G Knepley     }
1509aa1993deSMatthew G Knepley   }
1510aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1511aa1993deSMatthew G Knepley }
1512e7c4fc90SDmitry Karpeev 
1513435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1514435a35e8SMatthew G Knepley {
1515435a35e8SMatthew G Knepley   PetscFunctionBegin;
1516435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
151782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1518435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1519435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1520435a35e8SMatthew G Knepley }
1521435a35e8SMatthew G Knepley 
15220a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
15230a50eb56SMatthew G. Knepley {
15240a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15250a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1526f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15270a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15280a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15290a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15300a50eb56SMatthew G. Knepley }
15310a50eb56SMatthew G. Knepley 
1532f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1533f9d4088aSMatthew G. Knepley {
1534f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1535f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1536f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1537f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1538f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1539f9d4088aSMatthew G. Knepley }
1540f9d4088aSMatthew G. Knepley 
1541f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1542f9d4088aSMatthew G. Knepley {
1543f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1544f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1545f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1546f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1547f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1548f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1549f9d4088aSMatthew G. Knepley }
1550f9d4088aSMatthew G. Knepley 
15514f3b5142SJed Brown /*@C
15524d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
15534d343eeaSMatthew G Knepley 
15544d343eeaSMatthew G Knepley   Not collective
15554d343eeaSMatthew G Knepley 
15564d343eeaSMatthew G Knepley   Input Parameter:
15574d343eeaSMatthew G Knepley . dm - the DM object
15584d343eeaSMatthew G Knepley 
15594d343eeaSMatthew G Knepley   Output Parameters:
15600298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
15610298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
15620298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
15634d343eeaSMatthew G Knepley 
15644d343eeaSMatthew G Knepley   Level: intermediate
15654d343eeaSMatthew G Knepley 
156621c9b008SJed Brown   Notes:
156721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
156821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
156921c9b008SJed Brown   PetscFree().
157021c9b008SJed Brown 
15714d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
15724d343eeaSMatthew G Knepley @*/
157337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
15744d343eeaSMatthew G Knepley {
157537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15764d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15774d343eeaSMatthew G Knepley 
15784d343eeaSMatthew G Knepley   PetscFunctionBegin;
15794d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
158069ca1f37SDmitry Karpeev   if (numFields) {
1581534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
158269ca1f37SDmitry Karpeev     *numFields = 0;
158369ca1f37SDmitry Karpeev   }
158437d0c07bSMatthew G Knepley   if (fieldNames) {
158537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15860298fd71SBarry Smith     *fieldNames = NULL;
158769ca1f37SDmitry Karpeev   }
158869ca1f37SDmitry Karpeev   if (fields) {
158969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15900298fd71SBarry Smith     *fields = NULL;
159169ca1f37SDmitry Karpeev   }
159292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
159337d0c07bSMatthew G Knepley   if (section) {
15943a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
159537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
159637d0c07bSMatthew G Knepley 
1597e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
159837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15993a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
160037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
160137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
160237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
16033a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
160437d0c07bSMatthew G Knepley     }
160537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
160637d0c07bSMatthew G Knepley       PetscInt gdof;
160737d0c07bSMatthew G Knepley 
160837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
160937d0c07bSMatthew G Knepley       if (gdof > 0) {
161037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
16113a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
161237d0c07bSMatthew G Knepley 
161337d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
161437d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
16153a544194SStefano Zampini           fpdof = fdof-fcdof;
16163a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
16173a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
16183a544194SStefano Zampini             fieldNc[f] = 1;
16193a544194SStefano Zampini           }
16203a544194SStefano Zampini           fieldSizes[f] += fpdof;
162137d0c07bSMatthew G Knepley         }
162237d0c07bSMatthew G Knepley       }
162337d0c07bSMatthew G Knepley     }
162437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1625785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
162637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
162737d0c07bSMatthew G Knepley     }
162837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
162937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
163037d0c07bSMatthew G Knepley 
163137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
163237d0c07bSMatthew G Knepley       if (gdof > 0) {
163337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
163437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
163537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
163637d0c07bSMatthew G Knepley 
163737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
163837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
163937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
164037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
164137d0c07bSMatthew G Knepley           }
164237d0c07bSMatthew G Knepley         }
164337d0c07bSMatthew G Knepley       }
164437d0c07bSMatthew G Knepley     }
16458865f1eaSKarl Rupp     if (numFields) *numFields = nF;
164637d0c07bSMatthew G Knepley     if (fieldNames) {
1647785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
164837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
164937d0c07bSMatthew G Knepley         const char *fieldName;
165037d0c07bSMatthew G Knepley 
165137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
165237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
165337d0c07bSMatthew G Knepley       }
165437d0c07bSMatthew G Knepley     }
165537d0c07bSMatthew G Knepley     if (fields) {
1656785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
165737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
16583a544194SStefano Zampini         PetscInt bs, in[2], out[2];
16593a544194SStefano Zampini 
166082f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
16613a544194SStefano Zampini         in[0] = -fieldNc[f];
16623a544194SStefano Zampini         in[1] = fieldNc[f];
16633a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
16643a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
16653a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
166637d0c07bSMatthew G Knepley       }
166737d0c07bSMatthew G Knepley     }
16683a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
16698865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
16708865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
167169ca1f37SDmitry Karpeev   }
16724d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
16734d343eeaSMatthew G Knepley }
16744d343eeaSMatthew G Knepley 
167516621825SDmitry Karpeev 
167616621825SDmitry Karpeev /*@C
167716621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
167816621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
167916621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1680e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1681e7c4fc90SDmitry Karpeev 
1682e7c4fc90SDmitry Karpeev   Not collective
1683e7c4fc90SDmitry Karpeev 
1684e7c4fc90SDmitry Karpeev   Input Parameter:
1685e7c4fc90SDmitry Karpeev . dm - the DM object
1686e7c4fc90SDmitry Karpeev 
1687e7c4fc90SDmitry Karpeev   Output Parameters:
16880298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16890298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16900298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16910298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1692e7c4fc90SDmitry Karpeev 
1693e7c4fc90SDmitry Karpeev   Level: intermediate
1694e7c4fc90SDmitry Karpeev 
1695e7c4fc90SDmitry Karpeev   Notes:
1696e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1697e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1698e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1699e7c4fc90SDmitry Karpeev 
1700e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1701e7c4fc90SDmitry Karpeev @*/
170216621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1703e7c4fc90SDmitry Karpeev {
1704e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1705e7c4fc90SDmitry Karpeev 
1706e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1707e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17088865f1eaSKarl Rupp   if (len) {
1709534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
17108865f1eaSKarl Rupp     *len = 0;
17118865f1eaSKarl Rupp   }
17128865f1eaSKarl Rupp   if (namelist) {
17138865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
17148865f1eaSKarl Rupp     *namelist = 0;
17158865f1eaSKarl Rupp   }
17168865f1eaSKarl Rupp   if (islist) {
17178865f1eaSKarl Rupp     PetscValidPointer(islist,4);
17188865f1eaSKarl Rupp     *islist = 0;
17198865f1eaSKarl Rupp   }
17208865f1eaSKarl Rupp   if (dmlist) {
17218865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
17228865f1eaSKarl Rupp     *dmlist = 0;
17238865f1eaSKarl Rupp   }
1724f3f0edfdSDmitry Karpeev   /*
1725f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1726f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1727f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1728f3f0edfdSDmitry Karpeev    */
1729ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
173016621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1731435a35e8SMatthew G Knepley     PetscSection section;
1732435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1733435a35e8SMatthew G Knepley 
173492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1735435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1736435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1737f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
173803dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
173903dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
174003dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1741435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1742435a35e8SMatthew G Knepley         const char *fieldName;
1743435a35e8SMatthew G Knepley 
174403dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
174503dc3394SMatthew G. Knepley         if (namelist) {
1746435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1747435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1748435a35e8SMatthew G Knepley         }
174903dc3394SMatthew G. Knepley       }
1750435a35e8SMatthew G Knepley     } else {
175169ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1752e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
17530298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1754e7c4fc90SDmitry Karpeev     }
17558865f1eaSKarl Rupp   } else {
175616621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
175716621825SDmitry Karpeev   }
175816621825SDmitry Karpeev   PetscFunctionReturn(0);
175916621825SDmitry Karpeev }
176016621825SDmitry Karpeev 
1761564cec59SMatthew G. Knepley /*@
1762435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1763435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1764435a35e8SMatthew G Knepley 
1765435a35e8SMatthew G Knepley   Not collective
1766435a35e8SMatthew G Knepley 
1767435a35e8SMatthew G Knepley   Input Parameters:
17682adcc780SMatthew G. Knepley + dm        - The DM object
17692adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
17702adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1771435a35e8SMatthew G Knepley 
1772435a35e8SMatthew G Knepley   Output Parameters:
17732adcc780SMatthew G. Knepley + is - The global indices for the subproblem
17742adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1775435a35e8SMatthew G Knepley 
17765d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17775d3b26e6SMatthew G. Knepley 
1778435a35e8SMatthew G Knepley   Level: intermediate
1779435a35e8SMatthew G Knepley 
17805d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1781435a35e8SMatthew G Knepley @*/
178237bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1783435a35e8SMatthew G Knepley {
1784435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1785435a35e8SMatthew G Knepley 
1786435a35e8SMatthew G Knepley   PetscFunctionBegin;
1787435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1788435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17898865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17908865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1791b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1792435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1793435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1794435a35e8SMatthew G Knepley }
1795435a35e8SMatthew G Knepley 
17962adcc780SMatthew G. Knepley /*@C
17972adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17982adcc780SMatthew G. Knepley 
17992adcc780SMatthew G. Knepley   Not collective
18002adcc780SMatthew G. Knepley 
18012adcc780SMatthew G. Knepley   Input Parameter:
18022adcc780SMatthew G. Knepley + dms - The DM objects
18032adcc780SMatthew G. Knepley - len - The number of DMs
18042adcc780SMatthew G. Knepley 
18052adcc780SMatthew G. Knepley   Output Parameters:
1806a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
18072adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
18082adcc780SMatthew G. Knepley 
18095d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
18105d3b26e6SMatthew G. Knepley 
18112adcc780SMatthew G. Knepley   Level: intermediate
18122adcc780SMatthew G. Knepley 
18135d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
18142adcc780SMatthew G. Knepley @*/
18152adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
18162adcc780SMatthew G. Knepley {
18172adcc780SMatthew G. Knepley   PetscInt       i;
18182adcc780SMatthew G. Knepley   PetscErrorCode ierr;
18192adcc780SMatthew G. Knepley 
18202adcc780SMatthew G. Knepley   PetscFunctionBegin;
18212adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
18222adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
18232adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1824a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
18252adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
18262adcc780SMatthew G. Knepley   if (len) {
1827b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1828b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1829b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
18302adcc780SMatthew G. Knepley   }
18312adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
18322adcc780SMatthew G. Knepley }
18332adcc780SMatthew G. Knepley 
183416621825SDmitry Karpeev 
183516621825SDmitry Karpeev /*@C
18368d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
18378d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
18388d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
18398d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
18408d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
184116621825SDmitry Karpeev 
184216621825SDmitry Karpeev   Not collective
184316621825SDmitry Karpeev 
184416621825SDmitry Karpeev   Input Parameter:
184516621825SDmitry Karpeev . dm - the DM object
184616621825SDmitry Karpeev 
184716621825SDmitry Karpeev   Output Parameters:
18480298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
18490298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
18500298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
18510298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
18520298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
185316621825SDmitry Karpeev 
185416621825SDmitry Karpeev   Level: intermediate
185516621825SDmitry Karpeev 
185616621825SDmitry Karpeev   Notes:
185716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
185816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
185916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
186016621825SDmitry Karpeev 
1861245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
186216621825SDmitry Karpeev @*/
18638d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
186416621825SDmitry Karpeev {
186516621825SDmitry Karpeev   PetscErrorCode      ierr;
1866be081cd6SPeter Brune   DMSubDomainHookLink link;
1867be081cd6SPeter Brune   PetscInt            i,l;
186816621825SDmitry Karpeev 
186916621825SDmitry Karpeev   PetscFunctionBegin;
187016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
187114a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
18720298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
18730298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
18740298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
18750298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1876f3f0edfdSDmitry Karpeev   /*
1877f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1878f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1879f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1880f3f0edfdSDmitry Karpeev    */
1881ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
188216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1883be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
188414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1885f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1886be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1887be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1888be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1889be081cd6SPeter Brune         }
1890648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1891e7c4fc90SDmitry Karpeev       }
189214a18fd3SPeter Brune     }
189314a18fd3SPeter Brune     if (len) *len = l;
189414a18fd3SPeter Brune   }
1895e30e807fSPeter Brune   PetscFunctionReturn(0);
1896e30e807fSPeter Brune }
1897e30e807fSPeter Brune 
1898e30e807fSPeter Brune 
1899e30e807fSPeter Brune /*@C
1900e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1901e30e807fSPeter Brune 
1902e30e807fSPeter Brune   Not collective
1903e30e807fSPeter Brune 
1904e30e807fSPeter Brune   Input Parameters:
1905e30e807fSPeter Brune + dm - the DM object
1906e30e807fSPeter Brune . n  - the number of subdomain scatters
1907e30e807fSPeter Brune - subdms - the local subdomains
1908e30e807fSPeter Brune 
1909e30e807fSPeter Brune   Output Parameters:
1910e30e807fSPeter Brune + n     - the number of scatters returned
1911e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1912e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1913e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1914e30e807fSPeter Brune 
191595452b02SPatrick Sanan   Notes:
191695452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1917e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1918e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1919e30e807fSPeter Brune   solution and residual data.
1920e30e807fSPeter Brune 
1921e30e807fSPeter Brune   Level: developer
1922e30e807fSPeter Brune 
1923e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1924e30e807fSPeter Brune @*/
1925e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1926e30e807fSPeter Brune {
1927e30e807fSPeter Brune   PetscErrorCode ierr;
1928e30e807fSPeter Brune 
1929e30e807fSPeter Brune   PetscFunctionBegin;
1930e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1931e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1932b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1933e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1934e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1935e7c4fc90SDmitry Karpeev }
1936e7c4fc90SDmitry Karpeev 
193747c6ae99SBarry Smith /*@
193847c6ae99SBarry Smith   DMRefine - Refines a DM object
193947c6ae99SBarry Smith 
1940d083f849SBarry Smith   Collective on dm
194147c6ae99SBarry Smith 
194247c6ae99SBarry Smith   Input Parameter:
194347c6ae99SBarry Smith + dm   - the DM object
194491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
194547c6ae99SBarry Smith 
194647c6ae99SBarry Smith   Output Parameter:
19470298fd71SBarry Smith . dmf - the refined DM, or NULL
1948ae0a1c52SMatthew G Knepley 
1949412e9a14SMatthew G. Knepley   Options Dtabase Keys:
1950412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
1951412e9a14SMatthew G. Knepley 
19520298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
195347c6ae99SBarry Smith 
195447c6ae99SBarry Smith   Level: developer
195547c6ae99SBarry Smith 
1956e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
195747c6ae99SBarry Smith @*/
19587087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
195947c6ae99SBarry Smith {
196047c6ae99SBarry Smith   PetscErrorCode   ierr;
1961c833c3b5SJed Brown   DMRefineHookLink link;
196247c6ae99SBarry Smith 
196347c6ae99SBarry Smith   PetscFunctionBegin;
1964732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1965b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
19661ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
196747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
19684057135bSMatthew G Knepley   if (*dmf) {
196943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
19708865f1eaSKarl Rupp 
19718cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
19728865f1eaSKarl Rupp 
1973644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
19740598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1975656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
19768865f1eaSKarl Rupp 
1977e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1978c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19798865f1eaSKarl Rupp       if (link->refinehook) {
19808865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19818865f1eaSKarl Rupp       }
1982c833c3b5SJed Brown     }
1983c833c3b5SJed Brown   }
19841ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1985c833c3b5SJed Brown   PetscFunctionReturn(0);
1986c833c3b5SJed Brown }
1987c833c3b5SJed Brown 
1988bb9467b5SJed Brown /*@C
1989c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1990c833c3b5SJed Brown 
1991c833c3b5SJed Brown    Logically Collective
1992c833c3b5SJed Brown 
1993c833c3b5SJed Brown    Input Arguments:
1994c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1995c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1996c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19970298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1998c833c3b5SJed Brown 
1999c833c3b5SJed Brown    Calling sequence of refinehook:
2000c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2001c833c3b5SJed Brown 
2002c833c3b5SJed Brown +  coarse - coarse level DM
2003c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2004c833c3b5SJed Brown -  ctx - optional user-defined function context
2005c833c3b5SJed Brown 
2006c833c3b5SJed Brown    Calling sequence for interphook:
2007c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2008c833c3b5SJed Brown 
2009c833c3b5SJed Brown +  coarse - coarse level DM
2010c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2011c833c3b5SJed Brown .  fine - fine level DM to update
2012c833c3b5SJed Brown -  ctx - optional user-defined function context
2013c833c3b5SJed Brown 
2014c833c3b5SJed Brown    Level: advanced
2015c833c3b5SJed Brown 
2016c833c3b5SJed Brown    Notes:
2017c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2018c833c3b5SJed Brown 
2019c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2020c833c3b5SJed Brown 
2021bb9467b5SJed Brown    This function is currently not available from Fortran.
2022bb9467b5SJed Brown 
2023c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2024c833c3b5SJed Brown @*/
2025c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2026c833c3b5SJed Brown {
2027c833c3b5SJed Brown   PetscErrorCode   ierr;
2028c833c3b5SJed Brown   DMRefineHookLink link,*p;
2029c833c3b5SJed Brown 
2030c833c3b5SJed Brown   PetscFunctionBegin;
2031c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20323d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
20333d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
20343d8e3701SJed Brown   }
203595dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2036c833c3b5SJed Brown   link->refinehook = refinehook;
2037c833c3b5SJed Brown   link->interphook = interphook;
2038c833c3b5SJed Brown   link->ctx        = ctx;
20390298fd71SBarry Smith   link->next       = NULL;
2040c833c3b5SJed Brown   *p               = link;
2041c833c3b5SJed Brown   PetscFunctionReturn(0);
2042c833c3b5SJed Brown }
2043c833c3b5SJed Brown 
20443d8e3701SJed Brown /*@C
20453d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
20463d8e3701SJed Brown 
20473d8e3701SJed Brown    Logically Collective
20483d8e3701SJed Brown 
20493d8e3701SJed Brown    Input Arguments:
20503d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
20513d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
20523d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20533d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20543d8e3701SJed Brown 
20553d8e3701SJed Brown    Level: advanced
20563d8e3701SJed Brown 
20573d8e3701SJed Brown    Notes:
20583d8e3701SJed Brown    This function does nothing if the hook is not in the list.
20593d8e3701SJed Brown 
20603d8e3701SJed Brown    This function is currently not available from Fortran.
20613d8e3701SJed Brown 
20623d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20633d8e3701SJed Brown @*/
20643d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
20653d8e3701SJed Brown {
20663d8e3701SJed Brown   PetscErrorCode   ierr;
20673d8e3701SJed Brown   DMRefineHookLink link,*p;
20683d8e3701SJed Brown 
20693d8e3701SJed Brown   PetscFunctionBegin;
20703d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20713d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
20723d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
20733d8e3701SJed Brown       link = *p;
20743d8e3701SJed Brown       *p = link->next;
20753d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
20763d8e3701SJed Brown       break;
20773d8e3701SJed Brown     }
20783d8e3701SJed Brown   }
20793d8e3701SJed Brown   PetscFunctionReturn(0);
20803d8e3701SJed Brown }
20813d8e3701SJed Brown 
2082c833c3b5SJed Brown /*@
2083c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2084c833c3b5SJed Brown 
2085c833c3b5SJed Brown    Collective if any hooks are
2086c833c3b5SJed Brown 
2087c833c3b5SJed Brown    Input Arguments:
2088c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2089e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2090c833c3b5SJed Brown -  fine - finer DM to update
2091c833c3b5SJed Brown 
2092c833c3b5SJed Brown    Level: developer
2093c833c3b5SJed Brown 
2094c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2095c833c3b5SJed Brown @*/
2096c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2097c833c3b5SJed Brown {
2098c833c3b5SJed Brown   PetscErrorCode   ierr;
2099c833c3b5SJed Brown   DMRefineHookLink link;
2100c833c3b5SJed Brown 
2101c833c3b5SJed Brown   PetscFunctionBegin;
2102c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
21038865f1eaSKarl Rupp     if (link->interphook) {
21048865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
21058865f1eaSKarl Rupp     }
21064057135bSMatthew G Knepley   }
210747c6ae99SBarry Smith   PetscFunctionReturn(0);
210847c6ae99SBarry Smith }
210947c6ae99SBarry Smith 
2110eb3f98d2SBarry Smith /*@
2111eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2112eb3f98d2SBarry Smith 
2113eb3f98d2SBarry Smith     Not Collective
2114eb3f98d2SBarry Smith 
2115eb3f98d2SBarry Smith     Input Parameter:
2116eb3f98d2SBarry Smith .   dm - the DM object
2117eb3f98d2SBarry Smith 
2118eb3f98d2SBarry Smith     Output Parameter:
2119eb3f98d2SBarry Smith .   level - number of refinements
2120eb3f98d2SBarry Smith 
2121eb3f98d2SBarry Smith     Level: developer
2122eb3f98d2SBarry Smith 
21236a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2124eb3f98d2SBarry Smith 
2125eb3f98d2SBarry Smith @*/
2126eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2127eb3f98d2SBarry Smith {
2128eb3f98d2SBarry Smith   PetscFunctionBegin;
2129eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2130eb3f98d2SBarry Smith   *level = dm->levelup;
2131eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2132eb3f98d2SBarry Smith }
2133eb3f98d2SBarry Smith 
2134fef3a512SBarry Smith /*@
2135fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2136fef3a512SBarry Smith 
2137fef3a512SBarry Smith     Not Collective
2138fef3a512SBarry Smith 
2139fef3a512SBarry Smith     Input Parameter:
2140fef3a512SBarry Smith +   dm - the DM object
2141fef3a512SBarry Smith -   level - number of refinements
2142fef3a512SBarry Smith 
2143fef3a512SBarry Smith     Level: advanced
2144fef3a512SBarry Smith 
214595452b02SPatrick Sanan     Notes:
214695452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2147fef3a512SBarry Smith 
2148fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2149fef3a512SBarry Smith 
2150fef3a512SBarry Smith @*/
2151fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2152fef3a512SBarry Smith {
2153fef3a512SBarry Smith   PetscFunctionBegin;
2154fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2155fef3a512SBarry Smith   dm->levelup = level;
2156fef3a512SBarry Smith   PetscFunctionReturn(0);
2157fef3a512SBarry Smith }
2158fef3a512SBarry Smith 
2159ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2160ca3d3a14SMatthew G. Knepley {
2161ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2162ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2163ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2164ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2165ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2166ca3d3a14SMatthew G. Knepley }
2167ca3d3a14SMatthew G. Knepley 
2168ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2169ca3d3a14SMatthew G. Knepley {
2170ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2171ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2172ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2173ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2174ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2175ca3d3a14SMatthew G. Knepley }
2176ca3d3a14SMatthew G. Knepley 
2177ca3d3a14SMatthew G. Knepley /*@
2178c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2179ca3d3a14SMatthew G. Knepley 
2180ca3d3a14SMatthew G. Knepley   Input Parameter:
2181ca3d3a14SMatthew G. Knepley . dm - The DM
2182ca3d3a14SMatthew G. Knepley 
2183ca3d3a14SMatthew G. Knepley   Output Parameter:
2184ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2185ca3d3a14SMatthew G. Knepley 
2186ca3d3a14SMatthew G. Knepley   Level: developer
2187ca3d3a14SMatthew G. Knepley 
2188436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2189ca3d3a14SMatthew G. Knepley @*/
2190ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2191ca3d3a14SMatthew G. Knepley {
2192ca3d3a14SMatthew G. Knepley   Vec            tv;
2193ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2194ca3d3a14SMatthew G. Knepley 
2195ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2196ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2197534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2198ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2199ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2200ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2201ca3d3a14SMatthew G. Knepley }
2202ca3d3a14SMatthew G. Knepley 
2203ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2204ca3d3a14SMatthew G. Knepley {
2205ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2206ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2207ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2208ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2209ca3d3a14SMatthew G. Knepley 
2210ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2211ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
221292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2213ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2214ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2215ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
221692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2217ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2218ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2219ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2220ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2221ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2222ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2223ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2224ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2225ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2226ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2227ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2228ca3d3a14SMatthew G. Knepley     }
2229ca3d3a14SMatthew G. Knepley   }
2230ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2231ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2232ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2233ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2234ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2235ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2236ca3d3a14SMatthew G. Knepley       if (dof) {
2237ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2238ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2239ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2240ca3d3a14SMatthew G. Knepley 
2241ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2242ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2243ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2244580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2245ca3d3a14SMatthew G. Knepley       }
2246ca3d3a14SMatthew G. Knepley     }
2247ca3d3a14SMatthew G. Knepley   }
2248ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2249ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2250ca3d3a14SMatthew G. Knepley }
2251ca3d3a14SMatthew G. Knepley 
2252ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2253ca3d3a14SMatthew G. Knepley {
2254ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2255ca3d3a14SMatthew G. Knepley 
2256ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2257ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2258ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2259ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2260ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2261ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2262ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2263ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2264ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2265ca3d3a14SMatthew G. Knepley }
2266ca3d3a14SMatthew G. Knepley 
2267bb9467b5SJed Brown /*@C
2268baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2269baf369e7SPeter Brune 
2270baf369e7SPeter Brune    Logically Collective
2271baf369e7SPeter Brune 
2272baf369e7SPeter Brune    Input Arguments:
2273baf369e7SPeter Brune +  dm - the DM
2274baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2275baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
22760298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2277baf369e7SPeter Brune 
2278baf369e7SPeter Brune    Calling sequence for beginhook:
2279baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2280baf369e7SPeter Brune 
2281baf369e7SPeter Brune +  dm - global DM
2282baf369e7SPeter Brune .  g - global vector
2283baf369e7SPeter Brune .  mode - mode
2284baf369e7SPeter Brune .  l - local vector
2285baf369e7SPeter Brune -  ctx - optional user-defined function context
2286baf369e7SPeter Brune 
2287baf369e7SPeter Brune 
2288baf369e7SPeter Brune    Calling sequence for endhook:
2289ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2290baf369e7SPeter Brune 
2291baf369e7SPeter Brune +  global - global DM
2292baf369e7SPeter Brune -  ctx - optional user-defined function context
2293baf369e7SPeter Brune 
2294baf369e7SPeter Brune    Level: advanced
2295baf369e7SPeter Brune 
2296baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2297baf369e7SPeter Brune @*/
2298baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2299baf369e7SPeter Brune {
2300baf369e7SPeter Brune   PetscErrorCode          ierr;
2301baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2302baf369e7SPeter Brune 
2303baf369e7SPeter Brune   PetscFunctionBegin;
2304baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2305baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
230695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2307baf369e7SPeter Brune   link->beginhook = beginhook;
2308baf369e7SPeter Brune   link->endhook   = endhook;
2309baf369e7SPeter Brune   link->ctx       = ctx;
23100298fd71SBarry Smith   link->next      = NULL;
2311baf369e7SPeter Brune   *p              = link;
2312baf369e7SPeter Brune   PetscFunctionReturn(0);
2313baf369e7SPeter Brune }
2314baf369e7SPeter Brune 
23154c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
23164c274da1SToby Isaac {
23174c274da1SToby Isaac   Mat cMat;
23184c274da1SToby Isaac   Vec cVec;
23194c274da1SToby Isaac   PetscSection section, cSec;
23204c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23214c274da1SToby Isaac   PetscErrorCode ierr;
23224c274da1SToby Isaac 
23234c274da1SToby Isaac   PetscFunctionBegin;
23244c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23254c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23264c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
23275db9a05bSToby Isaac     PetscInt nRows;
23285db9a05bSToby Isaac 
23295db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23305db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
233192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
23327711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23334c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
23344c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23354c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23364c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23374c274da1SToby Isaac       if (dof) {
23384c274da1SToby Isaac         PetscScalar *vals;
23394c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
23404c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
23414c274da1SToby Isaac       }
23424c274da1SToby Isaac     }
23434c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23444c274da1SToby Isaac   }
23454c274da1SToby Isaac   PetscFunctionReturn(0);
23464c274da1SToby Isaac }
23474c274da1SToby Isaac 
234847c6ae99SBarry Smith /*@
234901729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
235001729b5cSPatrick Sanan 
2351d083f849SBarry Smith     Neighbor-wise Collective on dm
235201729b5cSPatrick Sanan 
235301729b5cSPatrick Sanan     Input Parameters:
235401729b5cSPatrick Sanan +   dm - the DM object
235501729b5cSPatrick Sanan .   g - the global vector
235601729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
235701729b5cSPatrick Sanan -   l - the local vector
235801729b5cSPatrick Sanan 
235901729b5cSPatrick Sanan     Notes:
236001729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
236101729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
236201729b5cSPatrick Sanan 
236301729b5cSPatrick Sanan     Level: beginner
236401729b5cSPatrick Sanan 
236501729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
236601729b5cSPatrick Sanan 
236701729b5cSPatrick Sanan @*/
236801729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
236901729b5cSPatrick Sanan {
237001729b5cSPatrick Sanan   PetscErrorCode ierr;
237101729b5cSPatrick Sanan 
237201729b5cSPatrick Sanan   PetscFunctionBegin;
237301729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
237401729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
237501729b5cSPatrick Sanan   PetscFunctionReturn(0);
237601729b5cSPatrick Sanan }
237701729b5cSPatrick Sanan 
237801729b5cSPatrick Sanan /*@
237947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
238047c6ae99SBarry Smith 
2381d083f849SBarry Smith     Neighbor-wise Collective on dm
238247c6ae99SBarry Smith 
238347c6ae99SBarry Smith     Input Parameters:
238447c6ae99SBarry Smith +   dm - the DM object
238547c6ae99SBarry Smith .   g - the global vector
238647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
238747c6ae99SBarry Smith -   l - the local vector
238847c6ae99SBarry Smith 
238901729b5cSPatrick Sanan     Level: intermediate
239047c6ae99SBarry Smith 
239101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
239247c6ae99SBarry Smith 
239347c6ae99SBarry Smith @*/
23947087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
239547c6ae99SBarry Smith {
23967128ae9fSMatthew G Knepley   PetscSF                 sf;
239747c6ae99SBarry Smith   PetscErrorCode          ierr;
2398baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
239947c6ae99SBarry Smith 
240047c6ae99SBarry Smith   PetscFunctionBegin;
2401171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2402baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
24038865f1eaSKarl Rupp     if (link->beginhook) {
24048865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
24058865f1eaSKarl Rupp     }
2406baf369e7SPeter Brune   }
24071bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
24087128ae9fSMatthew G Knepley   if (sf) {
2409ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2410ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
24117128ae9fSMatthew G Knepley 
241282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2413fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2414fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24157128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2416fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2417fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24187128ae9fSMatthew G Knepley   } else {
241933907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2420843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24217128ae9fSMatthew G Knepley   }
242247c6ae99SBarry Smith   PetscFunctionReturn(0);
242347c6ae99SBarry Smith }
242447c6ae99SBarry Smith 
242547c6ae99SBarry Smith /*@
242647c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
242747c6ae99SBarry Smith 
2428d083f849SBarry Smith     Neighbor-wise Collective on dm
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith     Input Parameters:
243147c6ae99SBarry Smith +   dm - the DM object
243247c6ae99SBarry Smith .   g - the global vector
243347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
243447c6ae99SBarry Smith -   l - the local vector
243547c6ae99SBarry Smith 
243601729b5cSPatrick Sanan     Level: intermediate
243747c6ae99SBarry Smith 
243801729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
243947c6ae99SBarry Smith 
244047c6ae99SBarry Smith @*/
24417087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
244247c6ae99SBarry Smith {
24437128ae9fSMatthew G Knepley   PetscSF                 sf;
244447c6ae99SBarry Smith   PetscErrorCode          ierr;
2445ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2446ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2447ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2448baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
244947c6ae99SBarry Smith 
245047c6ae99SBarry Smith   PetscFunctionBegin;
2451171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24521bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2453ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
24547128ae9fSMatthew G Knepley   if (sf) {
245582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24567128ae9fSMatthew G Knepley 
2457fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2458fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24597128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2460fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2461fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
2462ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
24637128ae9fSMatthew G Knepley   } else {
246433907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2465843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24667128ae9fSMatthew G Knepley   }
24674c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2468baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2469baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2470baf369e7SPeter Brune   }
247147c6ae99SBarry Smith   PetscFunctionReturn(0);
247247c6ae99SBarry Smith }
247347c6ae99SBarry Smith 
2474d4d07f1eSToby Isaac /*@C
2475d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2476d4d07f1eSToby Isaac 
2477d4d07f1eSToby Isaac    Logically Collective
2478d4d07f1eSToby Isaac 
2479d4d07f1eSToby Isaac    Input Arguments:
2480d4d07f1eSToby Isaac +  dm - the DM
2481d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2482d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2483d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2484d4d07f1eSToby Isaac 
2485d4d07f1eSToby Isaac    Calling sequence for beginhook:
2486d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2487d4d07f1eSToby Isaac 
2488d4d07f1eSToby Isaac +  dm - global DM
2489d4d07f1eSToby Isaac .  l - local vector
2490d4d07f1eSToby Isaac .  mode - mode
2491d4d07f1eSToby Isaac .  g - global vector
2492d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2493d4d07f1eSToby Isaac 
2494d4d07f1eSToby Isaac 
2495d4d07f1eSToby Isaac    Calling sequence for endhook:
2496d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2497d4d07f1eSToby Isaac 
2498d4d07f1eSToby Isaac +  global - global DM
2499d4d07f1eSToby Isaac .  l - local vector
2500d4d07f1eSToby Isaac .  mode - mode
2501d4d07f1eSToby Isaac .  g - global vector
2502d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2503d4d07f1eSToby Isaac 
2504d4d07f1eSToby Isaac    Level: advanced
2505d4d07f1eSToby Isaac 
2506d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2507d4d07f1eSToby Isaac @*/
2508d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2509d4d07f1eSToby Isaac {
2510d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2511d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2512d4d07f1eSToby Isaac 
2513d4d07f1eSToby Isaac   PetscFunctionBegin;
2514d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2515d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
251695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2517d4d07f1eSToby Isaac   link->beginhook = beginhook;
2518d4d07f1eSToby Isaac   link->endhook   = endhook;
2519d4d07f1eSToby Isaac   link->ctx       = ctx;
2520d4d07f1eSToby Isaac   link->next      = NULL;
2521d4d07f1eSToby Isaac   *p              = link;
2522d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2523d4d07f1eSToby Isaac }
2524d4d07f1eSToby Isaac 
25254c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
25264c274da1SToby Isaac {
25274c274da1SToby Isaac   Mat cMat;
25284c274da1SToby Isaac   Vec cVec;
25294c274da1SToby Isaac   PetscSection section, cSec;
25304c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25314c274da1SToby Isaac   PetscErrorCode ierr;
25324c274da1SToby Isaac 
25334c274da1SToby Isaac   PetscFunctionBegin;
25344c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25354c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25364c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
25375db9a05bSToby Isaac     PetscInt nRows;
25385db9a05bSToby Isaac 
25395db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25405db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
254192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25427711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25434c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25444c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25454c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25464c274da1SToby Isaac       if (dof) {
25474c274da1SToby Isaac         PetscInt d;
25484c274da1SToby Isaac         PetscScalar *vals;
25494c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
25504c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
25514c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
25524c274da1SToby Isaac          * we just extracted */
25534c274da1SToby Isaac         for (d = 0; d < dof; d++) {
25544c274da1SToby Isaac           vals[d] = 0.;
25554c274da1SToby Isaac         }
25564c274da1SToby Isaac       }
25574c274da1SToby Isaac     }
25584c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
25594c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25604c274da1SToby Isaac   }
25614c274da1SToby Isaac   PetscFunctionReturn(0);
25624c274da1SToby Isaac }
256301729b5cSPatrick Sanan /*@
256401729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
256501729b5cSPatrick Sanan 
2566d083f849SBarry Smith     Neighbor-wise Collective on dm
256701729b5cSPatrick Sanan 
256801729b5cSPatrick Sanan     Input Parameters:
256901729b5cSPatrick Sanan +   dm - the DM object
257001729b5cSPatrick Sanan .   l - the local vector
257101729b5cSPatrick 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.
257201729b5cSPatrick Sanan -   g - the global vector
257301729b5cSPatrick Sanan 
257401729b5cSPatrick Sanan     Notes:
257501729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
257601729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
257701729b5cSPatrick Sanan 
257801729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
257901729b5cSPatrick 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.
258001729b5cSPatrick Sanan 
258101729b5cSPatrick Sanan     Level: beginner
258201729b5cSPatrick Sanan 
258301729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
258401729b5cSPatrick Sanan 
258501729b5cSPatrick Sanan @*/
258601729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
258701729b5cSPatrick Sanan {
258801729b5cSPatrick Sanan   PetscErrorCode ierr;
258901729b5cSPatrick Sanan 
259001729b5cSPatrick Sanan   PetscFunctionBegin;
259101729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
259201729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
259301729b5cSPatrick Sanan   PetscFunctionReturn(0);
259401729b5cSPatrick Sanan }
25954c274da1SToby Isaac 
259647c6ae99SBarry Smith /*@
259701729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25989a42bb27SBarry Smith 
2599d083f849SBarry Smith     Neighbor-wise Collective on dm
26009a42bb27SBarry Smith 
26019a42bb27SBarry Smith     Input Parameters:
26029a42bb27SBarry Smith +   dm - the DM object
2603f6813fd5SJed Brown .   l - the local vector
26041eb28f2eSBarry 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.
26051eb28f2eSBarry Smith -   g - the global vector
26069a42bb27SBarry Smith 
260795452b02SPatrick Sanan     Notes:
260895452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
260984330215SMatthew 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.
26109a42bb27SBarry Smith 
261101729b5cSPatrick Sanan     Level: intermediate
26129a42bb27SBarry Smith 
261301729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
26149a42bb27SBarry Smith 
26159a42bb27SBarry Smith @*/
26167087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
26179a42bb27SBarry Smith {
26187128ae9fSMatthew G Knepley   PetscSF                 sf;
261984330215SMatthew G. Knepley   PetscSection            s, gs;
2620d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2621ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2622ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2623ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2624fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
262584330215SMatthew G. Knepley   PetscErrorCode          ierr;
26269a42bb27SBarry Smith 
26279a42bb27SBarry Smith   PetscFunctionBegin;
2628171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2629d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2630d4d07f1eSToby Isaac     if (link->beginhook) {
2631d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2632d4d07f1eSToby Isaac     }
2633d4d07f1eSToby Isaac   }
26344c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
26351bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
263692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26377128ae9fSMatthew G Knepley   switch (mode) {
26387128ae9fSMatthew G Knepley   case INSERT_VALUES:
26397128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2640304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
264184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26427128ae9fSMatthew G Knepley   case ADD_VALUES:
26437128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2644304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
264584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26467128ae9fSMatthew G Knepley   default:
264782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26487128ae9fSMatthew G Knepley   }
2649ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2650ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2651ca3d3a14SMatthew G. Knepley     if (transform) {
2652ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2653ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2654ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2655ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2656fa88e482SJed Brown     } else if (isInsert) {
2657ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2658fa88e482SJed Brown     } else {
2659fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2660fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2661ca3d3a14SMatthew G. Knepley     }
2662fa88e482SJed Brown     if (s && isInsert) {
26637128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2664fa88e482SJed Brown     } else {
2665fa88e482SJed Brown       ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2666fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2667fa88e482SJed Brown     }
2668ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2669a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
267084330215SMatthew G. Knepley     } else if (s && isInsert) {
267184330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
267284330215SMatthew G. Knepley 
2673e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
267484330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
267584330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
267684330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2677b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
267884330215SMatthew G. Knepley 
267984330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
268003442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
268184330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2682b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
268384330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
268484330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2685b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
268603442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2687b3b16f48SMatthew G. Knepley         if (dof != gdof) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
2688b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2689b3b16f48SMatthew G. Knepley         if (!gcdof) {
269084330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2691b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2692b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
269384330215SMatthew G. Knepley           const PetscInt *cdofs;
269484330215SMatthew G. Knepley           PetscInt        cind = 0;
269584330215SMatthew G. Knepley 
269684330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2697b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
269884330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2699b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
270084330215SMatthew G. Knepley           }
2701b3b16f48SMatthew G. Knepley         } else SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
270284330215SMatthew G. Knepley       }
2703ca3d3a14SMatthew G. Knepley     }
2704fa88e482SJed Brown     if (g_inplace) {
2705fa88e482SJed Brown       ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
2706fa88e482SJed Brown     } else {
27077128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2708fa88e482SJed Brown     }
2709ca3d3a14SMatthew G. Knepley     if (transform) {
2710ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2711ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2712fa88e482SJed Brown     } else if (l_inplace) {
2713fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2714ca3d3a14SMatthew G. Knepley     } else {
2715ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2716ca3d3a14SMatthew G. Knepley     }
27177128ae9fSMatthew G Knepley   } else {
2718b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2719843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27207128ae9fSMatthew G Knepley   }
27219a42bb27SBarry Smith   PetscFunctionReturn(0);
27229a42bb27SBarry Smith }
27239a42bb27SBarry Smith 
27249a42bb27SBarry Smith /*@
27259a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
272647c6ae99SBarry Smith 
2727d083f849SBarry Smith     Neighbor-wise Collective on dm
272847c6ae99SBarry Smith 
272947c6ae99SBarry Smith     Input Parameters:
273047c6ae99SBarry Smith +   dm - the DM object
2731f6813fd5SJed Brown .   l - the local vector
273247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2733f6813fd5SJed Brown -   g - the global vector
273447c6ae99SBarry Smith 
273501729b5cSPatrick Sanan     Level: intermediate
273647c6ae99SBarry Smith 
2737e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
273847c6ae99SBarry Smith 
273947c6ae99SBarry Smith @*/
27407087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
274147c6ae99SBarry Smith {
27427128ae9fSMatthew G Knepley   PetscSF                 sf;
274384330215SMatthew G. Knepley   PetscSection            s;
2744d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2745ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
274684330215SMatthew G. Knepley   PetscErrorCode          ierr;
274747c6ae99SBarry Smith 
274847c6ae99SBarry Smith   PetscFunctionBegin;
2749171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27501bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
275192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27527128ae9fSMatthew G Knepley   switch (mode) {
27537128ae9fSMatthew G Knepley   case INSERT_VALUES:
27547128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
275584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27567128ae9fSMatthew G Knepley   case ADD_VALUES:
27577128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
275884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27597128ae9fSMatthew G Knepley   default:
276082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27617128ae9fSMatthew G Knepley   }
276284330215SMatthew G. Knepley   if (sf && !isInsert) {
2763ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2764ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2765ca3d3a14SMatthew G. Knepley     Vec                tmpl;
276684330215SMatthew G. Knepley 
2767ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2768ca3d3a14SMatthew G. Knepley     if (transform) {
2769ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2770ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2771ca3d3a14SMatthew G. Knepley     } else {
2772fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2773ca3d3a14SMatthew G. Knepley     }
2774fa88e482SJed Brown     ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2775a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2776ca3d3a14SMatthew G. Knepley     if (transform) {
2777ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2778ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2779ca3d3a14SMatthew G. Knepley     } else {
2780fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2781ca3d3a14SMatthew G. Knepley     }
2782fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
278384330215SMatthew G. Knepley   } else if (s && isInsert) {
27847128ae9fSMatthew G Knepley   } else {
2785b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2786843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27877128ae9fSMatthew G Knepley   }
2788d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2789d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2790d4d07f1eSToby Isaac   }
279147c6ae99SBarry Smith   PetscFunctionReturn(0);
279247c6ae99SBarry Smith }
279347c6ae99SBarry Smith 
2794f089877aSRichard Tran Mills /*@
2795bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2796bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2797d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2798f089877aSRichard Tran Mills 
2799d083f849SBarry Smith    Neighbor-wise Collective on dm
2800f089877aSRichard Tran Mills 
2801f089877aSRichard Tran Mills    Input Parameters:
2802f089877aSRichard Tran Mills +  dm - the DM object
2803bc0a1609SRichard Tran Mills .  g - the original local vector
2804bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2805f089877aSRichard Tran Mills 
2806bc0a1609SRichard Tran Mills    Output Parameter:
2807bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2808f089877aSRichard Tran Mills 
2809f089877aSRichard Tran Mills    Level: intermediate
2810f089877aSRichard Tran Mills 
2811bc0a1609SRichard Tran Mills    Notes:
2812bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2813bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2814bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2815bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2816bc0a1609SRichard Tran Mills 
2817bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2818f089877aSRichard Tran Mills 
2819f089877aSRichard Tran Mills @*/
2820f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2821f089877aSRichard Tran Mills {
2822f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2823f089877aSRichard Tran Mills 
2824f089877aSRichard Tran Mills   PetscFunctionBegin;
2825f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2826bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2827f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2828f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2829f089877aSRichard Tran Mills }
2830f089877aSRichard Tran Mills 
2831f089877aSRichard Tran Mills /*@
2832bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2833bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2834d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2835f089877aSRichard Tran Mills 
2836d083f849SBarry Smith    Neighbor-wise Collective on dm
2837f089877aSRichard Tran Mills 
2838f089877aSRichard Tran Mills    Input Parameters:
2839bc0a1609SRichard Tran Mills +  da - the DM object
2840bc0a1609SRichard Tran Mills .  g - the original local vector
2841bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2842f089877aSRichard Tran Mills 
2843bc0a1609SRichard Tran Mills    Output Parameter:
2844bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2845f089877aSRichard Tran Mills 
2846f089877aSRichard Tran Mills    Level: intermediate
2847f089877aSRichard Tran Mills 
2848bc0a1609SRichard Tran Mills    Notes:
2849bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2850bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2851bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2852bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2853bc0a1609SRichard Tran Mills 
2854bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2855f089877aSRichard Tran Mills 
2856f089877aSRichard Tran Mills @*/
2857f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2858f089877aSRichard Tran Mills {
2859f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2860f089877aSRichard Tran Mills 
2861f089877aSRichard Tran Mills   PetscFunctionBegin;
2862f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2863bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2864f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2865f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2866f089877aSRichard Tran Mills }
2867f089877aSRichard Tran Mills 
2868f089877aSRichard Tran Mills 
286947c6ae99SBarry Smith /*@
287047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
287147c6ae99SBarry Smith 
2872d083f849SBarry Smith     Collective on dm
287347c6ae99SBarry Smith 
287447c6ae99SBarry Smith     Input Parameter:
287547c6ae99SBarry Smith +   dm - the DM object
287691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
287747c6ae99SBarry Smith 
287847c6ae99SBarry Smith     Output Parameter:
287947c6ae99SBarry Smith .   dmc - the coarsened DM
288047c6ae99SBarry Smith 
288147c6ae99SBarry Smith     Level: developer
288247c6ae99SBarry Smith 
2883e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
288447c6ae99SBarry Smith 
288547c6ae99SBarry Smith @*/
28867087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
288747c6ae99SBarry Smith {
288847c6ae99SBarry Smith   PetscErrorCode    ierr;
2889b17ce1afSJed Brown   DMCoarsenHookLink link;
289047c6ae99SBarry Smith 
289147c6ae99SBarry Smith   PetscFunctionBegin;
2892171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2893b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
289447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
289547c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2896b9d85ea2SLisandro Dalcin   if (*dmc) {
2897a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
289843842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28998cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2900644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
29010598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2902656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2903e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2904b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2905b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2906b17ce1afSJed Brown     }
2907b9d85ea2SLisandro Dalcin   }
290847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2909b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2910b17ce1afSJed Brown   PetscFunctionReturn(0);
2911b17ce1afSJed Brown }
2912b17ce1afSJed Brown 
2913bb9467b5SJed Brown /*@C
2914b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2915b17ce1afSJed Brown 
2916b17ce1afSJed Brown    Logically Collective
2917b17ce1afSJed Brown 
2918b17ce1afSJed Brown    Input Arguments:
2919b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2920b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2921b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
29220298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2923b17ce1afSJed Brown 
2924b17ce1afSJed Brown    Calling sequence of coarsenhook:
2925b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2926b17ce1afSJed Brown 
2927b17ce1afSJed Brown +  fine - fine level DM
2928b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2929b17ce1afSJed Brown -  ctx - optional user-defined function context
2930b17ce1afSJed Brown 
2931b17ce1afSJed Brown    Calling sequence for restricthook:
2932c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2933b17ce1afSJed Brown 
2934b17ce1afSJed Brown +  fine - fine level DM
2935b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2936c833c3b5SJed Brown .  rscale - scaling vector for restriction
2937c833c3b5SJed Brown .  inject - matrix restricting by injection
2938b17ce1afSJed Brown .  coarse - coarse level DM to update
2939b17ce1afSJed Brown -  ctx - optional user-defined function context
2940b17ce1afSJed Brown 
2941b17ce1afSJed Brown    Level: advanced
2942b17ce1afSJed Brown 
2943b17ce1afSJed Brown    Notes:
2944b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2945b17ce1afSJed Brown 
2946b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2947b17ce1afSJed Brown 
2948b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2949b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2950b17ce1afSJed Brown 
2951bb9467b5SJed Brown    This function is currently not available from Fortran.
2952bb9467b5SJed Brown 
2953dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2954b17ce1afSJed Brown @*/
2955b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2956b17ce1afSJed Brown {
2957b17ce1afSJed Brown   PetscErrorCode    ierr;
2958b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2959b17ce1afSJed Brown 
2960b17ce1afSJed Brown   PetscFunctionBegin;
2961b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
29621e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
29631e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
29641e3d8eccSJed Brown   }
296595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2966b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2967b17ce1afSJed Brown   link->restricthook = restricthook;
2968b17ce1afSJed Brown   link->ctx          = ctx;
29690298fd71SBarry Smith   link->next         = NULL;
2970b17ce1afSJed Brown   *p                 = link;
2971b17ce1afSJed Brown   PetscFunctionReturn(0);
2972b17ce1afSJed Brown }
2973b17ce1afSJed Brown 
2974dc822a44SJed Brown /*@C
2975dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2976dc822a44SJed Brown 
2977dc822a44SJed Brown    Logically Collective
2978dc822a44SJed Brown 
2979dc822a44SJed Brown    Input Arguments:
2980dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2981dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2982dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2983dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2984dc822a44SJed Brown 
2985dc822a44SJed Brown    Level: advanced
2986dc822a44SJed Brown 
2987dc822a44SJed Brown    Notes:
2988dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2989dc822a44SJed Brown 
2990dc822a44SJed Brown    This function is currently not available from Fortran.
2991dc822a44SJed Brown 
2992dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2993dc822a44SJed Brown @*/
2994dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2995dc822a44SJed Brown {
2996dc822a44SJed Brown   PetscErrorCode    ierr;
2997dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2998dc822a44SJed Brown 
2999dc822a44SJed Brown   PetscFunctionBegin;
3000dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3001dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3002dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3003dc822a44SJed Brown       link = *p;
3004dc822a44SJed Brown       *p = link->next;
3005dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3006dc822a44SJed Brown       break;
3007dc822a44SJed Brown     }
3008dc822a44SJed Brown   }
3009dc822a44SJed Brown   PetscFunctionReturn(0);
3010dc822a44SJed Brown }
3011dc822a44SJed Brown 
3012dc822a44SJed Brown 
3013b17ce1afSJed Brown /*@
3014b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3015b17ce1afSJed Brown 
3016b17ce1afSJed Brown    Collective if any hooks are
3017b17ce1afSJed Brown 
3018b17ce1afSJed Brown    Input Arguments:
3019b17ce1afSJed Brown +  fine - finer DM to use as a base
3020b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3021e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3022b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3023e91eccc2SStefano Zampini -  coarse - coarser DM to update
3024b17ce1afSJed Brown 
3025b17ce1afSJed Brown    Level: developer
3026b17ce1afSJed Brown 
3027b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3028b17ce1afSJed Brown @*/
3029b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3030b17ce1afSJed Brown {
3031b17ce1afSJed Brown   PetscErrorCode    ierr;
3032b17ce1afSJed Brown   DMCoarsenHookLink link;
3033b17ce1afSJed Brown 
3034b17ce1afSJed Brown   PetscFunctionBegin;
3035b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
30368865f1eaSKarl Rupp     if (link->restricthook) {
30378865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
30388865f1eaSKarl Rupp     }
3039b17ce1afSJed Brown   }
304047c6ae99SBarry Smith   PetscFunctionReturn(0);
304147c6ae99SBarry Smith }
304247c6ae99SBarry Smith 
3043bb9467b5SJed Brown /*@C
3044be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
30455dbd56e3SPeter Brune 
3046d083f849SBarry Smith    Logically Collective on global
30475dbd56e3SPeter Brune 
30485dbd56e3SPeter Brune    Input Arguments:
30495dbd56e3SPeter Brune +  global - global DM
3050ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
30515dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
30520298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
30535dbd56e3SPeter Brune 
3054ec4806b8SPeter Brune 
3055ec4806b8SPeter Brune    Calling sequence for ddhook:
3056ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3057ec4806b8SPeter Brune 
3058ec4806b8SPeter Brune +  global - global DM
3059ec4806b8SPeter Brune .  block  - block DM
3060ec4806b8SPeter Brune -  ctx - optional user-defined function context
3061ec4806b8SPeter Brune 
30625dbd56e3SPeter Brune    Calling sequence for restricthook:
3063ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
30645dbd56e3SPeter Brune 
30655dbd56e3SPeter Brune +  global - global DM
30665dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
30675dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3068ec4806b8SPeter Brune .  block  - block DM
30695dbd56e3SPeter Brune -  ctx - optional user-defined function context
30705dbd56e3SPeter Brune 
30715dbd56e3SPeter Brune    Level: advanced
30725dbd56e3SPeter Brune 
30735dbd56e3SPeter Brune    Notes:
3074ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
30755dbd56e3SPeter Brune 
30765dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
30775dbd56e3SPeter Brune 
30785dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3079ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
30805dbd56e3SPeter Brune 
3081bb9467b5SJed Brown    This function is currently not available from Fortran.
3082bb9467b5SJed Brown 
30835dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
30845dbd56e3SPeter Brune @*/
3085be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
30865dbd56e3SPeter Brune {
30875dbd56e3SPeter Brune   PetscErrorCode      ierr;
3088be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
30895dbd56e3SPeter Brune 
30905dbd56e3SPeter Brune   PetscFunctionBegin;
30915dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3092b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3093b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3094b3a6b972SJed Brown   }
309595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30965dbd56e3SPeter Brune   link->restricthook = restricthook;
3097be081cd6SPeter Brune   link->ddhook       = ddhook;
30985dbd56e3SPeter Brune   link->ctx          = ctx;
30990298fd71SBarry Smith   link->next         = NULL;
31005dbd56e3SPeter Brune   *p                 = link;
31015dbd56e3SPeter Brune   PetscFunctionReturn(0);
31025dbd56e3SPeter Brune }
31035dbd56e3SPeter Brune 
3104b3a6b972SJed Brown /*@C
3105b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3106b3a6b972SJed Brown 
3107b3a6b972SJed Brown    Logically Collective
3108b3a6b972SJed Brown 
3109b3a6b972SJed Brown    Input Arguments:
3110b3a6b972SJed Brown +  global - global DM
3111b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3112b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3113b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3114b3a6b972SJed Brown 
3115b3a6b972SJed Brown    Level: advanced
3116b3a6b972SJed Brown 
3117b3a6b972SJed Brown    Notes:
3118b3a6b972SJed Brown 
3119b3a6b972SJed Brown    This function is currently not available from Fortran.
3120b3a6b972SJed Brown 
3121b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3122b3a6b972SJed Brown @*/
3123b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3124b3a6b972SJed Brown {
3125b3a6b972SJed Brown   PetscErrorCode      ierr;
3126b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3127b3a6b972SJed Brown 
3128b3a6b972SJed Brown   PetscFunctionBegin;
3129b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3130b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3131b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3132b3a6b972SJed Brown       link = *p;
3133b3a6b972SJed Brown       *p = link->next;
3134b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3135b3a6b972SJed Brown       break;
3136b3a6b972SJed Brown     }
3137b3a6b972SJed Brown   }
3138b3a6b972SJed Brown   PetscFunctionReturn(0);
3139b3a6b972SJed Brown }
3140b3a6b972SJed Brown 
31415dbd56e3SPeter Brune /*@
3142be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
31435dbd56e3SPeter Brune 
31445dbd56e3SPeter Brune    Collective if any hooks are
31455dbd56e3SPeter Brune 
31465dbd56e3SPeter Brune    Input Arguments:
31475dbd56e3SPeter Brune +  fine - finer DM to use as a base
3148be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3149be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
31505dbd56e3SPeter Brune -  coarse - coarer DM to update
31515dbd56e3SPeter Brune 
31525dbd56e3SPeter Brune    Level: developer
31535dbd56e3SPeter Brune 
31545dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
31555dbd56e3SPeter Brune @*/
3156be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
31575dbd56e3SPeter Brune {
31585dbd56e3SPeter Brune   PetscErrorCode      ierr;
3159be081cd6SPeter Brune   DMSubDomainHookLink link;
31605dbd56e3SPeter Brune 
31615dbd56e3SPeter Brune   PetscFunctionBegin;
3162be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
31638865f1eaSKarl Rupp     if (link->restricthook) {
31648865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
31658865f1eaSKarl Rupp     }
31665dbd56e3SPeter Brune   }
31675dbd56e3SPeter Brune   PetscFunctionReturn(0);
31685dbd56e3SPeter Brune }
31695dbd56e3SPeter Brune 
31705fe1f584SPeter Brune /*@
31716a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
31725fe1f584SPeter Brune 
31735fe1f584SPeter Brune     Not Collective
31745fe1f584SPeter Brune 
31755fe1f584SPeter Brune     Input Parameter:
31765fe1f584SPeter Brune .   dm - the DM object
31775fe1f584SPeter Brune 
31785fe1f584SPeter Brune     Output Parameter:
31796a7d9d85SPeter Brune .   level - number of coarsenings
31805fe1f584SPeter Brune 
31815fe1f584SPeter Brune     Level: developer
31825fe1f584SPeter Brune 
31836a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31845fe1f584SPeter Brune 
31855fe1f584SPeter Brune @*/
31865fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
31875fe1f584SPeter Brune {
31885fe1f584SPeter Brune   PetscFunctionBegin;
31895fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3190b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
31915fe1f584SPeter Brune   *level = dm->leveldown;
31925fe1f584SPeter Brune   PetscFunctionReturn(0);
31935fe1f584SPeter Brune }
31945fe1f584SPeter Brune 
31959a64c4a8SMatthew G. Knepley /*@
31969a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31979a64c4a8SMatthew G. Knepley 
31989a64c4a8SMatthew G. Knepley     Not Collective
31999a64c4a8SMatthew G. Knepley 
32009a64c4a8SMatthew G. Knepley     Input Parameters:
32019a64c4a8SMatthew G. Knepley +   dm - the DM object
32029a64c4a8SMatthew G. Knepley -   level - number of coarsenings
32039a64c4a8SMatthew G. Knepley 
32049a64c4a8SMatthew G. Knepley     Level: developer
32059a64c4a8SMatthew G. Knepley 
32069a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
32079a64c4a8SMatthew G. Knepley @*/
32089a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
32099a64c4a8SMatthew G. Knepley {
32109a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
32119a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32129a64c4a8SMatthew G. Knepley   dm->leveldown = level;
32139a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
32149a64c4a8SMatthew G. Knepley }
32159a64c4a8SMatthew G. Knepley 
32165fe1f584SPeter Brune 
32175fe1f584SPeter Brune 
321847c6ae99SBarry Smith /*@C
321947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
322047c6ae99SBarry Smith 
3221d083f849SBarry Smith     Collective on dm
322247c6ae99SBarry Smith 
322347c6ae99SBarry Smith     Input Parameter:
322447c6ae99SBarry Smith +   dm - the DM object
322547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
322647c6ae99SBarry Smith 
322747c6ae99SBarry Smith     Output Parameter:
322847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
322947c6ae99SBarry Smith 
323047c6ae99SBarry Smith     Level: developer
323147c6ae99SBarry Smith 
3232e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
323347c6ae99SBarry Smith 
323447c6ae99SBarry Smith @*/
32357087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
323647c6ae99SBarry Smith {
323747c6ae99SBarry Smith   PetscErrorCode ierr;
323847c6ae99SBarry Smith 
323947c6ae99SBarry Smith   PetscFunctionBegin;
3240171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3241ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
324247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3243b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
324447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
324547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
324647c6ae99SBarry Smith   } else if (dm->ops->refine) {
324747c6ae99SBarry Smith     PetscInt i;
324847c6ae99SBarry Smith 
3249ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
325047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3251ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
325247c6ae99SBarry Smith     }
3253ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
325447c6ae99SBarry Smith   PetscFunctionReturn(0);
325547c6ae99SBarry Smith }
325647c6ae99SBarry Smith 
325747c6ae99SBarry Smith /*@C
325847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
325947c6ae99SBarry Smith 
3260d083f849SBarry Smith     Collective on dm
326147c6ae99SBarry Smith 
326247c6ae99SBarry Smith     Input Parameter:
326347c6ae99SBarry Smith +   dm - the DM object
326447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
326547c6ae99SBarry Smith 
326647c6ae99SBarry Smith     Output Parameter:
326747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
326847c6ae99SBarry Smith 
326947c6ae99SBarry Smith     Level: developer
327047c6ae99SBarry Smith 
3271e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
327247c6ae99SBarry Smith 
327347c6ae99SBarry Smith @*/
32747087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
327547c6ae99SBarry Smith {
327647c6ae99SBarry Smith   PetscErrorCode ierr;
327747c6ae99SBarry Smith 
327847c6ae99SBarry Smith   PetscFunctionBegin;
3279171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3280ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
328147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
328247c6ae99SBarry Smith   PetscValidPointer(dmc,3);
328347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
328447c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
328547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
328647c6ae99SBarry Smith     PetscInt i;
328747c6ae99SBarry Smith 
3288ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
328947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3290ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
329147c6ae99SBarry Smith     }
3292ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
329347c6ae99SBarry Smith   PetscFunctionReturn(0);
329447c6ae99SBarry Smith }
329547c6ae99SBarry Smith 
32961a266240SBarry Smith /*@C
32971a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32981a266240SBarry Smith 
32991a266240SBarry Smith     Not Collective
33001a266240SBarry Smith 
33011a266240SBarry Smith     Input Parameters:
33021a266240SBarry Smith +   dm - the DM object
33031a266240SBarry Smith -   destroy - the destroy function
33041a266240SBarry Smith 
33051a266240SBarry Smith     Level: intermediate
33061a266240SBarry Smith 
3307e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
33081a266240SBarry Smith 
3309f07f9ceaSJed Brown @*/
33101a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
33111a266240SBarry Smith {
33121a266240SBarry Smith   PetscFunctionBegin;
3313171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33141a266240SBarry Smith   dm->ctxdestroy = destroy;
33151a266240SBarry Smith   PetscFunctionReturn(0);
33161a266240SBarry Smith }
33171a266240SBarry Smith 
3318b07ff414SBarry Smith /*@
33191b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
332047c6ae99SBarry Smith 
332147c6ae99SBarry Smith     Not Collective
332247c6ae99SBarry Smith 
332347c6ae99SBarry Smith     Input Parameters:
332447c6ae99SBarry Smith +   dm - the DM object
332547c6ae99SBarry Smith -   ctx - the user context
332647c6ae99SBarry Smith 
332747c6ae99SBarry Smith     Level: intermediate
332847c6ae99SBarry Smith 
3329e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
333047c6ae99SBarry Smith 
333147c6ae99SBarry Smith @*/
33321b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
333347c6ae99SBarry Smith {
333447c6ae99SBarry Smith   PetscFunctionBegin;
3335171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
333647c6ae99SBarry Smith   dm->ctx = ctx;
333747c6ae99SBarry Smith   PetscFunctionReturn(0);
333847c6ae99SBarry Smith }
333947c6ae99SBarry Smith 
334047c6ae99SBarry Smith /*@
33411b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
334247c6ae99SBarry Smith 
334347c6ae99SBarry Smith     Not Collective
334447c6ae99SBarry Smith 
334547c6ae99SBarry Smith     Input Parameter:
334647c6ae99SBarry Smith .   dm - the DM object
334747c6ae99SBarry Smith 
334847c6ae99SBarry Smith     Output Parameter:
334947c6ae99SBarry Smith .   ctx - the user context
335047c6ae99SBarry Smith 
335147c6ae99SBarry Smith     Level: intermediate
335247c6ae99SBarry Smith 
3353e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
335447c6ae99SBarry Smith 
335547c6ae99SBarry Smith @*/
33561b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
335747c6ae99SBarry Smith {
335847c6ae99SBarry Smith   PetscFunctionBegin;
3359171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33601b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
336147c6ae99SBarry Smith   PetscFunctionReturn(0);
336247c6ae99SBarry Smith }
336347c6ae99SBarry Smith 
336408da532bSDmitry Karpeev /*@C
3365df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
336608da532bSDmitry Karpeev 
3367d083f849SBarry Smith     Logically Collective on dm
336808da532bSDmitry Karpeev 
336908da532bSDmitry Karpeev     Input Parameter:
337008da532bSDmitry Karpeev +   dm - the DM object
33710298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
337208da532bSDmitry Karpeev 
337308da532bSDmitry Karpeev     Level: intermediate
337408da532bSDmitry Karpeev 
3375835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
337608da532bSDmitry Karpeev          DMSetJacobian()
337708da532bSDmitry Karpeev 
337808da532bSDmitry Karpeev @*/
337908da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
338008da532bSDmitry Karpeev {
338108da532bSDmitry Karpeev   PetscFunctionBegin;
33825a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
338308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
338408da532bSDmitry Karpeev   PetscFunctionReturn(0);
338508da532bSDmitry Karpeev }
338608da532bSDmitry Karpeev 
338708da532bSDmitry Karpeev /*@
338808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
338908da532bSDmitry Karpeev 
339008da532bSDmitry Karpeev     Not Collective
339108da532bSDmitry Karpeev 
339208da532bSDmitry Karpeev     Input Parameter:
339308da532bSDmitry Karpeev .   dm - the DM object to destroy
339408da532bSDmitry Karpeev 
339508da532bSDmitry Karpeev     Output Parameter:
339608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
339708da532bSDmitry Karpeev 
339808da532bSDmitry Karpeev     Level: developer
339908da532bSDmitry Karpeev 
340074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
340108da532bSDmitry Karpeev 
340208da532bSDmitry Karpeev @*/
340308da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
340408da532bSDmitry Karpeev {
340508da532bSDmitry Karpeev   PetscFunctionBegin;
34065a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3407534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
340808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
340908da532bSDmitry Karpeev   PetscFunctionReturn(0);
341008da532bSDmitry Karpeev }
341108da532bSDmitry Karpeev 
341208da532bSDmitry Karpeev /*@C
341308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
341408da532bSDmitry Karpeev 
3415d083f849SBarry Smith     Logically Collective on dm
341608da532bSDmitry Karpeev 
341708da532bSDmitry Karpeev     Input Parameters:
3418907376e6SBarry Smith .   dm - the DM object
341908da532bSDmitry Karpeev 
342008da532bSDmitry Karpeev     Output parameters:
342108da532bSDmitry Karpeev +   xl - lower bound
342208da532bSDmitry Karpeev -   xu - upper bound
342308da532bSDmitry Karpeev 
3424907376e6SBarry Smith     Level: advanced
3425907376e6SBarry Smith 
342695452b02SPatrick Sanan     Notes:
342795452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
342808da532bSDmitry Karpeev 
342974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
343008da532bSDmitry Karpeev 
343108da532bSDmitry Karpeev @*/
343208da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
343308da532bSDmitry Karpeev {
343408da532bSDmitry Karpeev   PetscErrorCode ierr;
34355fd66863SKarl Rupp 
343608da532bSDmitry Karpeev   PetscFunctionBegin;
34375a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
343808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
34395a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3440b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
344108da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
344208da532bSDmitry Karpeev   PetscFunctionReturn(0);
344308da532bSDmitry Karpeev }
344408da532bSDmitry Karpeev 
3445b0ae01b7SPeter Brune /*@
3446b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3447b0ae01b7SPeter Brune 
3448b0ae01b7SPeter Brune     Not Collective
3449b0ae01b7SPeter Brune 
3450b0ae01b7SPeter Brune     Input Parameter:
3451b0ae01b7SPeter Brune .   dm - the DM object
3452b0ae01b7SPeter Brune 
3453b0ae01b7SPeter Brune     Output Parameter:
3454b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3455b0ae01b7SPeter Brune 
3456b0ae01b7SPeter Brune     Level: developer
3457b0ae01b7SPeter Brune 
34581565f0a7SPatrick Sanan .seealso DMCreateColoring()
3459b0ae01b7SPeter Brune 
3460b0ae01b7SPeter Brune @*/
3461b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3462b0ae01b7SPeter Brune {
3463b0ae01b7SPeter Brune   PetscFunctionBegin;
34645a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3465534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3466b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3467b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3468b0ae01b7SPeter Brune }
3469b0ae01b7SPeter Brune 
34703ad4599aSBarry Smith /*@
34713ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
34723ad4599aSBarry Smith 
34733ad4599aSBarry Smith     Not Collective
34743ad4599aSBarry Smith 
34753ad4599aSBarry Smith     Input Parameter:
34763ad4599aSBarry Smith .   dm - the DM object
34773ad4599aSBarry Smith 
34783ad4599aSBarry Smith     Output Parameter:
34793ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34803ad4599aSBarry Smith 
34813ad4599aSBarry Smith     Level: developer
34823ad4599aSBarry Smith 
34831565f0a7SPatrick Sanan .seealso DMCreateRestriction()
34843ad4599aSBarry Smith 
34853ad4599aSBarry Smith @*/
34863ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
34873ad4599aSBarry Smith {
34883ad4599aSBarry Smith   PetscFunctionBegin;
34895a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3490534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34913ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34923ad4599aSBarry Smith   PetscFunctionReturn(0);
34933ad4599aSBarry Smith }
34943ad4599aSBarry Smith 
3495a7058e45SLawrence Mitchell 
3496a7058e45SLawrence Mitchell /*@
3497a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3498a7058e45SLawrence Mitchell 
3499a7058e45SLawrence Mitchell     Not Collective
3500a7058e45SLawrence Mitchell 
3501a7058e45SLawrence Mitchell     Input Parameter:
3502a7058e45SLawrence Mitchell .   dm - the DM object
3503a7058e45SLawrence Mitchell 
3504a7058e45SLawrence Mitchell     Output Parameter:
3505a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3506a7058e45SLawrence Mitchell 
3507a7058e45SLawrence Mitchell     Level: developer
3508a7058e45SLawrence Mitchell 
35091565f0a7SPatrick Sanan .seealso DMCreateInjection()
3510a7058e45SLawrence Mitchell 
3511a7058e45SLawrence Mitchell @*/
3512a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3513a7058e45SLawrence Mitchell {
35144a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
35155a84ad33SLisandro Dalcin 
3516a7058e45SLawrence Mitchell   PetscFunctionBegin;
35175a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3518534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
35195a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
35204a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
35215a84ad33SLisandro Dalcin   } else {
35225a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
35235a84ad33SLisandro Dalcin   }
3524a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3525a7058e45SLawrence Mitchell }
3526a7058e45SLawrence Mitchell 
35270298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3528264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3529264ace61SBarry Smith 
3530264ace61SBarry Smith /*@C
3531264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3532264ace61SBarry Smith 
3533d083f849SBarry Smith   Collective on dm
3534264ace61SBarry Smith 
3535264ace61SBarry Smith   Input Parameters:
3536264ace61SBarry Smith + dm     - The DM object
3537264ace61SBarry Smith - method - The name of the DM type
3538264ace61SBarry Smith 
3539264ace61SBarry Smith   Options Database Key:
3540264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3541264ace61SBarry Smith 
3542264ace61SBarry Smith   Notes:
3543e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3544264ace61SBarry Smith 
3545264ace61SBarry Smith   Level: intermediate
3546264ace61SBarry Smith 
3547264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3548264ace61SBarry Smith @*/
354919fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3550264ace61SBarry Smith {
3551264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3552264ace61SBarry Smith   PetscBool      match;
3553264ace61SBarry Smith   PetscErrorCode ierr;
3554264ace61SBarry Smith 
3555264ace61SBarry Smith   PetscFunctionBegin;
3556264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3557251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3558264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3559264ace61SBarry Smith 
35600f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35611c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3562ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3563264ace61SBarry Smith 
3564264ace61SBarry Smith   if (dm->ops->destroy) {
3565264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3566264ace61SBarry Smith   }
3567d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3568264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3569d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3570264ace61SBarry Smith   PetscFunctionReturn(0);
3571264ace61SBarry Smith }
3572264ace61SBarry Smith 
3573264ace61SBarry Smith /*@C
3574264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3575264ace61SBarry Smith 
3576264ace61SBarry Smith   Not Collective
3577264ace61SBarry Smith 
3578264ace61SBarry Smith   Input Parameter:
3579264ace61SBarry Smith . dm  - The DM
3580264ace61SBarry Smith 
3581264ace61SBarry Smith   Output Parameter:
3582264ace61SBarry Smith . type - The DM type name
3583264ace61SBarry Smith 
3584264ace61SBarry Smith   Level: intermediate
3585264ace61SBarry Smith 
3586264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3587264ace61SBarry Smith @*/
358819fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3589264ace61SBarry Smith {
3590264ace61SBarry Smith   PetscErrorCode ierr;
3591264ace61SBarry Smith 
3592264ace61SBarry Smith   PetscFunctionBegin;
3593264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3594c959eef4SJed Brown   PetscValidPointer(type,2);
3595607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3596264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3597264ace61SBarry Smith   PetscFunctionReturn(0);
3598264ace61SBarry Smith }
3599264ace61SBarry Smith 
360067a56275SMatthew G Knepley /*@C
360167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
360267a56275SMatthew G Knepley 
3603d083f849SBarry Smith   Collective on dm
360467a56275SMatthew G Knepley 
360567a56275SMatthew G Knepley   Input Parameters:
360667a56275SMatthew G Knepley + dm - the DM
360767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
360867a56275SMatthew G Knepley 
360967a56275SMatthew G Knepley   Output Parameter:
361067a56275SMatthew G Knepley . M - pointer to new DM
361167a56275SMatthew G Knepley 
361267a56275SMatthew G Knepley   Notes:
361367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
361467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
361567a56275SMatthew G Knepley   of the input DM.
361667a56275SMatthew G Knepley 
361767a56275SMatthew G Knepley   Level: intermediate
361867a56275SMatthew G Knepley 
361967a56275SMatthew G Knepley .seealso: DMCreate()
362067a56275SMatthew G Knepley @*/
362119fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
362267a56275SMatthew G Knepley {
362367a56275SMatthew G Knepley   DM             B;
362467a56275SMatthew G Knepley   char           convname[256];
3625c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
362667a56275SMatthew G Knepley   PetscErrorCode ierr;
362767a56275SMatthew G Knepley 
362867a56275SMatthew G Knepley   PetscFunctionBegin;
362967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
363067a56275SMatthew G Knepley   PetscValidType(dm,1);
363167a56275SMatthew G Knepley   PetscValidPointer(M,3);
3632251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3633c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3634c067b6caSMatthew G. Knepley   if (sametype) {
3635c067b6caSMatthew G. Knepley     *M   = dm;
3636c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3637c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3638c067b6caSMatthew G. Knepley   } else {
36390298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
364067a56275SMatthew G Knepley 
364167a56275SMatthew G Knepley     /*
364267a56275SMatthew G Knepley        Order of precedence:
364367a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
364467a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
364567a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
364667a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
364767a56275SMatthew G Knepley        5) Use a really basic converter.
364867a56275SMatthew G Knepley     */
364967a56275SMatthew G Knepley 
365067a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3651a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3652a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3653a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3654a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3655a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36560005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
365767a56275SMatthew G Knepley     if (conv) goto foundconv;
365867a56275SMatthew G Knepley 
365967a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
366082f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
366167a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3662a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3663a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3664a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3665a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3666a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36670005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
366867a56275SMatthew G Knepley     if (conv) {
3669fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
367067a56275SMatthew G Knepley       goto foundconv;
367167a56275SMatthew G Knepley     }
367267a56275SMatthew G Knepley 
367367a56275SMatthew G Knepley #if 0
367467a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
367567a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3676fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
367767a56275SMatthew G Knepley     if (conv) goto foundconv;
367867a56275SMatthew G Knepley 
367967a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
368067a56275SMatthew G Knepley     if (dm->ops->convert) {
368167a56275SMatthew G Knepley       conv = dm->ops->convert;
368267a56275SMatthew G Knepley     }
368367a56275SMatthew G Knepley     if (conv) goto foundconv;
368467a56275SMatthew G Knepley #endif
368567a56275SMatthew G Knepley 
368667a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
368782f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
368867a56275SMatthew G Knepley 
368967a56275SMatthew G Knepley foundconv:
369067a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
369167a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
369212fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
369390b157c4SStefano Zampini     {
369490b157c4SStefano Zampini       PetscBool             isper;
369512fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
369612fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
369790b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
369890b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
369912fa691eSMatthew G. Knepley     }
370067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
370167a56275SMatthew G Knepley   }
370267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
370367a56275SMatthew G Knepley   PetscFunctionReturn(0);
370467a56275SMatthew G Knepley }
3705264ace61SBarry Smith 
3706264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3707264ace61SBarry Smith 
3708264ace61SBarry Smith /*@C
37091c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
37101c84c290SBarry Smith 
37111c84c290SBarry Smith   Not Collective
37121c84c290SBarry Smith 
37131c84c290SBarry Smith   Input Parameters:
37141c84c290SBarry Smith + name        - The name of a new user-defined creation routine
37151c84c290SBarry Smith - create_func - The creation routine itself
37161c84c290SBarry Smith 
37171c84c290SBarry Smith   Notes:
37181c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
37191c84c290SBarry Smith 
37201c84c290SBarry Smith 
37211c84c290SBarry Smith   Sample usage:
37221c84c290SBarry Smith .vb
3723bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
37241c84c290SBarry Smith .ve
37251c84c290SBarry Smith 
37261c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
37271c84c290SBarry Smith .vb
37281c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
37291c84c290SBarry Smith     DMSetType(DM,"my_da");
37301c84c290SBarry Smith .ve
37311c84c290SBarry Smith    or at runtime via the option
37321c84c290SBarry Smith .vb
37331c84c290SBarry Smith     -da_type my_da
37341c84c290SBarry Smith .ve
3735264ace61SBarry Smith 
3736264ace61SBarry Smith   Level: advanced
37371c84c290SBarry Smith 
3738bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
37391c84c290SBarry Smith 
3740264ace61SBarry Smith @*/
3741bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3742264ace61SBarry Smith {
3743264ace61SBarry Smith   PetscErrorCode ierr;
3744264ace61SBarry Smith 
3745264ace61SBarry Smith   PetscFunctionBegin;
37461d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3747a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3748264ace61SBarry Smith   PetscFunctionReturn(0);
3749264ace61SBarry Smith }
3750264ace61SBarry Smith 
3751b859378eSBarry Smith /*@C
375255849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3753b859378eSBarry Smith 
3754d083f849SBarry Smith   Collective on viewer
3755b859378eSBarry Smith 
3756b859378eSBarry Smith   Input Parameters:
3757b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3758b859378eSBarry Smith            some related function before a call to DMLoad().
3759b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3760b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3761b859378eSBarry Smith 
3762b859378eSBarry Smith    Level: intermediate
3763b859378eSBarry Smith 
3764b859378eSBarry Smith   Notes:
376555849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3766b859378eSBarry Smith 
3767b859378eSBarry Smith   Notes for advanced users:
3768b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3769b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3770b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3771b859378eSBarry Smith   format is
3772b859378eSBarry Smith .vb
3773b859378eSBarry Smith      has not yet been determined
3774b859378eSBarry Smith .ve
3775b859378eSBarry Smith 
3776b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3777b859378eSBarry Smith @*/
3778b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3779b859378eSBarry Smith {
37809331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3781b859378eSBarry Smith   PetscErrorCode ierr;
3782b859378eSBarry Smith 
3783b859378eSBarry Smith   PetscFunctionBegin;
3784b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3785b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3786fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
378732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37889331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
378958cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
37909331c7a4SMatthew G. Knepley   if (isbinary) {
37919331c7a4SMatthew G. Knepley     PetscInt classid;
37929331c7a4SMatthew G. Knepley     char     type[256];
3793b859378eSBarry Smith 
3794060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37959200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3796060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
379732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37989331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37999331c7a4SMatthew G. Knepley   } else if (ishdf5) {
38009331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
38019331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
380258cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3803b859378eSBarry Smith   PetscFunctionReturn(0);
3804b859378eSBarry Smith }
3805b859378eSBarry Smith 
3806b2e4378dSMatthew G. Knepley /*@
3807b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3808b2e4378dSMatthew G. Knepley 
3809b2e4378dSMatthew G. Knepley   Not collective
3810b2e4378dSMatthew G. Knepley 
3811b2e4378dSMatthew G. Knepley   Input Parameter:
3812b2e4378dSMatthew G. Knepley . dm - the DM
3813b2e4378dSMatthew G. Knepley 
3814b2e4378dSMatthew G. Knepley   Output Parameters:
3815b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3816b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3817b2e4378dSMatthew G. Knepley 
3818b2e4378dSMatthew G. Knepley   Level: beginner
3819b2e4378dSMatthew G. Knepley 
3820b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3821b2e4378dSMatthew G. Knepley 
3822b2e4378dSMatthew G. Knepley 
3823b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3824b2e4378dSMatthew G. Knepley @*/
3825b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3826b2e4378dSMatthew G. Knepley {
3827b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3828b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3829b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3830b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3831b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3832b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3833b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3834b2e4378dSMatthew G. Knepley 
3835b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3836b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3837b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3838b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3839b2e4378dSMatthew G. Knepley   if (coords) {
3840b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3841b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3842b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3843b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3844b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3845b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3846b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3847b2e4378dSMatthew G. Knepley       }
3848b2e4378dSMatthew G. Knepley     }
3849b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3850b2e4378dSMatthew G. Knepley   } else {
3851b2e4378dSMatthew G. Knepley     PetscBool isda;
3852b2e4378dSMatthew G. Knepley 
3853b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3854b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3855b2e4378dSMatthew G. Knepley   }
3856b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3857b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3858b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3859b2e4378dSMatthew G. Knepley }
3860b2e4378dSMatthew G. Knepley 
3861b2e4378dSMatthew G. Knepley /*@
3862b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3863b2e4378dSMatthew G. Knepley 
3864b2e4378dSMatthew G. Knepley   Collective
3865b2e4378dSMatthew G. Knepley 
3866b2e4378dSMatthew G. Knepley   Input Parameter:
3867b2e4378dSMatthew G. Knepley . dm - the DM
3868b2e4378dSMatthew G. Knepley 
3869b2e4378dSMatthew G. Knepley   Output Parameters:
3870b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3871b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3872b2e4378dSMatthew G. Knepley 
3873b2e4378dSMatthew G. Knepley   Level: beginner
3874b2e4378dSMatthew G. Knepley 
3875b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3876b2e4378dSMatthew G. Knepley @*/
3877b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3878b2e4378dSMatthew G. Knepley {
3879b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38806ce308c4SMatthew G. Knepley   PetscInt       cdim;
38816ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3882b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3883b2e4378dSMatthew G. Knepley 
3884b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3885b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3886b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3887b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3888b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3889b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3890b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3891b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3892b2e4378dSMatthew G. Knepley }
3893b2e4378dSMatthew G. Knepley 
38947da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38957da65231SMatthew G Knepley 
3896a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3897a6dfd86eSKarl Rupp {
38981d47ebbbSSatish Balay   PetscInt       f;
38991b30c384SMatthew G Knepley   PetscErrorCode ierr;
39001b30c384SMatthew G Knepley 
39017da65231SMatthew G Knepley   PetscFunctionBegin;
390274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39031d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
390457622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
39057da65231SMatthew G Knepley   }
39067da65231SMatthew G Knepley   PetscFunctionReturn(0);
39077da65231SMatthew G Knepley }
39087da65231SMatthew G Knepley 
3909a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3910a6dfd86eSKarl Rupp {
39111b30c384SMatthew G Knepley   PetscInt       f, g;
39127da65231SMatthew G Knepley   PetscErrorCode ierr;
39137da65231SMatthew G Knepley 
39147da65231SMatthew G Knepley   PetscFunctionBegin;
391574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39161d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
391774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
39181d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3919e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
39207da65231SMatthew G Knepley     }
392174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
39227da65231SMatthew G Knepley   }
39237da65231SMatthew G Knepley   PetscFunctionReturn(0);
39247da65231SMatthew G Knepley }
3925e7c4fc90SDmitry Karpeev 
39266113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3927e759306cSMatthew G. Knepley {
39280c5b8624SToby Isaac   PetscInt          localSize, bs;
39290c5b8624SToby Isaac   PetscMPIInt       size;
39300c5b8624SToby Isaac   Vec               x, xglob;
39310c5b8624SToby Isaac   const PetscScalar *xarray;
3932e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3933e759306cSMatthew G. Knepley 
3934e759306cSMatthew G. Knepley   PetscFunctionBegin;
39359852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3936e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3937e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
39386113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
39390c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
39400c5b8624SToby Isaac   if (size > 1) {
39410c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
39420c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
39430c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
39440c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
39450c5b8624SToby Isaac   } else {
39460c5b8624SToby Isaac     xglob = x;
39470c5b8624SToby Isaac   }
39480c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
39490c5b8624SToby Isaac   if (size > 1) {
39500c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
39510c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
39520c5b8624SToby Isaac   }
3953e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3954e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3955e759306cSMatthew G. Knepley }
3956e759306cSMatthew G. Knepley 
395788ed4aceSMatthew G Knepley /*@
39581bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3959061576a5SJed Brown 
3960061576a5SJed Brown   Input Parameter:
3961061576a5SJed Brown . dm - The DM
3962061576a5SJed Brown 
3963061576a5SJed Brown   Output Parameter:
3964061576a5SJed Brown . section - The PetscSection
3965061576a5SJed Brown 
3966061576a5SJed Brown   Options Database Keys:
3967061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3968061576a5SJed Brown 
3969061576a5SJed Brown   Level: advanced
3970061576a5SJed Brown 
3971061576a5SJed Brown   Notes:
3972061576a5SJed Brown   Use DMGetLocalSection() in new code.
3973061576a5SJed Brown 
3974061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3975061576a5SJed Brown 
3976061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3977061576a5SJed Brown @*/
3978061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3979061576a5SJed Brown {
3980061576a5SJed Brown   PetscErrorCode ierr;
3981061576a5SJed Brown 
3982061576a5SJed Brown   PetscFunctionBegin;
3983061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3984061576a5SJed Brown   PetscFunctionReturn(0);
3985061576a5SJed Brown }
3986061576a5SJed Brown 
3987061576a5SJed Brown /*@
3988061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
398988ed4aceSMatthew G Knepley 
399088ed4aceSMatthew G Knepley   Input Parameter:
399188ed4aceSMatthew G Knepley . dm - The DM
399288ed4aceSMatthew G Knepley 
399388ed4aceSMatthew G Knepley   Output Parameter:
399488ed4aceSMatthew G Knepley . section - The PetscSection
399588ed4aceSMatthew G Knepley 
3996e5893cccSMatthew G. Knepley   Options Database Keys:
3997e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3998e5893cccSMatthew G. Knepley 
399988ed4aceSMatthew G Knepley   Level: intermediate
400088ed4aceSMatthew G Knepley 
400188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
400288ed4aceSMatthew G Knepley 
4003061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
400488ed4aceSMatthew G Knepley @*/
4005061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
40060adebc6cSBarry Smith {
4007fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4008fd59a867SMatthew G. Knepley 
400988ed4aceSMatthew G Knepley   PetscFunctionBegin;
401088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
401188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
40121bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4013e5e52638SMatthew G. Knepley     PetscInt d;
4014e5e52638SMatthew G. Knepley 
4015e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
40161bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
40171bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
40182f0f8703SMatthew G. Knepley   }
40191bb6d2a8SBarry Smith   *section = dm->localSection;
402088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
402188ed4aceSMatthew G Knepley }
402288ed4aceSMatthew G Knepley 
402388ed4aceSMatthew G Knepley /*@
40241bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4025061576a5SJed Brown 
4026061576a5SJed Brown   Input Parameters:
4027061576a5SJed Brown + dm - The DM
4028061576a5SJed Brown - section - The PetscSection
4029061576a5SJed Brown 
4030061576a5SJed Brown   Level: advanced
4031061576a5SJed Brown 
4032061576a5SJed Brown   Notes:
4033061576a5SJed Brown   Use DMSetLocalSection() in new code.
4034061576a5SJed Brown 
4035061576a5SJed Brown   Any existing Section will be destroyed
4036061576a5SJed Brown 
4037061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4038061576a5SJed Brown @*/
4039061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4040061576a5SJed Brown {
4041061576a5SJed Brown   PetscErrorCode ierr;
4042061576a5SJed Brown 
4043061576a5SJed Brown   PetscFunctionBegin;
4044061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4045061576a5SJed Brown   PetscFunctionReturn(0);
4046061576a5SJed Brown }
4047061576a5SJed Brown 
4048061576a5SJed Brown /*@
4049061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
405088ed4aceSMatthew G Knepley 
405188ed4aceSMatthew G Knepley   Input Parameters:
405288ed4aceSMatthew G Knepley + dm - The DM
405388ed4aceSMatthew G Knepley - section - The PetscSection
405488ed4aceSMatthew G Knepley 
405588ed4aceSMatthew G Knepley   Level: intermediate
405688ed4aceSMatthew G Knepley 
405788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
405888ed4aceSMatthew G Knepley 
4059061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
406088ed4aceSMatthew G Knepley @*/
4061061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
40620adebc6cSBarry Smith {
4063c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4064af122d2aSMatthew G Knepley   PetscInt       f;
406588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
406688ed4aceSMatthew G Knepley 
406788ed4aceSMatthew G Knepley   PetscFunctionBegin;
406888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4069b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40701d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40711bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40721bb6d2a8SBarry Smith   dm->localSection = section;
40731bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4074af122d2aSMatthew G Knepley   if (numFields) {
4075af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4076af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40770f21e855SMatthew G. Knepley       PetscObject disc;
4078af122d2aSMatthew G Knepley       const char *name;
4079af122d2aSMatthew G Knepley 
40801bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
408144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40820f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4083af122d2aSMatthew G Knepley     }
4084af122d2aSMatthew G Knepley   }
4085e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40861bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
408788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
408888ed4aceSMatthew G Knepley }
408988ed4aceSMatthew G Knepley 
40909435951eSToby Isaac /*@
4091b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40929435951eSToby Isaac 
4093e228b242SToby Isaac   not collective
4094e228b242SToby Isaac 
40959435951eSToby Isaac   Input Parameter:
40969435951eSToby Isaac . dm - The DM
40979435951eSToby Isaac 
40989435951eSToby Isaac   Output Parameter:
40999435951eSToby 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.
41009435951eSToby Isaac - 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.
41019435951eSToby Isaac 
41029435951eSToby Isaac   Level: advanced
41039435951eSToby Isaac 
41049435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
41059435951eSToby Isaac 
41069435951eSToby Isaac .seealso: DMSetDefaultConstraints()
41079435951eSToby Isaac @*/
41089435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
41099435951eSToby Isaac {
41109435951eSToby Isaac   PetscErrorCode ierr;
41119435951eSToby Isaac 
41129435951eSToby Isaac   PetscFunctionBegin;
41139435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41149435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
411545a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
411645a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
41179435951eSToby Isaac   PetscFunctionReturn(0);
41189435951eSToby Isaac }
41199435951eSToby Isaac 
41209435951eSToby Isaac /*@
4121b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
41229435951eSToby Isaac 
41239435951eSToby Isaac   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, l[s[i]] = c[i], where the scatter s is defined by the PetscSection returned by DMGetDefaultConstraintMatrix().
41249435951eSToby Isaac 
41259435951eSToby Isaac   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.
41269435951eSToby Isaac 
4127e228b242SToby Isaac   collective on dm
4128e228b242SToby Isaac 
41299435951eSToby Isaac   Input Parameters:
41309435951eSToby Isaac + dm - The DM
4131e228b242SToby Isaac + 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).
4132e228b242SToby Isaac - 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).
41339435951eSToby Isaac 
41349435951eSToby Isaac   Level: advanced
41359435951eSToby Isaac 
41369435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
41379435951eSToby Isaac 
41389435951eSToby Isaac .seealso: DMGetDefaultConstraints()
41399435951eSToby Isaac @*/
41409435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
41419435951eSToby Isaac {
4142e228b242SToby Isaac   PetscMPIInt result;
41439435951eSToby Isaac   PetscErrorCode ierr;
41449435951eSToby Isaac 
41459435951eSToby Isaac   PetscFunctionBegin;
41469435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4147e228b242SToby Isaac   if (section) {
4148e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4149e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4150f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4151e228b242SToby Isaac   }
4152e228b242SToby Isaac   if (mat) {
4153e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4154e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4155f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4156e228b242SToby Isaac   }
41579435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41589435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
41599435951eSToby Isaac   dm->defaultConstraintSection = section;
41609435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
41619435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
41629435951eSToby Isaac   dm->defaultConstraintMat = mat;
41639435951eSToby Isaac   PetscFunctionReturn(0);
41649435951eSToby Isaac }
41659435951eSToby Isaac 
4166497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4167507e4973SMatthew G. Knepley /*
4168507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4169507e4973SMatthew G. Knepley 
4170507e4973SMatthew G. Knepley   Input Parameters:
4171507e4973SMatthew G. Knepley + dm - The DM
4172507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4173507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4174507e4973SMatthew G. Knepley 
4175507e4973SMatthew G. Knepley   Level: intermediate
4176507e4973SMatthew G. Knepley 
41771bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4178507e4973SMatthew G. Knepley */
4179f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4180507e4973SMatthew G. Knepley {
4181507e4973SMatthew G. Knepley   MPI_Comm        comm;
4182507e4973SMatthew G. Knepley   PetscLayout     layout;
4183507e4973SMatthew G. Knepley   const PetscInt *ranges;
4184507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4185507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4186507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4187507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4188507e4973SMatthew G. Knepley 
4189507e4973SMatthew G. Knepley   PetscFunctionBegin;
4190507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4191507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4192507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4193507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4194507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4195507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4196507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4197507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4198507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4199507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4200507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4201507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4202f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4203507e4973SMatthew G. Knepley 
4204507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4205507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4206507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4207507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4208507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4209507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4210507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4211507e4973SMatthew G. Knepley     if ((gdof < 0 ? -(gdof+1) : gdof) != dof) {ierr = PetscSynchronizedPrintf(comm, "[%d]Global dof %d for point %d not equal to local dof %d\n", rank, gdof, p, dof);CHKERRQ(ierr); valid = PETSC_FALSE;}
4212507e4973SMatthew G. Knepley     if (gcdof && (gcdof != cdof)) {ierr = PetscSynchronizedPrintf(comm, "[%d]Global constraints %d for point %d not equal to local constraints %d\n", rank, gcdof, p, cdof);CHKERRQ(ierr); valid = PETSC_FALSE;}
4213507e4973SMatthew G. Knepley     if (gdof < 0) {
4214507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4215507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4216507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4217507e4973SMatthew G. Knepley 
4218507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4219507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4220507e4973SMatthew G. Knepley         if ((r < 0) || (r >= size)) {ierr = PetscSynchronizedPrintf(comm, "[%d]Point %d mapped to invalid process %d (%d, %d)\n", rank, p, r, gdof, goff);CHKERRQ(ierr); valid = PETSC_FALSE;break;}
4221507e4973SMatthew G. Knepley       }
4222507e4973SMatthew G. Knepley     }
4223507e4973SMatthew G. Knepley   }
4224507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4225507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4226b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4227507e4973SMatthew G. Knepley   if (!gvalid) {
4228507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4229507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4230507e4973SMatthew G. Knepley   }
4231507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4232507e4973SMatthew G. Knepley }
4233f741bcd2SMatthew G. Knepley #endif
4234507e4973SMatthew G. Knepley 
423588ed4aceSMatthew G Knepley /*@
4236e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
423788ed4aceSMatthew G Knepley 
4238d083f849SBarry Smith   Collective on dm
42398b1ab98fSJed Brown 
424088ed4aceSMatthew G Knepley   Input Parameter:
424188ed4aceSMatthew G Knepley . dm - The DM
424288ed4aceSMatthew G Knepley 
424388ed4aceSMatthew G Knepley   Output Parameter:
424488ed4aceSMatthew G Knepley . section - The PetscSection
424588ed4aceSMatthew G Knepley 
424688ed4aceSMatthew G Knepley   Level: intermediate
424788ed4aceSMatthew G Knepley 
424888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
424988ed4aceSMatthew G Knepley 
425092fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
425188ed4aceSMatthew G Knepley @*/
4252e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
42530adebc6cSBarry Smith {
425488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
425588ed4aceSMatthew G Knepley 
425688ed4aceSMatthew G Knepley   PetscFunctionBegin;
425788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42591bb6d2a8SBarry Smith   if (!dm->globalSection) {
4260fd59a867SMatthew G. Knepley     PetscSection s;
4261fd59a867SMatthew G. Knepley 
426292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4263fd59a867SMatthew G. Knepley     if (!s)  SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
426433907cc2SStefano Zampini     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
42651bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4266cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42671bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42681bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
426988ed4aceSMatthew G Knepley   }
42701bb6d2a8SBarry Smith   *section = dm->globalSection;
427188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
427288ed4aceSMatthew G Knepley }
427388ed4aceSMatthew G Knepley 
4274b21d0597SMatthew G Knepley /*@
4275e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4276b21d0597SMatthew G Knepley 
4277b21d0597SMatthew G Knepley   Input Parameters:
4278b21d0597SMatthew G Knepley + dm - The DM
42795080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4280b21d0597SMatthew G Knepley 
4281b21d0597SMatthew G Knepley   Level: intermediate
4282b21d0597SMatthew G Knepley 
4283b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4284b21d0597SMatthew G Knepley 
428592fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4286b21d0597SMatthew G Knepley @*/
4287e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42880adebc6cSBarry Smith {
4289b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4290b21d0597SMatthew G Knepley 
4291b21d0597SMatthew G Knepley   PetscFunctionBegin;
4292b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42935080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42941d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42951bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42961bb6d2a8SBarry Smith   dm->globalSection = section;
4297497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42981bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4299507e4973SMatthew G. Knepley #endif
4300b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4301b21d0597SMatthew G Knepley }
4302b21d0597SMatthew G Knepley 
430388ed4aceSMatthew G Knepley /*@
43041bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
430588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
430688ed4aceSMatthew G Knepley 
430788ed4aceSMatthew G Knepley   Input Parameter:
430888ed4aceSMatthew G Knepley . dm - The DM
430988ed4aceSMatthew G Knepley 
431088ed4aceSMatthew G Knepley   Output Parameter:
431188ed4aceSMatthew G Knepley . sf - The PetscSF
431288ed4aceSMatthew G Knepley 
431388ed4aceSMatthew G Knepley   Level: intermediate
431488ed4aceSMatthew G Knepley 
431588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
431688ed4aceSMatthew G Knepley 
43171bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
431888ed4aceSMatthew G Knepley @*/
43191bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
43200adebc6cSBarry Smith {
432188ed4aceSMatthew G Knepley   PetscInt       nroots;
432288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
432388ed4aceSMatthew G Knepley 
432488ed4aceSMatthew G Knepley   PetscFunctionBegin;
432588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
432688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
43271bb6d2a8SBarry Smith   if (!dm->sectionSF) {
43281bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
432933907cc2SStefano Zampini   }
43301bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
433188ed4aceSMatthew G Knepley   if (nroots < 0) {
433288ed4aceSMatthew G Knepley     PetscSection section, gSection;
433388ed4aceSMatthew G Knepley 
433492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
433531ea6d37SMatthew G Knepley     if (section) {
4336e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
43371bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
433831ea6d37SMatthew G Knepley     } else {
43390298fd71SBarry Smith       *sf = NULL;
434031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
434131ea6d37SMatthew G Knepley     }
434288ed4aceSMatthew G Knepley   }
43431bb6d2a8SBarry Smith   *sf = dm->sectionSF;
434488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
434588ed4aceSMatthew G Knepley }
434688ed4aceSMatthew G Knepley 
434788ed4aceSMatthew G Knepley /*@
43481bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
434988ed4aceSMatthew G Knepley 
435088ed4aceSMatthew G Knepley   Input Parameters:
435188ed4aceSMatthew G Knepley + dm - The DM
435288ed4aceSMatthew G Knepley - sf - The PetscSF
435388ed4aceSMatthew G Knepley 
435488ed4aceSMatthew G Knepley   Level: intermediate
435588ed4aceSMatthew G Knepley 
435688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
435788ed4aceSMatthew G Knepley 
43581bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
435988ed4aceSMatthew G Knepley @*/
43601bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
43610adebc6cSBarry Smith {
436288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
436388ed4aceSMatthew G Knepley 
436488ed4aceSMatthew G Knepley   PetscFunctionBegin;
436588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4366b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
436733907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43681bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43691bb6d2a8SBarry Smith   dm->sectionSF = sf;
437088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
437188ed4aceSMatthew G Knepley }
437288ed4aceSMatthew G Knepley 
437388ed4aceSMatthew G Knepley /*@C
43741bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
437588ed4aceSMatthew G Knepley   describing the data layout.
437688ed4aceSMatthew G Knepley 
437788ed4aceSMatthew G Knepley   Input Parameters:
437888ed4aceSMatthew G Knepley + dm - The DM
437988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
438088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
438188ed4aceSMatthew G Knepley 
43821bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
438388ed4aceSMatthew G Knepley 
43841bb6d2a8SBarry Smith   Level: developer
43851bb6d2a8SBarry Smith 
43861bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43871bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43881bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43891bb6d2a8SBarry Smith 
43901bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
439188ed4aceSMatthew G Knepley @*/
43921bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
439388ed4aceSMatthew G Knepley {
439482f516ccSBarry Smith   MPI_Comm       comm;
439588ed4aceSMatthew G Knepley   PetscLayout    layout;
439688ed4aceSMatthew G Knepley   const PetscInt *ranges;
439788ed4aceSMatthew G Knepley   PetscInt       *local;
439888ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4399ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
440088ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
440188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
440288ed4aceSMatthew G Knepley 
440388ed4aceSMatthew G Knepley   PetscFunctionBegin;
440488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4405367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
440688ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
440788ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
440888ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
440988ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
441088ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
441188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
441288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
441388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
441488ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4415ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
44166636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
441788ed4aceSMatthew G Knepley 
44186636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44196636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4420235fbf56SMatthew G. Knepley     if (gcdof > (gdof < 0 ? -(gdof+1) : gdof)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d has %d constraints > %d dof", p, gcdof, (gdof < 0 ? -(gdof+1) : gdof));
44216636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
442288ed4aceSMatthew G Knepley   }
4423785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4424785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
442588ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
44261f588964SMatthew G Knepley     const PetscInt *cind;
44276636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
442888ed4aceSMatthew G Knepley 
442988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
443088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
443188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
443288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
443388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44346636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
443588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
44366636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
44376636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
44386636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4439057b4bcdSMatthew G Knepley       if (gsize != dof) SETERRQ4(comm, PETSC_ERR_ARG_WRONG, "Global dof %d for point %d is neither the constrained size %d, nor the unconstrained %d", gsize, p, dof-cdof, dof);
44406636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
44416636e97aSMatthew G Knepley     }
444288ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
444388ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
444488ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
444588ed4aceSMatthew G Knepley     }
444688ed4aceSMatthew G Knepley     if (gdof < 0) {
44476636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
444888ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
444988ed4aceSMatthew G Knepley 
445005376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
445131d3f06eSJed Brown         if (r < 0) r = -(r+2);
445205376888SMatthew G. Knepley         if ((r < 0) || (r >= size)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d mapped to invalid process %d (%d, %d)", p, r, gdof, goff);
445388ed4aceSMatthew G Knepley         remote[l].rank  = r;
445488ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
445588ed4aceSMatthew G Knepley       }
445688ed4aceSMatthew G Knepley     } else {
44576636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
445888ed4aceSMatthew G Knepley         remote[l].rank  = rank;
445988ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
446088ed4aceSMatthew G Knepley       }
446188ed4aceSMatthew G Knepley     }
446288ed4aceSMatthew G Knepley   }
44636636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
446488ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44651bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
446688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
446788ed4aceSMatthew G Knepley }
4468af122d2aSMatthew G Knepley 
4469b21d0597SMatthew G Knepley /*@
4470b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4471b21d0597SMatthew G Knepley 
4472b21d0597SMatthew G Knepley   Input Parameter:
4473b21d0597SMatthew G Knepley . dm - The DM
4474b21d0597SMatthew G Knepley 
4475b21d0597SMatthew G Knepley   Output Parameter:
4476b21d0597SMatthew G Knepley . sf - The PetscSF
4477b21d0597SMatthew G Knepley 
4478b21d0597SMatthew G Knepley   Level: intermediate
4479b21d0597SMatthew G Knepley 
4480b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4481b21d0597SMatthew G Knepley 
44821bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4483b21d0597SMatthew G Knepley @*/
44840adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44850adebc6cSBarry Smith {
4486b21d0597SMatthew G Knepley   PetscFunctionBegin;
4487b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4488b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4489b21d0597SMatthew G Knepley   *sf = dm->sf;
4490b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4491b21d0597SMatthew G Knepley }
4492b21d0597SMatthew G Knepley 
4493057b4bcdSMatthew G Knepley /*@
4494057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4495057b4bcdSMatthew G Knepley 
4496057b4bcdSMatthew G Knepley   Input Parameters:
4497057b4bcdSMatthew G Knepley + dm - The DM
4498057b4bcdSMatthew G Knepley - sf - The PetscSF
4499057b4bcdSMatthew G Knepley 
4500057b4bcdSMatthew G Knepley   Level: intermediate
4501057b4bcdSMatthew G Knepley 
45021bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4503057b4bcdSMatthew G Knepley @*/
45040adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45050adebc6cSBarry Smith {
4506057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4507057b4bcdSMatthew G Knepley 
4508057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4509057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4510b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4511057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
451233907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4513057b4bcdSMatthew G Knepley   dm->sf = sf;
4514057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4515057b4bcdSMatthew G Knepley }
4516057b4bcdSMatthew G Knepley 
451734aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
451834aa8a36SMatthew G. Knepley {
451934aa8a36SMatthew G. Knepley   PetscClassId   id;
452034aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
452134aa8a36SMatthew G. Knepley 
452234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
452334aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
452434aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
452534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
452634aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
452734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
452817c1d62eSMatthew G. Knepley   } else {
452917c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
453034aa8a36SMatthew G. Knepley   }
453134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
453234aa8a36SMatthew G. Knepley }
453334aa8a36SMatthew G. Knepley 
453444a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
453544a7f3ddSMatthew G. Knepley {
453644a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
453744a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
453844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
453944a7f3ddSMatthew G. Knepley 
454044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
454144a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
454244a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
454344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
454444a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
454544a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
454644a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
454744a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
454844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
454944a7f3ddSMatthew G. Knepley }
455044a7f3ddSMatthew G. Knepley 
455144a7f3ddSMatthew G. Knepley /*@
455244a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
455344a7f3ddSMatthew G. Knepley 
4554d083f849SBarry Smith   Logically collective on dm
455544a7f3ddSMatthew G. Knepley 
455644a7f3ddSMatthew G. Knepley   Input Parameter:
455744a7f3ddSMatthew G. Knepley . dm - The DM
455844a7f3ddSMatthew G. Knepley 
455944a7f3ddSMatthew G. Knepley   Level: intermediate
456044a7f3ddSMatthew G. Knepley 
456144a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
456244a7f3ddSMatthew G. Knepley @*/
456344a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
456444a7f3ddSMatthew G. Knepley {
456544a7f3ddSMatthew G. Knepley   PetscInt       f;
456644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
456744a7f3ddSMatthew G. Knepley 
456844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
456944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457044a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
457144a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
457244a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
457344a7f3ddSMatthew G. Knepley   }
457444a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
457544a7f3ddSMatthew G. Knepley   dm->fields = NULL;
457644a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
457744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
457844a7f3ddSMatthew G. Knepley }
457944a7f3ddSMatthew G. Knepley 
4580689b5837SMatthew G. Knepley /*@
4581689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4582689b5837SMatthew G. Knepley 
4583689b5837SMatthew G. Knepley   Not collective
4584689b5837SMatthew G. Knepley 
4585689b5837SMatthew G. Knepley   Input Parameter:
4586689b5837SMatthew G. Knepley . dm - The DM
4587689b5837SMatthew G. Knepley 
4588689b5837SMatthew G. Knepley   Output Parameter:
4589689b5837SMatthew G. Knepley . Nf - The number of fields
4590689b5837SMatthew G. Knepley 
4591689b5837SMatthew G. Knepley   Level: intermediate
4592689b5837SMatthew G. Knepley 
4593689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4594689b5837SMatthew G. Knepley @*/
45950f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45960f21e855SMatthew G. Knepley {
45970f21e855SMatthew G. Knepley   PetscFunctionBegin;
45980f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4599534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
460044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4601af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4602af122d2aSMatthew G Knepley }
4603af122d2aSMatthew G Knepley 
4604689b5837SMatthew G. Knepley /*@
4605689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4606689b5837SMatthew G. Knepley 
4607d083f849SBarry Smith   Logically collective on dm
4608689b5837SMatthew G. Knepley 
4609689b5837SMatthew G. Knepley   Input Parameters:
4610689b5837SMatthew G. Knepley + dm - The DM
4611689b5837SMatthew G. Knepley - Nf - The number of fields
4612689b5837SMatthew G. Knepley 
4613689b5837SMatthew G. Knepley   Level: intermediate
4614689b5837SMatthew G. Knepley 
4615689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4616689b5837SMatthew G. Knepley @*/
4617af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4618af122d2aSMatthew G Knepley {
46190f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4620af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4621af122d2aSMatthew G Knepley 
4622af122d2aSMatthew G Knepley   PetscFunctionBegin;
4623af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462444a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
46250f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
46260f21e855SMatthew G. Knepley     PetscContainer obj;
46270f21e855SMatthew G. Knepley 
46280f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
462944a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
46300f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4631af122d2aSMatthew G Knepley   }
4632af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4633af122d2aSMatthew G Knepley }
4634af122d2aSMatthew G Knepley 
4635c1929be8SMatthew G. Knepley /*@
4636c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4637c1929be8SMatthew G. Knepley 
4638c1929be8SMatthew G. Knepley   Not collective
4639c1929be8SMatthew G. Knepley 
4640c1929be8SMatthew G. Knepley   Input Parameters:
4641c1929be8SMatthew G. Knepley + dm - The DM
4642c1929be8SMatthew G. Knepley - f  - The field number
4643c1929be8SMatthew G. Knepley 
464444a7f3ddSMatthew G. Knepley   Output Parameters:
464544a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
464644a7f3ddSMatthew G. Knepley - field - The discretization object
4647c1929be8SMatthew G. Knepley 
464844a7f3ddSMatthew G. Knepley   Level: intermediate
4649c1929be8SMatthew G. Knepley 
465044a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4651c1929be8SMatthew G. Knepley @*/
465244a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4653af122d2aSMatthew G Knepley {
4654af122d2aSMatthew G Knepley   PetscFunctionBegin;
4655af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465644a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
465744a7f3ddSMatthew G. Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
465844a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
465944a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4660decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4661decb47aaSMatthew G. Knepley }
4662decb47aaSMatthew G. Knepley 
4663083401c6SMatthew G. Knepley /* Does not clear the DS */
4664083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4665083401c6SMatthew G. Knepley {
4666083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4667083401c6SMatthew G. Knepley 
4668083401c6SMatthew G. Knepley   PetscFunctionBegin;
4669083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4670083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4671083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4672083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4673083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4674083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4675083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4676083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4677083401c6SMatthew G. Knepley }
4678083401c6SMatthew G. Knepley 
4679c1929be8SMatthew G. Knepley /*@
4680c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4681c1929be8SMatthew G. Knepley 
4682d083f849SBarry Smith   Logically collective on dm
4683c1929be8SMatthew G. Knepley 
4684c1929be8SMatthew G. Knepley   Input Parameters:
4685c1929be8SMatthew G. Knepley + dm    - The DM
4686c1929be8SMatthew G. Knepley . f     - The field number
468744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4688c1929be8SMatthew G. Knepley - field - The discretization object
4689c1929be8SMatthew G. Knepley 
469044a7f3ddSMatthew G. Knepley   Level: intermediate
4691c1929be8SMatthew G. Knepley 
469244a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4693c1929be8SMatthew G. Knepley @*/
469444a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4695decb47aaSMatthew G. Knepley {
4696decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4697decb47aaSMatthew G. Knepley 
4698decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4699decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4700e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
470144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4702e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4703083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
470434aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
47052df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
470644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
470744a7f3ddSMatthew G. Knepley }
470844a7f3ddSMatthew G. Knepley 
470944a7f3ddSMatthew G. Knepley /*@
471044a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
471144a7f3ddSMatthew G. Knepley 
4712d083f849SBarry Smith   Logically collective on dm
471344a7f3ddSMatthew G. Knepley 
471444a7f3ddSMatthew G. Knepley   Input Parameters:
471544a7f3ddSMatthew G. Knepley + dm    - The DM
471644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
471744a7f3ddSMatthew G. Knepley - field - The discretization object
471844a7f3ddSMatthew G. Knepley 
471944a7f3ddSMatthew G. Knepley   Level: intermediate
472044a7f3ddSMatthew G. Knepley 
472144a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
472244a7f3ddSMatthew G. Knepley @*/
472344a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
472444a7f3ddSMatthew G. Knepley {
472544a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
472644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
472744a7f3ddSMatthew G. Knepley 
472844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
472944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
473044a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
473144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
473244a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
473344a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
473444a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
473544a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
473644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
473734aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
47382df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4739af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4740af122d2aSMatthew G Knepley }
47416636e97aSMatthew G Knepley 
4742e5e52638SMatthew G. Knepley /*@
4743e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4744e5e52638SMatthew G. Knepley 
4745d083f849SBarry Smith   Collective on dm
4746e5e52638SMatthew G. Knepley 
4747e5e52638SMatthew G. Knepley   Input Parameter:
4748e5e52638SMatthew G. Knepley . dm - The DM
4749e5e52638SMatthew G. Knepley 
4750e5e52638SMatthew G. Knepley   Output Parameter:
4751e5e52638SMatthew G. Knepley . newdm - The DM
4752e5e52638SMatthew G. Knepley 
4753e5e52638SMatthew G. Knepley   Level: advanced
4754e5e52638SMatthew G. Knepley 
4755e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4756e5e52638SMatthew G. Knepley @*/
4757e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4758e5e52638SMatthew G. Knepley {
4759e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4760e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4761e5e52638SMatthew G. Knepley 
4762e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4763e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4764e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4765e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4766e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4767e5e52638SMatthew G. Knepley     DMLabel     label;
4768e5e52638SMatthew G. Knepley     PetscObject field;
476934aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4770e5e52638SMatthew G. Knepley 
4771e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4772e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
477334aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
477434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
477534aa8a36SMatthew G. Knepley   }
477634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
477734aa8a36SMatthew G. Knepley }
477834aa8a36SMatthew G. Knepley 
477934aa8a36SMatthew G. Knepley /*@
478034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
478134aa8a36SMatthew G. Knepley 
478234aa8a36SMatthew G. Knepley   Not collective
478334aa8a36SMatthew G. Knepley 
478434aa8a36SMatthew G. Knepley   Input Parameters:
478534aa8a36SMatthew G. Knepley + dm - The DM object
478634aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
478734aa8a36SMatthew G. Knepley 
478834aa8a36SMatthew G. Knepley   Output Parameter:
478934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
479034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
479134aa8a36SMatthew G. Knepley 
479234aa8a36SMatthew G. Knepley   Notes:
479334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
479434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
479534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4796979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
479734aa8a36SMatthew G. Knepley 
479834aa8a36SMatthew G. Knepley   Level: developer
479934aa8a36SMatthew G. Knepley 
480034aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
480134aa8a36SMatthew G. Knepley @*/
480234aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
480334aa8a36SMatthew G. Knepley {
480434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
480534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4806534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4807534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
480834aa8a36SMatthew G. Knepley   if (f < 0) {
480934aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
481034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
481134aa8a36SMatthew G. Knepley   } else {
481234aa8a36SMatthew G. Knepley     PetscInt       Nf;
481334aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
481434aa8a36SMatthew G. Knepley 
481534aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
481634aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
481734aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
481834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
481934aa8a36SMatthew G. Knepley   }
482034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
482134aa8a36SMatthew G. Knepley }
482234aa8a36SMatthew G. Knepley 
482334aa8a36SMatthew G. Knepley /*@
482434aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
482534aa8a36SMatthew G. Knepley 
482634aa8a36SMatthew G. Knepley   Not collective
482734aa8a36SMatthew G. Knepley 
482834aa8a36SMatthew G. Knepley   Input Parameters:
482934aa8a36SMatthew G. Knepley + dm         - The DM object
483034aa8a36SMatthew G. Knepley . f          - The field number
483134aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
483234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
483334aa8a36SMatthew G. Knepley 
483434aa8a36SMatthew G. Knepley   Notes:
483534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
483634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
483734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4838979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
483934aa8a36SMatthew G. Knepley 
484034aa8a36SMatthew G. Knepley   Level: developer
484134aa8a36SMatthew G. Knepley 
484234aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
484334aa8a36SMatthew G. Knepley @*/
484434aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
484534aa8a36SMatthew G. Knepley {
484634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
484734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
484834aa8a36SMatthew G. Knepley   if (f < 0) {
484934aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
485034aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
485134aa8a36SMatthew G. Knepley   } else {
485234aa8a36SMatthew G. Knepley     PetscInt       Nf;
485334aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
485434aa8a36SMatthew G. Knepley 
485534aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
485634aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
485734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
485834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4859e5e52638SMatthew G. Knepley   }
4860e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4861e5e52638SMatthew G. Knepley }
4862e5e52638SMatthew G. Knepley 
4863b0441da4SMatthew G. Knepley /*@
4864b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4865b0441da4SMatthew G. Knepley 
4866b0441da4SMatthew G. Knepley   Not collective
4867b0441da4SMatthew G. Knepley 
4868b0441da4SMatthew G. Knepley   Input Parameters:
4869b0441da4SMatthew G. Knepley . dm - The DM object
4870b0441da4SMatthew G. Knepley 
4871b0441da4SMatthew G. Knepley   Output Parameter:
4872b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4873b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4874b0441da4SMatthew G. Knepley 
4875b0441da4SMatthew G. Knepley   Notes:
4876b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4877b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4878b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4879b0441da4SMatthew G. Knepley 
4880b0441da4SMatthew G. Knepley   Level: developer
4881b0441da4SMatthew G. Knepley 
4882b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4883b0441da4SMatthew G. Knepley @*/
4884b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4885b0441da4SMatthew G. Knepley {
4886b0441da4SMatthew G. Knepley   PetscInt       Nf;
4887b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4888b0441da4SMatthew G. Knepley 
4889b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4890b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4891534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4892534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4893b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4894b0441da4SMatthew G. Knepley   if (!Nf) {
4895b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4896b0441da4SMatthew G. Knepley   } else {
4897b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4898b0441da4SMatthew G. Knepley   }
4899b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4900b0441da4SMatthew G. Knepley }
4901b0441da4SMatthew G. Knepley 
4902b0441da4SMatthew G. Knepley /*@
4903b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4904b0441da4SMatthew G. Knepley 
4905b0441da4SMatthew G. Knepley   Not collective
4906b0441da4SMatthew G. Knepley 
4907b0441da4SMatthew G. Knepley   Input Parameters:
4908b0441da4SMatthew G. Knepley + dm         - The DM object
4909b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4910b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4911b0441da4SMatthew G. Knepley 
4912b0441da4SMatthew G. Knepley   Notes:
4913b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4914b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4915b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4916b0441da4SMatthew G. Knepley 
4917b0441da4SMatthew G. Knepley   Level: developer
4918b0441da4SMatthew G. Knepley 
4919b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4920b0441da4SMatthew G. Knepley @*/
4921b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4922b0441da4SMatthew G. Knepley {
4923b0441da4SMatthew G. Knepley   PetscInt       Nf;
4924b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4925b0441da4SMatthew G. Knepley 
4926b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4927b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4928b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4929b0441da4SMatthew G. Knepley   if (!Nf) {
4930b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4931b0441da4SMatthew G. Knepley   } else {
4932b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4933e5e52638SMatthew G. Knepley   }
4934e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4935e5e52638SMatthew G. Knepley }
4936e5e52638SMatthew G. Knepley 
4937783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
4938783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
4939783e2ec8SMatthew G. Knepley {
4940783e2ec8SMatthew G. Knepley   DMLabel        label;
4941783e2ec8SMatthew G. Knepley   PetscObject    obj;
4942783e2ec8SMatthew G. Knepley   PetscClassId   id;
4943783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
4944783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
4945783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
4946783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
4947783e2ec8SMatthew G. Knepley 
4948783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
4949783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
4950783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
4951783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
4952783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
4953783e2ec8SMatthew G. Knepley   if (isFE && label) {
4954783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
4955783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
4956783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
4957783e2ec8SMatthew G. Knepley       const char *lname;
4958783e2ec8SMatthew G. Knepley 
4959783e2ec8SMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
4960783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
4961783e2ec8SMatthew G. Knepley       if (duplicate) break;
4962783e2ec8SMatthew G. Knepley     }
4963783e2ec8SMatthew G. Knepley     if (!duplicate) {
4964783e2ec8SMatthew G. Knepley       DM plex;
4965783e2ec8SMatthew G. Knepley 
4966783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
4967783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
4968783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
4969783e2ec8SMatthew G. Knepley     }
4970783e2ec8SMatthew G. Knepley   }
4971783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
4972783e2ec8SMatthew G. Knepley }
4973783e2ec8SMatthew G. Knepley 
4974e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4975e5e52638SMatthew G. Knepley {
4976e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4977e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4978e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4979e5e52638SMatthew G. Knepley 
4980e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4981e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4982e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4983e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4984b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4985e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4986e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4987e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4988e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4989e5e52638SMatthew G. Knepley }
4990e5e52638SMatthew G. Knepley 
4991e5e52638SMatthew G. Knepley /*@
4992e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4993e5e52638SMatthew G. Knepley 
4994e5e52638SMatthew G. Knepley   Not collective
4995e5e52638SMatthew G. Knepley 
4996e5e52638SMatthew G. Knepley   Input Parameter:
4997e5e52638SMatthew G. Knepley . dm - The DM
4998e5e52638SMatthew G. Knepley 
4999e5e52638SMatthew G. Knepley   Output Parameter:
5000e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5001e5e52638SMatthew G. Knepley 
5002e5e52638SMatthew G. Knepley   Level: intermediate
5003e5e52638SMatthew G. Knepley 
5004e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5005e5e52638SMatthew G. Knepley @*/
5006e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5007e5e52638SMatthew G. Knepley {
5008e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5009e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5010534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5011e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5012e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5013e5e52638SMatthew G. Knepley }
5014e5e52638SMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley /*@
5016e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5017e5e52638SMatthew G. Knepley 
5018d083f849SBarry Smith   Logically collective on dm
5019e5e52638SMatthew G. Knepley 
5020e5e52638SMatthew G. Knepley   Input Parameter:
5021e5e52638SMatthew G. Knepley . dm - The DM
5022e5e52638SMatthew G. Knepley 
5023e5e52638SMatthew G. Knepley   Level: intermediate
5024e5e52638SMatthew G. Knepley 
5025e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5026e5e52638SMatthew G. Knepley @*/
5027e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5028e5e52638SMatthew G. Knepley {
5029e5e52638SMatthew G. Knepley   PetscInt       s;
5030e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5031e5e52638SMatthew G. Knepley 
5032e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5033e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5034e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5035e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5036e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5037b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5038e5e52638SMatthew G. Knepley   }
5039e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5040e5e52638SMatthew G. Knepley   dm->probs = NULL;
5041e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5042e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5043e5e52638SMatthew G. Knepley }
5044e5e52638SMatthew G. Knepley 
5045e5e52638SMatthew G. Knepley /*@
5046e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5047e5e52638SMatthew G. Knepley 
5048e5e52638SMatthew G. Knepley   Not collective
5049e5e52638SMatthew G. Knepley 
5050e5e52638SMatthew G. Knepley   Input Parameter:
5051e5e52638SMatthew G. Knepley . dm    - The DM
5052e5e52638SMatthew G. Knepley 
5053e5e52638SMatthew G. Knepley   Output Parameter:
5054e5e52638SMatthew G. Knepley . prob - The default PetscDS
5055e5e52638SMatthew G. Knepley 
5056e5e52638SMatthew G. Knepley   Level: intermediate
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5059e5e52638SMatthew G. Knepley @*/
5060e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5061e5e52638SMatthew G. Knepley {
5062b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5063b0143b4dSMatthew G. Knepley 
5064e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5065e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5066e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5067b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5068b0143b4dSMatthew G. Knepley     PetscDS ds;
5069b0143b4dSMatthew G. Knepley 
5070b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5071b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5072b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5073b0143b4dSMatthew G. Knepley   }
5074b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5075e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5076e5e52638SMatthew G. Knepley }
5077e5e52638SMatthew G. Knepley 
5078e5e52638SMatthew G. Knepley /*@
5079e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5080e5e52638SMatthew G. Knepley 
5081e5e52638SMatthew G. Knepley   Not collective
5082e5e52638SMatthew G. Knepley 
5083e5e52638SMatthew G. Knepley   Input Parameters:
5084e5e52638SMatthew G. Knepley + dm    - The DM
5085e5e52638SMatthew G. Knepley - point - Cell for the DS
5086e5e52638SMatthew G. Knepley 
5087e5e52638SMatthew G. Knepley   Output Parameter:
5088e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5089e5e52638SMatthew G. Knepley 
5090e5e52638SMatthew G. Knepley   Level: developer
5091e5e52638SMatthew G. Knepley 
5092b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5093e5e52638SMatthew G. Knepley @*/
5094e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5095e5e52638SMatthew G. Knepley {
5096e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5097e5e52638SMatthew G. Knepley   PetscInt       s;
5098e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5099e5e52638SMatthew G. Knepley 
5100e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5101e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5102e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5103360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5104e5e52638SMatthew G. Knepley   *prob = NULL;
5105e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5106e5e52638SMatthew G. Knepley     PetscInt val;
5107e5e52638SMatthew G. Knepley 
5108e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5109e5e52638SMatthew G. Knepley     else {
5110e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5111e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5112e5e52638SMatthew G. Knepley     }
5113e5e52638SMatthew G. Knepley   }
5114e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5115e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5116e5e52638SMatthew G. Knepley }
5117e5e52638SMatthew G. Knepley 
5118e5e52638SMatthew G. Knepley /*@
5119e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5120e5e52638SMatthew G. Knepley 
5121e5e52638SMatthew G. Knepley   Not collective
5122e5e52638SMatthew G. Knepley 
5123e5e52638SMatthew G. Knepley   Input Parameters:
5124e5e52638SMatthew G. Knepley + dm    - The DM
5125e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5126e5e52638SMatthew G. Knepley 
5127b3cf3223SMatthew G. Knepley   Output Parameters:
5128b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5129b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5130e5e52638SMatthew G. Knepley 
5131e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5132e5e52638SMatthew G. Knepley 
5133e5e52638SMatthew G. Knepley   Level: advanced
5134e5e52638SMatthew G. Knepley 
5135e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5136e5e52638SMatthew G. Knepley @*/
5137b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5138e5e52638SMatthew G. Knepley {
5139e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5140e5e52638SMatthew G. Knepley 
5141e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5142e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5143e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5144b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5145b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5146e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5147b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5148b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5149b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5150b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5151b3cf3223SMatthew G. Knepley     }
5152e5e52638SMatthew G. Knepley   }
51532df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5154e5e52638SMatthew G. Knepley }
5155e5e52638SMatthew G. Knepley 
5156e5e52638SMatthew G. Knepley /*@
5157083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5158083401c6SMatthew G. Knepley 
5159083401c6SMatthew G. Knepley   Collective on dm
5160083401c6SMatthew G. Knepley 
5161083401c6SMatthew G. Knepley   Input Parameters:
5162083401c6SMatthew G. Knepley + dm     - The DM
5163083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5164083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5165083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5166083401c6SMatthew G. Knepley 
5167083401c6SMatthew 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,
5168083401c6SMatthew G. Knepley   the fields argument is ignored.
5169083401c6SMatthew G. Knepley 
5170083401c6SMatthew G. Knepley   Level: advanced
5171083401c6SMatthew G. Knepley 
5172083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5173083401c6SMatthew G. Knepley @*/
5174083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5175083401c6SMatthew G. Knepley {
5176083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5177083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5178083401c6SMatthew G. Knepley 
5179083401c6SMatthew G. Knepley   PetscFunctionBegin;
5180083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5181083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5182083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5183083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5184083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5185083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5186083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5187083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5188083401c6SMatthew G. Knepley     }
5189083401c6SMatthew G. Knepley   }
5190083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5191083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5192083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5193083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5194083401c6SMatthew G. Knepley   if (!label) {
5195083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5196083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5197083401c6SMatthew G. Knepley     Nds = 0;
5198083401c6SMatthew G. Knepley   }
5199083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5200083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5201083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5202083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5203083401c6SMatthew G. Knepley }
5204083401c6SMatthew G. Knepley 
5205083401c6SMatthew G. Knepley /*@
5206e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5207e5e52638SMatthew G. Knepley 
5208e5e52638SMatthew G. Knepley   Not collective
5209e5e52638SMatthew G. Knepley 
5210e5e52638SMatthew G. Knepley   Input Parameters:
5211e5e52638SMatthew G. Knepley + dm  - The DM
5212e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5213e5e52638SMatthew G. Knepley 
5214e5e52638SMatthew G. Knepley   Output Parameters:
5215b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5216b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5217083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5218e5e52638SMatthew G. Knepley 
5219e5e52638SMatthew G. Knepley   Level: advanced
5220e5e52638SMatthew G. Knepley 
5221e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5222e5e52638SMatthew G. Knepley @*/
5223b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5224e5e52638SMatthew G. Knepley {
5225e5e52638SMatthew G. Knepley   PetscInt       Nds;
5226e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5227e5e52638SMatthew G. Knepley 
5228e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5229e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5230e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5231e5e52638SMatthew G. Knepley   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5232e5e52638SMatthew G. Knepley   if (label) {
5233e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5234e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5235e5e52638SMatthew G. Knepley   }
5236b3cf3223SMatthew G. Knepley   if (fields) {
5237b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5238b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5239b3cf3223SMatthew G. Knepley   }
5240e5e52638SMatthew G. Knepley   if (ds) {
5241b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5242e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5243e5e52638SMatthew G. Knepley   }
5244e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5245e5e52638SMatthew G. Knepley }
5246e5e52638SMatthew G. Knepley 
5247e5e52638SMatthew G. Knepley /*@
5248083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5249e5e52638SMatthew G. Knepley 
5250083401c6SMatthew G. Knepley   Not collective
5251e5e52638SMatthew G. Knepley 
5252e5e52638SMatthew G. Knepley   Input Parameters:
5253e5e52638SMatthew G. Knepley + dm     - The DM
5254083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5255083401c6SMatthew G. Knepley . label  - The region label, or NULL
5256083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5257083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5258e5e52638SMatthew G. Knepley 
5259e5e52638SMatthew G. Knepley   Level: advanced
5260e5e52638SMatthew G. Knepley 
5261083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5262e5e52638SMatthew G. Knepley @*/
5263083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5264e5e52638SMatthew G. Knepley {
5265083401c6SMatthew G. Knepley   PetscInt       Nds;
5266e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5267e5e52638SMatthew G. Knepley 
5268e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5269e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5270083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5271083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5272083401c6SMatthew G. Knepley   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5273e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5274083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5275083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5276083401c6SMatthew G. Knepley   if (fields) {
5277083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5278b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5279083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5280083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5281e5e52638SMatthew G. Knepley   }
5282083401c6SMatthew G. Knepley   if (ds) {
5283083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5284083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5285083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5286083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5287083401c6SMatthew G. Knepley   }
5288e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5289e5e52638SMatthew G. Knepley }
5290e5e52638SMatthew G. Knepley 
5291e5e52638SMatthew G. Knepley /*@
52921d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
52931d3af9e0SMatthew G. Knepley 
52941d3af9e0SMatthew G. Knepley   Not collective
52951d3af9e0SMatthew G. Knepley 
52961d3af9e0SMatthew G. Knepley   Input Parameters:
52971d3af9e0SMatthew G. Knepley + dm  - The DM
52981d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
52991d3af9e0SMatthew G. Knepley 
53001d3af9e0SMatthew G. Knepley   Output Parameter:
53011d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
53021d3af9e0SMatthew G. Knepley 
53031d3af9e0SMatthew G. Knepley   Level: advanced
53041d3af9e0SMatthew G. Knepley 
53051d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
53061d3af9e0SMatthew G. Knepley @*/
53071d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
53081d3af9e0SMatthew G. Knepley {
53091d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
53101d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
53111d3af9e0SMatthew G. Knepley 
53121d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
53131d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53141d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
53151d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
53161d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
53171d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
53181d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
53191d3af9e0SMatthew G. Knepley   else          *num = n;
53201d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
53211d3af9e0SMatthew G. Knepley }
53221d3af9e0SMatthew G. Knepley 
53231d3af9e0SMatthew G. Knepley /*@
5324e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5325e5e52638SMatthew G. Knepley 
5326d083f849SBarry Smith   Collective on dm
5327e5e52638SMatthew G. Knepley 
5328e5e52638SMatthew G. Knepley   Input Parameter:
5329e5e52638SMatthew G. Knepley . dm - The DM
5330e5e52638SMatthew G. Knepley 
5331e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5332e5e52638SMatthew G. Knepley 
5333e5e52638SMatthew G. Knepley   Level: intermediate
5334e5e52638SMatthew G. Knepley 
5335e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5336e5e52638SMatthew G. Knepley @*/
5337e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5338e5e52638SMatthew G. Knepley {
5339e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5340083401c6SMatthew G. Knepley   PetscDS        dsDef;
5341083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5342083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5343e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5344e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5345e5e52638SMatthew G. Knepley 
5346e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5347e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5348e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5349e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5350083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5351083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5352083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5353083401c6SMatthew G. Knepley   Nl   = 0;
5354083401c6SMatthew G. Knepley   Ndef = 0;
5355083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5356083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5357083401c6SMatthew G. Knepley     PetscInt l;
5358083401c6SMatthew G. Knepley 
5359083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5360083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5361083401c6SMatthew G. Knepley     if (l < Nl) continue;
5362083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5363083401c6SMatthew G. Knepley   }
5364083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5365083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5366083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5367b3cf3223SMatthew G. Knepley     IS        fields;
5368b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5369b3cf3223SMatthew G. Knepley 
5370b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5371083401c6SMatthew G. Knepley     if (nf) {
5372b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5373b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
537488f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
537588f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
537688f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
537788f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
537888f0c812SMatthew G. Knepley 
5379083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5380083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5381083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5382b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
53832df9ee95SMatthew G. Knepley     }
5384083401c6SMatthew G. Knepley   }
5385083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5386083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5387083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5388083401c6SMatthew G. Knepley   if (Ndef && Nl) {
53890122748bSMatthew G. Knepley     DM              plex;
5390083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5391083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5392083401c6SMatthew G. Knepley     PetscInt       *fields;
5393083401c6SMatthew G. Knepley     const PetscInt *cells;
5394083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
53950122748bSMatthew G. Knepley 
53960122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
53970122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5398083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5399083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5400083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5401083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5402083401c6SMatthew G. Knepley       IS      pointIS;
5403083401c6SMatthew G. Knepley 
5404083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5405083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5406083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5407083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5408083401c6SMatthew G. Knepley     }
5409083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5410083401c6SMatthew G. Knepley 
5411083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5412083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5413083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5414083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5415083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5416083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5417083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5418083401c6SMatthew G. Knepley 
5419083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5420083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5421083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5422083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5423083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5424083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5425083401c6SMatthew G. Knepley 
5426083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5427083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5428083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5429083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5430083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5431083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
54320122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5433083401c6SMatthew G. Knepley   }
5434083401c6SMatthew G. Knepley   /* Create label DSes
5435083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5436083401c6SMatthew G. Knepley   */
5437083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5438083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5439083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5440083401c6SMatthew G. Knepley     PetscDS   ds;
5441083401c6SMatthew G. Knepley     IS        fields;
5442083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5443083401c6SMatthew G. Knepley 
5444083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5445083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5446083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5447083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5448083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5449083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5450083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5451083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5452083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5453083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5454083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5455083401c6SMatthew G. Knepley     {
5456083401c6SMatthew G. Knepley       DMPolytopeType ct;
5457083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5458083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
54590122748bSMatthew G. Knepley 
5460e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5461665f567fSMatthew G. Knepley       if (lStart >= 0) {
5462412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5463412e9a14SMatthew G. Knepley         switch (ct) {
5464412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5465412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5466412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5467412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5468083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5469083401c6SMatthew G. Knepley           default: break;
5470412e9a14SMatthew G. Knepley         }
5471665f567fSMatthew G. Knepley       }
5472083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5473083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5474e5e52638SMatthew G. Knepley     }
5475083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5476e5e52638SMatthew G. Knepley   }
5477083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5478e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5479083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5480083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5481083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5482083401c6SMatthew G. Knepley     const PetscInt *fld;
5483083401c6SMatthew G. Knepley     PetscInt        nf;
5484e5e52638SMatthew G. Knepley 
5485083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5486083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5487083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5488083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5489083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5490e5e52638SMatthew G. Knepley       PetscClassId id;
5491e5e52638SMatthew G. Knepley 
5492083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5493083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5494083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5495083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5496083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5497e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5498e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5499e5e52638SMatthew G. Knepley     }
5500083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5501e5e52638SMatthew G. Knepley   }
5502e5e52638SMatthew G. Knepley   /* Setup DSes */
5503e5e52638SMatthew G. Knepley   if (doSetup) {
5504e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5505e5e52638SMatthew G. Knepley   }
5506e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5507e5e52638SMatthew G. Knepley }
5508e5e52638SMatthew G. Knepley 
5509e5e52638SMatthew G. Knepley /*@
5510*7f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
5511*7f96f943SMatthew G. Knepley 
5512*7f96f943SMatthew G. Knepley   Input Parameters:
5513*7f96f943SMatthew G. Knepley + dm   - The DM
5514*7f96f943SMatthew G. Knepley - time - The time
5515*7f96f943SMatthew G. Knepley 
5516*7f96f943SMatthew G. Knepley   Output Parameters:
5517*7f96f943SMatthew G. Knepley . u    - The vector will be filled with exact solution values
5518*7f96f943SMatthew G. Knepley 
5519*7f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
5520*7f96f943SMatthew G. Knepley 
5521*7f96f943SMatthew G. Knepley   Level: developer
5522*7f96f943SMatthew G. Knepley 
5523*7f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
5524*7f96f943SMatthew G. Knepley @*/
5525*7f96f943SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u)
5526*7f96f943SMatthew G. Knepley {
5527*7f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
5528*7f96f943SMatthew G. Knepley   void            **ectxs;
5529*7f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
5530*7f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
5531*7f96f943SMatthew G. Knepley 
5532*7f96f943SMatthew G. Knepley   PetscFunctionBegin;
5533*7f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5534*7f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
5535*7f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5536*7f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5537*7f96f943SMatthew G. Knepley     PetscDS         ds;
5538*7f96f943SMatthew G. Knepley     DMLabel         label;
5539*7f96f943SMatthew G. Knepley     IS              fieldIS;
5540*7f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
5541*7f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
5542*7f96f943SMatthew G. Knepley 
5543*7f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
5544*7f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
5545*7f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
5546*7f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5547*7f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5548*7f96f943SMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
5549*7f96f943SMatthew G. Knepley       const PetscInt field = fields[f];
5550*7f96f943SMatthew G. Knepley       ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5551*7f96f943SMatthew G. Knepley     }
5552*7f96f943SMatthew G. Knepley     ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5553*7f96f943SMatthew G. Knepley     if (label) {
5554*7f96f943SMatthew G. Knepley       ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
5555*7f96f943SMatthew G. Knepley     } else {
5556*7f96f943SMatthew G. Knepley       ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
5557*7f96f943SMatthew G. Knepley     }
5558*7f96f943SMatthew G. Knepley   }
5559*7f96f943SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
5560*7f96f943SMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5561*7f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
5562*7f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
5563*7f96f943SMatthew G. Knepley }
5564*7f96f943SMatthew G. Knepley 
5565*7f96f943SMatthew G. Knepley /*@
5566e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5567e5e52638SMatthew G. Knepley 
5568d083f849SBarry Smith   Collective on dm
5569e5e52638SMatthew G. Knepley 
5570e5e52638SMatthew G. Knepley   Input Parameter:
5571e5e52638SMatthew G. Knepley . dm - The DM
5572e5e52638SMatthew G. Knepley 
5573e5e52638SMatthew G. Knepley   Output Parameter:
5574e5e52638SMatthew G. Knepley . newdm - The DM
5575e5e52638SMatthew G. Knepley 
5576e5e52638SMatthew G. Knepley   Level: advanced
5577e5e52638SMatthew G. Knepley 
5578e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5579e5e52638SMatthew G. Knepley @*/
5580e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5581e5e52638SMatthew G. Knepley {
5582e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5583e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5584e5e52638SMatthew G. Knepley 
5585e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5586e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5587e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5588e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5589e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5590e5e52638SMatthew G. Knepley     DMLabel  label;
5591b3cf3223SMatthew G. Knepley     IS       fields;
5592e5e52638SMatthew G. Knepley     PetscDS  ds;
5593783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5594e5e52638SMatthew G. Knepley 
5595b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5596b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5597783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5598783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5599783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5600783e2ec8SMatthew G. Knepley       PetscInt    field;
5601783e2ec8SMatthew G. Knepley 
5602783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
5603783e2ec8SMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5604783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5605783e2ec8SMatthew G. Knepley     }
5606e5e52638SMatthew G. Knepley   }
5607e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5608e5e52638SMatthew G. Knepley }
5609e5e52638SMatthew G. Knepley 
5610e5e52638SMatthew G. Knepley /*@
5611e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another 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 
5618e5e52638SMatthew G. Knepley   Output Parameter:
5619e5e52638SMatthew G. Knepley . newdm - The DM
5620e5e52638SMatthew G. Knepley 
5621e5e52638SMatthew G. Knepley   Level: advanced
5622e5e52638SMatthew G. Knepley 
5623e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5624e5e52638SMatthew G. Knepley @*/
5625e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5626e5e52638SMatthew G. Knepley {
5627e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5628e5e52638SMatthew G. Knepley 
5629e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5630e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5631e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5632e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5633e5e52638SMatthew G. Knepley }
5634e5e52638SMatthew G. Knepley 
5635b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5636b64e0483SPeter Brune {
5637b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5638b64e0483SPeter Brune   PetscErrorCode ierr;
5639b64e0483SPeter Brune   Vec coords,ccoords;
56406dbf9973SLawrence Mitchell   Mat inject;
5641b64e0483SPeter Brune   PetscFunctionBegin;
5642b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5643b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5644b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5645b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5646b64e0483SPeter Brune   if (coords && !ccoords) {
5647b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
56486668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
56496dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
56502adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
56516dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5652b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5653b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5654b64e0483SPeter Brune   }
5655b64e0483SPeter Brune   PetscFunctionReturn(0);
5656b64e0483SPeter Brune }
5657b64e0483SPeter Brune 
565803dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
565903dadc2fSPeter Brune {
566003dadc2fSPeter Brune   DM dm_coord,subdm_coord;
566103dadc2fSPeter Brune   PetscErrorCode ierr;
566203dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
566303dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
566403dadc2fSPeter Brune   PetscFunctionBegin;
566503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
566603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
566703dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
566803dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
566903dadc2fSPeter Brune   if (coords && !ccoords) {
567003dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
56716668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
567203dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
567324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
567403dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
567503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
567603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
56771ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
567803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
567903dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
568003dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
568103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
568203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
568303dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
568403dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
568503dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
568603dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
568703dadc2fSPeter Brune   }
568803dadc2fSPeter Brune   PetscFunctionReturn(0);
568903dadc2fSPeter Brune }
569003dadc2fSPeter Brune 
5691c73cfb54SMatthew G. Knepley /*@
5692c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5693c73cfb54SMatthew G. Knepley 
5694c73cfb54SMatthew G. Knepley   Not collective
5695c73cfb54SMatthew G. Knepley 
5696c73cfb54SMatthew G. Knepley   Input Parameter:
5697c73cfb54SMatthew G. Knepley . dm - The DM
5698c73cfb54SMatthew G. Knepley 
5699c73cfb54SMatthew G. Knepley   Output Parameter:
5700c73cfb54SMatthew G. Knepley . dim - The topological dimension
5701c73cfb54SMatthew G. Knepley 
5702c73cfb54SMatthew G. Knepley   Level: beginner
5703c73cfb54SMatthew G. Knepley 
5704c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5705c73cfb54SMatthew G. Knepley @*/
5706c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5707c73cfb54SMatthew G. Knepley {
5708c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5709c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5710534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5711c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5712c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5713c73cfb54SMatthew G. Knepley }
5714c73cfb54SMatthew G. Knepley 
5715c73cfb54SMatthew G. Knepley /*@
5716c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5717c73cfb54SMatthew G. Knepley 
5718c73cfb54SMatthew G. Knepley   Collective on dm
5719c73cfb54SMatthew G. Knepley 
5720c73cfb54SMatthew G. Knepley   Input Parameters:
5721c73cfb54SMatthew G. Knepley + dm - The DM
5722c73cfb54SMatthew G. Knepley - dim - The topological dimension
5723c73cfb54SMatthew G. Knepley 
5724c73cfb54SMatthew G. Knepley   Level: beginner
5725c73cfb54SMatthew G. Knepley 
5726c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5727c73cfb54SMatthew G. Knepley @*/
5728c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5729c73cfb54SMatthew G. Knepley {
5730e5e52638SMatthew G. Knepley   PetscDS        ds;
5731f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5732f17e8794SMatthew G. Knepley 
5733c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5734c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5735c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5736c73cfb54SMatthew G. Knepley   dm->dim = dim;
5737e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5738e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5739c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5740c73cfb54SMatthew G. Knepley }
5741c73cfb54SMatthew G. Knepley 
5742793f3fe5SMatthew G. Knepley /*@
5743793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5744793f3fe5SMatthew G. Knepley 
5745d083f849SBarry Smith   Collective on dm
5746793f3fe5SMatthew G. Knepley 
5747793f3fe5SMatthew G. Knepley   Input Parameters:
5748793f3fe5SMatthew G. Knepley + dm - the DM
5749793f3fe5SMatthew G. Knepley - dim - the dimension
5750793f3fe5SMatthew G. Knepley 
5751793f3fe5SMatthew G. Knepley   Output Parameters:
5752793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5753aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5754793f3fe5SMatthew G. Knepley 
5755793f3fe5SMatthew G. Knepley   Note:
5756793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5757a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5758793f3fe5SMatthew G. Knepley   then the interval is empty.
5759793f3fe5SMatthew G. Knepley 
5760793f3fe5SMatthew G. Knepley   Level: intermediate
5761793f3fe5SMatthew G. Knepley 
5762793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5763793f3fe5SMatthew G. Knepley @*/
5764793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5765793f3fe5SMatthew G. Knepley {
5766793f3fe5SMatthew G. Knepley   PetscInt       d;
5767793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5768793f3fe5SMatthew G. Knepley 
5769793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5770793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5771793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5772793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5773b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5774793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5775793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5776793f3fe5SMatthew G. Knepley }
5777793f3fe5SMatthew G. Knepley 
57786636e97aSMatthew G Knepley /*@
57796636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
57806636e97aSMatthew G Knepley 
5781d083f849SBarry Smith   Collective on dm
57826636e97aSMatthew G Knepley 
57836636e97aSMatthew G Knepley   Input Parameters:
57846636e97aSMatthew G Knepley + dm - the DM
57856636e97aSMatthew G Knepley - c - coordinate vector
57866636e97aSMatthew G Knepley 
57872dd40e9bSPatrick Sanan   Notes:
57882dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
57892dd40e9bSPatrick Sanan 
57902dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
57916636e97aSMatthew G Knepley 
57926636e97aSMatthew G Knepley   Level: intermediate
57936636e97aSMatthew G Knepley 
57942dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
57956636e97aSMatthew G Knepley @*/
57966636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
57976636e97aSMatthew G Knepley {
57986636e97aSMatthew G Knepley   PetscErrorCode ierr;
57996636e97aSMatthew G Knepley 
58006636e97aSMatthew G Knepley   PetscFunctionBegin;
58016636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58026636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
58036636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
58046636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
58056636e97aSMatthew G Knepley   dm->coordinates = c;
58066636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5807b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
580803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
58096636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58106636e97aSMatthew G Knepley }
58116636e97aSMatthew G Knepley 
58126636e97aSMatthew G Knepley /*@
58136636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
58146636e97aSMatthew G Knepley 
58157058e716SVaclav Hapla   Not collective
58166636e97aSMatthew G Knepley 
58176636e97aSMatthew G Knepley    Input Parameters:
58186636e97aSMatthew G Knepley +  dm - the DM
58196636e97aSMatthew G Knepley -  c - coordinate vector
58206636e97aSMatthew G Knepley 
58212dd40e9bSPatrick Sanan   Notes:
58226636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
58236636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
58246636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
58256636e97aSMatthew G Knepley 
58262dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
58272dd40e9bSPatrick Sanan 
58286636e97aSMatthew G Knepley   Level: intermediate
58296636e97aSMatthew G Knepley 
58306636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
58316636e97aSMatthew G Knepley @*/
58326636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
58336636e97aSMatthew G Knepley {
58346636e97aSMatthew G Knepley   PetscErrorCode ierr;
58356636e97aSMatthew G Knepley 
58366636e97aSMatthew G Knepley   PetscFunctionBegin;
58376636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58386636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
58396636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
58406636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
58418865f1eaSKarl Rupp 
58426636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
58438865f1eaSKarl Rupp 
58446636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
58456636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58466636e97aSMatthew G Knepley }
58476636e97aSMatthew G Knepley 
58486636e97aSMatthew G Knepley /*@
58496636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
58506636e97aSMatthew G Knepley 
5851d083f849SBarry Smith   Collective on dm
58526636e97aSMatthew G Knepley 
58536636e97aSMatthew G Knepley   Input Parameter:
58546636e97aSMatthew G Knepley . dm - the DM
58556636e97aSMatthew G Knepley 
58566636e97aSMatthew G Knepley   Output Parameter:
58576636e97aSMatthew G Knepley . c - global coordinate vector
58586636e97aSMatthew G Knepley 
58596636e97aSMatthew G Knepley   Note:
58606636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
58616636e97aSMatthew G Knepley 
58626636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
58636636e97aSMatthew G Knepley 
58646636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
58656636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
58666636e97aSMatthew G Knepley 
58676636e97aSMatthew G Knepley   Level: intermediate
58686636e97aSMatthew G Knepley 
58696636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
58706636e97aSMatthew G Knepley @*/
58716636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
58726636e97aSMatthew G Knepley {
58736636e97aSMatthew G Knepley   PetscErrorCode ierr;
58746636e97aSMatthew G Knepley 
58756636e97aSMatthew G Knepley   PetscFunctionBegin;
58766636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58776636e97aSMatthew G Knepley   PetscValidPointer(c,2);
58781f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
58790298fd71SBarry Smith     DM        cdm = NULL;
58801970a576SMatthew G. Knepley     PetscBool localized;
58816636e97aSMatthew G Knepley 
58826636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
58836636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
58841970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
58851970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
58861970a576SMatthew G. Knepley     if (localized) {
58871970a576SMatthew G. Knepley       PetscInt cdim;
58881970a576SMatthew G. Knepley 
58891970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
58901970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
58911970a576SMatthew G. Knepley     }
58926636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
58936636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
58946636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
58956636e97aSMatthew G Knepley   }
58966636e97aSMatthew G Knepley   *c = dm->coordinates;
58976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58986636e97aSMatthew G Knepley }
58996636e97aSMatthew G Knepley 
59006636e97aSMatthew G Knepley /*@
590181e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
590281e9a530SVaclav Hapla 
5903d083f849SBarry Smith   Collective on dm
590481e9a530SVaclav Hapla 
590581e9a530SVaclav Hapla   Input Parameter:
590681e9a530SVaclav Hapla . dm - the DM
590781e9a530SVaclav Hapla 
590881e9a530SVaclav Hapla   Level: advanced
590981e9a530SVaclav Hapla 
591081e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
591181e9a530SVaclav Hapla @*/
591281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
591381e9a530SVaclav Hapla {
591481e9a530SVaclav Hapla   PetscErrorCode ierr;
591581e9a530SVaclav Hapla 
591681e9a530SVaclav Hapla   PetscFunctionBegin;
591781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
591881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
59191970a576SMatthew G. Knepley     DM        cdm = NULL;
59201970a576SMatthew G. Knepley     PetscBool localized;
59211970a576SMatthew G. Knepley 
592281e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
592381e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
59241970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
59251970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
59261970a576SMatthew G. Knepley     if (localized) {
59271970a576SMatthew G. Knepley       PetscInt cdim;
59281970a576SMatthew G. Knepley 
59291970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
59301970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
59311970a576SMatthew G. Knepley     }
593281e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
593381e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
593481e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
593581e9a530SVaclav Hapla   }
593681e9a530SVaclav Hapla   PetscFunctionReturn(0);
593781e9a530SVaclav Hapla }
593881e9a530SVaclav Hapla 
593981e9a530SVaclav Hapla /*@
59406636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
59416636e97aSMatthew G Knepley 
5942d083f849SBarry Smith   Collective on dm
59436636e97aSMatthew G Knepley 
59446636e97aSMatthew G Knepley   Input Parameter:
59456636e97aSMatthew G Knepley . dm - the DM
59466636e97aSMatthew G Knepley 
59476636e97aSMatthew G Knepley   Output Parameter:
59486636e97aSMatthew G Knepley . c - coordinate vector
59496636e97aSMatthew G Knepley 
59506636e97aSMatthew G Knepley   Note:
59516636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
59526636e97aSMatthew G Knepley 
59536636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
59546636e97aSMatthew G Knepley 
59556636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59566636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59576636e97aSMatthew G Knepley 
59586636e97aSMatthew G Knepley   Level: intermediate
59596636e97aSMatthew G Knepley 
596081e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
59616636e97aSMatthew G Knepley @*/
59626636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
59636636e97aSMatthew G Knepley {
59646636e97aSMatthew G Knepley   PetscErrorCode ierr;
59656636e97aSMatthew G Knepley 
59666636e97aSMatthew G Knepley   PetscFunctionBegin;
59676636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59686636e97aSMatthew G Knepley   PetscValidPointer(c,2);
596981e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
59706636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
59716636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59726636e97aSMatthew G Knepley }
59736636e97aSMatthew G Knepley 
597481e9a530SVaclav Hapla /*@
597581e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
597681e9a530SVaclav Hapla 
597781e9a530SVaclav Hapla   Not collective
597881e9a530SVaclav Hapla 
597981e9a530SVaclav Hapla   Input Parameter:
598081e9a530SVaclav Hapla . dm - the DM
598181e9a530SVaclav Hapla 
598281e9a530SVaclav Hapla   Output Parameter:
598381e9a530SVaclav Hapla . c - coordinate vector
598481e9a530SVaclav Hapla 
598581e9a530SVaclav Hapla   Level: advanced
598681e9a530SVaclav Hapla 
598781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
598881e9a530SVaclav Hapla @*/
598981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
599081e9a530SVaclav Hapla {
599181e9a530SVaclav Hapla   PetscFunctionBegin;
599281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
599381e9a530SVaclav Hapla   PetscValidPointer(c,2);
599481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
59956636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
59966636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59976636e97aSMatthew G Knepley }
59986636e97aSMatthew G Knepley 
59992db98f8dSVaclav Hapla /*@
60002db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
60012db98f8dSVaclav Hapla 
60022db98f8dSVaclav Hapla   Not collective
60032db98f8dSVaclav Hapla 
60042db98f8dSVaclav Hapla   Input Parameter:
60052db98f8dSVaclav Hapla + dm - the DM
60062db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
60072db98f8dSVaclav Hapla 
60082db98f8dSVaclav Hapla   Output Parameter:
60092db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
60102db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
60112db98f8dSVaclav Hapla 
60122db98f8dSVaclav Hapla   Note:
60132db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
60142db98f8dSVaclav Hapla 
60152db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
60162db98f8dSVaclav Hapla 
60172db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
60182db98f8dSVaclav Hapla 
60192db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60202db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60212db98f8dSVaclav Hapla 
60222db98f8dSVaclav Hapla   Level: advanced
60232db98f8dSVaclav Hapla 
60242db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
60252db98f8dSVaclav Hapla @*/
60262db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
60272db98f8dSVaclav Hapla {
60282db98f8dSVaclav Hapla   PetscSection        cs, newcs;
60292db98f8dSVaclav Hapla   Vec                 coords;
60302db98f8dSVaclav Hapla   const PetscScalar   *arr;
60312db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
60322db98f8dSVaclav Hapla   PetscInt            n;
60332db98f8dSVaclav Hapla   PetscErrorCode      ierr;
60342db98f8dSVaclav Hapla 
60352db98f8dSVaclav Hapla   PetscFunctionBegin;
60362db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60372db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
60382db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
60392db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
60402db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
60411bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
60421bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
60432db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
60442db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
60452db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
60462db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
60472db98f8dSVaclav Hapla   if (pCoord) {
60482db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
60492db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
60502db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
60512db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6052ad9ac99dSVaclav Hapla   } else {
6053ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
60542db98f8dSVaclav Hapla   }
6055ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6056ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
60572db98f8dSVaclav Hapla   PetscFunctionReturn(0);
60582db98f8dSVaclav Hapla }
60592db98f8dSVaclav Hapla 
6060f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6061f19dbd58SToby Isaac {
6062f19dbd58SToby Isaac   PetscErrorCode ierr;
6063f19dbd58SToby Isaac 
6064f19dbd58SToby Isaac   PetscFunctionBegin;
6065f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6066f19dbd58SToby Isaac   PetscValidPointer(field,2);
6067f19dbd58SToby Isaac   if (!dm->coordinateField) {
6068f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6069f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6070f19dbd58SToby Isaac     }
6071f19dbd58SToby Isaac   }
6072f19dbd58SToby Isaac   *field = dm->coordinateField;
6073f19dbd58SToby Isaac   PetscFunctionReturn(0);
6074f19dbd58SToby Isaac }
6075f19dbd58SToby Isaac 
6076f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6077f19dbd58SToby Isaac {
6078f19dbd58SToby Isaac   PetscErrorCode ierr;
6079f19dbd58SToby Isaac 
6080f19dbd58SToby Isaac   PetscFunctionBegin;
6081f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6082f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6083c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6084f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6085f19dbd58SToby Isaac   dm->coordinateField = field;
6086f19dbd58SToby Isaac   PetscFunctionReturn(0);
6087f19dbd58SToby Isaac }
6088f19dbd58SToby Isaac 
60896636e97aSMatthew G Knepley /*@
60901cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
60916636e97aSMatthew G Knepley 
6092d083f849SBarry Smith   Collective on dm
60936636e97aSMatthew G Knepley 
60946636e97aSMatthew G Knepley   Input Parameter:
60956636e97aSMatthew G Knepley . dm - the DM
60966636e97aSMatthew G Knepley 
60976636e97aSMatthew G Knepley   Output Parameter:
60986636e97aSMatthew G Knepley . cdm - coordinate DM
60996636e97aSMatthew G Knepley 
61006636e97aSMatthew G Knepley   Level: intermediate
61016636e97aSMatthew G Knepley 
61021cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
61036636e97aSMatthew G Knepley @*/
61046636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
61056636e97aSMatthew G Knepley {
61066636e97aSMatthew G Knepley   PetscErrorCode ierr;
61076636e97aSMatthew G Knepley 
61086636e97aSMatthew G Knepley   PetscFunctionBegin;
61096636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61106636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
61116636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6112308f8a94SToby Isaac     DM cdm;
6113308f8a94SToby Isaac 
611482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6115308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6116308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6117308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6118308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6119308f8a94SToby Isaac     dm->coordinateDM = cdm;
61206636e97aSMatthew G Knepley   }
61216636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
61226636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61236636e97aSMatthew G Knepley }
6124e87bb0d3SMatthew G Knepley 
61251cfe2091SMatthew G. Knepley /*@
61261cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
61271cfe2091SMatthew G. Knepley 
6128d083f849SBarry Smith   Logically Collective on dm
61291cfe2091SMatthew G. Knepley 
61301cfe2091SMatthew G. Knepley   Input Parameters:
61311cfe2091SMatthew G. Knepley + dm - the DM
61321cfe2091SMatthew G. Knepley - cdm - coordinate DM
61331cfe2091SMatthew G. Knepley 
61341cfe2091SMatthew G. Knepley   Level: intermediate
61351cfe2091SMatthew G. Knepley 
61361cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
61371cfe2091SMatthew G. Knepley @*/
61381cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
61391cfe2091SMatthew G. Knepley {
61401cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
61411cfe2091SMatthew G. Knepley 
61421cfe2091SMatthew G. Knepley   PetscFunctionBegin;
61431cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61441cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6145f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
61461cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
61471cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
61481cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
61491cfe2091SMatthew G. Knepley }
61501cfe2091SMatthew G. Knepley 
615146e270d4SMatthew G. Knepley /*@
615246e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
615346e270d4SMatthew G. Knepley 
615446e270d4SMatthew G. Knepley   Not Collective
615546e270d4SMatthew G. Knepley 
615646e270d4SMatthew G. Knepley   Input Parameter:
615746e270d4SMatthew G. Knepley . dm - The DM object
615846e270d4SMatthew G. Knepley 
615946e270d4SMatthew G. Knepley   Output Parameter:
616046e270d4SMatthew G. Knepley . dim - The embedding dimension
616146e270d4SMatthew G. Knepley 
616246e270d4SMatthew G. Knepley   Level: intermediate
616346e270d4SMatthew G. Knepley 
616492fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
616546e270d4SMatthew G. Knepley @*/
616646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
616746e270d4SMatthew G. Knepley {
616846e270d4SMatthew G. Knepley   PetscFunctionBegin;
616946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6170534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
61719a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
61729a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
61739a9a41abSToby Isaac   }
617446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
617546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
617646e270d4SMatthew G. Knepley }
617746e270d4SMatthew G. Knepley 
617846e270d4SMatthew G. Knepley /*@
617946e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
618046e270d4SMatthew G. Knepley 
618146e270d4SMatthew G. Knepley   Not Collective
618246e270d4SMatthew G. Knepley 
618346e270d4SMatthew G. Knepley   Input Parameters:
618446e270d4SMatthew G. Knepley + dm  - The DM object
618546e270d4SMatthew G. Knepley - dim - The embedding dimension
618646e270d4SMatthew G. Knepley 
618746e270d4SMatthew G. Knepley   Level: intermediate
618846e270d4SMatthew G. Knepley 
618992fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
619046e270d4SMatthew G. Knepley @*/
619146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
619246e270d4SMatthew G. Knepley {
6193e5e52638SMatthew G. Knepley   PetscDS        ds;
6194f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6195f17e8794SMatthew G. Knepley 
619646e270d4SMatthew G. Knepley   PetscFunctionBegin;
619746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
619846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6199e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6200e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
620146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
620246e270d4SMatthew G. Knepley }
620346e270d4SMatthew G. Knepley 
6204e8abe2deSMatthew G. Knepley /*@
6205e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6206e8abe2deSMatthew G. Knepley 
6207d083f849SBarry Smith   Collective on dm
6208e8abe2deSMatthew G. Knepley 
6209e8abe2deSMatthew G. Knepley   Input Parameter:
6210e8abe2deSMatthew G. Knepley . dm - The DM object
6211e8abe2deSMatthew G. Knepley 
6212e8abe2deSMatthew G. Knepley   Output Parameter:
6213e8abe2deSMatthew G. Knepley . section - The PetscSection object
6214e8abe2deSMatthew G. Knepley 
6215e8abe2deSMatthew G. Knepley   Level: intermediate
6216e8abe2deSMatthew G. Knepley 
621792fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6218e8abe2deSMatthew G. Knepley @*/
6219e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6220e8abe2deSMatthew G. Knepley {
6221e8abe2deSMatthew G. Knepley   DM             cdm;
6222e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6223e8abe2deSMatthew G. Knepley 
6224e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6225e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6226e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6227e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
622892fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6229e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6230e8abe2deSMatthew G. Knepley }
6231e8abe2deSMatthew G. Knepley 
6232e8abe2deSMatthew G. Knepley /*@
6233e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6234e8abe2deSMatthew G. Knepley 
6235e8abe2deSMatthew G. Knepley   Not Collective
6236e8abe2deSMatthew G. Knepley 
6237e8abe2deSMatthew G. Knepley   Input Parameters:
6238e8abe2deSMatthew G. Knepley + dm      - The DM object
623946e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6240e8abe2deSMatthew G. Knepley - section - The PetscSection object
6241e8abe2deSMatthew G. Knepley 
6242e8abe2deSMatthew G. Knepley   Level: intermediate
6243e8abe2deSMatthew G. Knepley 
624492fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6245e8abe2deSMatthew G. Knepley @*/
624646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6247e8abe2deSMatthew G. Knepley {
6248e8abe2deSMatthew G. Knepley   DM             cdm;
6249e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6250e8abe2deSMatthew G. Knepley 
6251e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6252e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
625346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6254e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
625592fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
625646e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
62574c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
625846e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
625946e270d4SMatthew G. Knepley 
626046e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
626146e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
626246e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
626346e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
626446e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
626546e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
626646e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
626746e270d4SMatthew G. Knepley     }
6268ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
626946e270d4SMatthew G. Knepley   }
6270e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6271e8abe2deSMatthew G. Knepley }
6272e8abe2deSMatthew G. Knepley 
62735dc8c3f7SMatthew G. Knepley /*@C
627490b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
62755dc8c3f7SMatthew G. Knepley 
62765dc8c3f7SMatthew G. Knepley   Input Parameters:
627790b157c4SStefano Zampini . dm      - The DM object
627890b157c4SStefano Zampini 
627990b157c4SStefano Zampini   Output Parameters:
628090b157c4SStefano Zampini + per     - Whether the DM is periodic or not
62815dc8c3f7SMatthew 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
62825dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
62835dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
62845dc8c3f7SMatthew G. Knepley 
62855dc8c3f7SMatthew G. Knepley   Level: developer
62865dc8c3f7SMatthew G. Knepley 
62875dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
62885dc8c3f7SMatthew G. Knepley @*/
628990b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6290c6b900c6SMatthew G. Knepley {
6291c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6292c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
629390b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6294c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6295c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
62965dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6297c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6298c6b900c6SMatthew G. Knepley }
6299c6b900c6SMatthew G. Knepley 
63005dc8c3f7SMatthew G. Knepley /*@C
63015dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
63025dc8c3f7SMatthew G. Knepley 
63035dc8c3f7SMatthew G. Knepley   Input Parameters:
63045dc8c3f7SMatthew G. Knepley + dm      - The DM object
630590b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
63065dc8c3f7SMatthew 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
63075dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
63085dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
63095dc8c3f7SMatthew G. Knepley 
63105dc8c3f7SMatthew G. Knepley   Level: developer
63115dc8c3f7SMatthew G. Knepley 
63125dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
63135dc8c3f7SMatthew G. Knepley @*/
631490b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6315c6b900c6SMatthew G. Knepley {
6316c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6317c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6318c6b900c6SMatthew G. Knepley 
6319c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6320c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
632190b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6322412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6323412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6324412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
63255dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
632690b157c4SStefano Zampini   if (maxCell) {
6327412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6328412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6329412e9a14SMatthew G. Knepley   }
6330412e9a14SMatthew G. Knepley   if (L) {
6331412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6332412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6333412e9a14SMatthew G. Knepley   }
6334412e9a14SMatthew G. Knepley   if (bd) {
6335412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6336412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
633790b157c4SStefano Zampini   }
6338072d7d67SStefano Zampini   dm->periodic = per;
6339c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6340c6b900c6SMatthew G. Knepley }
6341c6b900c6SMatthew G. Knepley 
63422e17dfb7SMatthew G. Knepley /*@
63432e17dfb7SMatthew 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.
63442e17dfb7SMatthew G. Knepley 
63452e17dfb7SMatthew G. Knepley   Input Parameters:
63462e17dfb7SMatthew G. Knepley + dm     - The DM
634765da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
634865da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
63492e17dfb7SMatthew G. Knepley 
63502e17dfb7SMatthew G. Knepley   Output Parameter:
63512e17dfb7SMatthew G. Knepley . out - The localized coordinate point
63522e17dfb7SMatthew G. Knepley 
63532e17dfb7SMatthew G. Knepley   Level: developer
63542e17dfb7SMatthew G. Knepley 
63552e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
63562e17dfb7SMatthew G. Knepley @*/
635765da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
63582e17dfb7SMatthew G. Knepley {
63592e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
63602e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
63612e17dfb7SMatthew G. Knepley 
63622e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63632e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
63642e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
63652e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
63662e17dfb7SMatthew G. Knepley   } else {
636765da65dcSMatthew G. Knepley     if (endpoint) {
636865da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6369da3333bfSMatthew 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)) {
6370da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
637165da65dcSMatthew G. Knepley         } else {
6372da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
637365da65dcSMatthew G. Knepley         }
637465da65dcSMatthew G. Knepley       }
637565da65dcSMatthew G. Knepley     } else {
63762e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
63771118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
63782e17dfb7SMatthew G. Knepley       }
63792e17dfb7SMatthew G. Knepley     }
638065da65dcSMatthew G. Knepley   }
63812e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63822e17dfb7SMatthew G. Knepley }
63832e17dfb7SMatthew G. Knepley 
63842e17dfb7SMatthew G. Knepley /*
63852e17dfb7SMatthew 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.
63862e17dfb7SMatthew G. Knepley 
63872e17dfb7SMatthew G. Knepley   Input Parameters:
63882e17dfb7SMatthew G. Knepley + dm     - The DM
63892e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
63902e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
63912e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
63922e17dfb7SMatthew G. Knepley 
63932e17dfb7SMatthew G. Knepley   Output Parameter:
63942e17dfb7SMatthew G. Knepley . out - The localized coordinate point
63952e17dfb7SMatthew G. Knepley 
63962e17dfb7SMatthew G. Knepley   Level: developer
63972e17dfb7SMatthew G. Knepley 
63982e17dfb7SMatthew 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
63992e17dfb7SMatthew G. Knepley 
64002e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
64012e17dfb7SMatthew G. Knepley */
64022e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
64032e17dfb7SMatthew G. Knepley {
64042e17dfb7SMatthew G. Knepley   PetscInt d;
64052e17dfb7SMatthew G. Knepley 
64062e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
64072e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
64082e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
64092e17dfb7SMatthew G. Knepley   } else {
64102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6411908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
64122e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
64132e17dfb7SMatthew G. Knepley       } else {
64142e17dfb7SMatthew G. Knepley         out[d] = in[d];
64152e17dfb7SMatthew G. Knepley       }
64162e17dfb7SMatthew G. Knepley     }
64172e17dfb7SMatthew G. Knepley   }
64182e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64192e17dfb7SMatthew G. Knepley }
6420a5801f52SStefano Zampini 
64212e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
64222e17dfb7SMatthew G. Knepley {
64232e17dfb7SMatthew G. Knepley   PetscInt d;
64242e17dfb7SMatthew G. Knepley 
64252e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
64262e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
64272e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
64282e17dfb7SMatthew G. Knepley   } else {
64292e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6430908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
64312e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
64322e17dfb7SMatthew G. Knepley       } else {
64332e17dfb7SMatthew G. Knepley         out[d] = in[d];
64342e17dfb7SMatthew G. Knepley       }
64352e17dfb7SMatthew G. Knepley     }
64362e17dfb7SMatthew G. Knepley   }
64372e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64382e17dfb7SMatthew G. Knepley }
64392e17dfb7SMatthew G. Knepley 
64402e17dfb7SMatthew G. Knepley /*
64412e17dfb7SMatthew 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.
64422e17dfb7SMatthew G. Knepley 
64432e17dfb7SMatthew G. Knepley   Input Parameters:
64442e17dfb7SMatthew G. Knepley + dm     - The DM
64452e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
64462e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
64472e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
64482e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
64492e17dfb7SMatthew G. Knepley 
64502e17dfb7SMatthew G. Knepley   Output Parameter:
64512e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
64522e17dfb7SMatthew G. Knepley 
64532e17dfb7SMatthew G. Knepley   Level: developer
64542e17dfb7SMatthew G. Knepley 
64552e17dfb7SMatthew 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
64562e17dfb7SMatthew G. Knepley 
64572e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
64582e17dfb7SMatthew G. Knepley */
64592e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
64602e17dfb7SMatthew G. Knepley {
64612e17dfb7SMatthew G. Knepley   PetscInt d;
64622e17dfb7SMatthew G. Knepley 
64632e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
64642e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
64652e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
64662e17dfb7SMatthew G. Knepley   } else {
64672e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6468412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6469412e9a14SMatthew G. Knepley 
6470412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6471412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6472412e9a14SMatthew G. Knepley 
6473412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6474412e9a14SMatthew G. Knepley           SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%D-Coordinate %g more than %g away from anchor %g", d, (double) PetscRealPart(in[d]), (double) maxC, (double) PetscRealPart(anchor[d]));
6475412e9a14SMatthew G. Knepley         out[d] += newCoord;
64762e17dfb7SMatthew G. Knepley       } else {
64772e17dfb7SMatthew G. Knepley         out[d] += in[d];
64782e17dfb7SMatthew G. Knepley       }
64792e17dfb7SMatthew G. Knepley     }
64802e17dfb7SMatthew G. Knepley   }
64812e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64822e17dfb7SMatthew G. Knepley }
64832e17dfb7SMatthew G. Knepley 
648436447a5eSToby Isaac /*@
64858f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
64868f700142SStefano Zampini 
64878f700142SStefano Zampini   Not collective
648836447a5eSToby Isaac 
648936447a5eSToby Isaac   Input Parameter:
649036447a5eSToby Isaac . dm - The DM
649136447a5eSToby Isaac 
649236447a5eSToby Isaac   Output Parameter:
649336447a5eSToby Isaac   areLocalized - True if localized
649436447a5eSToby Isaac 
649536447a5eSToby Isaac   Level: developer
649636447a5eSToby Isaac 
64978f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
649836447a5eSToby Isaac @*/
64998f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
650036447a5eSToby Isaac {
650136447a5eSToby Isaac   DM             cdm;
650236447a5eSToby Isaac   PetscSection   coordSection;
650346a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
650446a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
650536447a5eSToby Isaac   PetscErrorCode ierr;
650636447a5eSToby Isaac 
650736447a5eSToby Isaac   PetscFunctionBegin;
650836447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6509534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
65108b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
651146a3a80fSLisandro Dalcin 
651236447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
651336447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
651446a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
65159f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
651646a3a80fSLisandro Dalcin 
65179f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
651846a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
651936447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
652036447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
652146a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
652246a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
652336447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
652446a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
652536447a5eSToby Isaac   }
65268f700142SStefano Zampini   *areLocalized = alreadyLocalized;
652736447a5eSToby Isaac   PetscFunctionReturn(0);
652836447a5eSToby Isaac }
652936447a5eSToby Isaac 
65308f700142SStefano Zampini /*@
65318f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
65328f700142SStefano Zampini 
65338f700142SStefano Zampini   Collective on dm
65348f700142SStefano Zampini 
65358f700142SStefano Zampini   Input Parameter:
65368f700142SStefano Zampini . dm - The DM
65378f700142SStefano Zampini 
65388f700142SStefano Zampini   Output Parameter:
65398f700142SStefano Zampini   areLocalized - True if localized
65408f700142SStefano Zampini 
65418f700142SStefano Zampini   Level: developer
65428f700142SStefano Zampini 
65438f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
65448f700142SStefano Zampini @*/
65458f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
65468f700142SStefano Zampini {
65478f700142SStefano Zampini   PetscBool      localized;
65488f700142SStefano Zampini   PetscErrorCode ierr;
65498f700142SStefano Zampini 
65508f700142SStefano Zampini   PetscFunctionBegin;
65518f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6552534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
65538f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
65548f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
65558f700142SStefano Zampini   PetscFunctionReturn(0);
65568f700142SStefano Zampini }
655736447a5eSToby Isaac 
65582e17dfb7SMatthew G. Knepley /*@
6559492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
65602e17dfb7SMatthew G. Knepley 
65618f700142SStefano Zampini   Collective on dm
65628f700142SStefano Zampini 
65632e17dfb7SMatthew G. Knepley   Input Parameter:
65642e17dfb7SMatthew G. Knepley . dm - The DM
65652e17dfb7SMatthew G. Knepley 
65662e17dfb7SMatthew G. Knepley   Level: developer
65672e17dfb7SMatthew G. Knepley 
65688f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
65692e17dfb7SMatthew G. Knepley @*/
65702e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
65712e17dfb7SMatthew G. Knepley {
65722e17dfb7SMatthew G. Knepley   DM             cdm;
65732e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
65742e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
65753e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
65763e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6577e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
65783e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
65793e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
65802e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
65812e17dfb7SMatthew G. Knepley 
65822e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65832e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
658492c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6585f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6586f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6587f7cbd40bSStefano Zampini 
65882e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
65892e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
65902e17dfb7SMatthew G. Knepley   {
65912e17dfb7SMatthew G. Knepley     PetscBool isplex;
65922e17dfb7SMatthew G. Knepley 
65932e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
65942e17dfb7SMatthew G. Knepley     if (isplex) {
65952e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
65963e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
659769291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
65983e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
65993e922f36SToby Isaac       newStart = vStart;
66003e922f36SToby Isaac       newEnd   = vEnd;
66013e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
66023e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
66033e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
66043e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
66053e922f36SToby Isaac       }
66062e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
66072e17dfb7SMatthew G. Knepley   }
66082e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
660943eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
66102e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
66113e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6612e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
66133e922f36SToby Isaac 
66142e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
66152e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
66162e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
66172e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
66183e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
66193e922f36SToby Isaac 
662069291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
66213e922f36SToby Isaac   localized = &anchor[bs];
66223e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
66233e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
66243e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
66253e922f36SToby Isaac 
66263e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
66273e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
66283e922f36SToby Isaac       PetscInt     b;
66293e922f36SToby Isaac 
66303e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
66313e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
66323e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
66333e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
66343e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
66353e922f36SToby Isaac         for (b = 0; b < bs; b++) {
66363e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
66373e922f36SToby Isaac         }
66383e922f36SToby Isaac         if (b < bs) break;
66393e922f36SToby Isaac       }
66403e922f36SToby Isaac       if (d < dof/bs) {
66413e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
66423e922f36SToby Isaac           PetscInt cdof;
66433e922f36SToby Isaac 
66443e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
66453e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
66463e922f36SToby Isaac         }
66473e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
66483e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
66493e922f36SToby Isaac       }
66503e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
66513e922f36SToby Isaac     }
66523e922f36SToby Isaac   }
66533e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
66543e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
665569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
66563e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
665769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
66583e922f36SToby Isaac     PetscFunctionReturn(0);
66593e922f36SToby Isaac   }
66602e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
66612e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
66622e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
66632e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
66642e17dfb7SMatthew G. Knepley   }
66652e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
66662e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6667c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
66682e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
66692e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
66702e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
66712e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6672c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
66732e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
66742e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
66752e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
66762e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
66772e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
66782e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
66792e17dfb7SMatthew G. Knepley   }
66803e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
66813e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
66823e922f36SToby Isaac 
66832e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
66842e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
66853e922f36SToby Isaac       PetscInt     b, cdof;
66862e17dfb7SMatthew G. Knepley 
66873e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
66883e922f36SToby Isaac       if (!cdof) continue;
66892e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
66902e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
66912e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
66922e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
66932e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
66942e17dfb7SMatthew G. Knepley     }
66953e922f36SToby Isaac   }
669669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
669769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6698c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
66992e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
67002e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
67012e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
67022e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
67032e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
67042e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
67052e17dfb7SMatthew G. Knepley }
67062e17dfb7SMatthew G. Knepley 
6707e87bb0d3SMatthew G Knepley /*@
67083a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6709e87bb0d3SMatthew G Knepley 
6710d083f849SBarry Smith   Collective on v (see explanation below)
6711e87bb0d3SMatthew G Knepley 
6712e87bb0d3SMatthew G Knepley   Input Parameters:
6713e87bb0d3SMatthew G Knepley + dm - The DM
67143a93e3b7SToby Isaac . v - The Vec of points
671562a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
67163a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6717e87bb0d3SMatthew G Knepley 
671861e3bb9bSMatthew G Knepley   Output Parameter:
671962a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
672062a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
67213a93e3b7SToby Isaac 
6722e87bb0d3SMatthew G Knepley 
6723e87bb0d3SMatthew G Knepley   Level: developer
672461e3bb9bSMatthew G Knepley 
672562a38674SMatthew G. Knepley   Notes:
67263a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
672762a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
67283a93e3b7SToby Isaac 
67293a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
673062a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
67313a93e3b7SToby Isaac 
67323a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
67333a93e3b7SToby Isaac 
673462a38674SMatthew G. Knepley $    const PetscSFNode *cells;
673562a38674SMatthew G. Knepley $    PetscInt           nFound;
6736a6216909SToby Isaac $    const PetscInt    *found;
673762a38674SMatthew G. Knepley $
6738a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
67393a93e3b7SToby Isaac 
67403a93e3b7SToby 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
67413a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
67423a93e3b7SToby Isaac 
674362a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
674461e3bb9bSMatthew G Knepley @*/
674562a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6746e87bb0d3SMatthew G Knepley {
6747735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6748735aa83eSMatthew G Knepley 
6749e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6750e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6751e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6752e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
67533a93e3b7SToby Isaac   if (*cellSF) {
67543a93e3b7SToby Isaac     PetscMPIInt result;
67553a93e3b7SToby Isaac 
6756e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6757a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
67583a93e3b7SToby Isaac     if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
6759e0fc9d1bSMatthew G. Knepley   } else {
67603a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
67613a93e3b7SToby Isaac   }
6762b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
676347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
676462a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
676547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6766e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6767e87bb0d3SMatthew G Knepley }
676814f150ffSMatthew G. Knepley 
6769f4d763aaSMatthew G. Knepley /*@
6770f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6771f4d763aaSMatthew G. Knepley 
67728f700142SStefano Zampini   Collective on dm
67738f700142SStefano Zampini 
6774f4d763aaSMatthew G. Knepley   Input Parameter:
6775f4d763aaSMatthew G. Knepley . dm - The original DM
6776f4d763aaSMatthew G. Knepley 
6777f4d763aaSMatthew G. Knepley   Output Parameter:
6778f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6779f4d763aaSMatthew G. Knepley 
6780f4d763aaSMatthew G. Knepley   Level: intermediate
6781f4d763aaSMatthew G. Knepley 
6782e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6783f4d763aaSMatthew G. Knepley @*/
678414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
678514f150ffSMatthew G. Knepley {
6786c26acbdeSMatthew G. Knepley   PetscSection   section;
67872d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
678814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
678914f150ffSMatthew G. Knepley 
679014f150ffSMatthew G. Knepley   PetscFunctionBegin;
679114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
679214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
679392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6794c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6795127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
67962d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6797c26acbdeSMatthew G. Knepley     *odm = dm;
6798c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6799c26acbdeSMatthew G. Knepley   }
680014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6801c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
680214f150ffSMatthew G. Knepley     PetscSF      sf;
680314f150ffSMatthew G. Knepley 
680414f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6805e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
680614f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
680792fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
680814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
680914f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
681015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6811e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
681214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
681314f150ffSMatthew G. Knepley   }
681414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
681514f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
681614f150ffSMatthew G. Knepley }
6817f4d763aaSMatthew G. Knepley 
6818f4d763aaSMatthew G. Knepley /*@
6819cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6820f4d763aaSMatthew G. Knepley 
6821f4d763aaSMatthew G. Knepley   Input Parameter:
6822f4d763aaSMatthew G. Knepley . dm - The original DM
6823f4d763aaSMatthew G. Knepley 
6824cdb7a50dSMatthew G. Knepley   Output Parameters:
6825cdb7a50dSMatthew G. Knepley + num - The output sequence number
6826cdb7a50dSMatthew G. Knepley - val - The output sequence value
6827f4d763aaSMatthew G. Knepley 
6828f4d763aaSMatthew G. Knepley   Level: intermediate
6829f4d763aaSMatthew G. Knepley 
6830f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6831f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6832f4d763aaSMatthew G. Knepley 
6833f4d763aaSMatthew G. Knepley .seealso: VecView()
6834f4d763aaSMatthew G. Knepley @*/
6835cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6836f4d763aaSMatthew G. Knepley {
6837f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6838f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6839534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6840534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6841f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6842f4d763aaSMatthew G. Knepley }
6843f4d763aaSMatthew G. Knepley 
6844f4d763aaSMatthew G. Knepley /*@
6845cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6846f4d763aaSMatthew G. Knepley 
6847f4d763aaSMatthew G. Knepley   Input Parameters:
6848f4d763aaSMatthew G. Knepley + dm - The original DM
6849cdb7a50dSMatthew G. Knepley . num - The output sequence number
6850cdb7a50dSMatthew G. Knepley - val - The output sequence value
6851f4d763aaSMatthew G. Knepley 
6852f4d763aaSMatthew G. Knepley   Level: intermediate
6853f4d763aaSMatthew G. Knepley 
6854f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6855f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6856f4d763aaSMatthew G. Knepley 
6857f4d763aaSMatthew G. Knepley .seealso: VecView()
6858f4d763aaSMatthew G. Knepley @*/
6859cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6860f4d763aaSMatthew G. Knepley {
6861f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6862f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6863f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6864cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6865cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6866cdb7a50dSMatthew G. Knepley }
6867cdb7a50dSMatthew G. Knepley 
6868cdb7a50dSMatthew G. Knepley /*@C
6869cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6870cdb7a50dSMatthew G. Knepley 
6871cdb7a50dSMatthew G. Knepley   Input Parameters:
6872cdb7a50dSMatthew G. Knepley + dm   - The original DM
6873cdb7a50dSMatthew G. Knepley . name - The sequence name
6874cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6875cdb7a50dSMatthew G. Knepley 
6876cdb7a50dSMatthew G. Knepley   Output Parameter:
6877cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6878cdb7a50dSMatthew G. Knepley 
6879cdb7a50dSMatthew G. Knepley   Level: intermediate
6880cdb7a50dSMatthew G. Knepley 
6881cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6882cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6883cdb7a50dSMatthew G. Knepley 
6884cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6885cdb7a50dSMatthew G. Knepley @*/
6886cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6887cdb7a50dSMatthew G. Knepley {
6888cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6889cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6890cdb7a50dSMatthew G. Knepley 
6891cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6892cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6893cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6894534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6895cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6896cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6897cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6898cdb7a50dSMatthew G. Knepley     PetscScalar value;
6899cdb7a50dSMatthew G. Knepley 
690039d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
69014aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6902cdb7a50dSMatthew G. Knepley #endif
6903cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6904f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6905f4d763aaSMatthew G. Knepley }
69068e4ac7eaSMatthew G. Knepley 
69078e4ac7eaSMatthew G. Knepley /*@
69088e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
69098e4ac7eaSMatthew G. Knepley 
69108e4ac7eaSMatthew G. Knepley   Not collective
69118e4ac7eaSMatthew G. Knepley 
69128e4ac7eaSMatthew G. Knepley   Input Parameter:
69138e4ac7eaSMatthew G. Knepley . dm - The DM
69148e4ac7eaSMatthew G. Knepley 
69158e4ac7eaSMatthew G. Knepley   Output Parameter:
69168e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
69178e4ac7eaSMatthew G. Knepley 
69188e4ac7eaSMatthew G. Knepley   Level: beginner
69198e4ac7eaSMatthew G. Knepley 
69208e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
69218e4ac7eaSMatthew G. Knepley @*/
69228e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
69238e4ac7eaSMatthew G. Knepley {
69248e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
69258e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6926534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
69278e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
69288e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
69298e4ac7eaSMatthew G. Knepley }
69308e4ac7eaSMatthew G. Knepley 
69318e4ac7eaSMatthew G. Knepley /*@
69325d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
69338e4ac7eaSMatthew G. Knepley 
69348e4ac7eaSMatthew G. Knepley   Collective on dm
69358e4ac7eaSMatthew G. Knepley 
69368e4ac7eaSMatthew G. Knepley   Input Parameters:
69378e4ac7eaSMatthew G. Knepley + dm - The DM
69388e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
69398e4ac7eaSMatthew G. Knepley 
69405d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
69415d3b26e6SMatthew G. Knepley 
69428e4ac7eaSMatthew G. Knepley   Level: beginner
69438e4ac7eaSMatthew G. Knepley 
69445d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
69458e4ac7eaSMatthew G. Knepley @*/
69468e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
69478e4ac7eaSMatthew G. Knepley {
69488e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
69498e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69508833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
69518e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
69528e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
69538e4ac7eaSMatthew G. Knepley }
6954c58f1c22SToby Isaac 
6955c58f1c22SToby Isaac 
6956c58f1c22SToby Isaac /*@C
6957c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Not Collective
6960c58f1c22SToby Isaac 
6961c58f1c22SToby Isaac   Input Parameters:
6962c58f1c22SToby Isaac + dm   - The DM object
6963c58f1c22SToby Isaac - name - The label name
6964c58f1c22SToby Isaac 
6965c58f1c22SToby Isaac   Level: intermediate
6966c58f1c22SToby Isaac 
6967c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6968c58f1c22SToby Isaac @*/
6969c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6970c58f1c22SToby Isaac {
69715d80c0bfSVaclav Hapla   PetscBool      flg;
69725d80c0bfSVaclav Hapla   DMLabel        label;
6973c58f1c22SToby Isaac   PetscErrorCode ierr;
6974c58f1c22SToby Isaac 
6975c58f1c22SToby Isaac   PetscFunctionBegin;
6976c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6977c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
69785d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6979c58f1c22SToby Isaac   if (!flg) {
69805d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
69815d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
69825d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6983c58f1c22SToby Isaac   }
6984c58f1c22SToby Isaac   PetscFunctionReturn(0);
6985c58f1c22SToby Isaac }
6986c58f1c22SToby Isaac 
6987c58f1c22SToby Isaac /*@C
6988c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6989c58f1c22SToby Isaac 
6990c58f1c22SToby Isaac   Not Collective
6991c58f1c22SToby Isaac 
6992c58f1c22SToby Isaac   Input Parameters:
6993c58f1c22SToby Isaac + dm   - The DM object
6994c58f1c22SToby Isaac . name - The label name
6995c58f1c22SToby Isaac - point - The mesh point
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   Output Parameter:
6998c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6999c58f1c22SToby Isaac 
7000c58f1c22SToby Isaac   Level: beginner
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7003c58f1c22SToby Isaac @*/
7004c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7005c58f1c22SToby Isaac {
7006c58f1c22SToby Isaac   DMLabel        label;
7007c58f1c22SToby Isaac   PetscErrorCode ierr;
7008c58f1c22SToby Isaac 
7009c58f1c22SToby Isaac   PetscFunctionBegin;
7010c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7011c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7012c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
701313903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7014c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7015c58f1c22SToby Isaac   PetscFunctionReturn(0);
7016c58f1c22SToby Isaac }
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac /*@C
7019c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7020c58f1c22SToby Isaac 
7021c58f1c22SToby Isaac   Not Collective
7022c58f1c22SToby Isaac 
7023c58f1c22SToby Isaac   Input Parameters:
7024c58f1c22SToby Isaac + dm   - The DM object
7025c58f1c22SToby Isaac . name - The label name
7026c58f1c22SToby Isaac . point - The mesh point
7027c58f1c22SToby Isaac - value - The label value for this point
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac   Output Parameter:
7030c58f1c22SToby Isaac 
7031c58f1c22SToby Isaac   Level: beginner
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7034c58f1c22SToby Isaac @*/
7035c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7036c58f1c22SToby Isaac {
7037c58f1c22SToby Isaac   DMLabel        label;
7038c58f1c22SToby Isaac   PetscErrorCode ierr;
7039c58f1c22SToby Isaac 
7040c58f1c22SToby Isaac   PetscFunctionBegin;
7041c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7042c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7043c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7044c58f1c22SToby Isaac   if (!label) {
7045c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7046c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7047c58f1c22SToby Isaac   }
7048c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7049c58f1c22SToby Isaac   PetscFunctionReturn(0);
7050c58f1c22SToby Isaac }
7051c58f1c22SToby Isaac 
7052c58f1c22SToby Isaac /*@C
7053c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac   Not Collective
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Input Parameters:
7058c58f1c22SToby Isaac + dm   - The DM object
7059c58f1c22SToby Isaac . name - The label name
7060c58f1c22SToby Isaac . point - The mesh point
7061c58f1c22SToby Isaac - value - The label value for this point
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   Output Parameter:
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Level: beginner
7066c58f1c22SToby Isaac 
7067c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7068c58f1c22SToby Isaac @*/
7069c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7070c58f1c22SToby Isaac {
7071c58f1c22SToby Isaac   DMLabel        label;
7072c58f1c22SToby Isaac   PetscErrorCode ierr;
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac   PetscFunctionBegin;
7075c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7076c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7077c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7078c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7079c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7080c58f1c22SToby Isaac   PetscFunctionReturn(0);
7081c58f1c22SToby Isaac }
7082c58f1c22SToby Isaac 
7083c58f1c22SToby Isaac /*@C
7084c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   Not Collective
7087c58f1c22SToby Isaac 
7088c58f1c22SToby Isaac   Input Parameters:
7089c58f1c22SToby Isaac + dm   - The DM object
7090c58f1c22SToby Isaac - name - The label name
7091c58f1c22SToby Isaac 
7092c58f1c22SToby Isaac   Output Parameter:
7093c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7094c58f1c22SToby Isaac 
7095c58f1c22SToby Isaac   Level: beginner
7096c58f1c22SToby Isaac 
7097df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7098c58f1c22SToby Isaac @*/
7099c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7100c58f1c22SToby Isaac {
7101c58f1c22SToby Isaac   DMLabel        label;
7102c58f1c22SToby Isaac   PetscErrorCode ierr;
7103c58f1c22SToby Isaac 
7104c58f1c22SToby Isaac   PetscFunctionBegin;
7105c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7106c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7107534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7108c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7109c58f1c22SToby Isaac   *size = 0;
7110c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7111c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7112c58f1c22SToby Isaac   PetscFunctionReturn(0);
7113c58f1c22SToby Isaac }
7114c58f1c22SToby Isaac 
7115c58f1c22SToby Isaac /*@C
7116c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7117c58f1c22SToby Isaac 
7118c58f1c22SToby Isaac   Not Collective
7119c58f1c22SToby Isaac 
7120c58f1c22SToby Isaac   Input Parameters:
7121c58f1c22SToby Isaac + mesh - The DM object
7122c58f1c22SToby Isaac - name - The label name
7123c58f1c22SToby Isaac 
7124c58f1c22SToby Isaac   Output Parameter:
7125c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7126c58f1c22SToby Isaac 
7127c58f1c22SToby Isaac   Level: beginner
7128c58f1c22SToby Isaac 
7129c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7130c58f1c22SToby Isaac @*/
7131c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7132c58f1c22SToby Isaac {
7133c58f1c22SToby Isaac   DMLabel        label;
7134c58f1c22SToby Isaac   PetscErrorCode ierr;
7135c58f1c22SToby Isaac 
7136c58f1c22SToby Isaac   PetscFunctionBegin;
7137c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7138c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7139c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7140c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7141c58f1c22SToby Isaac   *ids = NULL;
7142dab2e251SBlaise Bourdin  if (label) {
7143c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7144dab2e251SBlaise Bourdin   } else {
7145dab2e251SBlaise Bourdin     /* returning an empty IS */
7146dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7147dab2e251SBlaise Bourdin   }
7148c58f1c22SToby Isaac   PetscFunctionReturn(0);
7149c58f1c22SToby Isaac }
7150c58f1c22SToby Isaac 
7151c58f1c22SToby Isaac /*@C
7152c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   Not Collective
7155c58f1c22SToby Isaac 
7156c58f1c22SToby Isaac   Input Parameters:
7157c58f1c22SToby Isaac + dm - The DM object
7158c58f1c22SToby Isaac . name - The label name
7159c58f1c22SToby Isaac - value - The stratum value
7160c58f1c22SToby Isaac 
7161c58f1c22SToby Isaac   Output Parameter:
7162c58f1c22SToby Isaac . size - The stratum size
7163c58f1c22SToby Isaac 
7164c58f1c22SToby Isaac   Level: beginner
7165c58f1c22SToby Isaac 
7166c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7167c58f1c22SToby Isaac @*/
7168c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7169c58f1c22SToby Isaac {
7170c58f1c22SToby Isaac   DMLabel        label;
7171c58f1c22SToby Isaac   PetscErrorCode ierr;
7172c58f1c22SToby Isaac 
7173c58f1c22SToby Isaac   PetscFunctionBegin;
7174c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7175c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7176534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7177c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7178c58f1c22SToby Isaac   *size = 0;
7179c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7180c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7181c58f1c22SToby Isaac   PetscFunctionReturn(0);
7182c58f1c22SToby Isaac }
7183c58f1c22SToby Isaac 
7184c58f1c22SToby Isaac /*@C
7185c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7186c58f1c22SToby Isaac 
7187c58f1c22SToby Isaac   Not Collective
7188c58f1c22SToby Isaac 
7189c58f1c22SToby Isaac   Input Parameters:
7190c58f1c22SToby Isaac + dm - The DM object
7191c58f1c22SToby Isaac . name - The label name
7192c58f1c22SToby Isaac - value - The stratum value
7193c58f1c22SToby Isaac 
7194c58f1c22SToby Isaac   Output Parameter:
7195c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7196c58f1c22SToby Isaac 
7197c58f1c22SToby Isaac   Level: beginner
7198c58f1c22SToby Isaac 
7199c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7200c58f1c22SToby Isaac @*/
7201c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7202c58f1c22SToby Isaac {
7203c58f1c22SToby Isaac   DMLabel        label;
7204c58f1c22SToby Isaac   PetscErrorCode ierr;
7205c58f1c22SToby Isaac 
7206c58f1c22SToby Isaac   PetscFunctionBegin;
7207c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7208c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7209c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7210c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7211c58f1c22SToby Isaac   *points = NULL;
7212c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7213c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7214c58f1c22SToby Isaac   PetscFunctionReturn(0);
7215c58f1c22SToby Isaac }
7216c58f1c22SToby Isaac 
72174de306b1SToby Isaac /*@C
72189044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
72194de306b1SToby Isaac 
72204de306b1SToby Isaac   Not Collective
72214de306b1SToby Isaac 
72224de306b1SToby Isaac   Input Parameters:
72234de306b1SToby Isaac + dm - The DM object
72244de306b1SToby Isaac . name - The label name
72254de306b1SToby Isaac . value - The stratum value
72264de306b1SToby Isaac - points - The stratum points
72274de306b1SToby Isaac 
72284de306b1SToby Isaac   Level: beginner
72294de306b1SToby Isaac 
72304de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
72314de306b1SToby Isaac @*/
72324de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
72334de306b1SToby Isaac {
72344de306b1SToby Isaac   DMLabel        label;
72354de306b1SToby Isaac   PetscErrorCode ierr;
72364de306b1SToby Isaac 
72374de306b1SToby Isaac   PetscFunctionBegin;
72384de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72394de306b1SToby Isaac   PetscValidCharPointer(name, 2);
72404de306b1SToby Isaac   PetscValidPointer(points, 4);
72414de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
72424de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
72434de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
72444de306b1SToby Isaac   PetscFunctionReturn(0);
72454de306b1SToby Isaac }
72464de306b1SToby Isaac 
7247c58f1c22SToby Isaac /*@C
7248c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7249c58f1c22SToby Isaac 
7250c58f1c22SToby Isaac   Not Collective
7251c58f1c22SToby Isaac 
7252c58f1c22SToby Isaac   Input Parameters:
7253c58f1c22SToby Isaac + dm   - The DM object
7254c58f1c22SToby Isaac . name - The label name
7255c58f1c22SToby Isaac - value - The label value for this point
7256c58f1c22SToby Isaac 
7257c58f1c22SToby Isaac   Output Parameter:
7258c58f1c22SToby Isaac 
7259c58f1c22SToby Isaac   Level: beginner
7260c58f1c22SToby Isaac 
7261c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7262c58f1c22SToby Isaac @*/
7263c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7264c58f1c22SToby Isaac {
7265c58f1c22SToby Isaac   DMLabel        label;
7266c58f1c22SToby Isaac   PetscErrorCode ierr;
7267c58f1c22SToby Isaac 
7268c58f1c22SToby Isaac   PetscFunctionBegin;
7269c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7270c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7271c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7272c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7273c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7274c58f1c22SToby Isaac   PetscFunctionReturn(0);
7275c58f1c22SToby Isaac }
7276c58f1c22SToby Isaac 
7277c58f1c22SToby Isaac /*@
7278c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7279c58f1c22SToby Isaac 
7280c58f1c22SToby Isaac   Not Collective
7281c58f1c22SToby Isaac 
7282c58f1c22SToby Isaac   Input Parameter:
7283c58f1c22SToby Isaac . dm   - The DM object
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac   Output Parameter:
7286c58f1c22SToby Isaac . numLabels - the number of Labels
7287c58f1c22SToby Isaac 
7288c58f1c22SToby Isaac   Level: intermediate
7289c58f1c22SToby Isaac 
7290c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7291c58f1c22SToby Isaac @*/
7292c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7293c58f1c22SToby Isaac {
72945d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7295c58f1c22SToby Isaac   PetscInt  n    = 0;
7296c58f1c22SToby Isaac 
7297c58f1c22SToby Isaac   PetscFunctionBegin;
7298c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7299534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7300c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7301c58f1c22SToby Isaac   *numLabels = n;
7302c58f1c22SToby Isaac   PetscFunctionReturn(0);
7303c58f1c22SToby Isaac }
7304c58f1c22SToby Isaac 
7305c58f1c22SToby Isaac /*@C
7306c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7307c58f1c22SToby Isaac 
7308c58f1c22SToby Isaac   Not Collective
7309c58f1c22SToby Isaac 
7310c58f1c22SToby Isaac   Input Parameters:
7311c58f1c22SToby Isaac + dm - The DM object
7312c58f1c22SToby Isaac - n  - the label number
7313c58f1c22SToby Isaac 
7314c58f1c22SToby Isaac   Output Parameter:
7315c58f1c22SToby Isaac . name - the label name
7316c58f1c22SToby Isaac 
7317c58f1c22SToby Isaac   Level: intermediate
7318c58f1c22SToby Isaac 
7319c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7320c58f1c22SToby Isaac @*/
7321c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7322c58f1c22SToby Isaac {
73235d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7324c58f1c22SToby Isaac   PetscInt       l    = 0;
7325d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7326c58f1c22SToby Isaac 
7327c58f1c22SToby Isaac   PetscFunctionBegin;
7328c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7329c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7330c58f1c22SToby Isaac   while (next) {
7331c58f1c22SToby Isaac     if (l == n) {
7332d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7333c58f1c22SToby Isaac       PetscFunctionReturn(0);
7334c58f1c22SToby Isaac     }
7335c58f1c22SToby Isaac     ++l;
7336c58f1c22SToby Isaac     next = next->next;
7337c58f1c22SToby Isaac   }
7338c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7339c58f1c22SToby Isaac }
7340c58f1c22SToby Isaac 
7341c58f1c22SToby Isaac /*@C
7342c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac   Not Collective
7345c58f1c22SToby Isaac 
7346c58f1c22SToby Isaac   Input Parameters:
7347c58f1c22SToby Isaac + dm   - The DM object
7348c58f1c22SToby Isaac - name - The label name
7349c58f1c22SToby Isaac 
7350c58f1c22SToby Isaac   Output Parameter:
7351c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7352c58f1c22SToby Isaac 
7353c58f1c22SToby Isaac   Level: intermediate
7354c58f1c22SToby Isaac 
7355c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7356c58f1c22SToby Isaac @*/
7357c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7358c58f1c22SToby Isaac {
73595d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7360d67d17b1SMatthew G. Knepley   const char    *lname;
7361c58f1c22SToby Isaac   PetscErrorCode ierr;
7362c58f1c22SToby Isaac 
7363c58f1c22SToby Isaac   PetscFunctionBegin;
7364c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7365c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7366534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7367c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7368c58f1c22SToby Isaac   while (next) {
7369d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7370d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7371c58f1c22SToby Isaac     if (*hasLabel) break;
7372c58f1c22SToby Isaac     next = next->next;
7373c58f1c22SToby Isaac   }
7374c58f1c22SToby Isaac   PetscFunctionReturn(0);
7375c58f1c22SToby Isaac }
7376c58f1c22SToby Isaac 
7377c58f1c22SToby Isaac /*@C
7378c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7379c58f1c22SToby Isaac 
7380c58f1c22SToby Isaac   Not Collective
7381c58f1c22SToby Isaac 
7382c58f1c22SToby Isaac   Input Parameters:
7383c58f1c22SToby Isaac + dm   - The DM object
7384c58f1c22SToby Isaac - name - The label name
7385c58f1c22SToby Isaac 
7386c58f1c22SToby Isaac   Output Parameter:
7387c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7388c58f1c22SToby Isaac 
7389c58f1c22SToby Isaac   Level: intermediate
7390c58f1c22SToby Isaac 
7391c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7392c58f1c22SToby Isaac @*/
7393c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7394c58f1c22SToby Isaac {
73955d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7396c58f1c22SToby Isaac   PetscBool      hasLabel;
7397d67d17b1SMatthew G. Knepley   const char    *lname;
7398c58f1c22SToby Isaac   PetscErrorCode ierr;
7399c58f1c22SToby Isaac 
7400c58f1c22SToby Isaac   PetscFunctionBegin;
7401c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7402c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7403c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7404c58f1c22SToby Isaac   *label = NULL;
7405c58f1c22SToby Isaac   while (next) {
7406d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7407d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7408c58f1c22SToby Isaac     if (hasLabel) {
7409c58f1c22SToby Isaac       *label = next->label;
7410c58f1c22SToby Isaac       break;
7411c58f1c22SToby Isaac     }
7412c58f1c22SToby Isaac     next = next->next;
7413c58f1c22SToby Isaac   }
7414c58f1c22SToby Isaac   PetscFunctionReturn(0);
7415c58f1c22SToby Isaac }
7416c58f1c22SToby Isaac 
7417c58f1c22SToby Isaac /*@C
7418c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7419c58f1c22SToby Isaac 
7420c58f1c22SToby Isaac   Not Collective
7421c58f1c22SToby Isaac 
7422c58f1c22SToby Isaac   Input Parameters:
7423c58f1c22SToby Isaac + dm - The DM object
7424c58f1c22SToby Isaac - n  - the label number
7425c58f1c22SToby Isaac 
7426c58f1c22SToby Isaac   Output Parameter:
7427c58f1c22SToby Isaac . label - the label
7428c58f1c22SToby Isaac 
7429c58f1c22SToby Isaac   Level: intermediate
7430c58f1c22SToby Isaac 
7431c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7432c58f1c22SToby Isaac @*/
7433c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7434c58f1c22SToby Isaac {
74355d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7436c58f1c22SToby Isaac   PetscInt    l    = 0;
7437c58f1c22SToby Isaac 
7438c58f1c22SToby Isaac   PetscFunctionBegin;
7439c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7440c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7441c58f1c22SToby Isaac   while (next) {
7442c58f1c22SToby Isaac     if (l == n) {
7443c58f1c22SToby Isaac       *label = next->label;
7444c58f1c22SToby Isaac       PetscFunctionReturn(0);
7445c58f1c22SToby Isaac     }
7446c58f1c22SToby Isaac     ++l;
7447c58f1c22SToby Isaac     next = next->next;
7448c58f1c22SToby Isaac   }
7449c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7450c58f1c22SToby Isaac }
7451c58f1c22SToby Isaac 
7452c58f1c22SToby Isaac /*@C
7453c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7454c58f1c22SToby Isaac 
7455c58f1c22SToby Isaac   Not Collective
7456c58f1c22SToby Isaac 
7457c58f1c22SToby Isaac   Input Parameters:
7458c58f1c22SToby Isaac + dm   - The DM object
7459c58f1c22SToby Isaac - label - The DMLabel
7460c58f1c22SToby Isaac 
7461c58f1c22SToby Isaac   Level: developer
7462c58f1c22SToby Isaac 
7463c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7464c58f1c22SToby Isaac @*/
7465c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7466c58f1c22SToby Isaac {
74675d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7468c58f1c22SToby Isaac   PetscBool      hasLabel;
7469d67d17b1SMatthew G. Knepley   const char    *lname;
74705d80c0bfSVaclav Hapla   PetscBool      flg;
7471c58f1c22SToby Isaac   PetscErrorCode ierr;
7472c58f1c22SToby Isaac 
7473c58f1c22SToby Isaac   PetscFunctionBegin;
7474c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7475d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7476d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7477d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7478c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7479c58f1c22SToby Isaac   tmpLabel->label  = label;
7480c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
74815d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
74825d80c0bfSVaclav Hapla   *p = tmpLabel;
748308f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
74845d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
74855d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7486ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7487ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7488c58f1c22SToby Isaac   PetscFunctionReturn(0);
7489c58f1c22SToby Isaac }
7490c58f1c22SToby Isaac 
7491c58f1c22SToby Isaac /*@C
7492e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7493c58f1c22SToby Isaac 
7494c58f1c22SToby Isaac   Not Collective
7495c58f1c22SToby Isaac 
7496c58f1c22SToby Isaac   Input Parameters:
7497c58f1c22SToby Isaac + dm   - The DM object
7498c58f1c22SToby Isaac - name - The label name
7499c58f1c22SToby Isaac 
7500c58f1c22SToby Isaac   Output Parameter:
7501c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7502c58f1c22SToby Isaac 
7503c58f1c22SToby Isaac   Level: developer
7504c58f1c22SToby Isaac 
7505e5472504SVaclav Hapla   Notes:
7506e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7507e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7508e5472504SVaclav Hapla 
7509e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7510e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7511e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7512e5472504SVaclav Hapla 
7513e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7514c58f1c22SToby Isaac @*/
7515c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7516c58f1c22SToby Isaac {
751795d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7518c58f1c22SToby Isaac   PetscBool      hasLabel;
7519d67d17b1SMatthew G. Knepley   const char    *lname;
7520c58f1c22SToby Isaac   PetscErrorCode ierr;
7521c58f1c22SToby Isaac 
7522c58f1c22SToby Isaac   PetscFunctionBegin;
7523c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7524e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7525e5472504SVaclav Hapla   if (label) {
7526e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7527c58f1c22SToby Isaac     *label = NULL;
7528e5472504SVaclav Hapla   }
75295d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
753095d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7531d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7532c58f1c22SToby Isaac     if (hasLabel) {
753395d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7534c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
753595d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7536ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7537ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
753895d578d6SVaclav Hapla       if (label) *label = link->label;
753995d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
754095d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7541c58f1c22SToby Isaac       break;
7542c58f1c22SToby Isaac     }
7543c58f1c22SToby Isaac   }
7544c58f1c22SToby Isaac   PetscFunctionReturn(0);
7545c58f1c22SToby Isaac }
7546c58f1c22SToby Isaac 
7547306894acSVaclav Hapla /*@
7548306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7549306894acSVaclav Hapla 
7550306894acSVaclav Hapla   Not Collective
7551306894acSVaclav Hapla 
7552306894acSVaclav Hapla   Input Parameters:
7553306894acSVaclav Hapla + dm   - The DM object
7554306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7555306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7556306894acSVaclav Hapla 
7557306894acSVaclav Hapla   Level: developer
7558306894acSVaclav Hapla 
7559306894acSVaclav Hapla   Notes:
7560306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7561306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7562306894acSVaclav Hapla   *label nullified.
7563306894acSVaclav Hapla 
7564306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7565306894acSVaclav Hapla @*/
7566306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7567306894acSVaclav Hapla {
756843e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7569306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7570306894acSVaclav Hapla   PetscErrorCode ierr;
7571306894acSVaclav Hapla 
7572306894acSVaclav Hapla   PetscFunctionBegin;
7573306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7574306894acSVaclav Hapla   PetscValidPointer(label, 2);
7575f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7576306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7577306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
75785d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
757943e45a93SVaclav Hapla     if (*label == link->label) {
7580306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
758143e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7582306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7583ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
758443e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
758543e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
758643e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7587306894acSVaclav Hapla       break;
7588306894acSVaclav Hapla     }
7589306894acSVaclav Hapla   }
7590306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7591306894acSVaclav Hapla   PetscFunctionReturn(0);
7592306894acSVaclav Hapla }
7593306894acSVaclav Hapla 
7594c58f1c22SToby Isaac /*@C
7595c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7596c58f1c22SToby Isaac 
7597c58f1c22SToby Isaac   Not Collective
7598c58f1c22SToby Isaac 
7599c58f1c22SToby Isaac   Input Parameters:
7600c58f1c22SToby Isaac + dm   - The DM object
7601c58f1c22SToby Isaac - name - The label name
7602c58f1c22SToby Isaac 
7603c58f1c22SToby Isaac   Output Parameter:
7604c58f1c22SToby Isaac . output - The flag for output
7605c58f1c22SToby Isaac 
7606c58f1c22SToby Isaac   Level: developer
7607c58f1c22SToby Isaac 
7608c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7609c58f1c22SToby Isaac @*/
7610c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7611c58f1c22SToby Isaac {
76125d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7613d67d17b1SMatthew G. Knepley   const char    *lname;
7614c58f1c22SToby Isaac   PetscErrorCode ierr;
7615c58f1c22SToby Isaac 
7616c58f1c22SToby Isaac   PetscFunctionBegin;
7617c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7618c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7619c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7620c58f1c22SToby Isaac   while (next) {
7621c58f1c22SToby Isaac     PetscBool flg;
7622c58f1c22SToby Isaac 
7623d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7624d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7625c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7626c58f1c22SToby Isaac     next = next->next;
7627c58f1c22SToby Isaac   }
7628c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7629c58f1c22SToby Isaac }
7630c58f1c22SToby Isaac 
7631c58f1c22SToby Isaac /*@C
7632c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7633c58f1c22SToby Isaac 
7634c58f1c22SToby Isaac   Not Collective
7635c58f1c22SToby Isaac 
7636c58f1c22SToby Isaac   Input Parameters:
7637c58f1c22SToby Isaac + dm     - The DM object
7638c58f1c22SToby Isaac . name   - The label name
7639c58f1c22SToby Isaac - output - The flag for output
7640c58f1c22SToby Isaac 
7641c58f1c22SToby Isaac   Level: developer
7642c58f1c22SToby Isaac 
7643c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7644c58f1c22SToby Isaac @*/
7645c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7646c58f1c22SToby Isaac {
76475d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7648d67d17b1SMatthew G. Knepley   const char    *lname;
7649c58f1c22SToby Isaac   PetscErrorCode ierr;
7650c58f1c22SToby Isaac 
7651c58f1c22SToby Isaac   PetscFunctionBegin;
7652c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7653534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7654c58f1c22SToby Isaac   while (next) {
7655c58f1c22SToby Isaac     PetscBool flg;
7656c58f1c22SToby Isaac 
7657d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7658d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7659c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7660c58f1c22SToby Isaac     next = next->next;
7661c58f1c22SToby Isaac   }
7662c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7663c58f1c22SToby Isaac }
7664c58f1c22SToby Isaac 
7665c58f1c22SToby Isaac /*@
7666c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7667c58f1c22SToby Isaac 
7668d083f849SBarry Smith   Collective on dmA
7669c58f1c22SToby Isaac 
7670c58f1c22SToby Isaac   Input Parameter:
76715d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
76722e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
76735d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7674ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7675c58f1c22SToby Isaac 
7676c58f1c22SToby Isaac   Level: intermediate
7677c58f1c22SToby Isaac 
7678c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7679c58f1c22SToby Isaac 
76805d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7681c58f1c22SToby Isaac @*/
76825d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7683c58f1c22SToby Isaac {
7684c58f1c22SToby Isaac   DMLabel        label, labelNew;
7685c58f1c22SToby Isaac   const char    *name;
7686c58f1c22SToby Isaac   PetscBool      flg;
76875d80c0bfSVaclav Hapla   DMLabelLink    link;
76885d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7689c58f1c22SToby Isaac 
76905d80c0bfSVaclav Hapla   PetscFunctionBegin;
76915d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
76925d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
76935d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
76945d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
76955d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
76965d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
76975d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
76985d80c0bfSVaclav Hapla     label=link->label;
76995d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
77005d80c0bfSVaclav Hapla     if (!all) {
7701c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7702c58f1c22SToby Isaac       if (flg) continue;
77037d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
77047d5acc75SStefano Zampini       if (flg) continue;
7705ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7706ba2698f1SMatthew G. Knepley       if (flg) continue;
77075d80c0bfSVaclav Hapla     }
77085d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7709c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
77105d80c0bfSVaclav Hapla     } else {
77115d80c0bfSVaclav Hapla       labelNew = label;
77125d80c0bfSVaclav Hapla     }
7713c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
77145d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7715c58f1c22SToby Isaac   }
7716c58f1c22SToby Isaac   PetscFunctionReturn(0);
7717c58f1c22SToby Isaac }
7718a8fb8f29SToby Isaac 
7719a8fb8f29SToby Isaac /*@
7720a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7721a8fb8f29SToby Isaac 
7722a8fb8f29SToby Isaac   Input Parameter:
7723a8fb8f29SToby Isaac . dm - The DM object
7724a8fb8f29SToby Isaac 
7725a8fb8f29SToby Isaac   Output Parameter:
7726a8fb8f29SToby Isaac . cdm - The coarse DM
7727a8fb8f29SToby Isaac 
7728a8fb8f29SToby Isaac   Level: intermediate
7729a8fb8f29SToby Isaac 
7730a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7731a8fb8f29SToby Isaac @*/
7732a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7733a8fb8f29SToby Isaac {
7734a8fb8f29SToby Isaac   PetscFunctionBegin;
7735a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7736a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7737a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7738a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7739a8fb8f29SToby Isaac }
7740a8fb8f29SToby Isaac 
7741a8fb8f29SToby Isaac /*@
7742a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7743a8fb8f29SToby Isaac 
7744a8fb8f29SToby Isaac   Input Parameters:
7745a8fb8f29SToby Isaac + dm - The DM object
7746a8fb8f29SToby Isaac - cdm - The coarse DM
7747a8fb8f29SToby Isaac 
7748a8fb8f29SToby Isaac   Level: intermediate
7749a8fb8f29SToby Isaac 
7750a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7751a8fb8f29SToby Isaac @*/
7752a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7753a8fb8f29SToby Isaac {
7754a8fb8f29SToby Isaac   PetscErrorCode ierr;
7755a8fb8f29SToby Isaac 
7756a8fb8f29SToby Isaac   PetscFunctionBegin;
7757a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7758a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7759a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7760a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7761a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7762a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7763a8fb8f29SToby Isaac }
7764a8fb8f29SToby Isaac 
776588bdff64SToby Isaac /*@
776688bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
776788bdff64SToby Isaac 
776888bdff64SToby Isaac   Input Parameter:
776988bdff64SToby Isaac . dm - The DM object
777088bdff64SToby Isaac 
777188bdff64SToby Isaac   Output Parameter:
777288bdff64SToby Isaac . fdm - The fine DM
777388bdff64SToby Isaac 
777488bdff64SToby Isaac   Level: intermediate
777588bdff64SToby Isaac 
777688bdff64SToby Isaac .seealso: DMSetFineDM()
777788bdff64SToby Isaac @*/
777888bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
777988bdff64SToby Isaac {
778088bdff64SToby Isaac   PetscFunctionBegin;
778188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
778288bdff64SToby Isaac   PetscValidPointer(fdm, 2);
778388bdff64SToby Isaac   *fdm = dm->fineMesh;
778488bdff64SToby Isaac   PetscFunctionReturn(0);
778588bdff64SToby Isaac }
778688bdff64SToby Isaac 
778788bdff64SToby Isaac /*@
778888bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
778988bdff64SToby Isaac 
779088bdff64SToby Isaac   Input Parameters:
779188bdff64SToby Isaac + dm - The DM object
779288bdff64SToby Isaac - fdm - The fine DM
779388bdff64SToby Isaac 
779488bdff64SToby Isaac   Level: intermediate
779588bdff64SToby Isaac 
779688bdff64SToby Isaac .seealso: DMGetFineDM()
779788bdff64SToby Isaac @*/
779888bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
779988bdff64SToby Isaac {
780088bdff64SToby Isaac   PetscErrorCode ierr;
780188bdff64SToby Isaac 
780288bdff64SToby Isaac   PetscFunctionBegin;
780388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
780488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
780588bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
780688bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
780788bdff64SToby Isaac   dm->fineMesh = fdm;
780888bdff64SToby Isaac   PetscFunctionReturn(0);
780988bdff64SToby Isaac }
781088bdff64SToby Isaac 
7811a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7812a6ba4734SToby Isaac 
7813a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7814a6ba4734SToby Isaac {
7815e5e52638SMatthew G. Knepley   PetscInt       d;
7816a6ba4734SToby Isaac   PetscErrorCode ierr;
7817a6ba4734SToby Isaac 
7818a6ba4734SToby Isaac   PetscFunctionBegin;
7819e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7820e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7821e5e52638SMatthew G. Knepley   }
7822a6ba4734SToby Isaac   PetscFunctionReturn(0);
7823a6ba4734SToby Isaac }
7824a6ba4734SToby Isaac 
7825a6ba4734SToby Isaac /*@C
7826a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7827a6ba4734SToby Isaac 
7828783e2ec8SMatthew G. Knepley   Collective on dm
7829783e2ec8SMatthew G. Knepley 
7830a6ba4734SToby Isaac   Input Parameters:
78314c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7832f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7833a6ba4734SToby Isaac . name        - The BC name
7834a6ba4734SToby Isaac . labelname   - The label defining constrained points
7835a6ba4734SToby Isaac . field       - The field to constrain
7836e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7837a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7838a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7839a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7840a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7841a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7842a6ba4734SToby Isaac 
7843a6ba4734SToby Isaac   Options Database Keys:
7844a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7845a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7846a6ba4734SToby Isaac 
7847a6ba4734SToby Isaac   Level: developer
7848a6ba4734SToby Isaac 
7849a6ba4734SToby Isaac .seealso: DMGetBoundary()
7850a6ba4734SToby Isaac @*/
7851a30ec4eaSSatish Balay PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(void), PetscInt numids, const PetscInt *ids, void *ctx)
7852a6ba4734SToby Isaac {
7853e5e52638SMatthew G. Knepley   PetscDS        ds;
7854a6ba4734SToby Isaac   PetscErrorCode ierr;
7855a6ba4734SToby Isaac 
7856a6ba4734SToby Isaac   PetscFunctionBegin;
7857a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7858783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
7859783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
7860783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
7861783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
7862e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7863783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
7864e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7865a6ba4734SToby Isaac   PetscFunctionReturn(0);
7866a6ba4734SToby Isaac }
7867a6ba4734SToby Isaac 
7868a6ba4734SToby Isaac /*@
7869a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7870a6ba4734SToby Isaac 
7871a6ba4734SToby Isaac   Input Parameters:
7872a6ba4734SToby Isaac . dm - The mesh object
7873a6ba4734SToby Isaac 
7874a6ba4734SToby Isaac   Output Parameters:
7875a6ba4734SToby Isaac . numBd - The number of BC
7876a6ba4734SToby Isaac 
7877a6ba4734SToby Isaac   Level: intermediate
7878a6ba4734SToby Isaac 
7879a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7880a6ba4734SToby Isaac @*/
7881a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7882a6ba4734SToby Isaac {
7883e5e52638SMatthew G. Knepley   PetscDS        ds;
788458ebd649SToby Isaac   PetscErrorCode ierr;
7885a6ba4734SToby Isaac 
7886a6ba4734SToby Isaac   PetscFunctionBegin;
7887a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7888e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7889e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7890a6ba4734SToby Isaac   PetscFunctionReturn(0);
7891a6ba4734SToby Isaac }
7892a6ba4734SToby Isaac 
7893a6ba4734SToby Isaac /*@C
78941c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7895a6ba4734SToby Isaac 
7896a6ba4734SToby Isaac   Input Parameters:
7897a6ba4734SToby Isaac + dm          - The mesh object
7898a6ba4734SToby Isaac - bd          - The BC number
7899a6ba4734SToby Isaac 
7900a6ba4734SToby Isaac   Output Parameters:
7901f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7902a6ba4734SToby Isaac . name        - The BC name
7903a6ba4734SToby Isaac . labelname   - The label defining constrained points
7904a6ba4734SToby Isaac . field       - The field to constrain
7905a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7906a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7907a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7908a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7909a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7910a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7911a6ba4734SToby Isaac 
7912a6ba4734SToby Isaac   Options Database Keys:
7913a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7914a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7915a6ba4734SToby Isaac 
7916a6ba4734SToby Isaac   Level: developer
7917a6ba4734SToby Isaac 
7918a6ba4734SToby Isaac .seealso: DMAddBoundary()
7919a6ba4734SToby Isaac @*/
7920a30ec4eaSSatish Balay PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(void), PetscInt *numids, const PetscInt **ids, void **ctx)
7921a6ba4734SToby Isaac {
7922e5e52638SMatthew G. Knepley   PetscDS        ds;
792358ebd649SToby Isaac   PetscErrorCode ierr;
7924a6ba4734SToby Isaac 
7925a6ba4734SToby Isaac   PetscFunctionBegin;
7926a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7927e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7928e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7929a6ba4734SToby Isaac   PetscFunctionReturn(0);
7930a6ba4734SToby Isaac }
7931a6ba4734SToby Isaac 
7932e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7933e6f8dbb6SToby Isaac {
7934e5e52638SMatthew G. Knepley   PetscDS        ds;
7935dff059c6SToby Isaac   DMBoundary    *lastnext;
7936e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7937e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7938e6f8dbb6SToby Isaac 
7939e6f8dbb6SToby Isaac   PetscFunctionBegin;
7940e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7941e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
794247a1f5adSToby Isaac   if (dm->boundary) {
794347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
794447a1f5adSToby Isaac 
794547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
794647a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
794747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
794847a1f5adSToby Isaac     while (next) {
794947a1f5adSToby Isaac       DMBoundary b = next;
795047a1f5adSToby Isaac 
795147a1f5adSToby Isaac       next = b->next;
795247a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7953a6ba4734SToby Isaac     }
795447a1f5adSToby Isaac     dm->boundary = NULL;
7955a6ba4734SToby Isaac   }
795647a1f5adSToby Isaac 
7957dff059c6SToby Isaac   lastnext = &(dm->boundary);
7958e6f8dbb6SToby Isaac   while (dsbound) {
7959e6f8dbb6SToby Isaac     DMBoundary dmbound;
7960e6f8dbb6SToby Isaac 
7961e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7962e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7963e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7964994fe344SLisandro Dalcin     if (!dmbound->label) {ierr = PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);}
796547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7966dff059c6SToby Isaac     *lastnext = dmbound;
7967dff059c6SToby Isaac     lastnext = &(dmbound->next);
7968dff059c6SToby Isaac     dsbound = dsbound->next;
7969a6ba4734SToby Isaac   }
7970a6ba4734SToby Isaac   PetscFunctionReturn(0);
7971a6ba4734SToby Isaac }
7972a6ba4734SToby Isaac 
7973a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7974a6ba4734SToby Isaac {
7975b95f2879SToby Isaac   DMBoundary     b;
7976a6ba4734SToby Isaac   PetscErrorCode ierr;
7977a6ba4734SToby Isaac 
7978a6ba4734SToby Isaac   PetscFunctionBegin;
7979a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7980534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7981a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7982e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7983b95f2879SToby Isaac   b = dm->boundary;
7984a6ba4734SToby Isaac   while (b && !(*isBd)) {
7985e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7986e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
79873424c85cSToby Isaac 
79883424c85cSToby Isaac     if (label) {
7989a6ba4734SToby Isaac       PetscInt i;
7990a6ba4734SToby Isaac 
7991e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7992e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7993a6ba4734SToby Isaac       }
7994a6ba4734SToby Isaac     }
7995a6ba4734SToby Isaac     b = b->next;
7996a6ba4734SToby Isaac   }
7997a6ba4734SToby Isaac   PetscFunctionReturn(0);
7998a6ba4734SToby Isaac }
79994d6f44ffSToby Isaac 
80004d6f44ffSToby Isaac /*@C
8001a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8002a6e0b375SMatthew G. Knepley 
8003a6e0b375SMatthew G. Knepley   Collective on DM
80044d6f44ffSToby Isaac 
80054d6f44ffSToby Isaac   Input Parameters:
80064d6f44ffSToby Isaac + dm      - The DM
80070709b2feSToby Isaac . time    - The time
80084d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
80094d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
80104d6f44ffSToby Isaac - mode    - The insertion mode for values
80114d6f44ffSToby Isaac 
80124d6f44ffSToby Isaac   Output Parameter:
80134d6f44ffSToby Isaac . X - vector
80144d6f44ffSToby Isaac 
80154d6f44ffSToby Isaac    Calling sequence of func:
80160709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
80174d6f44ffSToby Isaac 
80184d6f44ffSToby Isaac +  dim - The spatial dimension
80194d6f44ffSToby Isaac .  x   - The coordinates
80204d6f44ffSToby Isaac .  Nf  - The number of fields
80214d6f44ffSToby Isaac .  u   - The output field values
80224d6f44ffSToby Isaac -  ctx - optional user-defined function context
80234d6f44ffSToby Isaac 
80244d6f44ffSToby Isaac   Level: developer
80254d6f44ffSToby Isaac 
8026a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
80274d6f44ffSToby Isaac @*/
80280709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
80294d6f44ffSToby Isaac {
80304d6f44ffSToby Isaac   Vec            localX;
80314d6f44ffSToby Isaac   PetscErrorCode ierr;
80324d6f44ffSToby Isaac 
80334d6f44ffSToby Isaac   PetscFunctionBegin;
80344d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80354d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
80360709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
80374d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
80384d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
80394d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
80404d6f44ffSToby Isaac   PetscFunctionReturn(0);
80414d6f44ffSToby Isaac }
80424d6f44ffSToby Isaac 
8043a6e0b375SMatthew G. Knepley /*@C
8044a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8045a6e0b375SMatthew G. Knepley 
8046a6e0b375SMatthew G. Knepley   Not collective
8047a6e0b375SMatthew G. Knepley 
8048a6e0b375SMatthew G. Knepley   Input Parameters:
8049a6e0b375SMatthew G. Knepley + dm      - The DM
8050a6e0b375SMatthew G. Knepley . time    - The time
8051a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8052a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8053a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8054a6e0b375SMatthew G. Knepley 
8055a6e0b375SMatthew G. Knepley   Output Parameter:
8056a6e0b375SMatthew G. Knepley . localX - vector
8057a6e0b375SMatthew G. Knepley 
8058a6e0b375SMatthew G. Knepley    Calling sequence of func:
8059a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8060a6e0b375SMatthew G. Knepley 
8061a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8062a6e0b375SMatthew G. Knepley .  x   - The coordinates
8063a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8064a6e0b375SMatthew G. Knepley .  u   - The output field values
8065a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8066a6e0b375SMatthew G. Knepley 
8067a6e0b375SMatthew G. Knepley   Level: developer
8068a6e0b375SMatthew G. Knepley 
8069a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8070a6e0b375SMatthew G. Knepley @*/
80710709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
80724d6f44ffSToby Isaac {
80734d6f44ffSToby Isaac   PetscErrorCode ierr;
80744d6f44ffSToby Isaac 
80754d6f44ffSToby Isaac   PetscFunctionBegin;
80764d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80774d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
80780918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
80790709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
80804d6f44ffSToby Isaac   PetscFunctionReturn(0);
80814d6f44ffSToby Isaac }
80824d6f44ffSToby Isaac 
8083a6e0b375SMatthew G. Knepley /*@C
8084a6e0b375SMatthew 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.
8085a6e0b375SMatthew G. Knepley 
8086a6e0b375SMatthew G. Knepley   Collective on DM
8087a6e0b375SMatthew G. Knepley 
8088a6e0b375SMatthew G. Knepley   Input Parameters:
8089a6e0b375SMatthew G. Knepley + dm      - The DM
8090a6e0b375SMatthew G. Knepley . time    - The time
8091a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8092a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8093a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8094a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8095a6e0b375SMatthew G. Knepley 
8096a6e0b375SMatthew G. Knepley   Output Parameter:
8097a6e0b375SMatthew G. Knepley . X - vector
8098a6e0b375SMatthew G. Knepley 
8099a6e0b375SMatthew G. Knepley    Calling sequence of func:
8100a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8101a6e0b375SMatthew G. Knepley 
8102a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8103a6e0b375SMatthew G. Knepley .  x   - The coordinates
8104a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8105a6e0b375SMatthew G. Knepley .  u   - The output field values
8106a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8107a6e0b375SMatthew G. Knepley 
8108a6e0b375SMatthew G. Knepley   Level: developer
8109a6e0b375SMatthew G. Knepley 
8110a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8111a6e0b375SMatthew G. Knepley @*/
81122c53366bSMatthew 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)
81132c53366bSMatthew G. Knepley {
81142c53366bSMatthew G. Knepley   Vec            localX;
81152c53366bSMatthew G. Knepley   PetscErrorCode ierr;
81162c53366bSMatthew G. Knepley 
81172c53366bSMatthew G. Knepley   PetscFunctionBegin;
81182c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81192c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
81202c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
81212c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
81222c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
81232c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
81242c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
81252c53366bSMatthew G. Knepley }
81262c53366bSMatthew G. Knepley 
8127a6e0b375SMatthew G. Knepley /*@C
8128a6e0b375SMatthew 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.
8129a6e0b375SMatthew G. Knepley 
8130a6e0b375SMatthew G. Knepley   Not collective
8131a6e0b375SMatthew G. Knepley 
8132a6e0b375SMatthew G. Knepley   Input Parameters:
8133a6e0b375SMatthew G. Knepley + dm      - The DM
8134a6e0b375SMatthew G. Knepley . time    - The time
8135a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8136a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8137a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8138a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8139a6e0b375SMatthew G. Knepley 
8140a6e0b375SMatthew G. Knepley   Output Parameter:
8141a6e0b375SMatthew G. Knepley . localX - vector
8142a6e0b375SMatthew G. Knepley 
8143a6e0b375SMatthew G. Knepley    Calling sequence of func:
8144a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8145a6e0b375SMatthew G. Knepley 
8146a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8147a6e0b375SMatthew G. Knepley .  x   - The coordinates
8148a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8149a6e0b375SMatthew G. Knepley .  u   - The output field values
8150a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8151a6e0b375SMatthew G. Knepley 
8152a6e0b375SMatthew G. Knepley   Level: developer
8153a6e0b375SMatthew G. Knepley 
8154a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8155a6e0b375SMatthew G. Knepley @*/
81561c531cf8SMatthew 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)
81574d6f44ffSToby Isaac {
81584d6f44ffSToby Isaac   PetscErrorCode ierr;
81594d6f44ffSToby Isaac 
81604d6f44ffSToby Isaac   PetscFunctionBegin;
81614d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81624d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
81630918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
81641c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
81654d6f44ffSToby Isaac   PetscFunctionReturn(0);
81664d6f44ffSToby Isaac }
81672716604bSToby Isaac 
8168a6e0b375SMatthew G. Knepley /*@C
8169a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8170a6e0b375SMatthew G. Knepley 
8171a6e0b375SMatthew G. Knepley   Not collective
8172a6e0b375SMatthew G. Knepley 
8173a6e0b375SMatthew G. Knepley   Input Parameters:
8174a6e0b375SMatthew G. Knepley + dm      - The DM
8175a6e0b375SMatthew G. Knepley . time    - The time
8176a6e0b375SMatthew G. Knepley . localU  - The input field vector
8177a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8178a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8179a6e0b375SMatthew G. Knepley 
8180a6e0b375SMatthew G. Knepley   Output Parameter:
8181a6e0b375SMatthew G. Knepley . localX  - The output vector
8182a6e0b375SMatthew G. Knepley 
8183a6e0b375SMatthew G. Knepley    Calling sequence of func:
8184a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8185a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8186a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8187a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8188a6e0b375SMatthew G. Knepley 
8189a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8190a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8191a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8192a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8193a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8194a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8195a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8196a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8197a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8198a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8199a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8200a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8201a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8202a6e0b375SMatthew G. Knepley .  t            - The current time
8203a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8204a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8205a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8206a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8207a6e0b375SMatthew G. Knepley 
8208a6e0b375SMatthew 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.
8209a6e0b375SMatthew 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
8210a6e0b375SMatthew 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
8211a6e0b375SMatthew 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.
8212a6e0b375SMatthew G. Knepley 
8213a6e0b375SMatthew G. Knepley   Level: intermediate
8214a6e0b375SMatthew G. Knepley 
8215a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8216a6e0b375SMatthew G. Knepley @*/
82178c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
82188c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
82198c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
82208c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8221191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
82228c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
82238c6c5593SMatthew G. Knepley {
82248c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
82258c6c5593SMatthew G. Knepley 
82268c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82278c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82288c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
82298c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
82300918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
82318c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
82328c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
82338c6c5593SMatthew G. Knepley }
82348c6c5593SMatthew G. Knepley 
8235a6e0b375SMatthew G. Knepley /*@C
8236a6e0b375SMatthew 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.
8237a6e0b375SMatthew G. Knepley 
8238a6e0b375SMatthew G. Knepley   Not collective
8239a6e0b375SMatthew G. Knepley 
8240a6e0b375SMatthew G. Knepley   Input Parameters:
8241a6e0b375SMatthew G. Knepley + dm      - The DM
8242a6e0b375SMatthew G. Knepley . time    - The time
8243a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8244a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8245a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8246a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8247a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8248a6e0b375SMatthew G. Knepley . localU  - The input field vector
8249a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8250a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8251a6e0b375SMatthew G. Knepley 
8252a6e0b375SMatthew G. Knepley   Output Parameter:
8253a6e0b375SMatthew G. Knepley . localX  - The output vector
8254a6e0b375SMatthew G. Knepley 
8255a6e0b375SMatthew G. Knepley    Calling sequence of func:
8256a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8257a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8258a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8259a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8260a6e0b375SMatthew G. Knepley 
8261a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8262a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8263a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8264a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8265a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8266a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8267a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8268a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8269a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8270a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8271a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8272a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8273a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8274a6e0b375SMatthew G. Knepley .  t            - The current time
8275a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8276a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8277a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8278a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8279a6e0b375SMatthew G. Knepley 
8280a6e0b375SMatthew 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.
8281a6e0b375SMatthew 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
8282a6e0b375SMatthew 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
8283a6e0b375SMatthew 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.
8284a6e0b375SMatthew G. Knepley 
8285a6e0b375SMatthew G. Knepley   Level: intermediate
8286a6e0b375SMatthew G. Knepley 
8287a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8288a6e0b375SMatthew G. Knepley @*/
82891c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
82908c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
82918c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
82928c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8293191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
82948c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
82958c6c5593SMatthew G. Knepley {
82968c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
82978c6c5593SMatthew G. Knepley 
82988c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82998c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83008c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
83018c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8302ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
83031c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
83048c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
83058c6c5593SMatthew G. Knepley }
83068c6c5593SMatthew G. Knepley 
83072716604bSToby Isaac /*@C
8308ece3a9fcSMatthew 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.
8309ece3a9fcSMatthew G. Knepley 
8310ece3a9fcSMatthew G. Knepley   Not collective
8311ece3a9fcSMatthew G. Knepley 
8312ece3a9fcSMatthew G. Knepley   Input Parameters:
8313ece3a9fcSMatthew G. Knepley + dm      - The DM
8314ece3a9fcSMatthew G. Knepley . time    - The time
8315ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8316ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8317ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8318ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8319ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8320ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8321ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8322ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8323ece3a9fcSMatthew G. Knepley 
8324ece3a9fcSMatthew G. Knepley   Output Parameter:
8325ece3a9fcSMatthew G. Knepley . localX  - The output vector
8326ece3a9fcSMatthew G. Knepley 
8327ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8328ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8329ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8330ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8331ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8332ece3a9fcSMatthew G. Knepley 
8333ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8334ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8335ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8336ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8337ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8338ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8339ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8340ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8341ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8342ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8343ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8344ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8345ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8346ece3a9fcSMatthew G. Knepley .  t            - The current time
8347ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8348ece3a9fcSMatthew G. Knepley .  n            - The face normal
8349ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8350ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8351ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8352ece3a9fcSMatthew G. Knepley 
8353ece3a9fcSMatthew G. Knepley   Note:
8354ece3a9fcSMatthew 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.
8355ece3a9fcSMatthew 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
8356ece3a9fcSMatthew 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
8357ece3a9fcSMatthew 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.
8358ece3a9fcSMatthew G. Knepley 
8359ece3a9fcSMatthew G. Knepley   Level: intermediate
8360ece3a9fcSMatthew G. Knepley 
8361ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8362ece3a9fcSMatthew G. Knepley @*/
8363ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8364ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8365ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8366ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8367ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8368ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8369ece3a9fcSMatthew G. Knepley {
8370ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8371ece3a9fcSMatthew G. Knepley 
8372ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8373ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8374ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8375ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8376ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8377ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8378ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8379ece3a9fcSMatthew G. Knepley }
8380ece3a9fcSMatthew G. Knepley 
8381ece3a9fcSMatthew G. Knepley /*@C
83822716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83832716604bSToby Isaac 
83842716604bSToby Isaac   Input Parameters:
83852716604bSToby Isaac + dm    - The DM
83860709b2feSToby Isaac . time  - The time
83872716604bSToby Isaac . funcs - The functions to evaluate for each field component
83882716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8389574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83902716604bSToby Isaac 
83912716604bSToby Isaac   Output Parameter:
83922716604bSToby Isaac . diff - The diff ||u - u_h||_2
83932716604bSToby Isaac 
83942716604bSToby Isaac   Level: developer
83952716604bSToby Isaac 
83961189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
83972716604bSToby Isaac @*/
83980709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
83992716604bSToby Isaac {
84002716604bSToby Isaac   PetscErrorCode ierr;
84012716604bSToby Isaac 
84022716604bSToby Isaac   PetscFunctionBegin;
84032716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8404b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
84050918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
84060709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
84072716604bSToby Isaac   PetscFunctionReturn(0);
84082716604bSToby Isaac }
8409b698f381SToby Isaac 
8410b698f381SToby Isaac /*@C
8411b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8412b698f381SToby Isaac 
8413d083f849SBarry Smith   Collective on dm
8414d083f849SBarry Smith 
8415b698f381SToby Isaac   Input Parameters:
8416b698f381SToby Isaac + dm    - The DM
8417b698f381SToby Isaac , time  - The time
8418b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8419b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8420574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8421b698f381SToby Isaac - n     - The vector to project along
8422b698f381SToby Isaac 
8423b698f381SToby Isaac   Output Parameter:
8424b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8425b698f381SToby Isaac 
8426b698f381SToby Isaac   Level: developer
8427b698f381SToby Isaac 
8428b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8429b698f381SToby Isaac @*/
8430b698f381SToby 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)
8431b698f381SToby Isaac {
8432b698f381SToby Isaac   PetscErrorCode ierr;
8433b698f381SToby Isaac 
8434b698f381SToby Isaac   PetscFunctionBegin;
8435b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8436b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8437b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8438b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8439b698f381SToby Isaac   PetscFunctionReturn(0);
8440b698f381SToby Isaac }
8441b698f381SToby Isaac 
84422a16baeaSToby Isaac /*@C
84432a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84442a16baeaSToby Isaac 
8445d083f849SBarry Smith   Collective on dm
8446d083f849SBarry Smith 
84472a16baeaSToby Isaac   Input Parameters:
84482a16baeaSToby Isaac + dm    - The DM
84492a16baeaSToby Isaac . time  - The time
84502a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84512a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8452574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84532a16baeaSToby Isaac 
84542a16baeaSToby Isaac   Output Parameter:
84552a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84562a16baeaSToby Isaac 
84572a16baeaSToby Isaac   Level: developer
84582a16baeaSToby Isaac 
84591189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
84602a16baeaSToby Isaac @*/
84611189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
84622a16baeaSToby Isaac {
84632a16baeaSToby Isaac   PetscErrorCode ierr;
84642a16baeaSToby Isaac 
84652a16baeaSToby Isaac   PetscFunctionBegin;
84662a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84672a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
84680918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
84692a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
84702a16baeaSToby Isaac   PetscFunctionReturn(0);
84712a16baeaSToby Isaac }
84722a16baeaSToby Isaac 
8473df0b854cSToby Isaac /*@C
8474df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8475cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8476df0b854cSToby Isaac 
8477df0b854cSToby Isaac   Collective on dm
8478df0b854cSToby Isaac 
8479df0b854cSToby Isaac   Input parameters:
8480df0b854cSToby Isaac + dm - the pre-adaptation DM object
8481a1b0c543SToby Isaac - label - label with the flags
8482df0b854cSToby Isaac 
8483df0b854cSToby Isaac   Output parameters:
84840d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8485df0b854cSToby Isaac 
8486df0b854cSToby Isaac   Level: intermediate
84870d1cd5e0SMatthew G. Knepley 
84880d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8489df0b854cSToby Isaac @*/
84900d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8491df0b854cSToby Isaac {
8492df0b854cSToby Isaac   PetscErrorCode ierr;
8493df0b854cSToby Isaac 
8494df0b854cSToby Isaac   PetscFunctionBegin;
8495df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8496a1b0c543SToby Isaac   PetscValidPointer(label,2);
84970d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
84980d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
84996f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
85000d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
85010d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
85020d1cd5e0SMatthew G. Knepley }
85030d1cd5e0SMatthew G. Knepley 
85040d1cd5e0SMatthew G. Knepley /*@C
85050d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
85060d1cd5e0SMatthew G. Knepley 
85070d1cd5e0SMatthew G. Knepley   Input Parameters:
85080d1cd5e0SMatthew G. Knepley + dm - The DM object
85090d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
85106f25b0d8SLisandro Dalcin - bdLabel - Label for boundary tags, which will be preserved in the output mesh. bdLabel should be NULL if there is no such label, and should be different from "_boundary_".
85110d1cd5e0SMatthew G. Knepley 
85120d1cd5e0SMatthew G. Knepley   Output Parameter:
85130d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
85140d1cd5e0SMatthew G. Knepley 
85150d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
85160d1cd5e0SMatthew G. Knepley 
85170d1cd5e0SMatthew G. Knepley   Level: advanced
85180d1cd5e0SMatthew G. Knepley 
85190d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
85200d1cd5e0SMatthew G. Knepley @*/
85210d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
85220d1cd5e0SMatthew G. Knepley {
85230d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
85240d1cd5e0SMatthew G. Knepley 
85250d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
85260d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85270d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
85280d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
85290d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
85306f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
85316f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
85320d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8533df0b854cSToby Isaac   PetscFunctionReturn(0);
8534df0b854cSToby Isaac }
8535c4088d22SMatthew G. Knepley 
8536502a2867SDave May /*@C
8537502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8538502a2867SDave May 
8539502a2867SDave May  Not Collective
8540502a2867SDave May 
8541502a2867SDave May  Input Parameter:
8542502a2867SDave May .  dm    - The DM
8543502a2867SDave May 
85440a19bb7dSprj-  Output Parameters:
85450a19bb7dSprj- +  nranks - the number of neighbours
85460a19bb7dSprj- -  ranks - the neighbors ranks
8547502a2867SDave May 
8548502a2867SDave May  Notes:
8549502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8550502a2867SDave May 
8551502a2867SDave May  Level: beginner
8552502a2867SDave May 
8553dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8554502a2867SDave May @*/
8555502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8556502a2867SDave May {
8557502a2867SDave May   PetscErrorCode ierr;
8558502a2867SDave May 
8559502a2867SDave May   PetscFunctionBegin;
8560502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85610918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8562502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8563502a2867SDave May   PetscFunctionReturn(0);
8564502a2867SDave May }
8565502a2867SDave May 
8566531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8567531c7667SBarry Smith 
8568531c7667SBarry Smith /*
8569531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8570531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8571531c7667SBarry Smith */
8572531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8573531c7667SBarry Smith {
8574531c7667SBarry Smith   PetscErrorCode ierr;
8575531c7667SBarry Smith 
8576531c7667SBarry Smith   PetscFunctionBegin;
8577531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8578531c7667SBarry Smith     Vec x1local;
8579531c7667SBarry Smith     DM  dm;
8580531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8581531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8582531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8583531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8584531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8585531c7667SBarry Smith     x1   = x1local;
8586531c7667SBarry Smith   }
8587531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8588531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8589531c7667SBarry Smith     DM  dm;
8590531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8591531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8592531c7667SBarry Smith   }
8593531c7667SBarry Smith   PetscFunctionReturn(0);
8594531c7667SBarry Smith }
8595531c7667SBarry Smith 
8596531c7667SBarry Smith /*@
8597531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8598531c7667SBarry Smith 
8599531c7667SBarry Smith     Input Parameter:
8600531c7667SBarry Smith .    coloring - the MatFDColoring object
8601531c7667SBarry Smith 
860295452b02SPatrick Sanan     Developer Notes:
860395452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8604531c7667SBarry Smith 
86051b266c99SBarry Smith     Level: advanced
86061b266c99SBarry Smith 
8607531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8608531c7667SBarry Smith @*/
8609531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8610531c7667SBarry Smith {
8611531c7667SBarry Smith   PetscFunctionBegin;
8612531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8613531c7667SBarry Smith   PetscFunctionReturn(0);
8614531c7667SBarry Smith }
86158320bc6fSPatrick Sanan 
86168320bc6fSPatrick Sanan /*@
86178320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
86188320bc6fSPatrick Sanan 
86198320bc6fSPatrick Sanan     Collective
86208320bc6fSPatrick Sanan 
86218320bc6fSPatrick Sanan     Input Parameters:
8622a5bc1bf3SBarry Smith +    dm1 - the first DM
86238320bc6fSPatrick Sanan -    dm2 - the second DM
86248320bc6fSPatrick Sanan 
86258320bc6fSPatrick Sanan     Output Parameters:
86268320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
86278320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
86288320bc6fSPatrick Sanan 
86298320bc6fSPatrick Sanan     Notes:
86308320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
86313d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
86328320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
86333d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
86343d862458SPatrick Sanan     decomposition, but hold different data.
86358320bc6fSPatrick Sanan 
86368320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
86378320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
86388320bc6fSPatrick Sanan 
86398320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
86408320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
86418320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
86428320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
86438320bc6fSPatrick Sanan 
86448320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
86458320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
86468320bc6fSPatrick Sanan .vb
86478320bc6fSPatrick Sanan   ...
86488320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
86498320bc6fSPatrick Sanan   if (set && compatible)  {
86508320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
86518320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
86523d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
86538320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
86548320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86558320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86568320bc6fSPatrick Sanan       }
86578320bc6fSPatrick Sanan     }
86588320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
86598320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
86608320bc6fSPatrick Sanan   } else {
86618320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
86628320bc6fSPatrick Sanan   }
86638320bc6fSPatrick Sanan   ...
86648320bc6fSPatrick Sanan .ve
86658320bc6fSPatrick Sanan 
86668320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
86678320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
86688320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
86698320bc6fSPatrick Sanan     always check the "set" output parameter.
86708320bc6fSPatrick Sanan 
86718320bc6fSPatrick Sanan     A DM is always compatible with itself.
86728320bc6fSPatrick Sanan 
86738320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
86748320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
86758320bc6fSPatrick Sanan     incompatible.
86768320bc6fSPatrick Sanan 
86778320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
86788320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
86798320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
86808320bc6fSPatrick Sanan 
86818320bc6fSPatrick Sanan     Developer Notes:
86823d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
86833d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8684a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
86858320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
86868320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
86873d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
86883d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
86898320bc6fSPatrick Sanan 
86908320bc6fSPatrick Sanan     Level: advanced
86918320bc6fSPatrick Sanan 
86923d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
86938320bc6fSPatrick Sanan @*/
86948320bc6fSPatrick Sanan 
8695a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
86968320bc6fSPatrick Sanan {
86978320bc6fSPatrick Sanan   PetscErrorCode ierr;
86988320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
86998320bc6fSPatrick Sanan   DMType         type,type2;
87008320bc6fSPatrick Sanan   PetscBool      sameType;
87018320bc6fSPatrick Sanan 
87028320bc6fSPatrick Sanan   PetscFunctionBegin;
8703a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
87048320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
87058320bc6fSPatrick Sanan 
87068320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8707a5bc1bf3SBarry Smith   if (dm1 == dm2) {
87088320bc6fSPatrick Sanan     *set = PETSC_TRUE;
87098320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
87108320bc6fSPatrick Sanan     PetscFunctionReturn(0);
87118320bc6fSPatrick Sanan   }
87128320bc6fSPatrick Sanan 
87138320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
87148320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
87158320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
87168320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8717a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
87188320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
87198320bc6fSPatrick Sanan     *set = PETSC_TRUE;
87208320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
87218320bc6fSPatrick Sanan     PetscFunctionReturn(0);
87228320bc6fSPatrick Sanan   }
87238320bc6fSPatrick Sanan 
87248320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8725a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8726a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8727b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
87288320bc6fSPatrick Sanan   }
87298320bc6fSPatrick Sanan 
8730a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
87318320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8732a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
87338320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
87348320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
87358320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8736a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
87378320bc6fSPatrick Sanan   } else {
87388320bc6fSPatrick Sanan     *set = PETSC_FALSE;
87398320bc6fSPatrick Sanan   }
87408320bc6fSPatrick Sanan   PetscFunctionReturn(0);
87418320bc6fSPatrick Sanan }
8742c0f0dcc3SMatthew G. Knepley 
8743c0f0dcc3SMatthew G. Knepley /*@C
8744c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8745c0f0dcc3SMatthew G. Knepley 
8746c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8747c0f0dcc3SMatthew G. Knepley 
8748c0f0dcc3SMatthew G. Knepley   Input Parameters:
8749c0f0dcc3SMatthew G. Knepley + DM - the DM
8750c0f0dcc3SMatthew G. Knepley . f - the monitor function
8751c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8752c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8753c0f0dcc3SMatthew G. Knepley 
8754c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8755c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8756c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8757c0f0dcc3SMatthew G. Knepley 
8758c0f0dcc3SMatthew G. Knepley   Notes:
8759c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8760c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8761c0f0dcc3SMatthew G. Knepley   order in which they were set.
8762c0f0dcc3SMatthew G. Knepley 
8763c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8764c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8765c0f0dcc3SMatthew G. Knepley 
8766c0f0dcc3SMatthew G. Knepley   Level: intermediate
8767c0f0dcc3SMatthew G. Knepley 
8768c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8769c0f0dcc3SMatthew G. Knepley @*/
8770c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8771c0f0dcc3SMatthew G. Knepley {
8772c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8773c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8774c0f0dcc3SMatthew G. Knepley 
8775c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8776c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8777c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8778c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8779c0f0dcc3SMatthew G. Knepley 
8780c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8781c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8782c0f0dcc3SMatthew G. Knepley   }
8783c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8784c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8785c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8786c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8787c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8788c0f0dcc3SMatthew G. Knepley }
8789c0f0dcc3SMatthew G. Knepley 
8790c0f0dcc3SMatthew G. Knepley /*@
8791c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8792c0f0dcc3SMatthew G. Knepley 
8793c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8794c0f0dcc3SMatthew G. Knepley 
8795c0f0dcc3SMatthew G. Knepley   Input Parameter:
8796c0f0dcc3SMatthew G. Knepley . dm - the DM
8797c0f0dcc3SMatthew G. Knepley 
8798c0f0dcc3SMatthew G. Knepley   Options Database Key:
8799c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8800c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8801c0f0dcc3SMatthew G. Knepley   set via the options database
8802c0f0dcc3SMatthew G. Knepley 
8803c0f0dcc3SMatthew G. Knepley   Notes:
8804c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8805c0f0dcc3SMatthew G. Knepley 
8806c0f0dcc3SMatthew G. Knepley   Level: intermediate
8807c0f0dcc3SMatthew G. Knepley 
8808c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8809c0f0dcc3SMatthew G. Knepley @*/
8810c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8811c0f0dcc3SMatthew G. Knepley {
8812c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8813c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8814c0f0dcc3SMatthew G. Knepley 
8815c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8816c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8817c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8818c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8819c0f0dcc3SMatthew G. Knepley   }
8820c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8821c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8822c0f0dcc3SMatthew G. Knepley }
8823c0f0dcc3SMatthew G. Knepley 
8824c0f0dcc3SMatthew G. Knepley /*@C
8825c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8826c0f0dcc3SMatthew G. Knepley 
8827c0f0dcc3SMatthew G. Knepley   Collective on DM
8828c0f0dcc3SMatthew G. Knepley 
8829c0f0dcc3SMatthew G. Knepley   Input Parameters:
8830c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8831c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8832c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8833c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8834c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8835c0f0dcc3SMatthew 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
8836c0f0dcc3SMatthew G. Knepley 
8837c0f0dcc3SMatthew G. Knepley   Output Parameter:
8838c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8839c0f0dcc3SMatthew G. Knepley 
8840c0f0dcc3SMatthew G. Knepley   Level: developer
8841c0f0dcc3SMatthew G. Knepley 
8842c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
8843c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
8844c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
8845c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
8846c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
8847c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
8848c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
8849c0f0dcc3SMatthew G. Knepley @*/
8850c0f0dcc3SMatthew 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)
8851c0f0dcc3SMatthew G. Knepley {
8852c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8853c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8854c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
8855c0f0dcc3SMatthew G. Knepley 
8856c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8857c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8858c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
8859c0f0dcc3SMatthew G. Knepley   if (*flg) {
8860c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8861c0f0dcc3SMatthew G. Knepley 
8862c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
8863c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
8864c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
8865c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
8866c0f0dcc3SMatthew G. Knepley   }
8867c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8868c0f0dcc3SMatthew G. Knepley }
8869c0f0dcc3SMatthew G. Knepley 
8870c0f0dcc3SMatthew G. Knepley /*@
8871c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8872c0f0dcc3SMatthew G. Knepley 
8873c0f0dcc3SMatthew G. Knepley    Collective on DM
8874c0f0dcc3SMatthew G. Knepley 
8875c0f0dcc3SMatthew G. Knepley    Input Parameters:
8876c0f0dcc3SMatthew G. Knepley .  dm - The DM
8877c0f0dcc3SMatthew G. Knepley 
8878c0f0dcc3SMatthew G. Knepley    Level: developer
8879c0f0dcc3SMatthew G. Knepley 
8880c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8881c0f0dcc3SMatthew G. Knepley @*/
8882c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8883c0f0dcc3SMatthew G. Knepley {
8884c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8885c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8886c0f0dcc3SMatthew G. Knepley 
8887c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8888c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8889c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8890c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8891c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
8892c0f0dcc3SMatthew G. Knepley   }
8893c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8894c0f0dcc3SMatthew G. Knepley }
8895