xref: /petsc/src/dm/interface/dm.c (revision 689b5837e6c793fe54dcaa7ee2d734695cc83636)
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 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
10d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
111ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1267a56275SMatthew G Knepley 
13e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
14bff4a2f0SMatthew G. Knepley 
154a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
164a7a4c06SLawrence Mitchell {
174a7a4c06SLawrence Mitchell   PetscFunctionBegin;
184a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
194a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
204a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
214a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
224a7a4c06SLawrence Mitchell }
234a7a4c06SLawrence Mitchell 
24a4121054SBarry Smith /*@
25de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
26a4121054SBarry Smith 
27a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
28a4121054SBarry Smith    error when you try to use the vector.
29a4121054SBarry Smith 
30a4121054SBarry Smith   Collective on MPI_Comm
31a4121054SBarry Smith 
32a4121054SBarry Smith   Input Parameter:
33a4121054SBarry Smith . comm - The communicator for the DM object
34a4121054SBarry Smith 
35a4121054SBarry Smith   Output Parameter:
36a4121054SBarry Smith . dm - The DM object
37a4121054SBarry Smith 
38a4121054SBarry Smith   Level: beginner
39a4121054SBarry Smith 
408472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
41a4121054SBarry Smith @*/
427087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
43a4121054SBarry Smith {
44a4121054SBarry Smith   DM             v;
45a4121054SBarry Smith   PetscErrorCode ierr;
46a4121054SBarry Smith 
47a4121054SBarry Smith   PetscFunctionBegin;
481411c6eeSJed Brown   PetscValidPointer(dm,2);
490298fd71SBarry Smith   *dm = NULL;
50607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
51a4121054SBarry Smith 
5273107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
53e7c4fc90SDmitry Karpeev 
540298fd71SBarry Smith   v->ltogmap                  = NULL;
551411c6eeSJed Brown   v->bs                       = 1;
56171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5788ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5888ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
59c58f1c22SToby Isaac   v->labels                   = NULL;
60c58f1c22SToby Isaac   v->depthLabel               = NULL;
610298fd71SBarry Smith   v->defaultSection           = NULL;
620298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
63fba222abSToby Isaac   v->defaultConstraintSection = NULL;
64fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
65c6b900c6SMatthew G. Knepley   v->L                        = NULL;
66c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
675dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
689a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
6996173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
70435a35e8SMatthew G Knepley   {
71435a35e8SMatthew G Knepley     PetscInt i;
72435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
730298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
74f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
75435a35e8SMatthew G Knepley     }
76435a35e8SMatthew G Knepley   }
772764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
7814f150ffSMatthew G. Knepley   v->dmBC = NULL;
79a8fb8f29SToby Isaac   v->coarseMesh = NULL;
80f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
81cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
82c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
83b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
84bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
85c58f1c22SToby Isaac   v->labels->refct = 1;
864a7a4c06SLawrence Mitchell 
874a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
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 
9638221697SMatthew G. Knepley   Collective on MPI_Comm
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 
10638221697SMatthew G. Knepley .keywords: DM, topology, create
10738221697SMatthew G. Knepley @*/
10838221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
10938221697SMatthew G. Knepley {
11038221697SMatthew G. Knepley   PetscSF        sf;
11138221697SMatthew G. Knepley   Vec            coords;
11238221697SMatthew G. Knepley   void          *ctx;
113a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11438221697SMatthew G. Knepley   PetscErrorCode ierr;
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   PetscFunctionBegin;
11738221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11838221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
11938221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
120c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
121c58f1c22SToby Isaac   dm->labels->refct++;
122c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
123c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1241de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1251de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12638221697SMatthew G. Knepley   if (dm->ops->clone) {
12738221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
12838221697SMatthew G. Knepley   }
1293f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13038221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13238221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13338221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
134be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
135be4c1c3eSMatthew G. Knepley     DM           ncdm;
136be4c1c3eSMatthew G. Knepley     PetscSection cs;
1375a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
138be4c1c3eSMatthew G. Knepley 
139e87a4003SBarry Smith     ierr = DMGetSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
140be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1415a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1425a0206caSToby Isaac     if (pEndMax >= 0) {
143be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
144e87a4003SBarry Smith       ierr = DMSetSection(ncdm, cs);CHKERRQ(ierr);
145a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
146be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley     }
148be4c1c3eSMatthew G. Knepley   }
149a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
150a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15138221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   if (coords) {
15338221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15438221697SMatthew G. Knepley   } else {
15538221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15638221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15738221697SMatthew G. Knepley   }
15890b157c4SStefano Zampini   {
15990b157c4SStefano Zampini     PetscBool             isper;
160c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1615dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16290b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16390b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
164c6b900c6SMatthew G. Knepley   }
16538221697SMatthew G. Knepley   PetscFunctionReturn(0);
16638221697SMatthew G. Knepley }
16738221697SMatthew G. Knepley 
1689a42bb27SBarry Smith /*@C
169564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1709a42bb27SBarry Smith 
1718472ad0fSDave May    Logically Collective on DM
1729a42bb27SBarry Smith 
1739a42bb27SBarry Smith    Input Parameter:
1749a42bb27SBarry Smith +  da - initial distributed array
175e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1769a42bb27SBarry Smith 
1779a42bb27SBarry Smith    Options Database:
178dd85299cSBarry Smith .   -dm_vec_type ctype
1799a42bb27SBarry Smith 
1809a42bb27SBarry Smith    Level: intermediate
1819a42bb27SBarry Smith 
182a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1839a42bb27SBarry Smith @*/
18419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1859a42bb27SBarry Smith {
1869a42bb27SBarry Smith   PetscErrorCode ierr;
1879a42bb27SBarry Smith 
1889a42bb27SBarry Smith   PetscFunctionBegin;
1899a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1909a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1929a42bb27SBarry Smith   PetscFunctionReturn(0);
1939a42bb27SBarry Smith }
1949a42bb27SBarry Smith 
195c0dedaeaSBarry Smith /*@C
196c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
197c0dedaeaSBarry Smith 
1988472ad0fSDave May    Logically Collective on DM
199c0dedaeaSBarry Smith 
200c0dedaeaSBarry Smith    Input Parameter:
201c0dedaeaSBarry Smith .  da - initial distributed array
202c0dedaeaSBarry Smith 
203c0dedaeaSBarry Smith    Output Parameter:
204c0dedaeaSBarry Smith .  ctype - the vector type
205c0dedaeaSBarry Smith 
206c0dedaeaSBarry Smith    Level: intermediate
207c0dedaeaSBarry Smith 
208a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
209c0dedaeaSBarry Smith @*/
210c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
211c0dedaeaSBarry Smith {
212c0dedaeaSBarry Smith   PetscFunctionBegin;
213c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
214c0dedaeaSBarry Smith   *ctype = da->vectype;
215c0dedaeaSBarry Smith   PetscFunctionReturn(0);
216c0dedaeaSBarry Smith }
217c0dedaeaSBarry Smith 
2185f1ad066SMatthew G Knepley /*@
21934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2205f1ad066SMatthew G Knepley 
2215f1ad066SMatthew G Knepley   Not collective
2225f1ad066SMatthew G Knepley 
2235f1ad066SMatthew G Knepley   Input Parameter:
2245f1ad066SMatthew G Knepley . v - The Vec
2255f1ad066SMatthew G Knepley 
2265f1ad066SMatthew G Knepley   Output Parameter:
2275f1ad066SMatthew G Knepley . dm - The DM
2285f1ad066SMatthew G Knepley 
2295f1ad066SMatthew G Knepley   Level: intermediate
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2325f1ad066SMatthew G Knepley @*/
2335f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2345f1ad066SMatthew G Knepley {
2355f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley   PetscFunctionBegin;
2385f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2395f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2405f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2415f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2425f1ad066SMatthew G Knepley }
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley /*@
245d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2465f1ad066SMatthew G Knepley 
2475f1ad066SMatthew G Knepley   Not collective
2485f1ad066SMatthew G Knepley 
2495f1ad066SMatthew G Knepley   Input Parameters:
2505f1ad066SMatthew G Knepley + v - The Vec
2515f1ad066SMatthew G Knepley - dm - The DM
2525f1ad066SMatthew G Knepley 
253d9805387SMatthew 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.
254d9805387SMatthew G. Knepley 
2555f1ad066SMatthew G Knepley   Level: intermediate
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2585f1ad066SMatthew G Knepley @*/
2595f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2605f1ad066SMatthew G Knepley {
2615f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley   PetscFunctionBegin;
2645f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
265d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2665f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2675f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2685f1ad066SMatthew G Knepley }
2695f1ad066SMatthew G Knepley 
270521d9a4cSLisandro Dalcin /*@C
2718f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2728f1509bcSBarry Smith 
2738f1509bcSBarry Smith    Logically Collective on DM
2748f1509bcSBarry Smith 
2758f1509bcSBarry Smith    Input Parameters:
2768f1509bcSBarry Smith +  dm - the DM context
2778f1509bcSBarry Smith -  ctype - the matrix type
2788f1509bcSBarry Smith 
2798f1509bcSBarry Smith    Options Database:
2808f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2818f1509bcSBarry Smith 
2828f1509bcSBarry Smith    Level: intermediate
2838f1509bcSBarry Smith 
2848f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2858f1509bcSBarry Smith           DMGetISColoringType()
2868f1509bcSBarry Smith @*/
2878f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2888f1509bcSBarry Smith {
2898f1509bcSBarry Smith   PetscFunctionBegin;
2908f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2918f1509bcSBarry Smith   dm->coloringtype = ctype;
2928f1509bcSBarry Smith   PetscFunctionReturn(0);
2938f1509bcSBarry Smith }
2948f1509bcSBarry Smith 
2958f1509bcSBarry Smith /*@C
2968f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
297521d9a4cSLisandro Dalcin 
298521d9a4cSLisandro Dalcin    Logically Collective on DM
299521d9a4cSLisandro Dalcin 
300521d9a4cSLisandro Dalcin    Input Parameter:
3018f1509bcSBarry Smith .  dm - the DM context
3028f1509bcSBarry Smith 
3038f1509bcSBarry Smith    Output Parameter:
3048f1509bcSBarry Smith .  ctype - the matrix type
3058f1509bcSBarry Smith 
3068f1509bcSBarry Smith    Options Database:
3078f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3088f1509bcSBarry Smith 
3098f1509bcSBarry Smith    Level: intermediate
3108f1509bcSBarry Smith 
3118f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3128f1509bcSBarry Smith           DMGetISColoringType()
3138f1509bcSBarry Smith @*/
3148f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3158f1509bcSBarry Smith {
3168f1509bcSBarry Smith   PetscFunctionBegin;
3178f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3188f1509bcSBarry Smith   *ctype = dm->coloringtype;
3198f1509bcSBarry Smith   PetscFunctionReturn(0);
3208f1509bcSBarry Smith }
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith /*@C
3238f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith    Logically Collective on DM
3268f1509bcSBarry Smith 
3278f1509bcSBarry Smith    Input Parameters:
328521d9a4cSLisandro Dalcin +  dm - the DM context
329a2b5a043SBarry Smith -  ctype - the matrix type
330521d9a4cSLisandro Dalcin 
331521d9a4cSLisandro Dalcin    Options Database:
332521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
333521d9a4cSLisandro Dalcin 
334521d9a4cSLisandro Dalcin    Level: intermediate
335521d9a4cSLisandro Dalcin 
336a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
337521d9a4cSLisandro Dalcin @*/
33819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
339521d9a4cSLisandro Dalcin {
340521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
34188f0584fSBarry Smith 
342521d9a4cSLisandro Dalcin   PetscFunctionBegin;
343521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
344521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
34519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
346521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
347521d9a4cSLisandro Dalcin }
348521d9a4cSLisandro Dalcin 
349c0dedaeaSBarry Smith /*@C
350c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
351c0dedaeaSBarry Smith 
352c0dedaeaSBarry Smith    Logically Collective on DM
353c0dedaeaSBarry Smith 
354c0dedaeaSBarry Smith    Input Parameter:
355c0dedaeaSBarry Smith .  dm - the DM context
356c0dedaeaSBarry Smith 
357c0dedaeaSBarry Smith    Output Parameter:
358c0dedaeaSBarry Smith .  ctype - the matrix type
359c0dedaeaSBarry Smith 
360c0dedaeaSBarry Smith    Options Database:
361c0dedaeaSBarry Smith .   -dm_mat_type ctype
362c0dedaeaSBarry Smith 
363c0dedaeaSBarry Smith    Level: intermediate
364c0dedaeaSBarry Smith 
365a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
366c0dedaeaSBarry Smith @*/
367c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
368c0dedaeaSBarry Smith {
369c0dedaeaSBarry Smith   PetscFunctionBegin;
370c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
371c0dedaeaSBarry Smith   *ctype = dm->mattype;
372c0dedaeaSBarry Smith   PetscFunctionReturn(0);
373c0dedaeaSBarry Smith }
374c0dedaeaSBarry Smith 
375c688c046SMatthew G Knepley /*@
37634f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
377c688c046SMatthew G Knepley 
378c688c046SMatthew G Knepley   Not collective
379c688c046SMatthew G Knepley 
380c688c046SMatthew G Knepley   Input Parameter:
381c688c046SMatthew G Knepley . A - The Mat
382c688c046SMatthew G Knepley 
383c688c046SMatthew G Knepley   Output Parameter:
384c688c046SMatthew G Knepley . dm - The DM
385c688c046SMatthew G Knepley 
386c688c046SMatthew G Knepley   Level: intermediate
387c688c046SMatthew G Knepley 
3888f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3898f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3908f1509bcSBarry Smith 
391c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
392c688c046SMatthew G Knepley @*/
393c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
394c688c046SMatthew G Knepley {
395c688c046SMatthew G Knepley   PetscErrorCode ierr;
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   PetscFunctionBegin;
398c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
399c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
400c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
401c688c046SMatthew G Knepley   PetscFunctionReturn(0);
402c688c046SMatthew G Knepley }
403c688c046SMatthew G Knepley 
404c688c046SMatthew G Knepley /*@
405c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
406c688c046SMatthew G Knepley 
407c688c046SMatthew G Knepley   Not collective
408c688c046SMatthew G Knepley 
409c688c046SMatthew G Knepley   Input Parameters:
410c688c046SMatthew G Knepley + A - The Mat
411c688c046SMatthew G Knepley - dm - The DM
412c688c046SMatthew G Knepley 
413c688c046SMatthew G Knepley   Level: intermediate
414c688c046SMatthew G Knepley 
4158f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4168f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4178f1509bcSBarry Smith 
4188f1509bcSBarry Smith 
419c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
420c688c046SMatthew G Knepley @*/
421c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
422c688c046SMatthew G Knepley {
423c688c046SMatthew G Knepley   PetscErrorCode ierr;
424c688c046SMatthew G Knepley 
425c688c046SMatthew G Knepley   PetscFunctionBegin;
426c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4278865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
428c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
429c688c046SMatthew G Knepley   PetscFunctionReturn(0);
430c688c046SMatthew G Knepley }
431c688c046SMatthew G Knepley 
4329a42bb27SBarry Smith /*@C
4339a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4346757b960SDave May    DM options in the database.
4359a42bb27SBarry Smith 
4368353ddbbSDave May    Logically Collective on DM
4379a42bb27SBarry Smith 
4389a42bb27SBarry Smith    Input Parameter:
4398353ddbbSDave May +  da - the DM context
4409a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4419a42bb27SBarry Smith 
4429a42bb27SBarry Smith    Notes:
4439a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4449a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4459a42bb27SBarry Smith 
4469a42bb27SBarry Smith    Level: advanced
4479a42bb27SBarry Smith 
4488353ddbbSDave May .keywords: DM, set, options, prefix, database
4499a42bb27SBarry Smith 
4509a42bb27SBarry Smith .seealso: DMSetFromOptions()
4519a42bb27SBarry Smith @*/
4527087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4539a42bb27SBarry Smith {
4549a42bb27SBarry Smith   PetscErrorCode ierr;
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith   PetscFunctionBegin;
4579a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4589a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
459691be533SLawrence Mitchell   if (dm->sf) {
460691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
461691be533SLawrence Mitchell   }
462691be533SLawrence Mitchell   if (dm->defaultSF) {
463691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
464691be533SLawrence Mitchell   }
4659a42bb27SBarry Smith   PetscFunctionReturn(0);
4669a42bb27SBarry Smith }
4679a42bb27SBarry Smith 
46831697293SDave May /*@C
46931697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47031697293SDave May    DM options in the database.
47131697293SDave May 
47231697293SDave May    Logically Collective on DM
47331697293SDave May 
47431697293SDave May    Input Parameters:
47531697293SDave May +  dm - the DM context
47631697293SDave May -  prefix - the prefix string to prepend to all DM option requests
47731697293SDave May 
47831697293SDave May    Notes:
47931697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48031697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48131697293SDave May 
48231697293SDave May    Level: advanced
48331697293SDave May 
48431697293SDave May .keywords: DM, append, options, prefix, database
48531697293SDave May 
48631697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
48731697293SDave May @*/
48831697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
48931697293SDave May {
49031697293SDave May   PetscErrorCode ierr;
49131697293SDave May 
49231697293SDave May   PetscFunctionBegin;
49331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49431697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
49531697293SDave May   PetscFunctionReturn(0);
49631697293SDave May }
49731697293SDave May 
49831697293SDave May /*@C
49931697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50031697293SDave May    DM options in the database.
50131697293SDave May 
50231697293SDave May    Not Collective
50331697293SDave May 
50431697293SDave May    Input Parameters:
50531697293SDave May .  dm - the DM context
50631697293SDave May 
50731697293SDave May    Output Parameters:
50831697293SDave May .  prefix - pointer to the prefix string used is returned
50931697293SDave May 
51095452b02SPatrick Sanan    Notes:
51195452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51231697293SDave May    sufficient length to hold the prefix.
51331697293SDave May 
51431697293SDave May    Level: advanced
51531697293SDave May 
51631697293SDave May .keywords: DM, set, options, prefix, database
51731697293SDave May 
51831697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
51931697293SDave May @*/
52031697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52131697293SDave May {
52231697293SDave May   PetscErrorCode ierr;
52331697293SDave May 
52431697293SDave May   PetscFunctionBegin;
52531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52631697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
52731697293SDave May   PetscFunctionReturn(0);
52831697293SDave May }
52931697293SDave May 
53088bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53188bdff64SToby Isaac {
53288bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53388bdff64SToby Isaac   DMNamedVecLink nlink;
53488bdff64SToby Isaac   PetscErrorCode ierr;
53588bdff64SToby Isaac 
53688bdff64SToby Isaac   PetscFunctionBegin;
537aab5bcd8SJed Brown   *ncrefct = 0;
53888bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
53988bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54088bdff64SToby Isaac     if (dm->localin[i])  refct--;
54188bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54288bdff64SToby Isaac   }
54388bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54488bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
54588bdff64SToby Isaac   if (dm->x) {
54688bdff64SToby Isaac     DM obj;
54788bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
54888bdff64SToby Isaac     if (obj == dm) refct--;
54988bdff64SToby Isaac   }
55088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55188bdff64SToby Isaac     refct--;
55288bdff64SToby Isaac     if (recurseCoarse) {
55388bdff64SToby Isaac       PetscInt coarseCount;
55488bdff64SToby Isaac 
55588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55688bdff64SToby Isaac       refct += coarseCount;
55788bdff64SToby Isaac     }
55888bdff64SToby Isaac   }
55988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56088bdff64SToby Isaac     refct--;
56188bdff64SToby Isaac     if (recurseFine) {
56288bdff64SToby Isaac       PetscInt fineCount;
56388bdff64SToby Isaac 
56488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56588bdff64SToby Isaac       refct += fineCount;
56688bdff64SToby Isaac     }
56788bdff64SToby Isaac   }
56888bdff64SToby Isaac   *ncrefct = refct;
56988bdff64SToby Isaac   PetscFunctionReturn(0);
57088bdff64SToby Isaac }
57188bdff64SToby Isaac 
572354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
573354557abSToby Isaac {
574354557abSToby Isaac   PetscErrorCode ierr;
575354557abSToby Isaac 
576354557abSToby Isaac   PetscFunctionBegin;
577354557abSToby Isaac   if (!--(dm->labels->refct)) {
578354557abSToby Isaac     DMLabelLink next = dm->labels->next;
579354557abSToby Isaac 
580354557abSToby Isaac     /* destroy the labels */
581354557abSToby Isaac     while (next) {
582354557abSToby Isaac       DMLabelLink tmp = next->next;
583354557abSToby Isaac 
584354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
585354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
586354557abSToby Isaac       next = tmp;
587354557abSToby Isaac     }
588354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
589354557abSToby Isaac   }
590354557abSToby Isaac   PetscFunctionReturn(0);
591354557abSToby Isaac }
592354557abSToby Isaac 
59347c6ae99SBarry Smith /*@
5948472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59547c6ae99SBarry Smith 
59647c6ae99SBarry Smith     Collective on DM
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith     Input Parameter:
59947c6ae99SBarry Smith .   dm - the DM object to destroy
60047c6ae99SBarry Smith 
60147c6ae99SBarry Smith     Level: developer
60247c6ae99SBarry Smith 
603e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith @*/
606fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60747c6ae99SBarry Smith {
60888bdff64SToby Isaac   PetscInt       i, cnt;
609dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61047c6ae99SBarry Smith   PetscErrorCode ierr;
61147c6ae99SBarry Smith 
61247c6ae99SBarry Smith   PetscFunctionBegin;
6136bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6146bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61587e657c6SBarry Smith 
61688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61788bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61888bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
61988bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
620732e2eb9SMatthew G Knepley   /*
621732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
622732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
623732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
624732e2eb9SMatthew G Knepley   */
6256bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6266bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
627732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6286bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6296bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
630732e2eb9SMatthew G Knepley   }
631f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6320298fd71SBarry Smith   (*dm)->namedglobal = NULL;
633f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6342348bcf4SPeter Brune     nnext = nlink->next;
6352348bcf4SPeter 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);
6362348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6372348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6382348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6392348bcf4SPeter Brune   }
640f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6410298fd71SBarry Smith   (*dm)->namedlocal = NULL;
642f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
643f490541aSPeter Brune     nnext = nlink->next;
644f490541aSPeter 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);
645f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
646f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
647f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
648f490541aSPeter Brune   }
6492348bcf4SPeter Brune 
650b17ce1afSJed Brown   /* Destroy the list of hooks */
651c833c3b5SJed Brown   {
652c833c3b5SJed Brown     DMCoarsenHookLink link,next;
653b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
654b17ce1afSJed Brown       next = link->next;
655b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
656b17ce1afSJed Brown     }
6570298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
658c833c3b5SJed Brown   }
659c833c3b5SJed Brown   {
660c833c3b5SJed Brown     DMRefineHookLink link,next;
661c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
662c833c3b5SJed Brown       next = link->next;
663c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
664c833c3b5SJed Brown     }
6650298fd71SBarry Smith     (*dm)->refinehook = NULL;
666c833c3b5SJed Brown   }
667be081cd6SPeter Brune   {
668be081cd6SPeter Brune     DMSubDomainHookLink link,next;
669be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
670be081cd6SPeter Brune       next = link->next;
671be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
672be081cd6SPeter Brune     }
6730298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
674be081cd6SPeter Brune   }
675baf369e7SPeter Brune   {
676baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
677baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
678baf369e7SPeter Brune       next = link->next;
679baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
680baf369e7SPeter Brune     }
6810298fd71SBarry Smith     (*dm)->gtolhook = NULL;
682baf369e7SPeter Brune   }
683d4d07f1eSToby Isaac   {
684d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
685d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
686d4d07f1eSToby Isaac       next = link->next;
687d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
688d4d07f1eSToby Isaac     }
689d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
690d4d07f1eSToby Isaac   }
691aa1993deSMatthew G Knepley   /* Destroy the work arrays */
692aa1993deSMatthew G Knepley   {
693aa1993deSMatthew G Knepley     DMWorkLink link,next;
694aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
695aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
696aa1993deSMatthew G Knepley       next = link->next;
697aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
698aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
699aa1993deSMatthew G Knepley     }
7000298fd71SBarry Smith     (*dm)->workin = NULL;
701aa1993deSMatthew G Knepley   }
702c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
703c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
704c58f1c22SToby Isaac 
705c58f1c22SToby Isaac     /* destroy the labels */
706c58f1c22SToby Isaac     while (next) {
707c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
708c58f1c22SToby Isaac 
709c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
710c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
711c58f1c22SToby Isaac       next = tmp;
712c58f1c22SToby Isaac     }
713c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
714c58f1c22SToby Isaac   }
715e6f8dbb6SToby Isaac   {
716e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
717e6f8dbb6SToby Isaac     while (next) {
718e6f8dbb6SToby Isaac       DMBoundary b = next;
719e6f8dbb6SToby Isaac 
720e6f8dbb6SToby Isaac       next = b->next;
721e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
722e6f8dbb6SToby Isaac     }
723e6f8dbb6SToby Isaac   }
724b17ce1afSJed Brown 
72552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
72652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72852536dc3SBarry Smith 
7291a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7301a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7311a266240SBarry Smith   }
73287e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
73371cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7344dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7356bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7366bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
737073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
73888ed4aceSMatthew G Knepley 
73988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
74088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
7418b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
742fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
743fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
74488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
74588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
746736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
747736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7488e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
749736995cdSBlaise Bourdin     }
750736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
751736995cdSBlaise Bourdin   }
75288bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75388bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75488bdff64SToby Isaac   }
755a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75688bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
75788bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
75888bdff64SToby Isaac   }
75988bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
760f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7616636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7626636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7636636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7645dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
7656636e97aSMatthew G Knepley 
7662764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
76714f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
768e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
769e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
770732e2eb9SMatthew G Knepley 
771ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7726bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
773ed3c66a1SDave May   }
774435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
775732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
77647c6ae99SBarry Smith   PetscFunctionReturn(0);
77747c6ae99SBarry Smith }
77847c6ae99SBarry Smith 
779d7bf68aeSBarry Smith /*@
780d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
781d7bf68aeSBarry Smith 
782d7bf68aeSBarry Smith     Collective on DM
783d7bf68aeSBarry Smith 
784d7bf68aeSBarry Smith     Input Parameter:
785d7bf68aeSBarry Smith .   dm - the DM object to setup
786d7bf68aeSBarry Smith 
787d7bf68aeSBarry Smith     Level: developer
788d7bf68aeSBarry Smith 
789e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
790d7bf68aeSBarry Smith 
791d7bf68aeSBarry Smith @*/
7927087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
793d7bf68aeSBarry Smith {
794d7bf68aeSBarry Smith   PetscErrorCode ierr;
795d7bf68aeSBarry Smith 
796d7bf68aeSBarry Smith   PetscFunctionBegin;
797171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7988387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
799d7bf68aeSBarry Smith   if (dm->ops->setup) {
800d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
801d7bf68aeSBarry Smith   }
8028387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
803d7bf68aeSBarry Smith   PetscFunctionReturn(0);
804d7bf68aeSBarry Smith }
805d7bf68aeSBarry Smith 
806d7bf68aeSBarry Smith /*@
807d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
808d7bf68aeSBarry Smith 
809d7bf68aeSBarry Smith     Collective on DM
810d7bf68aeSBarry Smith 
811d7bf68aeSBarry Smith     Input Parameter:
812d7bf68aeSBarry Smith .   dm - the DM object to set options for
813d7bf68aeSBarry Smith 
814732e2eb9SMatthew G Knepley     Options Database:
8154164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8164164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8174164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8188f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
819732e2eb9SMatthew G Knepley 
820d7bf68aeSBarry Smith     Level: developer
821d7bf68aeSBarry Smith 
822e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith @*/
8257087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
826d7bf68aeSBarry Smith {
8277781c08eSBarry Smith   char           typeName[256];
828ca266f36SBarry Smith   PetscBool      flg;
829d7bf68aeSBarry Smith   PetscErrorCode ierr;
830d7bf68aeSBarry Smith 
831d7bf68aeSBarry Smith   PetscFunctionBegin;
832171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
833f1fd5e65SToby Isaac   if (dm->prob) {
834f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
835f1fd5e65SToby Isaac   }
836691be533SLawrence Mitchell   if (dm->sf) {
837691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
838691be533SLawrence Mitchell   }
839691be533SLawrence Mitchell   if (dm->defaultSF) {
840691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
841691be533SLawrence Mitchell   }
8423194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8430298fd71SBarry 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);
844a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
845f9ba7244SBarry Smith   if (flg) {
846f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
847f9ba7244SBarry Smith   }
848a264d7a6SBarry 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);
849073dac72SJed Brown   if (flg) {
850521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
851073dac72SJed Brown   }
8528f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
853f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
854e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
855f9ba7244SBarry Smith   }
856f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8570633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
859d7bf68aeSBarry Smith   PetscFunctionReturn(0);
860d7bf68aeSBarry Smith }
861d7bf68aeSBarry Smith 
862fc9bc008SSatish Balay /*@C
863224748a4SBarry Smith     DMView - Views a DM
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith     Collective on DM
86647c6ae99SBarry Smith 
86747c6ae99SBarry Smith     Input Parameter:
86847c6ae99SBarry Smith +   dm - the DM object to view
86947c6ae99SBarry Smith -   v - the viewer
87047c6ae99SBarry Smith 
871224748a4SBarry Smith     Level: beginner
87247c6ae99SBarry Smith 
873e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith @*/
8767087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
87747c6ae99SBarry Smith {
87847c6ae99SBarry Smith   PetscErrorCode    ierr;
87932c0f0efSBarry Smith   PetscBool         isbinary;
88076a8abe0SBarry Smith   PetscMPIInt       size;
88176a8abe0SBarry Smith   PetscViewerFormat format;
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith   PetscFunctionBegin;
884171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8853014e516SBarry Smith   if (!v) {
886ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8873014e516SBarry Smith   }
88876a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
88976a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
89076a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
89198c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
89232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
89332c0f0efSBarry Smith   if (isbinary) {
89455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
89532c0f0efSBarry Smith     char     type[256];
89632c0f0efSBarry Smith 
89732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
89832c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
89932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
90032c0f0efSBarry Smith   }
9010c010503SBarry Smith   if (dm->ops->view) {
9020c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
90347c6ae99SBarry Smith   }
90447c6ae99SBarry Smith   PetscFunctionReturn(0);
90547c6ae99SBarry Smith }
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith /*@
9088472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
90947c6ae99SBarry Smith 
91047c6ae99SBarry Smith     Collective on DM
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Input Parameter:
91347c6ae99SBarry Smith .   dm - the DM object
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith     Output Parameter:
91647c6ae99SBarry Smith .   vec - the global vector
91747c6ae99SBarry Smith 
918073dac72SJed Brown     Level: beginner
91947c6ae99SBarry Smith 
920e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
92147c6ae99SBarry Smith 
92247c6ae99SBarry Smith @*/
9237087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
92447c6ae99SBarry Smith {
92547c6ae99SBarry Smith   PetscErrorCode ierr;
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith   PetscFunctionBegin;
928171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92947c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
93047c6ae99SBarry Smith   PetscFunctionReturn(0);
93147c6ae99SBarry Smith }
93247c6ae99SBarry Smith 
93347c6ae99SBarry Smith /*@
9348472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith     Not Collective
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith     Input Parameter:
93947c6ae99SBarry Smith .   dm - the DM object
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith     Output Parameter:
94247c6ae99SBarry Smith .   vec - the local vector
94347c6ae99SBarry Smith 
944073dac72SJed Brown     Level: beginner
94547c6ae99SBarry Smith 
946e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith @*/
9497087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
95047c6ae99SBarry Smith {
95147c6ae99SBarry Smith   PetscErrorCode ierr;
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith   PetscFunctionBegin;
954171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
95547c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
95647c6ae99SBarry Smith   PetscFunctionReturn(0);
95747c6ae99SBarry Smith }
95847c6ae99SBarry Smith 
9591411c6eeSJed Brown /*@
9601411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9611411c6eeSJed Brown 
9621411c6eeSJed Brown    Collective on DM
9631411c6eeSJed Brown 
9641411c6eeSJed Brown    Input Parameter:
9651411c6eeSJed Brown .  dm - the DM that provides the mapping
9661411c6eeSJed Brown 
9671411c6eeSJed Brown    Output Parameter:
9681411c6eeSJed Brown .  ltog - the mapping
9691411c6eeSJed Brown 
9701411c6eeSJed Brown    Level: intermediate
9711411c6eeSJed Brown 
9721411c6eeSJed Brown    Notes:
9731411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9741411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9751411c6eeSJed Brown 
976fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9771411c6eeSJed Brown @*/
9787087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9791411c6eeSJed Brown {
9800be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9811411c6eeSJed Brown   PetscErrorCode ierr;
9821411c6eeSJed Brown 
9831411c6eeSJed Brown   PetscFunctionBegin;
9841411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9851411c6eeSJed Brown   PetscValidPointer(ltog,2);
9861411c6eeSJed Brown   if (!dm->ltogmap) {
98737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
98837d0c07bSMatthew G Knepley 
989e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
99037d0c07bSMatthew G Knepley     if (section) {
991a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
99237d0c07bSMatthew G Knepley       PetscInt       *ltog;
993ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
99437d0c07bSMatthew G Knepley 
995e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
99637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
997ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
998ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
99937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1000a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
100137d0c07bSMatthew G Knepley 
100237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
100337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1004a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1005a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
100637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10071a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10081a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10091a7dc684SMatthew G. Knepley         if (dof) {
1010b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1011b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10121a7dc684SMatthew G. Knepley         }
101337d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1014a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1015a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
101637d0c07bSMatthew G Knepley         }
101737d0c07bSMatthew G Knepley       }
1018bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10190be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10200be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10210be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10220be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10237591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10247591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1025ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1026ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1027ccf3bd66SMatthew G. Knepley         n /= bs;
1028ccf3bd66SMatthew G. Knepley       }
1029ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10303bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
103137d0c07bSMatthew G Knepley     } else {
1032184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1033184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10341411c6eeSJed Brown     }
103537d0c07bSMatthew G Knepley   }
10361411c6eeSJed Brown   *ltog = dm->ltogmap;
10371411c6eeSJed Brown   PetscFunctionReturn(0);
10381411c6eeSJed Brown }
10391411c6eeSJed Brown 
10401411c6eeSJed Brown /*@
10411411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10421411c6eeSJed Brown 
10431411c6eeSJed Brown    Not Collective
10441411c6eeSJed Brown 
10451411c6eeSJed Brown    Input Parameter:
10461411c6eeSJed Brown .  dm - the DM with block structure
10471411c6eeSJed Brown 
10481411c6eeSJed Brown    Output Parameter:
10491411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10501411c6eeSJed Brown 
10511411c6eeSJed Brown    Level: intermediate
10521411c6eeSJed Brown 
1053fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10541411c6eeSJed Brown @*/
10557087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10561411c6eeSJed Brown {
10571411c6eeSJed Brown   PetscFunctionBegin;
10581411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10591411c6eeSJed Brown   PetscValidPointer(bs,2);
10601411c6eeSJed 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");
10611411c6eeSJed Brown   *bs = dm->bs;
10621411c6eeSJed Brown   PetscFunctionReturn(0);
10631411c6eeSJed Brown }
10641411c6eeSJed Brown 
106547c6ae99SBarry Smith /*@
10668472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
106747c6ae99SBarry Smith 
106847c6ae99SBarry Smith     Collective on DM
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith     Input Parameter:
107147c6ae99SBarry Smith +   dm1 - the DM object
107247c6ae99SBarry Smith -   dm2 - the second, finer DM object
107347c6ae99SBarry Smith 
107447c6ae99SBarry Smith     Output Parameter:
107547c6ae99SBarry Smith +  mat - the interpolation
107647c6ae99SBarry Smith -  vec - the scaling (optional)
107747c6ae99SBarry Smith 
107847c6ae99SBarry Smith     Level: developer
107947c6ae99SBarry Smith 
108095452b02SPatrick Sanan     Notes:
108195452b02SPatrick 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
108285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1083d52bd9f3SBarry Smith 
10841f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1085d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
108685afcc9aSBarry Smith 
108785afcc9aSBarry Smith 
10883ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
108947c6ae99SBarry Smith 
109047c6ae99SBarry Smith @*/
1091e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
109247c6ae99SBarry Smith {
109347c6ae99SBarry Smith   PetscErrorCode ierr;
109447c6ae99SBarry Smith 
109547c6ae99SBarry Smith   PetscFunctionBegin;
1096171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1097171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1098c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1099c7d20fa0SStefano Zampini   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInterpolation not implemented for type %s",((PetscObject)dm1)->type_name);
1100cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110125296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1102cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110347c6ae99SBarry Smith   PetscFunctionReturn(0);
110447c6ae99SBarry Smith }
110547c6ae99SBarry Smith 
11063ad4599aSBarry Smith /*@
11073ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11083ad4599aSBarry Smith 
11093ad4599aSBarry Smith     Collective on DM
11103ad4599aSBarry Smith 
11113ad4599aSBarry Smith     Input Parameter:
11123ad4599aSBarry Smith +   dm1 - the DM object
11133ad4599aSBarry Smith -   dm2 - the second, finer DM object
11143ad4599aSBarry Smith 
11153ad4599aSBarry Smith     Output Parameter:
11163ad4599aSBarry Smith .  mat - the restriction
11173ad4599aSBarry Smith 
11183ad4599aSBarry Smith 
11193ad4599aSBarry Smith     Level: developer
11203ad4599aSBarry Smith 
112195452b02SPatrick Sanan     Notes:
112295452b02SPatrick 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
11233ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11243ad4599aSBarry Smith 
11253ad4599aSBarry Smith 
11263ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11273ad4599aSBarry Smith 
11283ad4599aSBarry Smith @*/
11293ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11303ad4599aSBarry Smith {
11313ad4599aSBarry Smith   PetscErrorCode ierr;
11323ad4599aSBarry Smith 
11333ad4599aSBarry Smith   PetscFunctionBegin;
11343ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11353ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11363ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11377294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11383ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11393ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11403ad4599aSBarry Smith   PetscFunctionReturn(0);
11413ad4599aSBarry Smith }
11423ad4599aSBarry Smith 
114347c6ae99SBarry Smith /*@
11448472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
114547c6ae99SBarry Smith 
114647c6ae99SBarry Smith     Collective on DM
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith     Input Parameter:
114947c6ae99SBarry Smith +   dm1 - the DM object
115047c6ae99SBarry Smith -   dm2 - the second, finer DM object
115147c6ae99SBarry Smith 
115247c6ae99SBarry Smith     Output Parameter:
11536dbf9973SLawrence Mitchell .   mat - the injection
115447c6ae99SBarry Smith 
115547c6ae99SBarry Smith     Level: developer
115647c6ae99SBarry Smith 
115795452b02SPatrick Sanan    Notes:
115895452b02SPatrick 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
115985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
116085afcc9aSBarry Smith 
1161e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
116247c6ae99SBarry Smith 
116347c6ae99SBarry Smith @*/
11646dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
116547c6ae99SBarry Smith {
116647c6ae99SBarry Smith   PetscErrorCode ierr;
116747c6ae99SBarry Smith 
116847c6ae99SBarry Smith   PetscFunctionBegin;
1169171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1170171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11710c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11726dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
117347c6ae99SBarry Smith   PetscFunctionReturn(0);
117447c6ae99SBarry Smith }
117547c6ae99SBarry Smith 
1176b412c318SBarry Smith /*@
1177bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1178bd041c0cSMatthew G. Knepley 
1179bd041c0cSMatthew G. Knepley   Collective on DM
1180bd041c0cSMatthew G. Knepley 
1181bd041c0cSMatthew G. Knepley   Input Parameter:
1182bd041c0cSMatthew G. Knepley + dm1 - the DM object
1183bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1184bd041c0cSMatthew G. Knepley 
1185bd041c0cSMatthew G. Knepley   Output Parameter:
1186bd041c0cSMatthew G. Knepley . mat - the interpolation
1187bd041c0cSMatthew G. Knepley 
1188bd041c0cSMatthew G. Knepley   Level: developer
1189bd041c0cSMatthew G. Knepley 
1190bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1191bd041c0cSMatthew G. Knepley @*/
1192bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1193bd041c0cSMatthew G. Knepley {
1194bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1195bd041c0cSMatthew G. Knepley 
1196bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1197bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1198bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1199bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1200bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1201bd041c0cSMatthew G. Knepley }
1202bd041c0cSMatthew G. Knepley 
1203bd041c0cSMatthew G. Knepley /*@
1204b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
120547c6ae99SBarry Smith 
120647c6ae99SBarry Smith     Collective on DM
120747c6ae99SBarry Smith 
120847c6ae99SBarry Smith     Input Parameter:
120947c6ae99SBarry Smith +   dm - the DM object
12105bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
121147c6ae99SBarry Smith 
121247c6ae99SBarry Smith     Output Parameter:
121347c6ae99SBarry Smith .   coloring - the coloring
121447c6ae99SBarry Smith 
121547c6ae99SBarry Smith     Level: developer
121647c6ae99SBarry Smith 
1217b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
121847c6ae99SBarry Smith 
1219aab9d709SJed Brown @*/
1220b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
122147c6ae99SBarry Smith {
122247c6ae99SBarry Smith   PetscErrorCode ierr;
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith   PetscFunctionBegin;
1225171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1226ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1227b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
122847c6ae99SBarry Smith   PetscFunctionReturn(0);
122947c6ae99SBarry Smith }
123047c6ae99SBarry Smith 
1231b412c318SBarry Smith /*@
12328472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
123347c6ae99SBarry Smith 
123447c6ae99SBarry Smith     Collective on DM
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith     Input Parameter:
1237b412c318SBarry Smith .   dm - the DM object
123847c6ae99SBarry Smith 
123947c6ae99SBarry Smith     Output Parameter:
124047c6ae99SBarry Smith .   mat - the empty Jacobian
124147c6ae99SBarry Smith 
1242073dac72SJed Brown     Level: beginner
124347c6ae99SBarry Smith 
124495452b02SPatrick Sanan     Notes:
124595452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
124694013140SBarry Smith        do not need to do it yourself.
124794013140SBarry Smith 
124894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12497889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
125094013140SBarry Smith 
125194013140SBarry 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
125294013140SBarry Smith        internally by PETSc.
125394013140SBarry Smith 
125494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1255aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
125694013140SBarry Smith 
1257b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
125847c6ae99SBarry Smith 
1259aab9d709SJed Brown @*/
1260b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
126147c6ae99SBarry Smith {
126247c6ae99SBarry Smith   PetscErrorCode ierr;
126347c6ae99SBarry Smith 
126447c6ae99SBarry Smith   PetscFunctionBegin;
1265171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1266607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1267c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1268c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1269b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1270e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1271e571a35bSMatthew G. Knepley   if (dm->prob) {
1272e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1273e571a35bSMatthew G. Knepley     PetscInt     Nf;
1274e571a35bSMatthew G. Knepley 
1275e571a35bSMatthew G. Knepley     ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
1276e571a35bSMatthew G. Knepley     if (Nf == 1) {
1277e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1278e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1279e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1280e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1281e571a35bSMatthew G. Knepley       }
1282e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1283e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1284e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1285e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1286e571a35bSMatthew G. Knepley       }
1287e571a35bSMatthew G. Knepley     }
1288e571a35bSMatthew G. Knepley   }
128947c6ae99SBarry Smith   PetscFunctionReturn(0);
129047c6ae99SBarry Smith }
129147c6ae99SBarry Smith 
1292732e2eb9SMatthew G Knepley /*@
1293950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1294732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1295732e2eb9SMatthew G Knepley 
12968472ad0fSDave May   Logically Collective on DM
1297732e2eb9SMatthew G Knepley 
1298732e2eb9SMatthew G Knepley   Input Parameter:
1299732e2eb9SMatthew G Knepley + dm - the DM
1300732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1301732e2eb9SMatthew G Knepley 
1302732e2eb9SMatthew G Knepley   Level: developer
1303b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1304732e2eb9SMatthew G Knepley @*/
1305732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1306732e2eb9SMatthew G Knepley {
1307732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1308732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1309732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1310732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1311732e2eb9SMatthew G Knepley }
1312732e2eb9SMatthew G Knepley 
1313b06ff27eSHong Zhang /*@
1314b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1315b06ff27eSHong Zhang     but the array for values will not be allocated.
1316b06ff27eSHong Zhang 
1317b06ff27eSHong Zhang   Logically Collective on DM
1318b06ff27eSHong Zhang 
1319b06ff27eSHong Zhang   Input Parameter:
1320b06ff27eSHong Zhang + dm - the DM
1321b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1322b06ff27eSHong Zhang 
1323b06ff27eSHong Zhang   Level: developer
1324b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1325b06ff27eSHong Zhang @*/
1326b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1327b06ff27eSHong Zhang {
1328b06ff27eSHong Zhang   PetscFunctionBegin;
1329b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1330b06ff27eSHong Zhang   dm->structure_only = only;
1331b06ff27eSHong Zhang   PetscFunctionReturn(0);
1332b06ff27eSHong Zhang }
1333b06ff27eSHong Zhang 
1334a89ea682SMatthew G Knepley /*@C
1335aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1336a89ea682SMatthew G Knepley 
1337a89ea682SMatthew G Knepley   Not Collective
1338a89ea682SMatthew G Knepley 
1339a89ea682SMatthew G Knepley   Input Parameters:
1340a89ea682SMatthew G Knepley + dm - the DM object
1341aa1993deSMatthew G Knepley . count - The minium size
134269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1343a89ea682SMatthew G Knepley 
1344a89ea682SMatthew G Knepley   Output Parameter:
1345a89ea682SMatthew G Knepley . array - the work array
1346a89ea682SMatthew G Knepley 
1347a89ea682SMatthew G Knepley   Level: developer
1348a89ea682SMatthew G Knepley 
1349a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1350a89ea682SMatthew G Knepley @*/
135169291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1352a89ea682SMatthew G Knepley {
1353a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1354aa1993deSMatthew G Knepley   DMWorkLink     link;
135569291d52SBarry Smith   PetscMPIInt    dsize;
1356a89ea682SMatthew G Knepley 
1357a89ea682SMatthew G Knepley   PetscFunctionBegin;
1358a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1359aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1360aa1993deSMatthew G Knepley   if (dm->workin) {
1361aa1993deSMatthew G Knepley     link       = dm->workin;
1362aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1363aa1993deSMatthew G Knepley   } else {
1364b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1365a89ea682SMatthew G Knepley   }
136669291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13675056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1368aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1369854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1370854ce69bSBarry Smith     link->bytes = dsize*count;
1371aa1993deSMatthew G Knepley   }
1372aa1993deSMatthew G Knepley   link->next   = dm->workout;
1373aa1993deSMatthew G Knepley   dm->workout  = link;
1374aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1375a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1376a89ea682SMatthew G Knepley }
1377a89ea682SMatthew G Knepley 
1378aa1993deSMatthew G Knepley /*@C
1379aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1380aa1993deSMatthew G Knepley 
1381aa1993deSMatthew G Knepley   Not Collective
1382aa1993deSMatthew G Knepley 
1383aa1993deSMatthew G Knepley   Input Parameters:
1384aa1993deSMatthew G Knepley + dm - the DM object
1385aa1993deSMatthew G Knepley . count - The minium size
138669291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1387aa1993deSMatthew G Knepley 
1388aa1993deSMatthew G Knepley   Output Parameter:
1389aa1993deSMatthew G Knepley . array - the work array
1390aa1993deSMatthew G Knepley 
1391aa1993deSMatthew G Knepley   Level: developer
1392aa1993deSMatthew G Knepley 
139395452b02SPatrick Sanan   Developer Notes:
139495452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1395aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1396aa1993deSMatthew G Knepley @*/
139769291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1398aa1993deSMatthew G Knepley {
1399aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1400aa1993deSMatthew G Knepley 
1401aa1993deSMatthew G Knepley   PetscFunctionBegin;
1402aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1403aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1404aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1405aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1406aa1993deSMatthew G Knepley       *p           = link->next;
1407aa1993deSMatthew G Knepley       link->next   = dm->workin;
1408aa1993deSMatthew G Knepley       dm->workin   = link;
14090298fd71SBarry Smith       *(void**)mem = NULL;
1410aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1411aa1993deSMatthew G Knepley     }
1412aa1993deSMatthew G Knepley   }
1413aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1414aa1993deSMatthew G Knepley }
1415e7c4fc90SDmitry Karpeev 
1416435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1417435a35e8SMatthew G Knepley {
1418435a35e8SMatthew G Knepley   PetscFunctionBegin;
1419435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
142082f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1421435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1422435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1423435a35e8SMatthew G Knepley }
1424435a35e8SMatthew G Knepley 
14250a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14260a50eb56SMatthew G. Knepley {
14270a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14280a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1429f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14300a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14310a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14320a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14330a50eb56SMatthew G. Knepley }
14340a50eb56SMatthew G. Knepley 
1435f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1436f9d4088aSMatthew G. Knepley {
1437f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1438f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1439f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1440f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1441f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1442f9d4088aSMatthew G. Knepley }
1443f9d4088aSMatthew G. Knepley 
1444f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1445f9d4088aSMatthew G. Knepley {
1446f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1447f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1448f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1449f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1450f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1451f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1452f9d4088aSMatthew G. Knepley }
1453f9d4088aSMatthew G. Knepley 
14544f3b5142SJed Brown /*@C
14554d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14564d343eeaSMatthew G Knepley 
14574d343eeaSMatthew G Knepley   Not collective
14584d343eeaSMatthew G Knepley 
14594d343eeaSMatthew G Knepley   Input Parameter:
14604d343eeaSMatthew G Knepley . dm - the DM object
14614d343eeaSMatthew G Knepley 
14624d343eeaSMatthew G Knepley   Output Parameters:
14630298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14640298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14650298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14664d343eeaSMatthew G Knepley 
14674d343eeaSMatthew G Knepley   Level: intermediate
14684d343eeaSMatthew G Knepley 
146921c9b008SJed Brown   Notes:
147021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
147121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
147221c9b008SJed Brown   PetscFree().
147321c9b008SJed Brown 
14744d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14754d343eeaSMatthew G Knepley @*/
147637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14774d343eeaSMatthew G Knepley {
147837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14794d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14804d343eeaSMatthew G Knepley 
14814d343eeaSMatthew G Knepley   PetscFunctionBegin;
14824d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
148369ca1f37SDmitry Karpeev   if (numFields) {
148469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
148569ca1f37SDmitry Karpeev     *numFields = 0;
148669ca1f37SDmitry Karpeev   }
148737d0c07bSMatthew G Knepley   if (fieldNames) {
148837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14890298fd71SBarry Smith     *fieldNames = NULL;
149069ca1f37SDmitry Karpeev   }
149169ca1f37SDmitry Karpeev   if (fields) {
149269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
14930298fd71SBarry Smith     *fields = NULL;
149469ca1f37SDmitry Karpeev   }
1495e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
149637d0c07bSMatthew G Knepley   if (section) {
14973a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
149837d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
149937d0c07bSMatthew G Knepley 
1500e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
150137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15023a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
150337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
150437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
150537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15063a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
150737d0c07bSMatthew G Knepley     }
150837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
150937d0c07bSMatthew G Knepley       PetscInt gdof;
151037d0c07bSMatthew G Knepley 
151137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
151237d0c07bSMatthew G Knepley       if (gdof > 0) {
151337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15143a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
151537d0c07bSMatthew G Knepley 
151637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
151737d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15183a544194SStefano Zampini           fpdof = fdof-fcdof;
15193a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15203a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15213a544194SStefano Zampini             fieldNc[f] = 1;
15223a544194SStefano Zampini           }
15233a544194SStefano Zampini           fieldSizes[f] += fpdof;
152437d0c07bSMatthew G Knepley         }
152537d0c07bSMatthew G Knepley       }
152637d0c07bSMatthew G Knepley     }
152737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1528785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
152937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
153037d0c07bSMatthew G Knepley     }
153137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
153237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
153337d0c07bSMatthew G Knepley 
153437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
153537d0c07bSMatthew G Knepley       if (gdof > 0) {
153637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
153737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
153837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
153937d0c07bSMatthew G Knepley 
154037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
154137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
154237d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
154337d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
154437d0c07bSMatthew G Knepley           }
154537d0c07bSMatthew G Knepley         }
154637d0c07bSMatthew G Knepley       }
154737d0c07bSMatthew G Knepley     }
15488865f1eaSKarl Rupp     if (numFields) *numFields = nF;
154937d0c07bSMatthew G Knepley     if (fieldNames) {
1550785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
155137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
155237d0c07bSMatthew G Knepley         const char *fieldName;
155337d0c07bSMatthew G Knepley 
155437d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
155537d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
155637d0c07bSMatthew G Knepley       }
155737d0c07bSMatthew G Knepley     }
155837d0c07bSMatthew G Knepley     if (fields) {
1559785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
156037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15613a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15623a544194SStefano Zampini 
156382f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15643a544194SStefano Zampini         in[0] = -fieldNc[f];
15653a544194SStefano Zampini         in[1] = fieldNc[f];
15663a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15673a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15683a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
156937d0c07bSMatthew G Knepley       }
157037d0c07bSMatthew G Knepley     }
15713a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15728865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15738865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
157469ca1f37SDmitry Karpeev   }
15754d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15764d343eeaSMatthew G Knepley }
15774d343eeaSMatthew G Knepley 
157816621825SDmitry Karpeev 
157916621825SDmitry Karpeev /*@C
158016621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
158116621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
158216621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1583e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1584e7c4fc90SDmitry Karpeev 
1585e7c4fc90SDmitry Karpeev   Not collective
1586e7c4fc90SDmitry Karpeev 
1587e7c4fc90SDmitry Karpeev   Input Parameter:
1588e7c4fc90SDmitry Karpeev . dm - the DM object
1589e7c4fc90SDmitry Karpeev 
1590e7c4fc90SDmitry Karpeev   Output Parameters:
15910298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15920298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15930298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15940298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1595e7c4fc90SDmitry Karpeev 
1596e7c4fc90SDmitry Karpeev   Level: intermediate
1597e7c4fc90SDmitry Karpeev 
1598e7c4fc90SDmitry Karpeev   Notes:
1599e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1600e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1601e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1602e7c4fc90SDmitry Karpeev 
1603e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1604e7c4fc90SDmitry Karpeev @*/
160516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1606e7c4fc90SDmitry Karpeev {
1607e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1608e7c4fc90SDmitry Karpeev 
1609e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1610e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16118865f1eaSKarl Rupp   if (len) {
16128865f1eaSKarl Rupp     PetscValidPointer(len,2);
16138865f1eaSKarl Rupp     *len = 0;
16148865f1eaSKarl Rupp   }
16158865f1eaSKarl Rupp   if (namelist) {
16168865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16178865f1eaSKarl Rupp     *namelist = 0;
16188865f1eaSKarl Rupp   }
16198865f1eaSKarl Rupp   if (islist) {
16208865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16218865f1eaSKarl Rupp     *islist = 0;
16228865f1eaSKarl Rupp   }
16238865f1eaSKarl Rupp   if (dmlist) {
16248865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16258865f1eaSKarl Rupp     *dmlist = 0;
16268865f1eaSKarl Rupp   }
1627f3f0edfdSDmitry Karpeev   /*
1628f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1629f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1630f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1631f3f0edfdSDmitry Karpeev    */
1632ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
163316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1634435a35e8SMatthew G Knepley     PetscSection section;
1635435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1636435a35e8SMatthew G Knepley 
1637e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1638435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1639435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1640f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
164103dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
164203dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
164303dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1644435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1645435a35e8SMatthew G Knepley         const char *fieldName;
1646435a35e8SMatthew G Knepley 
164703dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
164803dc3394SMatthew G. Knepley         if (namelist) {
1649435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1650435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1651435a35e8SMatthew G Knepley         }
165203dc3394SMatthew G. Knepley       }
1653435a35e8SMatthew G Knepley     } else {
165469ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1655e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16560298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1657e7c4fc90SDmitry Karpeev     }
16588865f1eaSKarl Rupp   } else {
165916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
166016621825SDmitry Karpeev   }
166116621825SDmitry Karpeev   PetscFunctionReturn(0);
166216621825SDmitry Karpeev }
166316621825SDmitry Karpeev 
1664564cec59SMatthew G. Knepley /*@
1665435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1666435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1667435a35e8SMatthew G Knepley 
1668435a35e8SMatthew G Knepley   Not collective
1669435a35e8SMatthew G Knepley 
1670435a35e8SMatthew G Knepley   Input Parameters:
16712adcc780SMatthew G. Knepley + dm        - The DM object
16722adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16732adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1674435a35e8SMatthew G Knepley 
1675435a35e8SMatthew G Knepley   Output Parameters:
16762adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16772adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1678435a35e8SMatthew G Knepley 
16795d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16805d3b26e6SMatthew G. Knepley 
1681435a35e8SMatthew G Knepley   Level: intermediate
1682435a35e8SMatthew G Knepley 
16835d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1684435a35e8SMatthew G Knepley @*/
168537bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1686435a35e8SMatthew G Knepley {
1687435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1688435a35e8SMatthew G Knepley 
1689435a35e8SMatthew G Knepley   PetscFunctionBegin;
1690435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1691435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16928865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16938865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1694435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1695435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
169682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1697435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1698435a35e8SMatthew G Knepley }
1699435a35e8SMatthew G Knepley 
17002adcc780SMatthew G. Knepley /*@C
17012adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17022adcc780SMatthew G. Knepley 
17032adcc780SMatthew G. Knepley   Not collective
17042adcc780SMatthew G. Knepley 
17052adcc780SMatthew G. Knepley   Input Parameter:
17062adcc780SMatthew G. Knepley + dms - The DM objects
17072adcc780SMatthew G. Knepley - len - The number of DMs
17082adcc780SMatthew G. Knepley 
17092adcc780SMatthew G. Knepley   Output Parameters:
1710a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17112adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17122adcc780SMatthew G. Knepley 
17135d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17145d3b26e6SMatthew G. Knepley 
17152adcc780SMatthew G. Knepley   Level: intermediate
17162adcc780SMatthew G. Knepley 
17175d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17182adcc780SMatthew G. Knepley @*/
17192adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17202adcc780SMatthew G. Knepley {
17212adcc780SMatthew G. Knepley   PetscInt       i;
17222adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17232adcc780SMatthew G. Knepley 
17242adcc780SMatthew G. Knepley   PetscFunctionBegin;
17252adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17262adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17272adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1728a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17292adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17302adcc780SMatthew G. Knepley   if (len) {
1731a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1732a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17332adcc780SMatthew G. Knepley   }
17342adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17352adcc780SMatthew G. Knepley }
17362adcc780SMatthew G. Knepley 
173716621825SDmitry Karpeev 
173816621825SDmitry Karpeev /*@C
17398d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17408d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17418d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17428d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17438d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
174416621825SDmitry Karpeev 
174516621825SDmitry Karpeev   Not collective
174616621825SDmitry Karpeev 
174716621825SDmitry Karpeev   Input Parameter:
174816621825SDmitry Karpeev . dm - the DM object
174916621825SDmitry Karpeev 
175016621825SDmitry Karpeev   Output Parameters:
17510298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17520298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17530298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17540298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17550298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
175616621825SDmitry Karpeev 
175716621825SDmitry Karpeev   Level: intermediate
175816621825SDmitry Karpeev 
175916621825SDmitry Karpeev   Notes:
176016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
176116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
176216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
176316621825SDmitry Karpeev 
17648d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
176516621825SDmitry Karpeev @*/
17668d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
176716621825SDmitry Karpeev {
176816621825SDmitry Karpeev   PetscErrorCode      ierr;
1769be081cd6SPeter Brune   DMSubDomainHookLink link;
1770be081cd6SPeter Brune   PetscInt            i,l;
177116621825SDmitry Karpeev 
177216621825SDmitry Karpeev   PetscFunctionBegin;
177316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
177414a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17750298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17760298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17770298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17780298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1779f3f0edfdSDmitry Karpeev   /*
1780f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1781f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1782f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1783f3f0edfdSDmitry Karpeev    */
1784ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
178516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1786be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
178714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1788f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1789be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1790be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1791be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1792be081cd6SPeter Brune         }
1793648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1794e7c4fc90SDmitry Karpeev       }
179514a18fd3SPeter Brune     }
179614a18fd3SPeter Brune     if (len) *len = l;
179714a18fd3SPeter Brune   }
1798e30e807fSPeter Brune   PetscFunctionReturn(0);
1799e30e807fSPeter Brune }
1800e30e807fSPeter Brune 
1801e30e807fSPeter Brune 
1802e30e807fSPeter Brune /*@C
1803e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1804e30e807fSPeter Brune 
1805e30e807fSPeter Brune   Not collective
1806e30e807fSPeter Brune 
1807e30e807fSPeter Brune   Input Parameters:
1808e30e807fSPeter Brune + dm - the DM object
1809e30e807fSPeter Brune . n  - the number of subdomain scatters
1810e30e807fSPeter Brune - subdms - the local subdomains
1811e30e807fSPeter Brune 
1812e30e807fSPeter Brune   Output Parameters:
1813e30e807fSPeter Brune + n     - the number of scatters returned
1814e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1815e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1816e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1817e30e807fSPeter Brune 
181895452b02SPatrick Sanan   Notes:
181995452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1820e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1821e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1822e30e807fSPeter Brune   solution and residual data.
1823e30e807fSPeter Brune 
1824e30e807fSPeter Brune   Level: developer
1825e30e807fSPeter Brune 
1826e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1827e30e807fSPeter Brune @*/
1828e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1829e30e807fSPeter Brune {
1830e30e807fSPeter Brune   PetscErrorCode ierr;
1831e30e807fSPeter Brune 
1832e30e807fSPeter Brune   PetscFunctionBegin;
1833e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1834e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1835e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1836e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18376668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1838e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1839e7c4fc90SDmitry Karpeev }
1840e7c4fc90SDmitry Karpeev 
184147c6ae99SBarry Smith /*@
184247c6ae99SBarry Smith   DMRefine - Refines a DM object
184347c6ae99SBarry Smith 
184447c6ae99SBarry Smith   Collective on DM
184547c6ae99SBarry Smith 
184647c6ae99SBarry Smith   Input Parameter:
184747c6ae99SBarry Smith + dm   - the DM object
184891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
184947c6ae99SBarry Smith 
185047c6ae99SBarry Smith   Output Parameter:
18510298fd71SBarry Smith . dmf - the refined DM, or NULL
1852ae0a1c52SMatthew G Knepley 
18530298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
185447c6ae99SBarry Smith 
185547c6ae99SBarry Smith   Level: developer
185647c6ae99SBarry Smith 
1857e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
185847c6ae99SBarry Smith @*/
18597087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
186047c6ae99SBarry Smith {
186147c6ae99SBarry Smith   PetscErrorCode   ierr;
1862c833c3b5SJed Brown   DMRefineHookLink link;
186347c6ae99SBarry Smith 
186447c6ae99SBarry Smith   PetscFunctionBegin;
1865732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18661ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1867fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
186847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18694057135bSMatthew G Knepley   if (*dmf) {
187043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18718865f1eaSKarl Rupp 
18728cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18738865f1eaSKarl Rupp 
1874644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18750598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1876656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18778865f1eaSKarl Rupp 
1878e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1879c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18808865f1eaSKarl Rupp       if (link->refinehook) {
18818865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18828865f1eaSKarl Rupp       }
1883c833c3b5SJed Brown     }
1884c833c3b5SJed Brown   }
18851ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1886c833c3b5SJed Brown   PetscFunctionReturn(0);
1887c833c3b5SJed Brown }
1888c833c3b5SJed Brown 
1889bb9467b5SJed Brown /*@C
1890c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1891c833c3b5SJed Brown 
1892c833c3b5SJed Brown    Logically Collective
1893c833c3b5SJed Brown 
1894c833c3b5SJed Brown    Input Arguments:
1895c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1896c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1897c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18980298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1899c833c3b5SJed Brown 
1900c833c3b5SJed Brown    Calling sequence of refinehook:
1901c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1902c833c3b5SJed Brown 
1903c833c3b5SJed Brown +  coarse - coarse level DM
1904c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1905c833c3b5SJed Brown -  ctx - optional user-defined function context
1906c833c3b5SJed Brown 
1907c833c3b5SJed Brown    Calling sequence for interphook:
1908c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1909c833c3b5SJed Brown 
1910c833c3b5SJed Brown +  coarse - coarse level DM
1911c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1912c833c3b5SJed Brown .  fine - fine level DM to update
1913c833c3b5SJed Brown -  ctx - optional user-defined function context
1914c833c3b5SJed Brown 
1915c833c3b5SJed Brown    Level: advanced
1916c833c3b5SJed Brown 
1917c833c3b5SJed Brown    Notes:
1918c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1919c833c3b5SJed Brown 
1920c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1921c833c3b5SJed Brown 
1922bb9467b5SJed Brown    This function is currently not available from Fortran.
1923bb9467b5SJed Brown 
1924c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1925c833c3b5SJed Brown @*/
1926c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1927c833c3b5SJed Brown {
1928c833c3b5SJed Brown   PetscErrorCode   ierr;
1929c833c3b5SJed Brown   DMRefineHookLink link,*p;
1930c833c3b5SJed Brown 
1931c833c3b5SJed Brown   PetscFunctionBegin;
1932c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19333d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19343d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19353d8e3701SJed Brown   }
193695dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1937c833c3b5SJed Brown   link->refinehook = refinehook;
1938c833c3b5SJed Brown   link->interphook = interphook;
1939c833c3b5SJed Brown   link->ctx        = ctx;
19400298fd71SBarry Smith   link->next       = NULL;
1941c833c3b5SJed Brown   *p               = link;
1942c833c3b5SJed Brown   PetscFunctionReturn(0);
1943c833c3b5SJed Brown }
1944c833c3b5SJed Brown 
19453d8e3701SJed Brown /*@C
19463d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19473d8e3701SJed Brown 
19483d8e3701SJed Brown    Logically Collective
19493d8e3701SJed Brown 
19503d8e3701SJed Brown    Input Arguments:
19513d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19523d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19533d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19543d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19553d8e3701SJed Brown 
19563d8e3701SJed Brown    Level: advanced
19573d8e3701SJed Brown 
19583d8e3701SJed Brown    Notes:
19593d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19603d8e3701SJed Brown 
19613d8e3701SJed Brown    This function is currently not available from Fortran.
19623d8e3701SJed Brown 
19633d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19643d8e3701SJed Brown @*/
19653d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19663d8e3701SJed Brown {
19673d8e3701SJed Brown   PetscErrorCode   ierr;
19683d8e3701SJed Brown   DMRefineHookLink link,*p;
19693d8e3701SJed Brown 
19703d8e3701SJed Brown   PetscFunctionBegin;
19713d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19723d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19733d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19743d8e3701SJed Brown       link = *p;
19753d8e3701SJed Brown       *p = link->next;
19763d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19773d8e3701SJed Brown       break;
19783d8e3701SJed Brown     }
19793d8e3701SJed Brown   }
19803d8e3701SJed Brown   PetscFunctionReturn(0);
19813d8e3701SJed Brown }
19823d8e3701SJed Brown 
1983c833c3b5SJed Brown /*@
1984c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1985c833c3b5SJed Brown 
1986c833c3b5SJed Brown    Collective if any hooks are
1987c833c3b5SJed Brown 
1988c833c3b5SJed Brown    Input Arguments:
1989c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1990e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1991c833c3b5SJed Brown -  fine - finer DM to update
1992c833c3b5SJed Brown 
1993c833c3b5SJed Brown    Level: developer
1994c833c3b5SJed Brown 
1995c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1996c833c3b5SJed Brown @*/
1997c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1998c833c3b5SJed Brown {
1999c833c3b5SJed Brown   PetscErrorCode   ierr;
2000c833c3b5SJed Brown   DMRefineHookLink link;
2001c833c3b5SJed Brown 
2002c833c3b5SJed Brown   PetscFunctionBegin;
2003c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20048865f1eaSKarl Rupp     if (link->interphook) {
20058865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20068865f1eaSKarl Rupp     }
20074057135bSMatthew G Knepley   }
200847c6ae99SBarry Smith   PetscFunctionReturn(0);
200947c6ae99SBarry Smith }
201047c6ae99SBarry Smith 
2011eb3f98d2SBarry Smith /*@
2012eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2013eb3f98d2SBarry Smith 
2014eb3f98d2SBarry Smith     Not Collective
2015eb3f98d2SBarry Smith 
2016eb3f98d2SBarry Smith     Input Parameter:
2017eb3f98d2SBarry Smith .   dm - the DM object
2018eb3f98d2SBarry Smith 
2019eb3f98d2SBarry Smith     Output Parameter:
2020eb3f98d2SBarry Smith .   level - number of refinements
2021eb3f98d2SBarry Smith 
2022eb3f98d2SBarry Smith     Level: developer
2023eb3f98d2SBarry Smith 
20246a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2025eb3f98d2SBarry Smith 
2026eb3f98d2SBarry Smith @*/
2027eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2028eb3f98d2SBarry Smith {
2029eb3f98d2SBarry Smith   PetscFunctionBegin;
2030eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2031eb3f98d2SBarry Smith   *level = dm->levelup;
2032eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2033eb3f98d2SBarry Smith }
2034eb3f98d2SBarry Smith 
2035fef3a512SBarry Smith /*@
2036fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2037fef3a512SBarry Smith 
2038fef3a512SBarry Smith     Not Collective
2039fef3a512SBarry Smith 
2040fef3a512SBarry Smith     Input Parameter:
2041fef3a512SBarry Smith +   dm - the DM object
2042fef3a512SBarry Smith -   level - number of refinements
2043fef3a512SBarry Smith 
2044fef3a512SBarry Smith     Level: advanced
2045fef3a512SBarry Smith 
204695452b02SPatrick Sanan     Notes:
204795452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2048fef3a512SBarry Smith 
2049fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2050fef3a512SBarry Smith 
2051fef3a512SBarry Smith @*/
2052fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2053fef3a512SBarry Smith {
2054fef3a512SBarry Smith   PetscFunctionBegin;
2055fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2056fef3a512SBarry Smith   dm->levelup = level;
2057fef3a512SBarry Smith   PetscFunctionReturn(0);
2058fef3a512SBarry Smith }
2059fef3a512SBarry Smith 
2060bb9467b5SJed Brown /*@C
2061baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2062baf369e7SPeter Brune 
2063baf369e7SPeter Brune    Logically Collective
2064baf369e7SPeter Brune 
2065baf369e7SPeter Brune    Input Arguments:
2066baf369e7SPeter Brune +  dm - the DM
2067baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2068baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20690298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2070baf369e7SPeter Brune 
2071baf369e7SPeter Brune    Calling sequence for beginhook:
2072baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2073baf369e7SPeter Brune 
2074baf369e7SPeter Brune +  dm - global DM
2075baf369e7SPeter Brune .  g - global vector
2076baf369e7SPeter Brune .  mode - mode
2077baf369e7SPeter Brune .  l - local vector
2078baf369e7SPeter Brune -  ctx - optional user-defined function context
2079baf369e7SPeter Brune 
2080baf369e7SPeter Brune 
2081baf369e7SPeter Brune    Calling sequence for endhook:
2082ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2083baf369e7SPeter Brune 
2084baf369e7SPeter Brune +  global - global DM
2085baf369e7SPeter Brune -  ctx - optional user-defined function context
2086baf369e7SPeter Brune 
2087baf369e7SPeter Brune    Level: advanced
2088baf369e7SPeter Brune 
2089baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2090baf369e7SPeter Brune @*/
2091baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2092baf369e7SPeter Brune {
2093baf369e7SPeter Brune   PetscErrorCode          ierr;
2094baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2095baf369e7SPeter Brune 
2096baf369e7SPeter Brune   PetscFunctionBegin;
2097baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2098baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
209995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2100baf369e7SPeter Brune   link->beginhook = beginhook;
2101baf369e7SPeter Brune   link->endhook   = endhook;
2102baf369e7SPeter Brune   link->ctx       = ctx;
21030298fd71SBarry Smith   link->next      = NULL;
2104baf369e7SPeter Brune   *p              = link;
2105baf369e7SPeter Brune   PetscFunctionReturn(0);
2106baf369e7SPeter Brune }
2107baf369e7SPeter Brune 
21084c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
21094c274da1SToby Isaac {
21104c274da1SToby Isaac   Mat cMat;
21114c274da1SToby Isaac   Vec cVec;
21124c274da1SToby Isaac   PetscSection section, cSec;
21134c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21144c274da1SToby Isaac   PetscErrorCode ierr;
21154c274da1SToby Isaac 
21164c274da1SToby Isaac   PetscFunctionBegin;
21174c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21184c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21194c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
21205db9a05bSToby Isaac     PetscInt nRows;
21215db9a05bSToby Isaac 
21225db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21235db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2124e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
21257711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21264c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
21274c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21284c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21294c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21304c274da1SToby Isaac       if (dof) {
21314c274da1SToby Isaac         PetscScalar *vals;
21324c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
21334c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
21344c274da1SToby Isaac       }
21354c274da1SToby Isaac     }
21364c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21374c274da1SToby Isaac   }
21384c274da1SToby Isaac   PetscFunctionReturn(0);
21394c274da1SToby Isaac }
21404c274da1SToby Isaac 
214147c6ae99SBarry Smith /*@
214201729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
214301729b5cSPatrick Sanan 
214401729b5cSPatrick Sanan     Neighbor-wise Collective on DM
214501729b5cSPatrick Sanan 
214601729b5cSPatrick Sanan     Input Parameters:
214701729b5cSPatrick Sanan +   dm - the DM object
214801729b5cSPatrick Sanan .   g - the global vector
214901729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
215001729b5cSPatrick Sanan -   l - the local vector
215101729b5cSPatrick Sanan 
215201729b5cSPatrick Sanan     Notes:
215301729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
215401729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
215501729b5cSPatrick Sanan 
215601729b5cSPatrick Sanan     Level: beginner
215701729b5cSPatrick Sanan 
215801729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
215901729b5cSPatrick Sanan 
216001729b5cSPatrick Sanan @*/
216101729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
216201729b5cSPatrick Sanan {
216301729b5cSPatrick Sanan   PetscErrorCode ierr;
216401729b5cSPatrick Sanan 
216501729b5cSPatrick Sanan   PetscFunctionBegin;
216601729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
216701729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
216801729b5cSPatrick Sanan   PetscFunctionReturn(0);
216901729b5cSPatrick Sanan }
217001729b5cSPatrick Sanan 
217101729b5cSPatrick Sanan /*@
217247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
217347c6ae99SBarry Smith 
217447c6ae99SBarry Smith     Neighbor-wise Collective on DM
217547c6ae99SBarry Smith 
217647c6ae99SBarry Smith     Input Parameters:
217747c6ae99SBarry Smith +   dm - the DM object
217847c6ae99SBarry Smith .   g - the global vector
217947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
218047c6ae99SBarry Smith -   l - the local vector
218147c6ae99SBarry Smith 
218201729b5cSPatrick Sanan     Level: intermediate
218347c6ae99SBarry Smith 
218401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
218547c6ae99SBarry Smith 
218647c6ae99SBarry Smith @*/
21877087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
218847c6ae99SBarry Smith {
21897128ae9fSMatthew G Knepley   PetscSF                 sf;
219047c6ae99SBarry Smith   PetscErrorCode          ierr;
2191baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
219247c6ae99SBarry Smith 
219347c6ae99SBarry Smith   PetscFunctionBegin;
2194171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2195baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
21968865f1eaSKarl Rupp     if (link->beginhook) {
21978865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
21988865f1eaSKarl Rupp     }
2199baf369e7SPeter Brune   }
22007128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22017128ae9fSMatthew G Knepley   if (sf) {
2202ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2203ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
22047128ae9fSMatthew G Knepley 
220582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22067128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2207ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22087128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22097128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2210ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22117128ae9fSMatthew G Knepley   } else {
221233907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2213843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22147128ae9fSMatthew G Knepley   }
221547c6ae99SBarry Smith   PetscFunctionReturn(0);
221647c6ae99SBarry Smith }
221747c6ae99SBarry Smith 
221847c6ae99SBarry Smith /*@
221947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
222047c6ae99SBarry Smith 
222147c6ae99SBarry Smith     Neighbor-wise Collective on DM
222247c6ae99SBarry Smith 
222347c6ae99SBarry Smith     Input Parameters:
222447c6ae99SBarry Smith +   dm - the DM object
222547c6ae99SBarry Smith .   g - the global vector
222647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
222747c6ae99SBarry Smith -   l - the local vector
222847c6ae99SBarry Smith 
222901729b5cSPatrick Sanan     Level: intermediate
223047c6ae99SBarry Smith 
223101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
223247c6ae99SBarry Smith 
223347c6ae99SBarry Smith @*/
22347087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
223547c6ae99SBarry Smith {
22367128ae9fSMatthew G Knepley   PetscSF                 sf;
223747c6ae99SBarry Smith   PetscErrorCode          ierr;
2238ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2239ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2240baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
224147c6ae99SBarry Smith 
224247c6ae99SBarry Smith   PetscFunctionBegin;
2243171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22447128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22457128ae9fSMatthew G Knepley   if (sf) {
224682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22477128ae9fSMatthew G Knepley 
22487128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2249ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22507128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22517128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2252ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22537128ae9fSMatthew G Knepley   } else {
225433907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2255843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22567128ae9fSMatthew G Knepley   }
22574c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2258baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2259baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2260baf369e7SPeter Brune   }
226147c6ae99SBarry Smith   PetscFunctionReturn(0);
226247c6ae99SBarry Smith }
226347c6ae99SBarry Smith 
2264d4d07f1eSToby Isaac /*@C
2265d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2266d4d07f1eSToby Isaac 
2267d4d07f1eSToby Isaac    Logically Collective
2268d4d07f1eSToby Isaac 
2269d4d07f1eSToby Isaac    Input Arguments:
2270d4d07f1eSToby Isaac +  dm - the DM
2271d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2272d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2273d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2274d4d07f1eSToby Isaac 
2275d4d07f1eSToby Isaac    Calling sequence for beginhook:
2276d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2277d4d07f1eSToby Isaac 
2278d4d07f1eSToby Isaac +  dm - global DM
2279d4d07f1eSToby Isaac .  l - local vector
2280d4d07f1eSToby Isaac .  mode - mode
2281d4d07f1eSToby Isaac .  g - global vector
2282d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2283d4d07f1eSToby Isaac 
2284d4d07f1eSToby Isaac 
2285d4d07f1eSToby Isaac    Calling sequence for endhook:
2286d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2287d4d07f1eSToby Isaac 
2288d4d07f1eSToby Isaac +  global - global DM
2289d4d07f1eSToby Isaac .  l - local vector
2290d4d07f1eSToby Isaac .  mode - mode
2291d4d07f1eSToby Isaac .  g - global vector
2292d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2293d4d07f1eSToby Isaac 
2294d4d07f1eSToby Isaac    Level: advanced
2295d4d07f1eSToby Isaac 
2296d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2297d4d07f1eSToby Isaac @*/
2298d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2299d4d07f1eSToby Isaac {
2300d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2301d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2302d4d07f1eSToby Isaac 
2303d4d07f1eSToby Isaac   PetscFunctionBegin;
2304d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2305d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
230695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2307d4d07f1eSToby Isaac   link->beginhook = beginhook;
2308d4d07f1eSToby Isaac   link->endhook   = endhook;
2309d4d07f1eSToby Isaac   link->ctx       = ctx;
2310d4d07f1eSToby Isaac   link->next      = NULL;
2311d4d07f1eSToby Isaac   *p              = link;
2312d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2313d4d07f1eSToby Isaac }
2314d4d07f1eSToby Isaac 
23154c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, 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 == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
23275db9a05bSToby Isaac     PetscInt nRows;
23285db9a05bSToby Isaac 
23295db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23305db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2331e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
23327711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23334c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23344c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23354c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23364c274da1SToby Isaac       if (dof) {
23374c274da1SToby Isaac         PetscInt d;
23384c274da1SToby Isaac         PetscScalar *vals;
23394c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
23404c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
23414c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
23424c274da1SToby Isaac          * we just extracted */
23434c274da1SToby Isaac         for (d = 0; d < dof; d++) {
23444c274da1SToby Isaac           vals[d] = 0.;
23454c274da1SToby Isaac         }
23464c274da1SToby Isaac       }
23474c274da1SToby Isaac     }
23484c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
23494c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23504c274da1SToby Isaac   }
23514c274da1SToby Isaac   PetscFunctionReturn(0);
23524c274da1SToby Isaac }
235301729b5cSPatrick Sanan /*@
235401729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
235501729b5cSPatrick Sanan 
235601729b5cSPatrick Sanan     Neighbor-wise Collective on DM
235701729b5cSPatrick Sanan 
235801729b5cSPatrick Sanan     Input Parameters:
235901729b5cSPatrick Sanan +   dm - the DM object
236001729b5cSPatrick Sanan .   l - the local vector
236101729b5cSPatrick 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.
236201729b5cSPatrick Sanan -   g - the global vector
236301729b5cSPatrick Sanan 
236401729b5cSPatrick Sanan     Notes:
236501729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
236601729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
236701729b5cSPatrick Sanan 
236801729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
236901729b5cSPatrick 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.
237001729b5cSPatrick Sanan 
237101729b5cSPatrick Sanan     Level: beginner
237201729b5cSPatrick Sanan 
237301729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
237401729b5cSPatrick Sanan 
237501729b5cSPatrick Sanan @*/
237601729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
237701729b5cSPatrick Sanan {
237801729b5cSPatrick Sanan   PetscErrorCode ierr;
237901729b5cSPatrick Sanan 
238001729b5cSPatrick Sanan   PetscFunctionBegin;
238101729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
238201729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
238301729b5cSPatrick Sanan   PetscFunctionReturn(0);
238401729b5cSPatrick Sanan }
23854c274da1SToby Isaac 
238647c6ae99SBarry Smith /*@
238701729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
23889a42bb27SBarry Smith 
23899a42bb27SBarry Smith     Neighbor-wise Collective on DM
23909a42bb27SBarry Smith 
23919a42bb27SBarry Smith     Input Parameters:
23929a42bb27SBarry Smith +   dm - the DM object
2393f6813fd5SJed Brown .   l - the local vector
23941eb28f2eSBarry 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.
23951eb28f2eSBarry Smith -   g - the global vector
23969a42bb27SBarry Smith 
239795452b02SPatrick Sanan     Notes:
239895452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
239984330215SMatthew 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.
24009a42bb27SBarry Smith 
240101729b5cSPatrick Sanan     Level: intermediate
24029a42bb27SBarry Smith 
240301729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
24049a42bb27SBarry Smith 
24059a42bb27SBarry Smith @*/
24067087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
24079a42bb27SBarry Smith {
24087128ae9fSMatthew G Knepley   PetscSF                 sf;
240984330215SMatthew G. Knepley   PetscSection            s, gs;
2410d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2411ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2412ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
241384330215SMatthew G. Knepley   PetscBool               isInsert;
241484330215SMatthew G. Knepley   PetscErrorCode          ierr;
24159a42bb27SBarry Smith 
24169a42bb27SBarry Smith   PetscFunctionBegin;
2417171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2418d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2419d4d07f1eSToby Isaac     if (link->beginhook) {
2420d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2421d4d07f1eSToby Isaac     }
2422d4d07f1eSToby Isaac   }
24234c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
24247128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2425e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24267128ae9fSMatthew G Knepley   switch (mode) {
24277128ae9fSMatthew G Knepley   case INSERT_VALUES:
24287128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2429304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
243084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24317128ae9fSMatthew G Knepley   case ADD_VALUES:
24327128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2433304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
243484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
24357128ae9fSMatthew G Knepley   default:
243682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24377128ae9fSMatthew G Knepley   }
243884330215SMatthew G. Knepley   if (sf && !isInsert) {
2439ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24407128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2441a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2442ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
244384330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
244484330215SMatthew G. Knepley   } else if (s && isInsert) {
244584330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
244684330215SMatthew G. Knepley 
2447e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
244884330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
244984330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2450ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
245184330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
245284330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2453b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
245484330215SMatthew G. Knepley 
245584330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
245603442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
245784330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2458b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
245984330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
246084330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2461b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
246203442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2463b3b16f48SMatthew 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);
2464b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2465b3b16f48SMatthew G. Knepley       if (!gcdof) {
246684330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2467b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2468b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
246984330215SMatthew G. Knepley         const PetscInt *cdofs;
247084330215SMatthew G. Knepley         PetscInt        cind = 0;
247184330215SMatthew G. Knepley 
247284330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2473b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
247484330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2475b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
247684330215SMatthew G. Knepley         }
2477b3b16f48SMatthew 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);
247884330215SMatthew G. Knepley     }
2479ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24807128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
24817128ae9fSMatthew G Knepley   } else {
2482843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24837128ae9fSMatthew G Knepley   }
24849a42bb27SBarry Smith   PetscFunctionReturn(0);
24859a42bb27SBarry Smith }
24869a42bb27SBarry Smith 
24879a42bb27SBarry Smith /*@
24889a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
248947c6ae99SBarry Smith 
249047c6ae99SBarry Smith     Neighbor-wise Collective on DM
249147c6ae99SBarry Smith 
249247c6ae99SBarry Smith     Input Parameters:
249347c6ae99SBarry Smith +   dm - the DM object
2494f6813fd5SJed Brown .   l - the local vector
249547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2496f6813fd5SJed Brown -   g - the global vector
249747c6ae99SBarry Smith 
249801729b5cSPatrick Sanan     Level: intermediate
249947c6ae99SBarry Smith 
2500e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
250147c6ae99SBarry Smith 
250247c6ae99SBarry Smith @*/
25037087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
250447c6ae99SBarry Smith {
25057128ae9fSMatthew G Knepley   PetscSF                 sf;
250684330215SMatthew G. Knepley   PetscSection            s;
2507d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
250884330215SMatthew G. Knepley   PetscBool               isInsert;
250984330215SMatthew G. Knepley   PetscErrorCode          ierr;
251047c6ae99SBarry Smith 
251147c6ae99SBarry Smith   PetscFunctionBegin;
2512171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25137128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2514e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
25157128ae9fSMatthew G Knepley   switch (mode) {
25167128ae9fSMatthew G Knepley   case INSERT_VALUES:
25177128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
251884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25197128ae9fSMatthew G Knepley   case ADD_VALUES:
25207128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
252184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25227128ae9fSMatthew G Knepley   default:
252382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25247128ae9fSMatthew G Knepley   }
252584330215SMatthew G. Knepley   if (sf && !isInsert) {
2526ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2527ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
252884330215SMatthew G. Knepley 
2529ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
25307128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2531a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2532ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
25337128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
253484330215SMatthew G. Knepley   } else if (s && isInsert) {
25357128ae9fSMatthew G Knepley   } else {
2536843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
25377128ae9fSMatthew G Knepley   }
2538d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2539d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2540d4d07f1eSToby Isaac   }
254147c6ae99SBarry Smith   PetscFunctionReturn(0);
254247c6ae99SBarry Smith }
254347c6ae99SBarry Smith 
2544f089877aSRichard Tran Mills /*@
2545bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2546bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2547d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2548f089877aSRichard Tran Mills 
2549bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2550f089877aSRichard Tran Mills 
2551f089877aSRichard Tran Mills    Input Parameters:
2552f089877aSRichard Tran Mills +  dm - the DM object
2553bc0a1609SRichard Tran Mills .  g - the original local vector
2554bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2555f089877aSRichard Tran Mills 
2556bc0a1609SRichard Tran Mills    Output Parameter:
2557bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2558f089877aSRichard Tran Mills 
2559f089877aSRichard Tran Mills    Level: intermediate
2560f089877aSRichard Tran Mills 
2561bc0a1609SRichard Tran Mills    Notes:
2562bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2563bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2564bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2565bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2566bc0a1609SRichard Tran Mills 
2567bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2568bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2569f089877aSRichard Tran Mills 
2570f089877aSRichard Tran Mills @*/
2571f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2572f089877aSRichard Tran Mills {
2573f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2574f089877aSRichard Tran Mills 
2575f089877aSRichard Tran Mills   PetscFunctionBegin;
2576f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2577bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2578f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2579f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2580f089877aSRichard Tran Mills }
2581f089877aSRichard Tran Mills 
2582f089877aSRichard Tran Mills /*@
2583bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2584bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2585d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2586f089877aSRichard Tran Mills 
2587bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2588f089877aSRichard Tran Mills 
2589f089877aSRichard Tran Mills    Input Parameters:
2590bc0a1609SRichard Tran Mills +  da - the DM object
2591bc0a1609SRichard Tran Mills .  g - the original local vector
2592bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2593f089877aSRichard Tran Mills 
2594bc0a1609SRichard Tran Mills    Output Parameter:
2595bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2596f089877aSRichard Tran Mills 
2597f089877aSRichard Tran Mills    Level: intermediate
2598f089877aSRichard Tran Mills 
2599bc0a1609SRichard Tran Mills    Notes:
2600bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2601bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2602bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2603bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2604bc0a1609SRichard Tran Mills 
2605bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2606bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2607f089877aSRichard Tran Mills 
2608f089877aSRichard Tran Mills @*/
2609f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2610f089877aSRichard Tran Mills {
2611f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2612f089877aSRichard Tran Mills 
2613f089877aSRichard Tran Mills   PetscFunctionBegin;
2614f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2615bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2616f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2617f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2618f089877aSRichard Tran Mills }
2619f089877aSRichard Tran Mills 
2620f089877aSRichard Tran Mills 
262147c6ae99SBarry Smith /*@
262247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
262347c6ae99SBarry Smith 
262447c6ae99SBarry Smith     Collective on DM
262547c6ae99SBarry Smith 
262647c6ae99SBarry Smith     Input Parameter:
262747c6ae99SBarry Smith +   dm - the DM object
262891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
262947c6ae99SBarry Smith 
263047c6ae99SBarry Smith     Output Parameter:
263147c6ae99SBarry Smith .   dmc - the coarsened DM
263247c6ae99SBarry Smith 
263347c6ae99SBarry Smith     Level: developer
263447c6ae99SBarry Smith 
2635e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
263647c6ae99SBarry Smith 
263747c6ae99SBarry Smith @*/
26387087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
263947c6ae99SBarry Smith {
264047c6ae99SBarry Smith   PetscErrorCode    ierr;
2641b17ce1afSJed Brown   DMCoarsenHookLink link;
264247c6ae99SBarry Smith 
264347c6ae99SBarry Smith   PetscFunctionBegin;
2644171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2645fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
264647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
264747c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
26488892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2649a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
265043842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
26518cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2652644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
26530598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2654656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2655e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2656b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2657b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2658b17ce1afSJed Brown   }
265947a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2660b17ce1afSJed Brown   PetscFunctionReturn(0);
2661b17ce1afSJed Brown }
2662b17ce1afSJed Brown 
2663bb9467b5SJed Brown /*@C
2664b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2665b17ce1afSJed Brown 
2666b17ce1afSJed Brown    Logically Collective
2667b17ce1afSJed Brown 
2668b17ce1afSJed Brown    Input Arguments:
2669b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2670b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2671b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
26720298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2673b17ce1afSJed Brown 
2674b17ce1afSJed Brown    Calling sequence of coarsenhook:
2675b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2676b17ce1afSJed Brown 
2677b17ce1afSJed Brown +  fine - fine level DM
2678b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2679b17ce1afSJed Brown -  ctx - optional user-defined function context
2680b17ce1afSJed Brown 
2681b17ce1afSJed Brown    Calling sequence for restricthook:
2682c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2683b17ce1afSJed Brown 
2684b17ce1afSJed Brown +  fine - fine level DM
2685b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2686c833c3b5SJed Brown .  rscale - scaling vector for restriction
2687c833c3b5SJed Brown .  inject - matrix restricting by injection
2688b17ce1afSJed Brown .  coarse - coarse level DM to update
2689b17ce1afSJed Brown -  ctx - optional user-defined function context
2690b17ce1afSJed Brown 
2691b17ce1afSJed Brown    Level: advanced
2692b17ce1afSJed Brown 
2693b17ce1afSJed Brown    Notes:
2694b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2695b17ce1afSJed Brown 
2696b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2697b17ce1afSJed Brown 
2698b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2699b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2700b17ce1afSJed Brown 
2701bb9467b5SJed Brown    This function is currently not available from Fortran.
2702bb9467b5SJed Brown 
2703dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2704b17ce1afSJed Brown @*/
2705b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2706b17ce1afSJed Brown {
2707b17ce1afSJed Brown   PetscErrorCode    ierr;
2708b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2709b17ce1afSJed Brown 
2710b17ce1afSJed Brown   PetscFunctionBegin;
2711b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
27121e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
27131e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
27141e3d8eccSJed Brown   }
271595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2716b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2717b17ce1afSJed Brown   link->restricthook = restricthook;
2718b17ce1afSJed Brown   link->ctx          = ctx;
27190298fd71SBarry Smith   link->next         = NULL;
2720b17ce1afSJed Brown   *p                 = link;
2721b17ce1afSJed Brown   PetscFunctionReturn(0);
2722b17ce1afSJed Brown }
2723b17ce1afSJed Brown 
2724dc822a44SJed Brown /*@C
2725dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2726dc822a44SJed Brown 
2727dc822a44SJed Brown    Logically Collective
2728dc822a44SJed Brown 
2729dc822a44SJed Brown    Input Arguments:
2730dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2731dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2732dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2733dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2734dc822a44SJed Brown 
2735dc822a44SJed Brown    Level: advanced
2736dc822a44SJed Brown 
2737dc822a44SJed Brown    Notes:
2738dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2739dc822a44SJed Brown 
2740dc822a44SJed Brown    This function is currently not available from Fortran.
2741dc822a44SJed Brown 
2742dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2743dc822a44SJed Brown @*/
2744dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2745dc822a44SJed Brown {
2746dc822a44SJed Brown   PetscErrorCode    ierr;
2747dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2748dc822a44SJed Brown 
2749dc822a44SJed Brown   PetscFunctionBegin;
2750dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2751dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2752dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2753dc822a44SJed Brown       link = *p;
2754dc822a44SJed Brown       *p = link->next;
2755dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2756dc822a44SJed Brown       break;
2757dc822a44SJed Brown     }
2758dc822a44SJed Brown   }
2759dc822a44SJed Brown   PetscFunctionReturn(0);
2760dc822a44SJed Brown }
2761dc822a44SJed Brown 
2762dc822a44SJed Brown 
2763b17ce1afSJed Brown /*@
2764b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2765b17ce1afSJed Brown 
2766b17ce1afSJed Brown    Collective if any hooks are
2767b17ce1afSJed Brown 
2768b17ce1afSJed Brown    Input Arguments:
2769b17ce1afSJed Brown +  fine - finer DM to use as a base
2770b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2771e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2772b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2773e91eccc2SStefano Zampini -  coarse - coarser DM to update
2774b17ce1afSJed Brown 
2775b17ce1afSJed Brown    Level: developer
2776b17ce1afSJed Brown 
2777b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2778b17ce1afSJed Brown @*/
2779b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2780b17ce1afSJed Brown {
2781b17ce1afSJed Brown   PetscErrorCode    ierr;
2782b17ce1afSJed Brown   DMCoarsenHookLink link;
2783b17ce1afSJed Brown 
2784b17ce1afSJed Brown   PetscFunctionBegin;
2785b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
27868865f1eaSKarl Rupp     if (link->restricthook) {
27878865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
27888865f1eaSKarl Rupp     }
2789b17ce1afSJed Brown   }
279047c6ae99SBarry Smith   PetscFunctionReturn(0);
279147c6ae99SBarry Smith }
279247c6ae99SBarry Smith 
2793bb9467b5SJed Brown /*@C
2794be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
27955dbd56e3SPeter Brune 
27965dbd56e3SPeter Brune    Logically Collective
27975dbd56e3SPeter Brune 
27985dbd56e3SPeter Brune    Input Arguments:
27995dbd56e3SPeter Brune +  global - global DM
2800ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
28015dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
28020298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
28035dbd56e3SPeter Brune 
2804ec4806b8SPeter Brune 
2805ec4806b8SPeter Brune    Calling sequence for ddhook:
2806ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2807ec4806b8SPeter Brune 
2808ec4806b8SPeter Brune +  global - global DM
2809ec4806b8SPeter Brune .  block  - block DM
2810ec4806b8SPeter Brune -  ctx - optional user-defined function context
2811ec4806b8SPeter Brune 
28125dbd56e3SPeter Brune    Calling sequence for restricthook:
2813ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
28145dbd56e3SPeter Brune 
28155dbd56e3SPeter Brune +  global - global DM
28165dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
28175dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2818ec4806b8SPeter Brune .  block  - block DM
28195dbd56e3SPeter Brune -  ctx - optional user-defined function context
28205dbd56e3SPeter Brune 
28215dbd56e3SPeter Brune    Level: advanced
28225dbd56e3SPeter Brune 
28235dbd56e3SPeter Brune    Notes:
2824ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
28255dbd56e3SPeter Brune 
28265dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
28275dbd56e3SPeter Brune 
28285dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2829ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
28305dbd56e3SPeter Brune 
2831bb9467b5SJed Brown    This function is currently not available from Fortran.
2832bb9467b5SJed Brown 
28335dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
28345dbd56e3SPeter Brune @*/
2835be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
28365dbd56e3SPeter Brune {
28375dbd56e3SPeter Brune   PetscErrorCode      ierr;
2838be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
28395dbd56e3SPeter Brune 
28405dbd56e3SPeter Brune   PetscFunctionBegin;
28415dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2842b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2843b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2844b3a6b972SJed Brown   }
284595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
28465dbd56e3SPeter Brune   link->restricthook = restricthook;
2847be081cd6SPeter Brune   link->ddhook       = ddhook;
28485dbd56e3SPeter Brune   link->ctx          = ctx;
28490298fd71SBarry Smith   link->next         = NULL;
28505dbd56e3SPeter Brune   *p                 = link;
28515dbd56e3SPeter Brune   PetscFunctionReturn(0);
28525dbd56e3SPeter Brune }
28535dbd56e3SPeter Brune 
2854b3a6b972SJed Brown /*@C
2855b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2856b3a6b972SJed Brown 
2857b3a6b972SJed Brown    Logically Collective
2858b3a6b972SJed Brown 
2859b3a6b972SJed Brown    Input Arguments:
2860b3a6b972SJed Brown +  global - global DM
2861b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2862b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2863b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2864b3a6b972SJed Brown 
2865b3a6b972SJed Brown    Level: advanced
2866b3a6b972SJed Brown 
2867b3a6b972SJed Brown    Notes:
2868b3a6b972SJed Brown 
2869b3a6b972SJed Brown    This function is currently not available from Fortran.
2870b3a6b972SJed Brown 
2871b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2872b3a6b972SJed Brown @*/
2873b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2874b3a6b972SJed Brown {
2875b3a6b972SJed Brown   PetscErrorCode      ierr;
2876b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2877b3a6b972SJed Brown 
2878b3a6b972SJed Brown   PetscFunctionBegin;
2879b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2880b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2881b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2882b3a6b972SJed Brown       link = *p;
2883b3a6b972SJed Brown       *p = link->next;
2884b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2885b3a6b972SJed Brown       break;
2886b3a6b972SJed Brown     }
2887b3a6b972SJed Brown   }
2888b3a6b972SJed Brown   PetscFunctionReturn(0);
2889b3a6b972SJed Brown }
2890b3a6b972SJed Brown 
28915dbd56e3SPeter Brune /*@
2892be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
28935dbd56e3SPeter Brune 
28945dbd56e3SPeter Brune    Collective if any hooks are
28955dbd56e3SPeter Brune 
28965dbd56e3SPeter Brune    Input Arguments:
28975dbd56e3SPeter Brune +  fine - finer DM to use as a base
2898be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2899be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
29005dbd56e3SPeter Brune -  coarse - coarer DM to update
29015dbd56e3SPeter Brune 
29025dbd56e3SPeter Brune    Level: developer
29035dbd56e3SPeter Brune 
29045dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
29055dbd56e3SPeter Brune @*/
2906be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
29075dbd56e3SPeter Brune {
29085dbd56e3SPeter Brune   PetscErrorCode      ierr;
2909be081cd6SPeter Brune   DMSubDomainHookLink link;
29105dbd56e3SPeter Brune 
29115dbd56e3SPeter Brune   PetscFunctionBegin;
2912be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
29138865f1eaSKarl Rupp     if (link->restricthook) {
29148865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
29158865f1eaSKarl Rupp     }
29165dbd56e3SPeter Brune   }
29175dbd56e3SPeter Brune   PetscFunctionReturn(0);
29185dbd56e3SPeter Brune }
29195dbd56e3SPeter Brune 
29205fe1f584SPeter Brune /*@
29216a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
29225fe1f584SPeter Brune 
29235fe1f584SPeter Brune     Not Collective
29245fe1f584SPeter Brune 
29255fe1f584SPeter Brune     Input Parameter:
29265fe1f584SPeter Brune .   dm - the DM object
29275fe1f584SPeter Brune 
29285fe1f584SPeter Brune     Output Parameter:
29296a7d9d85SPeter Brune .   level - number of coarsenings
29305fe1f584SPeter Brune 
29315fe1f584SPeter Brune     Level: developer
29325fe1f584SPeter Brune 
29336a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
29345fe1f584SPeter Brune 
29355fe1f584SPeter Brune @*/
29365fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
29375fe1f584SPeter Brune {
29385fe1f584SPeter Brune   PetscFunctionBegin;
29395fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29405fe1f584SPeter Brune   *level = dm->leveldown;
29415fe1f584SPeter Brune   PetscFunctionReturn(0);
29425fe1f584SPeter Brune }
29435fe1f584SPeter Brune 
29445fe1f584SPeter Brune 
29455fe1f584SPeter Brune 
294647c6ae99SBarry Smith /*@C
294747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
294847c6ae99SBarry Smith 
294947c6ae99SBarry Smith     Collective on DM
295047c6ae99SBarry Smith 
295147c6ae99SBarry Smith     Input Parameter:
295247c6ae99SBarry Smith +   dm - the DM object
295347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
295447c6ae99SBarry Smith 
295547c6ae99SBarry Smith     Output Parameter:
295647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
295747c6ae99SBarry Smith 
295847c6ae99SBarry Smith     Level: developer
295947c6ae99SBarry Smith 
2960e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
296147c6ae99SBarry Smith 
296247c6ae99SBarry Smith @*/
29637087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
296447c6ae99SBarry Smith {
296547c6ae99SBarry Smith   PetscErrorCode ierr;
296647c6ae99SBarry Smith 
296747c6ae99SBarry Smith   PetscFunctionBegin;
2968171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2969ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
297047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
297147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
297247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
297347c6ae99SBarry Smith   } else if (dm->ops->refine) {
297447c6ae99SBarry Smith     PetscInt i;
297547c6ae99SBarry Smith 
2976ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
297747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2978ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
297947c6ae99SBarry Smith     }
2980ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
298147c6ae99SBarry Smith   PetscFunctionReturn(0);
298247c6ae99SBarry Smith }
298347c6ae99SBarry Smith 
298447c6ae99SBarry Smith /*@C
298547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
298647c6ae99SBarry Smith 
298747c6ae99SBarry Smith     Collective on DM
298847c6ae99SBarry Smith 
298947c6ae99SBarry Smith     Input Parameter:
299047c6ae99SBarry Smith +   dm - the DM object
299147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
299247c6ae99SBarry Smith 
299347c6ae99SBarry Smith     Output Parameter:
299447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
299547c6ae99SBarry Smith 
299647c6ae99SBarry Smith     Level: developer
299747c6ae99SBarry Smith 
2998e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
299947c6ae99SBarry Smith 
300047c6ae99SBarry Smith @*/
30017087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
300247c6ae99SBarry Smith {
300347c6ae99SBarry Smith   PetscErrorCode ierr;
300447c6ae99SBarry Smith 
300547c6ae99SBarry Smith   PetscFunctionBegin;
3006171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3007ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
300847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
300947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
301047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
301147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
301247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
301347c6ae99SBarry Smith     PetscInt i;
301447c6ae99SBarry Smith 
3015ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
301647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3017ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
301847c6ae99SBarry Smith     }
3019ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
302047c6ae99SBarry Smith   PetscFunctionReturn(0);
302147c6ae99SBarry Smith }
302247c6ae99SBarry Smith 
302347c6ae99SBarry Smith /*@
3024e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
302547c6ae99SBarry Smith    grids associated with two DMs.
302647c6ae99SBarry Smith 
302747c6ae99SBarry Smith    Collective on DM
302847c6ae99SBarry Smith 
302947c6ae99SBarry Smith    Input Parameters:
303047c6ae99SBarry Smith +  dmc - the coarse grid DM
303147c6ae99SBarry Smith -  dmf - the fine grid DM
303247c6ae99SBarry Smith 
303347c6ae99SBarry Smith    Output Parameters:
303447c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
303547c6ae99SBarry Smith 
303647c6ae99SBarry Smith    Level: intermediate
303747c6ae99SBarry Smith 
303847c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
303947c6ae99SBarry Smith 
3040e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
304147c6ae99SBarry Smith @*/
3042e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
304347c6ae99SBarry Smith {
304447c6ae99SBarry Smith   PetscErrorCode ierr;
304547c6ae99SBarry Smith 
304647c6ae99SBarry Smith   PetscFunctionBegin;
3047171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3048171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
304947c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
305047c6ae99SBarry Smith   PetscFunctionReturn(0);
305147c6ae99SBarry Smith }
305247c6ae99SBarry Smith 
30531a266240SBarry Smith /*@C
30541a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
30551a266240SBarry Smith 
30561a266240SBarry Smith     Not Collective
30571a266240SBarry Smith 
30581a266240SBarry Smith     Input Parameters:
30591a266240SBarry Smith +   dm - the DM object
30601a266240SBarry Smith -   destroy - the destroy function
30611a266240SBarry Smith 
30621a266240SBarry Smith     Level: intermediate
30631a266240SBarry Smith 
3064e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
30651a266240SBarry Smith 
3066f07f9ceaSJed Brown @*/
30671a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
30681a266240SBarry Smith {
30691a266240SBarry Smith   PetscFunctionBegin;
3070171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30711a266240SBarry Smith   dm->ctxdestroy = destroy;
30721a266240SBarry Smith   PetscFunctionReturn(0);
30731a266240SBarry Smith }
30741a266240SBarry Smith 
3075b07ff414SBarry Smith /*@
30761b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
307747c6ae99SBarry Smith 
307847c6ae99SBarry Smith     Not Collective
307947c6ae99SBarry Smith 
308047c6ae99SBarry Smith     Input Parameters:
308147c6ae99SBarry Smith +   dm - the DM object
308247c6ae99SBarry Smith -   ctx - the user context
308347c6ae99SBarry Smith 
308447c6ae99SBarry Smith     Level: intermediate
308547c6ae99SBarry Smith 
3086e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
308747c6ae99SBarry Smith 
308847c6ae99SBarry Smith @*/
30891b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
309047c6ae99SBarry Smith {
309147c6ae99SBarry Smith   PetscFunctionBegin;
3092171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
309347c6ae99SBarry Smith   dm->ctx = ctx;
309447c6ae99SBarry Smith   PetscFunctionReturn(0);
309547c6ae99SBarry Smith }
309647c6ae99SBarry Smith 
309747c6ae99SBarry Smith /*@
30981b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
309947c6ae99SBarry Smith 
310047c6ae99SBarry Smith     Not Collective
310147c6ae99SBarry Smith 
310247c6ae99SBarry Smith     Input Parameter:
310347c6ae99SBarry Smith .   dm - the DM object
310447c6ae99SBarry Smith 
310547c6ae99SBarry Smith     Output Parameter:
310647c6ae99SBarry Smith .   ctx - the user context
310747c6ae99SBarry Smith 
310847c6ae99SBarry Smith     Level: intermediate
310947c6ae99SBarry Smith 
3110e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
311147c6ae99SBarry Smith 
311247c6ae99SBarry Smith @*/
31131b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
311447c6ae99SBarry Smith {
311547c6ae99SBarry Smith   PetscFunctionBegin;
3116171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31171b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
311847c6ae99SBarry Smith   PetscFunctionReturn(0);
311947c6ae99SBarry Smith }
312047c6ae99SBarry Smith 
312108da532bSDmitry Karpeev /*@C
3122df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
312308da532bSDmitry Karpeev 
312408da532bSDmitry Karpeev     Logically Collective on DM
312508da532bSDmitry Karpeev 
312608da532bSDmitry Karpeev     Input Parameter:
312708da532bSDmitry Karpeev +   dm - the DM object
31280298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
312908da532bSDmitry Karpeev 
313008da532bSDmitry Karpeev     Level: intermediate
313108da532bSDmitry Karpeev 
3132835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
313308da532bSDmitry Karpeev          DMSetJacobian()
313408da532bSDmitry Karpeev 
313508da532bSDmitry Karpeev @*/
313608da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
313708da532bSDmitry Karpeev {
313808da532bSDmitry Karpeev   PetscFunctionBegin;
313908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
314008da532bSDmitry Karpeev   PetscFunctionReturn(0);
314108da532bSDmitry Karpeev }
314208da532bSDmitry Karpeev 
314308da532bSDmitry Karpeev /*@
314408da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
314508da532bSDmitry Karpeev 
314608da532bSDmitry Karpeev     Not Collective
314708da532bSDmitry Karpeev 
314808da532bSDmitry Karpeev     Input Parameter:
314908da532bSDmitry Karpeev .   dm - the DM object to destroy
315008da532bSDmitry Karpeev 
315108da532bSDmitry Karpeev     Output Parameter:
315208da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
315308da532bSDmitry Karpeev 
315408da532bSDmitry Karpeev     Level: developer
315508da532bSDmitry Karpeev 
315674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
315708da532bSDmitry Karpeev 
315808da532bSDmitry Karpeev @*/
315908da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
316008da532bSDmitry Karpeev {
316108da532bSDmitry Karpeev   PetscFunctionBegin;
316208da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
316308da532bSDmitry Karpeev   PetscFunctionReturn(0);
316408da532bSDmitry Karpeev }
316508da532bSDmitry Karpeev 
316608da532bSDmitry Karpeev /*@C
316708da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
316808da532bSDmitry Karpeev 
316908da532bSDmitry Karpeev     Logically Collective on DM
317008da532bSDmitry Karpeev 
317108da532bSDmitry Karpeev     Input Parameters:
3172907376e6SBarry Smith .   dm - the DM object
317308da532bSDmitry Karpeev 
317408da532bSDmitry Karpeev     Output parameters:
317508da532bSDmitry Karpeev +   xl - lower bound
317608da532bSDmitry Karpeev -   xu - upper bound
317708da532bSDmitry Karpeev 
3178907376e6SBarry Smith     Level: advanced
3179907376e6SBarry Smith 
318095452b02SPatrick Sanan     Notes:
318195452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
318208da532bSDmitry Karpeev 
318374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
318408da532bSDmitry Karpeev 
318508da532bSDmitry Karpeev @*/
318608da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
318708da532bSDmitry Karpeev {
318808da532bSDmitry Karpeev   PetscErrorCode ierr;
31895fd66863SKarl Rupp 
319008da532bSDmitry Karpeev   PetscFunctionBegin;
319108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
319208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
319308da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
319408da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
31958865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
319608da532bSDmitry Karpeev   PetscFunctionReturn(0);
319708da532bSDmitry Karpeev }
319808da532bSDmitry Karpeev 
3199b0ae01b7SPeter Brune /*@
3200b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3201b0ae01b7SPeter Brune 
3202b0ae01b7SPeter Brune     Not Collective
3203b0ae01b7SPeter Brune 
3204b0ae01b7SPeter Brune     Input Parameter:
3205b0ae01b7SPeter Brune .   dm - the DM object
3206b0ae01b7SPeter Brune 
3207b0ae01b7SPeter Brune     Output Parameter:
3208b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3209b0ae01b7SPeter Brune 
3210b0ae01b7SPeter Brune     Level: developer
3211b0ae01b7SPeter Brune 
3212b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3213b0ae01b7SPeter Brune 
3214b0ae01b7SPeter Brune @*/
3215b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3216b0ae01b7SPeter Brune {
3217b0ae01b7SPeter Brune   PetscFunctionBegin;
3218b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3219b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3220b0ae01b7SPeter Brune }
3221b0ae01b7SPeter Brune 
32223ad4599aSBarry Smith /*@
32233ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
32243ad4599aSBarry Smith 
32253ad4599aSBarry Smith     Not Collective
32263ad4599aSBarry Smith 
32273ad4599aSBarry Smith     Input Parameter:
32283ad4599aSBarry Smith .   dm - the DM object
32293ad4599aSBarry Smith 
32303ad4599aSBarry Smith     Output Parameter:
32313ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
32323ad4599aSBarry Smith 
32333ad4599aSBarry Smith     Level: developer
32343ad4599aSBarry Smith 
32353ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
32363ad4599aSBarry Smith 
32373ad4599aSBarry Smith @*/
32383ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
32393ad4599aSBarry Smith {
32403ad4599aSBarry Smith   PetscFunctionBegin;
32413ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
32423ad4599aSBarry Smith   PetscFunctionReturn(0);
32433ad4599aSBarry Smith }
32443ad4599aSBarry Smith 
3245a7058e45SLawrence Mitchell 
3246a7058e45SLawrence Mitchell /*@
3247a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3248a7058e45SLawrence Mitchell 
3249a7058e45SLawrence Mitchell     Not Collective
3250a7058e45SLawrence Mitchell 
3251a7058e45SLawrence Mitchell     Input Parameter:
3252a7058e45SLawrence Mitchell .   dm - the DM object
3253a7058e45SLawrence Mitchell 
3254a7058e45SLawrence Mitchell     Output Parameter:
3255a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3256a7058e45SLawrence Mitchell 
3257a7058e45SLawrence Mitchell     Level: developer
3258a7058e45SLawrence Mitchell 
3259a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3260a7058e45SLawrence Mitchell 
3261a7058e45SLawrence Mitchell @*/
3262a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3263a7058e45SLawrence Mitchell {
32644a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3265a7058e45SLawrence Mitchell   PetscFunctionBegin;
32664a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
32674a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3268a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3269a7058e45SLawrence Mitchell }
3270a7058e45SLawrence Mitchell 
3271748fac09SDmitry Karpeev /*@C
327208da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
327308da532bSDmitry Karpeev 
327408da532bSDmitry Karpeev     Collective on DM
327508da532bSDmitry Karpeev 
327608da532bSDmitry Karpeev     Input Parameter:
327708da532bSDmitry Karpeev +   dm - the DM object
32780298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
327908da532bSDmitry Karpeev 
328008da532bSDmitry Karpeev     Level: developer
328108da532bSDmitry Karpeev 
328274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
328308da532bSDmitry Karpeev 
328408da532bSDmitry Karpeev @*/
328508da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
328608da532bSDmitry Karpeev {
328708da532bSDmitry Karpeev   PetscErrorCode ierr;
32885fd66863SKarl Rupp 
328908da532bSDmitry Karpeev   PetscFunctionBegin;
329008da532bSDmitry Karpeev   if (x) {
329108da532bSDmitry Karpeev     if (!dm->x) {
329208da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
329308da532bSDmitry Karpeev     }
329408da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
32958865f1eaSKarl Rupp   } else if (dm->x) {
329608da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
329708da532bSDmitry Karpeev   }
329808da532bSDmitry Karpeev   PetscFunctionReturn(0);
329908da532bSDmitry Karpeev }
330008da532bSDmitry Karpeev 
33010298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3302264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3303264ace61SBarry Smith 
3304264ace61SBarry Smith /*@C
3305264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3306264ace61SBarry Smith 
3307264ace61SBarry Smith   Collective on DM
3308264ace61SBarry Smith 
3309264ace61SBarry Smith   Input Parameters:
3310264ace61SBarry Smith + dm     - The DM object
3311264ace61SBarry Smith - method - The name of the DM type
3312264ace61SBarry Smith 
3313264ace61SBarry Smith   Options Database Key:
3314264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3315264ace61SBarry Smith 
3316264ace61SBarry Smith   Notes:
3317e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3318264ace61SBarry Smith 
3319264ace61SBarry Smith   Level: intermediate
3320264ace61SBarry Smith 
3321264ace61SBarry Smith .keywords: DM, set, type
3322264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3323264ace61SBarry Smith @*/
332419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3325264ace61SBarry Smith {
3326264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3327264ace61SBarry Smith   PetscBool      match;
3328264ace61SBarry Smith   PetscErrorCode ierr;
3329264ace61SBarry Smith 
3330264ace61SBarry Smith   PetscFunctionBegin;
3331264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3332251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3333264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3334264ace61SBarry Smith 
33350f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
33361c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3337ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3338264ace61SBarry Smith 
3339264ace61SBarry Smith   if (dm->ops->destroy) {
3340264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
33410298fd71SBarry Smith     dm->ops->destroy = NULL;
3342264ace61SBarry Smith   }
3343264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3344264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3345264ace61SBarry Smith   PetscFunctionReturn(0);
3346264ace61SBarry Smith }
3347264ace61SBarry Smith 
3348264ace61SBarry Smith /*@C
3349264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3350264ace61SBarry Smith 
3351264ace61SBarry Smith   Not Collective
3352264ace61SBarry Smith 
3353264ace61SBarry Smith   Input Parameter:
3354264ace61SBarry Smith . dm  - The DM
3355264ace61SBarry Smith 
3356264ace61SBarry Smith   Output Parameter:
3357264ace61SBarry Smith . type - The DM type name
3358264ace61SBarry Smith 
3359264ace61SBarry Smith   Level: intermediate
3360264ace61SBarry Smith 
3361264ace61SBarry Smith .keywords: DM, get, type, name
3362264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3363264ace61SBarry Smith @*/
336419fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3365264ace61SBarry Smith {
3366264ace61SBarry Smith   PetscErrorCode ierr;
3367264ace61SBarry Smith 
3368264ace61SBarry Smith   PetscFunctionBegin;
3369264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3370c959eef4SJed Brown   PetscValidPointer(type,2);
3371607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3372264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3373264ace61SBarry Smith   PetscFunctionReturn(0);
3374264ace61SBarry Smith }
3375264ace61SBarry Smith 
337667a56275SMatthew G Knepley /*@C
337767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
337867a56275SMatthew G Knepley 
337967a56275SMatthew G Knepley   Collective on DM
338067a56275SMatthew G Knepley 
338167a56275SMatthew G Knepley   Input Parameters:
338267a56275SMatthew G Knepley + dm - the DM
338367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
338467a56275SMatthew G Knepley 
338567a56275SMatthew G Knepley   Output Parameter:
338667a56275SMatthew G Knepley . M - pointer to new DM
338767a56275SMatthew G Knepley 
338867a56275SMatthew G Knepley   Notes:
338967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
339067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
339167a56275SMatthew G Knepley   of the input DM.
339267a56275SMatthew G Knepley 
339367a56275SMatthew G Knepley   Level: intermediate
339467a56275SMatthew G Knepley 
339567a56275SMatthew G Knepley .seealso: DMCreate()
339667a56275SMatthew G Knepley @*/
339719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
339867a56275SMatthew G Knepley {
339967a56275SMatthew G Knepley   DM             B;
340067a56275SMatthew G Knepley   char           convname[256];
3401c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
340267a56275SMatthew G Knepley   PetscErrorCode ierr;
340367a56275SMatthew G Knepley 
340467a56275SMatthew G Knepley   PetscFunctionBegin;
340567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
340667a56275SMatthew G Knepley   PetscValidType(dm,1);
340767a56275SMatthew G Knepley   PetscValidPointer(M,3);
3408251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3409c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3410c067b6caSMatthew G. Knepley   if (sametype) {
3411c067b6caSMatthew G. Knepley     *M   = dm;
3412c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3413c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3414c067b6caSMatthew G. Knepley   } else {
34150298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
341667a56275SMatthew G Knepley 
341767a56275SMatthew G Knepley     /*
341867a56275SMatthew G Knepley        Order of precedence:
341967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
342067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
342167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
342267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
342367a56275SMatthew G Knepley        5) Use a really basic converter.
342467a56275SMatthew G Knepley     */
342567a56275SMatthew G Knepley 
342667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3427a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3428a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3429a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3430a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3431a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34320005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
343367a56275SMatthew G Knepley     if (conv) goto foundconv;
343467a56275SMatthew G Knepley 
343567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
343682f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
343767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3438a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3439a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3440a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3441a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3442a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34430005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
344467a56275SMatthew G Knepley     if (conv) {
3445fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
344667a56275SMatthew G Knepley       goto foundconv;
344767a56275SMatthew G Knepley     }
344867a56275SMatthew G Knepley 
344967a56275SMatthew G Knepley #if 0
345067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
345167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3452fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
345367a56275SMatthew G Knepley     if (conv) goto foundconv;
345467a56275SMatthew G Knepley 
345567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
345667a56275SMatthew G Knepley     if (dm->ops->convert) {
345767a56275SMatthew G Knepley       conv = dm->ops->convert;
345867a56275SMatthew G Knepley     }
345967a56275SMatthew G Knepley     if (conv) goto foundconv;
346067a56275SMatthew G Knepley #endif
346167a56275SMatthew G Knepley 
346267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
346382f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
346467a56275SMatthew G Knepley 
346567a56275SMatthew G Knepley foundconv:
346667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
346767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
346812fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
346990b157c4SStefano Zampini     {
347090b157c4SStefano Zampini       PetscBool             isper;
347112fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
347212fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
347390b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
347490b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
347512fa691eSMatthew G. Knepley     }
347667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
347767a56275SMatthew G Knepley   }
347867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
347967a56275SMatthew G Knepley   PetscFunctionReturn(0);
348067a56275SMatthew G Knepley }
3481264ace61SBarry Smith 
3482264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3483264ace61SBarry Smith 
3484264ace61SBarry Smith /*@C
34851c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
34861c84c290SBarry Smith 
34871c84c290SBarry Smith   Not Collective
34881c84c290SBarry Smith 
34891c84c290SBarry Smith   Input Parameters:
34901c84c290SBarry Smith + name        - The name of a new user-defined creation routine
34911c84c290SBarry Smith - create_func - The creation routine itself
34921c84c290SBarry Smith 
34931c84c290SBarry Smith   Notes:
34941c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
34951c84c290SBarry Smith 
34961c84c290SBarry Smith 
34971c84c290SBarry Smith   Sample usage:
34981c84c290SBarry Smith .vb
3499bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
35001c84c290SBarry Smith .ve
35011c84c290SBarry Smith 
35021c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
35031c84c290SBarry Smith .vb
35041c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
35051c84c290SBarry Smith     DMSetType(DM,"my_da");
35061c84c290SBarry Smith .ve
35071c84c290SBarry Smith    or at runtime via the option
35081c84c290SBarry Smith .vb
35091c84c290SBarry Smith     -da_type my_da
35101c84c290SBarry Smith .ve
3511264ace61SBarry Smith 
3512264ace61SBarry Smith   Level: advanced
35131c84c290SBarry Smith 
35141c84c290SBarry Smith .keywords: DM, register
3515bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
35161c84c290SBarry Smith 
3517264ace61SBarry Smith @*/
3518bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3519264ace61SBarry Smith {
3520264ace61SBarry Smith   PetscErrorCode ierr;
3521264ace61SBarry Smith 
3522264ace61SBarry Smith   PetscFunctionBegin;
35231d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3524a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3525264ace61SBarry Smith   PetscFunctionReturn(0);
3526264ace61SBarry Smith }
3527264ace61SBarry Smith 
3528b859378eSBarry Smith /*@C
352955849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3530b859378eSBarry Smith 
3531b859378eSBarry Smith   Collective on PetscViewer
3532b859378eSBarry Smith 
3533b859378eSBarry Smith   Input Parameters:
3534b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3535b859378eSBarry Smith            some related function before a call to DMLoad().
3536b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3537b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3538b859378eSBarry Smith 
3539b859378eSBarry Smith    Level: intermediate
3540b859378eSBarry Smith 
3541b859378eSBarry Smith   Notes:
354255849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3543b859378eSBarry Smith 
3544b859378eSBarry Smith   Notes for advanced users:
3545b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3546b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3547b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3548b859378eSBarry Smith   format is
3549b859378eSBarry Smith .vb
3550b859378eSBarry Smith      has not yet been determined
3551b859378eSBarry Smith .ve
3552b859378eSBarry Smith 
3553b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3554b859378eSBarry Smith @*/
3555b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3556b859378eSBarry Smith {
35579331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3558b859378eSBarry Smith   PetscErrorCode ierr;
3559b859378eSBarry Smith 
3560b859378eSBarry Smith   PetscFunctionBegin;
3561b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3562b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
356332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
35649331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
35659331c7a4SMatthew G. Knepley   if (isbinary) {
35669331c7a4SMatthew G. Knepley     PetscInt classid;
35679331c7a4SMatthew G. Knepley     char     type[256];
3568b859378eSBarry Smith 
3569060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
35709200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3571060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
357232c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
35739331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35749331c7a4SMatthew G. Knepley   } else if (ishdf5) {
35759331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35769331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3577b859378eSBarry Smith   PetscFunctionReturn(0);
3578b859378eSBarry Smith }
3579b859378eSBarry Smith 
35807da65231SMatthew G Knepley /******************************** FEM Support **********************************/
35817da65231SMatthew G Knepley 
3582a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3583a6dfd86eSKarl Rupp {
35841d47ebbbSSatish Balay   PetscInt       f;
35851b30c384SMatthew G Knepley   PetscErrorCode ierr;
35861b30c384SMatthew G Knepley 
35877da65231SMatthew G Knepley   PetscFunctionBegin;
358874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35891d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
359057622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
35917da65231SMatthew G Knepley   }
35927da65231SMatthew G Knepley   PetscFunctionReturn(0);
35937da65231SMatthew G Knepley }
35947da65231SMatthew G Knepley 
3595a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3596a6dfd86eSKarl Rupp {
35971b30c384SMatthew G Knepley   PetscInt       f, g;
35987da65231SMatthew G Knepley   PetscErrorCode ierr;
35997da65231SMatthew G Knepley 
36007da65231SMatthew G Knepley   PetscFunctionBegin;
360174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
36021d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
360374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
36041d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3605e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
36067da65231SMatthew G Knepley     }
360774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
36087da65231SMatthew G Knepley   }
36097da65231SMatthew G Knepley   PetscFunctionReturn(0);
36107da65231SMatthew G Knepley }
3611e7c4fc90SDmitry Karpeev 
36126113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3613e759306cSMatthew G. Knepley {
36140c5b8624SToby Isaac   PetscInt          localSize, bs;
36150c5b8624SToby Isaac   PetscMPIInt       size;
36160c5b8624SToby Isaac   Vec               x, xglob;
36170c5b8624SToby Isaac   const PetscScalar *xarray;
3618e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3619e759306cSMatthew G. Knepley 
3620e759306cSMatthew G. Knepley   PetscFunctionBegin;
36219852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3622e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3623e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
36246113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
36250c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
36260c5b8624SToby Isaac   if (size > 1) {
36270c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
36280c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
36290c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
36300c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
36310c5b8624SToby Isaac   } else {
36320c5b8624SToby Isaac     xglob = x;
36330c5b8624SToby Isaac   }
36340c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
36350c5b8624SToby Isaac   if (size > 1) {
36360c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
36370c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
36380c5b8624SToby Isaac   }
3639e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3640e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3641e759306cSMatthew G. Knepley }
3642e759306cSMatthew G. Knepley 
364388ed4aceSMatthew G Knepley /*@
3644e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
364588ed4aceSMatthew G Knepley 
364688ed4aceSMatthew G Knepley   Input Parameter:
364788ed4aceSMatthew G Knepley . dm - The DM
364888ed4aceSMatthew G Knepley 
364988ed4aceSMatthew G Knepley   Output Parameter:
365088ed4aceSMatthew G Knepley . section - The PetscSection
365188ed4aceSMatthew G Knepley 
3652e5893cccSMatthew G. Knepley   Options Database Keys:
3653e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3654e5893cccSMatthew G. Knepley 
365588ed4aceSMatthew G Knepley   Level: intermediate
365688ed4aceSMatthew G Knepley 
365788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
365888ed4aceSMatthew G Knepley 
3659e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
366088ed4aceSMatthew G Knepley @*/
3661e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
36620adebc6cSBarry Smith {
3663fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3664fd59a867SMatthew G. Knepley 
366588ed4aceSMatthew G Knepley   PetscFunctionBegin;
366688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
366788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
36682f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
36692f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3670ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
36712f0f8703SMatthew G. Knepley   }
367288ed4aceSMatthew G Knepley   *section = dm->defaultSection;
367388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
367488ed4aceSMatthew G Knepley }
367588ed4aceSMatthew G Knepley 
367688ed4aceSMatthew G Knepley /*@
3677e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
367888ed4aceSMatthew G Knepley 
367988ed4aceSMatthew G Knepley   Input Parameters:
368088ed4aceSMatthew G Knepley + dm - The DM
368188ed4aceSMatthew G Knepley - section - The PetscSection
368288ed4aceSMatthew G Knepley 
368388ed4aceSMatthew G Knepley   Level: intermediate
368488ed4aceSMatthew G Knepley 
368588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
368688ed4aceSMatthew G Knepley 
3687e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
368888ed4aceSMatthew G Knepley @*/
3689e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
36900adebc6cSBarry Smith {
3691c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3692af122d2aSMatthew G Knepley   PetscInt       f;
369388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
369488ed4aceSMatthew G Knepley 
369588ed4aceSMatthew G Knepley   PetscFunctionBegin;
369688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3697c473ab19SMatthew G. Knepley   if (section) {
36981d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36991d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3700c473ab19SMatthew G. Knepley   }
370188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
370288ed4aceSMatthew G Knepley   dm->defaultSection = section;
3703c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3704af122d2aSMatthew G Knepley   if (numFields) {
3705af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3706af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
37070f21e855SMatthew G. Knepley       PetscObject disc;
3708af122d2aSMatthew G Knepley       const char *name;
3709af122d2aSMatthew G Knepley 
3710af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
37110f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
37120f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3713af122d2aSMatthew G Knepley     }
3714af122d2aSMatthew G Knepley   }
3715e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
37161d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
371788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
371888ed4aceSMatthew G Knepley }
371988ed4aceSMatthew G Knepley 
37209435951eSToby Isaac /*@
37219435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
37229435951eSToby Isaac 
3723e228b242SToby Isaac   not collective
3724e228b242SToby Isaac 
37259435951eSToby Isaac   Input Parameter:
37269435951eSToby Isaac . dm - The DM
37279435951eSToby Isaac 
37289435951eSToby Isaac   Output Parameter:
37299435951eSToby 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.
37309435951eSToby 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.
37319435951eSToby Isaac 
37329435951eSToby Isaac   Level: advanced
37339435951eSToby Isaac 
37349435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
37359435951eSToby Isaac 
37369435951eSToby Isaac .seealso: DMSetDefaultConstraints()
37379435951eSToby Isaac @*/
37389435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
37399435951eSToby Isaac {
37409435951eSToby Isaac   PetscErrorCode ierr;
37419435951eSToby Isaac 
37429435951eSToby Isaac   PetscFunctionBegin;
37439435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37449435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
374545a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
374645a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
37479435951eSToby Isaac   PetscFunctionReturn(0);
37489435951eSToby Isaac }
37499435951eSToby Isaac 
37509435951eSToby Isaac /*@
37519435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
37529435951eSToby Isaac 
37539435951eSToby 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().
37549435951eSToby Isaac 
37559435951eSToby 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.
37569435951eSToby Isaac 
3757e228b242SToby Isaac   collective on dm
3758e228b242SToby Isaac 
37599435951eSToby Isaac   Input Parameters:
37609435951eSToby Isaac + dm - The DM
3761e228b242SToby 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).
3762e228b242SToby 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).
37639435951eSToby Isaac 
37649435951eSToby Isaac   Level: advanced
37659435951eSToby Isaac 
37669435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
37679435951eSToby Isaac 
37689435951eSToby Isaac .seealso: DMGetDefaultConstraints()
37699435951eSToby Isaac @*/
37709435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
37719435951eSToby Isaac {
3772e228b242SToby Isaac   PetscMPIInt result;
37739435951eSToby Isaac   PetscErrorCode ierr;
37749435951eSToby Isaac 
37759435951eSToby Isaac   PetscFunctionBegin;
37769435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3777e228b242SToby Isaac   if (section) {
3778e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3779e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3780f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3781e228b242SToby Isaac   }
3782e228b242SToby Isaac   if (mat) {
3783e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3784e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3785f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3786e228b242SToby Isaac   }
37879435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
37889435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
37899435951eSToby Isaac   dm->defaultConstraintSection = section;
37909435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
37919435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
37929435951eSToby Isaac   dm->defaultConstraintMat = mat;
37939435951eSToby Isaac   PetscFunctionReturn(0);
37949435951eSToby Isaac }
37959435951eSToby Isaac 
3796497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3797507e4973SMatthew G. Knepley /*
3798507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3799507e4973SMatthew G. Knepley 
3800507e4973SMatthew G. Knepley   Input Parameters:
3801507e4973SMatthew G. Knepley + dm - The DM
3802507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3803507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3804507e4973SMatthew G. Knepley 
3805507e4973SMatthew G. Knepley   Level: intermediate
3806507e4973SMatthew G. Knepley 
3807507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3808507e4973SMatthew G. Knepley */
3809f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3810507e4973SMatthew G. Knepley {
3811507e4973SMatthew G. Knepley   MPI_Comm        comm;
3812507e4973SMatthew G. Knepley   PetscLayout     layout;
3813507e4973SMatthew G. Knepley   const PetscInt *ranges;
3814507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3815507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3816507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3817507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3818507e4973SMatthew G. Knepley 
3819507e4973SMatthew G. Knepley   PetscFunctionBegin;
3820507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3821507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3822507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3823507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3824507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3825507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3826507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3827507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3828507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3829507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3830507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3831507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3832f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3833507e4973SMatthew G. Knepley 
3834507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3835507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3836507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3837507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3838507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3839507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3840507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3841507e4973SMatthew 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;}
3842507e4973SMatthew 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;}
3843507e4973SMatthew G. Knepley     if (gdof < 0) {
3844507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3845507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3846507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3847507e4973SMatthew G. Knepley 
3848507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3849507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3850507e4973SMatthew 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;}
3851507e4973SMatthew G. Knepley       }
3852507e4973SMatthew G. Knepley     }
3853507e4973SMatthew G. Knepley   }
3854507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3855507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3856b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3857507e4973SMatthew G. Knepley   if (!gvalid) {
3858507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3859507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3860507e4973SMatthew G. Knepley   }
3861507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3862507e4973SMatthew G. Knepley }
3863f741bcd2SMatthew G. Knepley #endif
3864507e4973SMatthew G. Knepley 
386588ed4aceSMatthew G Knepley /*@
3866e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
386788ed4aceSMatthew G Knepley 
38688b1ab98fSJed Brown   Collective on DM
38698b1ab98fSJed Brown 
387088ed4aceSMatthew G Knepley   Input Parameter:
387188ed4aceSMatthew G Knepley . dm - The DM
387288ed4aceSMatthew G Knepley 
387388ed4aceSMatthew G Knepley   Output Parameter:
387488ed4aceSMatthew G Knepley . section - The PetscSection
387588ed4aceSMatthew G Knepley 
387688ed4aceSMatthew G Knepley   Level: intermediate
387788ed4aceSMatthew G Knepley 
387888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
387988ed4aceSMatthew G Knepley 
3880e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
388188ed4aceSMatthew G Knepley @*/
3882e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
38830adebc6cSBarry Smith {
388488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
388588ed4aceSMatthew G Knepley 
388688ed4aceSMatthew G Knepley   PetscFunctionBegin;
388788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
388888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
388988ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3890fd59a867SMatthew G. Knepley     PetscSection s;
3891fd59a867SMatthew G. Knepley 
3892e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3893fd59a867SMatthew 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");
389433907cc2SStefano 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");
389515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3896cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3897ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3898685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
389988ed4aceSMatthew G Knepley   }
390088ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
390188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
390288ed4aceSMatthew G Knepley }
390388ed4aceSMatthew G Knepley 
3904b21d0597SMatthew G Knepley /*@
3905e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3906b21d0597SMatthew G Knepley 
3907b21d0597SMatthew G Knepley   Input Parameters:
3908b21d0597SMatthew G Knepley + dm - The DM
39095080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3910b21d0597SMatthew G Knepley 
3911b21d0597SMatthew G Knepley   Level: intermediate
3912b21d0597SMatthew G Knepley 
3913b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3914b21d0597SMatthew G Knepley 
3915e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3916b21d0597SMatthew G Knepley @*/
3917e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
39180adebc6cSBarry Smith {
3919b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3920b21d0597SMatthew G Knepley 
3921b21d0597SMatthew G Knepley   PetscFunctionBegin;
3922b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39235080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
39241d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3925b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3926b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3927497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3928f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3929507e4973SMatthew G. Knepley #endif
3930b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3931b21d0597SMatthew G Knepley }
3932b21d0597SMatthew G Knepley 
393388ed4aceSMatthew G Knepley /*@
393488ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
393588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
393688ed4aceSMatthew G Knepley 
393788ed4aceSMatthew G Knepley   Input Parameter:
393888ed4aceSMatthew G Knepley . dm - The DM
393988ed4aceSMatthew G Knepley 
394088ed4aceSMatthew G Knepley   Output Parameter:
394188ed4aceSMatthew G Knepley . sf - The PetscSF
394288ed4aceSMatthew G Knepley 
394388ed4aceSMatthew G Knepley   Level: intermediate
394488ed4aceSMatthew G Knepley 
394588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
394688ed4aceSMatthew G Knepley 
394788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
394888ed4aceSMatthew G Knepley @*/
39490adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
39500adebc6cSBarry Smith {
395188ed4aceSMatthew G Knepley   PetscInt       nroots;
395288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
395388ed4aceSMatthew G Knepley 
395488ed4aceSMatthew G Knepley   PetscFunctionBegin;
395588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
395688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
395733907cc2SStefano Zampini   if (!dm->defaultSF) {
395833907cc2SStefano Zampini     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->defaultSF);CHKERRQ(ierr);
395933907cc2SStefano Zampini   }
39600298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
396188ed4aceSMatthew G Knepley   if (nroots < 0) {
396288ed4aceSMatthew G Knepley     PetscSection section, gSection;
396388ed4aceSMatthew G Knepley 
3964e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
396531ea6d37SMatthew G Knepley     if (section) {
3966e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
396788ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
396831ea6d37SMatthew G Knepley     } else {
39690298fd71SBarry Smith       *sf = NULL;
397031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
397131ea6d37SMatthew G Knepley     }
397288ed4aceSMatthew G Knepley   }
397388ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
397488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
397588ed4aceSMatthew G Knepley }
397688ed4aceSMatthew G Knepley 
397788ed4aceSMatthew G Knepley /*@
397888ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
397988ed4aceSMatthew G Knepley 
398088ed4aceSMatthew G Knepley   Input Parameters:
398188ed4aceSMatthew G Knepley + dm - The DM
398288ed4aceSMatthew G Knepley - sf - The PetscSF
398388ed4aceSMatthew G Knepley 
398488ed4aceSMatthew G Knepley   Level: intermediate
398588ed4aceSMatthew G Knepley 
398688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
398788ed4aceSMatthew G Knepley 
398888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
398988ed4aceSMatthew G Knepley @*/
39900adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
39910adebc6cSBarry Smith {
399288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
399388ed4aceSMatthew G Knepley 
399488ed4aceSMatthew G Knepley   PetscFunctionBegin;
399588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
399633907cc2SStefano Zampini   if (sf) {
399788ed4aceSMatthew G Knepley     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
399833907cc2SStefano Zampini     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
399933907cc2SStefano Zampini   }
400088ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
400188ed4aceSMatthew G Knepley   dm->defaultSF = sf;
400288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
400388ed4aceSMatthew G Knepley }
400488ed4aceSMatthew G Knepley 
400588ed4aceSMatthew G Knepley /*@C
400688ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
400788ed4aceSMatthew G Knepley   describing the data layout.
400888ed4aceSMatthew G Knepley 
400988ed4aceSMatthew G Knepley   Input Parameters:
401088ed4aceSMatthew G Knepley + dm - The DM
401188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
401288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
401388ed4aceSMatthew G Knepley 
401488ed4aceSMatthew G Knepley   Level: intermediate
401588ed4aceSMatthew G Knepley 
401688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
401788ed4aceSMatthew G Knepley @*/
401888ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
401988ed4aceSMatthew G Knepley {
402082f516ccSBarry Smith   MPI_Comm       comm;
402188ed4aceSMatthew G Knepley   PetscLayout    layout;
402288ed4aceSMatthew G Knepley   const PetscInt *ranges;
402388ed4aceSMatthew G Knepley   PetscInt       *local;
402488ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4025ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
402688ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
402788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
402888ed4aceSMatthew G Knepley 
402988ed4aceSMatthew G Knepley   PetscFunctionBegin;
403088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4031367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
403288ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
403388ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
403488ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
403588ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
403688ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
403788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
403888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
403988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
404088ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4041ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
40426636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
404388ed4aceSMatthew G Knepley 
40446636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40456636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4046235fbf56SMatthew 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));
40476636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
404888ed4aceSMatthew G Knepley   }
4049785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4050785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
405188ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
40521f588964SMatthew G Knepley     const PetscInt *cind;
40536636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
405488ed4aceSMatthew G Knepley 
405588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
405688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
405788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
405888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
405988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40606636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
406188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
40626636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
40636636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
40646636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4065057b4bcdSMatthew 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);
40666636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
40676636e97aSMatthew G Knepley     }
406888ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
406988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
407088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
407188ed4aceSMatthew G Knepley     }
407288ed4aceSMatthew G Knepley     if (gdof < 0) {
40736636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
407488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
407588ed4aceSMatthew G Knepley 
407605376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
407731d3f06eSJed Brown         if (r < 0) r = -(r+2);
407805376888SMatthew 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);
407988ed4aceSMatthew G Knepley         remote[l].rank  = r;
408088ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
408188ed4aceSMatthew G Knepley       }
408288ed4aceSMatthew G Knepley     } else {
40836636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
408488ed4aceSMatthew G Knepley         remote[l].rank  = rank;
408588ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
408688ed4aceSMatthew G Knepley       }
408788ed4aceSMatthew G Knepley     }
408888ed4aceSMatthew G Knepley   }
40896636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
409088ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
409188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
409288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
409388ed4aceSMatthew G Knepley }
4094af122d2aSMatthew G Knepley 
4095b21d0597SMatthew G Knepley /*@
4096b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4097b21d0597SMatthew G Knepley 
4098b21d0597SMatthew G Knepley   Input Parameter:
4099b21d0597SMatthew G Knepley . dm - The DM
4100b21d0597SMatthew G Knepley 
4101b21d0597SMatthew G Knepley   Output Parameter:
4102b21d0597SMatthew G Knepley . sf - The PetscSF
4103b21d0597SMatthew G Knepley 
4104b21d0597SMatthew G Knepley   Level: intermediate
4105b21d0597SMatthew G Knepley 
4106b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4107b21d0597SMatthew G Knepley 
4108057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4109b21d0597SMatthew G Knepley @*/
41100adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
41110adebc6cSBarry Smith {
4112b21d0597SMatthew G Knepley   PetscFunctionBegin;
4113b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4114b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4115b21d0597SMatthew G Knepley   *sf = dm->sf;
4116b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4117b21d0597SMatthew G Knepley }
4118b21d0597SMatthew G Knepley 
4119057b4bcdSMatthew G Knepley /*@
4120057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4121057b4bcdSMatthew G Knepley 
4122057b4bcdSMatthew G Knepley   Input Parameters:
4123057b4bcdSMatthew G Knepley + dm - The DM
4124057b4bcdSMatthew G Knepley - sf - The PetscSF
4125057b4bcdSMatthew G Knepley 
4126057b4bcdSMatthew G Knepley   Level: intermediate
4127057b4bcdSMatthew G Knepley 
4128057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4129057b4bcdSMatthew G Knepley @*/
41300adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
41310adebc6cSBarry Smith {
4132057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4133057b4bcdSMatthew G Knepley 
4134057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4135057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413633907cc2SStefano Zampini   if (sf) {
413733907cc2SStefano Zampini     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4138057b4bcdSMatthew G Knepley     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
413933907cc2SStefano Zampini   }
414033907cc2SStefano Zampini   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4141057b4bcdSMatthew G Knepley   dm->sf = sf;
4142057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4143057b4bcdSMatthew G Knepley }
4144057b4bcdSMatthew G Knepley 
41452764a2aaSMatthew G. Knepley /*@
41462764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
41472764a2aaSMatthew G. Knepley 
41482764a2aaSMatthew G. Knepley   Input Parameter:
41492764a2aaSMatthew G. Knepley . dm - The DM
41502764a2aaSMatthew G. Knepley 
41512764a2aaSMatthew G. Knepley   Output Parameter:
41522764a2aaSMatthew G. Knepley . prob - The PetscDS
41532764a2aaSMatthew G. Knepley 
41542764a2aaSMatthew G. Knepley   Level: developer
41552764a2aaSMatthew G. Knepley 
41562764a2aaSMatthew G. Knepley .seealso: DMSetDS()
41572764a2aaSMatthew G. Knepley @*/
41582764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4159af122d2aSMatthew G Knepley {
4160af122d2aSMatthew G Knepley   PetscFunctionBegin;
4161af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41620f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
41630f21e855SMatthew G. Knepley   *prob = dm->prob;
41640f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41650f21e855SMatthew G. Knepley }
41660f21e855SMatthew G. Knepley 
41672764a2aaSMatthew G. Knepley /*@
41682764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
41692764a2aaSMatthew G. Knepley 
41702764a2aaSMatthew G. Knepley   Input Parameters:
41712764a2aaSMatthew G. Knepley + dm - The DM
41722764a2aaSMatthew G. Knepley - prob - The PetscDS
41732764a2aaSMatthew G. Knepley 
41742764a2aaSMatthew G. Knepley   Level: developer
41752764a2aaSMatthew G. Knepley 
41762764a2aaSMatthew G. Knepley .seealso: DMGetDS()
41772764a2aaSMatthew G. Knepley @*/
41782764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
41790f21e855SMatthew G. Knepley {
4180f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
41810f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41820f21e855SMatthew G. Knepley 
41830f21e855SMatthew G. Knepley   PetscFunctionBegin;
41840f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41852764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
418637316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
41872764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
41880f21e855SMatthew G. Knepley   dm->prob = prob;
4189f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4190f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
41910f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41920f21e855SMatthew G. Knepley }
41930f21e855SMatthew G. Knepley 
4194*689b5837SMatthew G. Knepley /*@
4195*689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4196*689b5837SMatthew G. Knepley 
4197*689b5837SMatthew G. Knepley   Not collective
4198*689b5837SMatthew G. Knepley 
4199*689b5837SMatthew G. Knepley   Input Parameter:
4200*689b5837SMatthew G. Knepley . dm - The DM
4201*689b5837SMatthew G. Knepley 
4202*689b5837SMatthew G. Knepley   Output Parameter:
4203*689b5837SMatthew G. Knepley . Nf - The number of fields
4204*689b5837SMatthew G. Knepley 
4205*689b5837SMatthew G. Knepley   Level: intermediate
4206*689b5837SMatthew G. Knepley 
4207*689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4208*689b5837SMatthew G. Knepley @*/
42090f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
42100f21e855SMatthew G. Knepley {
42110f21e855SMatthew G. Knepley   PetscErrorCode ierr;
42120f21e855SMatthew G. Knepley 
42130f21e855SMatthew G. Knepley   PetscFunctionBegin;
42140f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42152764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
4216af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4217af122d2aSMatthew G Knepley }
4218af122d2aSMatthew G Knepley 
4219*689b5837SMatthew G. Knepley /*@
4220*689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4221*689b5837SMatthew G. Knepley 
4222*689b5837SMatthew G. Knepley   Logically collective on DM
4223*689b5837SMatthew G. Knepley 
4224*689b5837SMatthew G. Knepley   Input Parameters:
4225*689b5837SMatthew G. Knepley + dm - The DM
4226*689b5837SMatthew G. Knepley - Nf - The number of fields
4227*689b5837SMatthew G. Knepley 
4228*689b5837SMatthew G. Knepley   Level: intermediate
4229*689b5837SMatthew G. Knepley 
4230*689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4231*689b5837SMatthew G. Knepley @*/
4232af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4233af122d2aSMatthew G Knepley {
42340f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4235af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4236af122d2aSMatthew G Knepley 
4237af122d2aSMatthew G Knepley   PetscFunctionBegin;
4238af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42392764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
42400f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
42410f21e855SMatthew G. Knepley     PetscContainer obj;
42420f21e855SMatthew G. Knepley 
42430f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
42442764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
42450f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4246af122d2aSMatthew G Knepley   }
4247af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4248af122d2aSMatthew G Knepley }
4249af122d2aSMatthew G Knepley 
4250c1929be8SMatthew G. Knepley /*@
4251c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4252c1929be8SMatthew G. Knepley 
4253c1929be8SMatthew G. Knepley   Not collective
4254c1929be8SMatthew G. Knepley 
4255c1929be8SMatthew G. Knepley   Input Parameters:
4256c1929be8SMatthew G. Knepley + dm - The DM
4257c1929be8SMatthew G. Knepley - f  - The field number
4258c1929be8SMatthew G. Knepley 
4259c1929be8SMatthew G. Knepley   Output Parameter:
4260c1929be8SMatthew G. Knepley . field - The discretization object
4261c1929be8SMatthew G. Knepley 
4262c1929be8SMatthew G. Knepley   Level: developer
4263c1929be8SMatthew G. Knepley 
4264c1929be8SMatthew G. Knepley .seealso: DMSetField()
4265c1929be8SMatthew G. Knepley @*/
4266af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4267af122d2aSMatthew G Knepley {
42680f21e855SMatthew G. Knepley   PetscErrorCode ierr;
42690f21e855SMatthew G. Knepley 
4270af122d2aSMatthew G Knepley   PetscFunctionBegin;
4271af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42722764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4273decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4274decb47aaSMatthew G. Knepley }
4275decb47aaSMatthew G. Knepley 
4276c1929be8SMatthew G. Knepley /*@
4277c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4278c1929be8SMatthew G. Knepley 
4279c1929be8SMatthew G. Knepley   Logically collective on DM
4280c1929be8SMatthew G. Knepley 
4281c1929be8SMatthew G. Knepley   Input Parameters:
4282c1929be8SMatthew G. Knepley + dm - The DM
4283c1929be8SMatthew G. Knepley . f  - The field number
4284c1929be8SMatthew G. Knepley - field - The discretization object
4285c1929be8SMatthew G. Knepley 
4286c1929be8SMatthew G. Knepley   Level: developer
4287c1929be8SMatthew G. Knepley 
4288c1929be8SMatthew G. Knepley .seealso: DMGetField()
4289c1929be8SMatthew G. Knepley @*/
4290decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4291decb47aaSMatthew G. Knepley {
4292decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4293decb47aaSMatthew G. Knepley 
4294decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4295decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42962764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4297af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4298af122d2aSMatthew G Knepley }
42996636e97aSMatthew G Knepley 
4300b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4301b64e0483SPeter Brune {
4302b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4303b64e0483SPeter Brune   PetscErrorCode ierr;
4304b64e0483SPeter Brune   Vec coords,ccoords;
43056dbf9973SLawrence Mitchell   Mat inject;
4306b64e0483SPeter Brune   PetscFunctionBegin;
4307b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4308b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4309b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4310b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4311b64e0483SPeter Brune   if (coords && !ccoords) {
4312b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
43136668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
43146dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
43152adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
43166dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4317b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4318b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4319b64e0483SPeter Brune   }
4320b64e0483SPeter Brune   PetscFunctionReturn(0);
4321b64e0483SPeter Brune }
4322b64e0483SPeter Brune 
432303dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
432403dadc2fSPeter Brune {
432503dadc2fSPeter Brune   DM dm_coord,subdm_coord;
432603dadc2fSPeter Brune   PetscErrorCode ierr;
432703dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
432803dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
432903dadc2fSPeter Brune   PetscFunctionBegin;
433003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
433103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
433203dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
433303dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
433403dadc2fSPeter Brune   if (coords && !ccoords) {
433503dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
43366668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
433703dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
433824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
433903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
434003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
434103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
434203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
434303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
434403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
434503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
434603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
434703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
434803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
434903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
435003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
435103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
435203dadc2fSPeter Brune   }
435303dadc2fSPeter Brune   PetscFunctionReturn(0);
435403dadc2fSPeter Brune }
435503dadc2fSPeter Brune 
4356c73cfb54SMatthew G. Knepley /*@
4357c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4358c73cfb54SMatthew G. Knepley 
4359c73cfb54SMatthew G. Knepley   Not collective
4360c73cfb54SMatthew G. Knepley 
4361c73cfb54SMatthew G. Knepley   Input Parameter:
4362c73cfb54SMatthew G. Knepley . dm - The DM
4363c73cfb54SMatthew G. Knepley 
4364c73cfb54SMatthew G. Knepley   Output Parameter:
4365c73cfb54SMatthew G. Knepley . dim - The topological dimension
4366c73cfb54SMatthew G. Knepley 
4367c73cfb54SMatthew G. Knepley   Level: beginner
4368c73cfb54SMatthew G. Knepley 
4369c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4370c73cfb54SMatthew G. Knepley @*/
4371c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4372c73cfb54SMatthew G. Knepley {
4373c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4374c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4375c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4376c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4377c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4378c73cfb54SMatthew G. Knepley }
4379c73cfb54SMatthew G. Knepley 
4380c73cfb54SMatthew G. Knepley /*@
4381c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4382c73cfb54SMatthew G. Knepley 
4383c73cfb54SMatthew G. Knepley   Collective on dm
4384c73cfb54SMatthew G. Knepley 
4385c73cfb54SMatthew G. Knepley   Input Parameters:
4386c73cfb54SMatthew G. Knepley + dm - The DM
4387c73cfb54SMatthew G. Knepley - dim - The topological dimension
4388c73cfb54SMatthew G. Knepley 
4389c73cfb54SMatthew G. Knepley   Level: beginner
4390c73cfb54SMatthew G. Knepley 
4391c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4392c73cfb54SMatthew G. Knepley @*/
4393c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4394c73cfb54SMatthew G. Knepley {
4395f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4396f17e8794SMatthew G. Knepley 
4397c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4398c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4399c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4400c73cfb54SMatthew G. Knepley   dm->dim = dim;
4401f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4402c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4403c73cfb54SMatthew G. Knepley }
4404c73cfb54SMatthew G. Knepley 
4405793f3fe5SMatthew G. Knepley /*@
4406793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4407793f3fe5SMatthew G. Knepley 
4408793f3fe5SMatthew G. Knepley   Collective on DM
4409793f3fe5SMatthew G. Knepley 
4410793f3fe5SMatthew G. Knepley   Input Parameters:
4411793f3fe5SMatthew G. Knepley + dm - the DM
4412793f3fe5SMatthew G. Knepley - dim - the dimension
4413793f3fe5SMatthew G. Knepley 
4414793f3fe5SMatthew G. Knepley   Output Parameters:
4415793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4416793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4417793f3fe5SMatthew G. Knepley 
4418793f3fe5SMatthew G. Knepley   Note:
4419793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4420793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4421793f3fe5SMatthew G. Knepley   then the interval is empty.
4422793f3fe5SMatthew G. Knepley 
4423793f3fe5SMatthew G. Knepley   Level: intermediate
4424793f3fe5SMatthew G. Knepley 
4425793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4426793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4427793f3fe5SMatthew G. Knepley @*/
4428793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4429793f3fe5SMatthew G. Knepley {
4430793f3fe5SMatthew G. Knepley   PetscInt       d;
4431793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4432793f3fe5SMatthew G. Knepley 
4433793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4434793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4435793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4436793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4437793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4438793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4439793f3fe5SMatthew G. Knepley }
4440793f3fe5SMatthew G. Knepley 
44416636e97aSMatthew G Knepley /*@
44426636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
44436636e97aSMatthew G Knepley 
44446636e97aSMatthew G Knepley   Collective on DM
44456636e97aSMatthew G Knepley 
44466636e97aSMatthew G Knepley   Input Parameters:
44476636e97aSMatthew G Knepley + dm - the DM
44486636e97aSMatthew G Knepley - c - coordinate vector
44496636e97aSMatthew G Knepley 
44502dd40e9bSPatrick Sanan   Notes:
44512dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
44522dd40e9bSPatrick Sanan 
44532dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
44546636e97aSMatthew G Knepley 
44556636e97aSMatthew G Knepley   Level: intermediate
44566636e97aSMatthew G Knepley 
44576636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44582dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
44596636e97aSMatthew G Knepley @*/
44606636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
44616636e97aSMatthew G Knepley {
44626636e97aSMatthew G Knepley   PetscErrorCode ierr;
44636636e97aSMatthew G Knepley 
44646636e97aSMatthew G Knepley   PetscFunctionBegin;
44656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
44676636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
44686636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
44696636e97aSMatthew G Knepley   dm->coordinates = c;
44706636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4471b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
447203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
44736636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44746636e97aSMatthew G Knepley }
44756636e97aSMatthew G Knepley 
44766636e97aSMatthew G Knepley /*@
44776636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
44786636e97aSMatthew G Knepley 
44797058e716SVaclav Hapla   Not collective
44806636e97aSMatthew G Knepley 
44816636e97aSMatthew G Knepley    Input Parameters:
44826636e97aSMatthew G Knepley +  dm - the DM
44836636e97aSMatthew G Knepley -  c - coordinate vector
44846636e97aSMatthew G Knepley 
44852dd40e9bSPatrick Sanan   Notes:
44866636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
44876636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
44886636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
44896636e97aSMatthew G Knepley 
44902dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
44912dd40e9bSPatrick Sanan 
44926636e97aSMatthew G Knepley   Level: intermediate
44936636e97aSMatthew G Knepley 
44946636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44956636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
44966636e97aSMatthew G Knepley @*/
44976636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
44986636e97aSMatthew G Knepley {
44996636e97aSMatthew G Knepley   PetscErrorCode ierr;
45006636e97aSMatthew G Knepley 
45016636e97aSMatthew G Knepley   PetscFunctionBegin;
45026636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45036636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
45046636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
45056636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
45068865f1eaSKarl Rupp 
45076636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
45088865f1eaSKarl Rupp 
45096636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
45106636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45116636e97aSMatthew G Knepley }
45126636e97aSMatthew G Knepley 
45136636e97aSMatthew G Knepley /*@
45146636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
45156636e97aSMatthew G Knepley 
4516c4a54dccSVaclav Hapla   Collective on DM
45176636e97aSMatthew G Knepley 
45186636e97aSMatthew G Knepley   Input Parameter:
45196636e97aSMatthew G Knepley . dm - the DM
45206636e97aSMatthew G Knepley 
45216636e97aSMatthew G Knepley   Output Parameter:
45226636e97aSMatthew G Knepley . c - global coordinate vector
45236636e97aSMatthew G Knepley 
45246636e97aSMatthew G Knepley   Note:
45256636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
45266636e97aSMatthew G Knepley 
45276636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
45286636e97aSMatthew G Knepley 
45296636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
45306636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
45316636e97aSMatthew G Knepley 
45326636e97aSMatthew G Knepley   Level: intermediate
45336636e97aSMatthew G Knepley 
45346636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45356636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
45366636e97aSMatthew G Knepley @*/
45376636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
45386636e97aSMatthew G Knepley {
45396636e97aSMatthew G Knepley   PetscErrorCode ierr;
45406636e97aSMatthew G Knepley 
45416636e97aSMatthew G Knepley   PetscFunctionBegin;
45426636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45436636e97aSMatthew G Knepley   PetscValidPointer(c,2);
45441f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
45450298fd71SBarry Smith     DM cdm = NULL;
45466636e97aSMatthew G Knepley 
45476636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
45486636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
45496636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
45506636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
45516636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
45526636e97aSMatthew G Knepley   }
45536636e97aSMatthew G Knepley   *c = dm->coordinates;
45546636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45556636e97aSMatthew G Knepley }
45566636e97aSMatthew G Knepley 
45576636e97aSMatthew G Knepley /*@
455881e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
455981e9a530SVaclav Hapla 
456081e9a530SVaclav Hapla   Collective on DM
456181e9a530SVaclav Hapla 
456281e9a530SVaclav Hapla   Input Parameter:
456381e9a530SVaclav Hapla . dm - the DM
456481e9a530SVaclav Hapla 
456581e9a530SVaclav Hapla   Level: advanced
456681e9a530SVaclav Hapla 
456781e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
456881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
456981e9a530SVaclav Hapla @*/
457081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
457181e9a530SVaclav Hapla {
457281e9a530SVaclav Hapla   DM cdm = NULL;
457381e9a530SVaclav Hapla   PetscErrorCode ierr;
457481e9a530SVaclav Hapla 
457581e9a530SVaclav Hapla   PetscFunctionBegin;
457681e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
457781e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
457881e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
457981e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
458081e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
458181e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
458281e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
458381e9a530SVaclav Hapla   }
458481e9a530SVaclav Hapla   PetscFunctionReturn(0);
458581e9a530SVaclav Hapla }
458681e9a530SVaclav Hapla 
458781e9a530SVaclav Hapla /*@
45886636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
45896636e97aSMatthew G Knepley 
45906636e97aSMatthew G Knepley   Collective on DM
45916636e97aSMatthew G Knepley 
45926636e97aSMatthew G Knepley   Input Parameter:
45936636e97aSMatthew G Knepley . dm - the DM
45946636e97aSMatthew G Knepley 
45956636e97aSMatthew G Knepley   Output Parameter:
45966636e97aSMatthew G Knepley . c - coordinate vector
45976636e97aSMatthew G Knepley 
45986636e97aSMatthew G Knepley   Note:
45996636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
46006636e97aSMatthew G Knepley 
46016636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
46026636e97aSMatthew G Knepley 
46036636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
46046636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
46056636e97aSMatthew G Knepley 
46066636e97aSMatthew G Knepley   Level: intermediate
46076636e97aSMatthew G Knepley 
46086636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
460981e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
46106636e97aSMatthew G Knepley @*/
46116636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
46126636e97aSMatthew G Knepley {
46136636e97aSMatthew G Knepley   PetscErrorCode ierr;
46146636e97aSMatthew G Knepley 
46156636e97aSMatthew G Knepley   PetscFunctionBegin;
46166636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46176636e97aSMatthew G Knepley   PetscValidPointer(c,2);
461881e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
46196636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
46206636e97aSMatthew G Knepley   PetscFunctionReturn(0);
46216636e97aSMatthew G Knepley }
46226636e97aSMatthew G Knepley 
462381e9a530SVaclav Hapla /*@
462481e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
462581e9a530SVaclav Hapla 
462681e9a530SVaclav Hapla   Not collective
462781e9a530SVaclav Hapla 
462881e9a530SVaclav Hapla   Input Parameter:
462981e9a530SVaclav Hapla . dm - the DM
463081e9a530SVaclav Hapla 
463181e9a530SVaclav Hapla   Output Parameter:
463281e9a530SVaclav Hapla . c - coordinate vector
463381e9a530SVaclav Hapla 
463481e9a530SVaclav Hapla   Level: advanced
463581e9a530SVaclav Hapla 
463681e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
463781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
463881e9a530SVaclav Hapla @*/
463981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
464081e9a530SVaclav Hapla {
464181e9a530SVaclav Hapla   PetscFunctionBegin;
464281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
464381e9a530SVaclav Hapla   PetscValidPointer(c,2);
464481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
46456636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
46466636e97aSMatthew G Knepley   PetscFunctionReturn(0);
46476636e97aSMatthew G Knepley }
46486636e97aSMatthew G Knepley 
46492db98f8dSVaclav Hapla /*@
46502db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
46512db98f8dSVaclav Hapla 
46522db98f8dSVaclav Hapla   Not collective
46532db98f8dSVaclav Hapla 
46542db98f8dSVaclav Hapla   Input Parameter:
46552db98f8dSVaclav Hapla + dm - the DM
46562db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
46572db98f8dSVaclav Hapla 
46582db98f8dSVaclav Hapla   Output Parameter:
46592db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
46602db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
46612db98f8dSVaclav Hapla 
46622db98f8dSVaclav Hapla   Note:
46632db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
46642db98f8dSVaclav Hapla 
46652db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
46662db98f8dSVaclav Hapla 
46672db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
46682db98f8dSVaclav Hapla 
46692db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
46702db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
46712db98f8dSVaclav Hapla 
46722db98f8dSVaclav Hapla   Level: advanced
46732db98f8dSVaclav Hapla 
46742db98f8dSVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
46752db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
46762db98f8dSVaclav Hapla @*/
46772db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
46782db98f8dSVaclav Hapla {
46792db98f8dSVaclav Hapla   PetscSection        cs, newcs;
46802db98f8dSVaclav Hapla   Vec                 coords;
46812db98f8dSVaclav Hapla   const PetscScalar   *arr;
46822db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
46832db98f8dSVaclav Hapla   PetscInt            n;
46842db98f8dSVaclav Hapla   PetscErrorCode      ierr;
46852db98f8dSVaclav Hapla 
46862db98f8dSVaclav Hapla   PetscFunctionBegin;
46872db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46882db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
46892db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
46902db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
46912db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
46922db98f8dSVaclav Hapla   if (!dm->coordinateDM || !dm->coordinateDM->defaultSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
46932db98f8dSVaclav Hapla   cs = dm->coordinateDM->defaultSection;
46942db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
46952db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
46962db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
46972db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
46982db98f8dSVaclav Hapla   if (pCoord) {
46992db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
47002db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
47012db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
47022db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
4703ad9ac99dSVaclav Hapla   } else {
4704ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
47052db98f8dSVaclav Hapla   }
4706ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
4707ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
47082db98f8dSVaclav Hapla   PetscFunctionReturn(0);
47092db98f8dSVaclav Hapla }
47102db98f8dSVaclav Hapla 
4711f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
4712f19dbd58SToby Isaac {
4713f19dbd58SToby Isaac   PetscErrorCode ierr;
4714f19dbd58SToby Isaac 
4715f19dbd58SToby Isaac   PetscFunctionBegin;
4716f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4717f19dbd58SToby Isaac   PetscValidPointer(field,2);
4718f19dbd58SToby Isaac   if (!dm->coordinateField) {
4719f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
4720f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
4721f19dbd58SToby Isaac     }
4722f19dbd58SToby Isaac   }
4723f19dbd58SToby Isaac   *field = dm->coordinateField;
4724f19dbd58SToby Isaac   PetscFunctionReturn(0);
4725f19dbd58SToby Isaac }
4726f19dbd58SToby Isaac 
4727f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
4728f19dbd58SToby Isaac {
4729f19dbd58SToby Isaac   PetscErrorCode ierr;
4730f19dbd58SToby Isaac 
4731f19dbd58SToby Isaac   PetscFunctionBegin;
4732f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4733f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
4734c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
4735f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
4736f19dbd58SToby Isaac   dm->coordinateField = field;
4737f19dbd58SToby Isaac   PetscFunctionReturn(0);
4738f19dbd58SToby Isaac }
4739f19dbd58SToby Isaac 
47406636e97aSMatthew G Knepley /*@
47411cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
47426636e97aSMatthew G Knepley 
47436636e97aSMatthew G Knepley   Collective on DM
47446636e97aSMatthew G Knepley 
47456636e97aSMatthew G Knepley   Input Parameter:
47466636e97aSMatthew G Knepley . dm - the DM
47476636e97aSMatthew G Knepley 
47486636e97aSMatthew G Knepley   Output Parameter:
47496636e97aSMatthew G Knepley . cdm - coordinate DM
47506636e97aSMatthew G Knepley 
47516636e97aSMatthew G Knepley   Level: intermediate
47526636e97aSMatthew G Knepley 
47536636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
47541cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
47556636e97aSMatthew G Knepley @*/
47566636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
47576636e97aSMatthew G Knepley {
47586636e97aSMatthew G Knepley   PetscErrorCode ierr;
47596636e97aSMatthew G Knepley 
47606636e97aSMatthew G Knepley   PetscFunctionBegin;
47616636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47626636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
47636636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
4764308f8a94SToby Isaac     DM cdm;
4765308f8a94SToby Isaac 
476682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
4767308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
4768308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
4769308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
4770308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
4771308f8a94SToby Isaac     dm->coordinateDM = cdm;
47726636e97aSMatthew G Knepley   }
47736636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
47746636e97aSMatthew G Knepley   PetscFunctionReturn(0);
47756636e97aSMatthew G Knepley }
4776e87bb0d3SMatthew G Knepley 
47771cfe2091SMatthew G. Knepley /*@
47781cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
47791cfe2091SMatthew G. Knepley 
47801cfe2091SMatthew G. Knepley   Logically Collective on DM
47811cfe2091SMatthew G. Knepley 
47821cfe2091SMatthew G. Knepley   Input Parameters:
47831cfe2091SMatthew G. Knepley + dm - the DM
47841cfe2091SMatthew G. Knepley - cdm - coordinate DM
47851cfe2091SMatthew G. Knepley 
47861cfe2091SMatthew G. Knepley   Level: intermediate
47871cfe2091SMatthew G. Knepley 
47881cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
47891cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
47901cfe2091SMatthew G. Knepley @*/
47911cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
47921cfe2091SMatthew G. Knepley {
47931cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
47941cfe2091SMatthew G. Knepley 
47951cfe2091SMatthew G. Knepley   PetscFunctionBegin;
47961cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47971cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4798f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
47991cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
48001cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
48011cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
48021cfe2091SMatthew G. Knepley }
48031cfe2091SMatthew G. Knepley 
480446e270d4SMatthew G. Knepley /*@
480546e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
480646e270d4SMatthew G. Knepley 
480746e270d4SMatthew G. Knepley   Not Collective
480846e270d4SMatthew G. Knepley 
480946e270d4SMatthew G. Knepley   Input Parameter:
481046e270d4SMatthew G. Knepley . dm - The DM object
481146e270d4SMatthew G. Knepley 
481246e270d4SMatthew G. Knepley   Output Parameter:
481346e270d4SMatthew G. Knepley . dim - The embedding dimension
481446e270d4SMatthew G. Knepley 
481546e270d4SMatthew G. Knepley   Level: intermediate
481646e270d4SMatthew G. Knepley 
481746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4818e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
481946e270d4SMatthew G. Knepley @*/
482046e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
482146e270d4SMatthew G. Knepley {
482246e270d4SMatthew G. Knepley   PetscFunctionBegin;
482346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
482446e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
48259a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
48269a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
48279a9a41abSToby Isaac   }
482846e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
482946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
483046e270d4SMatthew G. Knepley }
483146e270d4SMatthew G. Knepley 
483246e270d4SMatthew G. Knepley /*@
483346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
483446e270d4SMatthew G. Knepley 
483546e270d4SMatthew G. Knepley   Not Collective
483646e270d4SMatthew G. Knepley 
483746e270d4SMatthew G. Knepley   Input Parameters:
483846e270d4SMatthew G. Knepley + dm  - The DM object
483946e270d4SMatthew G. Knepley - dim - The embedding dimension
484046e270d4SMatthew G. Knepley 
484146e270d4SMatthew G. Knepley   Level: intermediate
484246e270d4SMatthew G. Knepley 
484346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4844e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
484546e270d4SMatthew G. Knepley @*/
484646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
484746e270d4SMatthew G. Knepley {
4848f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4849f17e8794SMatthew G. Knepley 
485046e270d4SMatthew G. Knepley   PetscFunctionBegin;
485146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
485246e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4853f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
485446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
485546e270d4SMatthew G. Knepley }
485646e270d4SMatthew G. Knepley 
4857e8abe2deSMatthew G. Knepley /*@
4858e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4859e8abe2deSMatthew G. Knepley 
4860116501dfSVaclav Hapla   Collective on DM
4861e8abe2deSMatthew G. Knepley 
4862e8abe2deSMatthew G. Knepley   Input Parameter:
4863e8abe2deSMatthew G. Knepley . dm - The DM object
4864e8abe2deSMatthew G. Knepley 
4865e8abe2deSMatthew G. Knepley   Output Parameter:
4866e8abe2deSMatthew G. Knepley . section - The PetscSection object
4867e8abe2deSMatthew G. Knepley 
4868e8abe2deSMatthew G. Knepley   Level: intermediate
4869e8abe2deSMatthew G. Knepley 
4870e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4871e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
4872e8abe2deSMatthew G. Knepley @*/
4873e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4874e8abe2deSMatthew G. Knepley {
4875e8abe2deSMatthew G. Knepley   DM             cdm;
4876e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4877e8abe2deSMatthew G. Knepley 
4878e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4879e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4880e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4881e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4882e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
4883e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4884e8abe2deSMatthew G. Knepley }
4885e8abe2deSMatthew G. Knepley 
4886e8abe2deSMatthew G. Knepley /*@
4887e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4888e8abe2deSMatthew G. Knepley 
4889e8abe2deSMatthew G. Knepley   Not Collective
4890e8abe2deSMatthew G. Knepley 
4891e8abe2deSMatthew G. Knepley   Input Parameters:
4892e8abe2deSMatthew G. Knepley + dm      - The DM object
489346e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4894e8abe2deSMatthew G. Knepley - section - The PetscSection object
4895e8abe2deSMatthew G. Knepley 
4896e8abe2deSMatthew G. Knepley   Level: intermediate
4897e8abe2deSMatthew G. Knepley 
4898e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4899e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
4900e8abe2deSMatthew G. Knepley @*/
490146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4902e8abe2deSMatthew G. Knepley {
4903e8abe2deSMatthew G. Knepley   DM             cdm;
4904e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4905e8abe2deSMatthew G. Knepley 
4906e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4907e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
490846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4909e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4910e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
491146e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
49124c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
491346e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
491446e270d4SMatthew G. Knepley 
491546e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
491646e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
491746e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
491846e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
491946e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
492046e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
492146e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
492246e270d4SMatthew G. Knepley     }
49236be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
492446e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
492546e270d4SMatthew G. Knepley   }
4926e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4927e8abe2deSMatthew G. Knepley }
4928e8abe2deSMatthew G. Knepley 
49295dc8c3f7SMatthew G. Knepley /*@C
493090b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
49315dc8c3f7SMatthew G. Knepley 
49325dc8c3f7SMatthew G. Knepley   Input Parameters:
493390b157c4SStefano Zampini . dm      - The DM object
493490b157c4SStefano Zampini 
493590b157c4SStefano Zampini   Output Parameters:
493690b157c4SStefano Zampini + per     - Whether the DM is periodic or not
49375dc8c3f7SMatthew 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
49385dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
49395dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
49405dc8c3f7SMatthew G. Knepley 
49415dc8c3f7SMatthew G. Knepley   Level: developer
49425dc8c3f7SMatthew G. Knepley 
49435dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
49445dc8c3f7SMatthew G. Knepley @*/
494590b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4946c6b900c6SMatthew G. Knepley {
4947c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4948c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
494990b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4950c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4951c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
49525dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4953c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4954c6b900c6SMatthew G. Knepley }
4955c6b900c6SMatthew G. Knepley 
49565dc8c3f7SMatthew G. Knepley /*@C
49575dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
49585dc8c3f7SMatthew G. Knepley 
49595dc8c3f7SMatthew G. Knepley   Input Parameters:
49605dc8c3f7SMatthew G. Knepley + dm      - The DM object
496190b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
49625dc8c3f7SMatthew 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
49635dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
49645dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
49655dc8c3f7SMatthew G. Knepley 
49665dc8c3f7SMatthew G. Knepley   Level: developer
49675dc8c3f7SMatthew G. Knepley 
49685dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
49695dc8c3f7SMatthew G. Knepley @*/
497090b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4971c6b900c6SMatthew G. Knepley {
4972c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4973c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4974c6b900c6SMatthew G. Knepley 
4975c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4976c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
497790b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
497890b157c4SStefano Zampini   if (maxCell) {
497990b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
498090b157c4SStefano Zampini     PetscValidPointer(L,4);
498190b157c4SStefano Zampini     PetscValidPointer(bd,5);
498290b157c4SStefano Zampini   }
49835dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
49845dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
498590b157c4SStefano Zampini   if (maxCell) {
49865dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
49875dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
498890b157c4SStefano Zampini   }
4989072d7d67SStefano Zampini   dm->periodic = per;
4990c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4991c6b900c6SMatthew G. Knepley }
4992c6b900c6SMatthew G. Knepley 
49932e17dfb7SMatthew G. Knepley /*@
49942e17dfb7SMatthew 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.
49952e17dfb7SMatthew G. Knepley 
49962e17dfb7SMatthew G. Knepley   Input Parameters:
49972e17dfb7SMatthew G. Knepley + dm     - The DM
499865da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
499965da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
50002e17dfb7SMatthew G. Knepley 
50012e17dfb7SMatthew G. Knepley   Output Parameter:
50022e17dfb7SMatthew G. Knepley . out - The localized coordinate point
50032e17dfb7SMatthew G. Knepley 
50042e17dfb7SMatthew G. Knepley   Level: developer
50052e17dfb7SMatthew G. Knepley 
50062e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
50072e17dfb7SMatthew G. Knepley @*/
500865da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
50092e17dfb7SMatthew G. Knepley {
50102e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
50112e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
50122e17dfb7SMatthew G. Knepley 
50132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
50142e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
50152e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
50162e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
50172e17dfb7SMatthew G. Knepley   } else {
501865da65dcSMatthew G. Knepley     if (endpoint) {
501965da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
5020da3333bfSMatthew 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)) {
5021da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
502265da65dcSMatthew G. Knepley         } else {
5023da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
502465da65dcSMatthew G. Knepley         }
502565da65dcSMatthew G. Knepley       }
502665da65dcSMatthew G. Knepley     } else {
50272e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
50281118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
50292e17dfb7SMatthew G. Knepley       }
50302e17dfb7SMatthew G. Knepley     }
503165da65dcSMatthew G. Knepley   }
50322e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50332e17dfb7SMatthew G. Knepley }
50342e17dfb7SMatthew G. Knepley 
50352e17dfb7SMatthew G. Knepley /*
50362e17dfb7SMatthew 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.
50372e17dfb7SMatthew G. Knepley 
50382e17dfb7SMatthew G. Knepley   Input Parameters:
50392e17dfb7SMatthew G. Knepley + dm     - The DM
50402e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
50412e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
50422e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
50432e17dfb7SMatthew G. Knepley 
50442e17dfb7SMatthew G. Knepley   Output Parameter:
50452e17dfb7SMatthew G. Knepley . out - The localized coordinate point
50462e17dfb7SMatthew G. Knepley 
50472e17dfb7SMatthew G. Knepley   Level: developer
50482e17dfb7SMatthew G. Knepley 
50492e17dfb7SMatthew 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
50502e17dfb7SMatthew G. Knepley 
50512e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
50522e17dfb7SMatthew G. Knepley */
50532e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
50542e17dfb7SMatthew G. Knepley {
50552e17dfb7SMatthew G. Knepley   PetscInt d;
50562e17dfb7SMatthew G. Knepley 
50572e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
50582e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
50592e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
50602e17dfb7SMatthew G. Knepley   } else {
50612e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5062908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
50632e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
50642e17dfb7SMatthew G. Knepley       } else {
50652e17dfb7SMatthew G. Knepley         out[d] = in[d];
50662e17dfb7SMatthew G. Knepley       }
50672e17dfb7SMatthew G. Knepley     }
50682e17dfb7SMatthew G. Knepley   }
50692e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50702e17dfb7SMatthew G. Knepley }
50712e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
50722e17dfb7SMatthew G. Knepley {
50732e17dfb7SMatthew G. Knepley   PetscInt d;
50742e17dfb7SMatthew G. Knepley 
50752e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
50762e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
50772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
50782e17dfb7SMatthew G. Knepley   } else {
50792e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5080908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
50812e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
50822e17dfb7SMatthew G. Knepley       } else {
50832e17dfb7SMatthew G. Knepley         out[d] = in[d];
50842e17dfb7SMatthew G. Knepley       }
50852e17dfb7SMatthew G. Knepley     }
50862e17dfb7SMatthew G. Knepley   }
50872e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50882e17dfb7SMatthew G. Knepley }
50892e17dfb7SMatthew G. Knepley 
50902e17dfb7SMatthew G. Knepley /*
50912e17dfb7SMatthew 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.
50922e17dfb7SMatthew G. Knepley 
50932e17dfb7SMatthew G. Knepley   Input Parameters:
50942e17dfb7SMatthew G. Knepley + dm     - The DM
50952e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
50962e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
50972e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
50982e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
50992e17dfb7SMatthew G. Knepley 
51002e17dfb7SMatthew G. Knepley   Output Parameter:
51012e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
51022e17dfb7SMatthew G. Knepley 
51032e17dfb7SMatthew G. Knepley   Level: developer
51042e17dfb7SMatthew G. Knepley 
51052e17dfb7SMatthew 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
51062e17dfb7SMatthew G. Knepley 
51072e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
51082e17dfb7SMatthew G. Knepley */
51092e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
51102e17dfb7SMatthew G. Knepley {
51112e17dfb7SMatthew G. Knepley   PetscInt d;
51122e17dfb7SMatthew G. Knepley 
51132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
51142e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
51152e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
51162e17dfb7SMatthew G. Knepley   } else {
51172e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5118908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
51192e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
51202e17dfb7SMatthew G. Knepley       } else {
51212e17dfb7SMatthew G. Knepley         out[d] += in[d];
51222e17dfb7SMatthew G. Knepley       }
51232e17dfb7SMatthew G. Knepley     }
51242e17dfb7SMatthew G. Knepley   }
51252e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
51262e17dfb7SMatthew G. Knepley }
51272e17dfb7SMatthew G. Knepley 
512836447a5eSToby Isaac /*@
51298f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
51308f700142SStefano Zampini 
51318f700142SStefano Zampini   Not collective
513236447a5eSToby Isaac 
513336447a5eSToby Isaac   Input Parameter:
513436447a5eSToby Isaac . dm - The DM
513536447a5eSToby Isaac 
513636447a5eSToby Isaac   Output Parameter:
513736447a5eSToby Isaac   areLocalized - True if localized
513836447a5eSToby Isaac 
513936447a5eSToby Isaac   Level: developer
514036447a5eSToby Isaac 
51418f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
514236447a5eSToby Isaac @*/
51438f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
514436447a5eSToby Isaac {
514536447a5eSToby Isaac   DM             cdm;
514636447a5eSToby Isaac   PetscSection   coordSection;
514746a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
514846a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
514936447a5eSToby Isaac   PetscErrorCode ierr;
515036447a5eSToby Isaac 
515136447a5eSToby Isaac   PetscFunctionBegin;
515236447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
515346a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
51548b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
515546a3a80fSLisandro Dalcin 
515636447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
515736447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
515836447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
515946a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
516046a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
516146a3a80fSLisandro Dalcin 
516246a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
516336447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
516436447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
516546a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
516646a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
516736447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
516846a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
516936447a5eSToby Isaac   }
51708f700142SStefano Zampini   *areLocalized = alreadyLocalized;
517136447a5eSToby Isaac   PetscFunctionReturn(0);
517236447a5eSToby Isaac }
517336447a5eSToby Isaac 
51748f700142SStefano Zampini /*@
51758f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
51768f700142SStefano Zampini 
51778f700142SStefano Zampini   Collective on dm
51788f700142SStefano Zampini 
51798f700142SStefano Zampini   Input Parameter:
51808f700142SStefano Zampini . dm - The DM
51818f700142SStefano Zampini 
51828f700142SStefano Zampini   Output Parameter:
51838f700142SStefano Zampini   areLocalized - True if localized
51848f700142SStefano Zampini 
51858f700142SStefano Zampini   Level: developer
51868f700142SStefano Zampini 
51878f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
51888f700142SStefano Zampini @*/
51898f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
51908f700142SStefano Zampini {
51918f700142SStefano Zampini   PetscBool      localized;
51928f700142SStefano Zampini   PetscErrorCode ierr;
51938f700142SStefano Zampini 
51948f700142SStefano Zampini   PetscFunctionBegin;
51958f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51968f700142SStefano Zampini   PetscValidPointer(areLocalized, 2);
51978f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
51988f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
51998f700142SStefano Zampini   PetscFunctionReturn(0);
52008f700142SStefano Zampini }
520136447a5eSToby Isaac 
52022e17dfb7SMatthew G. Knepley /*@
5203492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
52042e17dfb7SMatthew G. Knepley 
52058f700142SStefano Zampini   Collective on dm
52068f700142SStefano Zampini 
52072e17dfb7SMatthew G. Knepley   Input Parameter:
52082e17dfb7SMatthew G. Knepley . dm - The DM
52092e17dfb7SMatthew G. Knepley 
52102e17dfb7SMatthew G. Knepley   Level: developer
52112e17dfb7SMatthew G. Knepley 
52128f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
52132e17dfb7SMatthew G. Knepley @*/
52142e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
52152e17dfb7SMatthew G. Knepley {
52162e17dfb7SMatthew G. Knepley   DM             cdm;
52172e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
52182e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
52193e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
52203e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
5221e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
52223e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
52233e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
52242e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
52252e17dfb7SMatthew G. Knepley 
52262e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
52272e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
522892c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
5229f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
5230f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
5231f7cbd40bSStefano Zampini 
52322e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
52332e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
52342e17dfb7SMatthew G. Knepley   {
52352e17dfb7SMatthew G. Knepley     PetscBool isplex;
52362e17dfb7SMatthew G. Knepley 
52372e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
52382e17dfb7SMatthew G. Knepley     if (isplex) {
52392e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
52403e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
524169291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
52423e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
52433e922f36SToby Isaac       newStart = vStart;
52443e922f36SToby Isaac       newEnd   = vEnd;
52453e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
52463e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
52473e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
52483e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
52493e922f36SToby Isaac       }
52502e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
52512e17dfb7SMatthew G. Knepley   }
52522e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
525343eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
52542e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
52553e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
5256e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
52573e922f36SToby Isaac 
52582e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
52592e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
52602e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
52612e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
52623e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
52633e922f36SToby Isaac 
526469291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
52653e922f36SToby Isaac   localized = &anchor[bs];
52663e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
52673e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
52683e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
52693e922f36SToby Isaac 
52703e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
52713e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
52723e922f36SToby Isaac       PetscInt     b;
52733e922f36SToby Isaac 
52743e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
52753e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
52763e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
52773e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
52783e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
52793e922f36SToby Isaac         for (b = 0; b < bs; b++) {
52803e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
52813e922f36SToby Isaac         }
52823e922f36SToby Isaac         if (b < bs) break;
52833e922f36SToby Isaac       }
52843e922f36SToby Isaac       if (d < dof/bs) {
52853e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
52863e922f36SToby Isaac           PetscInt cdof;
52873e922f36SToby Isaac 
52883e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
52893e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
52903e922f36SToby Isaac         }
52913e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
52923e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
52933e922f36SToby Isaac       }
52943e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
52953e922f36SToby Isaac     }
52963e922f36SToby Isaac   }
52973e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
52983e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
529969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
53003e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
530169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
53023e922f36SToby Isaac     PetscFunctionReturn(0);
53033e922f36SToby Isaac   }
53042e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
53052e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
53062e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
53072e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
53082e17dfb7SMatthew G. Knepley   }
53092e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
53102e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5311c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
53122e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
53132e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
53142e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
53152e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5316c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
53172e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
53182e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
53192e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
53202e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
53212e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
53222e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
53232e17dfb7SMatthew G. Knepley   }
53243e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
53253e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
53263e922f36SToby Isaac 
53272e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
53282e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
53293e922f36SToby Isaac       PetscInt     b, cdof;
53302e17dfb7SMatthew G. Knepley 
53313e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
53323e922f36SToby Isaac       if (!cdof) continue;
53332e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
53342e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
53352e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
53362e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
53372e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
53382e17dfb7SMatthew G. Knepley     }
53393e922f36SToby Isaac   }
534069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
534169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5342c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
53432e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
53442e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
53452e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
53462e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
53472e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
53482e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
53492e17dfb7SMatthew G. Knepley }
53502e17dfb7SMatthew G. Knepley 
5351e87bb0d3SMatthew G Knepley /*@
53523a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5353e87bb0d3SMatthew G Knepley 
53543a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5355e87bb0d3SMatthew G Knepley 
5356e87bb0d3SMatthew G Knepley   Input Parameters:
5357e87bb0d3SMatthew G Knepley + dm - The DM
53583a93e3b7SToby Isaac . v - The Vec of points
535962a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
53603a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5361e87bb0d3SMatthew G Knepley 
536261e3bb9bSMatthew G Knepley   Output Parameter:
536362a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
536462a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
53653a93e3b7SToby Isaac 
5366e87bb0d3SMatthew G Knepley 
5367e87bb0d3SMatthew G Knepley   Level: developer
536861e3bb9bSMatthew G Knepley 
536962a38674SMatthew G. Knepley   Notes:
53703a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
537162a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
53723a93e3b7SToby Isaac 
53733a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
537462a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
53753a93e3b7SToby Isaac 
53763a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
53773a93e3b7SToby Isaac 
537862a38674SMatthew G. Knepley $    const PetscSFNode *cells;
537962a38674SMatthew G. Knepley $    PetscInt           nFound;
5380a6216909SToby Isaac $    const PetscInt    *found;
538162a38674SMatthew G. Knepley $
5382a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
53833a93e3b7SToby Isaac 
53843a93e3b7SToby 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
53853a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
53863a93e3b7SToby Isaac 
538761e3bb9bSMatthew G Knepley .keywords: point location, mesh
538862a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
538961e3bb9bSMatthew G Knepley @*/
539062a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5391e87bb0d3SMatthew G Knepley {
5392735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5393735aa83eSMatthew G Knepley 
5394e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5395e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5396e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5397e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
53983a93e3b7SToby Isaac   if (*cellSF) {
53993a93e3b7SToby Isaac     PetscMPIInt result;
54003a93e3b7SToby Isaac 
5401e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5402a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
54033a93e3b7SToby 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");
5404e0fc9d1bSMatthew G. Knepley   } else {
54053a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
54063a93e3b7SToby Isaac   }
540747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5408735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
540962a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
541082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
541147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5412e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5413e87bb0d3SMatthew G Knepley }
541414f150ffSMatthew G. Knepley 
5415f4d763aaSMatthew G. Knepley /*@
5416f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5417f4d763aaSMatthew G. Knepley 
54188f700142SStefano Zampini   Collective on dm
54198f700142SStefano Zampini 
5420f4d763aaSMatthew G. Knepley   Input Parameter:
5421f4d763aaSMatthew G. Knepley . dm - The original DM
5422f4d763aaSMatthew G. Knepley 
5423f4d763aaSMatthew G. Knepley   Output Parameter:
5424f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5425f4d763aaSMatthew G. Knepley 
5426f4d763aaSMatthew G. Knepley   Level: intermediate
5427f4d763aaSMatthew G. Knepley 
5428e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
5429f4d763aaSMatthew G. Knepley @*/
543014f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
543114f150ffSMatthew G. Knepley {
5432c26acbdeSMatthew G. Knepley   PetscSection   section;
54332d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
543414f150ffSMatthew G. Knepley   PetscErrorCode ierr;
543514f150ffSMatthew G. Knepley 
543614f150ffSMatthew G. Knepley   PetscFunctionBegin;
543714f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
543814f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5439e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5440c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5441127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
54422d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5443c26acbdeSMatthew G. Knepley     *odm = dm;
5444c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5445c26acbdeSMatthew G. Knepley   }
544614f150ffSMatthew G. Knepley   if (!dm->dmBC) {
54470305aff6SMatthew G. Knepley     PetscDS      ds;
5448c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
544914f150ffSMatthew G. Knepley     PetscSF      sf;
545014f150ffSMatthew G. Knepley 
545114f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
54520305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
54530305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
545414f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5455e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
545614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
545714f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
545815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5459e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
546014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
546114f150ffSMatthew G. Knepley   }
546214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
546314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
546414f150ffSMatthew G. Knepley }
5465f4d763aaSMatthew G. Knepley 
5466f4d763aaSMatthew G. Knepley /*@
5467cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5468f4d763aaSMatthew G. Knepley 
5469f4d763aaSMatthew G. Knepley   Input Parameter:
5470f4d763aaSMatthew G. Knepley . dm - The original DM
5471f4d763aaSMatthew G. Knepley 
5472cdb7a50dSMatthew G. Knepley   Output Parameters:
5473cdb7a50dSMatthew G. Knepley + num - The output sequence number
5474cdb7a50dSMatthew G. Knepley - val - The output sequence value
5475f4d763aaSMatthew G. Knepley 
5476f4d763aaSMatthew G. Knepley   Level: intermediate
5477f4d763aaSMatthew G. Knepley 
5478f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5479f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5480f4d763aaSMatthew G. Knepley 
5481f4d763aaSMatthew G. Knepley .seealso: VecView()
5482f4d763aaSMatthew G. Knepley @*/
5483cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5484f4d763aaSMatthew G. Knepley {
5485f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5486f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5487cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5488cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5489f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5490f4d763aaSMatthew G. Knepley }
5491f4d763aaSMatthew G. Knepley 
5492f4d763aaSMatthew G. Knepley /*@
5493cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5494f4d763aaSMatthew G. Knepley 
5495f4d763aaSMatthew G. Knepley   Input Parameters:
5496f4d763aaSMatthew G. Knepley + dm - The original DM
5497cdb7a50dSMatthew G. Knepley . num - The output sequence number
5498cdb7a50dSMatthew G. Knepley - val - The output sequence value
5499f4d763aaSMatthew G. Knepley 
5500f4d763aaSMatthew G. Knepley   Level: intermediate
5501f4d763aaSMatthew G. Knepley 
5502f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5503f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5504f4d763aaSMatthew G. Knepley 
5505f4d763aaSMatthew G. Knepley .seealso: VecView()
5506f4d763aaSMatthew G. Knepley @*/
5507cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5508f4d763aaSMatthew G. Knepley {
5509f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5510f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5511f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5512cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5513cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5514cdb7a50dSMatthew G. Knepley }
5515cdb7a50dSMatthew G. Knepley 
5516cdb7a50dSMatthew G. Knepley /*@C
5517cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5518cdb7a50dSMatthew G. Knepley 
5519cdb7a50dSMatthew G. Knepley   Input Parameters:
5520cdb7a50dSMatthew G. Knepley + dm   - The original DM
5521cdb7a50dSMatthew G. Knepley . name - The sequence name
5522cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5523cdb7a50dSMatthew G. Knepley 
5524cdb7a50dSMatthew G. Knepley   Output Parameter:
5525cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5526cdb7a50dSMatthew G. Knepley 
5527cdb7a50dSMatthew G. Knepley   Level: intermediate
5528cdb7a50dSMatthew G. Knepley 
5529cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5530cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5531cdb7a50dSMatthew G. Knepley 
5532cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5533cdb7a50dSMatthew G. Knepley @*/
5534cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5535cdb7a50dSMatthew G. Knepley {
5536cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5537cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5538cdb7a50dSMatthew G. Knepley 
5539cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5540cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5541cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5542cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5543cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5544cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5545cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5546cdb7a50dSMatthew G. Knepley     PetscScalar value;
5547cdb7a50dSMatthew G. Knepley 
554839d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
55494aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5550cdb7a50dSMatthew G. Knepley #endif
5551cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5552f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5553f4d763aaSMatthew G. Knepley }
55548e4ac7eaSMatthew G. Knepley 
55558e4ac7eaSMatthew G. Knepley /*@
55568e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
55578e4ac7eaSMatthew G. Knepley 
55588e4ac7eaSMatthew G. Knepley   Not collective
55598e4ac7eaSMatthew G. Knepley 
55608e4ac7eaSMatthew G. Knepley   Input Parameter:
55618e4ac7eaSMatthew G. Knepley . dm - The DM
55628e4ac7eaSMatthew G. Knepley 
55638e4ac7eaSMatthew G. Knepley   Output Parameter:
55648e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
55658e4ac7eaSMatthew G. Knepley 
55668e4ac7eaSMatthew G. Knepley   Level: beginner
55678e4ac7eaSMatthew G. Knepley 
55688e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
55698e4ac7eaSMatthew G. Knepley @*/
55708e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
55718e4ac7eaSMatthew G. Knepley {
55728e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
55738e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55748e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
55758e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
55768e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
55778e4ac7eaSMatthew G. Knepley }
55788e4ac7eaSMatthew G. Knepley 
55798e4ac7eaSMatthew G. Knepley /*@
55805d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
55818e4ac7eaSMatthew G. Knepley 
55828e4ac7eaSMatthew G. Knepley   Collective on dm
55838e4ac7eaSMatthew G. Knepley 
55848e4ac7eaSMatthew G. Knepley   Input Parameters:
55858e4ac7eaSMatthew G. Knepley + dm - The DM
55868e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
55878e4ac7eaSMatthew G. Knepley 
55885d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
55895d3b26e6SMatthew G. Knepley 
55908e4ac7eaSMatthew G. Knepley   Level: beginner
55918e4ac7eaSMatthew G. Knepley 
55925d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
55938e4ac7eaSMatthew G. Knepley @*/
55948e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
55958e4ac7eaSMatthew G. Knepley {
55968e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
55978e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55988833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
55998e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
56008e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
56018e4ac7eaSMatthew G. Knepley }
5602c58f1c22SToby Isaac 
5603c58f1c22SToby Isaac 
5604c58f1c22SToby Isaac /*@C
5605c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5606c58f1c22SToby Isaac 
5607c58f1c22SToby Isaac   Not Collective
5608c58f1c22SToby Isaac 
5609c58f1c22SToby Isaac   Input Parameters:
5610c58f1c22SToby Isaac + dm   - The DM object
5611c58f1c22SToby Isaac - name - The label name
5612c58f1c22SToby Isaac 
5613c58f1c22SToby Isaac   Level: intermediate
5614c58f1c22SToby Isaac 
5615c58f1c22SToby Isaac .keywords: mesh
5616c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5617c58f1c22SToby Isaac @*/
5618c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5619c58f1c22SToby Isaac {
5620c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5621c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5622d67d17b1SMatthew G. Knepley   const char    *lname;
5623c58f1c22SToby Isaac   PetscErrorCode ierr;
5624c58f1c22SToby Isaac 
5625c58f1c22SToby Isaac   PetscFunctionBegin;
5626c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5627c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5628c58f1c22SToby Isaac   while (next) {
5629d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
5630d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
5631c58f1c22SToby Isaac     if (flg) break;
5632c58f1c22SToby Isaac     next = next->next;
5633c58f1c22SToby Isaac   }
5634c58f1c22SToby Isaac   if (!flg) {
5635c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5636c58f1c22SToby Isaac 
5637c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5638d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
5639c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5640c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5641c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5642c58f1c22SToby Isaac   }
5643c58f1c22SToby Isaac   PetscFunctionReturn(0);
5644c58f1c22SToby Isaac }
5645c58f1c22SToby Isaac 
5646c58f1c22SToby Isaac /*@C
5647c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5648c58f1c22SToby Isaac 
5649c58f1c22SToby Isaac   Not Collective
5650c58f1c22SToby Isaac 
5651c58f1c22SToby Isaac   Input Parameters:
5652c58f1c22SToby Isaac + dm   - The DM object
5653c58f1c22SToby Isaac . name - The label name
5654c58f1c22SToby Isaac - point - The mesh point
5655c58f1c22SToby Isaac 
5656c58f1c22SToby Isaac   Output Parameter:
5657c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5658c58f1c22SToby Isaac 
5659c58f1c22SToby Isaac   Level: beginner
5660c58f1c22SToby Isaac 
5661c58f1c22SToby Isaac .keywords: mesh
5662c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5663c58f1c22SToby Isaac @*/
5664c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5665c58f1c22SToby Isaac {
5666c58f1c22SToby Isaac   DMLabel        label;
5667c58f1c22SToby Isaac   PetscErrorCode ierr;
5668c58f1c22SToby Isaac 
5669c58f1c22SToby Isaac   PetscFunctionBegin;
5670c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5671c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5672c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
567313903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5674c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5675c58f1c22SToby Isaac   PetscFunctionReturn(0);
5676c58f1c22SToby Isaac }
5677c58f1c22SToby Isaac 
5678c58f1c22SToby Isaac /*@C
5679c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5680c58f1c22SToby Isaac 
5681c58f1c22SToby Isaac   Not Collective
5682c58f1c22SToby Isaac 
5683c58f1c22SToby Isaac   Input Parameters:
5684c58f1c22SToby Isaac + dm   - The DM object
5685c58f1c22SToby Isaac . name - The label name
5686c58f1c22SToby Isaac . point - The mesh point
5687c58f1c22SToby Isaac - value - The label value for this point
5688c58f1c22SToby Isaac 
5689c58f1c22SToby Isaac   Output Parameter:
5690c58f1c22SToby Isaac 
5691c58f1c22SToby Isaac   Level: beginner
5692c58f1c22SToby Isaac 
5693c58f1c22SToby Isaac .keywords: mesh
5694c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5695c58f1c22SToby Isaac @*/
5696c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5697c58f1c22SToby Isaac {
5698c58f1c22SToby Isaac   DMLabel        label;
5699c58f1c22SToby Isaac   PetscErrorCode ierr;
5700c58f1c22SToby Isaac 
5701c58f1c22SToby Isaac   PetscFunctionBegin;
5702c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5703c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5704c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5705c58f1c22SToby Isaac   if (!label) {
5706c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5707c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5708c58f1c22SToby Isaac   }
5709c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5710c58f1c22SToby Isaac   PetscFunctionReturn(0);
5711c58f1c22SToby Isaac }
5712c58f1c22SToby Isaac 
5713c58f1c22SToby Isaac /*@C
5714c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5715c58f1c22SToby Isaac 
5716c58f1c22SToby Isaac   Not Collective
5717c58f1c22SToby Isaac 
5718c58f1c22SToby Isaac   Input Parameters:
5719c58f1c22SToby Isaac + dm   - The DM object
5720c58f1c22SToby Isaac . name - The label name
5721c58f1c22SToby Isaac . point - The mesh point
5722c58f1c22SToby Isaac - value - The label value for this point
5723c58f1c22SToby Isaac 
5724c58f1c22SToby Isaac   Output Parameter:
5725c58f1c22SToby Isaac 
5726c58f1c22SToby Isaac   Level: beginner
5727c58f1c22SToby Isaac 
5728c58f1c22SToby Isaac .keywords: mesh
5729c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5730c58f1c22SToby Isaac @*/
5731c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5732c58f1c22SToby Isaac {
5733c58f1c22SToby Isaac   DMLabel        label;
5734c58f1c22SToby Isaac   PetscErrorCode ierr;
5735c58f1c22SToby Isaac 
5736c58f1c22SToby Isaac   PetscFunctionBegin;
5737c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5738c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5739c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5740c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5741c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5742c58f1c22SToby Isaac   PetscFunctionReturn(0);
5743c58f1c22SToby Isaac }
5744c58f1c22SToby Isaac 
5745c58f1c22SToby Isaac /*@C
5746c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5747c58f1c22SToby Isaac 
5748c58f1c22SToby Isaac   Not Collective
5749c58f1c22SToby Isaac 
5750c58f1c22SToby Isaac   Input Parameters:
5751c58f1c22SToby Isaac + dm   - The DM object
5752c58f1c22SToby Isaac - name - The label name
5753c58f1c22SToby Isaac 
5754c58f1c22SToby Isaac   Output Parameter:
5755c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5756c58f1c22SToby Isaac 
5757c58f1c22SToby Isaac   Level: beginner
5758c58f1c22SToby Isaac 
5759c58f1c22SToby Isaac .keywords: mesh
5760df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
5761c58f1c22SToby Isaac @*/
5762c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5763c58f1c22SToby Isaac {
5764c58f1c22SToby Isaac   DMLabel        label;
5765c58f1c22SToby Isaac   PetscErrorCode ierr;
5766c58f1c22SToby Isaac 
5767c58f1c22SToby Isaac   PetscFunctionBegin;
5768c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5769c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5770c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5771c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5772c58f1c22SToby Isaac   *size = 0;
5773c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5774c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5775c58f1c22SToby Isaac   PetscFunctionReturn(0);
5776c58f1c22SToby Isaac }
5777c58f1c22SToby Isaac 
5778c58f1c22SToby Isaac /*@C
5779c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5780c58f1c22SToby Isaac 
5781c58f1c22SToby Isaac   Not Collective
5782c58f1c22SToby Isaac 
5783c58f1c22SToby Isaac   Input Parameters:
5784c58f1c22SToby Isaac + mesh - The DM object
5785c58f1c22SToby Isaac - name - The label name
5786c58f1c22SToby Isaac 
5787c58f1c22SToby Isaac   Output Parameter:
5788c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5789c58f1c22SToby Isaac 
5790c58f1c22SToby Isaac   Level: beginner
5791c58f1c22SToby Isaac 
5792c58f1c22SToby Isaac .keywords: mesh
5793c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5794c58f1c22SToby Isaac @*/
5795c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5796c58f1c22SToby Isaac {
5797c58f1c22SToby Isaac   DMLabel        label;
5798c58f1c22SToby Isaac   PetscErrorCode ierr;
5799c58f1c22SToby Isaac 
5800c58f1c22SToby Isaac   PetscFunctionBegin;
5801c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5802c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5803c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5804c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5805c58f1c22SToby Isaac   *ids = NULL;
5806dab2e251SBlaise Bourdin  if (label) {
5807c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5808dab2e251SBlaise Bourdin   } else {
5809dab2e251SBlaise Bourdin     /* returning an empty IS */
5810dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5811dab2e251SBlaise Bourdin   }
5812c58f1c22SToby Isaac   PetscFunctionReturn(0);
5813c58f1c22SToby Isaac }
5814c58f1c22SToby Isaac 
5815c58f1c22SToby Isaac /*@C
5816c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5817c58f1c22SToby Isaac 
5818c58f1c22SToby Isaac   Not Collective
5819c58f1c22SToby Isaac 
5820c58f1c22SToby Isaac   Input Parameters:
5821c58f1c22SToby Isaac + dm - The DM object
5822c58f1c22SToby Isaac . name - The label name
5823c58f1c22SToby Isaac - value - The stratum value
5824c58f1c22SToby Isaac 
5825c58f1c22SToby Isaac   Output Parameter:
5826c58f1c22SToby Isaac . size - The stratum size
5827c58f1c22SToby Isaac 
5828c58f1c22SToby Isaac   Level: beginner
5829c58f1c22SToby Isaac 
5830c58f1c22SToby Isaac .keywords: mesh
5831c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5832c58f1c22SToby Isaac @*/
5833c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5834c58f1c22SToby Isaac {
5835c58f1c22SToby Isaac   DMLabel        label;
5836c58f1c22SToby Isaac   PetscErrorCode ierr;
5837c58f1c22SToby Isaac 
5838c58f1c22SToby Isaac   PetscFunctionBegin;
5839c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5840c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5841c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5842c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5843c58f1c22SToby Isaac   *size = 0;
5844c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5845c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5846c58f1c22SToby Isaac   PetscFunctionReturn(0);
5847c58f1c22SToby Isaac }
5848c58f1c22SToby Isaac 
5849c58f1c22SToby Isaac /*@C
5850c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5851c58f1c22SToby Isaac 
5852c58f1c22SToby Isaac   Not Collective
5853c58f1c22SToby Isaac 
5854c58f1c22SToby Isaac   Input Parameters:
5855c58f1c22SToby Isaac + dm - The DM object
5856c58f1c22SToby Isaac . name - The label name
5857c58f1c22SToby Isaac - value - The stratum value
5858c58f1c22SToby Isaac 
5859c58f1c22SToby Isaac   Output Parameter:
5860c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5861c58f1c22SToby Isaac 
5862c58f1c22SToby Isaac   Level: beginner
5863c58f1c22SToby Isaac 
5864c58f1c22SToby Isaac .keywords: mesh
5865c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5866c58f1c22SToby Isaac @*/
5867c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5868c58f1c22SToby Isaac {
5869c58f1c22SToby Isaac   DMLabel        label;
5870c58f1c22SToby Isaac   PetscErrorCode ierr;
5871c58f1c22SToby Isaac 
5872c58f1c22SToby Isaac   PetscFunctionBegin;
5873c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5874c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5875c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5876c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5877c58f1c22SToby Isaac   *points = NULL;
5878c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5879c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5880c58f1c22SToby Isaac   PetscFunctionReturn(0);
5881c58f1c22SToby Isaac }
5882c58f1c22SToby Isaac 
58834de306b1SToby Isaac /*@C
58849044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
58854de306b1SToby Isaac 
58864de306b1SToby Isaac   Not Collective
58874de306b1SToby Isaac 
58884de306b1SToby Isaac   Input Parameters:
58894de306b1SToby Isaac + dm - The DM object
58904de306b1SToby Isaac . name - The label name
58914de306b1SToby Isaac . value - The stratum value
58924de306b1SToby Isaac - points - The stratum points
58934de306b1SToby Isaac 
58944de306b1SToby Isaac   Level: beginner
58954de306b1SToby Isaac 
58964de306b1SToby Isaac .keywords: mesh
58974de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
58984de306b1SToby Isaac @*/
58994de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
59004de306b1SToby Isaac {
59014de306b1SToby Isaac   DMLabel        label;
59024de306b1SToby Isaac   PetscErrorCode ierr;
59034de306b1SToby Isaac 
59044de306b1SToby Isaac   PetscFunctionBegin;
59054de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59064de306b1SToby Isaac   PetscValidCharPointer(name, 2);
59074de306b1SToby Isaac   PetscValidPointer(points, 4);
59084de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
59094de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
59104de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
59114de306b1SToby Isaac   PetscFunctionReturn(0);
59124de306b1SToby Isaac }
59134de306b1SToby Isaac 
5914c58f1c22SToby Isaac /*@C
5915c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5916c58f1c22SToby Isaac 
5917c58f1c22SToby Isaac   Not Collective
5918c58f1c22SToby Isaac 
5919c58f1c22SToby Isaac   Input Parameters:
5920c58f1c22SToby Isaac + dm   - The DM object
5921c58f1c22SToby Isaac . name - The label name
5922c58f1c22SToby Isaac - value - The label value for this point
5923c58f1c22SToby Isaac 
5924c58f1c22SToby Isaac   Output Parameter:
5925c58f1c22SToby Isaac 
5926c58f1c22SToby Isaac   Level: beginner
5927c58f1c22SToby Isaac 
5928c58f1c22SToby Isaac .keywords: mesh
5929c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5930c58f1c22SToby Isaac @*/
5931c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5932c58f1c22SToby Isaac {
5933c58f1c22SToby Isaac   DMLabel        label;
5934c58f1c22SToby Isaac   PetscErrorCode ierr;
5935c58f1c22SToby Isaac 
5936c58f1c22SToby Isaac   PetscFunctionBegin;
5937c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5938c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5939c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5940c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5941c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5942c58f1c22SToby Isaac   PetscFunctionReturn(0);
5943c58f1c22SToby Isaac }
5944c58f1c22SToby Isaac 
5945c58f1c22SToby Isaac /*@
5946c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5947c58f1c22SToby Isaac 
5948c58f1c22SToby Isaac   Not Collective
5949c58f1c22SToby Isaac 
5950c58f1c22SToby Isaac   Input Parameter:
5951c58f1c22SToby Isaac . dm   - The DM object
5952c58f1c22SToby Isaac 
5953c58f1c22SToby Isaac   Output Parameter:
5954c58f1c22SToby Isaac . numLabels - the number of Labels
5955c58f1c22SToby Isaac 
5956c58f1c22SToby Isaac   Level: intermediate
5957c58f1c22SToby Isaac 
5958c58f1c22SToby Isaac .keywords: mesh
5959c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5960c58f1c22SToby Isaac @*/
5961c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5962c58f1c22SToby Isaac {
5963c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5964c58f1c22SToby Isaac   PetscInt  n    = 0;
5965c58f1c22SToby Isaac 
5966c58f1c22SToby Isaac   PetscFunctionBegin;
5967c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5968c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5969c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5970c58f1c22SToby Isaac   *numLabels = n;
5971c58f1c22SToby Isaac   PetscFunctionReturn(0);
5972c58f1c22SToby Isaac }
5973c58f1c22SToby Isaac 
5974c58f1c22SToby Isaac /*@C
5975c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5976c58f1c22SToby Isaac 
5977c58f1c22SToby Isaac   Not Collective
5978c58f1c22SToby Isaac 
5979c58f1c22SToby Isaac   Input Parameters:
5980c58f1c22SToby Isaac + dm - The DM object
5981c58f1c22SToby Isaac - n  - the label number
5982c58f1c22SToby Isaac 
5983c58f1c22SToby Isaac   Output Parameter:
5984c58f1c22SToby Isaac . name - the label name
5985c58f1c22SToby Isaac 
5986c58f1c22SToby Isaac   Level: intermediate
5987c58f1c22SToby Isaac 
5988c58f1c22SToby Isaac .keywords: mesh
5989c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5990c58f1c22SToby Isaac @*/
5991c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5992c58f1c22SToby Isaac {
5993c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5994c58f1c22SToby Isaac   PetscInt       l    = 0;
5995d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
5996c58f1c22SToby Isaac 
5997c58f1c22SToby Isaac   PetscFunctionBegin;
5998c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5999c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6000c58f1c22SToby Isaac   while (next) {
6001c58f1c22SToby Isaac     if (l == n) {
6002d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6003c58f1c22SToby Isaac       PetscFunctionReturn(0);
6004c58f1c22SToby Isaac     }
6005c58f1c22SToby Isaac     ++l;
6006c58f1c22SToby Isaac     next = next->next;
6007c58f1c22SToby Isaac   }
6008c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6009c58f1c22SToby Isaac }
6010c58f1c22SToby Isaac 
6011c58f1c22SToby Isaac /*@C
6012c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6013c58f1c22SToby Isaac 
6014c58f1c22SToby Isaac   Not Collective
6015c58f1c22SToby Isaac 
6016c58f1c22SToby Isaac   Input Parameters:
6017c58f1c22SToby Isaac + dm   - The DM object
6018c58f1c22SToby Isaac - name - The label name
6019c58f1c22SToby Isaac 
6020c58f1c22SToby Isaac   Output Parameter:
6021c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6022c58f1c22SToby Isaac 
6023c58f1c22SToby Isaac   Level: intermediate
6024c58f1c22SToby Isaac 
6025c58f1c22SToby Isaac .keywords: mesh
6026c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6027c58f1c22SToby Isaac @*/
6028c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6029c58f1c22SToby Isaac {
6030c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6031d67d17b1SMatthew G. Knepley   const char    *lname;
6032c58f1c22SToby Isaac   PetscErrorCode ierr;
6033c58f1c22SToby Isaac 
6034c58f1c22SToby Isaac   PetscFunctionBegin;
6035c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6036c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6037c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
6038c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6039c58f1c22SToby Isaac   while (next) {
6040d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6041d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
6042c58f1c22SToby Isaac     if (*hasLabel) break;
6043c58f1c22SToby Isaac     next = next->next;
6044c58f1c22SToby Isaac   }
6045c58f1c22SToby Isaac   PetscFunctionReturn(0);
6046c58f1c22SToby Isaac }
6047c58f1c22SToby Isaac 
6048c58f1c22SToby Isaac /*@C
6049c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6050c58f1c22SToby Isaac 
6051c58f1c22SToby Isaac   Not Collective
6052c58f1c22SToby Isaac 
6053c58f1c22SToby Isaac   Input Parameters:
6054c58f1c22SToby Isaac + dm   - The DM object
6055c58f1c22SToby Isaac - name - The label name
6056c58f1c22SToby Isaac 
6057c58f1c22SToby Isaac   Output Parameter:
6058c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6059c58f1c22SToby Isaac 
6060c58f1c22SToby Isaac   Level: intermediate
6061c58f1c22SToby Isaac 
6062c58f1c22SToby Isaac .keywords: mesh
6063c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6064c58f1c22SToby Isaac @*/
6065c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6066c58f1c22SToby Isaac {
6067c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6068c58f1c22SToby Isaac   PetscBool      hasLabel;
6069d67d17b1SMatthew G. Knepley   const char    *lname;
6070c58f1c22SToby Isaac   PetscErrorCode ierr;
6071c58f1c22SToby Isaac 
6072c58f1c22SToby Isaac   PetscFunctionBegin;
6073c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6074c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6075c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6076c58f1c22SToby Isaac   *label = NULL;
6077c58f1c22SToby Isaac   while (next) {
6078d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6079d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6080c58f1c22SToby Isaac     if (hasLabel) {
6081c58f1c22SToby Isaac       *label = next->label;
6082c58f1c22SToby Isaac       break;
6083c58f1c22SToby Isaac     }
6084c58f1c22SToby Isaac     next = next->next;
6085c58f1c22SToby Isaac   }
6086c58f1c22SToby Isaac   PetscFunctionReturn(0);
6087c58f1c22SToby Isaac }
6088c58f1c22SToby Isaac 
6089c58f1c22SToby Isaac /*@C
6090c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6091c58f1c22SToby Isaac 
6092c58f1c22SToby Isaac   Not Collective
6093c58f1c22SToby Isaac 
6094c58f1c22SToby Isaac   Input Parameters:
6095c58f1c22SToby Isaac + dm - The DM object
6096c58f1c22SToby Isaac - n  - the label number
6097c58f1c22SToby Isaac 
6098c58f1c22SToby Isaac   Output Parameter:
6099c58f1c22SToby Isaac . label - the label
6100c58f1c22SToby Isaac 
6101c58f1c22SToby Isaac   Level: intermediate
6102c58f1c22SToby Isaac 
6103c58f1c22SToby Isaac .keywords: mesh
6104c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6105c58f1c22SToby Isaac @*/
6106c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6107c58f1c22SToby Isaac {
6108c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6109c58f1c22SToby Isaac   PetscInt    l    = 0;
6110c58f1c22SToby Isaac 
6111c58f1c22SToby Isaac   PetscFunctionBegin;
6112c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6113c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6114c58f1c22SToby Isaac   while (next) {
6115c58f1c22SToby Isaac     if (l == n) {
6116c58f1c22SToby Isaac       *label = next->label;
6117c58f1c22SToby Isaac       PetscFunctionReturn(0);
6118c58f1c22SToby Isaac     }
6119c58f1c22SToby Isaac     ++l;
6120c58f1c22SToby Isaac     next = next->next;
6121c58f1c22SToby Isaac   }
6122c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6123c58f1c22SToby Isaac }
6124c58f1c22SToby Isaac 
6125c58f1c22SToby Isaac /*@C
6126c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
6127c58f1c22SToby Isaac 
6128c58f1c22SToby Isaac   Not Collective
6129c58f1c22SToby Isaac 
6130c58f1c22SToby Isaac   Input Parameters:
6131c58f1c22SToby Isaac + dm   - The DM object
6132c58f1c22SToby Isaac - label - The DMLabel
6133c58f1c22SToby Isaac 
6134c58f1c22SToby Isaac   Level: developer
6135c58f1c22SToby Isaac 
6136c58f1c22SToby Isaac .keywords: mesh
6137c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6138c58f1c22SToby Isaac @*/
6139c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6140c58f1c22SToby Isaac {
6141c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
6142c58f1c22SToby Isaac   PetscBool      hasLabel;
6143d67d17b1SMatthew G. Knepley   const char    *lname;
6144c58f1c22SToby Isaac   PetscErrorCode ierr;
6145c58f1c22SToby Isaac 
6146c58f1c22SToby Isaac   PetscFunctionBegin;
6147c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6148d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
6149d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
6150d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
6151c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6152c58f1c22SToby Isaac   tmpLabel->label  = label;
6153c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
6154c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
6155c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
6156c58f1c22SToby Isaac   PetscFunctionReturn(0);
6157c58f1c22SToby Isaac }
6158c58f1c22SToby Isaac 
6159c58f1c22SToby Isaac /*@C
6160c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
6161c58f1c22SToby Isaac 
6162c58f1c22SToby Isaac   Not Collective
6163c58f1c22SToby Isaac 
6164c58f1c22SToby Isaac   Input Parameters:
6165c58f1c22SToby Isaac + dm   - The DM object
6166c58f1c22SToby Isaac - name - The label name
6167c58f1c22SToby Isaac 
6168c58f1c22SToby Isaac   Output Parameter:
6169c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6170c58f1c22SToby Isaac 
6171c58f1c22SToby Isaac   Level: developer
6172c58f1c22SToby Isaac 
6173c58f1c22SToby Isaac .keywords: mesh
6174c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6175c58f1c22SToby Isaac @*/
6176c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
6177c58f1c22SToby Isaac {
6178c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6179c58f1c22SToby Isaac   DMLabelLink    last = NULL;
6180c58f1c22SToby Isaac   PetscBool      hasLabel;
6181d67d17b1SMatthew G. Knepley   const char    *lname;
6182c58f1c22SToby Isaac   PetscErrorCode ierr;
6183c58f1c22SToby Isaac 
6184c58f1c22SToby Isaac   PetscFunctionBegin;
6185c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6186c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
6187c58f1c22SToby Isaac   *label = NULL;
6188c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
6189c58f1c22SToby Isaac   while (next) {
6190d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6191d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6192c58f1c22SToby Isaac     if (hasLabel) {
6193c58f1c22SToby Isaac       if (last) last->next       = next->next;
6194c58f1c22SToby Isaac       else      dm->labels->next = next->next;
6195c58f1c22SToby Isaac       next->next = NULL;
6196c58f1c22SToby Isaac       *label     = next->label;
6197c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
6198c58f1c22SToby Isaac       if (hasLabel) {
6199c58f1c22SToby Isaac         dm->depthLabel = NULL;
6200c58f1c22SToby Isaac       }
6201c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
6202c58f1c22SToby Isaac       break;
6203c58f1c22SToby Isaac     }
6204c58f1c22SToby Isaac     last = next;
6205c58f1c22SToby Isaac     next = next->next;
6206c58f1c22SToby Isaac   }
6207c58f1c22SToby Isaac   PetscFunctionReturn(0);
6208c58f1c22SToby Isaac }
6209c58f1c22SToby Isaac 
6210c58f1c22SToby Isaac /*@C
6211c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6212c58f1c22SToby Isaac 
6213c58f1c22SToby Isaac   Not Collective
6214c58f1c22SToby Isaac 
6215c58f1c22SToby Isaac   Input Parameters:
6216c58f1c22SToby Isaac + dm   - The DM object
6217c58f1c22SToby Isaac - name - The label name
6218c58f1c22SToby Isaac 
6219c58f1c22SToby Isaac   Output Parameter:
6220c58f1c22SToby Isaac . output - The flag for output
6221c58f1c22SToby Isaac 
6222c58f1c22SToby Isaac   Level: developer
6223c58f1c22SToby Isaac 
6224c58f1c22SToby Isaac .keywords: mesh
6225c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6226c58f1c22SToby Isaac @*/
6227c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6228c58f1c22SToby Isaac {
6229c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6230d67d17b1SMatthew G. Knepley   const char    *lname;
6231c58f1c22SToby Isaac   PetscErrorCode ierr;
6232c58f1c22SToby Isaac 
6233c58f1c22SToby Isaac   PetscFunctionBegin;
6234c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6235c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6236c58f1c22SToby Isaac   PetscValidPointer(output, 3);
6237c58f1c22SToby Isaac   while (next) {
6238c58f1c22SToby Isaac     PetscBool flg;
6239c58f1c22SToby Isaac 
6240d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6241d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6242c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6243c58f1c22SToby Isaac     next = next->next;
6244c58f1c22SToby Isaac   }
6245c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6246c58f1c22SToby Isaac }
6247c58f1c22SToby Isaac 
6248c58f1c22SToby Isaac /*@C
6249c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
6250c58f1c22SToby Isaac 
6251c58f1c22SToby Isaac   Not Collective
6252c58f1c22SToby Isaac 
6253c58f1c22SToby Isaac   Input Parameters:
6254c58f1c22SToby Isaac + dm     - The DM object
6255c58f1c22SToby Isaac . name   - The label name
6256c58f1c22SToby Isaac - output - The flag for output
6257c58f1c22SToby Isaac 
6258c58f1c22SToby Isaac   Level: developer
6259c58f1c22SToby Isaac 
6260c58f1c22SToby Isaac .keywords: mesh
6261c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6262c58f1c22SToby Isaac @*/
6263c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
6264c58f1c22SToby Isaac {
6265c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6266d67d17b1SMatthew G. Knepley   const char    *lname;
6267c58f1c22SToby Isaac   PetscErrorCode ierr;
6268c58f1c22SToby Isaac 
6269c58f1c22SToby Isaac   PetscFunctionBegin;
6270c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6271c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6272c58f1c22SToby Isaac   while (next) {
6273c58f1c22SToby Isaac     PetscBool flg;
6274c58f1c22SToby Isaac 
6275d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6276d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6277c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
6278c58f1c22SToby Isaac     next = next->next;
6279c58f1c22SToby Isaac   }
6280c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6281c58f1c22SToby Isaac }
6282c58f1c22SToby Isaac 
6283c58f1c22SToby Isaac 
6284c58f1c22SToby Isaac /*@
6285c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
6286c58f1c22SToby Isaac 
6287c58f1c22SToby Isaac   Collective on DM
6288c58f1c22SToby Isaac 
6289c58f1c22SToby Isaac   Input Parameter:
62902e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
6291c58f1c22SToby Isaac 
6292c58f1c22SToby Isaac   Output Parameter:
62932e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
6294c58f1c22SToby Isaac 
6295c58f1c22SToby Isaac   Level: intermediate
6296c58f1c22SToby Isaac 
6297c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
6298c58f1c22SToby Isaac 
6299c58f1c22SToby Isaac .keywords: mesh
6300367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
6301c58f1c22SToby Isaac @*/
6302c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
6303c58f1c22SToby Isaac {
6304c58f1c22SToby Isaac   PetscInt       numLabels, l;
6305c58f1c22SToby Isaac   PetscErrorCode ierr;
6306c58f1c22SToby Isaac 
6307c58f1c22SToby Isaac   PetscFunctionBegin;
6308c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
6309c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
6310c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
6311c58f1c22SToby Isaac     DMLabel     label, labelNew;
6312c58f1c22SToby Isaac     const char *name;
6313c58f1c22SToby Isaac     PetscBool   flg;
6314c58f1c22SToby Isaac 
6315c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
6316c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
6317c58f1c22SToby Isaac     if (flg) continue;
63187d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
63197d5acc75SStefano Zampini     if (flg) continue;
6320c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
6321c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
6322c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
6323c58f1c22SToby Isaac   }
6324c58f1c22SToby Isaac   PetscFunctionReturn(0);
6325c58f1c22SToby Isaac }
6326a8fb8f29SToby Isaac 
6327a8fb8f29SToby Isaac /*@
6328a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
6329a8fb8f29SToby Isaac 
6330a8fb8f29SToby Isaac   Input Parameter:
6331a8fb8f29SToby Isaac . dm - The DM object
6332a8fb8f29SToby Isaac 
6333a8fb8f29SToby Isaac   Output Parameter:
6334a8fb8f29SToby Isaac . cdm - The coarse DM
6335a8fb8f29SToby Isaac 
6336a8fb8f29SToby Isaac   Level: intermediate
6337a8fb8f29SToby Isaac 
6338a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
6339a8fb8f29SToby Isaac @*/
6340a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6341a8fb8f29SToby Isaac {
6342a8fb8f29SToby Isaac   PetscFunctionBegin;
6343a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6344a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
6345a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
6346a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6347a8fb8f29SToby Isaac }
6348a8fb8f29SToby Isaac 
6349a8fb8f29SToby Isaac /*@
6350a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6351a8fb8f29SToby Isaac 
6352a8fb8f29SToby Isaac   Input Parameters:
6353a8fb8f29SToby Isaac + dm - The DM object
6354a8fb8f29SToby Isaac - cdm - The coarse DM
6355a8fb8f29SToby Isaac 
6356a8fb8f29SToby Isaac   Level: intermediate
6357a8fb8f29SToby Isaac 
6358a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
6359a8fb8f29SToby Isaac @*/
6360a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6361a8fb8f29SToby Isaac {
6362a8fb8f29SToby Isaac   PetscErrorCode ierr;
6363a8fb8f29SToby Isaac 
6364a8fb8f29SToby Isaac   PetscFunctionBegin;
6365a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6366a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6367a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6368a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6369a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6370a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6371a8fb8f29SToby Isaac }
6372a8fb8f29SToby Isaac 
637388bdff64SToby Isaac /*@
637488bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
637588bdff64SToby Isaac 
637688bdff64SToby Isaac   Input Parameter:
637788bdff64SToby Isaac . dm - The DM object
637888bdff64SToby Isaac 
637988bdff64SToby Isaac   Output Parameter:
638088bdff64SToby Isaac . fdm - The fine DM
638188bdff64SToby Isaac 
638288bdff64SToby Isaac   Level: intermediate
638388bdff64SToby Isaac 
638488bdff64SToby Isaac .seealso: DMSetFineDM()
638588bdff64SToby Isaac @*/
638688bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
638788bdff64SToby Isaac {
638888bdff64SToby Isaac   PetscFunctionBegin;
638988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
639088bdff64SToby Isaac   PetscValidPointer(fdm, 2);
639188bdff64SToby Isaac   *fdm = dm->fineMesh;
639288bdff64SToby Isaac   PetscFunctionReturn(0);
639388bdff64SToby Isaac }
639488bdff64SToby Isaac 
639588bdff64SToby Isaac /*@
639688bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
639788bdff64SToby Isaac 
639888bdff64SToby Isaac   Input Parameters:
639988bdff64SToby Isaac + dm - The DM object
640088bdff64SToby Isaac - fdm - The fine DM
640188bdff64SToby Isaac 
640288bdff64SToby Isaac   Level: intermediate
640388bdff64SToby Isaac 
640488bdff64SToby Isaac .seealso: DMGetFineDM()
640588bdff64SToby Isaac @*/
640688bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
640788bdff64SToby Isaac {
640888bdff64SToby Isaac   PetscErrorCode ierr;
640988bdff64SToby Isaac 
641088bdff64SToby Isaac   PetscFunctionBegin;
641188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
641288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
641388bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
641488bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
641588bdff64SToby Isaac   dm->fineMesh = fdm;
641688bdff64SToby Isaac   PetscFunctionReturn(0);
641788bdff64SToby Isaac }
641888bdff64SToby Isaac 
6419a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6420a6ba4734SToby Isaac 
6421a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6422a6ba4734SToby Isaac {
6423a6ba4734SToby Isaac   PetscErrorCode ierr;
6424a6ba4734SToby Isaac 
6425a6ba4734SToby Isaac   PetscFunctionBegin;
6426e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6427a6ba4734SToby Isaac   PetscFunctionReturn(0);
6428a6ba4734SToby Isaac }
6429a6ba4734SToby Isaac 
6430a6ba4734SToby Isaac /*@C
6431a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6432a6ba4734SToby Isaac 
6433a6ba4734SToby Isaac   Input Parameters:
64344c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6435f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6436a6ba4734SToby Isaac . name        - The BC name
6437a6ba4734SToby Isaac . labelname   - The label defining constrained points
6438a6ba4734SToby Isaac . field       - The field to constrain
6439e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
6440a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6441a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6442a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6443a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6444a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6445a6ba4734SToby Isaac 
6446a6ba4734SToby Isaac   Options Database Keys:
6447a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6448a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6449a6ba4734SToby Isaac 
6450a6ba4734SToby Isaac   Level: developer
6451a6ba4734SToby Isaac 
6452a6ba4734SToby Isaac .seealso: DMGetBoundary()
6453a6ba4734SToby Isaac @*/
6454a30ec4eaSSatish 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)
6455a6ba4734SToby Isaac {
6456a6ba4734SToby Isaac   PetscErrorCode ierr;
6457a6ba4734SToby Isaac 
6458a6ba4734SToby Isaac   PetscFunctionBegin;
6459a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6460f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6461a6ba4734SToby Isaac   PetscFunctionReturn(0);
6462a6ba4734SToby Isaac }
6463a6ba4734SToby Isaac 
6464a6ba4734SToby Isaac /*@
6465a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6466a6ba4734SToby Isaac 
6467a6ba4734SToby Isaac   Input Parameters:
6468a6ba4734SToby Isaac . dm - The mesh object
6469a6ba4734SToby Isaac 
6470a6ba4734SToby Isaac   Output Parameters:
6471a6ba4734SToby Isaac . numBd - The number of BC
6472a6ba4734SToby Isaac 
6473a6ba4734SToby Isaac   Level: intermediate
6474a6ba4734SToby Isaac 
6475a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6476a6ba4734SToby Isaac @*/
6477a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6478a6ba4734SToby Isaac {
647958ebd649SToby Isaac   PetscErrorCode ierr;
6480a6ba4734SToby Isaac 
6481a6ba4734SToby Isaac   PetscFunctionBegin;
6482a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
648358ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6484a6ba4734SToby Isaac   PetscFunctionReturn(0);
6485a6ba4734SToby Isaac }
6486a6ba4734SToby Isaac 
6487a6ba4734SToby Isaac /*@C
64881c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6489a6ba4734SToby Isaac 
6490a6ba4734SToby Isaac   Input Parameters:
6491a6ba4734SToby Isaac + dm          - The mesh object
6492a6ba4734SToby Isaac - bd          - The BC number
6493a6ba4734SToby Isaac 
6494a6ba4734SToby Isaac   Output Parameters:
6495f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6496a6ba4734SToby Isaac . name        - The BC name
6497a6ba4734SToby Isaac . labelname   - The label defining constrained points
6498a6ba4734SToby Isaac . field       - The field to constrain
6499a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6500a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6501a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6502a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6503a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6504a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6505a6ba4734SToby Isaac 
6506a6ba4734SToby Isaac   Options Database Keys:
6507a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6508a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6509a6ba4734SToby Isaac 
6510a6ba4734SToby Isaac   Level: developer
6511a6ba4734SToby Isaac 
6512a6ba4734SToby Isaac .seealso: DMAddBoundary()
6513a6ba4734SToby Isaac @*/
6514a30ec4eaSSatish 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)
6515a6ba4734SToby Isaac {
651658ebd649SToby Isaac   PetscErrorCode ierr;
6517a6ba4734SToby Isaac 
6518a6ba4734SToby Isaac   PetscFunctionBegin;
6519a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6520f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6521a6ba4734SToby Isaac   PetscFunctionReturn(0);
6522a6ba4734SToby Isaac }
6523a6ba4734SToby Isaac 
6524e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6525e6f8dbb6SToby Isaac {
6526dff059c6SToby Isaac   DMBoundary *lastnext;
6527e6f8dbb6SToby Isaac   DSBoundary dsbound;
6528e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6529e6f8dbb6SToby Isaac 
6530e6f8dbb6SToby Isaac   PetscFunctionBegin;
6531e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
653247a1f5adSToby Isaac   if (dm->boundary) {
653347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
653447a1f5adSToby Isaac 
653547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
653647a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
653747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
653847a1f5adSToby Isaac     while (next) {
653947a1f5adSToby Isaac       DMBoundary b = next;
654047a1f5adSToby Isaac 
654147a1f5adSToby Isaac       next = b->next;
654247a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6543a6ba4734SToby Isaac     }
654447a1f5adSToby Isaac     dm->boundary = NULL;
6545a6ba4734SToby Isaac   }
654647a1f5adSToby Isaac 
6547dff059c6SToby Isaac   lastnext = &(dm->boundary);
6548e6f8dbb6SToby Isaac   while (dsbound) {
6549e6f8dbb6SToby Isaac     DMBoundary dmbound;
6550e6f8dbb6SToby Isaac 
6551e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6552e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6553e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6554994fe344SLisandro 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);}
655547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6556dff059c6SToby Isaac     *lastnext = dmbound;
6557dff059c6SToby Isaac     lastnext = &(dmbound->next);
6558dff059c6SToby Isaac     dsbound = dsbound->next;
6559a6ba4734SToby Isaac   }
6560a6ba4734SToby Isaac   PetscFunctionReturn(0);
6561a6ba4734SToby Isaac }
6562a6ba4734SToby Isaac 
6563a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6564a6ba4734SToby Isaac {
6565b95f2879SToby Isaac   DMBoundary     b;
6566a6ba4734SToby Isaac   PetscErrorCode ierr;
6567a6ba4734SToby Isaac 
6568a6ba4734SToby Isaac   PetscFunctionBegin;
6569a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6570a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6571a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6572e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6573b95f2879SToby Isaac   b = dm->boundary;
6574a6ba4734SToby Isaac   while (b && !(*isBd)) {
6575e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6576e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
65773424c85cSToby Isaac 
65783424c85cSToby Isaac     if (label) {
6579a6ba4734SToby Isaac       PetscInt i;
6580a6ba4734SToby Isaac 
6581e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6582e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6583a6ba4734SToby Isaac       }
6584a6ba4734SToby Isaac     }
6585a6ba4734SToby Isaac     b = b->next;
6586a6ba4734SToby Isaac   }
6587a6ba4734SToby Isaac   PetscFunctionReturn(0);
6588a6ba4734SToby Isaac }
65894d6f44ffSToby Isaac 
65904d6f44ffSToby Isaac /*@C
65914d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
65924d6f44ffSToby Isaac 
65934d6f44ffSToby Isaac   Input Parameters:
65944d6f44ffSToby Isaac + dm      - The DM
65950709b2feSToby Isaac . time    - The time
65964d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
65974d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
65984d6f44ffSToby Isaac - mode    - The insertion mode for values
65994d6f44ffSToby Isaac 
66004d6f44ffSToby Isaac   Output Parameter:
66014d6f44ffSToby Isaac . X - vector
66024d6f44ffSToby Isaac 
66034d6f44ffSToby Isaac    Calling sequence of func:
66040709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
66054d6f44ffSToby Isaac 
66064d6f44ffSToby Isaac +  dim - The spatial dimension
66074d6f44ffSToby Isaac .  x   - The coordinates
66084d6f44ffSToby Isaac .  Nf  - The number of fields
66094d6f44ffSToby Isaac .  u   - The output field values
66104d6f44ffSToby Isaac -  ctx - optional user-defined function context
66114d6f44ffSToby Isaac 
66124d6f44ffSToby Isaac   Level: developer
66134d6f44ffSToby Isaac 
66142716604bSToby Isaac .seealso: DMComputeL2Diff()
66154d6f44ffSToby Isaac @*/
66160709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
66174d6f44ffSToby Isaac {
66184d6f44ffSToby Isaac   Vec            localX;
66194d6f44ffSToby Isaac   PetscErrorCode ierr;
66204d6f44ffSToby Isaac 
66214d6f44ffSToby Isaac   PetscFunctionBegin;
66224d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66234d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
66240709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
66254d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
66264d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
66274d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
66284d6f44ffSToby Isaac   PetscFunctionReturn(0);
66294d6f44ffSToby Isaac }
66304d6f44ffSToby Isaac 
66310709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
66324d6f44ffSToby Isaac {
66334d6f44ffSToby Isaac   PetscErrorCode ierr;
66344d6f44ffSToby Isaac 
66354d6f44ffSToby Isaac   PetscFunctionBegin;
66364d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66374d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
66380918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
66390709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
66404d6f44ffSToby Isaac   PetscFunctionReturn(0);
66414d6f44ffSToby Isaac }
66424d6f44ffSToby Isaac 
66432c53366bSMatthew 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)
66442c53366bSMatthew G. Knepley {
66452c53366bSMatthew G. Knepley   Vec            localX;
66462c53366bSMatthew G. Knepley   PetscErrorCode ierr;
66472c53366bSMatthew G. Knepley 
66482c53366bSMatthew G. Knepley   PetscFunctionBegin;
66492c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66502c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
66512c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
66522c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
66532c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
66542c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
66552c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
66562c53366bSMatthew G. Knepley }
66572c53366bSMatthew G. Knepley 
66581c531cf8SMatthew 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)
66594d6f44ffSToby Isaac {
66604d6f44ffSToby Isaac   PetscErrorCode ierr;
66614d6f44ffSToby Isaac 
66624d6f44ffSToby Isaac   PetscFunctionBegin;
66634d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66644d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
66650918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
66661c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
66674d6f44ffSToby Isaac   PetscFunctionReturn(0);
66684d6f44ffSToby Isaac }
66692716604bSToby Isaac 
66708c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
66718c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
66728c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
66738c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6674191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
66758c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
66768c6c5593SMatthew G. Knepley {
66778c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
66788c6c5593SMatthew G. Knepley 
66798c6c5593SMatthew G. Knepley   PetscFunctionBegin;
66808c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66818c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
66828c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
66830918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
66848c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
66858c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
66868c6c5593SMatthew G. Knepley }
66878c6c5593SMatthew G. Knepley 
66881c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
66898c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
66908c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
66918c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6692191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
66938c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
66948c6c5593SMatthew G. Knepley {
66958c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
66968c6c5593SMatthew G. Knepley 
66978c6c5593SMatthew G. Knepley   PetscFunctionBegin;
66988c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66998c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
67008c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
67010918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
67021c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
67038c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
67048c6c5593SMatthew G. Knepley }
67058c6c5593SMatthew G. Knepley 
67062716604bSToby Isaac /*@C
67072716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
67082716604bSToby Isaac 
67092716604bSToby Isaac   Input Parameters:
67102716604bSToby Isaac + dm    - The DM
67110709b2feSToby Isaac . time  - The time
67122716604bSToby Isaac . funcs - The functions to evaluate for each field component
67132716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6714574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
67152716604bSToby Isaac 
67162716604bSToby Isaac   Output Parameter:
67172716604bSToby Isaac . diff - The diff ||u - u_h||_2
67182716604bSToby Isaac 
67192716604bSToby Isaac   Level: developer
67202716604bSToby Isaac 
67211189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
67222716604bSToby Isaac @*/
67230709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
67242716604bSToby Isaac {
67252716604bSToby Isaac   PetscErrorCode ierr;
67262716604bSToby Isaac 
67272716604bSToby Isaac   PetscFunctionBegin;
67282716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6729b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
67300918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
67310709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
67322716604bSToby Isaac   PetscFunctionReturn(0);
67332716604bSToby Isaac }
6734b698f381SToby Isaac 
6735b698f381SToby Isaac /*@C
6736b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6737b698f381SToby Isaac 
6738b698f381SToby Isaac   Input Parameters:
6739b698f381SToby Isaac + dm    - The DM
6740b698f381SToby Isaac , time  - The time
6741b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6742b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6743574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6744b698f381SToby Isaac - n     - The vector to project along
6745b698f381SToby Isaac 
6746b698f381SToby Isaac   Output Parameter:
6747b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6748b698f381SToby Isaac 
6749b698f381SToby Isaac   Level: developer
6750b698f381SToby Isaac 
6751b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6752b698f381SToby Isaac @*/
6753b698f381SToby 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)
6754b698f381SToby Isaac {
6755b698f381SToby Isaac   PetscErrorCode ierr;
6756b698f381SToby Isaac 
6757b698f381SToby Isaac   PetscFunctionBegin;
6758b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6759b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6760b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6761b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6762b698f381SToby Isaac   PetscFunctionReturn(0);
6763b698f381SToby Isaac }
6764b698f381SToby Isaac 
67652a16baeaSToby Isaac /*@C
67662a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
67672a16baeaSToby Isaac 
67682a16baeaSToby Isaac   Input Parameters:
67692a16baeaSToby Isaac + dm    - The DM
67702a16baeaSToby Isaac . time  - The time
67712a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
67722a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6773574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
67742a16baeaSToby Isaac 
67752a16baeaSToby Isaac   Output Parameter:
67762a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
67772a16baeaSToby Isaac 
67782a16baeaSToby Isaac   Level: developer
67792a16baeaSToby Isaac 
67801189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
67812a16baeaSToby Isaac @*/
67821189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
67832a16baeaSToby Isaac {
67842a16baeaSToby Isaac   PetscErrorCode ierr;
67852a16baeaSToby Isaac 
67862a16baeaSToby Isaac   PetscFunctionBegin;
67872a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67882a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
67890918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
67902a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
67912a16baeaSToby Isaac   PetscFunctionReturn(0);
67922a16baeaSToby Isaac }
67932a16baeaSToby Isaac 
6794df0b854cSToby Isaac /*@C
6795df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6796cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6797df0b854cSToby Isaac 
6798df0b854cSToby Isaac   Collective on dm
6799df0b854cSToby Isaac 
6800df0b854cSToby Isaac   Input parameters:
6801df0b854cSToby Isaac + dm - the pre-adaptation DM object
6802a1b0c543SToby Isaac - label - label with the flags
6803df0b854cSToby Isaac 
6804df0b854cSToby Isaac   Output parameters:
68050d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6806df0b854cSToby Isaac 
6807df0b854cSToby Isaac   Level: intermediate
68080d1cd5e0SMatthew G. Knepley 
68090d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6810df0b854cSToby Isaac @*/
68110d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6812df0b854cSToby Isaac {
6813df0b854cSToby Isaac   PetscErrorCode ierr;
6814df0b854cSToby Isaac 
6815df0b854cSToby Isaac   PetscFunctionBegin;
6816df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6817a1b0c543SToby Isaac   PetscValidPointer(label,2);
68180d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
68190d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
68206f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
68210d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
68220d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
68230d1cd5e0SMatthew G. Knepley }
68240d1cd5e0SMatthew G. Knepley 
68250d1cd5e0SMatthew G. Knepley /*@C
68260d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
68270d1cd5e0SMatthew G. Knepley 
68280d1cd5e0SMatthew G. Knepley   Input Parameters:
68290d1cd5e0SMatthew G. Knepley + dm - The DM object
68300d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
68316f25b0d8SLisandro 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_".
68320d1cd5e0SMatthew G. Knepley 
68330d1cd5e0SMatthew G. Knepley   Output Parameter:
68340d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
68350d1cd5e0SMatthew G. Knepley 
68360d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
68370d1cd5e0SMatthew G. Knepley 
68380d1cd5e0SMatthew G. Knepley   Level: advanced
68390d1cd5e0SMatthew G. Knepley 
68400d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
68410d1cd5e0SMatthew G. Knepley @*/
68420d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
68430d1cd5e0SMatthew G. Knepley {
68440d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
68450d1cd5e0SMatthew G. Knepley 
68460d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
68470d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68480d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
68490d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
68500d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
68516f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
68526f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
68530d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6854df0b854cSToby Isaac   PetscFunctionReturn(0);
6855df0b854cSToby Isaac }
6856c4088d22SMatthew G. Knepley 
6857502a2867SDave May /*@C
6858502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6859502a2867SDave May 
6860502a2867SDave May  Not Collective
6861502a2867SDave May 
6862502a2867SDave May  Input Parameter:
6863502a2867SDave May  . dm    - The DM
6864502a2867SDave May 
6865502a2867SDave May  Output Parameter:
6866502a2867SDave May  . nranks - the number of neighbours
6867502a2867SDave May  . ranks - the neighbors ranks
6868502a2867SDave May 
6869502a2867SDave May  Notes:
6870502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6871502a2867SDave May 
6872502a2867SDave May  Level: beginner
6873502a2867SDave May 
6874dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6875502a2867SDave May @*/
6876502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6877502a2867SDave May {
6878502a2867SDave May   PetscErrorCode ierr;
6879502a2867SDave May 
6880502a2867SDave May   PetscFunctionBegin;
6881502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68820918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6883502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6884502a2867SDave May   PetscFunctionReturn(0);
6885502a2867SDave May }
6886502a2867SDave May 
6887531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6888531c7667SBarry Smith 
6889531c7667SBarry Smith /*
6890531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6891531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6892531c7667SBarry Smith */
6893531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6894531c7667SBarry Smith {
6895531c7667SBarry Smith   PetscErrorCode ierr;
6896531c7667SBarry Smith 
6897531c7667SBarry Smith   PetscFunctionBegin;
6898531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6899531c7667SBarry Smith     Vec x1local;
6900531c7667SBarry Smith     DM  dm;
6901531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6902531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6903531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6904531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6905531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6906531c7667SBarry Smith     x1   = x1local;
6907531c7667SBarry Smith   }
6908531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6909531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6910531c7667SBarry Smith     DM  dm;
6911531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6912531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6913531c7667SBarry Smith   }
6914531c7667SBarry Smith   PetscFunctionReturn(0);
6915531c7667SBarry Smith }
6916531c7667SBarry Smith 
6917531c7667SBarry Smith /*@
6918531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6919531c7667SBarry Smith 
6920531c7667SBarry Smith     Input Parameter:
6921531c7667SBarry Smith .    coloring - the MatFDColoring object
6922531c7667SBarry Smith 
692395452b02SPatrick Sanan     Developer Notes:
692495452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
6925531c7667SBarry Smith 
69261b266c99SBarry Smith     Level: advanced
69271b266c99SBarry Smith 
6928531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6929531c7667SBarry Smith @*/
6930531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6931531c7667SBarry Smith {
6932531c7667SBarry Smith   PetscFunctionBegin;
6933531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6934531c7667SBarry Smith   PetscFunctionReturn(0);
6935531c7667SBarry Smith }
69368320bc6fSPatrick Sanan 
69378320bc6fSPatrick Sanan /*@
69388320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
69398320bc6fSPatrick Sanan 
69408320bc6fSPatrick Sanan     Collective
69418320bc6fSPatrick Sanan 
69428320bc6fSPatrick Sanan     Input Parameters:
69438320bc6fSPatrick Sanan +    dm - the first DM
69448320bc6fSPatrick Sanan -    dm2 - the second DM
69458320bc6fSPatrick Sanan 
69468320bc6fSPatrick Sanan     Output Parameters:
69478320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
69488320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
69498320bc6fSPatrick Sanan 
69508320bc6fSPatrick Sanan     Notes:
69518320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
69528320bc6fSPatrick Sanan     of the same topology. This implies that the the section (field data) on one
69538320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
69548320bc6fSPatrick Sanan     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
69558320bc6fSPatrick Sanan     decomposition, with different data.
69568320bc6fSPatrick Sanan 
69578320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
69588320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
69598320bc6fSPatrick Sanan 
69608320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
69618320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
69628320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
69638320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
69648320bc6fSPatrick Sanan 
69658320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
69668320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
69678320bc6fSPatrick Sanan .vb
69688320bc6fSPatrick Sanan   ...
69698320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
69708320bc6fSPatrick Sanan   if (set && compatible)  {
69718320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
69728320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
69738320bc6fSPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
69748320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
69758320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
69768320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
69778320bc6fSPatrick Sanan       }
69788320bc6fSPatrick Sanan     }
69798320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
69808320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
69818320bc6fSPatrick Sanan   } else {
69828320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
69838320bc6fSPatrick Sanan   }
69848320bc6fSPatrick Sanan   ...
69858320bc6fSPatrick Sanan .ve
69868320bc6fSPatrick Sanan 
69878320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
69888320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
69898320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
69908320bc6fSPatrick Sanan     always check the "set" output parameter.
69918320bc6fSPatrick Sanan 
69928320bc6fSPatrick Sanan     A DM is always compatible with itself.
69938320bc6fSPatrick Sanan 
69948320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
69958320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
69968320bc6fSPatrick Sanan     incompatible.
69978320bc6fSPatrick Sanan 
69988320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
69998320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
70008320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
70018320bc6fSPatrick Sanan 
70028320bc6fSPatrick Sanan     Developer Notes:
70038320bc6fSPatrick Sanan     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
70048320bc6fSPatrick Sanan     the DM B is compatible with DM A. Thus, this function checks the implementations
70058320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
70068320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
70078320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
70088320bc6fSPatrick Sanan     logic for this function, to check for existing logic in the implementation
70098320bc6fSPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found; the logic of this
70108320bc6fSPatrick Sanan     function will then call that logic.
70118320bc6fSPatrick Sanan 
70128320bc6fSPatrick Sanan     Level: advanced
70138320bc6fSPatrick Sanan 
70148320bc6fSPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA()
70158320bc6fSPatrick Sanan @*/
70168320bc6fSPatrick Sanan 
70178320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
70188320bc6fSPatrick Sanan {
70198320bc6fSPatrick Sanan   PetscErrorCode ierr;
70208320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
70218320bc6fSPatrick Sanan   DMType         type,type2;
70228320bc6fSPatrick Sanan   PetscBool      sameType;
70238320bc6fSPatrick Sanan 
70248320bc6fSPatrick Sanan   PetscFunctionBegin;
70258320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
70268320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
70278320bc6fSPatrick Sanan 
70288320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
70298320bc6fSPatrick Sanan   if (dm == dm2) {
70308320bc6fSPatrick Sanan     *set = PETSC_TRUE;
70318320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
70328320bc6fSPatrick Sanan     PetscFunctionReturn(0);
70338320bc6fSPatrick Sanan   }
70348320bc6fSPatrick Sanan 
70358320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
70368320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
70378320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
70388320bc6fSPatrick Sanan      determined by the implementation-specific logic */
70398320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
70408320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
70418320bc6fSPatrick Sanan     *set = PETSC_TRUE;
70428320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
70438320bc6fSPatrick Sanan     PetscFunctionReturn(0);
70448320bc6fSPatrick Sanan   }
70458320bc6fSPatrick Sanan 
70468320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
70478320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
70488320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
70498320bc6fSPatrick Sanan     if (*set) {
70508320bc6fSPatrick Sanan       PetscFunctionReturn(0);
70518320bc6fSPatrick Sanan     }
70528320bc6fSPatrick Sanan   }
70538320bc6fSPatrick Sanan 
70548320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
70558320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
70568320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
70578320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
70588320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
70598320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
70608320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
70618320bc6fSPatrick Sanan   } else {
70628320bc6fSPatrick Sanan     *set = PETSC_FALSE;
70638320bc6fSPatrick Sanan   }
70648320bc6fSPatrick Sanan   PetscFunctionReturn(0);
70658320bc6fSPatrick Sanan }
7066