xref: /petsc/src/dm/interface/dm.c (revision 072d7d67ac8bac0e46d5c869139c3d706bd79e86)
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;
101ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1167a56275SMatthew G Knepley 
12e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
13bff4a2f0SMatthew G. Knepley 
144a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
154a7a4c06SLawrence Mitchell {
164a7a4c06SLawrence Mitchell   PetscFunctionBegin;
174a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
184a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
194a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
204a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
214a7a4c06SLawrence Mitchell }
224a7a4c06SLawrence Mitchell 
23a4121054SBarry Smith /*@
24de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
25a4121054SBarry Smith 
26a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
27a4121054SBarry Smith    error when you try to use the vector.
28a4121054SBarry Smith 
29a4121054SBarry Smith   Collective on MPI_Comm
30a4121054SBarry Smith 
31a4121054SBarry Smith   Input Parameter:
32a4121054SBarry Smith . comm - The communicator for the DM object
33a4121054SBarry Smith 
34a4121054SBarry Smith   Output Parameter:
35a4121054SBarry Smith . dm - The DM object
36a4121054SBarry Smith 
37a4121054SBarry Smith   Level: beginner
38a4121054SBarry Smith 
398472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
40a4121054SBarry Smith @*/
417087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
42a4121054SBarry Smith {
43a4121054SBarry Smith   DM             v;
44a4121054SBarry Smith   PetscErrorCode ierr;
45a4121054SBarry Smith 
46a4121054SBarry Smith   PetscFunctionBegin;
471411c6eeSJed Brown   PetscValidPointer(dm,2);
480298fd71SBarry Smith   *dm = NULL;
49607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
50a4121054SBarry Smith 
5173107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
52e7c4fc90SDmitry Karpeev 
530298fd71SBarry Smith   v->ltogmap                  = NULL;
541411c6eeSJed Brown   v->bs                       = 1;
55171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5688ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5788ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
58c58f1c22SToby Isaac   v->labels                   = NULL;
59c58f1c22SToby Isaac   v->depthLabel               = NULL;
600298fd71SBarry Smith   v->defaultSection           = NULL;
610298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
62fba222abSToby Isaac   v->defaultConstraintSection = NULL;
63fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
64c6b900c6SMatthew G. Knepley   v->L                        = NULL;
65c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
665dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
679a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
6896173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
69435a35e8SMatthew G Knepley   {
70435a35e8SMatthew G Knepley     PetscInt i;
71435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
720298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
73f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
74435a35e8SMatthew G Knepley     }
75435a35e8SMatthew G Knepley   }
762764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
7714f150ffSMatthew G. Knepley   v->dmBC = NULL;
78a8fb8f29SToby Isaac   v->coarseMesh = NULL;
79f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
80cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
81c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
82b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
83bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
84c58f1c22SToby Isaac   v->labels->refct = 1;
854a7a4c06SLawrence Mitchell 
864a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
874a7a4c06SLawrence Mitchell 
881411c6eeSJed Brown   *dm = v;
89a4121054SBarry Smith   PetscFunctionReturn(0);
90a4121054SBarry Smith }
91a4121054SBarry Smith 
9238221697SMatthew G. Knepley /*@
9338221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley   Collective on MPI_Comm
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley   Input Parameter:
9838221697SMatthew G. Knepley . dm - The original DM object
9938221697SMatthew G. Knepley 
10038221697SMatthew G. Knepley   Output Parameter:
10138221697SMatthew G. Knepley . newdm  - The new DM object
10238221697SMatthew G. Knepley 
10338221697SMatthew G. Knepley   Level: beginner
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley .keywords: DM, topology, create
10638221697SMatthew G. Knepley @*/
10738221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
10838221697SMatthew G. Knepley {
10938221697SMatthew G. Knepley   PetscSF        sf;
11038221697SMatthew G. Knepley   Vec            coords;
11138221697SMatthew G. Knepley   void          *ctx;
112a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11338221697SMatthew G. Knepley   PetscErrorCode ierr;
11438221697SMatthew G. Knepley 
11538221697SMatthew G. Knepley   PetscFunctionBegin;
11638221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11738221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
11838221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
119c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
120c58f1c22SToby Isaac   dm->labels->refct++;
121c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
122c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1231de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1241de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12538221697SMatthew G. Knepley   if (dm->ops->clone) {
12638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
12738221697SMatthew G. Knepley   }
1283f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
12938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
133be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
134be4c1c3eSMatthew G. Knepley     DM           ncdm;
135be4c1c3eSMatthew G. Knepley     PetscSection cs;
1365a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
137be4c1c3eSMatthew G. Knepley 
138e87a4003SBarry Smith     ierr = DMGetSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
139be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1405a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1415a0206caSToby Isaac     if (pEndMax >= 0) {
142be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
143e87a4003SBarry Smith       ierr = DMSetSection(ncdm, cs);CHKERRQ(ierr);
144a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
145be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
146be4c1c3eSMatthew G. Knepley     }
147be4c1c3eSMatthew G. Knepley   }
148a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
149a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15038221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15138221697SMatthew G. Knepley   if (coords) {
15238221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   } else {
15438221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15538221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15638221697SMatthew G. Knepley   }
15790b157c4SStefano Zampini   {
15890b157c4SStefano Zampini     PetscBool             isper;
159c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1605dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16190b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16290b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
163c6b900c6SMatthew G. Knepley   }
16438221697SMatthew G. Knepley   PetscFunctionReturn(0);
16538221697SMatthew G. Knepley }
16638221697SMatthew G. Knepley 
1679a42bb27SBarry Smith /*@C
168564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1699a42bb27SBarry Smith 
1708472ad0fSDave May    Logically Collective on DM
1719a42bb27SBarry Smith 
1729a42bb27SBarry Smith    Input Parameter:
1739a42bb27SBarry Smith +  da - initial distributed array
174e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1759a42bb27SBarry Smith 
1769a42bb27SBarry Smith    Options Database:
177dd85299cSBarry Smith .   -dm_vec_type ctype
1789a42bb27SBarry Smith 
1799a42bb27SBarry Smith    Level: intermediate
1809a42bb27SBarry Smith 
181a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1829a42bb27SBarry Smith @*/
18319fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1849a42bb27SBarry Smith {
1859a42bb27SBarry Smith   PetscErrorCode ierr;
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith   PetscFunctionBegin;
1889a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1899a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1919a42bb27SBarry Smith   PetscFunctionReturn(0);
1929a42bb27SBarry Smith }
1939a42bb27SBarry Smith 
194c0dedaeaSBarry Smith /*@C
195c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
196c0dedaeaSBarry Smith 
1978472ad0fSDave May    Logically Collective on DM
198c0dedaeaSBarry Smith 
199c0dedaeaSBarry Smith    Input Parameter:
200c0dedaeaSBarry Smith .  da - initial distributed array
201c0dedaeaSBarry Smith 
202c0dedaeaSBarry Smith    Output Parameter:
203c0dedaeaSBarry Smith .  ctype - the vector type
204c0dedaeaSBarry Smith 
205c0dedaeaSBarry Smith    Level: intermediate
206c0dedaeaSBarry Smith 
207a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
208c0dedaeaSBarry Smith @*/
209c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
210c0dedaeaSBarry Smith {
211c0dedaeaSBarry Smith   PetscFunctionBegin;
212c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
213c0dedaeaSBarry Smith   *ctype = da->vectype;
214c0dedaeaSBarry Smith   PetscFunctionReturn(0);
215c0dedaeaSBarry Smith }
216c0dedaeaSBarry Smith 
2175f1ad066SMatthew G Knepley /*@
21834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2195f1ad066SMatthew G Knepley 
2205f1ad066SMatthew G Knepley   Not collective
2215f1ad066SMatthew G Knepley 
2225f1ad066SMatthew G Knepley   Input Parameter:
2235f1ad066SMatthew G Knepley . v - The Vec
2245f1ad066SMatthew G Knepley 
2255f1ad066SMatthew G Knepley   Output Parameter:
2265f1ad066SMatthew G Knepley . dm - The DM
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Level: intermediate
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2315f1ad066SMatthew G Knepley @*/
2325f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2335f1ad066SMatthew G Knepley {
2345f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   PetscFunctionBegin;
2375f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2385f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2395f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2405f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2415f1ad066SMatthew G Knepley }
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley /*@
244d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   Not collective
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Input Parameters:
2495f1ad066SMatthew G Knepley + v - The Vec
2505f1ad066SMatthew G Knepley - dm - The DM
2515f1ad066SMatthew G Knepley 
252d9805387SMatthew 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.
253d9805387SMatthew G. Knepley 
2545f1ad066SMatthew G Knepley   Level: intermediate
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2575f1ad066SMatthew G Knepley @*/
2585f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2595f1ad066SMatthew G Knepley {
2605f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2615f1ad066SMatthew G Knepley 
2625f1ad066SMatthew G Knepley   PetscFunctionBegin;
2635f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
264d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2655f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2665f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2675f1ad066SMatthew G Knepley }
2685f1ad066SMatthew G Knepley 
269521d9a4cSLisandro Dalcin /*@C
2708f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2718f1509bcSBarry Smith 
2728f1509bcSBarry Smith    Logically Collective on DM
2738f1509bcSBarry Smith 
2748f1509bcSBarry Smith    Input Parameters:
2758f1509bcSBarry Smith +  dm - the DM context
2768f1509bcSBarry Smith -  ctype - the matrix type
2778f1509bcSBarry Smith 
2788f1509bcSBarry Smith    Options Database:
2798f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2808f1509bcSBarry Smith 
2818f1509bcSBarry Smith    Level: intermediate
2828f1509bcSBarry Smith 
2838f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2848f1509bcSBarry Smith           DMGetISColoringType()
2858f1509bcSBarry Smith @*/
2868f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2878f1509bcSBarry Smith {
2888f1509bcSBarry Smith   PetscFunctionBegin;
2898f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2908f1509bcSBarry Smith   dm->coloringtype = ctype;
2918f1509bcSBarry Smith   PetscFunctionReturn(0);
2928f1509bcSBarry Smith }
2938f1509bcSBarry Smith 
2948f1509bcSBarry Smith /*@C
2958f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
296521d9a4cSLisandro Dalcin 
297521d9a4cSLisandro Dalcin    Logically Collective on DM
298521d9a4cSLisandro Dalcin 
299521d9a4cSLisandro Dalcin    Input Parameter:
3008f1509bcSBarry Smith .  dm - the DM context
3018f1509bcSBarry Smith 
3028f1509bcSBarry Smith    Output Parameter:
3038f1509bcSBarry Smith .  ctype - the matrix type
3048f1509bcSBarry Smith 
3058f1509bcSBarry Smith    Options Database:
3068f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3078f1509bcSBarry Smith 
3088f1509bcSBarry Smith    Level: intermediate
3098f1509bcSBarry Smith 
3108f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3118f1509bcSBarry Smith           DMGetISColoringType()
3128f1509bcSBarry Smith @*/
3138f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3148f1509bcSBarry Smith {
3158f1509bcSBarry Smith   PetscFunctionBegin;
3168f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3178f1509bcSBarry Smith   *ctype = dm->coloringtype;
3188f1509bcSBarry Smith   PetscFunctionReturn(0);
3198f1509bcSBarry Smith }
3208f1509bcSBarry Smith 
3218f1509bcSBarry Smith /*@C
3228f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith    Logically Collective on DM
3258f1509bcSBarry Smith 
3268f1509bcSBarry Smith    Input Parameters:
327521d9a4cSLisandro Dalcin +  dm - the DM context
328a2b5a043SBarry Smith -  ctype - the matrix type
329521d9a4cSLisandro Dalcin 
330521d9a4cSLisandro Dalcin    Options Database:
331521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
332521d9a4cSLisandro Dalcin 
333521d9a4cSLisandro Dalcin    Level: intermediate
334521d9a4cSLisandro Dalcin 
335a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
336521d9a4cSLisandro Dalcin @*/
33719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
338521d9a4cSLisandro Dalcin {
339521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
34088f0584fSBarry Smith 
341521d9a4cSLisandro Dalcin   PetscFunctionBegin;
342521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
343521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
34419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
345521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
346521d9a4cSLisandro Dalcin }
347521d9a4cSLisandro Dalcin 
348c0dedaeaSBarry Smith /*@C
349c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
350c0dedaeaSBarry Smith 
351c0dedaeaSBarry Smith    Logically Collective on DM
352c0dedaeaSBarry Smith 
353c0dedaeaSBarry Smith    Input Parameter:
354c0dedaeaSBarry Smith .  dm - the DM context
355c0dedaeaSBarry Smith 
356c0dedaeaSBarry Smith    Output Parameter:
357c0dedaeaSBarry Smith .  ctype - the matrix type
358c0dedaeaSBarry Smith 
359c0dedaeaSBarry Smith    Options Database:
360c0dedaeaSBarry Smith .   -dm_mat_type ctype
361c0dedaeaSBarry Smith 
362c0dedaeaSBarry Smith    Level: intermediate
363c0dedaeaSBarry Smith 
364a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
365c0dedaeaSBarry Smith @*/
366c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
367c0dedaeaSBarry Smith {
368c0dedaeaSBarry Smith   PetscFunctionBegin;
369c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
370c0dedaeaSBarry Smith   *ctype = dm->mattype;
371c0dedaeaSBarry Smith   PetscFunctionReturn(0);
372c0dedaeaSBarry Smith }
373c0dedaeaSBarry Smith 
374c688c046SMatthew G Knepley /*@
37534f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
376c688c046SMatthew G Knepley 
377c688c046SMatthew G Knepley   Not collective
378c688c046SMatthew G Knepley 
379c688c046SMatthew G Knepley   Input Parameter:
380c688c046SMatthew G Knepley . A - The Mat
381c688c046SMatthew G Knepley 
382c688c046SMatthew G Knepley   Output Parameter:
383c688c046SMatthew G Knepley . dm - The DM
384c688c046SMatthew G Knepley 
385c688c046SMatthew G Knepley   Level: intermediate
386c688c046SMatthew G Knepley 
3878f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3888f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3898f1509bcSBarry Smith 
390c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
391c688c046SMatthew G Knepley @*/
392c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
393c688c046SMatthew G Knepley {
394c688c046SMatthew G Knepley   PetscErrorCode ierr;
395c688c046SMatthew G Knepley 
396c688c046SMatthew G Knepley   PetscFunctionBegin;
397c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
398c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
399c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
400c688c046SMatthew G Knepley   PetscFunctionReturn(0);
401c688c046SMatthew G Knepley }
402c688c046SMatthew G Knepley 
403c688c046SMatthew G Knepley /*@
404c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
405c688c046SMatthew G Knepley 
406c688c046SMatthew G Knepley   Not collective
407c688c046SMatthew G Knepley 
408c688c046SMatthew G Knepley   Input Parameters:
409c688c046SMatthew G Knepley + A - The Mat
410c688c046SMatthew G Knepley - dm - The DM
411c688c046SMatthew G Knepley 
412c688c046SMatthew G Knepley   Level: intermediate
413c688c046SMatthew G Knepley 
4148f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4158f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4168f1509bcSBarry Smith 
4178f1509bcSBarry Smith 
418c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
419c688c046SMatthew G Knepley @*/
420c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
421c688c046SMatthew G Knepley {
422c688c046SMatthew G Knepley   PetscErrorCode ierr;
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   PetscFunctionBegin;
425c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4268865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
427c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
428c688c046SMatthew G Knepley   PetscFunctionReturn(0);
429c688c046SMatthew G Knepley }
430c688c046SMatthew G Knepley 
4319a42bb27SBarry Smith /*@C
4329a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4336757b960SDave May    DM options in the database.
4349a42bb27SBarry Smith 
4358353ddbbSDave May    Logically Collective on DM
4369a42bb27SBarry Smith 
4379a42bb27SBarry Smith    Input Parameter:
4388353ddbbSDave May +  da - the DM context
4399a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4409a42bb27SBarry Smith 
4419a42bb27SBarry Smith    Notes:
4429a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4439a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4449a42bb27SBarry Smith 
4459a42bb27SBarry Smith    Level: advanced
4469a42bb27SBarry Smith 
4478353ddbbSDave May .keywords: DM, set, options, prefix, database
4489a42bb27SBarry Smith 
4499a42bb27SBarry Smith .seealso: DMSetFromOptions()
4509a42bb27SBarry Smith @*/
4517087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4529a42bb27SBarry Smith {
4539a42bb27SBarry Smith   PetscErrorCode ierr;
4549a42bb27SBarry Smith 
4559a42bb27SBarry Smith   PetscFunctionBegin;
4569a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4579a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
458691be533SLawrence Mitchell   if (dm->sf) {
459691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
460691be533SLawrence Mitchell   }
461691be533SLawrence Mitchell   if (dm->defaultSF) {
462691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
463691be533SLawrence Mitchell   }
4649a42bb27SBarry Smith   PetscFunctionReturn(0);
4659a42bb27SBarry Smith }
4669a42bb27SBarry Smith 
46731697293SDave May /*@C
46831697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
46931697293SDave May    DM options in the database.
47031697293SDave May 
47131697293SDave May    Logically Collective on DM
47231697293SDave May 
47331697293SDave May    Input Parameters:
47431697293SDave May +  dm - the DM context
47531697293SDave May -  prefix - the prefix string to prepend to all DM option requests
47631697293SDave May 
47731697293SDave May    Notes:
47831697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
47931697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48031697293SDave May 
48131697293SDave May    Level: advanced
48231697293SDave May 
48331697293SDave May .keywords: DM, append, options, prefix, database
48431697293SDave May 
48531697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
48631697293SDave May @*/
48731697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
48831697293SDave May {
48931697293SDave May   PetscErrorCode ierr;
49031697293SDave May 
49131697293SDave May   PetscFunctionBegin;
49231697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49331697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
49431697293SDave May   PetscFunctionReturn(0);
49531697293SDave May }
49631697293SDave May 
49731697293SDave May /*@C
49831697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
49931697293SDave May    DM options in the database.
50031697293SDave May 
50131697293SDave May    Not Collective
50231697293SDave May 
50331697293SDave May    Input Parameters:
50431697293SDave May .  dm - the DM context
50531697293SDave May 
50631697293SDave May    Output Parameters:
50731697293SDave May .  prefix - pointer to the prefix string used is returned
50831697293SDave May 
50995452b02SPatrick Sanan    Notes:
51095452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51131697293SDave May    sufficient length to hold the prefix.
51231697293SDave May 
51331697293SDave May    Level: advanced
51431697293SDave May 
51531697293SDave May .keywords: DM, set, options, prefix, database
51631697293SDave May 
51731697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
51831697293SDave May @*/
51931697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52031697293SDave May {
52131697293SDave May   PetscErrorCode ierr;
52231697293SDave May 
52331697293SDave May   PetscFunctionBegin;
52431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52531697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
52631697293SDave May   PetscFunctionReturn(0);
52731697293SDave May }
52831697293SDave May 
52988bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53088bdff64SToby Isaac {
53188bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53288bdff64SToby Isaac   DMNamedVecLink nlink;
53388bdff64SToby Isaac   PetscErrorCode ierr;
53488bdff64SToby Isaac 
53588bdff64SToby Isaac   PetscFunctionBegin;
536aab5bcd8SJed Brown   *ncrefct = 0;
53788bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
53888bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
53988bdff64SToby Isaac     if (dm->localin[i])  refct--;
54088bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54188bdff64SToby Isaac   }
54288bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54388bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
54488bdff64SToby Isaac   if (dm->x) {
54588bdff64SToby Isaac     DM obj;
54688bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
54788bdff64SToby Isaac     if (obj == dm) refct--;
54888bdff64SToby Isaac   }
54988bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55088bdff64SToby Isaac     refct--;
55188bdff64SToby Isaac     if (recurseCoarse) {
55288bdff64SToby Isaac       PetscInt coarseCount;
55388bdff64SToby Isaac 
55488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55588bdff64SToby Isaac       refct += coarseCount;
55688bdff64SToby Isaac     }
55788bdff64SToby Isaac   }
55888bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55988bdff64SToby Isaac     refct--;
56088bdff64SToby Isaac     if (recurseFine) {
56188bdff64SToby Isaac       PetscInt fineCount;
56288bdff64SToby Isaac 
56388bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56488bdff64SToby Isaac       refct += fineCount;
56588bdff64SToby Isaac     }
56688bdff64SToby Isaac   }
56788bdff64SToby Isaac   *ncrefct = refct;
56888bdff64SToby Isaac   PetscFunctionReturn(0);
56988bdff64SToby Isaac }
57088bdff64SToby Isaac 
571354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
572354557abSToby Isaac {
573354557abSToby Isaac   PetscErrorCode ierr;
574354557abSToby Isaac 
575354557abSToby Isaac   PetscFunctionBegin;
576354557abSToby Isaac   if (!--(dm->labels->refct)) {
577354557abSToby Isaac     DMLabelLink next = dm->labels->next;
578354557abSToby Isaac 
579354557abSToby Isaac     /* destroy the labels */
580354557abSToby Isaac     while (next) {
581354557abSToby Isaac       DMLabelLink tmp = next->next;
582354557abSToby Isaac 
583354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
584354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
585354557abSToby Isaac       next = tmp;
586354557abSToby Isaac     }
587354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
588354557abSToby Isaac   }
589354557abSToby Isaac   PetscFunctionReturn(0);
590354557abSToby Isaac }
591354557abSToby Isaac 
59247c6ae99SBarry Smith /*@
5938472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith     Collective on DM
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith     Input Parameter:
59847c6ae99SBarry Smith .   dm - the DM object to destroy
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith     Level: developer
60147c6ae99SBarry Smith 
602e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60347c6ae99SBarry Smith 
60447c6ae99SBarry Smith @*/
605fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60647c6ae99SBarry Smith {
60788bdff64SToby Isaac   PetscInt       i, cnt;
608dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60947c6ae99SBarry Smith   PetscErrorCode ierr;
61047c6ae99SBarry Smith 
61147c6ae99SBarry Smith   PetscFunctionBegin;
6126bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6136bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61487e657c6SBarry Smith 
61588bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61688bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61788bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
61888bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
619732e2eb9SMatthew G Knepley   /*
620732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
621732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
622732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
623732e2eb9SMatthew G Knepley   */
6246bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6256bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
626732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6276bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6286bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
629732e2eb9SMatthew G Knepley   }
630f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6310298fd71SBarry Smith   (*dm)->namedglobal = NULL;
632f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6332348bcf4SPeter Brune     nnext = nlink->next;
6342348bcf4SPeter 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);
6352348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6362348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6372348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6382348bcf4SPeter Brune   }
639f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6400298fd71SBarry Smith   (*dm)->namedlocal = NULL;
641f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
642f490541aSPeter Brune     nnext = nlink->next;
643f490541aSPeter 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);
644f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
645f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
646f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
647f490541aSPeter Brune   }
6482348bcf4SPeter Brune 
649b17ce1afSJed Brown   /* Destroy the list of hooks */
650c833c3b5SJed Brown   {
651c833c3b5SJed Brown     DMCoarsenHookLink link,next;
652b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
653b17ce1afSJed Brown       next = link->next;
654b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
655b17ce1afSJed Brown     }
6560298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
657c833c3b5SJed Brown   }
658c833c3b5SJed Brown   {
659c833c3b5SJed Brown     DMRefineHookLink link,next;
660c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
661c833c3b5SJed Brown       next = link->next;
662c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
663c833c3b5SJed Brown     }
6640298fd71SBarry Smith     (*dm)->refinehook = NULL;
665c833c3b5SJed Brown   }
666be081cd6SPeter Brune   {
667be081cd6SPeter Brune     DMSubDomainHookLink link,next;
668be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
669be081cd6SPeter Brune       next = link->next;
670be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
671be081cd6SPeter Brune     }
6720298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
673be081cd6SPeter Brune   }
674baf369e7SPeter Brune   {
675baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
676baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
677baf369e7SPeter Brune       next = link->next;
678baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
679baf369e7SPeter Brune     }
6800298fd71SBarry Smith     (*dm)->gtolhook = NULL;
681baf369e7SPeter Brune   }
682d4d07f1eSToby Isaac   {
683d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
684d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
685d4d07f1eSToby Isaac       next = link->next;
686d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
687d4d07f1eSToby Isaac     }
688d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
689d4d07f1eSToby Isaac   }
690aa1993deSMatthew G Knepley   /* Destroy the work arrays */
691aa1993deSMatthew G Knepley   {
692aa1993deSMatthew G Knepley     DMWorkLink link,next;
693aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
694aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
695aa1993deSMatthew G Knepley       next = link->next;
696aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
697aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
698aa1993deSMatthew G Knepley     }
6990298fd71SBarry Smith     (*dm)->workin = NULL;
700aa1993deSMatthew G Knepley   }
701c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
702c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
703c58f1c22SToby Isaac 
704c58f1c22SToby Isaac     /* destroy the labels */
705c58f1c22SToby Isaac     while (next) {
706c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
707c58f1c22SToby Isaac 
708c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
709c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
710c58f1c22SToby Isaac       next = tmp;
711c58f1c22SToby Isaac     }
712c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
713c58f1c22SToby Isaac   }
714e6f8dbb6SToby Isaac   {
715e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
716e6f8dbb6SToby Isaac     while (next) {
717e6f8dbb6SToby Isaac       DMBoundary b = next;
718e6f8dbb6SToby Isaac 
719e6f8dbb6SToby Isaac       next = b->next;
720e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
721e6f8dbb6SToby Isaac     }
722e6f8dbb6SToby Isaac   }
723b17ce1afSJed Brown 
72452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
72552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72752536dc3SBarry Smith 
7281a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7291a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7301a266240SBarry Smith   }
73187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
73271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7334dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7346bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7356bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
736073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
73788ed4aceSMatthew G Knepley 
73888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
73988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
7408b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
741fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
742fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
74388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
74488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
745736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
746736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7478e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
748736995cdSBlaise Bourdin     }
749736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
750736995cdSBlaise Bourdin   }
75188bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75288bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75388bdff64SToby Isaac   }
754a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75588bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
75688bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
75788bdff64SToby Isaac   }
75888bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
759f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7606636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7616636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7626636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7635dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
7646636e97aSMatthew G Knepley 
7652764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
76614f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
767e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
768e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
769732e2eb9SMatthew G Knepley 
770ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7716bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
772ed3c66a1SDave May   }
773435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
774732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
77547c6ae99SBarry Smith   PetscFunctionReturn(0);
77647c6ae99SBarry Smith }
77747c6ae99SBarry Smith 
778d7bf68aeSBarry Smith /*@
779d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
780d7bf68aeSBarry Smith 
781d7bf68aeSBarry Smith     Collective on DM
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith     Input Parameter:
784d7bf68aeSBarry Smith .   dm - the DM object to setup
785d7bf68aeSBarry Smith 
786d7bf68aeSBarry Smith     Level: developer
787d7bf68aeSBarry Smith 
788e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
789d7bf68aeSBarry Smith 
790d7bf68aeSBarry Smith @*/
7917087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
792d7bf68aeSBarry Smith {
793d7bf68aeSBarry Smith   PetscErrorCode ierr;
794d7bf68aeSBarry Smith 
795d7bf68aeSBarry Smith   PetscFunctionBegin;
796171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7978387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
798d7bf68aeSBarry Smith   if (dm->ops->setup) {
799d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
800d7bf68aeSBarry Smith   }
8018387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
802d7bf68aeSBarry Smith   PetscFunctionReturn(0);
803d7bf68aeSBarry Smith }
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith /*@
806d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
807d7bf68aeSBarry Smith 
808d7bf68aeSBarry Smith     Collective on DM
809d7bf68aeSBarry Smith 
810d7bf68aeSBarry Smith     Input Parameter:
811d7bf68aeSBarry Smith .   dm - the DM object to set options for
812d7bf68aeSBarry Smith 
813732e2eb9SMatthew G Knepley     Options Database:
8144164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8154164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8164164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8178f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
818732e2eb9SMatthew G Knepley 
819d7bf68aeSBarry Smith     Level: developer
820d7bf68aeSBarry Smith 
821e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
822d7bf68aeSBarry Smith 
823d7bf68aeSBarry Smith @*/
8247087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
825d7bf68aeSBarry Smith {
8267781c08eSBarry Smith   char           typeName[256];
827ca266f36SBarry Smith   PetscBool      flg;
828d7bf68aeSBarry Smith   PetscErrorCode ierr;
829d7bf68aeSBarry Smith 
830d7bf68aeSBarry Smith   PetscFunctionBegin;
831171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
832f1fd5e65SToby Isaac   if (dm->prob) {
833f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
834f1fd5e65SToby Isaac   }
835691be533SLawrence Mitchell   if (dm->sf) {
836691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
837691be533SLawrence Mitchell   }
838691be533SLawrence Mitchell   if (dm->defaultSF) {
839691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
840691be533SLawrence Mitchell   }
8413194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8420298fd71SBarry 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);
843a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
844f9ba7244SBarry Smith   if (flg) {
845f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
846f9ba7244SBarry Smith   }
847a264d7a6SBarry 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);
848073dac72SJed Brown   if (flg) {
849521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
850073dac72SJed Brown   }
8518f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
852f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
853e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
854f9ba7244SBarry Smith   }
855f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8560633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85782fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
858d7bf68aeSBarry Smith   PetscFunctionReturn(0);
859d7bf68aeSBarry Smith }
860d7bf68aeSBarry Smith 
861fc9bc008SSatish Balay /*@C
862224748a4SBarry Smith     DMView - Views a DM
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith     Collective on DM
86547c6ae99SBarry Smith 
86647c6ae99SBarry Smith     Input Parameter:
86747c6ae99SBarry Smith +   dm - the DM object to view
86847c6ae99SBarry Smith -   v - the viewer
86947c6ae99SBarry Smith 
870224748a4SBarry Smith     Level: beginner
87147c6ae99SBarry Smith 
872e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith @*/
8757087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
87647c6ae99SBarry Smith {
87747c6ae99SBarry Smith   PetscErrorCode    ierr;
87832c0f0efSBarry Smith   PetscBool         isbinary;
87976a8abe0SBarry Smith   PetscMPIInt       size;
88076a8abe0SBarry Smith   PetscViewerFormat format;
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith   PetscFunctionBegin;
883171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8843014e516SBarry Smith   if (!v) {
885ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8863014e516SBarry Smith   }
88776a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
88876a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
88976a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
89098c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
89132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
89232c0f0efSBarry Smith   if (isbinary) {
89355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
89432c0f0efSBarry Smith     char     type[256];
89532c0f0efSBarry Smith 
89632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
89732c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
89832c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
89932c0f0efSBarry Smith   }
9000c010503SBarry Smith   if (dm->ops->view) {
9010c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
90247c6ae99SBarry Smith   }
90347c6ae99SBarry Smith   PetscFunctionReturn(0);
90447c6ae99SBarry Smith }
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith /*@
9078472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith     Collective on DM
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith     Input Parameter:
91247c6ae99SBarry Smith .   dm - the DM object
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith     Output Parameter:
91547c6ae99SBarry Smith .   vec - the global vector
91647c6ae99SBarry Smith 
917073dac72SJed Brown     Level: beginner
91847c6ae99SBarry Smith 
919e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
92047c6ae99SBarry Smith 
92147c6ae99SBarry Smith @*/
9227087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
92347c6ae99SBarry Smith {
92447c6ae99SBarry Smith   PetscErrorCode ierr;
92547c6ae99SBarry Smith 
92647c6ae99SBarry Smith   PetscFunctionBegin;
927171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92847c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
92947c6ae99SBarry Smith   PetscFunctionReturn(0);
93047c6ae99SBarry Smith }
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith /*@
9338472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith     Not Collective
93647c6ae99SBarry Smith 
93747c6ae99SBarry Smith     Input Parameter:
93847c6ae99SBarry Smith .   dm - the DM object
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith     Output Parameter:
94147c6ae99SBarry Smith .   vec - the local vector
94247c6ae99SBarry Smith 
943073dac72SJed Brown     Level: beginner
94447c6ae99SBarry Smith 
945e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith @*/
9487087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
94947c6ae99SBarry Smith {
95047c6ae99SBarry Smith   PetscErrorCode ierr;
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith   PetscFunctionBegin;
953171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
95447c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
95547c6ae99SBarry Smith   PetscFunctionReturn(0);
95647c6ae99SBarry Smith }
95747c6ae99SBarry Smith 
9581411c6eeSJed Brown /*@
9591411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9601411c6eeSJed Brown 
9611411c6eeSJed Brown    Collective on DM
9621411c6eeSJed Brown 
9631411c6eeSJed Brown    Input Parameter:
9641411c6eeSJed Brown .  dm - the DM that provides the mapping
9651411c6eeSJed Brown 
9661411c6eeSJed Brown    Output Parameter:
9671411c6eeSJed Brown .  ltog - the mapping
9681411c6eeSJed Brown 
9691411c6eeSJed Brown    Level: intermediate
9701411c6eeSJed Brown 
9711411c6eeSJed Brown    Notes:
9721411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9731411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9741411c6eeSJed Brown 
975fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9761411c6eeSJed Brown @*/
9777087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9781411c6eeSJed Brown {
9790be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9801411c6eeSJed Brown   PetscErrorCode ierr;
9811411c6eeSJed Brown 
9821411c6eeSJed Brown   PetscFunctionBegin;
9831411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9841411c6eeSJed Brown   PetscValidPointer(ltog,2);
9851411c6eeSJed Brown   if (!dm->ltogmap) {
98637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
98737d0c07bSMatthew G Knepley 
988e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
98937d0c07bSMatthew G Knepley     if (section) {
990a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
99137d0c07bSMatthew G Knepley       PetscInt       *ltog;
992ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
99337d0c07bSMatthew G Knepley 
994e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
99537d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
996ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
997ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
99837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
999a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
100037d0c07bSMatthew G Knepley 
100137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
100237d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1003a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1004a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
100537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10061a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10071a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10081a7dc684SMatthew G. Knepley         if (dof) {
1009b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1010b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10111a7dc684SMatthew G. Knepley         }
101237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1013a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1014a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
101537d0c07bSMatthew G Knepley         }
101637d0c07bSMatthew G Knepley       }
1017bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10180be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10190be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10200be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10210be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10227591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10237591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1024ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1025ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1026ccf3bd66SMatthew G. Knepley         n /= bs;
1027ccf3bd66SMatthew G. Knepley       }
1028ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10293bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
103037d0c07bSMatthew G Knepley     } else {
1031184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1032184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10331411c6eeSJed Brown     }
103437d0c07bSMatthew G Knepley   }
10351411c6eeSJed Brown   *ltog = dm->ltogmap;
10361411c6eeSJed Brown   PetscFunctionReturn(0);
10371411c6eeSJed Brown }
10381411c6eeSJed Brown 
10391411c6eeSJed Brown /*@
10401411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10411411c6eeSJed Brown 
10421411c6eeSJed Brown    Not Collective
10431411c6eeSJed Brown 
10441411c6eeSJed Brown    Input Parameter:
10451411c6eeSJed Brown .  dm - the DM with block structure
10461411c6eeSJed Brown 
10471411c6eeSJed Brown    Output Parameter:
10481411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10491411c6eeSJed Brown 
10501411c6eeSJed Brown    Level: intermediate
10511411c6eeSJed Brown 
1052fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10531411c6eeSJed Brown @*/
10547087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10551411c6eeSJed Brown {
10561411c6eeSJed Brown   PetscFunctionBegin;
10571411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10581411c6eeSJed Brown   PetscValidPointer(bs,2);
10591411c6eeSJed 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");
10601411c6eeSJed Brown   *bs = dm->bs;
10611411c6eeSJed Brown   PetscFunctionReturn(0);
10621411c6eeSJed Brown }
10631411c6eeSJed Brown 
106447c6ae99SBarry Smith /*@
10658472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
106647c6ae99SBarry Smith 
106747c6ae99SBarry Smith     Collective on DM
106847c6ae99SBarry Smith 
106947c6ae99SBarry Smith     Input Parameter:
107047c6ae99SBarry Smith +   dm1 - the DM object
107147c6ae99SBarry Smith -   dm2 - the second, finer DM object
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith     Output Parameter:
107447c6ae99SBarry Smith +  mat - the interpolation
107547c6ae99SBarry Smith -  vec - the scaling (optional)
107647c6ae99SBarry Smith 
107747c6ae99SBarry Smith     Level: developer
107847c6ae99SBarry Smith 
107995452b02SPatrick Sanan     Notes:
108095452b02SPatrick 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
108185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1082d52bd9f3SBarry Smith 
10831f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1084d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
108585afcc9aSBarry Smith 
108685afcc9aSBarry Smith 
10873ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
108847c6ae99SBarry Smith 
108947c6ae99SBarry Smith @*/
1090e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
109147c6ae99SBarry Smith {
109247c6ae99SBarry Smith   PetscErrorCode ierr;
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith   PetscFunctionBegin;
1095171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1096171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1097c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1098c7d20fa0SStefano Zampini   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInterpolation not implemented for type %s",((PetscObject)dm1)->type_name);
1099cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1101cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110247c6ae99SBarry Smith   PetscFunctionReturn(0);
110347c6ae99SBarry Smith }
110447c6ae99SBarry Smith 
11053ad4599aSBarry Smith /*@
11063ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11073ad4599aSBarry Smith 
11083ad4599aSBarry Smith     Collective on DM
11093ad4599aSBarry Smith 
11103ad4599aSBarry Smith     Input Parameter:
11113ad4599aSBarry Smith +   dm1 - the DM object
11123ad4599aSBarry Smith -   dm2 - the second, finer DM object
11133ad4599aSBarry Smith 
11143ad4599aSBarry Smith     Output Parameter:
11153ad4599aSBarry Smith .  mat - the restriction
11163ad4599aSBarry Smith 
11173ad4599aSBarry Smith 
11183ad4599aSBarry Smith     Level: developer
11193ad4599aSBarry Smith 
112095452b02SPatrick Sanan     Notes:
112195452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
11223ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11233ad4599aSBarry Smith 
11243ad4599aSBarry Smith 
11253ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11263ad4599aSBarry Smith 
11273ad4599aSBarry Smith @*/
11283ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11293ad4599aSBarry Smith {
11303ad4599aSBarry Smith   PetscErrorCode ierr;
11313ad4599aSBarry Smith 
11323ad4599aSBarry Smith   PetscFunctionBegin;
11333ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11343ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11353ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11367294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11373ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11383ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11393ad4599aSBarry Smith   PetscFunctionReturn(0);
11403ad4599aSBarry Smith }
11413ad4599aSBarry Smith 
114247c6ae99SBarry Smith /*@
11438472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
114447c6ae99SBarry Smith 
114547c6ae99SBarry Smith     Collective on DM
114647c6ae99SBarry Smith 
114747c6ae99SBarry Smith     Input Parameter:
114847c6ae99SBarry Smith +   dm1 - the DM object
114947c6ae99SBarry Smith -   dm2 - the second, finer DM object
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith     Output Parameter:
11526dbf9973SLawrence Mitchell .   mat - the injection
115347c6ae99SBarry Smith 
115447c6ae99SBarry Smith     Level: developer
115547c6ae99SBarry Smith 
115695452b02SPatrick Sanan    Notes:
115795452b02SPatrick 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
115885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
115985afcc9aSBarry Smith 
1160e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
116147c6ae99SBarry Smith 
116247c6ae99SBarry Smith @*/
11636dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
116447c6ae99SBarry Smith {
116547c6ae99SBarry Smith   PetscErrorCode ierr;
116647c6ae99SBarry Smith 
116747c6ae99SBarry Smith   PetscFunctionBegin;
1168171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1169171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11700c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11716dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
117247c6ae99SBarry Smith   PetscFunctionReturn(0);
117347c6ae99SBarry Smith }
117447c6ae99SBarry Smith 
1175b412c318SBarry Smith /*@
1176bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1177bd041c0cSMatthew G. Knepley 
1178bd041c0cSMatthew G. Knepley   Collective on DM
1179bd041c0cSMatthew G. Knepley 
1180bd041c0cSMatthew G. Knepley   Input Parameter:
1181bd041c0cSMatthew G. Knepley + dm1 - the DM object
1182bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1183bd041c0cSMatthew G. Knepley 
1184bd041c0cSMatthew G. Knepley   Output Parameter:
1185bd041c0cSMatthew G. Knepley . mat - the interpolation
1186bd041c0cSMatthew G. Knepley 
1187bd041c0cSMatthew G. Knepley   Level: developer
1188bd041c0cSMatthew G. Knepley 
1189bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1190bd041c0cSMatthew G. Knepley @*/
1191bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1192bd041c0cSMatthew G. Knepley {
1193bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1194bd041c0cSMatthew G. Knepley 
1195bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1196bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1197bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1198bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1199bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1200bd041c0cSMatthew G. Knepley }
1201bd041c0cSMatthew G. Knepley 
1202bd041c0cSMatthew G. Knepley /*@
1203b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith     Collective on DM
120647c6ae99SBarry Smith 
120747c6ae99SBarry Smith     Input Parameter:
120847c6ae99SBarry Smith +   dm - the DM object
12095bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
121047c6ae99SBarry Smith 
121147c6ae99SBarry Smith     Output Parameter:
121247c6ae99SBarry Smith .   coloring - the coloring
121347c6ae99SBarry Smith 
121447c6ae99SBarry Smith     Level: developer
121547c6ae99SBarry Smith 
1216b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
121747c6ae99SBarry Smith 
1218aab9d709SJed Brown @*/
1219b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
122047c6ae99SBarry Smith {
122147c6ae99SBarry Smith   PetscErrorCode ierr;
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith   PetscFunctionBegin;
1224171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1225ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1226b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
122747c6ae99SBarry Smith   PetscFunctionReturn(0);
122847c6ae99SBarry Smith }
122947c6ae99SBarry Smith 
1230b412c318SBarry Smith /*@
12318472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
123247c6ae99SBarry Smith 
123347c6ae99SBarry Smith     Collective on DM
123447c6ae99SBarry Smith 
123547c6ae99SBarry Smith     Input Parameter:
1236b412c318SBarry Smith .   dm - the DM object
123747c6ae99SBarry Smith 
123847c6ae99SBarry Smith     Output Parameter:
123947c6ae99SBarry Smith .   mat - the empty Jacobian
124047c6ae99SBarry Smith 
1241073dac72SJed Brown     Level: beginner
124247c6ae99SBarry Smith 
124395452b02SPatrick Sanan     Notes:
124495452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
124594013140SBarry Smith        do not need to do it yourself.
124694013140SBarry Smith 
124794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12487889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
124994013140SBarry Smith 
125094013140SBarry 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
125194013140SBarry Smith        internally by PETSc.
125294013140SBarry Smith 
125394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1254aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
125594013140SBarry Smith 
1256b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
125747c6ae99SBarry Smith 
1258aab9d709SJed Brown @*/
1259b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
126047c6ae99SBarry Smith {
126147c6ae99SBarry Smith   PetscErrorCode ierr;
126247c6ae99SBarry Smith 
126347c6ae99SBarry Smith   PetscFunctionBegin;
1264171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1265607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1266c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1267c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1268b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1269e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1270e571a35bSMatthew G. Knepley   if (dm->prob) {
1271e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1272e571a35bSMatthew G. Knepley     PetscInt     Nf;
1273e571a35bSMatthew G. Knepley 
1274e571a35bSMatthew G. Knepley     ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
1275e571a35bSMatthew G. Knepley     if (Nf == 1) {
1276e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1277e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1278e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1279e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1280e571a35bSMatthew G. Knepley       }
1281e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1282e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1283e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1284e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1285e571a35bSMatthew G. Knepley       }
1286e571a35bSMatthew G. Knepley     }
1287e571a35bSMatthew G. Knepley   }
128847c6ae99SBarry Smith   PetscFunctionReturn(0);
128947c6ae99SBarry Smith }
129047c6ae99SBarry Smith 
1291732e2eb9SMatthew G Knepley /*@
1292950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1293732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1294732e2eb9SMatthew G Knepley 
12958472ad0fSDave May   Logically Collective on DM
1296732e2eb9SMatthew G Knepley 
1297732e2eb9SMatthew G Knepley   Input Parameter:
1298732e2eb9SMatthew G Knepley + dm - the DM
1299732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1300732e2eb9SMatthew G Knepley 
1301732e2eb9SMatthew G Knepley   Level: developer
1302b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1303732e2eb9SMatthew G Knepley @*/
1304732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1305732e2eb9SMatthew G Knepley {
1306732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1307732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1308732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1309732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1310732e2eb9SMatthew G Knepley }
1311732e2eb9SMatthew G Knepley 
1312b06ff27eSHong Zhang /*@
1313b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1314b06ff27eSHong Zhang     but the array for values will not be allocated.
1315b06ff27eSHong Zhang 
1316b06ff27eSHong Zhang   Logically Collective on DM
1317b06ff27eSHong Zhang 
1318b06ff27eSHong Zhang   Input Parameter:
1319b06ff27eSHong Zhang + dm - the DM
1320b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1321b06ff27eSHong Zhang 
1322b06ff27eSHong Zhang   Level: developer
1323b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1324b06ff27eSHong Zhang @*/
1325b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1326b06ff27eSHong Zhang {
1327b06ff27eSHong Zhang   PetscFunctionBegin;
1328b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1329b06ff27eSHong Zhang   dm->structure_only = only;
1330b06ff27eSHong Zhang   PetscFunctionReturn(0);
1331b06ff27eSHong Zhang }
1332b06ff27eSHong Zhang 
1333a89ea682SMatthew G Knepley /*@C
1334aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1335a89ea682SMatthew G Knepley 
1336a89ea682SMatthew G Knepley   Not Collective
1337a89ea682SMatthew G Knepley 
1338a89ea682SMatthew G Knepley   Input Parameters:
1339a89ea682SMatthew G Knepley + dm - the DM object
1340aa1993deSMatthew G Knepley . count - The minium size
134169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1342a89ea682SMatthew G Knepley 
1343a89ea682SMatthew G Knepley   Output Parameter:
1344a89ea682SMatthew G Knepley . array - the work array
1345a89ea682SMatthew G Knepley 
1346a89ea682SMatthew G Knepley   Level: developer
1347a89ea682SMatthew G Knepley 
1348a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1349a89ea682SMatthew G Knepley @*/
135069291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1351a89ea682SMatthew G Knepley {
1352a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1353aa1993deSMatthew G Knepley   DMWorkLink     link;
135469291d52SBarry Smith   PetscMPIInt    dsize;
1355a89ea682SMatthew G Knepley 
1356a89ea682SMatthew G Knepley   PetscFunctionBegin;
1357a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1358aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1359aa1993deSMatthew G Knepley   if (dm->workin) {
1360aa1993deSMatthew G Knepley     link       = dm->workin;
1361aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1362aa1993deSMatthew G Knepley   } else {
1363b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1364a89ea682SMatthew G Knepley   }
136569291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13665056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1367aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1368854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1369854ce69bSBarry Smith     link->bytes = dsize*count;
1370aa1993deSMatthew G Knepley   }
1371aa1993deSMatthew G Knepley   link->next   = dm->workout;
1372aa1993deSMatthew G Knepley   dm->workout  = link;
1373aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1374a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1375a89ea682SMatthew G Knepley }
1376a89ea682SMatthew G Knepley 
1377aa1993deSMatthew G Knepley /*@C
1378aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1379aa1993deSMatthew G Knepley 
1380aa1993deSMatthew G Knepley   Not Collective
1381aa1993deSMatthew G Knepley 
1382aa1993deSMatthew G Knepley   Input Parameters:
1383aa1993deSMatthew G Knepley + dm - the DM object
1384aa1993deSMatthew G Knepley . count - The minium size
138569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1386aa1993deSMatthew G Knepley 
1387aa1993deSMatthew G Knepley   Output Parameter:
1388aa1993deSMatthew G Knepley . array - the work array
1389aa1993deSMatthew G Knepley 
1390aa1993deSMatthew G Knepley   Level: developer
1391aa1993deSMatthew G Knepley 
139295452b02SPatrick Sanan   Developer Notes:
139395452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1394aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1395aa1993deSMatthew G Knepley @*/
139669291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1397aa1993deSMatthew G Knepley {
1398aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1399aa1993deSMatthew G Knepley 
1400aa1993deSMatthew G Knepley   PetscFunctionBegin;
1401aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1402aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1403aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1404aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1405aa1993deSMatthew G Knepley       *p           = link->next;
1406aa1993deSMatthew G Knepley       link->next   = dm->workin;
1407aa1993deSMatthew G Knepley       dm->workin   = link;
14080298fd71SBarry Smith       *(void**)mem = NULL;
1409aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1410aa1993deSMatthew G Knepley     }
1411aa1993deSMatthew G Knepley   }
1412aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1413aa1993deSMatthew G Knepley }
1414e7c4fc90SDmitry Karpeev 
1415435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1416435a35e8SMatthew G Knepley {
1417435a35e8SMatthew G Knepley   PetscFunctionBegin;
1418435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
141982f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1420435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1421435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1422435a35e8SMatthew G Knepley }
1423435a35e8SMatthew G Knepley 
14240a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14250a50eb56SMatthew G. Knepley {
14260a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14270a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1428f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14290a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14300a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14310a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14320a50eb56SMatthew G. Knepley }
14330a50eb56SMatthew G. Knepley 
1434f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1435f9d4088aSMatthew G. Knepley {
1436f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1437f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1438f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1439f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1440f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1441f9d4088aSMatthew G. Knepley }
1442f9d4088aSMatthew G. Knepley 
1443f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1444f9d4088aSMatthew G. Knepley {
1445f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1446f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1447f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1448f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1449f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1450f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1451f9d4088aSMatthew G. Knepley }
1452f9d4088aSMatthew G. Knepley 
14534f3b5142SJed Brown /*@C
14544d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14554d343eeaSMatthew G Knepley 
14564d343eeaSMatthew G Knepley   Not collective
14574d343eeaSMatthew G Knepley 
14584d343eeaSMatthew G Knepley   Input Parameter:
14594d343eeaSMatthew G Knepley . dm - the DM object
14604d343eeaSMatthew G Knepley 
14614d343eeaSMatthew G Knepley   Output Parameters:
14620298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14630298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14640298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14654d343eeaSMatthew G Knepley 
14664d343eeaSMatthew G Knepley   Level: intermediate
14674d343eeaSMatthew G Knepley 
146821c9b008SJed Brown   Notes:
146921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
147021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
147121c9b008SJed Brown   PetscFree().
147221c9b008SJed Brown 
14734d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14744d343eeaSMatthew G Knepley @*/
147537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14764d343eeaSMatthew G Knepley {
147737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14784d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14794d343eeaSMatthew G Knepley 
14804d343eeaSMatthew G Knepley   PetscFunctionBegin;
14814d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
148269ca1f37SDmitry Karpeev   if (numFields) {
148369ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
148469ca1f37SDmitry Karpeev     *numFields = 0;
148569ca1f37SDmitry Karpeev   }
148637d0c07bSMatthew G Knepley   if (fieldNames) {
148737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14880298fd71SBarry Smith     *fieldNames = NULL;
148969ca1f37SDmitry Karpeev   }
149069ca1f37SDmitry Karpeev   if (fields) {
149169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
14920298fd71SBarry Smith     *fields = NULL;
149369ca1f37SDmitry Karpeev   }
1494e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
149537d0c07bSMatthew G Knepley   if (section) {
14963a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
149737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
149837d0c07bSMatthew G Knepley 
1499e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
150037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15013a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
150237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
150337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
150437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15053a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
150637d0c07bSMatthew G Knepley     }
150737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
150837d0c07bSMatthew G Knepley       PetscInt gdof;
150937d0c07bSMatthew G Knepley 
151037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
151137d0c07bSMatthew G Knepley       if (gdof > 0) {
151237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15133a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
151437d0c07bSMatthew G Knepley 
151537d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
151637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15173a544194SStefano Zampini           fpdof = fdof-fcdof;
15183a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15193a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15203a544194SStefano Zampini             fieldNc[f] = 1;
15213a544194SStefano Zampini           }
15223a544194SStefano Zampini           fieldSizes[f] += fpdof;
152337d0c07bSMatthew G Knepley         }
152437d0c07bSMatthew G Knepley       }
152537d0c07bSMatthew G Knepley     }
152637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1527785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
152837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
152937d0c07bSMatthew G Knepley     }
153037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
153137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
153237d0c07bSMatthew G Knepley 
153337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
153437d0c07bSMatthew G Knepley       if (gdof > 0) {
153537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
153637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
153737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
153837d0c07bSMatthew G Knepley 
153937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
154037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
154137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
154237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
154337d0c07bSMatthew G Knepley           }
154437d0c07bSMatthew G Knepley         }
154537d0c07bSMatthew G Knepley       }
154637d0c07bSMatthew G Knepley     }
15478865f1eaSKarl Rupp     if (numFields) *numFields = nF;
154837d0c07bSMatthew G Knepley     if (fieldNames) {
1549785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
155037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
155137d0c07bSMatthew G Knepley         const char *fieldName;
155237d0c07bSMatthew G Knepley 
155337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
155437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
155537d0c07bSMatthew G Knepley       }
155637d0c07bSMatthew G Knepley     }
155737d0c07bSMatthew G Knepley     if (fields) {
1558785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
155937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15603a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15613a544194SStefano Zampini 
156282f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15633a544194SStefano Zampini         in[0] = -fieldNc[f];
15643a544194SStefano Zampini         in[1] = fieldNc[f];
15653a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15663a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15673a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
156837d0c07bSMatthew G Knepley       }
156937d0c07bSMatthew G Knepley     }
15703a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15718865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15728865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
157369ca1f37SDmitry Karpeev   }
15744d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15754d343eeaSMatthew G Knepley }
15764d343eeaSMatthew G Knepley 
157716621825SDmitry Karpeev 
157816621825SDmitry Karpeev /*@C
157916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
158016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
158116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1582e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1583e7c4fc90SDmitry Karpeev 
1584e7c4fc90SDmitry Karpeev   Not collective
1585e7c4fc90SDmitry Karpeev 
1586e7c4fc90SDmitry Karpeev   Input Parameter:
1587e7c4fc90SDmitry Karpeev . dm - the DM object
1588e7c4fc90SDmitry Karpeev 
1589e7c4fc90SDmitry Karpeev   Output Parameters:
15900298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15910298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15920298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15930298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1594e7c4fc90SDmitry Karpeev 
1595e7c4fc90SDmitry Karpeev   Level: intermediate
1596e7c4fc90SDmitry Karpeev 
1597e7c4fc90SDmitry Karpeev   Notes:
1598e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1599e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1600e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1601e7c4fc90SDmitry Karpeev 
1602e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1603e7c4fc90SDmitry Karpeev @*/
160416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1605e7c4fc90SDmitry Karpeev {
1606e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1607e7c4fc90SDmitry Karpeev 
1608e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1609e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16108865f1eaSKarl Rupp   if (len) {
16118865f1eaSKarl Rupp     PetscValidPointer(len,2);
16128865f1eaSKarl Rupp     *len = 0;
16138865f1eaSKarl Rupp   }
16148865f1eaSKarl Rupp   if (namelist) {
16158865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16168865f1eaSKarl Rupp     *namelist = 0;
16178865f1eaSKarl Rupp   }
16188865f1eaSKarl Rupp   if (islist) {
16198865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16208865f1eaSKarl Rupp     *islist = 0;
16218865f1eaSKarl Rupp   }
16228865f1eaSKarl Rupp   if (dmlist) {
16238865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16248865f1eaSKarl Rupp     *dmlist = 0;
16258865f1eaSKarl Rupp   }
1626f3f0edfdSDmitry Karpeev   /*
1627f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1628f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1629f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1630f3f0edfdSDmitry Karpeev    */
1631ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
163216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1633435a35e8SMatthew G Knepley     PetscSection section;
1634435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1635435a35e8SMatthew G Knepley 
1636e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1637435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1638435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1639f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
164003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
164103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
164203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1643435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1644435a35e8SMatthew G Knepley         const char *fieldName;
1645435a35e8SMatthew G Knepley 
164603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
164703dc3394SMatthew G. Knepley         if (namelist) {
1648435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1649435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1650435a35e8SMatthew G Knepley         }
165103dc3394SMatthew G. Knepley       }
1652435a35e8SMatthew G Knepley     } else {
165369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1654e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16550298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1656e7c4fc90SDmitry Karpeev     }
16578865f1eaSKarl Rupp   } else {
165816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
165916621825SDmitry Karpeev   }
166016621825SDmitry Karpeev   PetscFunctionReturn(0);
166116621825SDmitry Karpeev }
166216621825SDmitry Karpeev 
1663564cec59SMatthew G. Knepley /*@
1664435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1665435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1666435a35e8SMatthew G Knepley 
1667435a35e8SMatthew G Knepley   Not collective
1668435a35e8SMatthew G Knepley 
1669435a35e8SMatthew G Knepley   Input Parameters:
16702adcc780SMatthew G. Knepley + dm        - The DM object
16712adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16722adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1673435a35e8SMatthew G Knepley 
1674435a35e8SMatthew G Knepley   Output Parameters:
16752adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16762adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1677435a35e8SMatthew G Knepley 
16785d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16795d3b26e6SMatthew G. Knepley 
1680435a35e8SMatthew G Knepley   Level: intermediate
1681435a35e8SMatthew G Knepley 
16825d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1683435a35e8SMatthew G Knepley @*/
168437bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1685435a35e8SMatthew G Knepley {
1686435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1687435a35e8SMatthew G Knepley 
1688435a35e8SMatthew G Knepley   PetscFunctionBegin;
1689435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1690435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16918865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16928865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1693435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1694435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
169582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1696435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1697435a35e8SMatthew G Knepley }
1698435a35e8SMatthew G Knepley 
16992adcc780SMatthew G. Knepley /*@C
17002adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17012adcc780SMatthew G. Knepley 
17022adcc780SMatthew G. Knepley   Not collective
17032adcc780SMatthew G. Knepley 
17042adcc780SMatthew G. Knepley   Input Parameter:
17052adcc780SMatthew G. Knepley + dms - The DM objects
17062adcc780SMatthew G. Knepley - len - The number of DMs
17072adcc780SMatthew G. Knepley 
17082adcc780SMatthew G. Knepley   Output Parameters:
1709a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17102adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17112adcc780SMatthew G. Knepley 
17125d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17135d3b26e6SMatthew G. Knepley 
17142adcc780SMatthew G. Knepley   Level: intermediate
17152adcc780SMatthew G. Knepley 
17165d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17172adcc780SMatthew G. Knepley @*/
17182adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17192adcc780SMatthew G. Knepley {
17202adcc780SMatthew G. Knepley   PetscInt       i;
17212adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17222adcc780SMatthew G. Knepley 
17232adcc780SMatthew G. Knepley   PetscFunctionBegin;
17242adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17252adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17262adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1727a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17282adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17292adcc780SMatthew G. Knepley   if (len) {
1730a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1731a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17322adcc780SMatthew G. Knepley   }
17332adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17342adcc780SMatthew G. Knepley }
17352adcc780SMatthew G. Knepley 
173616621825SDmitry Karpeev 
173716621825SDmitry Karpeev /*@C
17388d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17398d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17408d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17418d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17428d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
174316621825SDmitry Karpeev 
174416621825SDmitry Karpeev   Not collective
174516621825SDmitry Karpeev 
174616621825SDmitry Karpeev   Input Parameter:
174716621825SDmitry Karpeev . dm - the DM object
174816621825SDmitry Karpeev 
174916621825SDmitry Karpeev   Output Parameters:
17500298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17510298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17520298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17530298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17540298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
175516621825SDmitry Karpeev 
175616621825SDmitry Karpeev   Level: intermediate
175716621825SDmitry Karpeev 
175816621825SDmitry Karpeev   Notes:
175916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
176016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
176116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
176216621825SDmitry Karpeev 
17638d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
176416621825SDmitry Karpeev @*/
17658d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
176616621825SDmitry Karpeev {
176716621825SDmitry Karpeev   PetscErrorCode      ierr;
1768be081cd6SPeter Brune   DMSubDomainHookLink link;
1769be081cd6SPeter Brune   PetscInt            i,l;
177016621825SDmitry Karpeev 
177116621825SDmitry Karpeev   PetscFunctionBegin;
177216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
177314a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17740298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17750298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17760298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17770298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1778f3f0edfdSDmitry Karpeev   /*
1779f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1780f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1781f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1782f3f0edfdSDmitry Karpeev    */
1783ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
178416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1785be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
178614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1787f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1788be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1789be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1790be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1791be081cd6SPeter Brune         }
1792648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1793e7c4fc90SDmitry Karpeev       }
179414a18fd3SPeter Brune     }
179514a18fd3SPeter Brune     if (len) *len = l;
179614a18fd3SPeter Brune   }
1797e30e807fSPeter Brune   PetscFunctionReturn(0);
1798e30e807fSPeter Brune }
1799e30e807fSPeter Brune 
1800e30e807fSPeter Brune 
1801e30e807fSPeter Brune /*@C
1802e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1803e30e807fSPeter Brune 
1804e30e807fSPeter Brune   Not collective
1805e30e807fSPeter Brune 
1806e30e807fSPeter Brune   Input Parameters:
1807e30e807fSPeter Brune + dm - the DM object
1808e30e807fSPeter Brune . n  - the number of subdomain scatters
1809e30e807fSPeter Brune - subdms - the local subdomains
1810e30e807fSPeter Brune 
1811e30e807fSPeter Brune   Output Parameters:
1812e30e807fSPeter Brune + n     - the number of scatters returned
1813e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1814e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1815e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1816e30e807fSPeter Brune 
181795452b02SPatrick Sanan   Notes:
181895452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1819e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1820e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1821e30e807fSPeter Brune   solution and residual data.
1822e30e807fSPeter Brune 
1823e30e807fSPeter Brune   Level: developer
1824e30e807fSPeter Brune 
1825e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1826e30e807fSPeter Brune @*/
1827e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1828e30e807fSPeter Brune {
1829e30e807fSPeter Brune   PetscErrorCode ierr;
1830e30e807fSPeter Brune 
1831e30e807fSPeter Brune   PetscFunctionBegin;
1832e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1833e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1834e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1835e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18366668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1837e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1838e7c4fc90SDmitry Karpeev }
1839e7c4fc90SDmitry Karpeev 
184047c6ae99SBarry Smith /*@
184147c6ae99SBarry Smith   DMRefine - Refines a DM object
184247c6ae99SBarry Smith 
184347c6ae99SBarry Smith   Collective on DM
184447c6ae99SBarry Smith 
184547c6ae99SBarry Smith   Input Parameter:
184647c6ae99SBarry Smith + dm   - the DM object
184791d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
184847c6ae99SBarry Smith 
184947c6ae99SBarry Smith   Output Parameter:
18500298fd71SBarry Smith . dmf - the refined DM, or NULL
1851ae0a1c52SMatthew G Knepley 
18520298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
185347c6ae99SBarry Smith 
185447c6ae99SBarry Smith   Level: developer
185547c6ae99SBarry Smith 
1856e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
185747c6ae99SBarry Smith @*/
18587087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
185947c6ae99SBarry Smith {
186047c6ae99SBarry Smith   PetscErrorCode   ierr;
1861c833c3b5SJed Brown   DMRefineHookLink link;
186247c6ae99SBarry Smith 
186347c6ae99SBarry Smith   PetscFunctionBegin;
1864732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18651ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1866fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
186747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18684057135bSMatthew G Knepley   if (*dmf) {
186943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18708865f1eaSKarl Rupp 
18718cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18728865f1eaSKarl Rupp 
1873644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18740598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1875656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18768865f1eaSKarl Rupp 
1877e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1878c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18798865f1eaSKarl Rupp       if (link->refinehook) {
18808865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18818865f1eaSKarl Rupp       }
1882c833c3b5SJed Brown     }
1883c833c3b5SJed Brown   }
18841ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1885c833c3b5SJed Brown   PetscFunctionReturn(0);
1886c833c3b5SJed Brown }
1887c833c3b5SJed Brown 
1888bb9467b5SJed Brown /*@C
1889c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1890c833c3b5SJed Brown 
1891c833c3b5SJed Brown    Logically Collective
1892c833c3b5SJed Brown 
1893c833c3b5SJed Brown    Input Arguments:
1894c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1895c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1896c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18970298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1898c833c3b5SJed Brown 
1899c833c3b5SJed Brown    Calling sequence of refinehook:
1900c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1901c833c3b5SJed Brown 
1902c833c3b5SJed Brown +  coarse - coarse level DM
1903c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1904c833c3b5SJed Brown -  ctx - optional user-defined function context
1905c833c3b5SJed Brown 
1906c833c3b5SJed Brown    Calling sequence for interphook:
1907c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1908c833c3b5SJed Brown 
1909c833c3b5SJed Brown +  coarse - coarse level DM
1910c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1911c833c3b5SJed Brown .  fine - fine level DM to update
1912c833c3b5SJed Brown -  ctx - optional user-defined function context
1913c833c3b5SJed Brown 
1914c833c3b5SJed Brown    Level: advanced
1915c833c3b5SJed Brown 
1916c833c3b5SJed Brown    Notes:
1917c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1918c833c3b5SJed Brown 
1919c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1920c833c3b5SJed Brown 
1921bb9467b5SJed Brown    This function is currently not available from Fortran.
1922bb9467b5SJed Brown 
1923c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1924c833c3b5SJed Brown @*/
1925c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1926c833c3b5SJed Brown {
1927c833c3b5SJed Brown   PetscErrorCode   ierr;
1928c833c3b5SJed Brown   DMRefineHookLink link,*p;
1929c833c3b5SJed Brown 
1930c833c3b5SJed Brown   PetscFunctionBegin;
1931c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19323d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19333d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19343d8e3701SJed Brown   }
193595dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1936c833c3b5SJed Brown   link->refinehook = refinehook;
1937c833c3b5SJed Brown   link->interphook = interphook;
1938c833c3b5SJed Brown   link->ctx        = ctx;
19390298fd71SBarry Smith   link->next       = NULL;
1940c833c3b5SJed Brown   *p               = link;
1941c833c3b5SJed Brown   PetscFunctionReturn(0);
1942c833c3b5SJed Brown }
1943c833c3b5SJed Brown 
19443d8e3701SJed Brown /*@C
19453d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19463d8e3701SJed Brown 
19473d8e3701SJed Brown    Logically Collective
19483d8e3701SJed Brown 
19493d8e3701SJed Brown    Input Arguments:
19503d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19513d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19523d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19533d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19543d8e3701SJed Brown 
19553d8e3701SJed Brown    Level: advanced
19563d8e3701SJed Brown 
19573d8e3701SJed Brown    Notes:
19583d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19593d8e3701SJed Brown 
19603d8e3701SJed Brown    This function is currently not available from Fortran.
19613d8e3701SJed Brown 
19623d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19633d8e3701SJed Brown @*/
19643d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19653d8e3701SJed Brown {
19663d8e3701SJed Brown   PetscErrorCode   ierr;
19673d8e3701SJed Brown   DMRefineHookLink link,*p;
19683d8e3701SJed Brown 
19693d8e3701SJed Brown   PetscFunctionBegin;
19703d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19713d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19723d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19733d8e3701SJed Brown       link = *p;
19743d8e3701SJed Brown       *p = link->next;
19753d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19763d8e3701SJed Brown       break;
19773d8e3701SJed Brown     }
19783d8e3701SJed Brown   }
19793d8e3701SJed Brown   PetscFunctionReturn(0);
19803d8e3701SJed Brown }
19813d8e3701SJed Brown 
1982c833c3b5SJed Brown /*@
1983c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1984c833c3b5SJed Brown 
1985c833c3b5SJed Brown    Collective if any hooks are
1986c833c3b5SJed Brown 
1987c833c3b5SJed Brown    Input Arguments:
1988c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1989e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1990c833c3b5SJed Brown -  fine - finer DM to update
1991c833c3b5SJed Brown 
1992c833c3b5SJed Brown    Level: developer
1993c833c3b5SJed Brown 
1994c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1995c833c3b5SJed Brown @*/
1996c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1997c833c3b5SJed Brown {
1998c833c3b5SJed Brown   PetscErrorCode   ierr;
1999c833c3b5SJed Brown   DMRefineHookLink link;
2000c833c3b5SJed Brown 
2001c833c3b5SJed Brown   PetscFunctionBegin;
2002c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20038865f1eaSKarl Rupp     if (link->interphook) {
20048865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20058865f1eaSKarl Rupp     }
20064057135bSMatthew G Knepley   }
200747c6ae99SBarry Smith   PetscFunctionReturn(0);
200847c6ae99SBarry Smith }
200947c6ae99SBarry Smith 
2010eb3f98d2SBarry Smith /*@
2011eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2012eb3f98d2SBarry Smith 
2013eb3f98d2SBarry Smith     Not Collective
2014eb3f98d2SBarry Smith 
2015eb3f98d2SBarry Smith     Input Parameter:
2016eb3f98d2SBarry Smith .   dm - the DM object
2017eb3f98d2SBarry Smith 
2018eb3f98d2SBarry Smith     Output Parameter:
2019eb3f98d2SBarry Smith .   level - number of refinements
2020eb3f98d2SBarry Smith 
2021eb3f98d2SBarry Smith     Level: developer
2022eb3f98d2SBarry Smith 
20236a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2024eb3f98d2SBarry Smith 
2025eb3f98d2SBarry Smith @*/
2026eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2027eb3f98d2SBarry Smith {
2028eb3f98d2SBarry Smith   PetscFunctionBegin;
2029eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2030eb3f98d2SBarry Smith   *level = dm->levelup;
2031eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2032eb3f98d2SBarry Smith }
2033eb3f98d2SBarry Smith 
2034fef3a512SBarry Smith /*@
2035fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2036fef3a512SBarry Smith 
2037fef3a512SBarry Smith     Not Collective
2038fef3a512SBarry Smith 
2039fef3a512SBarry Smith     Input Parameter:
2040fef3a512SBarry Smith +   dm - the DM object
2041fef3a512SBarry Smith -   level - number of refinements
2042fef3a512SBarry Smith 
2043fef3a512SBarry Smith     Level: advanced
2044fef3a512SBarry Smith 
204595452b02SPatrick Sanan     Notes:
204695452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2047fef3a512SBarry Smith 
2048fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2049fef3a512SBarry Smith 
2050fef3a512SBarry Smith @*/
2051fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2052fef3a512SBarry Smith {
2053fef3a512SBarry Smith   PetscFunctionBegin;
2054fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2055fef3a512SBarry Smith   dm->levelup = level;
2056fef3a512SBarry Smith   PetscFunctionReturn(0);
2057fef3a512SBarry Smith }
2058fef3a512SBarry Smith 
2059bb9467b5SJed Brown /*@C
2060baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2061baf369e7SPeter Brune 
2062baf369e7SPeter Brune    Logically Collective
2063baf369e7SPeter Brune 
2064baf369e7SPeter Brune    Input Arguments:
2065baf369e7SPeter Brune +  dm - the DM
2066baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2067baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20680298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2069baf369e7SPeter Brune 
2070baf369e7SPeter Brune    Calling sequence for beginhook:
2071baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2072baf369e7SPeter Brune 
2073baf369e7SPeter Brune +  dm - global DM
2074baf369e7SPeter Brune .  g - global vector
2075baf369e7SPeter Brune .  mode - mode
2076baf369e7SPeter Brune .  l - local vector
2077baf369e7SPeter Brune -  ctx - optional user-defined function context
2078baf369e7SPeter Brune 
2079baf369e7SPeter Brune 
2080baf369e7SPeter Brune    Calling sequence for endhook:
2081ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2082baf369e7SPeter Brune 
2083baf369e7SPeter Brune +  global - global DM
2084baf369e7SPeter Brune -  ctx - optional user-defined function context
2085baf369e7SPeter Brune 
2086baf369e7SPeter Brune    Level: advanced
2087baf369e7SPeter Brune 
2088baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2089baf369e7SPeter Brune @*/
2090baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2091baf369e7SPeter Brune {
2092baf369e7SPeter Brune   PetscErrorCode          ierr;
2093baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2094baf369e7SPeter Brune 
2095baf369e7SPeter Brune   PetscFunctionBegin;
2096baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2097baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
209895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2099baf369e7SPeter Brune   link->beginhook = beginhook;
2100baf369e7SPeter Brune   link->endhook   = endhook;
2101baf369e7SPeter Brune   link->ctx       = ctx;
21020298fd71SBarry Smith   link->next      = NULL;
2103baf369e7SPeter Brune   *p              = link;
2104baf369e7SPeter Brune   PetscFunctionReturn(0);
2105baf369e7SPeter Brune }
2106baf369e7SPeter Brune 
21074c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
21084c274da1SToby Isaac {
21094c274da1SToby Isaac   Mat cMat;
21104c274da1SToby Isaac   Vec cVec;
21114c274da1SToby Isaac   PetscSection section, cSec;
21124c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21134c274da1SToby Isaac   PetscErrorCode ierr;
21144c274da1SToby Isaac 
21154c274da1SToby Isaac   PetscFunctionBegin;
21164c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21174c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21184c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
21195db9a05bSToby Isaac     PetscInt nRows;
21205db9a05bSToby Isaac 
21215db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21225db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2123e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
21247711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21254c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
21264c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21274c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21284c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21294c274da1SToby Isaac       if (dof) {
21304c274da1SToby Isaac         PetscScalar *vals;
21314c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
21324c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
21334c274da1SToby Isaac       }
21344c274da1SToby Isaac     }
21354c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21364c274da1SToby Isaac   }
21374c274da1SToby Isaac   PetscFunctionReturn(0);
21384c274da1SToby Isaac }
21394c274da1SToby Isaac 
214047c6ae99SBarry Smith /*@
214101729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
214201729b5cSPatrick Sanan 
214301729b5cSPatrick Sanan     Neighbor-wise Collective on DM
214401729b5cSPatrick Sanan 
214501729b5cSPatrick Sanan     Input Parameters:
214601729b5cSPatrick Sanan +   dm - the DM object
214701729b5cSPatrick Sanan .   g - the global vector
214801729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
214901729b5cSPatrick Sanan -   l - the local vector
215001729b5cSPatrick Sanan 
215101729b5cSPatrick Sanan     Notes:
215201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
215301729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
215401729b5cSPatrick Sanan 
215501729b5cSPatrick Sanan     Level: beginner
215601729b5cSPatrick Sanan 
215701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
215801729b5cSPatrick Sanan 
215901729b5cSPatrick Sanan @*/
216001729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
216101729b5cSPatrick Sanan {
216201729b5cSPatrick Sanan   PetscErrorCode ierr;
216301729b5cSPatrick Sanan 
216401729b5cSPatrick Sanan   PetscFunctionBegin;
216501729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
216601729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
216701729b5cSPatrick Sanan   PetscFunctionReturn(0);
216801729b5cSPatrick Sanan }
216901729b5cSPatrick Sanan 
217001729b5cSPatrick Sanan /*@
217147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
217247c6ae99SBarry Smith 
217347c6ae99SBarry Smith     Neighbor-wise Collective on DM
217447c6ae99SBarry Smith 
217547c6ae99SBarry Smith     Input Parameters:
217647c6ae99SBarry Smith +   dm - the DM object
217747c6ae99SBarry Smith .   g - the global vector
217847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
217947c6ae99SBarry Smith -   l - the local vector
218047c6ae99SBarry Smith 
218101729b5cSPatrick Sanan     Level: intermediate
218247c6ae99SBarry Smith 
218301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
218447c6ae99SBarry Smith 
218547c6ae99SBarry Smith @*/
21867087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
218747c6ae99SBarry Smith {
21887128ae9fSMatthew G Knepley   PetscSF                 sf;
218947c6ae99SBarry Smith   PetscErrorCode          ierr;
2190baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
219147c6ae99SBarry Smith 
219247c6ae99SBarry Smith   PetscFunctionBegin;
2193171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2194baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
21958865f1eaSKarl Rupp     if (link->beginhook) {
21968865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
21978865f1eaSKarl Rupp     }
2198baf369e7SPeter Brune   }
21997128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22007128ae9fSMatthew G Knepley   if (sf) {
2201ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2202ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
22037128ae9fSMatthew G Knepley 
220482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22057128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2206ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22077128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22087128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2209ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22107128ae9fSMatthew G Knepley   } else {
2211843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22127128ae9fSMatthew G Knepley   }
221347c6ae99SBarry Smith   PetscFunctionReturn(0);
221447c6ae99SBarry Smith }
221547c6ae99SBarry Smith 
221647c6ae99SBarry Smith /*@
221747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
221847c6ae99SBarry Smith 
221947c6ae99SBarry Smith     Neighbor-wise Collective on DM
222047c6ae99SBarry Smith 
222147c6ae99SBarry Smith     Input Parameters:
222247c6ae99SBarry Smith +   dm - the DM object
222347c6ae99SBarry Smith .   g - the global vector
222447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
222547c6ae99SBarry Smith -   l - the local vector
222647c6ae99SBarry Smith 
222701729b5cSPatrick Sanan     Level: intermediate
222847c6ae99SBarry Smith 
222901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
223047c6ae99SBarry Smith 
223147c6ae99SBarry Smith @*/
22327087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
223347c6ae99SBarry Smith {
22347128ae9fSMatthew G Knepley   PetscSF                 sf;
223547c6ae99SBarry Smith   PetscErrorCode          ierr;
2236ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2237ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2238baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
223947c6ae99SBarry Smith 
224047c6ae99SBarry Smith   PetscFunctionBegin;
2241171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22427128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22437128ae9fSMatthew G Knepley   if (sf) {
224482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22457128ae9fSMatthew G Knepley 
22467128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2247ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22487128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22497128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2250ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22517128ae9fSMatthew G Knepley   } else {
2252843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22537128ae9fSMatthew G Knepley   }
22544c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2255baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2256baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2257baf369e7SPeter Brune   }
225847c6ae99SBarry Smith   PetscFunctionReturn(0);
225947c6ae99SBarry Smith }
226047c6ae99SBarry Smith 
2261d4d07f1eSToby Isaac /*@C
2262d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2263d4d07f1eSToby Isaac 
2264d4d07f1eSToby Isaac    Logically Collective
2265d4d07f1eSToby Isaac 
2266d4d07f1eSToby Isaac    Input Arguments:
2267d4d07f1eSToby Isaac +  dm - the DM
2268d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2269d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2270d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2271d4d07f1eSToby Isaac 
2272d4d07f1eSToby Isaac    Calling sequence for beginhook:
2273d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2274d4d07f1eSToby Isaac 
2275d4d07f1eSToby Isaac +  dm - global DM
2276d4d07f1eSToby Isaac .  l - local vector
2277d4d07f1eSToby Isaac .  mode - mode
2278d4d07f1eSToby Isaac .  g - global vector
2279d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2280d4d07f1eSToby Isaac 
2281d4d07f1eSToby Isaac 
2282d4d07f1eSToby Isaac    Calling sequence for endhook:
2283d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2284d4d07f1eSToby Isaac 
2285d4d07f1eSToby Isaac +  global - global DM
2286d4d07f1eSToby Isaac .  l - local vector
2287d4d07f1eSToby Isaac .  mode - mode
2288d4d07f1eSToby Isaac .  g - global vector
2289d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2290d4d07f1eSToby Isaac 
2291d4d07f1eSToby Isaac    Level: advanced
2292d4d07f1eSToby Isaac 
2293d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2294d4d07f1eSToby Isaac @*/
2295d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2296d4d07f1eSToby Isaac {
2297d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2298d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2299d4d07f1eSToby Isaac 
2300d4d07f1eSToby Isaac   PetscFunctionBegin;
2301d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2302d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
230395dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2304d4d07f1eSToby Isaac   link->beginhook = beginhook;
2305d4d07f1eSToby Isaac   link->endhook   = endhook;
2306d4d07f1eSToby Isaac   link->ctx       = ctx;
2307d4d07f1eSToby Isaac   link->next      = NULL;
2308d4d07f1eSToby Isaac   *p              = link;
2309d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2310d4d07f1eSToby Isaac }
2311d4d07f1eSToby Isaac 
23124c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
23134c274da1SToby Isaac {
23144c274da1SToby Isaac   Mat cMat;
23154c274da1SToby Isaac   Vec cVec;
23164c274da1SToby Isaac   PetscSection section, cSec;
23174c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23184c274da1SToby Isaac   PetscErrorCode ierr;
23194c274da1SToby Isaac 
23204c274da1SToby Isaac   PetscFunctionBegin;
23214c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23224c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23234c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
23245db9a05bSToby Isaac     PetscInt nRows;
23255db9a05bSToby Isaac 
23265db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23275db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2328e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
23297711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23304c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23314c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23324c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23334c274da1SToby Isaac       if (dof) {
23344c274da1SToby Isaac         PetscInt d;
23354c274da1SToby Isaac         PetscScalar *vals;
23364c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
23374c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
23384c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
23394c274da1SToby Isaac          * we just extracted */
23404c274da1SToby Isaac         for (d = 0; d < dof; d++) {
23414c274da1SToby Isaac           vals[d] = 0.;
23424c274da1SToby Isaac         }
23434c274da1SToby Isaac       }
23444c274da1SToby Isaac     }
23454c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
23464c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23474c274da1SToby Isaac   }
23484c274da1SToby Isaac   PetscFunctionReturn(0);
23494c274da1SToby Isaac }
235001729b5cSPatrick Sanan /*@
235101729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
235201729b5cSPatrick Sanan 
235301729b5cSPatrick Sanan     Neighbor-wise Collective on DM
235401729b5cSPatrick Sanan 
235501729b5cSPatrick Sanan     Input Parameters:
235601729b5cSPatrick Sanan +   dm - the DM object
235701729b5cSPatrick Sanan .   l - the local vector
235801729b5cSPatrick 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.
235901729b5cSPatrick Sanan -   g - the global vector
236001729b5cSPatrick Sanan 
236101729b5cSPatrick Sanan     Notes:
236201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
236301729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
236401729b5cSPatrick Sanan 
236501729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
236601729b5cSPatrick 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.
236701729b5cSPatrick Sanan 
236801729b5cSPatrick Sanan     Level: beginner
236901729b5cSPatrick Sanan 
237001729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
237101729b5cSPatrick Sanan 
237201729b5cSPatrick Sanan @*/
237301729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
237401729b5cSPatrick Sanan {
237501729b5cSPatrick Sanan   PetscErrorCode ierr;
237601729b5cSPatrick Sanan 
237701729b5cSPatrick Sanan   PetscFunctionBegin;
237801729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
237901729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
238001729b5cSPatrick Sanan   PetscFunctionReturn(0);
238101729b5cSPatrick Sanan }
23824c274da1SToby Isaac 
238347c6ae99SBarry Smith /*@
238401729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
23859a42bb27SBarry Smith 
23869a42bb27SBarry Smith     Neighbor-wise Collective on DM
23879a42bb27SBarry Smith 
23889a42bb27SBarry Smith     Input Parameters:
23899a42bb27SBarry Smith +   dm - the DM object
2390f6813fd5SJed Brown .   l - the local vector
23911eb28f2eSBarry 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.
23921eb28f2eSBarry Smith -   g - the global vector
23939a42bb27SBarry Smith 
239495452b02SPatrick Sanan     Notes:
239595452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
239684330215SMatthew 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.
23979a42bb27SBarry Smith 
239801729b5cSPatrick Sanan     Level: intermediate
23999a42bb27SBarry Smith 
240001729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
24019a42bb27SBarry Smith 
24029a42bb27SBarry Smith @*/
24037087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
24049a42bb27SBarry Smith {
24057128ae9fSMatthew G Knepley   PetscSF                 sf;
240684330215SMatthew G. Knepley   PetscSection            s, gs;
2407d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2408ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2409ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
241084330215SMatthew G. Knepley   PetscBool               isInsert;
241184330215SMatthew G. Knepley   PetscErrorCode          ierr;
24129a42bb27SBarry Smith 
24139a42bb27SBarry Smith   PetscFunctionBegin;
2414171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2415d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2416d4d07f1eSToby Isaac     if (link->beginhook) {
2417d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2418d4d07f1eSToby Isaac     }
2419d4d07f1eSToby Isaac   }
24204c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
24217128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2422e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24237128ae9fSMatthew G Knepley   switch (mode) {
24247128ae9fSMatthew G Knepley   case INSERT_VALUES:
24257128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2426304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
242784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24287128ae9fSMatthew G Knepley   case ADD_VALUES:
24297128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2430304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
243184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
24327128ae9fSMatthew G Knepley   default:
243382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24347128ae9fSMatthew G Knepley   }
243584330215SMatthew G. Knepley   if (sf && !isInsert) {
2436ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24377128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2438a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2439ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
244084330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
244184330215SMatthew G. Knepley   } else if (s && isInsert) {
244284330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
244384330215SMatthew G. Knepley 
2444e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
244584330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
244684330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2447ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
244884330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
244984330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2450b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
245184330215SMatthew G. Knepley 
245284330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
245303442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
245484330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2455b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
245684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
245784330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2458b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
245903442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2460b3b16f48SMatthew 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);
2461b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2462b3b16f48SMatthew G. Knepley       if (!gcdof) {
246384330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2464b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2465b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
246684330215SMatthew G. Knepley         const PetscInt *cdofs;
246784330215SMatthew G. Knepley         PetscInt        cind = 0;
246884330215SMatthew G. Knepley 
246984330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2470b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
247184330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2472b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
247384330215SMatthew G. Knepley         }
2474b3b16f48SMatthew 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);
247584330215SMatthew G. Knepley     }
2476ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24777128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
24787128ae9fSMatthew G Knepley   } else {
2479843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24807128ae9fSMatthew G Knepley   }
24819a42bb27SBarry Smith   PetscFunctionReturn(0);
24829a42bb27SBarry Smith }
24839a42bb27SBarry Smith 
24849a42bb27SBarry Smith /*@
24859a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
248647c6ae99SBarry Smith 
248747c6ae99SBarry Smith     Neighbor-wise Collective on DM
248847c6ae99SBarry Smith 
248947c6ae99SBarry Smith     Input Parameters:
249047c6ae99SBarry Smith +   dm - the DM object
2491f6813fd5SJed Brown .   l - the local vector
249247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2493f6813fd5SJed Brown -   g - the global vector
249447c6ae99SBarry Smith 
249501729b5cSPatrick Sanan     Level: intermediate
249647c6ae99SBarry Smith 
2497e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
249847c6ae99SBarry Smith 
249947c6ae99SBarry Smith @*/
25007087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
250147c6ae99SBarry Smith {
25027128ae9fSMatthew G Knepley   PetscSF                 sf;
250384330215SMatthew G. Knepley   PetscSection            s;
2504d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
250584330215SMatthew G. Knepley   PetscBool               isInsert;
250684330215SMatthew G. Knepley   PetscErrorCode          ierr;
250747c6ae99SBarry Smith 
250847c6ae99SBarry Smith   PetscFunctionBegin;
2509171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25107128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2511e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
25127128ae9fSMatthew G Knepley   switch (mode) {
25137128ae9fSMatthew G Knepley   case INSERT_VALUES:
25147128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
251584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25167128ae9fSMatthew G Knepley   case ADD_VALUES:
25177128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
251884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25197128ae9fSMatthew G Knepley   default:
252082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25217128ae9fSMatthew G Knepley   }
252284330215SMatthew G. Knepley   if (sf && !isInsert) {
2523ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2524ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
252584330215SMatthew G. Knepley 
2526ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
25277128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2528a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2529ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
25307128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
253184330215SMatthew G. Knepley   } else if (s && isInsert) {
25327128ae9fSMatthew G Knepley   } else {
2533843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
25347128ae9fSMatthew G Knepley   }
2535d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2536d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2537d4d07f1eSToby Isaac   }
253847c6ae99SBarry Smith   PetscFunctionReturn(0);
253947c6ae99SBarry Smith }
254047c6ae99SBarry Smith 
2541f089877aSRichard Tran Mills /*@
2542bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2543bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2544d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2545f089877aSRichard Tran Mills 
2546bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2547f089877aSRichard Tran Mills 
2548f089877aSRichard Tran Mills    Input Parameters:
2549f089877aSRichard Tran Mills +  dm - the DM object
2550bc0a1609SRichard Tran Mills .  g - the original local vector
2551bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2552f089877aSRichard Tran Mills 
2553bc0a1609SRichard Tran Mills    Output Parameter:
2554bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2555f089877aSRichard Tran Mills 
2556f089877aSRichard Tran Mills    Level: intermediate
2557f089877aSRichard Tran Mills 
2558bc0a1609SRichard Tran Mills    Notes:
2559bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2560bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2561bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2562bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2563bc0a1609SRichard Tran Mills 
2564bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2565bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2566f089877aSRichard Tran Mills 
2567f089877aSRichard Tran Mills @*/
2568f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2569f089877aSRichard Tran Mills {
2570f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2571f089877aSRichard Tran Mills 
2572f089877aSRichard Tran Mills   PetscFunctionBegin;
2573f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2574bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2575f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2576f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2577f089877aSRichard Tran Mills }
2578f089877aSRichard Tran Mills 
2579f089877aSRichard Tran Mills /*@
2580bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2581bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2582d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2583f089877aSRichard Tran Mills 
2584bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2585f089877aSRichard Tran Mills 
2586f089877aSRichard Tran Mills    Input Parameters:
2587bc0a1609SRichard Tran Mills +  da - the DM object
2588bc0a1609SRichard Tran Mills .  g - the original local vector
2589bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2590f089877aSRichard Tran Mills 
2591bc0a1609SRichard Tran Mills    Output Parameter:
2592bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2593f089877aSRichard Tran Mills 
2594f089877aSRichard Tran Mills    Level: intermediate
2595f089877aSRichard Tran Mills 
2596bc0a1609SRichard Tran Mills    Notes:
2597bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2598bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2599bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2600bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2601bc0a1609SRichard Tran Mills 
2602bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2603bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2604f089877aSRichard Tran Mills 
2605f089877aSRichard Tran Mills @*/
2606f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2607f089877aSRichard Tran Mills {
2608f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2609f089877aSRichard Tran Mills 
2610f089877aSRichard Tran Mills   PetscFunctionBegin;
2611f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2612bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2613f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2614f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2615f089877aSRichard Tran Mills }
2616f089877aSRichard Tran Mills 
2617f089877aSRichard Tran Mills 
261847c6ae99SBarry Smith /*@
261947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
262047c6ae99SBarry Smith 
262147c6ae99SBarry Smith     Collective on DM
262247c6ae99SBarry Smith 
262347c6ae99SBarry Smith     Input Parameter:
262447c6ae99SBarry Smith +   dm - the DM object
262591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
262647c6ae99SBarry Smith 
262747c6ae99SBarry Smith     Output Parameter:
262847c6ae99SBarry Smith .   dmc - the coarsened DM
262947c6ae99SBarry Smith 
263047c6ae99SBarry Smith     Level: developer
263147c6ae99SBarry Smith 
2632e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
263347c6ae99SBarry Smith 
263447c6ae99SBarry Smith @*/
26357087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
263647c6ae99SBarry Smith {
263747c6ae99SBarry Smith   PetscErrorCode    ierr;
2638b17ce1afSJed Brown   DMCoarsenHookLink link;
263947c6ae99SBarry Smith 
264047c6ae99SBarry Smith   PetscFunctionBegin;
2641171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2642fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
264347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
264447c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
26458892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2646a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
264743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
26488cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2649644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
26500598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2651656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2652e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2653b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2654b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2655b17ce1afSJed Brown   }
265647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2657b17ce1afSJed Brown   PetscFunctionReturn(0);
2658b17ce1afSJed Brown }
2659b17ce1afSJed Brown 
2660bb9467b5SJed Brown /*@C
2661b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2662b17ce1afSJed Brown 
2663b17ce1afSJed Brown    Logically Collective
2664b17ce1afSJed Brown 
2665b17ce1afSJed Brown    Input Arguments:
2666b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2667b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2668b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
26690298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2670b17ce1afSJed Brown 
2671b17ce1afSJed Brown    Calling sequence of coarsenhook:
2672b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2673b17ce1afSJed Brown 
2674b17ce1afSJed Brown +  fine - fine level DM
2675b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2676b17ce1afSJed Brown -  ctx - optional user-defined function context
2677b17ce1afSJed Brown 
2678b17ce1afSJed Brown    Calling sequence for restricthook:
2679c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2680b17ce1afSJed Brown 
2681b17ce1afSJed Brown +  fine - fine level DM
2682b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2683c833c3b5SJed Brown .  rscale - scaling vector for restriction
2684c833c3b5SJed Brown .  inject - matrix restricting by injection
2685b17ce1afSJed Brown .  coarse - coarse level DM to update
2686b17ce1afSJed Brown -  ctx - optional user-defined function context
2687b17ce1afSJed Brown 
2688b17ce1afSJed Brown    Level: advanced
2689b17ce1afSJed Brown 
2690b17ce1afSJed Brown    Notes:
2691b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2692b17ce1afSJed Brown 
2693b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2694b17ce1afSJed Brown 
2695b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2696b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2697b17ce1afSJed Brown 
2698bb9467b5SJed Brown    This function is currently not available from Fortran.
2699bb9467b5SJed Brown 
2700dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2701b17ce1afSJed Brown @*/
2702b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2703b17ce1afSJed Brown {
2704b17ce1afSJed Brown   PetscErrorCode    ierr;
2705b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2706b17ce1afSJed Brown 
2707b17ce1afSJed Brown   PetscFunctionBegin;
2708b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
27091e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
27101e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
27111e3d8eccSJed Brown   }
271295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2713b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2714b17ce1afSJed Brown   link->restricthook = restricthook;
2715b17ce1afSJed Brown   link->ctx          = ctx;
27160298fd71SBarry Smith   link->next         = NULL;
2717b17ce1afSJed Brown   *p                 = link;
2718b17ce1afSJed Brown   PetscFunctionReturn(0);
2719b17ce1afSJed Brown }
2720b17ce1afSJed Brown 
2721dc822a44SJed Brown /*@C
2722dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2723dc822a44SJed Brown 
2724dc822a44SJed Brown    Logically Collective
2725dc822a44SJed Brown 
2726dc822a44SJed Brown    Input Arguments:
2727dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2728dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2729dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2730dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2731dc822a44SJed Brown 
2732dc822a44SJed Brown    Level: advanced
2733dc822a44SJed Brown 
2734dc822a44SJed Brown    Notes:
2735dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2736dc822a44SJed Brown 
2737dc822a44SJed Brown    This function is currently not available from Fortran.
2738dc822a44SJed Brown 
2739dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2740dc822a44SJed Brown @*/
2741dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2742dc822a44SJed Brown {
2743dc822a44SJed Brown   PetscErrorCode    ierr;
2744dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2745dc822a44SJed Brown 
2746dc822a44SJed Brown   PetscFunctionBegin;
2747dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2748dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2749dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2750dc822a44SJed Brown       link = *p;
2751dc822a44SJed Brown       *p = link->next;
2752dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2753dc822a44SJed Brown       break;
2754dc822a44SJed Brown     }
2755dc822a44SJed Brown   }
2756dc822a44SJed Brown   PetscFunctionReturn(0);
2757dc822a44SJed Brown }
2758dc822a44SJed Brown 
2759dc822a44SJed Brown 
2760b17ce1afSJed Brown /*@
2761b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2762b17ce1afSJed Brown 
2763b17ce1afSJed Brown    Collective if any hooks are
2764b17ce1afSJed Brown 
2765b17ce1afSJed Brown    Input Arguments:
2766b17ce1afSJed Brown +  fine - finer DM to use as a base
2767b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2768e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2769b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2770e91eccc2SStefano Zampini -  coarse - coarser DM to update
2771b17ce1afSJed Brown 
2772b17ce1afSJed Brown    Level: developer
2773b17ce1afSJed Brown 
2774b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2775b17ce1afSJed Brown @*/
2776b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2777b17ce1afSJed Brown {
2778b17ce1afSJed Brown   PetscErrorCode    ierr;
2779b17ce1afSJed Brown   DMCoarsenHookLink link;
2780b17ce1afSJed Brown 
2781b17ce1afSJed Brown   PetscFunctionBegin;
2782b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
27838865f1eaSKarl Rupp     if (link->restricthook) {
27848865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
27858865f1eaSKarl Rupp     }
2786b17ce1afSJed Brown   }
278747c6ae99SBarry Smith   PetscFunctionReturn(0);
278847c6ae99SBarry Smith }
278947c6ae99SBarry Smith 
2790bb9467b5SJed Brown /*@C
2791be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
27925dbd56e3SPeter Brune 
27935dbd56e3SPeter Brune    Logically Collective
27945dbd56e3SPeter Brune 
27955dbd56e3SPeter Brune    Input Arguments:
27965dbd56e3SPeter Brune +  global - global DM
2797ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
27985dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
27990298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
28005dbd56e3SPeter Brune 
2801ec4806b8SPeter Brune 
2802ec4806b8SPeter Brune    Calling sequence for ddhook:
2803ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2804ec4806b8SPeter Brune 
2805ec4806b8SPeter Brune +  global - global DM
2806ec4806b8SPeter Brune .  block  - block DM
2807ec4806b8SPeter Brune -  ctx - optional user-defined function context
2808ec4806b8SPeter Brune 
28095dbd56e3SPeter Brune    Calling sequence for restricthook:
2810ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
28115dbd56e3SPeter Brune 
28125dbd56e3SPeter Brune +  global - global DM
28135dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
28145dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2815ec4806b8SPeter Brune .  block  - block DM
28165dbd56e3SPeter Brune -  ctx - optional user-defined function context
28175dbd56e3SPeter Brune 
28185dbd56e3SPeter Brune    Level: advanced
28195dbd56e3SPeter Brune 
28205dbd56e3SPeter Brune    Notes:
2821ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
28225dbd56e3SPeter Brune 
28235dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
28245dbd56e3SPeter Brune 
28255dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2826ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
28275dbd56e3SPeter Brune 
2828bb9467b5SJed Brown    This function is currently not available from Fortran.
2829bb9467b5SJed Brown 
28305dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
28315dbd56e3SPeter Brune @*/
2832be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
28335dbd56e3SPeter Brune {
28345dbd56e3SPeter Brune   PetscErrorCode      ierr;
2835be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
28365dbd56e3SPeter Brune 
28375dbd56e3SPeter Brune   PetscFunctionBegin;
28385dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2839b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2840b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2841b3a6b972SJed Brown   }
284295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
28435dbd56e3SPeter Brune   link->restricthook = restricthook;
2844be081cd6SPeter Brune   link->ddhook       = ddhook;
28455dbd56e3SPeter Brune   link->ctx          = ctx;
28460298fd71SBarry Smith   link->next         = NULL;
28475dbd56e3SPeter Brune   *p                 = link;
28485dbd56e3SPeter Brune   PetscFunctionReturn(0);
28495dbd56e3SPeter Brune }
28505dbd56e3SPeter Brune 
2851b3a6b972SJed Brown /*@C
2852b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2853b3a6b972SJed Brown 
2854b3a6b972SJed Brown    Logically Collective
2855b3a6b972SJed Brown 
2856b3a6b972SJed Brown    Input Arguments:
2857b3a6b972SJed Brown +  global - global DM
2858b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2859b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2860b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2861b3a6b972SJed Brown 
2862b3a6b972SJed Brown    Level: advanced
2863b3a6b972SJed Brown 
2864b3a6b972SJed Brown    Notes:
2865b3a6b972SJed Brown 
2866b3a6b972SJed Brown    This function is currently not available from Fortran.
2867b3a6b972SJed Brown 
2868b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2869b3a6b972SJed Brown @*/
2870b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2871b3a6b972SJed Brown {
2872b3a6b972SJed Brown   PetscErrorCode      ierr;
2873b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2874b3a6b972SJed Brown 
2875b3a6b972SJed Brown   PetscFunctionBegin;
2876b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2877b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2878b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2879b3a6b972SJed Brown       link = *p;
2880b3a6b972SJed Brown       *p = link->next;
2881b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2882b3a6b972SJed Brown       break;
2883b3a6b972SJed Brown     }
2884b3a6b972SJed Brown   }
2885b3a6b972SJed Brown   PetscFunctionReturn(0);
2886b3a6b972SJed Brown }
2887b3a6b972SJed Brown 
28885dbd56e3SPeter Brune /*@
2889be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
28905dbd56e3SPeter Brune 
28915dbd56e3SPeter Brune    Collective if any hooks are
28925dbd56e3SPeter Brune 
28935dbd56e3SPeter Brune    Input Arguments:
28945dbd56e3SPeter Brune +  fine - finer DM to use as a base
2895be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2896be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
28975dbd56e3SPeter Brune -  coarse - coarer DM to update
28985dbd56e3SPeter Brune 
28995dbd56e3SPeter Brune    Level: developer
29005dbd56e3SPeter Brune 
29015dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
29025dbd56e3SPeter Brune @*/
2903be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
29045dbd56e3SPeter Brune {
29055dbd56e3SPeter Brune   PetscErrorCode      ierr;
2906be081cd6SPeter Brune   DMSubDomainHookLink link;
29075dbd56e3SPeter Brune 
29085dbd56e3SPeter Brune   PetscFunctionBegin;
2909be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
29108865f1eaSKarl Rupp     if (link->restricthook) {
29118865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
29128865f1eaSKarl Rupp     }
29135dbd56e3SPeter Brune   }
29145dbd56e3SPeter Brune   PetscFunctionReturn(0);
29155dbd56e3SPeter Brune }
29165dbd56e3SPeter Brune 
29175fe1f584SPeter Brune /*@
29186a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
29195fe1f584SPeter Brune 
29205fe1f584SPeter Brune     Not Collective
29215fe1f584SPeter Brune 
29225fe1f584SPeter Brune     Input Parameter:
29235fe1f584SPeter Brune .   dm - the DM object
29245fe1f584SPeter Brune 
29255fe1f584SPeter Brune     Output Parameter:
29266a7d9d85SPeter Brune .   level - number of coarsenings
29275fe1f584SPeter Brune 
29285fe1f584SPeter Brune     Level: developer
29295fe1f584SPeter Brune 
29306a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
29315fe1f584SPeter Brune 
29325fe1f584SPeter Brune @*/
29335fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
29345fe1f584SPeter Brune {
29355fe1f584SPeter Brune   PetscFunctionBegin;
29365fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29375fe1f584SPeter Brune   *level = dm->leveldown;
29385fe1f584SPeter Brune   PetscFunctionReturn(0);
29395fe1f584SPeter Brune }
29405fe1f584SPeter Brune 
29415fe1f584SPeter Brune 
29425fe1f584SPeter Brune 
294347c6ae99SBarry Smith /*@C
294447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
294547c6ae99SBarry Smith 
294647c6ae99SBarry Smith     Collective on DM
294747c6ae99SBarry Smith 
294847c6ae99SBarry Smith     Input Parameter:
294947c6ae99SBarry Smith +   dm - the DM object
295047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
295147c6ae99SBarry Smith 
295247c6ae99SBarry Smith     Output Parameter:
295347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
295447c6ae99SBarry Smith 
295547c6ae99SBarry Smith     Level: developer
295647c6ae99SBarry Smith 
2957e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
295847c6ae99SBarry Smith 
295947c6ae99SBarry Smith @*/
29607087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
296147c6ae99SBarry Smith {
296247c6ae99SBarry Smith   PetscErrorCode ierr;
296347c6ae99SBarry Smith 
296447c6ae99SBarry Smith   PetscFunctionBegin;
2965171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2966ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
296747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
296847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
296947c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
297047c6ae99SBarry Smith   } else if (dm->ops->refine) {
297147c6ae99SBarry Smith     PetscInt i;
297247c6ae99SBarry Smith 
2973ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
297447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2975ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
297647c6ae99SBarry Smith     }
2977ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
297847c6ae99SBarry Smith   PetscFunctionReturn(0);
297947c6ae99SBarry Smith }
298047c6ae99SBarry Smith 
298147c6ae99SBarry Smith /*@C
298247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
298347c6ae99SBarry Smith 
298447c6ae99SBarry Smith     Collective on DM
298547c6ae99SBarry Smith 
298647c6ae99SBarry Smith     Input Parameter:
298747c6ae99SBarry Smith +   dm - the DM object
298847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
298947c6ae99SBarry Smith 
299047c6ae99SBarry Smith     Output Parameter:
299147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
299247c6ae99SBarry Smith 
299347c6ae99SBarry Smith     Level: developer
299447c6ae99SBarry Smith 
2995e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
299647c6ae99SBarry Smith 
299747c6ae99SBarry Smith @*/
29987087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
299947c6ae99SBarry Smith {
300047c6ae99SBarry Smith   PetscErrorCode ierr;
300147c6ae99SBarry Smith 
300247c6ae99SBarry Smith   PetscFunctionBegin;
3003171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3004ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
300547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
300647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
300747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
300847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
300947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
301047c6ae99SBarry Smith     PetscInt i;
301147c6ae99SBarry Smith 
3012ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
301347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3014ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
301547c6ae99SBarry Smith     }
3016ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
301747c6ae99SBarry Smith   PetscFunctionReturn(0);
301847c6ae99SBarry Smith }
301947c6ae99SBarry Smith 
302047c6ae99SBarry Smith /*@
3021e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
302247c6ae99SBarry Smith    grids associated with two DMs.
302347c6ae99SBarry Smith 
302447c6ae99SBarry Smith    Collective on DM
302547c6ae99SBarry Smith 
302647c6ae99SBarry Smith    Input Parameters:
302747c6ae99SBarry Smith +  dmc - the coarse grid DM
302847c6ae99SBarry Smith -  dmf - the fine grid DM
302947c6ae99SBarry Smith 
303047c6ae99SBarry Smith    Output Parameters:
303147c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
303247c6ae99SBarry Smith 
303347c6ae99SBarry Smith    Level: intermediate
303447c6ae99SBarry Smith 
303547c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
303647c6ae99SBarry Smith 
3037e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
303847c6ae99SBarry Smith @*/
3039e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
304047c6ae99SBarry Smith {
304147c6ae99SBarry Smith   PetscErrorCode ierr;
304247c6ae99SBarry Smith 
304347c6ae99SBarry Smith   PetscFunctionBegin;
3044171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3045171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
304647c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
304747c6ae99SBarry Smith   PetscFunctionReturn(0);
304847c6ae99SBarry Smith }
304947c6ae99SBarry Smith 
30501a266240SBarry Smith /*@C
30511a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
30521a266240SBarry Smith 
30531a266240SBarry Smith     Not Collective
30541a266240SBarry Smith 
30551a266240SBarry Smith     Input Parameters:
30561a266240SBarry Smith +   dm - the DM object
30571a266240SBarry Smith -   destroy - the destroy function
30581a266240SBarry Smith 
30591a266240SBarry Smith     Level: intermediate
30601a266240SBarry Smith 
3061e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
30621a266240SBarry Smith 
3063f07f9ceaSJed Brown @*/
30641a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
30651a266240SBarry Smith {
30661a266240SBarry Smith   PetscFunctionBegin;
3067171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30681a266240SBarry Smith   dm->ctxdestroy = destroy;
30691a266240SBarry Smith   PetscFunctionReturn(0);
30701a266240SBarry Smith }
30711a266240SBarry Smith 
3072b07ff414SBarry Smith /*@
30731b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
307447c6ae99SBarry Smith 
307547c6ae99SBarry Smith     Not Collective
307647c6ae99SBarry Smith 
307747c6ae99SBarry Smith     Input Parameters:
307847c6ae99SBarry Smith +   dm - the DM object
307947c6ae99SBarry Smith -   ctx - the user context
308047c6ae99SBarry Smith 
308147c6ae99SBarry Smith     Level: intermediate
308247c6ae99SBarry Smith 
3083e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
308447c6ae99SBarry Smith 
308547c6ae99SBarry Smith @*/
30861b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
308747c6ae99SBarry Smith {
308847c6ae99SBarry Smith   PetscFunctionBegin;
3089171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
309047c6ae99SBarry Smith   dm->ctx = ctx;
309147c6ae99SBarry Smith   PetscFunctionReturn(0);
309247c6ae99SBarry Smith }
309347c6ae99SBarry Smith 
309447c6ae99SBarry Smith /*@
30951b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
309647c6ae99SBarry Smith 
309747c6ae99SBarry Smith     Not Collective
309847c6ae99SBarry Smith 
309947c6ae99SBarry Smith     Input Parameter:
310047c6ae99SBarry Smith .   dm - the DM object
310147c6ae99SBarry Smith 
310247c6ae99SBarry Smith     Output Parameter:
310347c6ae99SBarry Smith .   ctx - the user context
310447c6ae99SBarry Smith 
310547c6ae99SBarry Smith     Level: intermediate
310647c6ae99SBarry Smith 
3107e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
310847c6ae99SBarry Smith 
310947c6ae99SBarry Smith @*/
31101b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
311147c6ae99SBarry Smith {
311247c6ae99SBarry Smith   PetscFunctionBegin;
3113171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31141b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
311547c6ae99SBarry Smith   PetscFunctionReturn(0);
311647c6ae99SBarry Smith }
311747c6ae99SBarry Smith 
311808da532bSDmitry Karpeev /*@C
3119df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
312008da532bSDmitry Karpeev 
312108da532bSDmitry Karpeev     Logically Collective on DM
312208da532bSDmitry Karpeev 
312308da532bSDmitry Karpeev     Input Parameter:
312408da532bSDmitry Karpeev +   dm - the DM object
31250298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
312608da532bSDmitry Karpeev 
312708da532bSDmitry Karpeev     Level: intermediate
312808da532bSDmitry Karpeev 
3129835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
313008da532bSDmitry Karpeev          DMSetJacobian()
313108da532bSDmitry Karpeev 
313208da532bSDmitry Karpeev @*/
313308da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
313408da532bSDmitry Karpeev {
313508da532bSDmitry Karpeev   PetscFunctionBegin;
313608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
313708da532bSDmitry Karpeev   PetscFunctionReturn(0);
313808da532bSDmitry Karpeev }
313908da532bSDmitry Karpeev 
314008da532bSDmitry Karpeev /*@
314108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
314208da532bSDmitry Karpeev 
314308da532bSDmitry Karpeev     Not Collective
314408da532bSDmitry Karpeev 
314508da532bSDmitry Karpeev     Input Parameter:
314608da532bSDmitry Karpeev .   dm - the DM object to destroy
314708da532bSDmitry Karpeev 
314808da532bSDmitry Karpeev     Output Parameter:
314908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
315008da532bSDmitry Karpeev 
315108da532bSDmitry Karpeev     Level: developer
315208da532bSDmitry Karpeev 
315374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
315408da532bSDmitry Karpeev 
315508da532bSDmitry Karpeev @*/
315608da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
315708da532bSDmitry Karpeev {
315808da532bSDmitry Karpeev   PetscFunctionBegin;
315908da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
316008da532bSDmitry Karpeev   PetscFunctionReturn(0);
316108da532bSDmitry Karpeev }
316208da532bSDmitry Karpeev 
316308da532bSDmitry Karpeev /*@C
316408da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
316508da532bSDmitry Karpeev 
316608da532bSDmitry Karpeev     Logically Collective on DM
316708da532bSDmitry Karpeev 
316808da532bSDmitry Karpeev     Input Parameters:
3169907376e6SBarry Smith .   dm - the DM object
317008da532bSDmitry Karpeev 
317108da532bSDmitry Karpeev     Output parameters:
317208da532bSDmitry Karpeev +   xl - lower bound
317308da532bSDmitry Karpeev -   xu - upper bound
317408da532bSDmitry Karpeev 
3175907376e6SBarry Smith     Level: advanced
3176907376e6SBarry Smith 
317795452b02SPatrick Sanan     Notes:
317895452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
317908da532bSDmitry Karpeev 
318074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
318108da532bSDmitry Karpeev 
318208da532bSDmitry Karpeev @*/
318308da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
318408da532bSDmitry Karpeev {
318508da532bSDmitry Karpeev   PetscErrorCode ierr;
31865fd66863SKarl Rupp 
318708da532bSDmitry Karpeev   PetscFunctionBegin;
318808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
318908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
319008da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
319108da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
31928865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
319308da532bSDmitry Karpeev   PetscFunctionReturn(0);
319408da532bSDmitry Karpeev }
319508da532bSDmitry Karpeev 
3196b0ae01b7SPeter Brune /*@
3197b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3198b0ae01b7SPeter Brune 
3199b0ae01b7SPeter Brune     Not Collective
3200b0ae01b7SPeter Brune 
3201b0ae01b7SPeter Brune     Input Parameter:
3202b0ae01b7SPeter Brune .   dm - the DM object
3203b0ae01b7SPeter Brune 
3204b0ae01b7SPeter Brune     Output Parameter:
3205b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3206b0ae01b7SPeter Brune 
3207b0ae01b7SPeter Brune     Level: developer
3208b0ae01b7SPeter Brune 
3209b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3210b0ae01b7SPeter Brune 
3211b0ae01b7SPeter Brune @*/
3212b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3213b0ae01b7SPeter Brune {
3214b0ae01b7SPeter Brune   PetscFunctionBegin;
3215b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3216b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3217b0ae01b7SPeter Brune }
3218b0ae01b7SPeter Brune 
32193ad4599aSBarry Smith /*@
32203ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
32213ad4599aSBarry Smith 
32223ad4599aSBarry Smith     Not Collective
32233ad4599aSBarry Smith 
32243ad4599aSBarry Smith     Input Parameter:
32253ad4599aSBarry Smith .   dm - the DM object
32263ad4599aSBarry Smith 
32273ad4599aSBarry Smith     Output Parameter:
32283ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
32293ad4599aSBarry Smith 
32303ad4599aSBarry Smith     Level: developer
32313ad4599aSBarry Smith 
32323ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
32333ad4599aSBarry Smith 
32343ad4599aSBarry Smith @*/
32353ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
32363ad4599aSBarry Smith {
32373ad4599aSBarry Smith   PetscFunctionBegin;
32383ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
32393ad4599aSBarry Smith   PetscFunctionReturn(0);
32403ad4599aSBarry Smith }
32413ad4599aSBarry Smith 
3242a7058e45SLawrence Mitchell 
3243a7058e45SLawrence Mitchell /*@
3244a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3245a7058e45SLawrence Mitchell 
3246a7058e45SLawrence Mitchell     Not Collective
3247a7058e45SLawrence Mitchell 
3248a7058e45SLawrence Mitchell     Input Parameter:
3249a7058e45SLawrence Mitchell .   dm - the DM object
3250a7058e45SLawrence Mitchell 
3251a7058e45SLawrence Mitchell     Output Parameter:
3252a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3253a7058e45SLawrence Mitchell 
3254a7058e45SLawrence Mitchell     Level: developer
3255a7058e45SLawrence Mitchell 
3256a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3257a7058e45SLawrence Mitchell 
3258a7058e45SLawrence Mitchell @*/
3259a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3260a7058e45SLawrence Mitchell {
32614a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3262a7058e45SLawrence Mitchell   PetscFunctionBegin;
32634a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
32644a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3265a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3266a7058e45SLawrence Mitchell }
3267a7058e45SLawrence Mitchell 
3268748fac09SDmitry Karpeev /*@C
326908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
327008da532bSDmitry Karpeev 
327108da532bSDmitry Karpeev     Collective on DM
327208da532bSDmitry Karpeev 
327308da532bSDmitry Karpeev     Input Parameter:
327408da532bSDmitry Karpeev +   dm - the DM object
32750298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
327608da532bSDmitry Karpeev 
327708da532bSDmitry Karpeev     Level: developer
327808da532bSDmitry Karpeev 
327974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
328008da532bSDmitry Karpeev 
328108da532bSDmitry Karpeev @*/
328208da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
328308da532bSDmitry Karpeev {
328408da532bSDmitry Karpeev   PetscErrorCode ierr;
32855fd66863SKarl Rupp 
328608da532bSDmitry Karpeev   PetscFunctionBegin;
328708da532bSDmitry Karpeev   if (x) {
328808da532bSDmitry Karpeev     if (!dm->x) {
328908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
329008da532bSDmitry Karpeev     }
329108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
32928865f1eaSKarl Rupp   } else if (dm->x) {
329308da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
329408da532bSDmitry Karpeev   }
329508da532bSDmitry Karpeev   PetscFunctionReturn(0);
329608da532bSDmitry Karpeev }
329708da532bSDmitry Karpeev 
32980298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3299264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3300264ace61SBarry Smith 
3301264ace61SBarry Smith /*@C
3302264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3303264ace61SBarry Smith 
3304264ace61SBarry Smith   Collective on DM
3305264ace61SBarry Smith 
3306264ace61SBarry Smith   Input Parameters:
3307264ace61SBarry Smith + dm     - The DM object
3308264ace61SBarry Smith - method - The name of the DM type
3309264ace61SBarry Smith 
3310264ace61SBarry Smith   Options Database Key:
3311264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3312264ace61SBarry Smith 
3313264ace61SBarry Smith   Notes:
3314e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3315264ace61SBarry Smith 
3316264ace61SBarry Smith   Level: intermediate
3317264ace61SBarry Smith 
3318264ace61SBarry Smith .keywords: DM, set, type
3319264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3320264ace61SBarry Smith @*/
332119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3322264ace61SBarry Smith {
3323264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3324264ace61SBarry Smith   PetscBool      match;
3325264ace61SBarry Smith   PetscErrorCode ierr;
3326264ace61SBarry Smith 
3327264ace61SBarry Smith   PetscFunctionBegin;
3328264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3329251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3330264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3331264ace61SBarry Smith 
33320f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
33331c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3334ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3335264ace61SBarry Smith 
3336264ace61SBarry Smith   if (dm->ops->destroy) {
3337264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
33380298fd71SBarry Smith     dm->ops->destroy = NULL;
3339264ace61SBarry Smith   }
3340264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3341264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3342264ace61SBarry Smith   PetscFunctionReturn(0);
3343264ace61SBarry Smith }
3344264ace61SBarry Smith 
3345264ace61SBarry Smith /*@C
3346264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3347264ace61SBarry Smith 
3348264ace61SBarry Smith   Not Collective
3349264ace61SBarry Smith 
3350264ace61SBarry Smith   Input Parameter:
3351264ace61SBarry Smith . dm  - The DM
3352264ace61SBarry Smith 
3353264ace61SBarry Smith   Output Parameter:
3354264ace61SBarry Smith . type - The DM type name
3355264ace61SBarry Smith 
3356264ace61SBarry Smith   Level: intermediate
3357264ace61SBarry Smith 
3358264ace61SBarry Smith .keywords: DM, get, type, name
3359264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3360264ace61SBarry Smith @*/
336119fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3362264ace61SBarry Smith {
3363264ace61SBarry Smith   PetscErrorCode ierr;
3364264ace61SBarry Smith 
3365264ace61SBarry Smith   PetscFunctionBegin;
3366264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3367c959eef4SJed Brown   PetscValidPointer(type,2);
3368607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3369264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3370264ace61SBarry Smith   PetscFunctionReturn(0);
3371264ace61SBarry Smith }
3372264ace61SBarry Smith 
337367a56275SMatthew G Knepley /*@C
337467a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
337567a56275SMatthew G Knepley 
337667a56275SMatthew G Knepley   Collective on DM
337767a56275SMatthew G Knepley 
337867a56275SMatthew G Knepley   Input Parameters:
337967a56275SMatthew G Knepley + dm - the DM
338067a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
338167a56275SMatthew G Knepley 
338267a56275SMatthew G Knepley   Output Parameter:
338367a56275SMatthew G Knepley . M - pointer to new DM
338467a56275SMatthew G Knepley 
338567a56275SMatthew G Knepley   Notes:
338667a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
338767a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
338867a56275SMatthew G Knepley   of the input DM.
338967a56275SMatthew G Knepley 
339067a56275SMatthew G Knepley   Level: intermediate
339167a56275SMatthew G Knepley 
339267a56275SMatthew G Knepley .seealso: DMCreate()
339367a56275SMatthew G Knepley @*/
339419fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
339567a56275SMatthew G Knepley {
339667a56275SMatthew G Knepley   DM             B;
339767a56275SMatthew G Knepley   char           convname[256];
3398c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
339967a56275SMatthew G Knepley   PetscErrorCode ierr;
340067a56275SMatthew G Knepley 
340167a56275SMatthew G Knepley   PetscFunctionBegin;
340267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
340367a56275SMatthew G Knepley   PetscValidType(dm,1);
340467a56275SMatthew G Knepley   PetscValidPointer(M,3);
3405251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3406c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3407c067b6caSMatthew G. Knepley   if (sametype) {
3408c067b6caSMatthew G. Knepley     *M   = dm;
3409c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3410c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3411c067b6caSMatthew G. Knepley   } else {
34120298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
341367a56275SMatthew G Knepley 
341467a56275SMatthew G Knepley     /*
341567a56275SMatthew G Knepley        Order of precedence:
341667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
341767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
341867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
341967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
342067a56275SMatthew G Knepley        5) Use a really basic converter.
342167a56275SMatthew G Knepley     */
342267a56275SMatthew G Knepley 
342367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3424a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3425a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3426a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3427a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3428a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34290005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
343067a56275SMatthew G Knepley     if (conv) goto foundconv;
343167a56275SMatthew G Knepley 
343267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
343382f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
343467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3435a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3436a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3437a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3438a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3439a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34400005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
344167a56275SMatthew G Knepley     if (conv) {
3442fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
344367a56275SMatthew G Knepley       goto foundconv;
344467a56275SMatthew G Knepley     }
344567a56275SMatthew G Knepley 
344667a56275SMatthew G Knepley #if 0
344767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
344867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3449fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
345067a56275SMatthew G Knepley     if (conv) goto foundconv;
345167a56275SMatthew G Knepley 
345267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
345367a56275SMatthew G Knepley     if (dm->ops->convert) {
345467a56275SMatthew G Knepley       conv = dm->ops->convert;
345567a56275SMatthew G Knepley     }
345667a56275SMatthew G Knepley     if (conv) goto foundconv;
345767a56275SMatthew G Knepley #endif
345867a56275SMatthew G Knepley 
345967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
346082f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
346167a56275SMatthew G Knepley 
346267a56275SMatthew G Knepley foundconv:
346367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
346467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
346512fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
346690b157c4SStefano Zampini     {
346790b157c4SStefano Zampini       PetscBool             isper;
346812fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
346912fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
347090b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
347190b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
347212fa691eSMatthew G. Knepley     }
347367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
347467a56275SMatthew G Knepley   }
347567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
347667a56275SMatthew G Knepley   PetscFunctionReturn(0);
347767a56275SMatthew G Knepley }
3478264ace61SBarry Smith 
3479264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3480264ace61SBarry Smith 
3481264ace61SBarry Smith /*@C
34821c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
34831c84c290SBarry Smith 
34841c84c290SBarry Smith   Not Collective
34851c84c290SBarry Smith 
34861c84c290SBarry Smith   Input Parameters:
34871c84c290SBarry Smith + name        - The name of a new user-defined creation routine
34881c84c290SBarry Smith - create_func - The creation routine itself
34891c84c290SBarry Smith 
34901c84c290SBarry Smith   Notes:
34911c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
34921c84c290SBarry Smith 
34931c84c290SBarry Smith 
34941c84c290SBarry Smith   Sample usage:
34951c84c290SBarry Smith .vb
3496bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
34971c84c290SBarry Smith .ve
34981c84c290SBarry Smith 
34991c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
35001c84c290SBarry Smith .vb
35011c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
35021c84c290SBarry Smith     DMSetType(DM,"my_da");
35031c84c290SBarry Smith .ve
35041c84c290SBarry Smith    or at runtime via the option
35051c84c290SBarry Smith .vb
35061c84c290SBarry Smith     -da_type my_da
35071c84c290SBarry Smith .ve
3508264ace61SBarry Smith 
3509264ace61SBarry Smith   Level: advanced
35101c84c290SBarry Smith 
35111c84c290SBarry Smith .keywords: DM, register
3512bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
35131c84c290SBarry Smith 
3514264ace61SBarry Smith @*/
3515bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3516264ace61SBarry Smith {
3517264ace61SBarry Smith   PetscErrorCode ierr;
3518264ace61SBarry Smith 
3519264ace61SBarry Smith   PetscFunctionBegin;
35201d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3521a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3522264ace61SBarry Smith   PetscFunctionReturn(0);
3523264ace61SBarry Smith }
3524264ace61SBarry Smith 
3525b859378eSBarry Smith /*@C
352655849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3527b859378eSBarry Smith 
3528b859378eSBarry Smith   Collective on PetscViewer
3529b859378eSBarry Smith 
3530b859378eSBarry Smith   Input Parameters:
3531b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3532b859378eSBarry Smith            some related function before a call to DMLoad().
3533b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3534b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3535b859378eSBarry Smith 
3536b859378eSBarry Smith    Level: intermediate
3537b859378eSBarry Smith 
3538b859378eSBarry Smith   Notes:
353955849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3540b859378eSBarry Smith 
3541b859378eSBarry Smith   Notes for advanced users:
3542b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3543b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3544b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3545b859378eSBarry Smith   format is
3546b859378eSBarry Smith .vb
3547b859378eSBarry Smith      has not yet been determined
3548b859378eSBarry Smith .ve
3549b859378eSBarry Smith 
3550b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3551b859378eSBarry Smith @*/
3552b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3553b859378eSBarry Smith {
35549331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3555b859378eSBarry Smith   PetscErrorCode ierr;
3556b859378eSBarry Smith 
3557b859378eSBarry Smith   PetscFunctionBegin;
3558b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3559b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
356032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
35619331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
35629331c7a4SMatthew G. Knepley   if (isbinary) {
35639331c7a4SMatthew G. Knepley     PetscInt classid;
35649331c7a4SMatthew G. Knepley     char     type[256];
3565b859378eSBarry Smith 
3566060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
35679200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3568060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
356932c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
35709331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35719331c7a4SMatthew G. Knepley   } else if (ishdf5) {
35729331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35739331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3574b859378eSBarry Smith   PetscFunctionReturn(0);
3575b859378eSBarry Smith }
3576b859378eSBarry Smith 
35777da65231SMatthew G Knepley /******************************** FEM Support **********************************/
35787da65231SMatthew G Knepley 
3579a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3580a6dfd86eSKarl Rupp {
35811d47ebbbSSatish Balay   PetscInt       f;
35821b30c384SMatthew G Knepley   PetscErrorCode ierr;
35831b30c384SMatthew G Knepley 
35847da65231SMatthew G Knepley   PetscFunctionBegin;
358574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35861d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
358757622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
35887da65231SMatthew G Knepley   }
35897da65231SMatthew G Knepley   PetscFunctionReturn(0);
35907da65231SMatthew G Knepley }
35917da65231SMatthew G Knepley 
3592a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3593a6dfd86eSKarl Rupp {
35941b30c384SMatthew G Knepley   PetscInt       f, g;
35957da65231SMatthew G Knepley   PetscErrorCode ierr;
35967da65231SMatthew G Knepley 
35977da65231SMatthew G Knepley   PetscFunctionBegin;
359874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35991d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
360074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
36011d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3602e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
36037da65231SMatthew G Knepley     }
360474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
36057da65231SMatthew G Knepley   }
36067da65231SMatthew G Knepley   PetscFunctionReturn(0);
36077da65231SMatthew G Knepley }
3608e7c4fc90SDmitry Karpeev 
36096113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3610e759306cSMatthew G. Knepley {
36110c5b8624SToby Isaac   PetscInt          localSize, bs;
36120c5b8624SToby Isaac   PetscMPIInt       size;
36130c5b8624SToby Isaac   Vec               x, xglob;
36140c5b8624SToby Isaac   const PetscScalar *xarray;
3615e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3616e759306cSMatthew G. Knepley 
3617e759306cSMatthew G. Knepley   PetscFunctionBegin;
36189852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3619e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3620e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
36216113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
36220c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
36230c5b8624SToby Isaac   if (size > 1) {
36240c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
36250c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
36260c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
36270c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
36280c5b8624SToby Isaac   } else {
36290c5b8624SToby Isaac     xglob = x;
36300c5b8624SToby Isaac   }
36310c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
36320c5b8624SToby Isaac   if (size > 1) {
36330c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
36340c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
36350c5b8624SToby Isaac   }
3636e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3637e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3638e759306cSMatthew G. Knepley }
3639e759306cSMatthew G. Knepley 
364088ed4aceSMatthew G Knepley /*@
3641e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
364288ed4aceSMatthew G Knepley 
364388ed4aceSMatthew G Knepley   Input Parameter:
364488ed4aceSMatthew G Knepley . dm - The DM
364588ed4aceSMatthew G Knepley 
364688ed4aceSMatthew G Knepley   Output Parameter:
364788ed4aceSMatthew G Knepley . section - The PetscSection
364888ed4aceSMatthew G Knepley 
3649e5893cccSMatthew G. Knepley   Options Database Keys:
3650e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3651e5893cccSMatthew G. Knepley 
365288ed4aceSMatthew G Knepley   Level: intermediate
365388ed4aceSMatthew G Knepley 
365488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
365588ed4aceSMatthew G Knepley 
3656e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
365788ed4aceSMatthew G Knepley @*/
3658e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
36590adebc6cSBarry Smith {
3660fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3661fd59a867SMatthew G. Knepley 
366288ed4aceSMatthew G Knepley   PetscFunctionBegin;
366388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
366488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
36652f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
36662f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3667ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
36682f0f8703SMatthew G. Knepley   }
366988ed4aceSMatthew G Knepley   *section = dm->defaultSection;
367088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
367188ed4aceSMatthew G Knepley }
367288ed4aceSMatthew G Knepley 
367388ed4aceSMatthew G Knepley /*@
3674e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
367588ed4aceSMatthew G Knepley 
367688ed4aceSMatthew G Knepley   Input Parameters:
367788ed4aceSMatthew G Knepley + dm - The DM
367888ed4aceSMatthew G Knepley - section - The PetscSection
367988ed4aceSMatthew G Knepley 
368088ed4aceSMatthew G Knepley   Level: intermediate
368188ed4aceSMatthew G Knepley 
368288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
368388ed4aceSMatthew G Knepley 
3684e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
368588ed4aceSMatthew G Knepley @*/
3686e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
36870adebc6cSBarry Smith {
3688c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3689af122d2aSMatthew G Knepley   PetscInt       f;
369088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
369188ed4aceSMatthew G Knepley 
369288ed4aceSMatthew G Knepley   PetscFunctionBegin;
369388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3694c473ab19SMatthew G. Knepley   if (section) {
36951d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36961d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3697c473ab19SMatthew G. Knepley   }
369888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
369988ed4aceSMatthew G Knepley   dm->defaultSection = section;
3700c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3701af122d2aSMatthew G Knepley   if (numFields) {
3702af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3703af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
37040f21e855SMatthew G. Knepley       PetscObject disc;
3705af122d2aSMatthew G Knepley       const char *name;
3706af122d2aSMatthew G Knepley 
3707af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
37080f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
37090f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3710af122d2aSMatthew G Knepley     }
3711af122d2aSMatthew G Knepley   }
3712e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
37131d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
371488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
371588ed4aceSMatthew G Knepley }
371688ed4aceSMatthew G Knepley 
37179435951eSToby Isaac /*@
37189435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
37199435951eSToby Isaac 
3720e228b242SToby Isaac   not collective
3721e228b242SToby Isaac 
37229435951eSToby Isaac   Input Parameter:
37239435951eSToby Isaac . dm - The DM
37249435951eSToby Isaac 
37259435951eSToby Isaac   Output Parameter:
37269435951eSToby 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.
37279435951eSToby 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.
37289435951eSToby Isaac 
37299435951eSToby Isaac   Level: advanced
37309435951eSToby Isaac 
37319435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
37329435951eSToby Isaac 
37339435951eSToby Isaac .seealso: DMSetDefaultConstraints()
37349435951eSToby Isaac @*/
37359435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
37369435951eSToby Isaac {
37379435951eSToby Isaac   PetscErrorCode ierr;
37389435951eSToby Isaac 
37399435951eSToby Isaac   PetscFunctionBegin;
37409435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37419435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
374245a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
374345a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
37449435951eSToby Isaac   PetscFunctionReturn(0);
37459435951eSToby Isaac }
37469435951eSToby Isaac 
37479435951eSToby Isaac /*@
37489435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
37499435951eSToby Isaac 
37509435951eSToby 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().
37519435951eSToby Isaac 
37529435951eSToby 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.
37539435951eSToby Isaac 
3754e228b242SToby Isaac   collective on dm
3755e228b242SToby Isaac 
37569435951eSToby Isaac   Input Parameters:
37579435951eSToby Isaac + dm - The DM
3758e228b242SToby 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).
3759e228b242SToby 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).
37609435951eSToby Isaac 
37619435951eSToby Isaac   Level: advanced
37629435951eSToby Isaac 
37639435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
37649435951eSToby Isaac 
37659435951eSToby Isaac .seealso: DMGetDefaultConstraints()
37669435951eSToby Isaac @*/
37679435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
37689435951eSToby Isaac {
3769e228b242SToby Isaac   PetscMPIInt result;
37709435951eSToby Isaac   PetscErrorCode ierr;
37719435951eSToby Isaac 
37729435951eSToby Isaac   PetscFunctionBegin;
37739435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3774e228b242SToby Isaac   if (section) {
3775e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3776e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3777f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3778e228b242SToby Isaac   }
3779e228b242SToby Isaac   if (mat) {
3780e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3781e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3782f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3783e228b242SToby Isaac   }
37849435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
37859435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
37869435951eSToby Isaac   dm->defaultConstraintSection = section;
37879435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
37889435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
37899435951eSToby Isaac   dm->defaultConstraintMat = mat;
37909435951eSToby Isaac   PetscFunctionReturn(0);
37919435951eSToby Isaac }
37929435951eSToby Isaac 
3793497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3794507e4973SMatthew G. Knepley /*
3795507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3796507e4973SMatthew G. Knepley 
3797507e4973SMatthew G. Knepley   Input Parameters:
3798507e4973SMatthew G. Knepley + dm - The DM
3799507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3800507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3801507e4973SMatthew G. Knepley 
3802507e4973SMatthew G. Knepley   Level: intermediate
3803507e4973SMatthew G. Knepley 
3804507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3805507e4973SMatthew G. Knepley */
3806f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3807507e4973SMatthew G. Knepley {
3808507e4973SMatthew G. Knepley   MPI_Comm        comm;
3809507e4973SMatthew G. Knepley   PetscLayout     layout;
3810507e4973SMatthew G. Knepley   const PetscInt *ranges;
3811507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3812507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3813507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3814507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3815507e4973SMatthew G. Knepley 
3816507e4973SMatthew G. Knepley   PetscFunctionBegin;
3817507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3818507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3819507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3820507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3821507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3822507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3823507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3824507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3825507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3826507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3827507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3828507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3829f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3830507e4973SMatthew G. Knepley 
3831507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3832507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3833507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3834507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3835507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3836507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3837507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3838507e4973SMatthew 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;}
3839507e4973SMatthew 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;}
3840507e4973SMatthew G. Knepley     if (gdof < 0) {
3841507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3842507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3843507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3844507e4973SMatthew G. Knepley 
3845507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3846507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3847507e4973SMatthew 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;}
3848507e4973SMatthew G. Knepley       }
3849507e4973SMatthew G. Knepley     }
3850507e4973SMatthew G. Knepley   }
3851507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3852507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3853b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3854507e4973SMatthew G. Knepley   if (!gvalid) {
3855507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3856507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3857507e4973SMatthew G. Knepley   }
3858507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3859507e4973SMatthew G. Knepley }
3860f741bcd2SMatthew G. Knepley #endif
3861507e4973SMatthew G. Knepley 
386288ed4aceSMatthew G Knepley /*@
3863e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
386488ed4aceSMatthew G Knepley 
38658b1ab98fSJed Brown   Collective on DM
38668b1ab98fSJed Brown 
386788ed4aceSMatthew G Knepley   Input Parameter:
386888ed4aceSMatthew G Knepley . dm - The DM
386988ed4aceSMatthew G Knepley 
387088ed4aceSMatthew G Knepley   Output Parameter:
387188ed4aceSMatthew G Knepley . section - The PetscSection
387288ed4aceSMatthew G Knepley 
387388ed4aceSMatthew G Knepley   Level: intermediate
387488ed4aceSMatthew G Knepley 
387588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
387688ed4aceSMatthew G Knepley 
3877e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
387888ed4aceSMatthew G Knepley @*/
3879e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
38800adebc6cSBarry Smith {
388188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
388288ed4aceSMatthew G Knepley 
388388ed4aceSMatthew G Knepley   PetscFunctionBegin;
388488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
388588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
388688ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3887fd59a867SMatthew G. Knepley     PetscSection s;
3888fd59a867SMatthew G. Knepley 
3889e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3890fd59a867SMatthew 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");
3891fd59a867SMatthew G. Knepley     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSF in order to create a global PetscSection");
389215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3893cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3894ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3895685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
389688ed4aceSMatthew G Knepley   }
389788ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
389888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
389988ed4aceSMatthew G Knepley }
390088ed4aceSMatthew G Knepley 
3901b21d0597SMatthew G Knepley /*@
3902e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3903b21d0597SMatthew G Knepley 
3904b21d0597SMatthew G Knepley   Input Parameters:
3905b21d0597SMatthew G Knepley + dm - The DM
39065080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3907b21d0597SMatthew G Knepley 
3908b21d0597SMatthew G Knepley   Level: intermediate
3909b21d0597SMatthew G Knepley 
3910b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3911b21d0597SMatthew G Knepley 
3912e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3913b21d0597SMatthew G Knepley @*/
3914e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
39150adebc6cSBarry Smith {
3916b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3917b21d0597SMatthew G Knepley 
3918b21d0597SMatthew G Knepley   PetscFunctionBegin;
3919b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39205080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
39211d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3922b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3923b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3924497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3925f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3926507e4973SMatthew G. Knepley #endif
3927b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3928b21d0597SMatthew G Knepley }
3929b21d0597SMatthew G Knepley 
393088ed4aceSMatthew G Knepley /*@
393188ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
393288ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
393388ed4aceSMatthew G Knepley 
393488ed4aceSMatthew G Knepley   Input Parameter:
393588ed4aceSMatthew G Knepley . dm - The DM
393688ed4aceSMatthew G Knepley 
393788ed4aceSMatthew G Knepley   Output Parameter:
393888ed4aceSMatthew G Knepley . sf - The PetscSF
393988ed4aceSMatthew G Knepley 
394088ed4aceSMatthew G Knepley   Level: intermediate
394188ed4aceSMatthew G Knepley 
394288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
394388ed4aceSMatthew G Knepley 
394488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
394588ed4aceSMatthew G Knepley @*/
39460adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
39470adebc6cSBarry Smith {
394888ed4aceSMatthew G Knepley   PetscInt       nroots;
394988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
395088ed4aceSMatthew G Knepley 
395188ed4aceSMatthew G Knepley   PetscFunctionBegin;
395288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
395388ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
39540298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
395588ed4aceSMatthew G Knepley   if (nroots < 0) {
395688ed4aceSMatthew G Knepley     PetscSection section, gSection;
395788ed4aceSMatthew G Knepley 
3958e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
395931ea6d37SMatthew G Knepley     if (section) {
3960e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
396188ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
396231ea6d37SMatthew G Knepley     } else {
39630298fd71SBarry Smith       *sf = NULL;
396431ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
396531ea6d37SMatthew G Knepley     }
396688ed4aceSMatthew G Knepley   }
396788ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
396888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
396988ed4aceSMatthew G Knepley }
397088ed4aceSMatthew G Knepley 
397188ed4aceSMatthew G Knepley /*@
397288ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
397388ed4aceSMatthew G Knepley 
397488ed4aceSMatthew G Knepley   Input Parameters:
397588ed4aceSMatthew G Knepley + dm - The DM
397688ed4aceSMatthew G Knepley - sf - The PetscSF
397788ed4aceSMatthew G Knepley 
397888ed4aceSMatthew G Knepley   Level: intermediate
397988ed4aceSMatthew G Knepley 
398088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
398188ed4aceSMatthew G Knepley 
398288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
398388ed4aceSMatthew G Knepley @*/
39840adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
39850adebc6cSBarry Smith {
398688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
398788ed4aceSMatthew G Knepley 
398888ed4aceSMatthew G Knepley   PetscFunctionBegin;
398988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
399088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
399188ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
399288ed4aceSMatthew G Knepley   dm->defaultSF = sf;
399388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
399488ed4aceSMatthew G Knepley }
399588ed4aceSMatthew G Knepley 
399688ed4aceSMatthew G Knepley /*@C
399788ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
399888ed4aceSMatthew G Knepley   describing the data layout.
399988ed4aceSMatthew G Knepley 
400088ed4aceSMatthew G Knepley   Input Parameters:
400188ed4aceSMatthew G Knepley + dm - The DM
400288ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
400388ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
400488ed4aceSMatthew G Knepley 
400588ed4aceSMatthew G Knepley   Level: intermediate
400688ed4aceSMatthew G Knepley 
400788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
400888ed4aceSMatthew G Knepley @*/
400988ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
401088ed4aceSMatthew G Knepley {
401182f516ccSBarry Smith   MPI_Comm       comm;
401288ed4aceSMatthew G Knepley   PetscLayout    layout;
401388ed4aceSMatthew G Knepley   const PetscInt *ranges;
401488ed4aceSMatthew G Knepley   PetscInt       *local;
401588ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4016ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
401788ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
401888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
401988ed4aceSMatthew G Knepley 
402088ed4aceSMatthew G Knepley   PetscFunctionBegin;
402188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4022367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
402388ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
402488ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
402588ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
402688ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
402788ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
402888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
402988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
403088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
403188ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4032ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
40336636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
403488ed4aceSMatthew G Knepley 
40356636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40366636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4037235fbf56SMatthew 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));
40386636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
403988ed4aceSMatthew G Knepley   }
4040785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4041785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
404288ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
40431f588964SMatthew G Knepley     const PetscInt *cind;
40446636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
404588ed4aceSMatthew G Knepley 
404688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
404788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
404888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
404988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
405088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40516636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
405288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
40536636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
40546636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
40556636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4056057b4bcdSMatthew 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);
40576636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
40586636e97aSMatthew G Knepley     }
405988ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
406088ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
406188ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
406288ed4aceSMatthew G Knepley     }
406388ed4aceSMatthew G Knepley     if (gdof < 0) {
40646636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
406588ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
406688ed4aceSMatthew G Knepley 
406705376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
406831d3f06eSJed Brown         if (r < 0) r = -(r+2);
406905376888SMatthew 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);
407088ed4aceSMatthew G Knepley         remote[l].rank  = r;
407188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
407288ed4aceSMatthew G Knepley       }
407388ed4aceSMatthew G Knepley     } else {
40746636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
407588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
407688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
407788ed4aceSMatthew G Knepley       }
407888ed4aceSMatthew G Knepley     }
407988ed4aceSMatthew G Knepley   }
40806636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
408188ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
408288ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
408388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
408488ed4aceSMatthew G Knepley }
4085af122d2aSMatthew G Knepley 
4086b21d0597SMatthew G Knepley /*@
4087b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4088b21d0597SMatthew G Knepley 
4089b21d0597SMatthew G Knepley   Input Parameter:
4090b21d0597SMatthew G Knepley . dm - The DM
4091b21d0597SMatthew G Knepley 
4092b21d0597SMatthew G Knepley   Output Parameter:
4093b21d0597SMatthew G Knepley . sf - The PetscSF
4094b21d0597SMatthew G Knepley 
4095b21d0597SMatthew G Knepley   Level: intermediate
4096b21d0597SMatthew G Knepley 
4097b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4098b21d0597SMatthew G Knepley 
4099057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4100b21d0597SMatthew G Knepley @*/
41010adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
41020adebc6cSBarry Smith {
4103b21d0597SMatthew G Knepley   PetscFunctionBegin;
4104b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4105b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4106b21d0597SMatthew G Knepley   *sf = dm->sf;
4107b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4108b21d0597SMatthew G Knepley }
4109b21d0597SMatthew G Knepley 
4110057b4bcdSMatthew G Knepley /*@
4111057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4112057b4bcdSMatthew G Knepley 
4113057b4bcdSMatthew G Knepley   Input Parameters:
4114057b4bcdSMatthew G Knepley + dm - The DM
4115057b4bcdSMatthew G Knepley - sf - The PetscSF
4116057b4bcdSMatthew G Knepley 
4117057b4bcdSMatthew G Knepley   Level: intermediate
4118057b4bcdSMatthew G Knepley 
4119057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4120057b4bcdSMatthew G Knepley @*/
41210adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
41220adebc6cSBarry Smith {
4123057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4124057b4bcdSMatthew G Knepley 
4125057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4126057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4127057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
4128057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4129057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4130057b4bcdSMatthew G Knepley   dm->sf = sf;
4131057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4132057b4bcdSMatthew G Knepley }
4133057b4bcdSMatthew G Knepley 
41342764a2aaSMatthew G. Knepley /*@
41352764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
41362764a2aaSMatthew G. Knepley 
41372764a2aaSMatthew G. Knepley   Input Parameter:
41382764a2aaSMatthew G. Knepley . dm - The DM
41392764a2aaSMatthew G. Knepley 
41402764a2aaSMatthew G. Knepley   Output Parameter:
41412764a2aaSMatthew G. Knepley . prob - The PetscDS
41422764a2aaSMatthew G. Knepley 
41432764a2aaSMatthew G. Knepley   Level: developer
41442764a2aaSMatthew G. Knepley 
41452764a2aaSMatthew G. Knepley .seealso: DMSetDS()
41462764a2aaSMatthew G. Knepley @*/
41472764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4148af122d2aSMatthew G Knepley {
4149af122d2aSMatthew G Knepley   PetscFunctionBegin;
4150af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41510f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
41520f21e855SMatthew G. Knepley   *prob = dm->prob;
41530f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41540f21e855SMatthew G. Knepley }
41550f21e855SMatthew G. Knepley 
41562764a2aaSMatthew G. Knepley /*@
41572764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
41582764a2aaSMatthew G. Knepley 
41592764a2aaSMatthew G. Knepley   Input Parameters:
41602764a2aaSMatthew G. Knepley + dm - The DM
41612764a2aaSMatthew G. Knepley - prob - The PetscDS
41622764a2aaSMatthew G. Knepley 
41632764a2aaSMatthew G. Knepley   Level: developer
41642764a2aaSMatthew G. Knepley 
41652764a2aaSMatthew G. Knepley .seealso: DMGetDS()
41662764a2aaSMatthew G. Knepley @*/
41672764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
41680f21e855SMatthew G. Knepley {
4169f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
41700f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41710f21e855SMatthew G. Knepley 
41720f21e855SMatthew G. Knepley   PetscFunctionBegin;
41730f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41742764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
417537316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
41762764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
41770f21e855SMatthew G. Knepley   dm->prob = prob;
4178f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4179f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
41800f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41810f21e855SMatthew G. Knepley }
41820f21e855SMatthew G. Knepley 
41830f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
41840f21e855SMatthew G. Knepley {
41850f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41860f21e855SMatthew G. Knepley 
41870f21e855SMatthew G. Knepley   PetscFunctionBegin;
41880f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41892764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
4190af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4191af122d2aSMatthew G Knepley }
4192af122d2aSMatthew G Knepley 
4193af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4194af122d2aSMatthew G Knepley {
41950f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4196af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4197af122d2aSMatthew G Knepley 
4198af122d2aSMatthew G Knepley   PetscFunctionBegin;
4199af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42002764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
42010f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
42020f21e855SMatthew G. Knepley     PetscContainer obj;
42030f21e855SMatthew G. Knepley 
42040f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
42052764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
42060f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4207af122d2aSMatthew G Knepley   }
4208af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4209af122d2aSMatthew G Knepley }
4210af122d2aSMatthew G Knepley 
4211c1929be8SMatthew G. Knepley /*@
4212c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4213c1929be8SMatthew G. Knepley 
4214c1929be8SMatthew G. Knepley   Not collective
4215c1929be8SMatthew G. Knepley 
4216c1929be8SMatthew G. Knepley   Input Parameters:
4217c1929be8SMatthew G. Knepley + dm - The DM
4218c1929be8SMatthew G. Knepley - f  - The field number
4219c1929be8SMatthew G. Knepley 
4220c1929be8SMatthew G. Knepley   Output Parameter:
4221c1929be8SMatthew G. Knepley . field - The discretization object
4222c1929be8SMatthew G. Knepley 
4223c1929be8SMatthew G. Knepley   Level: developer
4224c1929be8SMatthew G. Knepley 
4225c1929be8SMatthew G. Knepley .seealso: DMSetField()
4226c1929be8SMatthew G. Knepley @*/
4227af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4228af122d2aSMatthew G Knepley {
42290f21e855SMatthew G. Knepley   PetscErrorCode ierr;
42300f21e855SMatthew G. Knepley 
4231af122d2aSMatthew G Knepley   PetscFunctionBegin;
4232af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42332764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4234decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4235decb47aaSMatthew G. Knepley }
4236decb47aaSMatthew G. Knepley 
4237c1929be8SMatthew G. Knepley /*@
4238c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4239c1929be8SMatthew G. Knepley 
4240c1929be8SMatthew G. Knepley   Logically collective on DM
4241c1929be8SMatthew G. Knepley 
4242c1929be8SMatthew G. Knepley   Input Parameters:
4243c1929be8SMatthew G. Knepley + dm - The DM
4244c1929be8SMatthew G. Knepley . f  - The field number
4245c1929be8SMatthew G. Knepley - field - The discretization object
4246c1929be8SMatthew G. Knepley 
4247c1929be8SMatthew G. Knepley   Level: developer
4248c1929be8SMatthew G. Knepley 
4249c1929be8SMatthew G. Knepley .seealso: DMGetField()
4250c1929be8SMatthew G. Knepley @*/
4251decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4252decb47aaSMatthew G. Knepley {
4253decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4254decb47aaSMatthew G. Knepley 
4255decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4256decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42572764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4258af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4259af122d2aSMatthew G Knepley }
42606636e97aSMatthew G Knepley 
4261b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4262b64e0483SPeter Brune {
4263b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4264b64e0483SPeter Brune   PetscErrorCode ierr;
4265b64e0483SPeter Brune   Vec coords,ccoords;
42666dbf9973SLawrence Mitchell   Mat inject;
4267b64e0483SPeter Brune   PetscFunctionBegin;
4268b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4269b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4270b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4271b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4272b64e0483SPeter Brune   if (coords && !ccoords) {
4273b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
42746668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
42756dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
42762adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
42776dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4278b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4279b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4280b64e0483SPeter Brune   }
4281b64e0483SPeter Brune   PetscFunctionReturn(0);
4282b64e0483SPeter Brune }
4283b64e0483SPeter Brune 
428403dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
428503dadc2fSPeter Brune {
428603dadc2fSPeter Brune   DM dm_coord,subdm_coord;
428703dadc2fSPeter Brune   PetscErrorCode ierr;
428803dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
428903dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
429003dadc2fSPeter Brune   PetscFunctionBegin;
429103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
429203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
429303dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
429403dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
429503dadc2fSPeter Brune   if (coords && !ccoords) {
429603dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
42976668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
429803dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
429924640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
430003dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
430103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
430203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
430303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
430403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
430503dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
430603dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
430703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
430803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
430903dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
431003dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
431103dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
431203dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
431303dadc2fSPeter Brune   }
431403dadc2fSPeter Brune   PetscFunctionReturn(0);
431503dadc2fSPeter Brune }
431603dadc2fSPeter Brune 
4317c73cfb54SMatthew G. Knepley /*@
4318c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4319c73cfb54SMatthew G. Knepley 
4320c73cfb54SMatthew G. Knepley   Not collective
4321c73cfb54SMatthew G. Knepley 
4322c73cfb54SMatthew G. Knepley   Input Parameter:
4323c73cfb54SMatthew G. Knepley . dm - The DM
4324c73cfb54SMatthew G. Knepley 
4325c73cfb54SMatthew G. Knepley   Output Parameter:
4326c73cfb54SMatthew G. Knepley . dim - The topological dimension
4327c73cfb54SMatthew G. Knepley 
4328c73cfb54SMatthew G. Knepley   Level: beginner
4329c73cfb54SMatthew G. Knepley 
4330c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4331c73cfb54SMatthew G. Knepley @*/
4332c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4333c73cfb54SMatthew G. Knepley {
4334c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4335c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4336c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4337c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4338c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4339c73cfb54SMatthew G. Knepley }
4340c73cfb54SMatthew G. Knepley 
4341c73cfb54SMatthew G. Knepley /*@
4342c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4343c73cfb54SMatthew G. Knepley 
4344c73cfb54SMatthew G. Knepley   Collective on dm
4345c73cfb54SMatthew G. Knepley 
4346c73cfb54SMatthew G. Knepley   Input Parameters:
4347c73cfb54SMatthew G. Knepley + dm - The DM
4348c73cfb54SMatthew G. Knepley - dim - The topological dimension
4349c73cfb54SMatthew G. Knepley 
4350c73cfb54SMatthew G. Knepley   Level: beginner
4351c73cfb54SMatthew G. Knepley 
4352c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4353c73cfb54SMatthew G. Knepley @*/
4354c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4355c73cfb54SMatthew G. Knepley {
4356f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4357f17e8794SMatthew G. Knepley 
4358c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4359c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4360c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4361c73cfb54SMatthew G. Knepley   dm->dim = dim;
4362f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4363c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4364c73cfb54SMatthew G. Knepley }
4365c73cfb54SMatthew G. Knepley 
4366793f3fe5SMatthew G. Knepley /*@
4367793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4368793f3fe5SMatthew G. Knepley 
4369793f3fe5SMatthew G. Knepley   Collective on DM
4370793f3fe5SMatthew G. Knepley 
4371793f3fe5SMatthew G. Knepley   Input Parameters:
4372793f3fe5SMatthew G. Knepley + dm - the DM
4373793f3fe5SMatthew G. Knepley - dim - the dimension
4374793f3fe5SMatthew G. Knepley 
4375793f3fe5SMatthew G. Knepley   Output Parameters:
4376793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4377793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4378793f3fe5SMatthew G. Knepley 
4379793f3fe5SMatthew G. Knepley   Note:
4380793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4381793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4382793f3fe5SMatthew G. Knepley   then the interval is empty.
4383793f3fe5SMatthew G. Knepley 
4384793f3fe5SMatthew G. Knepley   Level: intermediate
4385793f3fe5SMatthew G. Knepley 
4386793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4387793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4388793f3fe5SMatthew G. Knepley @*/
4389793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4390793f3fe5SMatthew G. Knepley {
4391793f3fe5SMatthew G. Knepley   PetscInt       d;
4392793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4393793f3fe5SMatthew G. Knepley 
4394793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4395793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4396793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4397793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4398793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4399793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4400793f3fe5SMatthew G. Knepley }
4401793f3fe5SMatthew G. Knepley 
44026636e97aSMatthew G Knepley /*@
44036636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
44046636e97aSMatthew G Knepley 
44056636e97aSMatthew G Knepley   Collective on DM
44066636e97aSMatthew G Knepley 
44076636e97aSMatthew G Knepley   Input Parameters:
44086636e97aSMatthew G Knepley + dm - the DM
44096636e97aSMatthew G Knepley - c - coordinate vector
44106636e97aSMatthew G Knepley 
44112dd40e9bSPatrick Sanan   Notes:
44122dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
44132dd40e9bSPatrick Sanan 
44142dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
44156636e97aSMatthew G Knepley 
44166636e97aSMatthew G Knepley   Level: intermediate
44176636e97aSMatthew G Knepley 
44186636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44192dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
44206636e97aSMatthew G Knepley @*/
44216636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
44226636e97aSMatthew G Knepley {
44236636e97aSMatthew G Knepley   PetscErrorCode ierr;
44246636e97aSMatthew G Knepley 
44256636e97aSMatthew G Knepley   PetscFunctionBegin;
44266636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44276636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
44286636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
44296636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
44306636e97aSMatthew G Knepley   dm->coordinates = c;
44316636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4432b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
443303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
44346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44356636e97aSMatthew G Knepley }
44366636e97aSMatthew G Knepley 
44376636e97aSMatthew G Knepley /*@
44386636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
44396636e97aSMatthew G Knepley 
44407058e716SVaclav Hapla   Not collective
44416636e97aSMatthew G Knepley 
44426636e97aSMatthew G Knepley    Input Parameters:
44436636e97aSMatthew G Knepley +  dm - the DM
44446636e97aSMatthew G Knepley -  c - coordinate vector
44456636e97aSMatthew G Knepley 
44462dd40e9bSPatrick Sanan   Notes:
44476636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
44486636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
44496636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
44506636e97aSMatthew G Knepley 
44512dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
44522dd40e9bSPatrick Sanan 
44536636e97aSMatthew G Knepley   Level: intermediate
44546636e97aSMatthew G Knepley 
44556636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44566636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
44576636e97aSMatthew G Knepley @*/
44586636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
44596636e97aSMatthew G Knepley {
44606636e97aSMatthew G Knepley   PetscErrorCode ierr;
44616636e97aSMatthew G Knepley 
44626636e97aSMatthew G Knepley   PetscFunctionBegin;
44636636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44646636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
44656636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
44666636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
44678865f1eaSKarl Rupp 
44686636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
44698865f1eaSKarl Rupp 
44706636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
44716636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44726636e97aSMatthew G Knepley }
44736636e97aSMatthew G Knepley 
44746636e97aSMatthew G Knepley /*@
44756636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
44766636e97aSMatthew G Knepley 
4477c4a54dccSVaclav Hapla   Collective on DM
44786636e97aSMatthew G Knepley 
44796636e97aSMatthew G Knepley   Input Parameter:
44806636e97aSMatthew G Knepley . dm - the DM
44816636e97aSMatthew G Knepley 
44826636e97aSMatthew G Knepley   Output Parameter:
44836636e97aSMatthew G Knepley . c - global coordinate vector
44846636e97aSMatthew G Knepley 
44856636e97aSMatthew G Knepley   Note:
44866636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
44876636e97aSMatthew G Knepley 
44886636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
44896636e97aSMatthew G Knepley 
44906636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
44916636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
44926636e97aSMatthew G Knepley 
44936636e97aSMatthew G Knepley   Level: intermediate
44946636e97aSMatthew G Knepley 
44956636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44966636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
44976636e97aSMatthew G Knepley @*/
44986636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
44996636e97aSMatthew G Knepley {
45006636e97aSMatthew G Knepley   PetscErrorCode ierr;
45016636e97aSMatthew G Knepley 
45026636e97aSMatthew G Knepley   PetscFunctionBegin;
45036636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45046636e97aSMatthew G Knepley   PetscValidPointer(c,2);
45051f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
45060298fd71SBarry Smith     DM cdm = NULL;
45076636e97aSMatthew G Knepley 
45086636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
45096636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
45106636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
45116636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
45126636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
45136636e97aSMatthew G Knepley   }
45146636e97aSMatthew G Knepley   *c = dm->coordinates;
45156636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45166636e97aSMatthew G Knepley }
45176636e97aSMatthew G Knepley 
45186636e97aSMatthew G Knepley /*@
451981e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
452081e9a530SVaclav Hapla 
452181e9a530SVaclav Hapla   Collective on DM
452281e9a530SVaclav Hapla 
452381e9a530SVaclav Hapla   Input Parameter:
452481e9a530SVaclav Hapla . dm - the DM
452581e9a530SVaclav Hapla 
452681e9a530SVaclav Hapla   Level: advanced
452781e9a530SVaclav Hapla 
452881e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
452981e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
453081e9a530SVaclav Hapla @*/
453181e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
453281e9a530SVaclav Hapla {
453381e9a530SVaclav Hapla   DM cdm = NULL;
453481e9a530SVaclav Hapla   PetscErrorCode ierr;
453581e9a530SVaclav Hapla 
453681e9a530SVaclav Hapla   PetscFunctionBegin;
453781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
453881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
453981e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
454081e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
454181e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
454281e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
454381e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
454481e9a530SVaclav Hapla   }
454581e9a530SVaclav Hapla   PetscFunctionReturn(0);
454681e9a530SVaclav Hapla }
454781e9a530SVaclav Hapla 
454881e9a530SVaclav Hapla /*@
45496636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
45506636e97aSMatthew G Knepley 
45516636e97aSMatthew G Knepley   Collective on DM
45526636e97aSMatthew G Knepley 
45536636e97aSMatthew G Knepley   Input Parameter:
45546636e97aSMatthew G Knepley . dm - the DM
45556636e97aSMatthew G Knepley 
45566636e97aSMatthew G Knepley   Output Parameter:
45576636e97aSMatthew G Knepley . c - coordinate vector
45586636e97aSMatthew G Knepley 
45596636e97aSMatthew G Knepley   Note:
45606636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
45616636e97aSMatthew G Knepley 
45626636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
45636636e97aSMatthew G Knepley 
45646636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
45656636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
45666636e97aSMatthew G Knepley 
45676636e97aSMatthew G Knepley   Level: intermediate
45686636e97aSMatthew G Knepley 
45696636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
457081e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
45716636e97aSMatthew G Knepley @*/
45726636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
45736636e97aSMatthew G Knepley {
45746636e97aSMatthew G Knepley   PetscErrorCode ierr;
45756636e97aSMatthew G Knepley 
45766636e97aSMatthew G Knepley   PetscFunctionBegin;
45776636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45786636e97aSMatthew G Knepley   PetscValidPointer(c,2);
457981e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
45806636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
45816636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45826636e97aSMatthew G Knepley }
45836636e97aSMatthew G Knepley 
458481e9a530SVaclav Hapla /*@
458581e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
458681e9a530SVaclav Hapla 
458781e9a530SVaclav Hapla   Not collective
458881e9a530SVaclav Hapla 
458981e9a530SVaclav Hapla   Input Parameter:
459081e9a530SVaclav Hapla . dm - the DM
459181e9a530SVaclav Hapla 
459281e9a530SVaclav Hapla   Output Parameter:
459381e9a530SVaclav Hapla . c - coordinate vector
459481e9a530SVaclav Hapla 
459581e9a530SVaclav Hapla   Level: advanced
459681e9a530SVaclav Hapla 
459781e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
459881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
459981e9a530SVaclav Hapla @*/
460081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
460181e9a530SVaclav Hapla {
460281e9a530SVaclav Hapla   PetscFunctionBegin;
460381e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
460481e9a530SVaclav Hapla   PetscValidPointer(c,2);
460581e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
46066636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
46076636e97aSMatthew G Knepley   PetscFunctionReturn(0);
46086636e97aSMatthew G Knepley }
46096636e97aSMatthew G Knepley 
46102db98f8dSVaclav Hapla /*@
46112db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
46122db98f8dSVaclav Hapla 
46132db98f8dSVaclav Hapla   Not collective
46142db98f8dSVaclav Hapla 
46152db98f8dSVaclav Hapla   Input Parameter:
46162db98f8dSVaclav Hapla + dm - the DM
46172db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
46182db98f8dSVaclav Hapla 
46192db98f8dSVaclav Hapla   Output Parameter:
46202db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
46212db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
46222db98f8dSVaclav Hapla 
46232db98f8dSVaclav Hapla   Note:
46242db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
46252db98f8dSVaclav Hapla 
46262db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
46272db98f8dSVaclav Hapla 
46282db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
46292db98f8dSVaclav Hapla 
46302db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
46312db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
46322db98f8dSVaclav Hapla 
46332db98f8dSVaclav Hapla   Level: advanced
46342db98f8dSVaclav Hapla 
46352db98f8dSVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
46362db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
46372db98f8dSVaclav Hapla @*/
46382db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
46392db98f8dSVaclav Hapla {
46402db98f8dSVaclav Hapla   PetscSection        cs, newcs;
46412db98f8dSVaclav Hapla   Vec                 coords;
46422db98f8dSVaclav Hapla   const PetscScalar   *arr;
46432db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
46442db98f8dSVaclav Hapla   PetscInt            n;
46452db98f8dSVaclav Hapla   PetscErrorCode      ierr;
46462db98f8dSVaclav Hapla 
46472db98f8dSVaclav Hapla   PetscFunctionBegin;
46482db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46492db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
46502db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
46512db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
46522db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
46532db98f8dSVaclav Hapla   if (!dm->coordinateDM || !dm->coordinateDM->defaultSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
46542db98f8dSVaclav Hapla   cs = dm->coordinateDM->defaultSection;
46552db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
46562db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
46572db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
46582db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
46592db98f8dSVaclav Hapla   if (pCoord) {
46602db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
46612db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
46622db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
46632db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
4664ad9ac99dSVaclav Hapla   } else {
4665ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
46662db98f8dSVaclav Hapla   }
4667ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
4668ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
46692db98f8dSVaclav Hapla   PetscFunctionReturn(0);
46702db98f8dSVaclav Hapla }
46712db98f8dSVaclav Hapla 
4672f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
4673f19dbd58SToby Isaac {
4674f19dbd58SToby Isaac   PetscErrorCode ierr;
4675f19dbd58SToby Isaac 
4676f19dbd58SToby Isaac   PetscFunctionBegin;
4677f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4678f19dbd58SToby Isaac   PetscValidPointer(field,2);
4679f19dbd58SToby Isaac   if (!dm->coordinateField) {
4680f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
4681f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
4682f19dbd58SToby Isaac     }
4683f19dbd58SToby Isaac   }
4684f19dbd58SToby Isaac   *field = dm->coordinateField;
4685f19dbd58SToby Isaac   PetscFunctionReturn(0);
4686f19dbd58SToby Isaac }
4687f19dbd58SToby Isaac 
4688f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
4689f19dbd58SToby Isaac {
4690f19dbd58SToby Isaac   PetscErrorCode ierr;
4691f19dbd58SToby Isaac 
4692f19dbd58SToby Isaac   PetscFunctionBegin;
4693f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4694f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
4695c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
4696f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
4697f19dbd58SToby Isaac   dm->coordinateField = field;
4698f19dbd58SToby Isaac   PetscFunctionReturn(0);
4699f19dbd58SToby Isaac }
4700f19dbd58SToby Isaac 
47016636e97aSMatthew G Knepley /*@
47021cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
47036636e97aSMatthew G Knepley 
47046636e97aSMatthew G Knepley   Collective on DM
47056636e97aSMatthew G Knepley 
47066636e97aSMatthew G Knepley   Input Parameter:
47076636e97aSMatthew G Knepley . dm - the DM
47086636e97aSMatthew G Knepley 
47096636e97aSMatthew G Knepley   Output Parameter:
47106636e97aSMatthew G Knepley . cdm - coordinate DM
47116636e97aSMatthew G Knepley 
47126636e97aSMatthew G Knepley   Level: intermediate
47136636e97aSMatthew G Knepley 
47146636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
47151cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
47166636e97aSMatthew G Knepley @*/
47176636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
47186636e97aSMatthew G Knepley {
47196636e97aSMatthew G Knepley   PetscErrorCode ierr;
47206636e97aSMatthew G Knepley 
47216636e97aSMatthew G Knepley   PetscFunctionBegin;
47226636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47236636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
47246636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
472582f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
47266636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
47276636e97aSMatthew G Knepley   }
47286636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
47296636e97aSMatthew G Knepley   PetscFunctionReturn(0);
47306636e97aSMatthew G Knepley }
4731e87bb0d3SMatthew G Knepley 
47321cfe2091SMatthew G. Knepley /*@
47331cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
47341cfe2091SMatthew G. Knepley 
47351cfe2091SMatthew G. Knepley   Logically Collective on DM
47361cfe2091SMatthew G. Knepley 
47371cfe2091SMatthew G. Knepley   Input Parameters:
47381cfe2091SMatthew G. Knepley + dm - the DM
47391cfe2091SMatthew G. Knepley - cdm - coordinate DM
47401cfe2091SMatthew G. Knepley 
47411cfe2091SMatthew G. Knepley   Level: intermediate
47421cfe2091SMatthew G. Knepley 
47431cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
47441cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
47451cfe2091SMatthew G. Knepley @*/
47461cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
47471cfe2091SMatthew G. Knepley {
47481cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
47491cfe2091SMatthew G. Knepley 
47501cfe2091SMatthew G. Knepley   PetscFunctionBegin;
47511cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47521cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4753f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
47541cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
47551cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
47561cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
47571cfe2091SMatthew G. Knepley }
47581cfe2091SMatthew G. Knepley 
475946e270d4SMatthew G. Knepley /*@
476046e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
476146e270d4SMatthew G. Knepley 
476246e270d4SMatthew G. Knepley   Not Collective
476346e270d4SMatthew G. Knepley 
476446e270d4SMatthew G. Knepley   Input Parameter:
476546e270d4SMatthew G. Knepley . dm - The DM object
476646e270d4SMatthew G. Knepley 
476746e270d4SMatthew G. Knepley   Output Parameter:
476846e270d4SMatthew G. Knepley . dim - The embedding dimension
476946e270d4SMatthew G. Knepley 
477046e270d4SMatthew G. Knepley   Level: intermediate
477146e270d4SMatthew G. Knepley 
477246e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4773e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
477446e270d4SMatthew G. Knepley @*/
477546e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
477646e270d4SMatthew G. Knepley {
477746e270d4SMatthew G. Knepley   PetscFunctionBegin;
477846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477946e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
47809a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
47819a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
47829a9a41abSToby Isaac   }
478346e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
478446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
478546e270d4SMatthew G. Knepley }
478646e270d4SMatthew G. Knepley 
478746e270d4SMatthew G. Knepley /*@
478846e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
478946e270d4SMatthew G. Knepley 
479046e270d4SMatthew G. Knepley   Not Collective
479146e270d4SMatthew G. Knepley 
479246e270d4SMatthew G. Knepley   Input Parameters:
479346e270d4SMatthew G. Knepley + dm  - The DM object
479446e270d4SMatthew G. Knepley - dim - The embedding dimension
479546e270d4SMatthew G. Knepley 
479646e270d4SMatthew G. Knepley   Level: intermediate
479746e270d4SMatthew G. Knepley 
479846e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4799e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
480046e270d4SMatthew G. Knepley @*/
480146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
480246e270d4SMatthew G. Knepley {
4803f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4804f17e8794SMatthew G. Knepley 
480546e270d4SMatthew G. Knepley   PetscFunctionBegin;
480646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
480746e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4808f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
480946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
481046e270d4SMatthew G. Knepley }
481146e270d4SMatthew G. Knepley 
4812e8abe2deSMatthew G. Knepley /*@
4813e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4814e8abe2deSMatthew G. Knepley 
4815116501dfSVaclav Hapla   Collective on DM
4816e8abe2deSMatthew G. Knepley 
4817e8abe2deSMatthew G. Knepley   Input Parameter:
4818e8abe2deSMatthew G. Knepley . dm - The DM object
4819e8abe2deSMatthew G. Knepley 
4820e8abe2deSMatthew G. Knepley   Output Parameter:
4821e8abe2deSMatthew G. Knepley . section - The PetscSection object
4822e8abe2deSMatthew G. Knepley 
4823e8abe2deSMatthew G. Knepley   Level: intermediate
4824e8abe2deSMatthew G. Knepley 
4825e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4826e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
4827e8abe2deSMatthew G. Knepley @*/
4828e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4829e8abe2deSMatthew G. Knepley {
4830e8abe2deSMatthew G. Knepley   DM             cdm;
4831e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4832e8abe2deSMatthew G. Knepley 
4833e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4834e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4835e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4836e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4837e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
4838e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4839e8abe2deSMatthew G. Knepley }
4840e8abe2deSMatthew G. Knepley 
4841e8abe2deSMatthew G. Knepley /*@
4842e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4843e8abe2deSMatthew G. Knepley 
4844e8abe2deSMatthew G. Knepley   Not Collective
4845e8abe2deSMatthew G. Knepley 
4846e8abe2deSMatthew G. Knepley   Input Parameters:
4847e8abe2deSMatthew G. Knepley + dm      - The DM object
484846e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4849e8abe2deSMatthew G. Knepley - section - The PetscSection object
4850e8abe2deSMatthew G. Knepley 
4851e8abe2deSMatthew G. Knepley   Level: intermediate
4852e8abe2deSMatthew G. Knepley 
4853e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4854e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
4855e8abe2deSMatthew G. Knepley @*/
485646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4857e8abe2deSMatthew G. Knepley {
4858e8abe2deSMatthew G. Knepley   DM             cdm;
4859e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4860e8abe2deSMatthew G. Knepley 
4861e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4862e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
486346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4864e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4865e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
486646e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
48674c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
486846e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
486946e270d4SMatthew G. Knepley 
487046e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
487146e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
487246e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
487346e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
487446e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
487546e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
487646e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
487746e270d4SMatthew G. Knepley     }
48786be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
487946e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
488046e270d4SMatthew G. Knepley   }
4881e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4882e8abe2deSMatthew G. Knepley }
4883e8abe2deSMatthew G. Knepley 
48845dc8c3f7SMatthew G. Knepley /*@C
488590b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
48865dc8c3f7SMatthew G. Knepley 
48875dc8c3f7SMatthew G. Knepley   Input Parameters:
488890b157c4SStefano Zampini . dm      - The DM object
488990b157c4SStefano Zampini 
489090b157c4SStefano Zampini   Output Parameters:
489190b157c4SStefano Zampini + per     - Whether the DM is periodic or not
48925dc8c3f7SMatthew 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
48935dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
48945dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
48955dc8c3f7SMatthew G. Knepley 
48965dc8c3f7SMatthew G. Knepley   Level: developer
48975dc8c3f7SMatthew G. Knepley 
48985dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
48995dc8c3f7SMatthew G. Knepley @*/
490090b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4901c6b900c6SMatthew G. Knepley {
4902c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4903c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
490490b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4905c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4906c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
49075dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4908c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4909c6b900c6SMatthew G. Knepley }
4910c6b900c6SMatthew G. Knepley 
49115dc8c3f7SMatthew G. Knepley /*@C
49125dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
49135dc8c3f7SMatthew G. Knepley 
49145dc8c3f7SMatthew G. Knepley   Input Parameters:
49155dc8c3f7SMatthew G. Knepley + dm      - The DM object
491690b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
49175dc8c3f7SMatthew 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
49185dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
49195dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
49205dc8c3f7SMatthew G. Knepley 
49215dc8c3f7SMatthew G. Knepley   Level: developer
49225dc8c3f7SMatthew G. Knepley 
49235dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
49245dc8c3f7SMatthew G. Knepley @*/
492590b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4926c6b900c6SMatthew G. Knepley {
4927c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4928c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4929c6b900c6SMatthew G. Knepley 
4930c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4931c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
493290b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
493390b157c4SStefano Zampini   if (maxCell) {
493490b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
493590b157c4SStefano Zampini     PetscValidPointer(L,4);
493690b157c4SStefano Zampini     PetscValidPointer(bd,5);
493790b157c4SStefano Zampini   }
49385dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
49395dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
494090b157c4SStefano Zampini   if (maxCell) {
49415dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
49425dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
494390b157c4SStefano Zampini   }
4944*072d7d67SStefano Zampini   dm->periodic = per;
4945c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4946c6b900c6SMatthew G. Knepley }
4947c6b900c6SMatthew G. Knepley 
49482e17dfb7SMatthew G. Knepley /*@
49492e17dfb7SMatthew 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.
49502e17dfb7SMatthew G. Knepley 
49512e17dfb7SMatthew G. Knepley   Input Parameters:
49522e17dfb7SMatthew G. Knepley + dm     - The DM
495365da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
495465da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
49552e17dfb7SMatthew G. Knepley 
49562e17dfb7SMatthew G. Knepley   Output Parameter:
49572e17dfb7SMatthew G. Knepley . out - The localized coordinate point
49582e17dfb7SMatthew G. Knepley 
49592e17dfb7SMatthew G. Knepley   Level: developer
49602e17dfb7SMatthew G. Knepley 
49612e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
49622e17dfb7SMatthew G. Knepley @*/
496365da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
49642e17dfb7SMatthew G. Knepley {
49652e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
49662e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
49672e17dfb7SMatthew G. Knepley 
49682e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
49692e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
49702e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
49712e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
49722e17dfb7SMatthew G. Knepley   } else {
497365da65dcSMatthew G. Knepley     if (endpoint) {
497465da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4975da3333bfSMatthew 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)) {
4976da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
497765da65dcSMatthew G. Knepley         } else {
4978da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
497965da65dcSMatthew G. Knepley         }
498065da65dcSMatthew G. Knepley       }
498165da65dcSMatthew G. Knepley     } else {
49822e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
49831118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
49842e17dfb7SMatthew G. Knepley       }
49852e17dfb7SMatthew G. Knepley     }
498665da65dcSMatthew G. Knepley   }
49872e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
49882e17dfb7SMatthew G. Knepley }
49892e17dfb7SMatthew G. Knepley 
49902e17dfb7SMatthew G. Knepley /*
49912e17dfb7SMatthew 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.
49922e17dfb7SMatthew G. Knepley 
49932e17dfb7SMatthew G. Knepley   Input Parameters:
49942e17dfb7SMatthew G. Knepley + dm     - The DM
49952e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
49962e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
49972e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
49982e17dfb7SMatthew G. Knepley 
49992e17dfb7SMatthew G. Knepley   Output Parameter:
50002e17dfb7SMatthew G. Knepley . out - The localized coordinate point
50012e17dfb7SMatthew G. Knepley 
50022e17dfb7SMatthew G. Knepley   Level: developer
50032e17dfb7SMatthew G. Knepley 
50042e17dfb7SMatthew 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
50052e17dfb7SMatthew G. Knepley 
50062e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
50072e17dfb7SMatthew G. Knepley */
50082e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
50092e17dfb7SMatthew G. Knepley {
50102e17dfb7SMatthew G. Knepley   PetscInt d;
50112e17dfb7SMatthew G. Knepley 
50122e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
50132e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
50142e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
50152e17dfb7SMatthew G. Knepley   } else {
50162e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5017908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
50182e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
50192e17dfb7SMatthew G. Knepley       } else {
50202e17dfb7SMatthew G. Knepley         out[d] = in[d];
50212e17dfb7SMatthew G. Knepley       }
50222e17dfb7SMatthew G. Knepley     }
50232e17dfb7SMatthew G. Knepley   }
50242e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50252e17dfb7SMatthew G. Knepley }
50262e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
50272e17dfb7SMatthew G. Knepley {
50282e17dfb7SMatthew G. Knepley   PetscInt d;
50292e17dfb7SMatthew G. Knepley 
50302e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
50312e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
50322e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
50332e17dfb7SMatthew G. Knepley   } else {
50342e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5035908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
50362e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
50372e17dfb7SMatthew G. Knepley       } else {
50382e17dfb7SMatthew G. Knepley         out[d] = in[d];
50392e17dfb7SMatthew G. Knepley       }
50402e17dfb7SMatthew G. Knepley     }
50412e17dfb7SMatthew G. Knepley   }
50422e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50432e17dfb7SMatthew G. Knepley }
50442e17dfb7SMatthew G. Knepley 
50452e17dfb7SMatthew G. Knepley /*
50462e17dfb7SMatthew 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.
50472e17dfb7SMatthew G. Knepley 
50482e17dfb7SMatthew G. Knepley   Input Parameters:
50492e17dfb7SMatthew G. Knepley + dm     - The DM
50502e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
50512e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
50522e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
50532e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
50542e17dfb7SMatthew G. Knepley 
50552e17dfb7SMatthew G. Knepley   Output Parameter:
50562e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
50572e17dfb7SMatthew G. Knepley 
50582e17dfb7SMatthew G. Knepley   Level: developer
50592e17dfb7SMatthew G. Knepley 
50602e17dfb7SMatthew 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
50612e17dfb7SMatthew G. Knepley 
50622e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
50632e17dfb7SMatthew G. Knepley */
50642e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
50652e17dfb7SMatthew G. Knepley {
50662e17dfb7SMatthew G. Knepley   PetscInt d;
50672e17dfb7SMatthew G. Knepley 
50682e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
50692e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
50702e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
50712e17dfb7SMatthew G. Knepley   } else {
50722e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5073908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
50742e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
50752e17dfb7SMatthew G. Knepley       } else {
50762e17dfb7SMatthew G. Knepley         out[d] += in[d];
50772e17dfb7SMatthew G. Knepley       }
50782e17dfb7SMatthew G. Knepley     }
50792e17dfb7SMatthew G. Knepley   }
50802e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50812e17dfb7SMatthew G. Knepley }
50822e17dfb7SMatthew G. Knepley 
508336447a5eSToby Isaac /*@
508436447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
508536447a5eSToby Isaac 
508636447a5eSToby Isaac   Input Parameter:
508736447a5eSToby Isaac . dm - The DM
508836447a5eSToby Isaac 
508936447a5eSToby Isaac   Output Parameter:
509036447a5eSToby Isaac   areLocalized - True if localized
509136447a5eSToby Isaac 
509236447a5eSToby Isaac   Level: developer
509336447a5eSToby Isaac 
509436447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
509536447a5eSToby Isaac @*/
509636447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
509736447a5eSToby Isaac {
509836447a5eSToby Isaac   DM             cdm;
509936447a5eSToby Isaac   PetscSection   coordSection;
510046a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
510146a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
510236447a5eSToby Isaac   PetscErrorCode ierr;
510336447a5eSToby Isaac 
510436447a5eSToby Isaac   PetscFunctionBegin;
510536447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
510646a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
510746a3a80fSLisandro Dalcin 
51088b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
510946a3a80fSLisandro Dalcin   if (!dm->periodic) PetscFunctionReturn(0); /* This is a hideous optimization hack! */
511046a3a80fSLisandro Dalcin 
511136447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
511236447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
511336447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
511446a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
511546a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
511646a3a80fSLisandro Dalcin 
511746a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
511836447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
511936447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
512046a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
512146a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
512236447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
512346a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
512436447a5eSToby Isaac   }
512546a3a80fSLisandro Dalcin   ierr = MPIU_Allreduce(&alreadyLocalized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
512636447a5eSToby Isaac   PetscFunctionReturn(0);
512736447a5eSToby Isaac }
512836447a5eSToby Isaac 
512936447a5eSToby Isaac 
51302e17dfb7SMatthew G. Knepley /*@
5131492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
51322e17dfb7SMatthew G. Knepley 
51332e17dfb7SMatthew G. Knepley   Input Parameter:
51342e17dfb7SMatthew G. Knepley . dm - The DM
51352e17dfb7SMatthew G. Knepley 
51362e17dfb7SMatthew G. Knepley   Level: developer
51372e17dfb7SMatthew G. Knepley 
51382e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
51392e17dfb7SMatthew G. Knepley @*/
51402e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
51412e17dfb7SMatthew G. Knepley {
51422e17dfb7SMatthew G. Knepley   DM             cdm;
51432e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
51442e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
51453e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
51463e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
5147e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
51483e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
51493e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
51502e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
51512e17dfb7SMatthew G. Knepley 
51522e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
51532e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
515492c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
5155f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
5156f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
5157f7cbd40bSStefano Zampini 
51582e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
51592e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
51602e17dfb7SMatthew G. Knepley   {
51612e17dfb7SMatthew G. Knepley     PetscBool isplex;
51622e17dfb7SMatthew G. Knepley 
51632e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
51642e17dfb7SMatthew G. Knepley     if (isplex) {
51652e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
51663e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
516769291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
51683e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
51693e922f36SToby Isaac       newStart = vStart;
51703e922f36SToby Isaac       newEnd   = vEnd;
51713e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
51723e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
51733e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
51743e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
51753e922f36SToby Isaac       }
51762e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
51772e17dfb7SMatthew G. Knepley   }
51782e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
517943eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
51802e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
51813e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
5182e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
51833e922f36SToby Isaac 
51842e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
51852e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
51862e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
51872e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
51883e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
51893e922f36SToby Isaac 
519069291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
51913e922f36SToby Isaac   localized = &anchor[bs];
51923e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
51933e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
51943e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
51953e922f36SToby Isaac 
51963e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
51973e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
51983e922f36SToby Isaac       PetscInt     b;
51993e922f36SToby Isaac 
52003e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
52013e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
52023e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
52033e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
52043e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
52053e922f36SToby Isaac         for (b = 0; b < bs; b++) {
52063e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
52073e922f36SToby Isaac         }
52083e922f36SToby Isaac         if (b < bs) break;
52093e922f36SToby Isaac       }
52103e922f36SToby Isaac       if (d < dof/bs) {
52113e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
52123e922f36SToby Isaac           PetscInt cdof;
52133e922f36SToby Isaac 
52143e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
52153e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
52163e922f36SToby Isaac         }
52173e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
52183e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
52193e922f36SToby Isaac       }
52203e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
52213e922f36SToby Isaac     }
52223e922f36SToby Isaac   }
52233e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
52243e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
522569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
52263e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
522769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
52283e922f36SToby Isaac     PetscFunctionReturn(0);
52293e922f36SToby Isaac   }
52302e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
52312e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
52322e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
52332e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
52342e17dfb7SMatthew G. Knepley   }
52352e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
52362e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5237c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
52382e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
52392e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
52402e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
52412e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5242c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
52432e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
52442e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
52452e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
52462e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
52472e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
52482e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
52492e17dfb7SMatthew G. Knepley   }
52503e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
52513e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
52523e922f36SToby Isaac 
52532e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
52542e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
52553e922f36SToby Isaac       PetscInt     b, cdof;
52562e17dfb7SMatthew G. Knepley 
52573e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
52583e922f36SToby Isaac       if (!cdof) continue;
52592e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
52602e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
52612e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
52622e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
52632e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
52642e17dfb7SMatthew G. Knepley     }
52653e922f36SToby Isaac   }
526669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
526769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5268c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
52692e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
52702e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
52712e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
52722e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
52732e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
52742e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
52752e17dfb7SMatthew G. Knepley }
52762e17dfb7SMatthew G. Knepley 
5277e87bb0d3SMatthew G Knepley /*@
52783a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5279e87bb0d3SMatthew G Knepley 
52803a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5281e87bb0d3SMatthew G Knepley 
5282e87bb0d3SMatthew G Knepley   Input Parameters:
5283e87bb0d3SMatthew G Knepley + dm - The DM
52843a93e3b7SToby Isaac . v - The Vec of points
528562a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
52863a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5287e87bb0d3SMatthew G Knepley 
528861e3bb9bSMatthew G Knepley   Output Parameter:
528962a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
529062a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
52913a93e3b7SToby Isaac 
5292e87bb0d3SMatthew G Knepley 
5293e87bb0d3SMatthew G Knepley   Level: developer
529461e3bb9bSMatthew G Knepley 
529562a38674SMatthew G. Knepley   Notes:
52963a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
529762a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
52983a93e3b7SToby Isaac 
52993a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
530062a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
53013a93e3b7SToby Isaac 
53023a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
53033a93e3b7SToby Isaac 
530462a38674SMatthew G. Knepley $    const PetscSFNode *cells;
530562a38674SMatthew G. Knepley $    PetscInt           nFound;
5306a6216909SToby Isaac $    const PetscInt    *found;
530762a38674SMatthew G. Knepley $
5308a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
53093a93e3b7SToby Isaac 
53103a93e3b7SToby 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
53113a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
53123a93e3b7SToby Isaac 
531361e3bb9bSMatthew G Knepley .keywords: point location, mesh
531462a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
531561e3bb9bSMatthew G Knepley @*/
531662a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5317e87bb0d3SMatthew G Knepley {
5318735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5319735aa83eSMatthew G Knepley 
5320e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5321e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5322e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5323e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
53243a93e3b7SToby Isaac   if (*cellSF) {
53253a93e3b7SToby Isaac     PetscMPIInt result;
53263a93e3b7SToby Isaac 
5327e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5328a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
53293a93e3b7SToby 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");
5330e0fc9d1bSMatthew G. Knepley   } else {
53313a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
53323a93e3b7SToby Isaac   }
533347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5334735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
533562a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
533682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
533747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5338e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5339e87bb0d3SMatthew G Knepley }
534014f150ffSMatthew G. Knepley 
5341f4d763aaSMatthew G. Knepley /*@
5342f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5343f4d763aaSMatthew G. Knepley 
5344f4d763aaSMatthew G. Knepley   Input Parameter:
5345f4d763aaSMatthew G. Knepley . dm - The original DM
5346f4d763aaSMatthew G. Knepley 
5347f4d763aaSMatthew G. Knepley   Output Parameter:
5348f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5349f4d763aaSMatthew G. Knepley 
5350f4d763aaSMatthew G. Knepley   Level: intermediate
5351f4d763aaSMatthew G. Knepley 
5352e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
5353f4d763aaSMatthew G. Knepley @*/
535414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
535514f150ffSMatthew G. Knepley {
5356c26acbdeSMatthew G. Knepley   PetscSection   section;
53572d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
535814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
535914f150ffSMatthew G. Knepley 
536014f150ffSMatthew G. Knepley   PetscFunctionBegin;
536114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
536214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5363e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5364c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5365127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
53662d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5367c26acbdeSMatthew G. Knepley     *odm = dm;
5368c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5369c26acbdeSMatthew G. Knepley   }
537014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
53710305aff6SMatthew G. Knepley     PetscDS      ds;
5372c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
537314f150ffSMatthew G. Knepley     PetscSF      sf;
537414f150ffSMatthew G. Knepley 
537514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
53760305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
53770305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
537814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5379e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
538014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
538114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
538215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5383e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
538414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
538514f150ffSMatthew G. Knepley   }
538614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
538714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
538814f150ffSMatthew G. Knepley }
5389f4d763aaSMatthew G. Knepley 
5390f4d763aaSMatthew G. Knepley /*@
5391cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5392f4d763aaSMatthew G. Knepley 
5393f4d763aaSMatthew G. Knepley   Input Parameter:
5394f4d763aaSMatthew G. Knepley . dm - The original DM
5395f4d763aaSMatthew G. Knepley 
5396cdb7a50dSMatthew G. Knepley   Output Parameters:
5397cdb7a50dSMatthew G. Knepley + num - The output sequence number
5398cdb7a50dSMatthew G. Knepley - val - The output sequence value
5399f4d763aaSMatthew G. Knepley 
5400f4d763aaSMatthew G. Knepley   Level: intermediate
5401f4d763aaSMatthew G. Knepley 
5402f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5403f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5404f4d763aaSMatthew G. Knepley 
5405f4d763aaSMatthew G. Knepley .seealso: VecView()
5406f4d763aaSMatthew G. Knepley @*/
5407cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5408f4d763aaSMatthew G. Knepley {
5409f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5410f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5411cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5412cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5413f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5414f4d763aaSMatthew G. Knepley }
5415f4d763aaSMatthew G. Knepley 
5416f4d763aaSMatthew G. Knepley /*@
5417cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5418f4d763aaSMatthew G. Knepley 
5419f4d763aaSMatthew G. Knepley   Input Parameters:
5420f4d763aaSMatthew G. Knepley + dm - The original DM
5421cdb7a50dSMatthew G. Knepley . num - The output sequence number
5422cdb7a50dSMatthew G. Knepley - val - The output sequence value
5423f4d763aaSMatthew G. Knepley 
5424f4d763aaSMatthew G. Knepley   Level: intermediate
5425f4d763aaSMatthew G. Knepley 
5426f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5427f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5428f4d763aaSMatthew G. Knepley 
5429f4d763aaSMatthew G. Knepley .seealso: VecView()
5430f4d763aaSMatthew G. Knepley @*/
5431cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5432f4d763aaSMatthew G. Knepley {
5433f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5434f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5435f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5436cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5437cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5438cdb7a50dSMatthew G. Knepley }
5439cdb7a50dSMatthew G. Knepley 
5440cdb7a50dSMatthew G. Knepley /*@C
5441cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5442cdb7a50dSMatthew G. Knepley 
5443cdb7a50dSMatthew G. Knepley   Input Parameters:
5444cdb7a50dSMatthew G. Knepley + dm   - The original DM
5445cdb7a50dSMatthew G. Knepley . name - The sequence name
5446cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5447cdb7a50dSMatthew G. Knepley 
5448cdb7a50dSMatthew G. Knepley   Output Parameter:
5449cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5450cdb7a50dSMatthew G. Knepley 
5451cdb7a50dSMatthew G. Knepley   Level: intermediate
5452cdb7a50dSMatthew G. Knepley 
5453cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5454cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5455cdb7a50dSMatthew G. Knepley 
5456cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5457cdb7a50dSMatthew G. Knepley @*/
5458cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5459cdb7a50dSMatthew G. Knepley {
5460cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5461cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5462cdb7a50dSMatthew G. Knepley 
5463cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5464cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5465cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5466cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5467cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5468cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5469cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5470cdb7a50dSMatthew G. Knepley     PetscScalar value;
5471cdb7a50dSMatthew G. Knepley 
547239d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
54734aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5474cdb7a50dSMatthew G. Knepley #endif
5475cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5476f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5477f4d763aaSMatthew G. Knepley }
54788e4ac7eaSMatthew G. Knepley 
54798e4ac7eaSMatthew G. Knepley /*@
54808e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
54818e4ac7eaSMatthew G. Knepley 
54828e4ac7eaSMatthew G. Knepley   Not collective
54838e4ac7eaSMatthew G. Knepley 
54848e4ac7eaSMatthew G. Knepley   Input Parameter:
54858e4ac7eaSMatthew G. Knepley . dm - The DM
54868e4ac7eaSMatthew G. Knepley 
54878e4ac7eaSMatthew G. Knepley   Output Parameter:
54888e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
54898e4ac7eaSMatthew G. Knepley 
54908e4ac7eaSMatthew G. Knepley   Level: beginner
54918e4ac7eaSMatthew G. Knepley 
54928e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
54938e4ac7eaSMatthew G. Knepley @*/
54948e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
54958e4ac7eaSMatthew G. Knepley {
54968e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
54978e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54988e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
54998e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
55008e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
55018e4ac7eaSMatthew G. Knepley }
55028e4ac7eaSMatthew G. Knepley 
55038e4ac7eaSMatthew G. Knepley /*@
55045d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
55058e4ac7eaSMatthew G. Knepley 
55068e4ac7eaSMatthew G. Knepley   Collective on dm
55078e4ac7eaSMatthew G. Knepley 
55088e4ac7eaSMatthew G. Knepley   Input Parameters:
55098e4ac7eaSMatthew G. Knepley + dm - The DM
55108e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
55118e4ac7eaSMatthew G. Knepley 
55125d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
55135d3b26e6SMatthew G. Knepley 
55148e4ac7eaSMatthew G. Knepley   Level: beginner
55158e4ac7eaSMatthew G. Knepley 
55165d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
55178e4ac7eaSMatthew G. Knepley @*/
55188e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
55198e4ac7eaSMatthew G. Knepley {
55208e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
55218e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55228833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
55238e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
55248e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
55258e4ac7eaSMatthew G. Knepley }
5526c58f1c22SToby Isaac 
5527c58f1c22SToby Isaac 
5528c58f1c22SToby Isaac /*@C
5529c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5530c58f1c22SToby Isaac 
5531c58f1c22SToby Isaac   Not Collective
5532c58f1c22SToby Isaac 
5533c58f1c22SToby Isaac   Input Parameters:
5534c58f1c22SToby Isaac + dm   - The DM object
5535c58f1c22SToby Isaac - name - The label name
5536c58f1c22SToby Isaac 
5537c58f1c22SToby Isaac   Level: intermediate
5538c58f1c22SToby Isaac 
5539c58f1c22SToby Isaac .keywords: mesh
5540c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5541c58f1c22SToby Isaac @*/
5542c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5543c58f1c22SToby Isaac {
5544c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5545c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5546c58f1c22SToby Isaac   PetscErrorCode ierr;
5547c58f1c22SToby Isaac 
5548c58f1c22SToby Isaac   PetscFunctionBegin;
5549c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5550c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5551c58f1c22SToby Isaac   while (next) {
5552c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5553c58f1c22SToby Isaac     if (flg) break;
5554c58f1c22SToby Isaac     next = next->next;
5555c58f1c22SToby Isaac   }
5556c58f1c22SToby Isaac   if (!flg) {
5557c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5558c58f1c22SToby Isaac 
5559c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5560c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5561c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5562c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5563c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5564c58f1c22SToby Isaac   }
5565c58f1c22SToby Isaac   PetscFunctionReturn(0);
5566c58f1c22SToby Isaac }
5567c58f1c22SToby Isaac 
5568c58f1c22SToby Isaac /*@C
5569c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5570c58f1c22SToby Isaac 
5571c58f1c22SToby Isaac   Not Collective
5572c58f1c22SToby Isaac 
5573c58f1c22SToby Isaac   Input Parameters:
5574c58f1c22SToby Isaac + dm   - The DM object
5575c58f1c22SToby Isaac . name - The label name
5576c58f1c22SToby Isaac - point - The mesh point
5577c58f1c22SToby Isaac 
5578c58f1c22SToby Isaac   Output Parameter:
5579c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5580c58f1c22SToby Isaac 
5581c58f1c22SToby Isaac   Level: beginner
5582c58f1c22SToby Isaac 
5583c58f1c22SToby Isaac .keywords: mesh
5584c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5585c58f1c22SToby Isaac @*/
5586c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5587c58f1c22SToby Isaac {
5588c58f1c22SToby Isaac   DMLabel        label;
5589c58f1c22SToby Isaac   PetscErrorCode ierr;
5590c58f1c22SToby Isaac 
5591c58f1c22SToby Isaac   PetscFunctionBegin;
5592c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5593c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5594c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
559513903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5596c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5597c58f1c22SToby Isaac   PetscFunctionReturn(0);
5598c58f1c22SToby Isaac }
5599c58f1c22SToby Isaac 
5600c58f1c22SToby Isaac /*@C
5601c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5602c58f1c22SToby Isaac 
5603c58f1c22SToby Isaac   Not Collective
5604c58f1c22SToby Isaac 
5605c58f1c22SToby Isaac   Input Parameters:
5606c58f1c22SToby Isaac + dm   - The DM object
5607c58f1c22SToby Isaac . name - The label name
5608c58f1c22SToby Isaac . point - The mesh point
5609c58f1c22SToby Isaac - value - The label value for this point
5610c58f1c22SToby Isaac 
5611c58f1c22SToby Isaac   Output Parameter:
5612c58f1c22SToby Isaac 
5613c58f1c22SToby Isaac   Level: beginner
5614c58f1c22SToby Isaac 
5615c58f1c22SToby Isaac .keywords: mesh
5616c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5617c58f1c22SToby Isaac @*/
5618c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5619c58f1c22SToby Isaac {
5620c58f1c22SToby Isaac   DMLabel        label;
5621c58f1c22SToby Isaac   PetscErrorCode ierr;
5622c58f1c22SToby Isaac 
5623c58f1c22SToby Isaac   PetscFunctionBegin;
5624c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5625c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5626c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5627c58f1c22SToby Isaac   if (!label) {
5628c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5629c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5630c58f1c22SToby Isaac   }
5631c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5632c58f1c22SToby Isaac   PetscFunctionReturn(0);
5633c58f1c22SToby Isaac }
5634c58f1c22SToby Isaac 
5635c58f1c22SToby Isaac /*@C
5636c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5637c58f1c22SToby Isaac 
5638c58f1c22SToby Isaac   Not Collective
5639c58f1c22SToby Isaac 
5640c58f1c22SToby Isaac   Input Parameters:
5641c58f1c22SToby Isaac + dm   - The DM object
5642c58f1c22SToby Isaac . name - The label name
5643c58f1c22SToby Isaac . point - The mesh point
5644c58f1c22SToby Isaac - value - The label value for this point
5645c58f1c22SToby Isaac 
5646c58f1c22SToby Isaac   Output Parameter:
5647c58f1c22SToby Isaac 
5648c58f1c22SToby Isaac   Level: beginner
5649c58f1c22SToby Isaac 
5650c58f1c22SToby Isaac .keywords: mesh
5651c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5652c58f1c22SToby Isaac @*/
5653c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5654c58f1c22SToby Isaac {
5655c58f1c22SToby Isaac   DMLabel        label;
5656c58f1c22SToby Isaac   PetscErrorCode ierr;
5657c58f1c22SToby Isaac 
5658c58f1c22SToby Isaac   PetscFunctionBegin;
5659c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5660c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5661c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5662c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5663c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5664c58f1c22SToby Isaac   PetscFunctionReturn(0);
5665c58f1c22SToby Isaac }
5666c58f1c22SToby Isaac 
5667c58f1c22SToby Isaac /*@C
5668c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5669c58f1c22SToby Isaac 
5670c58f1c22SToby Isaac   Not Collective
5671c58f1c22SToby Isaac 
5672c58f1c22SToby Isaac   Input Parameters:
5673c58f1c22SToby Isaac + dm   - The DM object
5674c58f1c22SToby Isaac - name - The label name
5675c58f1c22SToby Isaac 
5676c58f1c22SToby Isaac   Output Parameter:
5677c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5678c58f1c22SToby Isaac 
5679c58f1c22SToby Isaac   Level: beginner
5680c58f1c22SToby Isaac 
5681c58f1c22SToby Isaac .keywords: mesh
5682c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5683c58f1c22SToby Isaac @*/
5684c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5685c58f1c22SToby Isaac {
5686c58f1c22SToby Isaac   DMLabel        label;
5687c58f1c22SToby Isaac   PetscErrorCode ierr;
5688c58f1c22SToby Isaac 
5689c58f1c22SToby Isaac   PetscFunctionBegin;
5690c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5691c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5692c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5693c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5694c58f1c22SToby Isaac   *size = 0;
5695c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5696c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5697c58f1c22SToby Isaac   PetscFunctionReturn(0);
5698c58f1c22SToby Isaac }
5699c58f1c22SToby Isaac 
5700c58f1c22SToby Isaac /*@C
5701c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5702c58f1c22SToby Isaac 
5703c58f1c22SToby Isaac   Not Collective
5704c58f1c22SToby Isaac 
5705c58f1c22SToby Isaac   Input Parameters:
5706c58f1c22SToby Isaac + mesh - The DM object
5707c58f1c22SToby Isaac - name - The label name
5708c58f1c22SToby Isaac 
5709c58f1c22SToby Isaac   Output Parameter:
5710c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5711c58f1c22SToby Isaac 
5712c58f1c22SToby Isaac   Level: beginner
5713c58f1c22SToby Isaac 
5714c58f1c22SToby Isaac .keywords: mesh
5715c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5716c58f1c22SToby Isaac @*/
5717c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5718c58f1c22SToby Isaac {
5719c58f1c22SToby Isaac   DMLabel        label;
5720c58f1c22SToby Isaac   PetscErrorCode ierr;
5721c58f1c22SToby Isaac 
5722c58f1c22SToby Isaac   PetscFunctionBegin;
5723c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5724c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5725c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5726c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5727c58f1c22SToby Isaac   *ids = NULL;
5728dab2e251SBlaise Bourdin  if (label) {
5729c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5730dab2e251SBlaise Bourdin   } else {
5731dab2e251SBlaise Bourdin     /* returning an empty IS */
5732dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5733dab2e251SBlaise Bourdin   }
5734c58f1c22SToby Isaac   PetscFunctionReturn(0);
5735c58f1c22SToby Isaac }
5736c58f1c22SToby Isaac 
5737c58f1c22SToby Isaac /*@C
5738c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5739c58f1c22SToby Isaac 
5740c58f1c22SToby Isaac   Not Collective
5741c58f1c22SToby Isaac 
5742c58f1c22SToby Isaac   Input Parameters:
5743c58f1c22SToby Isaac + dm - The DM object
5744c58f1c22SToby Isaac . name - The label name
5745c58f1c22SToby Isaac - value - The stratum value
5746c58f1c22SToby Isaac 
5747c58f1c22SToby Isaac   Output Parameter:
5748c58f1c22SToby Isaac . size - The stratum size
5749c58f1c22SToby Isaac 
5750c58f1c22SToby Isaac   Level: beginner
5751c58f1c22SToby Isaac 
5752c58f1c22SToby Isaac .keywords: mesh
5753c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5754c58f1c22SToby Isaac @*/
5755c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5756c58f1c22SToby Isaac {
5757c58f1c22SToby Isaac   DMLabel        label;
5758c58f1c22SToby Isaac   PetscErrorCode ierr;
5759c58f1c22SToby Isaac 
5760c58f1c22SToby Isaac   PetscFunctionBegin;
5761c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5762c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5763c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5764c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5765c58f1c22SToby Isaac   *size = 0;
5766c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5767c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5768c58f1c22SToby Isaac   PetscFunctionReturn(0);
5769c58f1c22SToby Isaac }
5770c58f1c22SToby Isaac 
5771c58f1c22SToby Isaac /*@C
5772c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5773c58f1c22SToby Isaac 
5774c58f1c22SToby Isaac   Not Collective
5775c58f1c22SToby Isaac 
5776c58f1c22SToby Isaac   Input Parameters:
5777c58f1c22SToby Isaac + dm - The DM object
5778c58f1c22SToby Isaac . name - The label name
5779c58f1c22SToby Isaac - value - The stratum value
5780c58f1c22SToby Isaac 
5781c58f1c22SToby Isaac   Output Parameter:
5782c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5783c58f1c22SToby Isaac 
5784c58f1c22SToby Isaac   Level: beginner
5785c58f1c22SToby Isaac 
5786c58f1c22SToby Isaac .keywords: mesh
5787c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5788c58f1c22SToby Isaac @*/
5789c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5790c58f1c22SToby Isaac {
5791c58f1c22SToby Isaac   DMLabel        label;
5792c58f1c22SToby Isaac   PetscErrorCode ierr;
5793c58f1c22SToby Isaac 
5794c58f1c22SToby Isaac   PetscFunctionBegin;
5795c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5796c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5797c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5798c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5799c58f1c22SToby Isaac   *points = NULL;
5800c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5801c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5802c58f1c22SToby Isaac   PetscFunctionReturn(0);
5803c58f1c22SToby Isaac }
5804c58f1c22SToby Isaac 
58054de306b1SToby Isaac /*@C
58069044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
58074de306b1SToby Isaac 
58084de306b1SToby Isaac   Not Collective
58094de306b1SToby Isaac 
58104de306b1SToby Isaac   Input Parameters:
58114de306b1SToby Isaac + dm - The DM object
58124de306b1SToby Isaac . name - The label name
58134de306b1SToby Isaac . value - The stratum value
58144de306b1SToby Isaac - points - The stratum points
58154de306b1SToby Isaac 
58164de306b1SToby Isaac   Level: beginner
58174de306b1SToby Isaac 
58184de306b1SToby Isaac .keywords: mesh
58194de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
58204de306b1SToby Isaac @*/
58214de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
58224de306b1SToby Isaac {
58234de306b1SToby Isaac   DMLabel        label;
58244de306b1SToby Isaac   PetscErrorCode ierr;
58254de306b1SToby Isaac 
58264de306b1SToby Isaac   PetscFunctionBegin;
58274de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58284de306b1SToby Isaac   PetscValidCharPointer(name, 2);
58294de306b1SToby Isaac   PetscValidPointer(points, 4);
58304de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
58314de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
58324de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
58334de306b1SToby Isaac   PetscFunctionReturn(0);
58344de306b1SToby Isaac }
58354de306b1SToby Isaac 
5836c58f1c22SToby Isaac /*@C
5837c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5838c58f1c22SToby Isaac 
5839c58f1c22SToby Isaac   Not Collective
5840c58f1c22SToby Isaac 
5841c58f1c22SToby Isaac   Input Parameters:
5842c58f1c22SToby Isaac + dm   - The DM object
5843c58f1c22SToby Isaac . name - The label name
5844c58f1c22SToby Isaac - value - The label value for this point
5845c58f1c22SToby Isaac 
5846c58f1c22SToby Isaac   Output Parameter:
5847c58f1c22SToby Isaac 
5848c58f1c22SToby Isaac   Level: beginner
5849c58f1c22SToby Isaac 
5850c58f1c22SToby Isaac .keywords: mesh
5851c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5852c58f1c22SToby Isaac @*/
5853c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5854c58f1c22SToby Isaac {
5855c58f1c22SToby Isaac   DMLabel        label;
5856c58f1c22SToby Isaac   PetscErrorCode ierr;
5857c58f1c22SToby Isaac 
5858c58f1c22SToby Isaac   PetscFunctionBegin;
5859c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5860c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5861c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5862c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5863c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5864c58f1c22SToby Isaac   PetscFunctionReturn(0);
5865c58f1c22SToby Isaac }
5866c58f1c22SToby Isaac 
5867c58f1c22SToby Isaac /*@
5868c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5869c58f1c22SToby Isaac 
5870c58f1c22SToby Isaac   Not Collective
5871c58f1c22SToby Isaac 
5872c58f1c22SToby Isaac   Input Parameter:
5873c58f1c22SToby Isaac . dm   - The DM object
5874c58f1c22SToby Isaac 
5875c58f1c22SToby Isaac   Output Parameter:
5876c58f1c22SToby Isaac . numLabels - the number of Labels
5877c58f1c22SToby Isaac 
5878c58f1c22SToby Isaac   Level: intermediate
5879c58f1c22SToby Isaac 
5880c58f1c22SToby Isaac .keywords: mesh
5881c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5882c58f1c22SToby Isaac @*/
5883c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5884c58f1c22SToby Isaac {
5885c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5886c58f1c22SToby Isaac   PetscInt  n    = 0;
5887c58f1c22SToby Isaac 
5888c58f1c22SToby Isaac   PetscFunctionBegin;
5889c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5890c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5891c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5892c58f1c22SToby Isaac   *numLabels = n;
5893c58f1c22SToby Isaac   PetscFunctionReturn(0);
5894c58f1c22SToby Isaac }
5895c58f1c22SToby Isaac 
5896c58f1c22SToby Isaac /*@C
5897c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5898c58f1c22SToby Isaac 
5899c58f1c22SToby Isaac   Not Collective
5900c58f1c22SToby Isaac 
5901c58f1c22SToby Isaac   Input Parameters:
5902c58f1c22SToby Isaac + dm - The DM object
5903c58f1c22SToby Isaac - n  - the label number
5904c58f1c22SToby Isaac 
5905c58f1c22SToby Isaac   Output Parameter:
5906c58f1c22SToby Isaac . name - the label name
5907c58f1c22SToby Isaac 
5908c58f1c22SToby Isaac   Level: intermediate
5909c58f1c22SToby Isaac 
5910c58f1c22SToby Isaac .keywords: mesh
5911c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5912c58f1c22SToby Isaac @*/
5913c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5914c58f1c22SToby Isaac {
5915c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5916c58f1c22SToby Isaac   PetscInt  l    = 0;
5917c58f1c22SToby Isaac 
5918c58f1c22SToby Isaac   PetscFunctionBegin;
5919c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5920c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5921c58f1c22SToby Isaac   while (next) {
5922c58f1c22SToby Isaac     if (l == n) {
5923c58f1c22SToby Isaac       *name = next->label->name;
5924c58f1c22SToby Isaac       PetscFunctionReturn(0);
5925c58f1c22SToby Isaac     }
5926c58f1c22SToby Isaac     ++l;
5927c58f1c22SToby Isaac     next = next->next;
5928c58f1c22SToby Isaac   }
5929c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5930c58f1c22SToby Isaac }
5931c58f1c22SToby Isaac 
5932c58f1c22SToby Isaac /*@C
5933c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5934c58f1c22SToby Isaac 
5935c58f1c22SToby Isaac   Not Collective
5936c58f1c22SToby Isaac 
5937c58f1c22SToby Isaac   Input Parameters:
5938c58f1c22SToby Isaac + dm   - The DM object
5939c58f1c22SToby Isaac - name - The label name
5940c58f1c22SToby Isaac 
5941c58f1c22SToby Isaac   Output Parameter:
5942c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5943c58f1c22SToby Isaac 
5944c58f1c22SToby Isaac   Level: intermediate
5945c58f1c22SToby Isaac 
5946c58f1c22SToby Isaac .keywords: mesh
5947c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5948c58f1c22SToby Isaac @*/
5949c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5950c58f1c22SToby Isaac {
5951c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5952c58f1c22SToby Isaac   PetscErrorCode ierr;
5953c58f1c22SToby Isaac 
5954c58f1c22SToby Isaac   PetscFunctionBegin;
5955c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5956c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5957c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5958c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5959c58f1c22SToby Isaac   while (next) {
5960c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5961c58f1c22SToby Isaac     if (*hasLabel) break;
5962c58f1c22SToby Isaac     next = next->next;
5963c58f1c22SToby Isaac   }
5964c58f1c22SToby Isaac   PetscFunctionReturn(0);
5965c58f1c22SToby Isaac }
5966c58f1c22SToby Isaac 
5967c58f1c22SToby Isaac /*@C
5968c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5969c58f1c22SToby Isaac 
5970c58f1c22SToby Isaac   Not Collective
5971c58f1c22SToby Isaac 
5972c58f1c22SToby Isaac   Input Parameters:
5973c58f1c22SToby Isaac + dm   - The DM object
5974c58f1c22SToby Isaac - name - The label name
5975c58f1c22SToby Isaac 
5976c58f1c22SToby Isaac   Output Parameter:
5977c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5978c58f1c22SToby Isaac 
5979c58f1c22SToby Isaac   Level: intermediate
5980c58f1c22SToby Isaac 
5981c58f1c22SToby Isaac .keywords: mesh
5982c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5983c58f1c22SToby Isaac @*/
5984c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5985c58f1c22SToby Isaac {
5986c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5987c58f1c22SToby Isaac   PetscBool      hasLabel;
5988c58f1c22SToby Isaac   PetscErrorCode ierr;
5989c58f1c22SToby Isaac 
5990c58f1c22SToby Isaac   PetscFunctionBegin;
5991c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5992c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5993c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5994c58f1c22SToby Isaac   *label = NULL;
5995c58f1c22SToby Isaac   while (next) {
5996c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5997c58f1c22SToby Isaac     if (hasLabel) {
5998c58f1c22SToby Isaac       *label = next->label;
5999c58f1c22SToby Isaac       break;
6000c58f1c22SToby Isaac     }
6001c58f1c22SToby Isaac     next = next->next;
6002c58f1c22SToby Isaac   }
6003c58f1c22SToby Isaac   PetscFunctionReturn(0);
6004c58f1c22SToby Isaac }
6005c58f1c22SToby Isaac 
6006c58f1c22SToby Isaac /*@C
6007c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6008c58f1c22SToby Isaac 
6009c58f1c22SToby Isaac   Not Collective
6010c58f1c22SToby Isaac 
6011c58f1c22SToby Isaac   Input Parameters:
6012c58f1c22SToby Isaac + dm - The DM object
6013c58f1c22SToby Isaac - n  - the label number
6014c58f1c22SToby Isaac 
6015c58f1c22SToby Isaac   Output Parameter:
6016c58f1c22SToby Isaac . label - the label
6017c58f1c22SToby Isaac 
6018c58f1c22SToby Isaac   Level: intermediate
6019c58f1c22SToby Isaac 
6020c58f1c22SToby Isaac .keywords: mesh
6021c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6022c58f1c22SToby Isaac @*/
6023c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6024c58f1c22SToby Isaac {
6025c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6026c58f1c22SToby Isaac   PetscInt    l    = 0;
6027c58f1c22SToby Isaac 
6028c58f1c22SToby Isaac   PetscFunctionBegin;
6029c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6030c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6031c58f1c22SToby Isaac   while (next) {
6032c58f1c22SToby Isaac     if (l == n) {
6033c58f1c22SToby Isaac       *label = next->label;
6034c58f1c22SToby Isaac       PetscFunctionReturn(0);
6035c58f1c22SToby Isaac     }
6036c58f1c22SToby Isaac     ++l;
6037c58f1c22SToby Isaac     next = next->next;
6038c58f1c22SToby Isaac   }
6039c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6040c58f1c22SToby Isaac }
6041c58f1c22SToby Isaac 
6042c58f1c22SToby Isaac /*@C
6043c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
6044c58f1c22SToby Isaac 
6045c58f1c22SToby Isaac   Not Collective
6046c58f1c22SToby Isaac 
6047c58f1c22SToby Isaac   Input Parameters:
6048c58f1c22SToby Isaac + dm   - The DM object
6049c58f1c22SToby Isaac - label - The DMLabel
6050c58f1c22SToby Isaac 
6051c58f1c22SToby Isaac   Level: developer
6052c58f1c22SToby Isaac 
6053c58f1c22SToby Isaac .keywords: mesh
6054c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6055c58f1c22SToby Isaac @*/
6056c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6057c58f1c22SToby Isaac {
6058c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
6059c58f1c22SToby Isaac   PetscBool      hasLabel;
6060c58f1c22SToby Isaac   PetscErrorCode ierr;
6061c58f1c22SToby Isaac 
6062c58f1c22SToby Isaac   PetscFunctionBegin;
6063c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6064c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
6065c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
6066c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6067c58f1c22SToby Isaac   tmpLabel->label  = label;
6068c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
6069c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
6070c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
6071c58f1c22SToby Isaac   PetscFunctionReturn(0);
6072c58f1c22SToby Isaac }
6073c58f1c22SToby Isaac 
6074c58f1c22SToby Isaac /*@C
6075c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
6076c58f1c22SToby Isaac 
6077c58f1c22SToby Isaac   Not Collective
6078c58f1c22SToby Isaac 
6079c58f1c22SToby Isaac   Input Parameters:
6080c58f1c22SToby Isaac + dm   - The DM object
6081c58f1c22SToby Isaac - name - The label name
6082c58f1c22SToby Isaac 
6083c58f1c22SToby Isaac   Output Parameter:
6084c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6085c58f1c22SToby Isaac 
6086c58f1c22SToby Isaac   Level: developer
6087c58f1c22SToby Isaac 
6088c58f1c22SToby Isaac .keywords: mesh
6089c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6090c58f1c22SToby Isaac @*/
6091c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
6092c58f1c22SToby Isaac {
6093c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6094c58f1c22SToby Isaac   DMLabelLink    last = NULL;
6095c58f1c22SToby Isaac   PetscBool      hasLabel;
6096c58f1c22SToby Isaac   PetscErrorCode ierr;
6097c58f1c22SToby Isaac 
6098c58f1c22SToby Isaac   PetscFunctionBegin;
6099c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6100c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
6101c58f1c22SToby Isaac   *label = NULL;
6102c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
6103c58f1c22SToby Isaac   while (next) {
6104c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
6105c58f1c22SToby Isaac     if (hasLabel) {
6106c58f1c22SToby Isaac       if (last) last->next       = next->next;
6107c58f1c22SToby Isaac       else      dm->labels->next = next->next;
6108c58f1c22SToby Isaac       next->next = NULL;
6109c58f1c22SToby Isaac       *label     = next->label;
6110c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
6111c58f1c22SToby Isaac       if (hasLabel) {
6112c58f1c22SToby Isaac         dm->depthLabel = NULL;
6113c58f1c22SToby Isaac       }
6114c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
6115c58f1c22SToby Isaac       break;
6116c58f1c22SToby Isaac     }
6117c58f1c22SToby Isaac     last = next;
6118c58f1c22SToby Isaac     next = next->next;
6119c58f1c22SToby Isaac   }
6120c58f1c22SToby Isaac   PetscFunctionReturn(0);
6121c58f1c22SToby Isaac }
6122c58f1c22SToby Isaac 
6123c58f1c22SToby Isaac /*@C
6124c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6125c58f1c22SToby Isaac 
6126c58f1c22SToby Isaac   Not Collective
6127c58f1c22SToby Isaac 
6128c58f1c22SToby Isaac   Input Parameters:
6129c58f1c22SToby Isaac + dm   - The DM object
6130c58f1c22SToby Isaac - name - The label name
6131c58f1c22SToby Isaac 
6132c58f1c22SToby Isaac   Output Parameter:
6133c58f1c22SToby Isaac . output - The flag for output
6134c58f1c22SToby Isaac 
6135c58f1c22SToby Isaac   Level: developer
6136c58f1c22SToby Isaac 
6137c58f1c22SToby Isaac .keywords: mesh
6138c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6139c58f1c22SToby Isaac @*/
6140c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6141c58f1c22SToby Isaac {
6142c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6143c58f1c22SToby Isaac   PetscErrorCode ierr;
6144c58f1c22SToby Isaac 
6145c58f1c22SToby Isaac   PetscFunctionBegin;
6146c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6147c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6148c58f1c22SToby Isaac   PetscValidPointer(output, 3);
6149c58f1c22SToby Isaac   while (next) {
6150c58f1c22SToby Isaac     PetscBool flg;
6151c58f1c22SToby Isaac 
6152c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
6153c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6154c58f1c22SToby Isaac     next = next->next;
6155c58f1c22SToby Isaac   }
6156c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6157c58f1c22SToby Isaac }
6158c58f1c22SToby Isaac 
6159c58f1c22SToby Isaac /*@C
6160c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
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 - output - The flag for output
6168c58f1c22SToby Isaac 
6169c58f1c22SToby Isaac   Level: developer
6170c58f1c22SToby Isaac 
6171c58f1c22SToby Isaac .keywords: mesh
6172c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6173c58f1c22SToby Isaac @*/
6174c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
6175c58f1c22SToby Isaac {
6176c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6177c58f1c22SToby Isaac   PetscErrorCode ierr;
6178c58f1c22SToby Isaac 
6179c58f1c22SToby Isaac   PetscFunctionBegin;
6180c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6181c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6182c58f1c22SToby Isaac   while (next) {
6183c58f1c22SToby Isaac     PetscBool flg;
6184c58f1c22SToby Isaac 
6185c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
6186c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
6187c58f1c22SToby Isaac     next = next->next;
6188c58f1c22SToby Isaac   }
6189c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6190c58f1c22SToby Isaac }
6191c58f1c22SToby Isaac 
6192c58f1c22SToby Isaac 
6193c58f1c22SToby Isaac /*@
6194c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
6195c58f1c22SToby Isaac 
6196c58f1c22SToby Isaac   Collective on DM
6197c58f1c22SToby Isaac 
6198c58f1c22SToby Isaac   Input Parameter:
61992e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
6200c58f1c22SToby Isaac 
6201c58f1c22SToby Isaac   Output Parameter:
62022e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
6203c58f1c22SToby Isaac 
6204c58f1c22SToby Isaac   Level: intermediate
6205c58f1c22SToby Isaac 
6206c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
6207c58f1c22SToby Isaac 
6208c58f1c22SToby Isaac .keywords: mesh
6209367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
6210c58f1c22SToby Isaac @*/
6211c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
6212c58f1c22SToby Isaac {
6213c58f1c22SToby Isaac   PetscInt       numLabels, l;
6214c58f1c22SToby Isaac   PetscErrorCode ierr;
6215c58f1c22SToby Isaac 
6216c58f1c22SToby Isaac   PetscFunctionBegin;
6217c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
6218c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
6219c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
6220c58f1c22SToby Isaac     DMLabel     label, labelNew;
6221c58f1c22SToby Isaac     const char *name;
6222c58f1c22SToby Isaac     PetscBool   flg;
6223c58f1c22SToby Isaac 
6224c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
6225c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
6226c58f1c22SToby Isaac     if (flg) continue;
62277d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
62287d5acc75SStefano Zampini     if (flg) continue;
6229c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
6230c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
6231c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
6232c58f1c22SToby Isaac   }
6233c58f1c22SToby Isaac   PetscFunctionReturn(0);
6234c58f1c22SToby Isaac }
6235a8fb8f29SToby Isaac 
6236a8fb8f29SToby Isaac /*@
6237a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
6238a8fb8f29SToby Isaac 
6239a8fb8f29SToby Isaac   Input Parameter:
6240a8fb8f29SToby Isaac . dm - The DM object
6241a8fb8f29SToby Isaac 
6242a8fb8f29SToby Isaac   Output Parameter:
6243a8fb8f29SToby Isaac . cdm - The coarse DM
6244a8fb8f29SToby Isaac 
6245a8fb8f29SToby Isaac   Level: intermediate
6246a8fb8f29SToby Isaac 
6247a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
6248a8fb8f29SToby Isaac @*/
6249a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6250a8fb8f29SToby Isaac {
6251a8fb8f29SToby Isaac   PetscFunctionBegin;
6252a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6253a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
6254a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
6255a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6256a8fb8f29SToby Isaac }
6257a8fb8f29SToby Isaac 
6258a8fb8f29SToby Isaac /*@
6259a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6260a8fb8f29SToby Isaac 
6261a8fb8f29SToby Isaac   Input Parameters:
6262a8fb8f29SToby Isaac + dm - The DM object
6263a8fb8f29SToby Isaac - cdm - The coarse DM
6264a8fb8f29SToby Isaac 
6265a8fb8f29SToby Isaac   Level: intermediate
6266a8fb8f29SToby Isaac 
6267a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
6268a8fb8f29SToby Isaac @*/
6269a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6270a8fb8f29SToby Isaac {
6271a8fb8f29SToby Isaac   PetscErrorCode ierr;
6272a8fb8f29SToby Isaac 
6273a8fb8f29SToby Isaac   PetscFunctionBegin;
6274a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6275a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6276a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6277a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6278a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6279a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6280a8fb8f29SToby Isaac }
6281a8fb8f29SToby Isaac 
628288bdff64SToby Isaac /*@
628388bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
628488bdff64SToby Isaac 
628588bdff64SToby Isaac   Input Parameter:
628688bdff64SToby Isaac . dm - The DM object
628788bdff64SToby Isaac 
628888bdff64SToby Isaac   Output Parameter:
628988bdff64SToby Isaac . fdm - The fine DM
629088bdff64SToby Isaac 
629188bdff64SToby Isaac   Level: intermediate
629288bdff64SToby Isaac 
629388bdff64SToby Isaac .seealso: DMSetFineDM()
629488bdff64SToby Isaac @*/
629588bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
629688bdff64SToby Isaac {
629788bdff64SToby Isaac   PetscFunctionBegin;
629888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
629988bdff64SToby Isaac   PetscValidPointer(fdm, 2);
630088bdff64SToby Isaac   *fdm = dm->fineMesh;
630188bdff64SToby Isaac   PetscFunctionReturn(0);
630288bdff64SToby Isaac }
630388bdff64SToby Isaac 
630488bdff64SToby Isaac /*@
630588bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
630688bdff64SToby Isaac 
630788bdff64SToby Isaac   Input Parameters:
630888bdff64SToby Isaac + dm - The DM object
630988bdff64SToby Isaac - fdm - The fine DM
631088bdff64SToby Isaac 
631188bdff64SToby Isaac   Level: intermediate
631288bdff64SToby Isaac 
631388bdff64SToby Isaac .seealso: DMGetFineDM()
631488bdff64SToby Isaac @*/
631588bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
631688bdff64SToby Isaac {
631788bdff64SToby Isaac   PetscErrorCode ierr;
631888bdff64SToby Isaac 
631988bdff64SToby Isaac   PetscFunctionBegin;
632088bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
632188bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
632288bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
632388bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
632488bdff64SToby Isaac   dm->fineMesh = fdm;
632588bdff64SToby Isaac   PetscFunctionReturn(0);
632688bdff64SToby Isaac }
632788bdff64SToby Isaac 
6328a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6329a6ba4734SToby Isaac 
6330a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6331a6ba4734SToby Isaac {
6332a6ba4734SToby Isaac   PetscErrorCode ierr;
6333a6ba4734SToby Isaac 
6334a6ba4734SToby Isaac   PetscFunctionBegin;
6335e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6336a6ba4734SToby Isaac   PetscFunctionReturn(0);
6337a6ba4734SToby Isaac }
6338a6ba4734SToby Isaac 
6339a6ba4734SToby Isaac /*@C
6340a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6341a6ba4734SToby Isaac 
6342a6ba4734SToby Isaac   Input Parameters:
63434c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6344f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6345a6ba4734SToby Isaac . name        - The BC name
6346a6ba4734SToby Isaac . labelname   - The label defining constrained points
6347a6ba4734SToby Isaac . field       - The field to constrain
6348a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6349a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6350a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6351a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6352a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6353a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6354a6ba4734SToby Isaac 
6355a6ba4734SToby Isaac   Options Database Keys:
6356a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6357a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6358a6ba4734SToby Isaac 
6359a6ba4734SToby Isaac   Level: developer
6360a6ba4734SToby Isaac 
6361a6ba4734SToby Isaac .seealso: DMGetBoundary()
6362a6ba4734SToby Isaac @*/
6363a30ec4eaSSatish 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)
6364a6ba4734SToby Isaac {
6365a6ba4734SToby Isaac   PetscErrorCode ierr;
6366a6ba4734SToby Isaac 
6367a6ba4734SToby Isaac   PetscFunctionBegin;
6368a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6369f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6370a6ba4734SToby Isaac   PetscFunctionReturn(0);
6371a6ba4734SToby Isaac }
6372a6ba4734SToby Isaac 
6373a6ba4734SToby Isaac /*@
6374a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6375a6ba4734SToby Isaac 
6376a6ba4734SToby Isaac   Input Parameters:
6377a6ba4734SToby Isaac . dm - The mesh object
6378a6ba4734SToby Isaac 
6379a6ba4734SToby Isaac   Output Parameters:
6380a6ba4734SToby Isaac . numBd - The number of BC
6381a6ba4734SToby Isaac 
6382a6ba4734SToby Isaac   Level: intermediate
6383a6ba4734SToby Isaac 
6384a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6385a6ba4734SToby Isaac @*/
6386a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6387a6ba4734SToby Isaac {
638858ebd649SToby Isaac   PetscErrorCode ierr;
6389a6ba4734SToby Isaac 
6390a6ba4734SToby Isaac   PetscFunctionBegin;
6391a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
639258ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6393a6ba4734SToby Isaac   PetscFunctionReturn(0);
6394a6ba4734SToby Isaac }
6395a6ba4734SToby Isaac 
6396a6ba4734SToby Isaac /*@C
63971c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6398a6ba4734SToby Isaac 
6399a6ba4734SToby Isaac   Input Parameters:
6400a6ba4734SToby Isaac + dm          - The mesh object
6401a6ba4734SToby Isaac - bd          - The BC number
6402a6ba4734SToby Isaac 
6403a6ba4734SToby Isaac   Output Parameters:
6404f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6405a6ba4734SToby Isaac . name        - The BC name
6406a6ba4734SToby Isaac . labelname   - The label defining constrained points
6407a6ba4734SToby Isaac . field       - The field to constrain
6408a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6409a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6410a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6411a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6412a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6413a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6414a6ba4734SToby Isaac 
6415a6ba4734SToby Isaac   Options Database Keys:
6416a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6417a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6418a6ba4734SToby Isaac 
6419a6ba4734SToby Isaac   Level: developer
6420a6ba4734SToby Isaac 
6421a6ba4734SToby Isaac .seealso: DMAddBoundary()
6422a6ba4734SToby Isaac @*/
6423a30ec4eaSSatish 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)
6424a6ba4734SToby Isaac {
642558ebd649SToby Isaac   PetscErrorCode ierr;
6426a6ba4734SToby Isaac 
6427a6ba4734SToby Isaac   PetscFunctionBegin;
6428a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6429f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6430a6ba4734SToby Isaac   PetscFunctionReturn(0);
6431a6ba4734SToby Isaac }
6432a6ba4734SToby Isaac 
6433e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6434e6f8dbb6SToby Isaac {
6435dff059c6SToby Isaac   DMBoundary *lastnext;
6436e6f8dbb6SToby Isaac   DSBoundary dsbound;
6437e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6438e6f8dbb6SToby Isaac 
6439e6f8dbb6SToby Isaac   PetscFunctionBegin;
6440e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
644147a1f5adSToby Isaac   if (dm->boundary) {
644247a1f5adSToby Isaac     DMBoundary next = dm->boundary;
644347a1f5adSToby Isaac 
644447a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
644547a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
644647a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
644747a1f5adSToby Isaac     while (next) {
644847a1f5adSToby Isaac       DMBoundary b = next;
644947a1f5adSToby Isaac 
645047a1f5adSToby Isaac       next = b->next;
645147a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6452a6ba4734SToby Isaac     }
645347a1f5adSToby Isaac     dm->boundary = NULL;
6454a6ba4734SToby Isaac   }
645547a1f5adSToby Isaac 
6456dff059c6SToby Isaac   lastnext = &(dm->boundary);
6457e6f8dbb6SToby Isaac   while (dsbound) {
6458e6f8dbb6SToby Isaac     DMBoundary dmbound;
6459e6f8dbb6SToby Isaac 
6460e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6461e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6462e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6463994fe344SLisandro 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);}
646447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6465dff059c6SToby Isaac     *lastnext = dmbound;
6466dff059c6SToby Isaac     lastnext = &(dmbound->next);
6467dff059c6SToby Isaac     dsbound = dsbound->next;
6468a6ba4734SToby Isaac   }
6469a6ba4734SToby Isaac   PetscFunctionReturn(0);
6470a6ba4734SToby Isaac }
6471a6ba4734SToby Isaac 
6472a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6473a6ba4734SToby Isaac {
6474b95f2879SToby Isaac   DMBoundary     b;
6475a6ba4734SToby Isaac   PetscErrorCode ierr;
6476a6ba4734SToby Isaac 
6477a6ba4734SToby Isaac   PetscFunctionBegin;
6478a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6479a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6480a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6481e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6482b95f2879SToby Isaac   b = dm->boundary;
6483a6ba4734SToby Isaac   while (b && !(*isBd)) {
6484e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6485e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
64863424c85cSToby Isaac 
64873424c85cSToby Isaac     if (label) {
6488a6ba4734SToby Isaac       PetscInt i;
6489a6ba4734SToby Isaac 
6490e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6491e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6492a6ba4734SToby Isaac       }
6493a6ba4734SToby Isaac     }
6494a6ba4734SToby Isaac     b = b->next;
6495a6ba4734SToby Isaac   }
6496a6ba4734SToby Isaac   PetscFunctionReturn(0);
6497a6ba4734SToby Isaac }
64984d6f44ffSToby Isaac 
64994d6f44ffSToby Isaac /*@C
65004d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
65014d6f44ffSToby Isaac 
65024d6f44ffSToby Isaac   Input Parameters:
65034d6f44ffSToby Isaac + dm      - The DM
65040709b2feSToby Isaac . time    - The time
65054d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
65064d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
65074d6f44ffSToby Isaac - mode    - The insertion mode for values
65084d6f44ffSToby Isaac 
65094d6f44ffSToby Isaac   Output Parameter:
65104d6f44ffSToby Isaac . X - vector
65114d6f44ffSToby Isaac 
65124d6f44ffSToby Isaac    Calling sequence of func:
65130709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
65144d6f44ffSToby Isaac 
65154d6f44ffSToby Isaac +  dim - The spatial dimension
65164d6f44ffSToby Isaac .  x   - The coordinates
65174d6f44ffSToby Isaac .  Nf  - The number of fields
65184d6f44ffSToby Isaac .  u   - The output field values
65194d6f44ffSToby Isaac -  ctx - optional user-defined function context
65204d6f44ffSToby Isaac 
65214d6f44ffSToby Isaac   Level: developer
65224d6f44ffSToby Isaac 
65232716604bSToby Isaac .seealso: DMComputeL2Diff()
65244d6f44ffSToby Isaac @*/
65250709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
65264d6f44ffSToby Isaac {
65274d6f44ffSToby Isaac   Vec            localX;
65284d6f44ffSToby Isaac   PetscErrorCode ierr;
65294d6f44ffSToby Isaac 
65304d6f44ffSToby Isaac   PetscFunctionBegin;
65314d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65324d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
65330709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
65344d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
65354d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
65364d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
65374d6f44ffSToby Isaac   PetscFunctionReturn(0);
65384d6f44ffSToby Isaac }
65394d6f44ffSToby Isaac 
65400709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
65414d6f44ffSToby Isaac {
65424d6f44ffSToby Isaac   PetscErrorCode ierr;
65434d6f44ffSToby Isaac 
65444d6f44ffSToby Isaac   PetscFunctionBegin;
65454d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65464d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
65470918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
65480709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
65494d6f44ffSToby Isaac   PetscFunctionReturn(0);
65504d6f44ffSToby Isaac }
65514d6f44ffSToby Isaac 
65522c53366bSMatthew 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)
65532c53366bSMatthew G. Knepley {
65542c53366bSMatthew G. Knepley   Vec            localX;
65552c53366bSMatthew G. Knepley   PetscErrorCode ierr;
65562c53366bSMatthew G. Knepley 
65572c53366bSMatthew G. Knepley   PetscFunctionBegin;
65582c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65592c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
65602c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
65612c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
65622c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
65632c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
65642c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
65652c53366bSMatthew G. Knepley }
65662c53366bSMatthew G. Knepley 
65671c531cf8SMatthew 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)
65684d6f44ffSToby Isaac {
65694d6f44ffSToby Isaac   PetscErrorCode ierr;
65704d6f44ffSToby Isaac 
65714d6f44ffSToby Isaac   PetscFunctionBegin;
65724d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65734d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
65740918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
65751c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
65764d6f44ffSToby Isaac   PetscFunctionReturn(0);
65774d6f44ffSToby Isaac }
65782716604bSToby Isaac 
65798c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
65808c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
65818c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
65828c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6583191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
65848c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
65858c6c5593SMatthew G. Knepley {
65868c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
65878c6c5593SMatthew G. Knepley 
65888c6c5593SMatthew G. Knepley   PetscFunctionBegin;
65898c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65908c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
65918c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
65920918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
65938c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
65948c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
65958c6c5593SMatthew G. Knepley }
65968c6c5593SMatthew G. Knepley 
65971c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
65988c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
65998c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
66008c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6601191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
66028c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
66038c6c5593SMatthew G. Knepley {
66048c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
66058c6c5593SMatthew G. Knepley 
66068c6c5593SMatthew G. Knepley   PetscFunctionBegin;
66078c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66088c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
66098c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
66100918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
66111c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
66128c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
66138c6c5593SMatthew G. Knepley }
66148c6c5593SMatthew G. Knepley 
66152716604bSToby Isaac /*@C
66162716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
66172716604bSToby Isaac 
66182716604bSToby Isaac   Input Parameters:
66192716604bSToby Isaac + dm    - The DM
66200709b2feSToby Isaac . time  - The time
66212716604bSToby Isaac . funcs - The functions to evaluate for each field component
66222716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6623574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
66242716604bSToby Isaac 
66252716604bSToby Isaac   Output Parameter:
66262716604bSToby Isaac . diff - The diff ||u - u_h||_2
66272716604bSToby Isaac 
66282716604bSToby Isaac   Level: developer
66292716604bSToby Isaac 
66301189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
66312716604bSToby Isaac @*/
66320709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
66332716604bSToby Isaac {
66342716604bSToby Isaac   PetscErrorCode ierr;
66352716604bSToby Isaac 
66362716604bSToby Isaac   PetscFunctionBegin;
66372716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6638b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
66390918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
66400709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
66412716604bSToby Isaac   PetscFunctionReturn(0);
66422716604bSToby Isaac }
6643b698f381SToby Isaac 
6644b698f381SToby Isaac /*@C
6645b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6646b698f381SToby Isaac 
6647b698f381SToby Isaac   Input Parameters:
6648b698f381SToby Isaac + dm    - The DM
6649b698f381SToby Isaac , time  - The time
6650b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6651b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6652574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6653b698f381SToby Isaac - n     - The vector to project along
6654b698f381SToby Isaac 
6655b698f381SToby Isaac   Output Parameter:
6656b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6657b698f381SToby Isaac 
6658b698f381SToby Isaac   Level: developer
6659b698f381SToby Isaac 
6660b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6661b698f381SToby Isaac @*/
6662b698f381SToby 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)
6663b698f381SToby Isaac {
6664b698f381SToby Isaac   PetscErrorCode ierr;
6665b698f381SToby Isaac 
6666b698f381SToby Isaac   PetscFunctionBegin;
6667b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6668b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6669b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6670b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6671b698f381SToby Isaac   PetscFunctionReturn(0);
6672b698f381SToby Isaac }
6673b698f381SToby Isaac 
66742a16baeaSToby Isaac /*@C
66752a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
66762a16baeaSToby Isaac 
66772a16baeaSToby Isaac   Input Parameters:
66782a16baeaSToby Isaac + dm    - The DM
66792a16baeaSToby Isaac . time  - The time
66802a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
66812a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6682574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
66832a16baeaSToby Isaac 
66842a16baeaSToby Isaac   Output Parameter:
66852a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
66862a16baeaSToby Isaac 
66872a16baeaSToby Isaac   Level: developer
66882a16baeaSToby Isaac 
66891189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
66902a16baeaSToby Isaac @*/
66911189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
66922a16baeaSToby Isaac {
66932a16baeaSToby Isaac   PetscErrorCode ierr;
66942a16baeaSToby Isaac 
66952a16baeaSToby Isaac   PetscFunctionBegin;
66962a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66972a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
66980918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
66992a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
67002a16baeaSToby Isaac   PetscFunctionReturn(0);
67012a16baeaSToby Isaac }
67022a16baeaSToby Isaac 
6703df0b854cSToby Isaac /*@C
6704df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6705cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6706df0b854cSToby Isaac 
6707df0b854cSToby Isaac   Collective on dm
6708df0b854cSToby Isaac 
6709df0b854cSToby Isaac   Input parameters:
6710df0b854cSToby Isaac + dm - the pre-adaptation DM object
6711a1b0c543SToby Isaac - label - label with the flags
6712df0b854cSToby Isaac 
6713df0b854cSToby Isaac   Output parameters:
67140d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6715df0b854cSToby Isaac 
6716df0b854cSToby Isaac   Level: intermediate
67170d1cd5e0SMatthew G. Knepley 
67180d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6719df0b854cSToby Isaac @*/
67200d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6721df0b854cSToby Isaac {
6722df0b854cSToby Isaac   PetscErrorCode ierr;
6723df0b854cSToby Isaac 
6724df0b854cSToby Isaac   PetscFunctionBegin;
6725df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6726a1b0c543SToby Isaac   PetscValidPointer(label,2);
67270d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
67280d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
67296f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
67300d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
67310d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
67320d1cd5e0SMatthew G. Knepley }
67330d1cd5e0SMatthew G. Knepley 
67340d1cd5e0SMatthew G. Knepley /*@C
67350d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
67360d1cd5e0SMatthew G. Knepley 
67370d1cd5e0SMatthew G. Knepley   Input Parameters:
67380d1cd5e0SMatthew G. Knepley + dm - The DM object
67390d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
67406f25b0d8SLisandro 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_".
67410d1cd5e0SMatthew G. Knepley 
67420d1cd5e0SMatthew G. Knepley   Output Parameter:
67430d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
67440d1cd5e0SMatthew G. Knepley 
67450d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
67460d1cd5e0SMatthew G. Knepley 
67470d1cd5e0SMatthew G. Knepley   Level: advanced
67480d1cd5e0SMatthew G. Knepley 
67490d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
67500d1cd5e0SMatthew G. Knepley @*/
67510d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
67520d1cd5e0SMatthew G. Knepley {
67530d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
67540d1cd5e0SMatthew G. Knepley 
67550d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
67560d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67570d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
67580d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
67590d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
67606f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
67616f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
67620d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6763df0b854cSToby Isaac   PetscFunctionReturn(0);
6764df0b854cSToby Isaac }
6765c4088d22SMatthew G. Knepley 
6766502a2867SDave May /*@C
6767502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6768502a2867SDave May 
6769502a2867SDave May  Not Collective
6770502a2867SDave May 
6771502a2867SDave May  Input Parameter:
6772502a2867SDave May  . dm    - The DM
6773502a2867SDave May 
6774502a2867SDave May  Output Parameter:
6775502a2867SDave May  . nranks - the number of neighbours
6776502a2867SDave May  . ranks - the neighbors ranks
6777502a2867SDave May 
6778502a2867SDave May  Notes:
6779502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6780502a2867SDave May 
6781502a2867SDave May  Level: beginner
6782502a2867SDave May 
6783dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6784502a2867SDave May @*/
6785502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6786502a2867SDave May {
6787502a2867SDave May   PetscErrorCode ierr;
6788502a2867SDave May 
6789502a2867SDave May   PetscFunctionBegin;
6790502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67910918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6792502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6793502a2867SDave May   PetscFunctionReturn(0);
6794502a2867SDave May }
6795502a2867SDave May 
6796531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6797531c7667SBarry Smith 
6798531c7667SBarry Smith /*
6799531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6800531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6801531c7667SBarry Smith */
6802531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6803531c7667SBarry Smith {
6804531c7667SBarry Smith   PetscErrorCode ierr;
6805531c7667SBarry Smith 
6806531c7667SBarry Smith   PetscFunctionBegin;
6807531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6808531c7667SBarry Smith     Vec x1local;
6809531c7667SBarry Smith     DM  dm;
6810531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6811531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6812531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6813531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6814531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6815531c7667SBarry Smith     x1   = x1local;
6816531c7667SBarry Smith   }
6817531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6818531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6819531c7667SBarry Smith     DM  dm;
6820531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6821531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6822531c7667SBarry Smith   }
6823531c7667SBarry Smith   PetscFunctionReturn(0);
6824531c7667SBarry Smith }
6825531c7667SBarry Smith 
6826531c7667SBarry Smith /*@
6827531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6828531c7667SBarry Smith 
6829531c7667SBarry Smith     Input Parameter:
6830531c7667SBarry Smith .    coloring - the MatFDColoring object
6831531c7667SBarry Smith 
683295452b02SPatrick Sanan     Developer Notes:
683395452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
6834531c7667SBarry Smith 
68351b266c99SBarry Smith     Level: advanced
68361b266c99SBarry Smith 
6837531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6838531c7667SBarry Smith @*/
6839531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6840531c7667SBarry Smith {
6841531c7667SBarry Smith   PetscFunctionBegin;
6842531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6843531c7667SBarry Smith   PetscFunctionReturn(0);
6844531c7667SBarry Smith }
68458320bc6fSPatrick Sanan 
68468320bc6fSPatrick Sanan /*@
68478320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
68488320bc6fSPatrick Sanan 
68498320bc6fSPatrick Sanan     Collective
68508320bc6fSPatrick Sanan 
68518320bc6fSPatrick Sanan     Input Parameters:
68528320bc6fSPatrick Sanan +    dm - the first DM
68538320bc6fSPatrick Sanan -    dm2 - the second DM
68548320bc6fSPatrick Sanan 
68558320bc6fSPatrick Sanan     Output Parameters:
68568320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
68578320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
68588320bc6fSPatrick Sanan 
68598320bc6fSPatrick Sanan     Notes:
68608320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
68618320bc6fSPatrick Sanan     of the same topology. This implies that the the section (field data) on one
68628320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
68638320bc6fSPatrick Sanan     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
68648320bc6fSPatrick Sanan     decomposition, with different data.
68658320bc6fSPatrick Sanan 
68668320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
68678320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
68688320bc6fSPatrick Sanan 
68698320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
68708320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
68718320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
68728320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
68738320bc6fSPatrick Sanan 
68748320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
68758320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
68768320bc6fSPatrick Sanan .vb
68778320bc6fSPatrick Sanan   ...
68788320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
68798320bc6fSPatrick Sanan   if (set && compatible)  {
68808320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
68818320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
68828320bc6fSPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
68838320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
68848320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
68858320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
68868320bc6fSPatrick Sanan       }
68878320bc6fSPatrick Sanan     }
68888320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
68898320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
68908320bc6fSPatrick Sanan   } else {
68918320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
68928320bc6fSPatrick Sanan   }
68938320bc6fSPatrick Sanan   ...
68948320bc6fSPatrick Sanan .ve
68958320bc6fSPatrick Sanan 
68968320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
68978320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
68988320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
68998320bc6fSPatrick Sanan     always check the "set" output parameter.
69008320bc6fSPatrick Sanan 
69018320bc6fSPatrick Sanan     A DM is always compatible with itself.
69028320bc6fSPatrick Sanan 
69038320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
69048320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
69058320bc6fSPatrick Sanan     incompatible.
69068320bc6fSPatrick Sanan 
69078320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
69088320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
69098320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
69108320bc6fSPatrick Sanan 
69118320bc6fSPatrick Sanan     Developer Notes:
69128320bc6fSPatrick Sanan     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
69138320bc6fSPatrick Sanan     the DM B is compatible with DM A. Thus, this function checks the implementations
69148320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
69158320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
69168320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
69178320bc6fSPatrick Sanan     logic for this function, to check for existing logic in the implementation
69188320bc6fSPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found; the logic of this
69198320bc6fSPatrick Sanan     function will then call that logic.
69208320bc6fSPatrick Sanan 
69218320bc6fSPatrick Sanan     Level: advanced
69228320bc6fSPatrick Sanan 
69238320bc6fSPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA()
69248320bc6fSPatrick Sanan @*/
69258320bc6fSPatrick Sanan 
69268320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
69278320bc6fSPatrick Sanan {
69288320bc6fSPatrick Sanan   PetscErrorCode ierr;
69298320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
69308320bc6fSPatrick Sanan   DMType         type,type2;
69318320bc6fSPatrick Sanan   PetscBool      sameType;
69328320bc6fSPatrick Sanan 
69338320bc6fSPatrick Sanan   PetscFunctionBegin;
69348320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
69358320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
69368320bc6fSPatrick Sanan 
69378320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
69388320bc6fSPatrick Sanan   if (dm == dm2) {
69398320bc6fSPatrick Sanan     *set = PETSC_TRUE;
69408320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
69418320bc6fSPatrick Sanan     PetscFunctionReturn(0);
69428320bc6fSPatrick Sanan   }
69438320bc6fSPatrick Sanan 
69448320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
69458320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
69468320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
69478320bc6fSPatrick Sanan      determined by the implementation-specific logic */
69488320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
69498320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
69508320bc6fSPatrick Sanan     *set = PETSC_TRUE;
69518320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
69528320bc6fSPatrick Sanan     PetscFunctionReturn(0);
69538320bc6fSPatrick Sanan   }
69548320bc6fSPatrick Sanan 
69558320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
69568320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
69578320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
69588320bc6fSPatrick Sanan     if (*set) {
69598320bc6fSPatrick Sanan       PetscFunctionReturn(0);
69608320bc6fSPatrick Sanan     }
69618320bc6fSPatrick Sanan   }
69628320bc6fSPatrick Sanan 
69638320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
69648320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
69658320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
69668320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
69678320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
69688320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
69698320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
69708320bc6fSPatrick Sanan   } else {
69718320bc6fSPatrick Sanan     *set = PETSC_FALSE;
69728320bc6fSPatrick Sanan   }
69738320bc6fSPatrick Sanan   PetscFunctionReturn(0);
69748320bc6fSPatrick Sanan }
6975