xref: /petsc/src/dm/interface/dm.c (revision 01729b5c4ceb077be39438443e26df7af3cb0de5)
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);
1097cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
109825296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1099cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110047c6ae99SBarry Smith   PetscFunctionReturn(0);
110147c6ae99SBarry Smith }
110247c6ae99SBarry Smith 
11033ad4599aSBarry Smith /*@
11043ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11053ad4599aSBarry Smith 
11063ad4599aSBarry Smith     Collective on DM
11073ad4599aSBarry Smith 
11083ad4599aSBarry Smith     Input Parameter:
11093ad4599aSBarry Smith +   dm1 - the DM object
11103ad4599aSBarry Smith -   dm2 - the second, finer DM object
11113ad4599aSBarry Smith 
11123ad4599aSBarry Smith     Output Parameter:
11133ad4599aSBarry Smith .  mat - the restriction
11143ad4599aSBarry Smith 
11153ad4599aSBarry Smith 
11163ad4599aSBarry Smith     Level: developer
11173ad4599aSBarry Smith 
111895452b02SPatrick Sanan     Notes:
111995452b02SPatrick 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
11203ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11213ad4599aSBarry Smith 
11223ad4599aSBarry Smith 
11233ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11243ad4599aSBarry Smith 
11253ad4599aSBarry Smith @*/
11263ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11273ad4599aSBarry Smith {
11283ad4599aSBarry Smith   PetscErrorCode ierr;
11293ad4599aSBarry Smith 
11303ad4599aSBarry Smith   PetscFunctionBegin;
11313ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11323ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11333ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11347294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11353ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11363ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11373ad4599aSBarry Smith   PetscFunctionReturn(0);
11383ad4599aSBarry Smith }
11393ad4599aSBarry Smith 
114047c6ae99SBarry Smith /*@
11418472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
114247c6ae99SBarry Smith 
114347c6ae99SBarry Smith     Collective on DM
114447c6ae99SBarry Smith 
114547c6ae99SBarry Smith     Input Parameter:
114647c6ae99SBarry Smith +   dm1 - the DM object
114747c6ae99SBarry Smith -   dm2 - the second, finer DM object
114847c6ae99SBarry Smith 
114947c6ae99SBarry Smith     Output Parameter:
11506dbf9973SLawrence Mitchell .   mat - the injection
115147c6ae99SBarry Smith 
115247c6ae99SBarry Smith     Level: developer
115347c6ae99SBarry Smith 
115495452b02SPatrick Sanan    Notes:
115595452b02SPatrick 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
115685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
115785afcc9aSBarry Smith 
1158e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
115947c6ae99SBarry Smith 
116047c6ae99SBarry Smith @*/
11616dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
116247c6ae99SBarry Smith {
116347c6ae99SBarry Smith   PetscErrorCode ierr;
116447c6ae99SBarry Smith 
116547c6ae99SBarry Smith   PetscFunctionBegin;
1166171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1167171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11680c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11696dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
117047c6ae99SBarry Smith   PetscFunctionReturn(0);
117147c6ae99SBarry Smith }
117247c6ae99SBarry Smith 
1173b412c318SBarry Smith /*@
1174bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1175bd041c0cSMatthew G. Knepley 
1176bd041c0cSMatthew G. Knepley   Collective on DM
1177bd041c0cSMatthew G. Knepley 
1178bd041c0cSMatthew G. Knepley   Input Parameter:
1179bd041c0cSMatthew G. Knepley + dm1 - the DM object
1180bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1181bd041c0cSMatthew G. Knepley 
1182bd041c0cSMatthew G. Knepley   Output Parameter:
1183bd041c0cSMatthew G. Knepley . mat - the interpolation
1184bd041c0cSMatthew G. Knepley 
1185bd041c0cSMatthew G. Knepley   Level: developer
1186bd041c0cSMatthew G. Knepley 
1187bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1188bd041c0cSMatthew G. Knepley @*/
1189bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1190bd041c0cSMatthew G. Knepley {
1191bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1192bd041c0cSMatthew G. Knepley 
1193bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1194bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1195bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1196bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1197bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1198bd041c0cSMatthew G. Knepley }
1199bd041c0cSMatthew G. Knepley 
1200bd041c0cSMatthew G. Knepley /*@
1201b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
120247c6ae99SBarry Smith 
120347c6ae99SBarry Smith     Collective on DM
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith     Input Parameter:
120647c6ae99SBarry Smith +   dm - the DM object
12075bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
120847c6ae99SBarry Smith 
120947c6ae99SBarry Smith     Output Parameter:
121047c6ae99SBarry Smith .   coloring - the coloring
121147c6ae99SBarry Smith 
121247c6ae99SBarry Smith     Level: developer
121347c6ae99SBarry Smith 
1214b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
121547c6ae99SBarry Smith 
1216aab9d709SJed Brown @*/
1217b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
121847c6ae99SBarry Smith {
121947c6ae99SBarry Smith   PetscErrorCode ierr;
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith   PetscFunctionBegin;
1222171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1223ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1224b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
122547c6ae99SBarry Smith   PetscFunctionReturn(0);
122647c6ae99SBarry Smith }
122747c6ae99SBarry Smith 
1228b412c318SBarry Smith /*@
12298472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
123047c6ae99SBarry Smith 
123147c6ae99SBarry Smith     Collective on DM
123247c6ae99SBarry Smith 
123347c6ae99SBarry Smith     Input Parameter:
1234b412c318SBarry Smith .   dm - the DM object
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith     Output Parameter:
123747c6ae99SBarry Smith .   mat - the empty Jacobian
123847c6ae99SBarry Smith 
1239073dac72SJed Brown     Level: beginner
124047c6ae99SBarry Smith 
124195452b02SPatrick Sanan     Notes:
124295452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
124394013140SBarry Smith        do not need to do it yourself.
124494013140SBarry Smith 
124594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12467889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
124794013140SBarry Smith 
124894013140SBarry 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
124994013140SBarry Smith        internally by PETSc.
125094013140SBarry Smith 
125194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1252aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
125394013140SBarry Smith 
1254b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
125547c6ae99SBarry Smith 
1256aab9d709SJed Brown @*/
1257b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
125847c6ae99SBarry Smith {
125947c6ae99SBarry Smith   PetscErrorCode ierr;
126047c6ae99SBarry Smith 
126147c6ae99SBarry Smith   PetscFunctionBegin;
1262171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1263607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1264c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1265c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1266b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1267e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1268e571a35bSMatthew G. Knepley   if (dm->prob) {
1269e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1270e571a35bSMatthew G. Knepley     PetscInt     Nf;
1271e571a35bSMatthew G. Knepley 
1272e571a35bSMatthew G. Knepley     ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
1273e571a35bSMatthew G. Knepley     if (Nf == 1) {
1274e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1275e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1276e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1277e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1278e571a35bSMatthew G. Knepley       }
1279e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1280e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1281e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1282e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1283e571a35bSMatthew G. Knepley       }
1284e571a35bSMatthew G. Knepley     }
1285e571a35bSMatthew G. Knepley   }
128647c6ae99SBarry Smith   PetscFunctionReturn(0);
128747c6ae99SBarry Smith }
128847c6ae99SBarry Smith 
1289732e2eb9SMatthew G Knepley /*@
1290950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1291732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1292732e2eb9SMatthew G Knepley 
12938472ad0fSDave May   Logically Collective on DM
1294732e2eb9SMatthew G Knepley 
1295732e2eb9SMatthew G Knepley   Input Parameter:
1296732e2eb9SMatthew G Knepley + dm - the DM
1297732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1298732e2eb9SMatthew G Knepley 
1299732e2eb9SMatthew G Knepley   Level: developer
1300b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1301732e2eb9SMatthew G Knepley @*/
1302732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1303732e2eb9SMatthew G Knepley {
1304732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1305732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1306732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1307732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1308732e2eb9SMatthew G Knepley }
1309732e2eb9SMatthew G Knepley 
1310b06ff27eSHong Zhang /*@
1311b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1312b06ff27eSHong Zhang     but the array for values will not be allocated.
1313b06ff27eSHong Zhang 
1314b06ff27eSHong Zhang   Logically Collective on DM
1315b06ff27eSHong Zhang 
1316b06ff27eSHong Zhang   Input Parameter:
1317b06ff27eSHong Zhang + dm - the DM
1318b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1319b06ff27eSHong Zhang 
1320b06ff27eSHong Zhang   Level: developer
1321b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1322b06ff27eSHong Zhang @*/
1323b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1324b06ff27eSHong Zhang {
1325b06ff27eSHong Zhang   PetscFunctionBegin;
1326b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1327b06ff27eSHong Zhang   dm->structure_only = only;
1328b06ff27eSHong Zhang   PetscFunctionReturn(0);
1329b06ff27eSHong Zhang }
1330b06ff27eSHong Zhang 
1331a89ea682SMatthew G Knepley /*@C
1332aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1333a89ea682SMatthew G Knepley 
1334a89ea682SMatthew G Knepley   Not Collective
1335a89ea682SMatthew G Knepley 
1336a89ea682SMatthew G Knepley   Input Parameters:
1337a89ea682SMatthew G Knepley + dm - the DM object
1338aa1993deSMatthew G Knepley . count - The minium size
133969291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1340a89ea682SMatthew G Knepley 
1341a89ea682SMatthew G Knepley   Output Parameter:
1342a89ea682SMatthew G Knepley . array - the work array
1343a89ea682SMatthew G Knepley 
1344a89ea682SMatthew G Knepley   Level: developer
1345a89ea682SMatthew G Knepley 
1346a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1347a89ea682SMatthew G Knepley @*/
134869291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1349a89ea682SMatthew G Knepley {
1350a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1351aa1993deSMatthew G Knepley   DMWorkLink     link;
135269291d52SBarry Smith   PetscMPIInt    dsize;
1353a89ea682SMatthew G Knepley 
1354a89ea682SMatthew G Knepley   PetscFunctionBegin;
1355a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1356aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1357aa1993deSMatthew G Knepley   if (dm->workin) {
1358aa1993deSMatthew G Knepley     link       = dm->workin;
1359aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1360aa1993deSMatthew G Knepley   } else {
1361b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1362a89ea682SMatthew G Knepley   }
136369291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13645056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1365aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1366854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1367854ce69bSBarry Smith     link->bytes = dsize*count;
1368aa1993deSMatthew G Knepley   }
1369aa1993deSMatthew G Knepley   link->next   = dm->workout;
1370aa1993deSMatthew G Knepley   dm->workout  = link;
1371aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1372a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1373a89ea682SMatthew G Knepley }
1374a89ea682SMatthew G Knepley 
1375aa1993deSMatthew G Knepley /*@C
1376aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1377aa1993deSMatthew G Knepley 
1378aa1993deSMatthew G Knepley   Not Collective
1379aa1993deSMatthew G Knepley 
1380aa1993deSMatthew G Knepley   Input Parameters:
1381aa1993deSMatthew G Knepley + dm - the DM object
1382aa1993deSMatthew G Knepley . count - The minium size
138369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1384aa1993deSMatthew G Knepley 
1385aa1993deSMatthew G Knepley   Output Parameter:
1386aa1993deSMatthew G Knepley . array - the work array
1387aa1993deSMatthew G Knepley 
1388aa1993deSMatthew G Knepley   Level: developer
1389aa1993deSMatthew G Knepley 
139095452b02SPatrick Sanan   Developer Notes:
139195452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1392aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1393aa1993deSMatthew G Knepley @*/
139469291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1395aa1993deSMatthew G Knepley {
1396aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1397aa1993deSMatthew G Knepley 
1398aa1993deSMatthew G Knepley   PetscFunctionBegin;
1399aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1400aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1401aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1402aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1403aa1993deSMatthew G Knepley       *p           = link->next;
1404aa1993deSMatthew G Knepley       link->next   = dm->workin;
1405aa1993deSMatthew G Knepley       dm->workin   = link;
14060298fd71SBarry Smith       *(void**)mem = NULL;
1407aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1408aa1993deSMatthew G Knepley     }
1409aa1993deSMatthew G Knepley   }
1410aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1411aa1993deSMatthew G Knepley }
1412e7c4fc90SDmitry Karpeev 
1413435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1414435a35e8SMatthew G Knepley {
1415435a35e8SMatthew G Knepley   PetscFunctionBegin;
1416435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
141782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1418435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1419435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1420435a35e8SMatthew G Knepley }
1421435a35e8SMatthew G Knepley 
14220a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14230a50eb56SMatthew G. Knepley {
14240a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14250a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1426f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14270a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14280a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14290a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14300a50eb56SMatthew G. Knepley }
14310a50eb56SMatthew G. Knepley 
1432f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1433f9d4088aSMatthew G. Knepley {
1434f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1435f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1436f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1437f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1438f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1439f9d4088aSMatthew G. Knepley }
1440f9d4088aSMatthew G. Knepley 
1441f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1442f9d4088aSMatthew G. Knepley {
1443f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1444f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1445f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1446f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1447f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1448f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1449f9d4088aSMatthew G. Knepley }
1450f9d4088aSMatthew G. Knepley 
14514f3b5142SJed Brown /*@C
14524d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14534d343eeaSMatthew G Knepley 
14544d343eeaSMatthew G Knepley   Not collective
14554d343eeaSMatthew G Knepley 
14564d343eeaSMatthew G Knepley   Input Parameter:
14574d343eeaSMatthew G Knepley . dm - the DM object
14584d343eeaSMatthew G Knepley 
14594d343eeaSMatthew G Knepley   Output Parameters:
14600298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14610298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14620298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14634d343eeaSMatthew G Knepley 
14644d343eeaSMatthew G Knepley   Level: intermediate
14654d343eeaSMatthew G Knepley 
146621c9b008SJed Brown   Notes:
146721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
146821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
146921c9b008SJed Brown   PetscFree().
147021c9b008SJed Brown 
14714d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14724d343eeaSMatthew G Knepley @*/
147337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14744d343eeaSMatthew G Knepley {
147537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14764d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14774d343eeaSMatthew G Knepley 
14784d343eeaSMatthew G Knepley   PetscFunctionBegin;
14794d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
148069ca1f37SDmitry Karpeev   if (numFields) {
148169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
148269ca1f37SDmitry Karpeev     *numFields = 0;
148369ca1f37SDmitry Karpeev   }
148437d0c07bSMatthew G Knepley   if (fieldNames) {
148537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14860298fd71SBarry Smith     *fieldNames = NULL;
148769ca1f37SDmitry Karpeev   }
148869ca1f37SDmitry Karpeev   if (fields) {
148969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
14900298fd71SBarry Smith     *fields = NULL;
149169ca1f37SDmitry Karpeev   }
1492e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
149337d0c07bSMatthew G Knepley   if (section) {
149437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
149537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
149637d0c07bSMatthew G Knepley 
1497e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
149837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1499dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
150037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
150137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
150237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
150337d0c07bSMatthew G Knepley     }
150437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
150537d0c07bSMatthew G Knepley       PetscInt gdof;
150637d0c07bSMatthew G Knepley 
150737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
150837d0c07bSMatthew G Knepley       if (gdof > 0) {
150937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
151037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
151137d0c07bSMatthew G Knepley 
151237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
151337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
151437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
151537d0c07bSMatthew G Knepley         }
151637d0c07bSMatthew G Knepley       }
151737d0c07bSMatthew G Knepley     }
151837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1519785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
152037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
152137d0c07bSMatthew G Knepley     }
152237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
152337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
152437d0c07bSMatthew G Knepley 
152537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
152637d0c07bSMatthew G Knepley       if (gdof > 0) {
152737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
152837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
152937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
153037d0c07bSMatthew G Knepley 
153137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
153237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
153337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
153437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
153537d0c07bSMatthew G Knepley           }
153637d0c07bSMatthew G Knepley         }
153737d0c07bSMatthew G Knepley       }
153837d0c07bSMatthew G Knepley     }
15398865f1eaSKarl Rupp     if (numFields) *numFields = nF;
154037d0c07bSMatthew G Knepley     if (fieldNames) {
1541785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
154237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
154337d0c07bSMatthew G Knepley         const char *fieldName;
154437d0c07bSMatthew G Knepley 
154537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
154637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
154737d0c07bSMatthew G Knepley       }
154837d0c07bSMatthew G Knepley     }
154937d0c07bSMatthew G Knepley     if (fields) {
1550785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
155137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
155282f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
155337d0c07bSMatthew G Knepley       }
155437d0c07bSMatthew G Knepley     }
155537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
15568865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15578865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
155869ca1f37SDmitry Karpeev   }
15594d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15604d343eeaSMatthew G Knepley }
15614d343eeaSMatthew G Knepley 
156216621825SDmitry Karpeev 
156316621825SDmitry Karpeev /*@C
156416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
156516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
156616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1567e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1568e7c4fc90SDmitry Karpeev 
1569e7c4fc90SDmitry Karpeev   Not collective
1570e7c4fc90SDmitry Karpeev 
1571e7c4fc90SDmitry Karpeev   Input Parameter:
1572e7c4fc90SDmitry Karpeev . dm - the DM object
1573e7c4fc90SDmitry Karpeev 
1574e7c4fc90SDmitry Karpeev   Output Parameters:
15750298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15760298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15770298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15780298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1579e7c4fc90SDmitry Karpeev 
1580e7c4fc90SDmitry Karpeev   Level: intermediate
1581e7c4fc90SDmitry Karpeev 
1582e7c4fc90SDmitry Karpeev   Notes:
1583e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1584e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1585e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1586e7c4fc90SDmitry Karpeev 
1587e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1588e7c4fc90SDmitry Karpeev @*/
158916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1590e7c4fc90SDmitry Karpeev {
1591e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1592e7c4fc90SDmitry Karpeev 
1593e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1594e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15958865f1eaSKarl Rupp   if (len) {
15968865f1eaSKarl Rupp     PetscValidPointer(len,2);
15978865f1eaSKarl Rupp     *len = 0;
15988865f1eaSKarl Rupp   }
15998865f1eaSKarl Rupp   if (namelist) {
16008865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16018865f1eaSKarl Rupp     *namelist = 0;
16028865f1eaSKarl Rupp   }
16038865f1eaSKarl Rupp   if (islist) {
16048865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16058865f1eaSKarl Rupp     *islist = 0;
16068865f1eaSKarl Rupp   }
16078865f1eaSKarl Rupp   if (dmlist) {
16088865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16098865f1eaSKarl Rupp     *dmlist = 0;
16108865f1eaSKarl Rupp   }
1611f3f0edfdSDmitry Karpeev   /*
1612f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1613f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1614f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1615f3f0edfdSDmitry Karpeev    */
1616ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
161716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1618435a35e8SMatthew G Knepley     PetscSection section;
1619435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1620435a35e8SMatthew G Knepley 
1621e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1622435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1623435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1624f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
162503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
162603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
162703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1628435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1629435a35e8SMatthew G Knepley         const char *fieldName;
1630435a35e8SMatthew G Knepley 
163103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
163203dc3394SMatthew G. Knepley         if (namelist) {
1633435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1634435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1635435a35e8SMatthew G Knepley         }
163603dc3394SMatthew G. Knepley       }
1637435a35e8SMatthew G Knepley     } else {
163869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1639e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16400298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1641e7c4fc90SDmitry Karpeev     }
16428865f1eaSKarl Rupp   } else {
164316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
164416621825SDmitry Karpeev   }
164516621825SDmitry Karpeev   PetscFunctionReturn(0);
164616621825SDmitry Karpeev }
164716621825SDmitry Karpeev 
1648564cec59SMatthew G. Knepley /*@
1649435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1650435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1651435a35e8SMatthew G Knepley 
1652435a35e8SMatthew G Knepley   Not collective
1653435a35e8SMatthew G Knepley 
1654435a35e8SMatthew G Knepley   Input Parameters:
16552adcc780SMatthew G. Knepley + dm        - The DM object
16562adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16572adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1658435a35e8SMatthew G Knepley 
1659435a35e8SMatthew G Knepley   Output Parameters:
16602adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16612adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1662435a35e8SMatthew G Knepley 
1663435a35e8SMatthew G Knepley   Level: intermediate
1664435a35e8SMatthew G Knepley 
1665435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1666435a35e8SMatthew G Knepley @*/
166737bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1668435a35e8SMatthew G Knepley {
1669435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1670435a35e8SMatthew G Knepley 
1671435a35e8SMatthew G Knepley   PetscFunctionBegin;
1672435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1673435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16748865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16758865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1676435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1677435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
167882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1679435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1680435a35e8SMatthew G Knepley }
1681435a35e8SMatthew G Knepley 
16822adcc780SMatthew G. Knepley /*@C
16832adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
16842adcc780SMatthew G. Knepley 
16852adcc780SMatthew G. Knepley   Not collective
16862adcc780SMatthew G. Knepley 
16872adcc780SMatthew G. Knepley   Input Parameter:
16882adcc780SMatthew G. Knepley + dms - The DM objects
16892adcc780SMatthew G. Knepley - len - The number of DMs
16902adcc780SMatthew G. Knepley 
16912adcc780SMatthew G. Knepley   Output Parameters:
1692a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
16932adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
16942adcc780SMatthew G. Knepley 
16952adcc780SMatthew G. Knepley   Level: intermediate
16962adcc780SMatthew G. Knepley 
16972adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
16982adcc780SMatthew G. Knepley @*/
16992adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17002adcc780SMatthew G. Knepley {
17012adcc780SMatthew G. Knepley   PetscInt       i;
17022adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17032adcc780SMatthew G. Knepley 
17042adcc780SMatthew G. Knepley   PetscFunctionBegin;
17052adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17062adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17072adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1708a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17092adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17102adcc780SMatthew G. Knepley   if (len) {
1711a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1712a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17132adcc780SMatthew G. Knepley   }
17142adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17152adcc780SMatthew G. Knepley }
17162adcc780SMatthew G. Knepley 
171716621825SDmitry Karpeev 
171816621825SDmitry Karpeev /*@C
17198d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17208d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17218d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17228d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17238d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
172416621825SDmitry Karpeev 
172516621825SDmitry Karpeev   Not collective
172616621825SDmitry Karpeev 
172716621825SDmitry Karpeev   Input Parameter:
172816621825SDmitry Karpeev . dm - the DM object
172916621825SDmitry Karpeev 
173016621825SDmitry Karpeev   Output Parameters:
17310298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17320298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17330298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17340298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17350298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
173616621825SDmitry Karpeev 
173716621825SDmitry Karpeev   Level: intermediate
173816621825SDmitry Karpeev 
173916621825SDmitry Karpeev   Notes:
174016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
174116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
174216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
174316621825SDmitry Karpeev 
17448d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
174516621825SDmitry Karpeev @*/
17468d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
174716621825SDmitry Karpeev {
174816621825SDmitry Karpeev   PetscErrorCode      ierr;
1749be081cd6SPeter Brune   DMSubDomainHookLink link;
1750be081cd6SPeter Brune   PetscInt            i,l;
175116621825SDmitry Karpeev 
175216621825SDmitry Karpeev   PetscFunctionBegin;
175316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
175414a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17550298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17560298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17570298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17580298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1759f3f0edfdSDmitry Karpeev   /*
1760f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1761f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1762f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1763f3f0edfdSDmitry Karpeev    */
1764ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
176516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1766be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
176714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1768f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1769be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1770be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1771be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1772be081cd6SPeter Brune         }
1773648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1774e7c4fc90SDmitry Karpeev       }
177514a18fd3SPeter Brune     }
177614a18fd3SPeter Brune     if (len) *len = l;
177714a18fd3SPeter Brune   }
1778e30e807fSPeter Brune   PetscFunctionReturn(0);
1779e30e807fSPeter Brune }
1780e30e807fSPeter Brune 
1781e30e807fSPeter Brune 
1782e30e807fSPeter Brune /*@C
1783e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1784e30e807fSPeter Brune 
1785e30e807fSPeter Brune   Not collective
1786e30e807fSPeter Brune 
1787e30e807fSPeter Brune   Input Parameters:
1788e30e807fSPeter Brune + dm - the DM object
1789e30e807fSPeter Brune . n  - the number of subdomain scatters
1790e30e807fSPeter Brune - subdms - the local subdomains
1791e30e807fSPeter Brune 
1792e30e807fSPeter Brune   Output Parameters:
1793e30e807fSPeter Brune + n     - the number of scatters returned
1794e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1795e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1796e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1797e30e807fSPeter Brune 
179895452b02SPatrick Sanan   Notes:
179995452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1800e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1801e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1802e30e807fSPeter Brune   solution and residual data.
1803e30e807fSPeter Brune 
1804e30e807fSPeter Brune   Level: developer
1805e30e807fSPeter Brune 
1806e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1807e30e807fSPeter Brune @*/
1808e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1809e30e807fSPeter Brune {
1810e30e807fSPeter Brune   PetscErrorCode ierr;
1811e30e807fSPeter Brune 
1812e30e807fSPeter Brune   PetscFunctionBegin;
1813e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1814e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1815e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1816e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18176668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1818e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1819e7c4fc90SDmitry Karpeev }
1820e7c4fc90SDmitry Karpeev 
182147c6ae99SBarry Smith /*@
182247c6ae99SBarry Smith   DMRefine - Refines a DM object
182347c6ae99SBarry Smith 
182447c6ae99SBarry Smith   Collective on DM
182547c6ae99SBarry Smith 
182647c6ae99SBarry Smith   Input Parameter:
182747c6ae99SBarry Smith + dm   - the DM object
182891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
182947c6ae99SBarry Smith 
183047c6ae99SBarry Smith   Output Parameter:
18310298fd71SBarry Smith . dmf - the refined DM, or NULL
1832ae0a1c52SMatthew G Knepley 
18330298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
183447c6ae99SBarry Smith 
183547c6ae99SBarry Smith   Level: developer
183647c6ae99SBarry Smith 
1837e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
183847c6ae99SBarry Smith @*/
18397087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
184047c6ae99SBarry Smith {
184147c6ae99SBarry Smith   PetscErrorCode   ierr;
1842c833c3b5SJed Brown   DMRefineHookLink link;
184347c6ae99SBarry Smith 
184447c6ae99SBarry Smith   PetscFunctionBegin;
1845732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18461ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1847fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
184847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18494057135bSMatthew G Knepley   if (*dmf) {
185043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18518865f1eaSKarl Rupp 
18528cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18538865f1eaSKarl Rupp 
1854644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18550598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1856656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18578865f1eaSKarl Rupp 
1858e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1859c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18608865f1eaSKarl Rupp       if (link->refinehook) {
18618865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18628865f1eaSKarl Rupp       }
1863c833c3b5SJed Brown     }
1864c833c3b5SJed Brown   }
18651ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1866c833c3b5SJed Brown   PetscFunctionReturn(0);
1867c833c3b5SJed Brown }
1868c833c3b5SJed Brown 
1869bb9467b5SJed Brown /*@C
1870c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1871c833c3b5SJed Brown 
1872c833c3b5SJed Brown    Logically Collective
1873c833c3b5SJed Brown 
1874c833c3b5SJed Brown    Input Arguments:
1875c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1876c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1877c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18780298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1879c833c3b5SJed Brown 
1880c833c3b5SJed Brown    Calling sequence of refinehook:
1881c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1882c833c3b5SJed Brown 
1883c833c3b5SJed Brown +  coarse - coarse level DM
1884c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1885c833c3b5SJed Brown -  ctx - optional user-defined function context
1886c833c3b5SJed Brown 
1887c833c3b5SJed Brown    Calling sequence for interphook:
1888c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1889c833c3b5SJed Brown 
1890c833c3b5SJed Brown +  coarse - coarse level DM
1891c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1892c833c3b5SJed Brown .  fine - fine level DM to update
1893c833c3b5SJed Brown -  ctx - optional user-defined function context
1894c833c3b5SJed Brown 
1895c833c3b5SJed Brown    Level: advanced
1896c833c3b5SJed Brown 
1897c833c3b5SJed Brown    Notes:
1898c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1899c833c3b5SJed Brown 
1900c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1901c833c3b5SJed Brown 
1902bb9467b5SJed Brown    This function is currently not available from Fortran.
1903bb9467b5SJed Brown 
1904c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1905c833c3b5SJed Brown @*/
1906c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1907c833c3b5SJed Brown {
1908c833c3b5SJed Brown   PetscErrorCode   ierr;
1909c833c3b5SJed Brown   DMRefineHookLink link,*p;
1910c833c3b5SJed Brown 
1911c833c3b5SJed Brown   PetscFunctionBegin;
1912c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19133d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19143d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19153d8e3701SJed Brown   }
191695dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1917c833c3b5SJed Brown   link->refinehook = refinehook;
1918c833c3b5SJed Brown   link->interphook = interphook;
1919c833c3b5SJed Brown   link->ctx        = ctx;
19200298fd71SBarry Smith   link->next       = NULL;
1921c833c3b5SJed Brown   *p               = link;
1922c833c3b5SJed Brown   PetscFunctionReturn(0);
1923c833c3b5SJed Brown }
1924c833c3b5SJed Brown 
19253d8e3701SJed Brown /*@C
19263d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19273d8e3701SJed Brown 
19283d8e3701SJed Brown    Logically Collective
19293d8e3701SJed Brown 
19303d8e3701SJed Brown    Input Arguments:
19313d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19323d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19333d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19343d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19353d8e3701SJed Brown 
19363d8e3701SJed Brown    Level: advanced
19373d8e3701SJed Brown 
19383d8e3701SJed Brown    Notes:
19393d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19403d8e3701SJed Brown 
19413d8e3701SJed Brown    This function is currently not available from Fortran.
19423d8e3701SJed Brown 
19433d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19443d8e3701SJed Brown @*/
19453d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19463d8e3701SJed Brown {
19473d8e3701SJed Brown   PetscErrorCode   ierr;
19483d8e3701SJed Brown   DMRefineHookLink link,*p;
19493d8e3701SJed Brown 
19503d8e3701SJed Brown   PetscFunctionBegin;
19513d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19523d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19533d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19543d8e3701SJed Brown       link = *p;
19553d8e3701SJed Brown       *p = link->next;
19563d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19573d8e3701SJed Brown       break;
19583d8e3701SJed Brown     }
19593d8e3701SJed Brown   }
19603d8e3701SJed Brown   PetscFunctionReturn(0);
19613d8e3701SJed Brown }
19623d8e3701SJed Brown 
1963c833c3b5SJed Brown /*@
1964c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1965c833c3b5SJed Brown 
1966c833c3b5SJed Brown    Collective if any hooks are
1967c833c3b5SJed Brown 
1968c833c3b5SJed Brown    Input Arguments:
1969c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1970e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1971c833c3b5SJed Brown -  fine - finer DM to update
1972c833c3b5SJed Brown 
1973c833c3b5SJed Brown    Level: developer
1974c833c3b5SJed Brown 
1975c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1976c833c3b5SJed Brown @*/
1977c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1978c833c3b5SJed Brown {
1979c833c3b5SJed Brown   PetscErrorCode   ierr;
1980c833c3b5SJed Brown   DMRefineHookLink link;
1981c833c3b5SJed Brown 
1982c833c3b5SJed Brown   PetscFunctionBegin;
1983c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
19848865f1eaSKarl Rupp     if (link->interphook) {
19858865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
19868865f1eaSKarl Rupp     }
19874057135bSMatthew G Knepley   }
198847c6ae99SBarry Smith   PetscFunctionReturn(0);
198947c6ae99SBarry Smith }
199047c6ae99SBarry Smith 
1991eb3f98d2SBarry Smith /*@
1992eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1993eb3f98d2SBarry Smith 
1994eb3f98d2SBarry Smith     Not Collective
1995eb3f98d2SBarry Smith 
1996eb3f98d2SBarry Smith     Input Parameter:
1997eb3f98d2SBarry Smith .   dm - the DM object
1998eb3f98d2SBarry Smith 
1999eb3f98d2SBarry Smith     Output Parameter:
2000eb3f98d2SBarry Smith .   level - number of refinements
2001eb3f98d2SBarry Smith 
2002eb3f98d2SBarry Smith     Level: developer
2003eb3f98d2SBarry Smith 
20046a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2005eb3f98d2SBarry Smith 
2006eb3f98d2SBarry Smith @*/
2007eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2008eb3f98d2SBarry Smith {
2009eb3f98d2SBarry Smith   PetscFunctionBegin;
2010eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2011eb3f98d2SBarry Smith   *level = dm->levelup;
2012eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2013eb3f98d2SBarry Smith }
2014eb3f98d2SBarry Smith 
2015fef3a512SBarry Smith /*@
2016fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2017fef3a512SBarry Smith 
2018fef3a512SBarry Smith     Not Collective
2019fef3a512SBarry Smith 
2020fef3a512SBarry Smith     Input Parameter:
2021fef3a512SBarry Smith +   dm - the DM object
2022fef3a512SBarry Smith -   level - number of refinements
2023fef3a512SBarry Smith 
2024fef3a512SBarry Smith     Level: advanced
2025fef3a512SBarry Smith 
202695452b02SPatrick Sanan     Notes:
202795452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2028fef3a512SBarry Smith 
2029fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2030fef3a512SBarry Smith 
2031fef3a512SBarry Smith @*/
2032fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2033fef3a512SBarry Smith {
2034fef3a512SBarry Smith   PetscFunctionBegin;
2035fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2036fef3a512SBarry Smith   dm->levelup = level;
2037fef3a512SBarry Smith   PetscFunctionReturn(0);
2038fef3a512SBarry Smith }
2039fef3a512SBarry Smith 
2040bb9467b5SJed Brown /*@C
2041baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2042baf369e7SPeter Brune 
2043baf369e7SPeter Brune    Logically Collective
2044baf369e7SPeter Brune 
2045baf369e7SPeter Brune    Input Arguments:
2046baf369e7SPeter Brune +  dm - the DM
2047baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2048baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20490298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2050baf369e7SPeter Brune 
2051baf369e7SPeter Brune    Calling sequence for beginhook:
2052baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2053baf369e7SPeter Brune 
2054baf369e7SPeter Brune +  dm - global DM
2055baf369e7SPeter Brune .  g - global vector
2056baf369e7SPeter Brune .  mode - mode
2057baf369e7SPeter Brune .  l - local vector
2058baf369e7SPeter Brune -  ctx - optional user-defined function context
2059baf369e7SPeter Brune 
2060baf369e7SPeter Brune 
2061baf369e7SPeter Brune    Calling sequence for endhook:
2062ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2063baf369e7SPeter Brune 
2064baf369e7SPeter Brune +  global - global DM
2065baf369e7SPeter Brune -  ctx - optional user-defined function context
2066baf369e7SPeter Brune 
2067baf369e7SPeter Brune    Level: advanced
2068baf369e7SPeter Brune 
2069baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2070baf369e7SPeter Brune @*/
2071baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2072baf369e7SPeter Brune {
2073baf369e7SPeter Brune   PetscErrorCode          ierr;
2074baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2075baf369e7SPeter Brune 
2076baf369e7SPeter Brune   PetscFunctionBegin;
2077baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2078baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
207995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2080baf369e7SPeter Brune   link->beginhook = beginhook;
2081baf369e7SPeter Brune   link->endhook   = endhook;
2082baf369e7SPeter Brune   link->ctx       = ctx;
20830298fd71SBarry Smith   link->next      = NULL;
2084baf369e7SPeter Brune   *p              = link;
2085baf369e7SPeter Brune   PetscFunctionReturn(0);
2086baf369e7SPeter Brune }
2087baf369e7SPeter Brune 
20884c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
20894c274da1SToby Isaac {
20904c274da1SToby Isaac   Mat cMat;
20914c274da1SToby Isaac   Vec cVec;
20924c274da1SToby Isaac   PetscSection section, cSec;
20934c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20944c274da1SToby Isaac   PetscErrorCode ierr;
20954c274da1SToby Isaac 
20964c274da1SToby Isaac   PetscFunctionBegin;
20974c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20984c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20994c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
21005db9a05bSToby Isaac     PetscInt nRows;
21015db9a05bSToby Isaac 
21025db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21035db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2104e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
21057711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21064c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
21074c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21084c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21094c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21104c274da1SToby Isaac       if (dof) {
21114c274da1SToby Isaac         PetscScalar *vals;
21124c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
21134c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
21144c274da1SToby Isaac       }
21154c274da1SToby Isaac     }
21164c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21174c274da1SToby Isaac   }
21184c274da1SToby Isaac   PetscFunctionReturn(0);
21194c274da1SToby Isaac }
21204c274da1SToby Isaac 
212147c6ae99SBarry Smith /*@
2122*01729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
2123*01729b5cSPatrick Sanan 
2124*01729b5cSPatrick Sanan     Neighbor-wise Collective on DM
2125*01729b5cSPatrick Sanan 
2126*01729b5cSPatrick Sanan     Input Parameters:
2127*01729b5cSPatrick Sanan +   dm - the DM object
2128*01729b5cSPatrick Sanan .   g - the global vector
2129*01729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
2130*01729b5cSPatrick Sanan -   l - the local vector
2131*01729b5cSPatrick Sanan 
2132*01729b5cSPatrick Sanan     Notes:
2133*01729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2134*01729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
2135*01729b5cSPatrick Sanan 
2136*01729b5cSPatrick Sanan     Level: beginner
2137*01729b5cSPatrick Sanan 
2138*01729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
2139*01729b5cSPatrick Sanan 
2140*01729b5cSPatrick Sanan @*/
2141*01729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
2142*01729b5cSPatrick Sanan {
2143*01729b5cSPatrick Sanan   PetscErrorCode ierr;
2144*01729b5cSPatrick Sanan 
2145*01729b5cSPatrick Sanan   PetscFunctionBegin;
2146*01729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
2147*01729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
2148*01729b5cSPatrick Sanan   PetscFunctionReturn(0);
2149*01729b5cSPatrick Sanan }
2150*01729b5cSPatrick Sanan 
2151*01729b5cSPatrick Sanan /*@
215247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
215347c6ae99SBarry Smith 
215447c6ae99SBarry Smith     Neighbor-wise Collective on DM
215547c6ae99SBarry Smith 
215647c6ae99SBarry Smith     Input Parameters:
215747c6ae99SBarry Smith +   dm - the DM object
215847c6ae99SBarry Smith .   g - the global vector
215947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
216047c6ae99SBarry Smith -   l - the local vector
216147c6ae99SBarry Smith 
2162*01729b5cSPatrick Sanan     Level: intermediate
216347c6ae99SBarry Smith 
2164*01729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
216547c6ae99SBarry Smith 
216647c6ae99SBarry Smith @*/
21677087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
216847c6ae99SBarry Smith {
21697128ae9fSMatthew G Knepley   PetscSF                 sf;
217047c6ae99SBarry Smith   PetscErrorCode          ierr;
2171baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
217247c6ae99SBarry Smith 
217347c6ae99SBarry Smith   PetscFunctionBegin;
2174171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2175baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
21768865f1eaSKarl Rupp     if (link->beginhook) {
21778865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
21788865f1eaSKarl Rupp     }
2179baf369e7SPeter Brune   }
21807128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21817128ae9fSMatthew G Knepley   if (sf) {
2182ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2183ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
21847128ae9fSMatthew G Knepley 
218582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21867128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2187ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
21887128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
21897128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2190ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
21917128ae9fSMatthew G Knepley   } else {
2192843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
21937128ae9fSMatthew G Knepley   }
219447c6ae99SBarry Smith   PetscFunctionReturn(0);
219547c6ae99SBarry Smith }
219647c6ae99SBarry Smith 
219747c6ae99SBarry Smith /*@
219847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
219947c6ae99SBarry Smith 
220047c6ae99SBarry Smith     Neighbor-wise Collective on DM
220147c6ae99SBarry Smith 
220247c6ae99SBarry Smith     Input Parameters:
220347c6ae99SBarry Smith +   dm - the DM object
220447c6ae99SBarry Smith .   g - the global vector
220547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
220647c6ae99SBarry Smith -   l - the local vector
220747c6ae99SBarry Smith 
2208*01729b5cSPatrick Sanan     Level: intermediate
220947c6ae99SBarry Smith 
2210*01729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
221147c6ae99SBarry Smith 
221247c6ae99SBarry Smith @*/
22137087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
221447c6ae99SBarry Smith {
22157128ae9fSMatthew G Knepley   PetscSF                 sf;
221647c6ae99SBarry Smith   PetscErrorCode          ierr;
2217ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2218ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2219baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
222047c6ae99SBarry Smith 
222147c6ae99SBarry Smith   PetscFunctionBegin;
2222171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22237128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22247128ae9fSMatthew G Knepley   if (sf) {
222582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22267128ae9fSMatthew G Knepley 
22277128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2228ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22297128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22307128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2231ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22327128ae9fSMatthew G Knepley   } else {
2233843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22347128ae9fSMatthew G Knepley   }
22354c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2236baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2237baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2238baf369e7SPeter Brune   }
223947c6ae99SBarry Smith   PetscFunctionReturn(0);
224047c6ae99SBarry Smith }
224147c6ae99SBarry Smith 
2242d4d07f1eSToby Isaac /*@C
2243d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2244d4d07f1eSToby Isaac 
2245d4d07f1eSToby Isaac    Logically Collective
2246d4d07f1eSToby Isaac 
2247d4d07f1eSToby Isaac    Input Arguments:
2248d4d07f1eSToby Isaac +  dm - the DM
2249d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2250d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2251d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2252d4d07f1eSToby Isaac 
2253d4d07f1eSToby Isaac    Calling sequence for beginhook:
2254d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2255d4d07f1eSToby Isaac 
2256d4d07f1eSToby Isaac +  dm - global DM
2257d4d07f1eSToby Isaac .  l - local vector
2258d4d07f1eSToby Isaac .  mode - mode
2259d4d07f1eSToby Isaac .  g - global vector
2260d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2261d4d07f1eSToby Isaac 
2262d4d07f1eSToby Isaac 
2263d4d07f1eSToby Isaac    Calling sequence for endhook:
2264d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2265d4d07f1eSToby Isaac 
2266d4d07f1eSToby Isaac +  global - global DM
2267d4d07f1eSToby Isaac .  l - local vector
2268d4d07f1eSToby Isaac .  mode - mode
2269d4d07f1eSToby Isaac .  g - global vector
2270d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2271d4d07f1eSToby Isaac 
2272d4d07f1eSToby Isaac    Level: advanced
2273d4d07f1eSToby Isaac 
2274d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2275d4d07f1eSToby Isaac @*/
2276d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2277d4d07f1eSToby Isaac {
2278d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2279d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2280d4d07f1eSToby Isaac 
2281d4d07f1eSToby Isaac   PetscFunctionBegin;
2282d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2283d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
228495dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2285d4d07f1eSToby Isaac   link->beginhook = beginhook;
2286d4d07f1eSToby Isaac   link->endhook   = endhook;
2287d4d07f1eSToby Isaac   link->ctx       = ctx;
2288d4d07f1eSToby Isaac   link->next      = NULL;
2289d4d07f1eSToby Isaac   *p              = link;
2290d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2291d4d07f1eSToby Isaac }
2292d4d07f1eSToby Isaac 
22934c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
22944c274da1SToby Isaac {
22954c274da1SToby Isaac   Mat cMat;
22964c274da1SToby Isaac   Vec cVec;
22974c274da1SToby Isaac   PetscSection section, cSec;
22984c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22994c274da1SToby Isaac   PetscErrorCode ierr;
23004c274da1SToby Isaac 
23014c274da1SToby Isaac   PetscFunctionBegin;
23024c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23034c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23044c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
23055db9a05bSToby Isaac     PetscInt nRows;
23065db9a05bSToby Isaac 
23075db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23085db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2309e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
23107711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23114c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23124c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23134c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23144c274da1SToby Isaac       if (dof) {
23154c274da1SToby Isaac         PetscInt d;
23164c274da1SToby Isaac         PetscScalar *vals;
23174c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
23184c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
23194c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
23204c274da1SToby Isaac          * we just extracted */
23214c274da1SToby Isaac         for (d = 0; d < dof; d++) {
23224c274da1SToby Isaac           vals[d] = 0.;
23234c274da1SToby Isaac         }
23244c274da1SToby Isaac       }
23254c274da1SToby Isaac     }
23264c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
23274c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23284c274da1SToby Isaac   }
23294c274da1SToby Isaac   PetscFunctionReturn(0);
23304c274da1SToby Isaac }
2331*01729b5cSPatrick Sanan /*@
2332*01729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
2333*01729b5cSPatrick Sanan 
2334*01729b5cSPatrick Sanan     Neighbor-wise Collective on DM
2335*01729b5cSPatrick Sanan 
2336*01729b5cSPatrick Sanan     Input Parameters:
2337*01729b5cSPatrick Sanan +   dm - the DM object
2338*01729b5cSPatrick Sanan .   l - the local vector
2339*01729b5cSPatrick 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.
2340*01729b5cSPatrick Sanan -   g - the global vector
2341*01729b5cSPatrick Sanan 
2342*01729b5cSPatrick Sanan     Notes:
2343*01729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2344*01729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
2345*01729b5cSPatrick Sanan 
2346*01729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
2347*01729b5cSPatrick 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.
2348*01729b5cSPatrick Sanan 
2349*01729b5cSPatrick Sanan     Level: beginner
2350*01729b5cSPatrick Sanan 
2351*01729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
2352*01729b5cSPatrick Sanan 
2353*01729b5cSPatrick Sanan @*/
2354*01729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
2355*01729b5cSPatrick Sanan {
2356*01729b5cSPatrick Sanan   PetscErrorCode ierr;
2357*01729b5cSPatrick Sanan 
2358*01729b5cSPatrick Sanan   PetscFunctionBegin;
2359*01729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
2360*01729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
2361*01729b5cSPatrick Sanan   PetscFunctionReturn(0);
2362*01729b5cSPatrick Sanan }
23634c274da1SToby Isaac 
236447c6ae99SBarry Smith /*@
2365*01729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
23669a42bb27SBarry Smith 
23679a42bb27SBarry Smith     Neighbor-wise Collective on DM
23689a42bb27SBarry Smith 
23699a42bb27SBarry Smith     Input Parameters:
23709a42bb27SBarry Smith +   dm - the DM object
2371f6813fd5SJed Brown .   l - the local vector
23721eb28f2eSBarry 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.
23731eb28f2eSBarry Smith -   g - the global vector
23749a42bb27SBarry Smith 
237595452b02SPatrick Sanan     Notes:
237695452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
237784330215SMatthew 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.
23789a42bb27SBarry Smith 
2379*01729b5cSPatrick Sanan     Level: intermediate
23809a42bb27SBarry Smith 
2381*01729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
23829a42bb27SBarry Smith 
23839a42bb27SBarry Smith @*/
23847087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
23859a42bb27SBarry Smith {
23867128ae9fSMatthew G Knepley   PetscSF                 sf;
238784330215SMatthew G. Knepley   PetscSection            s, gs;
2388d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2389ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2390ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
239184330215SMatthew G. Knepley   PetscBool               isInsert;
239284330215SMatthew G. Knepley   PetscErrorCode          ierr;
23939a42bb27SBarry Smith 
23949a42bb27SBarry Smith   PetscFunctionBegin;
2395171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2396d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2397d4d07f1eSToby Isaac     if (link->beginhook) {
2398d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2399d4d07f1eSToby Isaac     }
2400d4d07f1eSToby Isaac   }
24014c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
24027128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2403e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24047128ae9fSMatthew G Knepley   switch (mode) {
24057128ae9fSMatthew G Knepley   case INSERT_VALUES:
24067128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2407304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
240884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24097128ae9fSMatthew G Knepley   case ADD_VALUES:
24107128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2411304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
241284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
24137128ae9fSMatthew G Knepley   default:
241482f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24157128ae9fSMatthew G Knepley   }
241684330215SMatthew G. Knepley   if (sf && !isInsert) {
2417ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24187128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2419a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2420ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
242184330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
242284330215SMatthew G. Knepley   } else if (s && isInsert) {
242384330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
242484330215SMatthew G. Knepley 
2425e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
242684330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
242784330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2428ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
242984330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
243084330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2431b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
243284330215SMatthew G. Knepley 
243384330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
243403442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
243584330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2436b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
243784330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
243884330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2439b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
244003442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2441b3b16f48SMatthew 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);
2442b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2443b3b16f48SMatthew G. Knepley       if (!gcdof) {
244484330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2445b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2446b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
244784330215SMatthew G. Knepley         const PetscInt *cdofs;
244884330215SMatthew G. Knepley         PetscInt        cind = 0;
244984330215SMatthew G. Knepley 
245084330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2451b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
245284330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2453b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
245484330215SMatthew G. Knepley         }
2455b3b16f48SMatthew 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);
245684330215SMatthew G. Knepley     }
2457ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24587128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
24597128ae9fSMatthew G Knepley   } else {
2460843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24617128ae9fSMatthew G Knepley   }
24629a42bb27SBarry Smith   PetscFunctionReturn(0);
24639a42bb27SBarry Smith }
24649a42bb27SBarry Smith 
24659a42bb27SBarry Smith /*@
24669a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
246747c6ae99SBarry Smith 
246847c6ae99SBarry Smith     Neighbor-wise Collective on DM
246947c6ae99SBarry Smith 
247047c6ae99SBarry Smith     Input Parameters:
247147c6ae99SBarry Smith +   dm - the DM object
2472f6813fd5SJed Brown .   l - the local vector
247347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2474f6813fd5SJed Brown -   g - the global vector
247547c6ae99SBarry Smith 
2476*01729b5cSPatrick Sanan     Level: intermediate
247747c6ae99SBarry Smith 
2478e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
247947c6ae99SBarry Smith 
248047c6ae99SBarry Smith @*/
24817087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
248247c6ae99SBarry Smith {
24837128ae9fSMatthew G Knepley   PetscSF                 sf;
248484330215SMatthew G. Knepley   PetscSection            s;
2485d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
248684330215SMatthew G. Knepley   PetscBool               isInsert;
248784330215SMatthew G. Knepley   PetscErrorCode          ierr;
248847c6ae99SBarry Smith 
248947c6ae99SBarry Smith   PetscFunctionBegin;
2490171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24917128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2492e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24937128ae9fSMatthew G Knepley   switch (mode) {
24947128ae9fSMatthew G Knepley   case INSERT_VALUES:
24957128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
249684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24977128ae9fSMatthew G Knepley   case ADD_VALUES:
24987128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
249984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25007128ae9fSMatthew G Knepley   default:
250182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25027128ae9fSMatthew G Knepley   }
250384330215SMatthew G. Knepley   if (sf && !isInsert) {
2504ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2505ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
250684330215SMatthew G. Knepley 
2507ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
25087128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2509a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2510ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
25117128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
251284330215SMatthew G. Knepley   } else if (s && isInsert) {
25137128ae9fSMatthew G Knepley   } else {
2514843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
25157128ae9fSMatthew G Knepley   }
2516d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2517d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2518d4d07f1eSToby Isaac   }
251947c6ae99SBarry Smith   PetscFunctionReturn(0);
252047c6ae99SBarry Smith }
252147c6ae99SBarry Smith 
2522f089877aSRichard Tran Mills /*@
2523bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2524bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2525d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2526f089877aSRichard Tran Mills 
2527bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2528f089877aSRichard Tran Mills 
2529f089877aSRichard Tran Mills    Input Parameters:
2530f089877aSRichard Tran Mills +  dm - the DM object
2531bc0a1609SRichard Tran Mills .  g - the original local vector
2532bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2533f089877aSRichard Tran Mills 
2534bc0a1609SRichard Tran Mills    Output Parameter:
2535bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2536f089877aSRichard Tran Mills 
2537f089877aSRichard Tran Mills    Level: intermediate
2538f089877aSRichard Tran Mills 
2539bc0a1609SRichard Tran Mills    Notes:
2540bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2541bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2542bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2543bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2544bc0a1609SRichard Tran Mills 
2545bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2546bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2547f089877aSRichard Tran Mills 
2548f089877aSRichard Tran Mills @*/
2549f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2550f089877aSRichard Tran Mills {
2551f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2552f089877aSRichard Tran Mills 
2553f089877aSRichard Tran Mills   PetscFunctionBegin;
2554f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2555bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2556f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2557f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2558f089877aSRichard Tran Mills }
2559f089877aSRichard Tran Mills 
2560f089877aSRichard Tran Mills /*@
2561bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2562bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2563d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2564f089877aSRichard Tran Mills 
2565bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2566f089877aSRichard Tran Mills 
2567f089877aSRichard Tran Mills    Input Parameters:
2568bc0a1609SRichard Tran Mills +  da - the DM object
2569bc0a1609SRichard Tran Mills .  g - the original local vector
2570bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2571f089877aSRichard Tran Mills 
2572bc0a1609SRichard Tran Mills    Output Parameter:
2573bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2574f089877aSRichard Tran Mills 
2575f089877aSRichard Tran Mills    Level: intermediate
2576f089877aSRichard Tran Mills 
2577bc0a1609SRichard Tran Mills    Notes:
2578bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2579bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2580bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2581bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2582bc0a1609SRichard Tran Mills 
2583bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2584bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2585f089877aSRichard Tran Mills 
2586f089877aSRichard Tran Mills @*/
2587f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2588f089877aSRichard Tran Mills {
2589f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2590f089877aSRichard Tran Mills 
2591f089877aSRichard Tran Mills   PetscFunctionBegin;
2592f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2593bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2594f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2595f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2596f089877aSRichard Tran Mills }
2597f089877aSRichard Tran Mills 
2598f089877aSRichard Tran Mills 
259947c6ae99SBarry Smith /*@
260047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
260147c6ae99SBarry Smith 
260247c6ae99SBarry Smith     Collective on DM
260347c6ae99SBarry Smith 
260447c6ae99SBarry Smith     Input Parameter:
260547c6ae99SBarry Smith +   dm - the DM object
260691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
260747c6ae99SBarry Smith 
260847c6ae99SBarry Smith     Output Parameter:
260947c6ae99SBarry Smith .   dmc - the coarsened DM
261047c6ae99SBarry Smith 
261147c6ae99SBarry Smith     Level: developer
261247c6ae99SBarry Smith 
2613e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
261447c6ae99SBarry Smith 
261547c6ae99SBarry Smith @*/
26167087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
261747c6ae99SBarry Smith {
261847c6ae99SBarry Smith   PetscErrorCode    ierr;
2619b17ce1afSJed Brown   DMCoarsenHookLink link;
262047c6ae99SBarry Smith 
262147c6ae99SBarry Smith   PetscFunctionBegin;
2622171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2623fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
262447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
262547c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
26268892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2627a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
262843842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
26298cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2630644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
26310598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2632656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2633e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2634b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2635b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2636b17ce1afSJed Brown   }
263747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2638b17ce1afSJed Brown   PetscFunctionReturn(0);
2639b17ce1afSJed Brown }
2640b17ce1afSJed Brown 
2641bb9467b5SJed Brown /*@C
2642b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2643b17ce1afSJed Brown 
2644b17ce1afSJed Brown    Logically Collective
2645b17ce1afSJed Brown 
2646b17ce1afSJed Brown    Input Arguments:
2647b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2648b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2649b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
26500298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2651b17ce1afSJed Brown 
2652b17ce1afSJed Brown    Calling sequence of coarsenhook:
2653b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2654b17ce1afSJed Brown 
2655b17ce1afSJed Brown +  fine - fine level DM
2656b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2657b17ce1afSJed Brown -  ctx - optional user-defined function context
2658b17ce1afSJed Brown 
2659b17ce1afSJed Brown    Calling sequence for restricthook:
2660c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2661b17ce1afSJed Brown 
2662b17ce1afSJed Brown +  fine - fine level DM
2663b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2664c833c3b5SJed Brown .  rscale - scaling vector for restriction
2665c833c3b5SJed Brown .  inject - matrix restricting by injection
2666b17ce1afSJed Brown .  coarse - coarse level DM to update
2667b17ce1afSJed Brown -  ctx - optional user-defined function context
2668b17ce1afSJed Brown 
2669b17ce1afSJed Brown    Level: advanced
2670b17ce1afSJed Brown 
2671b17ce1afSJed Brown    Notes:
2672b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2673b17ce1afSJed Brown 
2674b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2675b17ce1afSJed Brown 
2676b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2677b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2678b17ce1afSJed Brown 
2679bb9467b5SJed Brown    This function is currently not available from Fortran.
2680bb9467b5SJed Brown 
2681dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2682b17ce1afSJed Brown @*/
2683b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2684b17ce1afSJed Brown {
2685b17ce1afSJed Brown   PetscErrorCode    ierr;
2686b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2687b17ce1afSJed Brown 
2688b17ce1afSJed Brown   PetscFunctionBegin;
2689b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
26901e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
26911e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
26921e3d8eccSJed Brown   }
269395dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2694b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2695b17ce1afSJed Brown   link->restricthook = restricthook;
2696b17ce1afSJed Brown   link->ctx          = ctx;
26970298fd71SBarry Smith   link->next         = NULL;
2698b17ce1afSJed Brown   *p                 = link;
2699b17ce1afSJed Brown   PetscFunctionReturn(0);
2700b17ce1afSJed Brown }
2701b17ce1afSJed Brown 
2702dc822a44SJed Brown /*@C
2703dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2704dc822a44SJed Brown 
2705dc822a44SJed Brown    Logically Collective
2706dc822a44SJed Brown 
2707dc822a44SJed Brown    Input Arguments:
2708dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2709dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2710dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2711dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2712dc822a44SJed Brown 
2713dc822a44SJed Brown    Level: advanced
2714dc822a44SJed Brown 
2715dc822a44SJed Brown    Notes:
2716dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2717dc822a44SJed Brown 
2718dc822a44SJed Brown    This function is currently not available from Fortran.
2719dc822a44SJed Brown 
2720dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2721dc822a44SJed Brown @*/
2722dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2723dc822a44SJed Brown {
2724dc822a44SJed Brown   PetscErrorCode    ierr;
2725dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2726dc822a44SJed Brown 
2727dc822a44SJed Brown   PetscFunctionBegin;
2728dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2729dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2730dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2731dc822a44SJed Brown       link = *p;
2732dc822a44SJed Brown       *p = link->next;
2733dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2734dc822a44SJed Brown       break;
2735dc822a44SJed Brown     }
2736dc822a44SJed Brown   }
2737dc822a44SJed Brown   PetscFunctionReturn(0);
2738dc822a44SJed Brown }
2739dc822a44SJed Brown 
2740dc822a44SJed Brown 
2741b17ce1afSJed Brown /*@
2742b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2743b17ce1afSJed Brown 
2744b17ce1afSJed Brown    Collective if any hooks are
2745b17ce1afSJed Brown 
2746b17ce1afSJed Brown    Input Arguments:
2747b17ce1afSJed Brown +  fine - finer DM to use as a base
2748b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2749e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2750b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2751e91eccc2SStefano Zampini -  coarse - coarser DM to update
2752b17ce1afSJed Brown 
2753b17ce1afSJed Brown    Level: developer
2754b17ce1afSJed Brown 
2755b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2756b17ce1afSJed Brown @*/
2757b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2758b17ce1afSJed Brown {
2759b17ce1afSJed Brown   PetscErrorCode    ierr;
2760b17ce1afSJed Brown   DMCoarsenHookLink link;
2761b17ce1afSJed Brown 
2762b17ce1afSJed Brown   PetscFunctionBegin;
2763b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
27648865f1eaSKarl Rupp     if (link->restricthook) {
27658865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
27668865f1eaSKarl Rupp     }
2767b17ce1afSJed Brown   }
276847c6ae99SBarry Smith   PetscFunctionReturn(0);
276947c6ae99SBarry Smith }
277047c6ae99SBarry Smith 
2771bb9467b5SJed Brown /*@C
2772be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
27735dbd56e3SPeter Brune 
27745dbd56e3SPeter Brune    Logically Collective
27755dbd56e3SPeter Brune 
27765dbd56e3SPeter Brune    Input Arguments:
27775dbd56e3SPeter Brune +  global - global DM
2778ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
27795dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
27800298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
27815dbd56e3SPeter Brune 
2782ec4806b8SPeter Brune 
2783ec4806b8SPeter Brune    Calling sequence for ddhook:
2784ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2785ec4806b8SPeter Brune 
2786ec4806b8SPeter Brune +  global - global DM
2787ec4806b8SPeter Brune .  block  - block DM
2788ec4806b8SPeter Brune -  ctx - optional user-defined function context
2789ec4806b8SPeter Brune 
27905dbd56e3SPeter Brune    Calling sequence for restricthook:
2791ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
27925dbd56e3SPeter Brune 
27935dbd56e3SPeter Brune +  global - global DM
27945dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
27955dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2796ec4806b8SPeter Brune .  block  - block DM
27975dbd56e3SPeter Brune -  ctx - optional user-defined function context
27985dbd56e3SPeter Brune 
27995dbd56e3SPeter Brune    Level: advanced
28005dbd56e3SPeter Brune 
28015dbd56e3SPeter Brune    Notes:
2802ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
28035dbd56e3SPeter Brune 
28045dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
28055dbd56e3SPeter Brune 
28065dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2807ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
28085dbd56e3SPeter Brune 
2809bb9467b5SJed Brown    This function is currently not available from Fortran.
2810bb9467b5SJed Brown 
28115dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
28125dbd56e3SPeter Brune @*/
2813be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
28145dbd56e3SPeter Brune {
28155dbd56e3SPeter Brune   PetscErrorCode      ierr;
2816be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
28175dbd56e3SPeter Brune 
28185dbd56e3SPeter Brune   PetscFunctionBegin;
28195dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2820b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2821b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2822b3a6b972SJed Brown   }
282395dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
28245dbd56e3SPeter Brune   link->restricthook = restricthook;
2825be081cd6SPeter Brune   link->ddhook       = ddhook;
28265dbd56e3SPeter Brune   link->ctx          = ctx;
28270298fd71SBarry Smith   link->next         = NULL;
28285dbd56e3SPeter Brune   *p                 = link;
28295dbd56e3SPeter Brune   PetscFunctionReturn(0);
28305dbd56e3SPeter Brune }
28315dbd56e3SPeter Brune 
2832b3a6b972SJed Brown /*@C
2833b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2834b3a6b972SJed Brown 
2835b3a6b972SJed Brown    Logically Collective
2836b3a6b972SJed Brown 
2837b3a6b972SJed Brown    Input Arguments:
2838b3a6b972SJed Brown +  global - global DM
2839b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2840b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2841b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2842b3a6b972SJed Brown 
2843b3a6b972SJed Brown    Level: advanced
2844b3a6b972SJed Brown 
2845b3a6b972SJed Brown    Notes:
2846b3a6b972SJed Brown 
2847b3a6b972SJed Brown    This function is currently not available from Fortran.
2848b3a6b972SJed Brown 
2849b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2850b3a6b972SJed Brown @*/
2851b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2852b3a6b972SJed Brown {
2853b3a6b972SJed Brown   PetscErrorCode      ierr;
2854b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2855b3a6b972SJed Brown 
2856b3a6b972SJed Brown   PetscFunctionBegin;
2857b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2858b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2859b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2860b3a6b972SJed Brown       link = *p;
2861b3a6b972SJed Brown       *p = link->next;
2862b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2863b3a6b972SJed Brown       break;
2864b3a6b972SJed Brown     }
2865b3a6b972SJed Brown   }
2866b3a6b972SJed Brown   PetscFunctionReturn(0);
2867b3a6b972SJed Brown }
2868b3a6b972SJed Brown 
28695dbd56e3SPeter Brune /*@
2870be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
28715dbd56e3SPeter Brune 
28725dbd56e3SPeter Brune    Collective if any hooks are
28735dbd56e3SPeter Brune 
28745dbd56e3SPeter Brune    Input Arguments:
28755dbd56e3SPeter Brune +  fine - finer DM to use as a base
2876be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2877be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
28785dbd56e3SPeter Brune -  coarse - coarer DM to update
28795dbd56e3SPeter Brune 
28805dbd56e3SPeter Brune    Level: developer
28815dbd56e3SPeter Brune 
28825dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
28835dbd56e3SPeter Brune @*/
2884be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
28855dbd56e3SPeter Brune {
28865dbd56e3SPeter Brune   PetscErrorCode      ierr;
2887be081cd6SPeter Brune   DMSubDomainHookLink link;
28885dbd56e3SPeter Brune 
28895dbd56e3SPeter Brune   PetscFunctionBegin;
2890be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
28918865f1eaSKarl Rupp     if (link->restricthook) {
28928865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
28938865f1eaSKarl Rupp     }
28945dbd56e3SPeter Brune   }
28955dbd56e3SPeter Brune   PetscFunctionReturn(0);
28965dbd56e3SPeter Brune }
28975dbd56e3SPeter Brune 
28985fe1f584SPeter Brune /*@
28996a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
29005fe1f584SPeter Brune 
29015fe1f584SPeter Brune     Not Collective
29025fe1f584SPeter Brune 
29035fe1f584SPeter Brune     Input Parameter:
29045fe1f584SPeter Brune .   dm - the DM object
29055fe1f584SPeter Brune 
29065fe1f584SPeter Brune     Output Parameter:
29076a7d9d85SPeter Brune .   level - number of coarsenings
29085fe1f584SPeter Brune 
29095fe1f584SPeter Brune     Level: developer
29105fe1f584SPeter Brune 
29116a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
29125fe1f584SPeter Brune 
29135fe1f584SPeter Brune @*/
29145fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
29155fe1f584SPeter Brune {
29165fe1f584SPeter Brune   PetscFunctionBegin;
29175fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29185fe1f584SPeter Brune   *level = dm->leveldown;
29195fe1f584SPeter Brune   PetscFunctionReturn(0);
29205fe1f584SPeter Brune }
29215fe1f584SPeter Brune 
29225fe1f584SPeter Brune 
29235fe1f584SPeter Brune 
292447c6ae99SBarry Smith /*@C
292547c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
292647c6ae99SBarry Smith 
292747c6ae99SBarry Smith     Collective on DM
292847c6ae99SBarry Smith 
292947c6ae99SBarry Smith     Input Parameter:
293047c6ae99SBarry Smith +   dm - the DM object
293147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
293247c6ae99SBarry Smith 
293347c6ae99SBarry Smith     Output Parameter:
293447c6ae99SBarry Smith .   dmf - the refined DM hierarchy
293547c6ae99SBarry Smith 
293647c6ae99SBarry Smith     Level: developer
293747c6ae99SBarry Smith 
2938e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
293947c6ae99SBarry Smith 
294047c6ae99SBarry Smith @*/
29417087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
294247c6ae99SBarry Smith {
294347c6ae99SBarry Smith   PetscErrorCode ierr;
294447c6ae99SBarry Smith 
294547c6ae99SBarry Smith   PetscFunctionBegin;
2946171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2947ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
294847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
294947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
295047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
295147c6ae99SBarry Smith   } else if (dm->ops->refine) {
295247c6ae99SBarry Smith     PetscInt i;
295347c6ae99SBarry Smith 
2954ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
295547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2956ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
295747c6ae99SBarry Smith     }
2958ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
295947c6ae99SBarry Smith   PetscFunctionReturn(0);
296047c6ae99SBarry Smith }
296147c6ae99SBarry Smith 
296247c6ae99SBarry Smith /*@C
296347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
296447c6ae99SBarry Smith 
296547c6ae99SBarry Smith     Collective on DM
296647c6ae99SBarry Smith 
296747c6ae99SBarry Smith     Input Parameter:
296847c6ae99SBarry Smith +   dm - the DM object
296947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
297047c6ae99SBarry Smith 
297147c6ae99SBarry Smith     Output Parameter:
297247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
297347c6ae99SBarry Smith 
297447c6ae99SBarry Smith     Level: developer
297547c6ae99SBarry Smith 
2976e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
297747c6ae99SBarry Smith 
297847c6ae99SBarry Smith @*/
29797087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
298047c6ae99SBarry Smith {
298147c6ae99SBarry Smith   PetscErrorCode ierr;
298247c6ae99SBarry Smith 
298347c6ae99SBarry Smith   PetscFunctionBegin;
2984171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2985ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
298647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
298747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
298847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
298947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
299047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
299147c6ae99SBarry Smith     PetscInt i;
299247c6ae99SBarry Smith 
2993ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
299447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2995ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
299647c6ae99SBarry Smith     }
2997ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
299847c6ae99SBarry Smith   PetscFunctionReturn(0);
299947c6ae99SBarry Smith }
300047c6ae99SBarry Smith 
300147c6ae99SBarry Smith /*@
3002e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
300347c6ae99SBarry Smith    grids associated with two DMs.
300447c6ae99SBarry Smith 
300547c6ae99SBarry Smith    Collective on DM
300647c6ae99SBarry Smith 
300747c6ae99SBarry Smith    Input Parameters:
300847c6ae99SBarry Smith +  dmc - the coarse grid DM
300947c6ae99SBarry Smith -  dmf - the fine grid DM
301047c6ae99SBarry Smith 
301147c6ae99SBarry Smith    Output Parameters:
301247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
301347c6ae99SBarry Smith 
301447c6ae99SBarry Smith    Level: intermediate
301547c6ae99SBarry Smith 
301647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
301747c6ae99SBarry Smith 
3018e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
301947c6ae99SBarry Smith @*/
3020e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
302147c6ae99SBarry Smith {
302247c6ae99SBarry Smith   PetscErrorCode ierr;
302347c6ae99SBarry Smith 
302447c6ae99SBarry Smith   PetscFunctionBegin;
3025171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3026171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
302747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
302847c6ae99SBarry Smith   PetscFunctionReturn(0);
302947c6ae99SBarry Smith }
303047c6ae99SBarry Smith 
30311a266240SBarry Smith /*@C
30321a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
30331a266240SBarry Smith 
30341a266240SBarry Smith     Not Collective
30351a266240SBarry Smith 
30361a266240SBarry Smith     Input Parameters:
30371a266240SBarry Smith +   dm - the DM object
30381a266240SBarry Smith -   destroy - the destroy function
30391a266240SBarry Smith 
30401a266240SBarry Smith     Level: intermediate
30411a266240SBarry Smith 
3042e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
30431a266240SBarry Smith 
3044f07f9ceaSJed Brown @*/
30451a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
30461a266240SBarry Smith {
30471a266240SBarry Smith   PetscFunctionBegin;
3048171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30491a266240SBarry Smith   dm->ctxdestroy = destroy;
30501a266240SBarry Smith   PetscFunctionReturn(0);
30511a266240SBarry Smith }
30521a266240SBarry Smith 
3053b07ff414SBarry Smith /*@
30541b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
305547c6ae99SBarry Smith 
305647c6ae99SBarry Smith     Not Collective
305747c6ae99SBarry Smith 
305847c6ae99SBarry Smith     Input Parameters:
305947c6ae99SBarry Smith +   dm - the DM object
306047c6ae99SBarry Smith -   ctx - the user context
306147c6ae99SBarry Smith 
306247c6ae99SBarry Smith     Level: intermediate
306347c6ae99SBarry Smith 
3064e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
306547c6ae99SBarry Smith 
306647c6ae99SBarry Smith @*/
30671b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
306847c6ae99SBarry Smith {
306947c6ae99SBarry Smith   PetscFunctionBegin;
3070171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
307147c6ae99SBarry Smith   dm->ctx = ctx;
307247c6ae99SBarry Smith   PetscFunctionReturn(0);
307347c6ae99SBarry Smith }
307447c6ae99SBarry Smith 
307547c6ae99SBarry Smith /*@
30761b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
307747c6ae99SBarry Smith 
307847c6ae99SBarry Smith     Not Collective
307947c6ae99SBarry Smith 
308047c6ae99SBarry Smith     Input Parameter:
308147c6ae99SBarry Smith .   dm - the DM object
308247c6ae99SBarry Smith 
308347c6ae99SBarry Smith     Output Parameter:
308447c6ae99SBarry Smith .   ctx - the user context
308547c6ae99SBarry Smith 
308647c6ae99SBarry Smith     Level: intermediate
308747c6ae99SBarry Smith 
3088e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
308947c6ae99SBarry Smith 
309047c6ae99SBarry Smith @*/
30911b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
309247c6ae99SBarry Smith {
309347c6ae99SBarry Smith   PetscFunctionBegin;
3094171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30951b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
309647c6ae99SBarry Smith   PetscFunctionReturn(0);
309747c6ae99SBarry Smith }
309847c6ae99SBarry Smith 
309908da532bSDmitry Karpeev /*@C
3100df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
310108da532bSDmitry Karpeev 
310208da532bSDmitry Karpeev     Logically Collective on DM
310308da532bSDmitry Karpeev 
310408da532bSDmitry Karpeev     Input Parameter:
310508da532bSDmitry Karpeev +   dm - the DM object
31060298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
310708da532bSDmitry Karpeev 
310808da532bSDmitry Karpeev     Level: intermediate
310908da532bSDmitry Karpeev 
3110835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
311108da532bSDmitry Karpeev          DMSetJacobian()
311208da532bSDmitry Karpeev 
311308da532bSDmitry Karpeev @*/
311408da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
311508da532bSDmitry Karpeev {
311608da532bSDmitry Karpeev   PetscFunctionBegin;
311708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
311808da532bSDmitry Karpeev   PetscFunctionReturn(0);
311908da532bSDmitry Karpeev }
312008da532bSDmitry Karpeev 
312108da532bSDmitry Karpeev /*@
312208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
312308da532bSDmitry Karpeev 
312408da532bSDmitry Karpeev     Not Collective
312508da532bSDmitry Karpeev 
312608da532bSDmitry Karpeev     Input Parameter:
312708da532bSDmitry Karpeev .   dm - the DM object to destroy
312808da532bSDmitry Karpeev 
312908da532bSDmitry Karpeev     Output Parameter:
313008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
313108da532bSDmitry Karpeev 
313208da532bSDmitry Karpeev     Level: developer
313308da532bSDmitry Karpeev 
313474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
313508da532bSDmitry Karpeev 
313608da532bSDmitry Karpeev @*/
313708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
313808da532bSDmitry Karpeev {
313908da532bSDmitry Karpeev   PetscFunctionBegin;
314008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
314108da532bSDmitry Karpeev   PetscFunctionReturn(0);
314208da532bSDmitry Karpeev }
314308da532bSDmitry Karpeev 
314408da532bSDmitry Karpeev /*@C
314508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
314608da532bSDmitry Karpeev 
314708da532bSDmitry Karpeev     Logically Collective on DM
314808da532bSDmitry Karpeev 
314908da532bSDmitry Karpeev     Input Parameters:
3150907376e6SBarry Smith .   dm - the DM object
315108da532bSDmitry Karpeev 
315208da532bSDmitry Karpeev     Output parameters:
315308da532bSDmitry Karpeev +   xl - lower bound
315408da532bSDmitry Karpeev -   xu - upper bound
315508da532bSDmitry Karpeev 
3156907376e6SBarry Smith     Level: advanced
3157907376e6SBarry Smith 
315895452b02SPatrick Sanan     Notes:
315995452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
316008da532bSDmitry Karpeev 
316174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
316208da532bSDmitry Karpeev 
316308da532bSDmitry Karpeev @*/
316408da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
316508da532bSDmitry Karpeev {
316608da532bSDmitry Karpeev   PetscErrorCode ierr;
31675fd66863SKarl Rupp 
316808da532bSDmitry Karpeev   PetscFunctionBegin;
316908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
317008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
317108da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
317208da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
31738865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
317408da532bSDmitry Karpeev   PetscFunctionReturn(0);
317508da532bSDmitry Karpeev }
317608da532bSDmitry Karpeev 
3177b0ae01b7SPeter Brune /*@
3178b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3179b0ae01b7SPeter Brune 
3180b0ae01b7SPeter Brune     Not Collective
3181b0ae01b7SPeter Brune 
3182b0ae01b7SPeter Brune     Input Parameter:
3183b0ae01b7SPeter Brune .   dm - the DM object
3184b0ae01b7SPeter Brune 
3185b0ae01b7SPeter Brune     Output Parameter:
3186b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3187b0ae01b7SPeter Brune 
3188b0ae01b7SPeter Brune     Level: developer
3189b0ae01b7SPeter Brune 
3190b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3191b0ae01b7SPeter Brune 
3192b0ae01b7SPeter Brune @*/
3193b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3194b0ae01b7SPeter Brune {
3195b0ae01b7SPeter Brune   PetscFunctionBegin;
3196b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3197b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3198b0ae01b7SPeter Brune }
3199b0ae01b7SPeter Brune 
32003ad4599aSBarry Smith /*@
32013ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
32023ad4599aSBarry Smith 
32033ad4599aSBarry Smith     Not Collective
32043ad4599aSBarry Smith 
32053ad4599aSBarry Smith     Input Parameter:
32063ad4599aSBarry Smith .   dm - the DM object
32073ad4599aSBarry Smith 
32083ad4599aSBarry Smith     Output Parameter:
32093ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
32103ad4599aSBarry Smith 
32113ad4599aSBarry Smith     Level: developer
32123ad4599aSBarry Smith 
32133ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
32143ad4599aSBarry Smith 
32153ad4599aSBarry Smith @*/
32163ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
32173ad4599aSBarry Smith {
32183ad4599aSBarry Smith   PetscFunctionBegin;
32193ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
32203ad4599aSBarry Smith   PetscFunctionReturn(0);
32213ad4599aSBarry Smith }
32223ad4599aSBarry Smith 
3223a7058e45SLawrence Mitchell 
3224a7058e45SLawrence Mitchell /*@
3225a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3226a7058e45SLawrence Mitchell 
3227a7058e45SLawrence Mitchell     Not Collective
3228a7058e45SLawrence Mitchell 
3229a7058e45SLawrence Mitchell     Input Parameter:
3230a7058e45SLawrence Mitchell .   dm - the DM object
3231a7058e45SLawrence Mitchell 
3232a7058e45SLawrence Mitchell     Output Parameter:
3233a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3234a7058e45SLawrence Mitchell 
3235a7058e45SLawrence Mitchell     Level: developer
3236a7058e45SLawrence Mitchell 
3237a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3238a7058e45SLawrence Mitchell 
3239a7058e45SLawrence Mitchell @*/
3240a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3241a7058e45SLawrence Mitchell {
32424a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3243a7058e45SLawrence Mitchell   PetscFunctionBegin;
32444a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
32454a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3246a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3247a7058e45SLawrence Mitchell }
3248a7058e45SLawrence Mitchell 
3249748fac09SDmitry Karpeev /*@C
325008da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
325108da532bSDmitry Karpeev 
325208da532bSDmitry Karpeev     Collective on DM
325308da532bSDmitry Karpeev 
325408da532bSDmitry Karpeev     Input Parameter:
325508da532bSDmitry Karpeev +   dm - the DM object
32560298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
325708da532bSDmitry Karpeev 
325808da532bSDmitry Karpeev     Level: developer
325908da532bSDmitry Karpeev 
326074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
326108da532bSDmitry Karpeev 
326208da532bSDmitry Karpeev @*/
326308da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
326408da532bSDmitry Karpeev {
326508da532bSDmitry Karpeev   PetscErrorCode ierr;
32665fd66863SKarl Rupp 
326708da532bSDmitry Karpeev   PetscFunctionBegin;
326808da532bSDmitry Karpeev   if (x) {
326908da532bSDmitry Karpeev     if (!dm->x) {
327008da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
327108da532bSDmitry Karpeev     }
327208da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
32738865f1eaSKarl Rupp   } else if (dm->x) {
327408da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
327508da532bSDmitry Karpeev   }
327608da532bSDmitry Karpeev   PetscFunctionReturn(0);
327708da532bSDmitry Karpeev }
327808da532bSDmitry Karpeev 
32790298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3280264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3281264ace61SBarry Smith 
3282264ace61SBarry Smith /*@C
3283264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3284264ace61SBarry Smith 
3285264ace61SBarry Smith   Collective on DM
3286264ace61SBarry Smith 
3287264ace61SBarry Smith   Input Parameters:
3288264ace61SBarry Smith + dm     - The DM object
3289264ace61SBarry Smith - method - The name of the DM type
3290264ace61SBarry Smith 
3291264ace61SBarry Smith   Options Database Key:
3292264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3293264ace61SBarry Smith 
3294264ace61SBarry Smith   Notes:
3295e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3296264ace61SBarry Smith 
3297264ace61SBarry Smith   Level: intermediate
3298264ace61SBarry Smith 
3299264ace61SBarry Smith .keywords: DM, set, type
3300264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3301264ace61SBarry Smith @*/
330219fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3303264ace61SBarry Smith {
3304264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3305264ace61SBarry Smith   PetscBool      match;
3306264ace61SBarry Smith   PetscErrorCode ierr;
3307264ace61SBarry Smith 
3308264ace61SBarry Smith   PetscFunctionBegin;
3309264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3310251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3311264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3312264ace61SBarry Smith 
33130f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
33141c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3315ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3316264ace61SBarry Smith 
3317264ace61SBarry Smith   if (dm->ops->destroy) {
3318264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
33190298fd71SBarry Smith     dm->ops->destroy = NULL;
3320264ace61SBarry Smith   }
3321264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3322264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3323264ace61SBarry Smith   PetscFunctionReturn(0);
3324264ace61SBarry Smith }
3325264ace61SBarry Smith 
3326264ace61SBarry Smith /*@C
3327264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3328264ace61SBarry Smith 
3329264ace61SBarry Smith   Not Collective
3330264ace61SBarry Smith 
3331264ace61SBarry Smith   Input Parameter:
3332264ace61SBarry Smith . dm  - The DM
3333264ace61SBarry Smith 
3334264ace61SBarry Smith   Output Parameter:
3335264ace61SBarry Smith . type - The DM type name
3336264ace61SBarry Smith 
3337264ace61SBarry Smith   Level: intermediate
3338264ace61SBarry Smith 
3339264ace61SBarry Smith .keywords: DM, get, type, name
3340264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3341264ace61SBarry Smith @*/
334219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3343264ace61SBarry Smith {
3344264ace61SBarry Smith   PetscErrorCode ierr;
3345264ace61SBarry Smith 
3346264ace61SBarry Smith   PetscFunctionBegin;
3347264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3348c959eef4SJed Brown   PetscValidPointer(type,2);
3349607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3350264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3351264ace61SBarry Smith   PetscFunctionReturn(0);
3352264ace61SBarry Smith }
3353264ace61SBarry Smith 
335467a56275SMatthew G Knepley /*@C
335567a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
335667a56275SMatthew G Knepley 
335767a56275SMatthew G Knepley   Collective on DM
335867a56275SMatthew G Knepley 
335967a56275SMatthew G Knepley   Input Parameters:
336067a56275SMatthew G Knepley + dm - the DM
336167a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
336267a56275SMatthew G Knepley 
336367a56275SMatthew G Knepley   Output Parameter:
336467a56275SMatthew G Knepley . M - pointer to new DM
336567a56275SMatthew G Knepley 
336667a56275SMatthew G Knepley   Notes:
336767a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
336867a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
336967a56275SMatthew G Knepley   of the input DM.
337067a56275SMatthew G Knepley 
337167a56275SMatthew G Knepley   Level: intermediate
337267a56275SMatthew G Knepley 
337367a56275SMatthew G Knepley .seealso: DMCreate()
337467a56275SMatthew G Knepley @*/
337519fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
337667a56275SMatthew G Knepley {
337767a56275SMatthew G Knepley   DM             B;
337867a56275SMatthew G Knepley   char           convname[256];
3379c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
338067a56275SMatthew G Knepley   PetscErrorCode ierr;
338167a56275SMatthew G Knepley 
338267a56275SMatthew G Knepley   PetscFunctionBegin;
338367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
338467a56275SMatthew G Knepley   PetscValidType(dm,1);
338567a56275SMatthew G Knepley   PetscValidPointer(M,3);
3386251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3387c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3388c067b6caSMatthew G. Knepley   if (sametype) {
3389c067b6caSMatthew G. Knepley     *M   = dm;
3390c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3391c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3392c067b6caSMatthew G. Knepley   } else {
33930298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
339467a56275SMatthew G Knepley 
339567a56275SMatthew G Knepley     /*
339667a56275SMatthew G Knepley        Order of precedence:
339767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
339867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
339967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
340067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
340167a56275SMatthew G Knepley        5) Use a really basic converter.
340267a56275SMatthew G Knepley     */
340367a56275SMatthew G Knepley 
340467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3405a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3406a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3407a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3408a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3409a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34100005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
341167a56275SMatthew G Knepley     if (conv) goto foundconv;
341267a56275SMatthew G Knepley 
341367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
341482f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
341567a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3416a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3417a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3418a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3419a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3420a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34210005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
342267a56275SMatthew G Knepley     if (conv) {
3423fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
342467a56275SMatthew G Knepley       goto foundconv;
342567a56275SMatthew G Knepley     }
342667a56275SMatthew G Knepley 
342767a56275SMatthew G Knepley #if 0
342867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
342967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3430fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
343167a56275SMatthew G Knepley     if (conv) goto foundconv;
343267a56275SMatthew G Knepley 
343367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
343467a56275SMatthew G Knepley     if (dm->ops->convert) {
343567a56275SMatthew G Knepley       conv = dm->ops->convert;
343667a56275SMatthew G Knepley     }
343767a56275SMatthew G Knepley     if (conv) goto foundconv;
343867a56275SMatthew G Knepley #endif
343967a56275SMatthew G Knepley 
344067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
344182f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
344267a56275SMatthew G Knepley 
344367a56275SMatthew G Knepley foundconv:
344467a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
344567a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
344612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
344790b157c4SStefano Zampini     {
344890b157c4SStefano Zampini       PetscBool             isper;
344912fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
345012fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
345190b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
345290b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
345312fa691eSMatthew G. Knepley     }
345467a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
345567a56275SMatthew G Knepley   }
345667a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
345767a56275SMatthew G Knepley   PetscFunctionReturn(0);
345867a56275SMatthew G Knepley }
3459264ace61SBarry Smith 
3460264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3461264ace61SBarry Smith 
3462264ace61SBarry Smith /*@C
34631c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
34641c84c290SBarry Smith 
34651c84c290SBarry Smith   Not Collective
34661c84c290SBarry Smith 
34671c84c290SBarry Smith   Input Parameters:
34681c84c290SBarry Smith + name        - The name of a new user-defined creation routine
34691c84c290SBarry Smith - create_func - The creation routine itself
34701c84c290SBarry Smith 
34711c84c290SBarry Smith   Notes:
34721c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
34731c84c290SBarry Smith 
34741c84c290SBarry Smith 
34751c84c290SBarry Smith   Sample usage:
34761c84c290SBarry Smith .vb
3477bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
34781c84c290SBarry Smith .ve
34791c84c290SBarry Smith 
34801c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
34811c84c290SBarry Smith .vb
34821c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
34831c84c290SBarry Smith     DMSetType(DM,"my_da");
34841c84c290SBarry Smith .ve
34851c84c290SBarry Smith    or at runtime via the option
34861c84c290SBarry Smith .vb
34871c84c290SBarry Smith     -da_type my_da
34881c84c290SBarry Smith .ve
3489264ace61SBarry Smith 
3490264ace61SBarry Smith   Level: advanced
34911c84c290SBarry Smith 
34921c84c290SBarry Smith .keywords: DM, register
3493bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
34941c84c290SBarry Smith 
3495264ace61SBarry Smith @*/
3496bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3497264ace61SBarry Smith {
3498264ace61SBarry Smith   PetscErrorCode ierr;
3499264ace61SBarry Smith 
3500264ace61SBarry Smith   PetscFunctionBegin;
35011d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3502a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3503264ace61SBarry Smith   PetscFunctionReturn(0);
3504264ace61SBarry Smith }
3505264ace61SBarry Smith 
3506b859378eSBarry Smith /*@C
350755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3508b859378eSBarry Smith 
3509b859378eSBarry Smith   Collective on PetscViewer
3510b859378eSBarry Smith 
3511b859378eSBarry Smith   Input Parameters:
3512b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3513b859378eSBarry Smith            some related function before a call to DMLoad().
3514b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3515b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3516b859378eSBarry Smith 
3517b859378eSBarry Smith    Level: intermediate
3518b859378eSBarry Smith 
3519b859378eSBarry Smith   Notes:
352055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3521b859378eSBarry Smith 
3522b859378eSBarry Smith   Notes for advanced users:
3523b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3524b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3525b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3526b859378eSBarry Smith   format is
3527b859378eSBarry Smith .vb
3528b859378eSBarry Smith      has not yet been determined
3529b859378eSBarry Smith .ve
3530b859378eSBarry Smith 
3531b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3532b859378eSBarry Smith @*/
3533b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3534b859378eSBarry Smith {
35359331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3536b859378eSBarry Smith   PetscErrorCode ierr;
3537b859378eSBarry Smith 
3538b859378eSBarry Smith   PetscFunctionBegin;
3539b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3540b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
354132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
35429331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
35439331c7a4SMatthew G. Knepley   if (isbinary) {
35449331c7a4SMatthew G. Knepley     PetscInt classid;
35459331c7a4SMatthew G. Knepley     char     type[256];
3546b859378eSBarry Smith 
3547060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
35489200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3549060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
355032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
35519331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35529331c7a4SMatthew G. Knepley   } else if (ishdf5) {
35539331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35549331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3555b859378eSBarry Smith   PetscFunctionReturn(0);
3556b859378eSBarry Smith }
3557b859378eSBarry Smith 
35587da65231SMatthew G Knepley /******************************** FEM Support **********************************/
35597da65231SMatthew G Knepley 
3560a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3561a6dfd86eSKarl Rupp {
35621d47ebbbSSatish Balay   PetscInt       f;
35631b30c384SMatthew G Knepley   PetscErrorCode ierr;
35641b30c384SMatthew G Knepley 
35657da65231SMatthew G Knepley   PetscFunctionBegin;
356674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35671d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
356857622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
35697da65231SMatthew G Knepley   }
35707da65231SMatthew G Knepley   PetscFunctionReturn(0);
35717da65231SMatthew G Knepley }
35727da65231SMatthew G Knepley 
3573a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3574a6dfd86eSKarl Rupp {
35751b30c384SMatthew G Knepley   PetscInt       f, g;
35767da65231SMatthew G Knepley   PetscErrorCode ierr;
35777da65231SMatthew G Knepley 
35787da65231SMatthew G Knepley   PetscFunctionBegin;
357974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35801d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
358174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
35821d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3583e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
35847da65231SMatthew G Knepley     }
358574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
35867da65231SMatthew G Knepley   }
35877da65231SMatthew G Knepley   PetscFunctionReturn(0);
35887da65231SMatthew G Knepley }
3589e7c4fc90SDmitry Karpeev 
35906113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3591e759306cSMatthew G. Knepley {
35920c5b8624SToby Isaac   PetscInt          localSize, bs;
35930c5b8624SToby Isaac   PetscMPIInt       size;
35940c5b8624SToby Isaac   Vec               x, xglob;
35950c5b8624SToby Isaac   const PetscScalar *xarray;
3596e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3597e759306cSMatthew G. Knepley 
3598e759306cSMatthew G. Knepley   PetscFunctionBegin;
35999852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3600e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3601e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
36026113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
36030c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
36040c5b8624SToby Isaac   if (size > 1) {
36050c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
36060c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
36070c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
36080c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
36090c5b8624SToby Isaac   } else {
36100c5b8624SToby Isaac     xglob = x;
36110c5b8624SToby Isaac   }
36120c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
36130c5b8624SToby Isaac   if (size > 1) {
36140c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
36150c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
36160c5b8624SToby Isaac   }
3617e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3618e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3619e759306cSMatthew G. Knepley }
3620e759306cSMatthew G. Knepley 
362188ed4aceSMatthew G Knepley /*@
3622e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
362388ed4aceSMatthew G Knepley 
362488ed4aceSMatthew G Knepley   Input Parameter:
362588ed4aceSMatthew G Knepley . dm - The DM
362688ed4aceSMatthew G Knepley 
362788ed4aceSMatthew G Knepley   Output Parameter:
362888ed4aceSMatthew G Knepley . section - The PetscSection
362988ed4aceSMatthew G Knepley 
3630e5893cccSMatthew G. Knepley   Options Database Keys:
3631e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3632e5893cccSMatthew G. Knepley 
363388ed4aceSMatthew G Knepley   Level: intermediate
363488ed4aceSMatthew G Knepley 
363588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
363688ed4aceSMatthew G Knepley 
3637e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
363888ed4aceSMatthew G Knepley @*/
3639e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
36400adebc6cSBarry Smith {
3641fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3642fd59a867SMatthew G. Knepley 
364388ed4aceSMatthew G Knepley   PetscFunctionBegin;
364488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
364588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
36462f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
36472f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3648ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
36492f0f8703SMatthew G. Knepley   }
365088ed4aceSMatthew G Knepley   *section = dm->defaultSection;
365188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
365288ed4aceSMatthew G Knepley }
365388ed4aceSMatthew G Knepley 
365488ed4aceSMatthew G Knepley /*@
3655e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
365688ed4aceSMatthew G Knepley 
365788ed4aceSMatthew G Knepley   Input Parameters:
365888ed4aceSMatthew G Knepley + dm - The DM
365988ed4aceSMatthew G Knepley - section - The PetscSection
366088ed4aceSMatthew G Knepley 
366188ed4aceSMatthew G Knepley   Level: intermediate
366288ed4aceSMatthew G Knepley 
366388ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
366488ed4aceSMatthew G Knepley 
3665e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
366688ed4aceSMatthew G Knepley @*/
3667e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
36680adebc6cSBarry Smith {
3669c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3670af122d2aSMatthew G Knepley   PetscInt       f;
367188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
367288ed4aceSMatthew G Knepley 
367388ed4aceSMatthew G Knepley   PetscFunctionBegin;
367488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3675c473ab19SMatthew G. Knepley   if (section) {
36761d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36771d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3678c473ab19SMatthew G. Knepley   }
367988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
368088ed4aceSMatthew G Knepley   dm->defaultSection = section;
3681c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3682af122d2aSMatthew G Knepley   if (numFields) {
3683af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3684af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
36850f21e855SMatthew G. Knepley       PetscObject disc;
3686af122d2aSMatthew G Knepley       const char *name;
3687af122d2aSMatthew G Knepley 
3688af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
36890f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
36900f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3691af122d2aSMatthew G Knepley     }
3692af122d2aSMatthew G Knepley   }
3693e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
36941d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
369588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
369688ed4aceSMatthew G Knepley }
369788ed4aceSMatthew G Knepley 
36989435951eSToby Isaac /*@
36999435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
37009435951eSToby Isaac 
3701e228b242SToby Isaac   not collective
3702e228b242SToby Isaac 
37039435951eSToby Isaac   Input Parameter:
37049435951eSToby Isaac . dm - The DM
37059435951eSToby Isaac 
37069435951eSToby Isaac   Output Parameter:
37079435951eSToby 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.
37089435951eSToby 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.
37099435951eSToby Isaac 
37109435951eSToby Isaac   Level: advanced
37119435951eSToby Isaac 
37129435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
37139435951eSToby Isaac 
37149435951eSToby Isaac .seealso: DMSetDefaultConstraints()
37159435951eSToby Isaac @*/
37169435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
37179435951eSToby Isaac {
37189435951eSToby Isaac   PetscErrorCode ierr;
37199435951eSToby Isaac 
37209435951eSToby Isaac   PetscFunctionBegin;
37219435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37229435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
372345a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
372445a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
37259435951eSToby Isaac   PetscFunctionReturn(0);
37269435951eSToby Isaac }
37279435951eSToby Isaac 
37289435951eSToby Isaac /*@
37299435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
37309435951eSToby Isaac 
37319435951eSToby 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().
37329435951eSToby Isaac 
37339435951eSToby 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.
37349435951eSToby Isaac 
3735e228b242SToby Isaac   collective on dm
3736e228b242SToby Isaac 
37379435951eSToby Isaac   Input Parameters:
37389435951eSToby Isaac + dm - The DM
3739e228b242SToby 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).
3740e228b242SToby 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).
37419435951eSToby Isaac 
37429435951eSToby Isaac   Level: advanced
37439435951eSToby Isaac 
37449435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
37459435951eSToby Isaac 
37469435951eSToby Isaac .seealso: DMGetDefaultConstraints()
37479435951eSToby Isaac @*/
37489435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
37499435951eSToby Isaac {
3750e228b242SToby Isaac   PetscMPIInt result;
37519435951eSToby Isaac   PetscErrorCode ierr;
37529435951eSToby Isaac 
37539435951eSToby Isaac   PetscFunctionBegin;
37549435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3755e228b242SToby Isaac   if (section) {
3756e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3757e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3758f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3759e228b242SToby Isaac   }
3760e228b242SToby Isaac   if (mat) {
3761e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3762e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3763f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3764e228b242SToby Isaac   }
37659435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
37669435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
37679435951eSToby Isaac   dm->defaultConstraintSection = section;
37689435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
37699435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
37709435951eSToby Isaac   dm->defaultConstraintMat = mat;
37719435951eSToby Isaac   PetscFunctionReturn(0);
37729435951eSToby Isaac }
37739435951eSToby Isaac 
3774497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3775507e4973SMatthew G. Knepley /*
3776507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3777507e4973SMatthew G. Knepley 
3778507e4973SMatthew G. Knepley   Input Parameters:
3779507e4973SMatthew G. Knepley + dm - The DM
3780507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3781507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3782507e4973SMatthew G. Knepley 
3783507e4973SMatthew G. Knepley   Level: intermediate
3784507e4973SMatthew G. Knepley 
3785507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3786507e4973SMatthew G. Knepley */
3787f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3788507e4973SMatthew G. Knepley {
3789507e4973SMatthew G. Knepley   MPI_Comm        comm;
3790507e4973SMatthew G. Knepley   PetscLayout     layout;
3791507e4973SMatthew G. Knepley   const PetscInt *ranges;
3792507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3793507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3794507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3795507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3796507e4973SMatthew G. Knepley 
3797507e4973SMatthew G. Knepley   PetscFunctionBegin;
3798507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3799507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3800507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3801507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3802507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3803507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3804507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3805507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3806507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3807507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3808507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3809507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3810f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3811507e4973SMatthew G. Knepley 
3812507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3813507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3814507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3815507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3816507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3817507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3818507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3819507e4973SMatthew 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;}
3820507e4973SMatthew 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;}
3821507e4973SMatthew G. Knepley     if (gdof < 0) {
3822507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3823507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3824507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3825507e4973SMatthew G. Knepley 
3826507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3827507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3828507e4973SMatthew 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;}
3829507e4973SMatthew G. Knepley       }
3830507e4973SMatthew G. Knepley     }
3831507e4973SMatthew G. Knepley   }
3832507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3833507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3834b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3835507e4973SMatthew G. Knepley   if (!gvalid) {
3836507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3837507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3838507e4973SMatthew G. Knepley   }
3839507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3840507e4973SMatthew G. Knepley }
3841f741bcd2SMatthew G. Knepley #endif
3842507e4973SMatthew G. Knepley 
384388ed4aceSMatthew G Knepley /*@
3844e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
384588ed4aceSMatthew G Knepley 
38468b1ab98fSJed Brown   Collective on DM
38478b1ab98fSJed Brown 
384888ed4aceSMatthew G Knepley   Input Parameter:
384988ed4aceSMatthew G Knepley . dm - The DM
385088ed4aceSMatthew G Knepley 
385188ed4aceSMatthew G Knepley   Output Parameter:
385288ed4aceSMatthew G Knepley . section - The PetscSection
385388ed4aceSMatthew G Knepley 
385488ed4aceSMatthew G Knepley   Level: intermediate
385588ed4aceSMatthew G Knepley 
385688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
385788ed4aceSMatthew G Knepley 
3858e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
385988ed4aceSMatthew G Knepley @*/
3860e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
38610adebc6cSBarry Smith {
386288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
386388ed4aceSMatthew G Knepley 
386488ed4aceSMatthew G Knepley   PetscFunctionBegin;
386588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
386688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
386788ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3868fd59a867SMatthew G. Knepley     PetscSection s;
3869fd59a867SMatthew G. Knepley 
3870e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3871fd59a867SMatthew 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");
3872fd59a867SMatthew 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");
387315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3874cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3875ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3876685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
387788ed4aceSMatthew G Knepley   }
387888ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
387988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
388088ed4aceSMatthew G Knepley }
388188ed4aceSMatthew G Knepley 
3882b21d0597SMatthew G Knepley /*@
3883e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3884b21d0597SMatthew G Knepley 
3885b21d0597SMatthew G Knepley   Input Parameters:
3886b21d0597SMatthew G Knepley + dm - The DM
38875080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3888b21d0597SMatthew G Knepley 
3889b21d0597SMatthew G Knepley   Level: intermediate
3890b21d0597SMatthew G Knepley 
3891b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3892b21d0597SMatthew G Knepley 
3893e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3894b21d0597SMatthew G Knepley @*/
3895e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
38960adebc6cSBarry Smith {
3897b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3898b21d0597SMatthew G Knepley 
3899b21d0597SMatthew G Knepley   PetscFunctionBegin;
3900b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39015080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
39021d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3903b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3904b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3905497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3906f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3907507e4973SMatthew G. Knepley #endif
3908b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3909b21d0597SMatthew G Knepley }
3910b21d0597SMatthew G Knepley 
391188ed4aceSMatthew G Knepley /*@
391288ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
391388ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
391488ed4aceSMatthew G Knepley 
391588ed4aceSMatthew G Knepley   Input Parameter:
391688ed4aceSMatthew G Knepley . dm - The DM
391788ed4aceSMatthew G Knepley 
391888ed4aceSMatthew G Knepley   Output Parameter:
391988ed4aceSMatthew G Knepley . sf - The PetscSF
392088ed4aceSMatthew G Knepley 
392188ed4aceSMatthew G Knepley   Level: intermediate
392288ed4aceSMatthew G Knepley 
392388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
392488ed4aceSMatthew G Knepley 
392588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
392688ed4aceSMatthew G Knepley @*/
39270adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
39280adebc6cSBarry Smith {
392988ed4aceSMatthew G Knepley   PetscInt       nroots;
393088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
393188ed4aceSMatthew G Knepley 
393288ed4aceSMatthew G Knepley   PetscFunctionBegin;
393388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
393488ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
39350298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
393688ed4aceSMatthew G Knepley   if (nroots < 0) {
393788ed4aceSMatthew G Knepley     PetscSection section, gSection;
393888ed4aceSMatthew G Knepley 
3939e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
394031ea6d37SMatthew G Knepley     if (section) {
3941e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
394288ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
394331ea6d37SMatthew G Knepley     } else {
39440298fd71SBarry Smith       *sf = NULL;
394531ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
394631ea6d37SMatthew G Knepley     }
394788ed4aceSMatthew G Knepley   }
394888ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
394988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
395088ed4aceSMatthew G Knepley }
395188ed4aceSMatthew G Knepley 
395288ed4aceSMatthew G Knepley /*@
395388ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
395488ed4aceSMatthew G Knepley 
395588ed4aceSMatthew G Knepley   Input Parameters:
395688ed4aceSMatthew G Knepley + dm - The DM
395788ed4aceSMatthew G Knepley - sf - The PetscSF
395888ed4aceSMatthew G Knepley 
395988ed4aceSMatthew G Knepley   Level: intermediate
396088ed4aceSMatthew G Knepley 
396188ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
396288ed4aceSMatthew G Knepley 
396388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
396488ed4aceSMatthew G Knepley @*/
39650adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
39660adebc6cSBarry Smith {
396788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
396888ed4aceSMatthew G Knepley 
396988ed4aceSMatthew G Knepley   PetscFunctionBegin;
397088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
397188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
397288ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
397388ed4aceSMatthew G Knepley   dm->defaultSF = sf;
397488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
397588ed4aceSMatthew G Knepley }
397688ed4aceSMatthew G Knepley 
397788ed4aceSMatthew G Knepley /*@C
397888ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
397988ed4aceSMatthew G Knepley   describing the data layout.
398088ed4aceSMatthew G Knepley 
398188ed4aceSMatthew G Knepley   Input Parameters:
398288ed4aceSMatthew G Knepley + dm - The DM
398388ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
398488ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
398588ed4aceSMatthew G Knepley 
398688ed4aceSMatthew G Knepley   Level: intermediate
398788ed4aceSMatthew G Knepley 
398888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
398988ed4aceSMatthew G Knepley @*/
399088ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
399188ed4aceSMatthew G Knepley {
399282f516ccSBarry Smith   MPI_Comm       comm;
399388ed4aceSMatthew G Knepley   PetscLayout    layout;
399488ed4aceSMatthew G Knepley   const PetscInt *ranges;
399588ed4aceSMatthew G Knepley   PetscInt       *local;
399688ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3997ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
399888ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
399988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
400088ed4aceSMatthew G Knepley 
400188ed4aceSMatthew G Knepley   PetscFunctionBegin;
400282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
400388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400488ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
400588ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
400688ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
400788ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
400888ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
400988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
401088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
401188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
401288ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4013ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
40146636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
401588ed4aceSMatthew G Knepley 
40166636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40176636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4018235fbf56SMatthew 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));
40196636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
402088ed4aceSMatthew G Knepley   }
4021785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4022785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
402388ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
40241f588964SMatthew G Knepley     const PetscInt *cind;
40256636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
402688ed4aceSMatthew G Knepley 
402788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
402888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
402988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
403088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
403188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40326636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
403388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
40346636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
40356636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
40366636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4037057b4bcdSMatthew 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);
40386636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
40396636e97aSMatthew G Knepley     }
404088ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
404188ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
404288ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
404388ed4aceSMatthew G Knepley     }
404488ed4aceSMatthew G Knepley     if (gdof < 0) {
40456636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
404688ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
404788ed4aceSMatthew G Knepley 
404805376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
404931d3f06eSJed Brown         if (r < 0) r = -(r+2);
405005376888SMatthew 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);
405188ed4aceSMatthew G Knepley         remote[l].rank  = r;
405288ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
405388ed4aceSMatthew G Knepley       }
405488ed4aceSMatthew G Knepley     } else {
40556636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
405688ed4aceSMatthew G Knepley         remote[l].rank  = rank;
405788ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
405888ed4aceSMatthew G Knepley       }
405988ed4aceSMatthew G Knepley     }
406088ed4aceSMatthew G Knepley   }
40616636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
406288ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
406388ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
406488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
406588ed4aceSMatthew G Knepley }
4066af122d2aSMatthew G Knepley 
4067b21d0597SMatthew G Knepley /*@
4068b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4069b21d0597SMatthew G Knepley 
4070b21d0597SMatthew G Knepley   Input Parameter:
4071b21d0597SMatthew G Knepley . dm - The DM
4072b21d0597SMatthew G Knepley 
4073b21d0597SMatthew G Knepley   Output Parameter:
4074b21d0597SMatthew G Knepley . sf - The PetscSF
4075b21d0597SMatthew G Knepley 
4076b21d0597SMatthew G Knepley   Level: intermediate
4077b21d0597SMatthew G Knepley 
4078b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4079b21d0597SMatthew G Knepley 
4080057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4081b21d0597SMatthew G Knepley @*/
40820adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
40830adebc6cSBarry Smith {
4084b21d0597SMatthew G Knepley   PetscFunctionBegin;
4085b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4086b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4087b21d0597SMatthew G Knepley   *sf = dm->sf;
4088b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4089b21d0597SMatthew G Knepley }
4090b21d0597SMatthew G Knepley 
4091057b4bcdSMatthew G Knepley /*@
4092057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4093057b4bcdSMatthew G Knepley 
4094057b4bcdSMatthew G Knepley   Input Parameters:
4095057b4bcdSMatthew G Knepley + dm - The DM
4096057b4bcdSMatthew G Knepley - sf - The PetscSF
4097057b4bcdSMatthew G Knepley 
4098057b4bcdSMatthew G Knepley   Level: intermediate
4099057b4bcdSMatthew G Knepley 
4100057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4101057b4bcdSMatthew G Knepley @*/
41020adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
41030adebc6cSBarry Smith {
4104057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4105057b4bcdSMatthew G Knepley 
4106057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4107057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4108057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
4109057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4110057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4111057b4bcdSMatthew G Knepley   dm->sf = sf;
4112057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4113057b4bcdSMatthew G Knepley }
4114057b4bcdSMatthew G Knepley 
41152764a2aaSMatthew G. Knepley /*@
41162764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
41172764a2aaSMatthew G. Knepley 
41182764a2aaSMatthew G. Knepley   Input Parameter:
41192764a2aaSMatthew G. Knepley . dm - The DM
41202764a2aaSMatthew G. Knepley 
41212764a2aaSMatthew G. Knepley   Output Parameter:
41222764a2aaSMatthew G. Knepley . prob - The PetscDS
41232764a2aaSMatthew G. Knepley 
41242764a2aaSMatthew G. Knepley   Level: developer
41252764a2aaSMatthew G. Knepley 
41262764a2aaSMatthew G. Knepley .seealso: DMSetDS()
41272764a2aaSMatthew G. Knepley @*/
41282764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4129af122d2aSMatthew G Knepley {
4130af122d2aSMatthew G Knepley   PetscFunctionBegin;
4131af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41320f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
41330f21e855SMatthew G. Knepley   *prob = dm->prob;
41340f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41350f21e855SMatthew G. Knepley }
41360f21e855SMatthew G. Knepley 
41372764a2aaSMatthew G. Knepley /*@
41382764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
41392764a2aaSMatthew G. Knepley 
41402764a2aaSMatthew G. Knepley   Input Parameters:
41412764a2aaSMatthew G. Knepley + dm - The DM
41422764a2aaSMatthew G. Knepley - prob - The PetscDS
41432764a2aaSMatthew G. Knepley 
41442764a2aaSMatthew G. Knepley   Level: developer
41452764a2aaSMatthew G. Knepley 
41462764a2aaSMatthew G. Knepley .seealso: DMGetDS()
41472764a2aaSMatthew G. Knepley @*/
41482764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
41490f21e855SMatthew G. Knepley {
4150f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
41510f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41520f21e855SMatthew G. Knepley 
41530f21e855SMatthew G. Knepley   PetscFunctionBegin;
41540f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41552764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
415637316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
41572764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
41580f21e855SMatthew G. Knepley   dm->prob = prob;
4159f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4160f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
41610f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41620f21e855SMatthew G. Knepley }
41630f21e855SMatthew G. Knepley 
41640f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
41650f21e855SMatthew G. Knepley {
41660f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41670f21e855SMatthew G. Knepley 
41680f21e855SMatthew G. Knepley   PetscFunctionBegin;
41690f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41702764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
4171af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4172af122d2aSMatthew G Knepley }
4173af122d2aSMatthew G Knepley 
4174af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4175af122d2aSMatthew G Knepley {
41760f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4177af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4178af122d2aSMatthew G Knepley 
4179af122d2aSMatthew G Knepley   PetscFunctionBegin;
4180af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41812764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
41820f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
41830f21e855SMatthew G. Knepley     PetscContainer obj;
41840f21e855SMatthew G. Knepley 
41850f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
41862764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
41870f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4188af122d2aSMatthew G Knepley   }
4189af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4190af122d2aSMatthew G Knepley }
4191af122d2aSMatthew G Knepley 
4192c1929be8SMatthew G. Knepley /*@
4193c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4194c1929be8SMatthew G. Knepley 
4195c1929be8SMatthew G. Knepley   Not collective
4196c1929be8SMatthew G. Knepley 
4197c1929be8SMatthew G. Knepley   Input Parameters:
4198c1929be8SMatthew G. Knepley + dm - The DM
4199c1929be8SMatthew G. Knepley - f  - The field number
4200c1929be8SMatthew G. Knepley 
4201c1929be8SMatthew G. Knepley   Output Parameter:
4202c1929be8SMatthew G. Knepley . field - The discretization object
4203c1929be8SMatthew G. Knepley 
4204c1929be8SMatthew G. Knepley   Level: developer
4205c1929be8SMatthew G. Knepley 
4206c1929be8SMatthew G. Knepley .seealso: DMSetField()
4207c1929be8SMatthew G. Knepley @*/
4208af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4209af122d2aSMatthew G Knepley {
42100f21e855SMatthew G. Knepley   PetscErrorCode ierr;
42110f21e855SMatthew G. Knepley 
4212af122d2aSMatthew G Knepley   PetscFunctionBegin;
4213af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42142764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4215decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4216decb47aaSMatthew G. Knepley }
4217decb47aaSMatthew G. Knepley 
4218c1929be8SMatthew G. Knepley /*@
4219c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4220c1929be8SMatthew G. Knepley 
4221c1929be8SMatthew G. Knepley   Logically collective on DM
4222c1929be8SMatthew G. Knepley 
4223c1929be8SMatthew G. Knepley   Input Parameters:
4224c1929be8SMatthew G. Knepley + dm - The DM
4225c1929be8SMatthew G. Knepley . f  - The field number
4226c1929be8SMatthew G. Knepley - field - The discretization object
4227c1929be8SMatthew G. Knepley 
4228c1929be8SMatthew G. Knepley   Level: developer
4229c1929be8SMatthew G. Knepley 
4230c1929be8SMatthew G. Knepley .seealso: DMGetField()
4231c1929be8SMatthew G. Knepley @*/
4232decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4233decb47aaSMatthew G. Knepley {
4234decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4235decb47aaSMatthew G. Knepley 
4236decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4237decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42382764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4239af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4240af122d2aSMatthew G Knepley }
42416636e97aSMatthew G Knepley 
4242b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4243b64e0483SPeter Brune {
4244b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4245b64e0483SPeter Brune   PetscErrorCode ierr;
4246b64e0483SPeter Brune   Vec coords,ccoords;
42476dbf9973SLawrence Mitchell   Mat inject;
4248b64e0483SPeter Brune   PetscFunctionBegin;
4249b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4250b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4251b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4252b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4253b64e0483SPeter Brune   if (coords && !ccoords) {
4254b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
42556668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
42566dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
42572adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
42586dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4259b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4260b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4261b64e0483SPeter Brune   }
4262b64e0483SPeter Brune   PetscFunctionReturn(0);
4263b64e0483SPeter Brune }
4264b64e0483SPeter Brune 
426503dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
426603dadc2fSPeter Brune {
426703dadc2fSPeter Brune   DM dm_coord,subdm_coord;
426803dadc2fSPeter Brune   PetscErrorCode ierr;
426903dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
427003dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
427103dadc2fSPeter Brune   PetscFunctionBegin;
427203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
427303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
427403dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
427503dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
427603dadc2fSPeter Brune   if (coords && !ccoords) {
427703dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
42786668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
427903dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
428024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
428103dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
428203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
428303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
428403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
428503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
428603dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
428703dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
428803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
428903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
429003dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
429103dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
429203dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
429303dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
429403dadc2fSPeter Brune   }
429503dadc2fSPeter Brune   PetscFunctionReturn(0);
429603dadc2fSPeter Brune }
429703dadc2fSPeter Brune 
4298c73cfb54SMatthew G. Knepley /*@
4299c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4300c73cfb54SMatthew G. Knepley 
4301c73cfb54SMatthew G. Knepley   Not collective
4302c73cfb54SMatthew G. Knepley 
4303c73cfb54SMatthew G. Knepley   Input Parameter:
4304c73cfb54SMatthew G. Knepley . dm - The DM
4305c73cfb54SMatthew G. Knepley 
4306c73cfb54SMatthew G. Knepley   Output Parameter:
4307c73cfb54SMatthew G. Knepley . dim - The topological dimension
4308c73cfb54SMatthew G. Knepley 
4309c73cfb54SMatthew G. Knepley   Level: beginner
4310c73cfb54SMatthew G. Knepley 
4311c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4312c73cfb54SMatthew G. Knepley @*/
4313c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4314c73cfb54SMatthew G. Knepley {
4315c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4316c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4317c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4318c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4319c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4320c73cfb54SMatthew G. Knepley }
4321c73cfb54SMatthew G. Knepley 
4322c73cfb54SMatthew G. Knepley /*@
4323c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4324c73cfb54SMatthew G. Knepley 
4325c73cfb54SMatthew G. Knepley   Collective on dm
4326c73cfb54SMatthew G. Knepley 
4327c73cfb54SMatthew G. Knepley   Input Parameters:
4328c73cfb54SMatthew G. Knepley + dm - The DM
4329c73cfb54SMatthew G. Knepley - dim - The topological dimension
4330c73cfb54SMatthew G. Knepley 
4331c73cfb54SMatthew G. Knepley   Level: beginner
4332c73cfb54SMatthew G. Knepley 
4333c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4334c73cfb54SMatthew G. Knepley @*/
4335c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4336c73cfb54SMatthew G. Knepley {
4337f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4338f17e8794SMatthew G. Knepley 
4339c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4340c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4341c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4342c73cfb54SMatthew G. Knepley   dm->dim = dim;
4343f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4344c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4345c73cfb54SMatthew G. Knepley }
4346c73cfb54SMatthew G. Knepley 
4347793f3fe5SMatthew G. Knepley /*@
4348793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4349793f3fe5SMatthew G. Knepley 
4350793f3fe5SMatthew G. Knepley   Collective on DM
4351793f3fe5SMatthew G. Knepley 
4352793f3fe5SMatthew G. Knepley   Input Parameters:
4353793f3fe5SMatthew G. Knepley + dm - the DM
4354793f3fe5SMatthew G. Knepley - dim - the dimension
4355793f3fe5SMatthew G. Knepley 
4356793f3fe5SMatthew G. Knepley   Output Parameters:
4357793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4358793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4359793f3fe5SMatthew G. Knepley 
4360793f3fe5SMatthew G. Knepley   Note:
4361793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4362793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4363793f3fe5SMatthew G. Knepley   then the interval is empty.
4364793f3fe5SMatthew G. Knepley 
4365793f3fe5SMatthew G. Knepley   Level: intermediate
4366793f3fe5SMatthew G. Knepley 
4367793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4368793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4369793f3fe5SMatthew G. Knepley @*/
4370793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4371793f3fe5SMatthew G. Knepley {
4372793f3fe5SMatthew G. Knepley   PetscInt       d;
4373793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4374793f3fe5SMatthew G. Knepley 
4375793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4376793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4377793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4378793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4379793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4380793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4381793f3fe5SMatthew G. Knepley }
4382793f3fe5SMatthew G. Knepley 
43836636e97aSMatthew G Knepley /*@
43846636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
43856636e97aSMatthew G Knepley 
43866636e97aSMatthew G Knepley   Collective on DM
43876636e97aSMatthew G Knepley 
43886636e97aSMatthew G Knepley   Input Parameters:
43896636e97aSMatthew G Knepley + dm - the DM
43906636e97aSMatthew G Knepley - c - coordinate vector
43916636e97aSMatthew G Knepley 
43922dd40e9bSPatrick Sanan   Notes:
43932dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
43942dd40e9bSPatrick Sanan 
43952dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
43966636e97aSMatthew G Knepley 
43976636e97aSMatthew G Knepley   Level: intermediate
43986636e97aSMatthew G Knepley 
43996636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44002dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
44016636e97aSMatthew G Knepley @*/
44026636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
44036636e97aSMatthew G Knepley {
44046636e97aSMatthew G Knepley   PetscErrorCode ierr;
44056636e97aSMatthew G Knepley 
44066636e97aSMatthew G Knepley   PetscFunctionBegin;
44076636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44086636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
44096636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
44106636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
44116636e97aSMatthew G Knepley   dm->coordinates = c;
44126636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4413b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
441403dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
44156636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44166636e97aSMatthew G Knepley }
44176636e97aSMatthew G Knepley 
44186636e97aSMatthew G Knepley /*@
44196636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
44206636e97aSMatthew G Knepley 
44216636e97aSMatthew G Knepley   Collective on DM
44226636e97aSMatthew G Knepley 
44236636e97aSMatthew G Knepley    Input Parameters:
44246636e97aSMatthew G Knepley +  dm - the DM
44256636e97aSMatthew G Knepley -  c - coordinate vector
44266636e97aSMatthew G Knepley 
44272dd40e9bSPatrick Sanan   Notes:
44286636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
44296636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
44306636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
44316636e97aSMatthew G Knepley 
44322dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
44332dd40e9bSPatrick Sanan 
44346636e97aSMatthew G Knepley   Level: intermediate
44356636e97aSMatthew G Knepley 
44366636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44376636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
44386636e97aSMatthew G Knepley @*/
44396636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
44406636e97aSMatthew G Knepley {
44416636e97aSMatthew G Knepley   PetscErrorCode ierr;
44426636e97aSMatthew G Knepley 
44436636e97aSMatthew G Knepley   PetscFunctionBegin;
44446636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44456636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
44466636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
44476636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
44488865f1eaSKarl Rupp 
44496636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
44508865f1eaSKarl Rupp 
44516636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
44526636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44536636e97aSMatthew G Knepley }
44546636e97aSMatthew G Knepley 
44556636e97aSMatthew G Knepley /*@
44566636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
44576636e97aSMatthew G Knepley 
44586636e97aSMatthew G Knepley   Not Collective
44596636e97aSMatthew G Knepley 
44606636e97aSMatthew G Knepley   Input Parameter:
44616636e97aSMatthew G Knepley . dm - the DM
44626636e97aSMatthew G Knepley 
44636636e97aSMatthew G Knepley   Output Parameter:
44646636e97aSMatthew G Knepley . c - global coordinate vector
44656636e97aSMatthew G Knepley 
44666636e97aSMatthew G Knepley   Note:
44676636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
44686636e97aSMatthew G Knepley 
44696636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
44706636e97aSMatthew G Knepley 
44716636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
44726636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
44736636e97aSMatthew G Knepley 
44746636e97aSMatthew G Knepley   Level: intermediate
44756636e97aSMatthew G Knepley 
44766636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44776636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
44786636e97aSMatthew G Knepley @*/
44796636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
44806636e97aSMatthew G Knepley {
44816636e97aSMatthew G Knepley   PetscErrorCode ierr;
44826636e97aSMatthew G Knepley 
44836636e97aSMatthew G Knepley   PetscFunctionBegin;
44846636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44856636e97aSMatthew G Knepley   PetscValidPointer(c,2);
44861f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
44870298fd71SBarry Smith     DM cdm = NULL;
44886636e97aSMatthew G Knepley 
44896636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
44906636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
44916636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
44926636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
44936636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
44946636e97aSMatthew G Knepley   }
44956636e97aSMatthew G Knepley   *c = dm->coordinates;
44966636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44976636e97aSMatthew G Knepley }
44986636e97aSMatthew G Knepley 
44996636e97aSMatthew G Knepley /*@
45006636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
45016636e97aSMatthew G Knepley 
45026636e97aSMatthew G Knepley   Collective on DM
45036636e97aSMatthew G Knepley 
45046636e97aSMatthew G Knepley   Input Parameter:
45056636e97aSMatthew G Knepley . dm - the DM
45066636e97aSMatthew G Knepley 
45076636e97aSMatthew G Knepley   Output Parameter:
45086636e97aSMatthew G Knepley . c - coordinate vector
45096636e97aSMatthew G Knepley 
45106636e97aSMatthew G Knepley   Note:
45116636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
45126636e97aSMatthew G Knepley 
45136636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
45146636e97aSMatthew G Knepley 
45156636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
45166636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
45176636e97aSMatthew G Knepley 
45186636e97aSMatthew G Knepley   Level: intermediate
45196636e97aSMatthew G Knepley 
45206636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45216636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
45226636e97aSMatthew G Knepley @*/
45236636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
45246636e97aSMatthew G Knepley {
45256636e97aSMatthew G Knepley   PetscErrorCode ierr;
45266636e97aSMatthew G Knepley 
45276636e97aSMatthew G Knepley   PetscFunctionBegin;
45286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45296636e97aSMatthew G Knepley   PetscValidPointer(c,2);
45301f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
45310298fd71SBarry Smith     DM cdm = NULL;
45326636e97aSMatthew G Knepley 
45336636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
45346636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
45356636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
45366636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
45376636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
45386636e97aSMatthew G Knepley   }
45396636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
45406636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45416636e97aSMatthew G Knepley }
45426636e97aSMatthew G Knepley 
4543f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
4544f19dbd58SToby Isaac {
4545f19dbd58SToby Isaac   PetscErrorCode ierr;
4546f19dbd58SToby Isaac 
4547f19dbd58SToby Isaac   PetscFunctionBegin;
4548f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4549f19dbd58SToby Isaac   PetscValidPointer(field,2);
4550f19dbd58SToby Isaac   if (!dm->coordinateField) {
4551f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
4552f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
4553f19dbd58SToby Isaac     }
4554f19dbd58SToby Isaac   }
4555f19dbd58SToby Isaac   *field = dm->coordinateField;
4556f19dbd58SToby Isaac   PetscFunctionReturn(0);
4557f19dbd58SToby Isaac }
4558f19dbd58SToby Isaac 
4559f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
4560f19dbd58SToby Isaac {
4561f19dbd58SToby Isaac   PetscErrorCode ierr;
4562f19dbd58SToby Isaac 
4563f19dbd58SToby Isaac   PetscFunctionBegin;
4564f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4565f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
4566c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
4567f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
4568f19dbd58SToby Isaac   dm->coordinateField = field;
4569f19dbd58SToby Isaac   PetscFunctionReturn(0);
4570f19dbd58SToby Isaac }
4571f19dbd58SToby Isaac 
45726636e97aSMatthew G Knepley /*@
45731cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
45746636e97aSMatthew G Knepley 
45756636e97aSMatthew G Knepley   Collective on DM
45766636e97aSMatthew G Knepley 
45776636e97aSMatthew G Knepley   Input Parameter:
45786636e97aSMatthew G Knepley . dm - the DM
45796636e97aSMatthew G Knepley 
45806636e97aSMatthew G Knepley   Output Parameter:
45816636e97aSMatthew G Knepley . cdm - coordinate DM
45826636e97aSMatthew G Knepley 
45836636e97aSMatthew G Knepley   Level: intermediate
45846636e97aSMatthew G Knepley 
45856636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45861cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
45876636e97aSMatthew G Knepley @*/
45886636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
45896636e97aSMatthew G Knepley {
45906636e97aSMatthew G Knepley   PetscErrorCode ierr;
45916636e97aSMatthew G Knepley 
45926636e97aSMatthew G Knepley   PetscFunctionBegin;
45936636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45946636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
45956636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
459682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
45976636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
45986636e97aSMatthew G Knepley   }
45996636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
46006636e97aSMatthew G Knepley   PetscFunctionReturn(0);
46016636e97aSMatthew G Knepley }
4602e87bb0d3SMatthew G Knepley 
46031cfe2091SMatthew G. Knepley /*@
46041cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
46051cfe2091SMatthew G. Knepley 
46061cfe2091SMatthew G. Knepley   Logically Collective on DM
46071cfe2091SMatthew G. Knepley 
46081cfe2091SMatthew G. Knepley   Input Parameters:
46091cfe2091SMatthew G. Knepley + dm - the DM
46101cfe2091SMatthew G. Knepley - cdm - coordinate DM
46111cfe2091SMatthew G. Knepley 
46121cfe2091SMatthew G. Knepley   Level: intermediate
46131cfe2091SMatthew G. Knepley 
46141cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
46151cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
46161cfe2091SMatthew G. Knepley @*/
46171cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
46181cfe2091SMatthew G. Knepley {
46191cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
46201cfe2091SMatthew G. Knepley 
46211cfe2091SMatthew G. Knepley   PetscFunctionBegin;
46221cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46231cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4624f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
46251cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
46261cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
46271cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
46281cfe2091SMatthew G. Knepley }
46291cfe2091SMatthew G. Knepley 
463046e270d4SMatthew G. Knepley /*@
463146e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
463246e270d4SMatthew G. Knepley 
463346e270d4SMatthew G. Knepley   Not Collective
463446e270d4SMatthew G. Knepley 
463546e270d4SMatthew G. Knepley   Input Parameter:
463646e270d4SMatthew G. Knepley . dm - The DM object
463746e270d4SMatthew G. Knepley 
463846e270d4SMatthew G. Knepley   Output Parameter:
463946e270d4SMatthew G. Knepley . dim - The embedding dimension
464046e270d4SMatthew G. Knepley 
464146e270d4SMatthew G. Knepley   Level: intermediate
464246e270d4SMatthew G. Knepley 
464346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4644e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
464546e270d4SMatthew G. Knepley @*/
464646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
464746e270d4SMatthew G. Knepley {
464846e270d4SMatthew G. Knepley   PetscFunctionBegin;
464946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465046e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
46519a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
46529a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
46539a9a41abSToby Isaac   }
465446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
465546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
465646e270d4SMatthew G. Knepley }
465746e270d4SMatthew G. Knepley 
465846e270d4SMatthew G. Knepley /*@
465946e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
466046e270d4SMatthew G. Knepley 
466146e270d4SMatthew G. Knepley   Not Collective
466246e270d4SMatthew G. Knepley 
466346e270d4SMatthew G. Knepley   Input Parameters:
466446e270d4SMatthew G. Knepley + dm  - The DM object
466546e270d4SMatthew G. Knepley - dim - The embedding dimension
466646e270d4SMatthew G. Knepley 
466746e270d4SMatthew G. Knepley   Level: intermediate
466846e270d4SMatthew G. Knepley 
466946e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4670e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
467146e270d4SMatthew G. Knepley @*/
467246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
467346e270d4SMatthew G. Knepley {
4674f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4675f17e8794SMatthew G. Knepley 
467646e270d4SMatthew G. Knepley   PetscFunctionBegin;
467746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
467846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4679f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
468046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
468146e270d4SMatthew G. Knepley }
468246e270d4SMatthew G. Knepley 
4683e8abe2deSMatthew G. Knepley /*@
4684e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4685e8abe2deSMatthew G. Knepley 
4686e8abe2deSMatthew G. Knepley   Not Collective
4687e8abe2deSMatthew G. Knepley 
4688e8abe2deSMatthew G. Knepley   Input Parameter:
4689e8abe2deSMatthew G. Knepley . dm - The DM object
4690e8abe2deSMatthew G. Knepley 
4691e8abe2deSMatthew G. Knepley   Output Parameter:
4692e8abe2deSMatthew G. Knepley . section - The PetscSection object
4693e8abe2deSMatthew G. Knepley 
4694e8abe2deSMatthew G. Knepley   Level: intermediate
4695e8abe2deSMatthew G. Knepley 
4696e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4697e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
4698e8abe2deSMatthew G. Knepley @*/
4699e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4700e8abe2deSMatthew G. Knepley {
4701e8abe2deSMatthew G. Knepley   DM             cdm;
4702e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4703e8abe2deSMatthew G. Knepley 
4704e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4705e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4706e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4707e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4708e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
4709e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4710e8abe2deSMatthew G. Knepley }
4711e8abe2deSMatthew G. Knepley 
4712e8abe2deSMatthew G. Knepley /*@
4713e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4714e8abe2deSMatthew G. Knepley 
4715e8abe2deSMatthew G. Knepley   Not Collective
4716e8abe2deSMatthew G. Knepley 
4717e8abe2deSMatthew G. Knepley   Input Parameters:
4718e8abe2deSMatthew G. Knepley + dm      - The DM object
471946e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4720e8abe2deSMatthew G. Knepley - section - The PetscSection object
4721e8abe2deSMatthew G. Knepley 
4722e8abe2deSMatthew G. Knepley   Level: intermediate
4723e8abe2deSMatthew G. Knepley 
4724e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4725e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
4726e8abe2deSMatthew G. Knepley @*/
472746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4728e8abe2deSMatthew G. Knepley {
4729e8abe2deSMatthew G. Knepley   DM             cdm;
4730e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4731e8abe2deSMatthew G. Knepley 
4732e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4733e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
473446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4735e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4736e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
473746e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
47384c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
473946e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
474046e270d4SMatthew G. Knepley 
474146e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
474246e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
474346e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
474446e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
474546e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
474646e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
474746e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
474846e270d4SMatthew G. Knepley     }
47496be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
475046e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
475146e270d4SMatthew G. Knepley   }
4752e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4753e8abe2deSMatthew G. Knepley }
4754e8abe2deSMatthew G. Knepley 
47555dc8c3f7SMatthew G. Knepley /*@C
475690b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
47575dc8c3f7SMatthew G. Knepley 
47585dc8c3f7SMatthew G. Knepley   Input Parameters:
475990b157c4SStefano Zampini . dm      - The DM object
476090b157c4SStefano Zampini 
476190b157c4SStefano Zampini   Output Parameters:
476290b157c4SStefano Zampini + per     - Whether the DM is periodic or not
47635dc8c3f7SMatthew 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
47645dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
47655dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
47665dc8c3f7SMatthew G. Knepley 
47675dc8c3f7SMatthew G. Knepley   Level: developer
47685dc8c3f7SMatthew G. Knepley 
47695dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
47705dc8c3f7SMatthew G. Knepley @*/
477190b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4772c6b900c6SMatthew G. Knepley {
4773c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4774c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
477590b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4776c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4777c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
47785dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4779c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4780c6b900c6SMatthew G. Knepley }
4781c6b900c6SMatthew G. Knepley 
47825dc8c3f7SMatthew G. Knepley /*@C
47835dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
47845dc8c3f7SMatthew G. Knepley 
47855dc8c3f7SMatthew G. Knepley   Input Parameters:
47865dc8c3f7SMatthew G. Knepley + dm      - The DM object
478790b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
47885dc8c3f7SMatthew 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
47895dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
47905dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
47915dc8c3f7SMatthew G. Knepley 
47925dc8c3f7SMatthew G. Knepley   Level: developer
47935dc8c3f7SMatthew G. Knepley 
47945dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
47955dc8c3f7SMatthew G. Knepley @*/
479690b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4797c6b900c6SMatthew G. Knepley {
4798c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4799c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4800c6b900c6SMatthew G. Knepley 
4801c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4802c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
480390b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
480490b157c4SStefano Zampini   if (maxCell) {
480590b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
480690b157c4SStefano Zampini     PetscValidPointer(L,4);
480790b157c4SStefano Zampini     PetscValidPointer(bd,5);
480890b157c4SStefano Zampini   }
48095dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
48105dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
481190b157c4SStefano Zampini   if (maxCell) {
48125dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
48135dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
481490b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
481590b157c4SStefano Zampini   } else {
481690b157c4SStefano Zampini     dm->periodic = per;
481790b157c4SStefano Zampini   }
4818c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4819c6b900c6SMatthew G. Knepley }
4820c6b900c6SMatthew G. Knepley 
48212e17dfb7SMatthew G. Knepley /*@
48222e17dfb7SMatthew 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.
48232e17dfb7SMatthew G. Knepley 
48242e17dfb7SMatthew G. Knepley   Input Parameters:
48252e17dfb7SMatthew G. Knepley + dm     - The DM
482665da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
482765da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
48282e17dfb7SMatthew G. Knepley 
48292e17dfb7SMatthew G. Knepley   Output Parameter:
48302e17dfb7SMatthew G. Knepley . out - The localized coordinate point
48312e17dfb7SMatthew G. Knepley 
48322e17dfb7SMatthew G. Knepley   Level: developer
48332e17dfb7SMatthew G. Knepley 
48342e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
48352e17dfb7SMatthew G. Knepley @*/
483665da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
48372e17dfb7SMatthew G. Knepley {
48382e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
48392e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
48402e17dfb7SMatthew G. Knepley 
48412e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48422e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
48432e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48442e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
48452e17dfb7SMatthew G. Knepley   } else {
484665da65dcSMatthew G. Knepley     if (endpoint) {
484765da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4848da3333bfSMatthew 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)) {
4849da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
485065da65dcSMatthew G. Knepley         } else {
4851da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
485265da65dcSMatthew G. Knepley         }
485365da65dcSMatthew G. Knepley       }
485465da65dcSMatthew G. Knepley     } else {
48552e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
48561118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
48572e17dfb7SMatthew G. Knepley       }
48582e17dfb7SMatthew G. Knepley     }
485965da65dcSMatthew G. Knepley   }
48602e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48612e17dfb7SMatthew G. Knepley }
48622e17dfb7SMatthew G. Knepley 
48632e17dfb7SMatthew G. Knepley /*
48642e17dfb7SMatthew 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.
48652e17dfb7SMatthew G. Knepley 
48662e17dfb7SMatthew G. Knepley   Input Parameters:
48672e17dfb7SMatthew G. Knepley + dm     - The DM
48682e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
48692e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
48702e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
48712e17dfb7SMatthew G. Knepley 
48722e17dfb7SMatthew G. Knepley   Output Parameter:
48732e17dfb7SMatthew G. Knepley . out - The localized coordinate point
48742e17dfb7SMatthew G. Knepley 
48752e17dfb7SMatthew G. Knepley   Level: developer
48762e17dfb7SMatthew G. Knepley 
48772e17dfb7SMatthew 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
48782e17dfb7SMatthew G. Knepley 
48792e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
48802e17dfb7SMatthew G. Knepley */
48812e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
48822e17dfb7SMatthew G. Knepley {
48832e17dfb7SMatthew G. Knepley   PetscInt d;
48842e17dfb7SMatthew G. Knepley 
48852e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48862e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48872e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
48882e17dfb7SMatthew G. Knepley   } else {
48892e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4890908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
48912e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
48922e17dfb7SMatthew G. Knepley       } else {
48932e17dfb7SMatthew G. Knepley         out[d] = in[d];
48942e17dfb7SMatthew G. Knepley       }
48952e17dfb7SMatthew G. Knepley     }
48962e17dfb7SMatthew G. Knepley   }
48972e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48982e17dfb7SMatthew G. Knepley }
48992e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
49002e17dfb7SMatthew G. Knepley {
49012e17dfb7SMatthew G. Knepley   PetscInt d;
49022e17dfb7SMatthew G. Knepley 
49032e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
49042e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
49052e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
49062e17dfb7SMatthew G. Knepley   } else {
49072e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4908908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
49092e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
49102e17dfb7SMatthew G. Knepley       } else {
49112e17dfb7SMatthew G. Knepley         out[d] = in[d];
49122e17dfb7SMatthew G. Knepley       }
49132e17dfb7SMatthew G. Knepley     }
49142e17dfb7SMatthew G. Knepley   }
49152e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
49162e17dfb7SMatthew G. Knepley }
49172e17dfb7SMatthew G. Knepley 
49182e17dfb7SMatthew G. Knepley /*
49192e17dfb7SMatthew 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.
49202e17dfb7SMatthew G. Knepley 
49212e17dfb7SMatthew G. Knepley   Input Parameters:
49222e17dfb7SMatthew G. Knepley + dm     - The DM
49232e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
49242e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
49252e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
49262e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
49272e17dfb7SMatthew G. Knepley 
49282e17dfb7SMatthew G. Knepley   Output Parameter:
49292e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
49302e17dfb7SMatthew G. Knepley 
49312e17dfb7SMatthew G. Knepley   Level: developer
49322e17dfb7SMatthew G. Knepley 
49332e17dfb7SMatthew 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
49342e17dfb7SMatthew G. Knepley 
49352e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
49362e17dfb7SMatthew G. Knepley */
49372e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
49382e17dfb7SMatthew G. Knepley {
49392e17dfb7SMatthew G. Knepley   PetscInt d;
49402e17dfb7SMatthew G. Knepley 
49412e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
49422e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
49432e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
49442e17dfb7SMatthew G. Knepley   } else {
49452e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4946908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
49472e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
49482e17dfb7SMatthew G. Knepley       } else {
49492e17dfb7SMatthew G. Knepley         out[d] += in[d];
49502e17dfb7SMatthew G. Knepley       }
49512e17dfb7SMatthew G. Knepley     }
49522e17dfb7SMatthew G. Knepley   }
49532e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
49542e17dfb7SMatthew G. Knepley }
49552e17dfb7SMatthew G. Knepley 
495636447a5eSToby Isaac /*@
495736447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
495836447a5eSToby Isaac 
495936447a5eSToby Isaac   Input Parameter:
496036447a5eSToby Isaac . dm - The DM
496136447a5eSToby Isaac 
496236447a5eSToby Isaac   Output Parameter:
496336447a5eSToby Isaac   areLocalized - True if localized
496436447a5eSToby Isaac 
496536447a5eSToby Isaac   Level: developer
496636447a5eSToby Isaac 
496736447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
496836447a5eSToby Isaac @*/
496936447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
497036447a5eSToby Isaac {
497136447a5eSToby Isaac   DM             cdm;
497236447a5eSToby Isaac   PetscSection   coordSection;
497346a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
497446a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
497536447a5eSToby Isaac   PetscErrorCode ierr;
497636447a5eSToby Isaac 
497736447a5eSToby Isaac   PetscFunctionBegin;
497836447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
497946a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
498046a3a80fSLisandro Dalcin 
49818b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
498246a3a80fSLisandro Dalcin   if (!dm->periodic) PetscFunctionReturn(0); /* This is a hideous optimization hack! */
498346a3a80fSLisandro Dalcin 
498436447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
498536447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
498636447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
498746a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
498846a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
498946a3a80fSLisandro Dalcin 
499046a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
499136447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
499236447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
499346a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
499446a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
499536447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
499646a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
499736447a5eSToby Isaac   }
499846a3a80fSLisandro Dalcin   ierr = MPIU_Allreduce(&alreadyLocalized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
499936447a5eSToby Isaac   PetscFunctionReturn(0);
500036447a5eSToby Isaac }
500136447a5eSToby Isaac 
500236447a5eSToby Isaac 
50032e17dfb7SMatthew G. Knepley /*@
5004492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
50052e17dfb7SMatthew G. Knepley 
50062e17dfb7SMatthew G. Knepley   Input Parameter:
50072e17dfb7SMatthew G. Knepley . dm - The DM
50082e17dfb7SMatthew G. Knepley 
50092e17dfb7SMatthew G. Knepley   Level: developer
50102e17dfb7SMatthew G. Knepley 
50112e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
50122e17dfb7SMatthew G. Knepley @*/
50132e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
50142e17dfb7SMatthew G. Knepley {
50152e17dfb7SMatthew G. Knepley   DM             cdm;
50162e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
50172e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
50183e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
50193e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
5020e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
50213e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
50223e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
50232e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
50242e17dfb7SMatthew G. Knepley 
50252e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
50262e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
502792c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
5028f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
5029f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
5030f7cbd40bSStefano Zampini 
50312e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
50322e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
50332e17dfb7SMatthew G. Knepley   {
50342e17dfb7SMatthew G. Knepley     PetscBool isplex;
50352e17dfb7SMatthew G. Knepley 
50362e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
50372e17dfb7SMatthew G. Knepley     if (isplex) {
50382e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
50393e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
504069291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
50413e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
50423e922f36SToby Isaac       newStart = vStart;
50433e922f36SToby Isaac       newEnd   = vEnd;
50443e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
50453e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
50463e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
50473e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
50483e922f36SToby Isaac       }
50492e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
50502e17dfb7SMatthew G. Knepley   }
50512e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
505243eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
50532e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
50543e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
5055e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
50563e922f36SToby Isaac 
50572e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
50582e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
50592e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
50602e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
50613e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
50623e922f36SToby Isaac 
506369291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
50643e922f36SToby Isaac   localized = &anchor[bs];
50653e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
50663e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
50673e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
50683e922f36SToby Isaac 
50693e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
50703e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
50713e922f36SToby Isaac       PetscInt     b;
50723e922f36SToby Isaac 
50733e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
50743e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50753e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
50763e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
50773e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
50783e922f36SToby Isaac         for (b = 0; b < bs; b++) {
50793e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
50803e922f36SToby Isaac         }
50813e922f36SToby Isaac         if (b < bs) break;
50823e922f36SToby Isaac       }
50833e922f36SToby Isaac       if (d < dof/bs) {
50843e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
50853e922f36SToby Isaac           PetscInt cdof;
50863e922f36SToby Isaac 
50873e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
50883e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
50893e922f36SToby Isaac         }
50903e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
50913e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
50923e922f36SToby Isaac       }
50933e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50943e922f36SToby Isaac     }
50953e922f36SToby Isaac   }
50963e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
50973e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
509869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
50993e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
510069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
51013e922f36SToby Isaac     PetscFunctionReturn(0);
51023e922f36SToby Isaac   }
51032e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
51042e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
51052e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
51062e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
51072e17dfb7SMatthew G. Knepley   }
51082e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
51092e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5110c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
51112e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
51122e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
51132e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
51142e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5115c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
51162e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
51172e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
51182e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
51192e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
51202e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
51212e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
51222e17dfb7SMatthew G. Knepley   }
51233e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
51243e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
51253e922f36SToby Isaac 
51262e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
51272e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
51283e922f36SToby Isaac       PetscInt     b, cdof;
51292e17dfb7SMatthew G. Knepley 
51303e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
51313e922f36SToby Isaac       if (!cdof) continue;
51322e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
51332e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
51342e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
51352e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
51362e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
51372e17dfb7SMatthew G. Knepley     }
51383e922f36SToby Isaac   }
513969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
514069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5141c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
51422e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
51432e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
51442e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
51452e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
51462e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
51472e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
51482e17dfb7SMatthew G. Knepley }
51492e17dfb7SMatthew G. Knepley 
5150e87bb0d3SMatthew G Knepley /*@
51513a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5152e87bb0d3SMatthew G Knepley 
51533a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5154e87bb0d3SMatthew G Knepley 
5155e87bb0d3SMatthew G Knepley   Input Parameters:
5156e87bb0d3SMatthew G Knepley + dm - The DM
51573a93e3b7SToby Isaac . v - The Vec of points
515862a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
51593a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5160e87bb0d3SMatthew G Knepley 
516161e3bb9bSMatthew G Knepley   Output Parameter:
516262a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
516362a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
51643a93e3b7SToby Isaac 
5165e87bb0d3SMatthew G Knepley 
5166e87bb0d3SMatthew G Knepley   Level: developer
516761e3bb9bSMatthew G Knepley 
516862a38674SMatthew G. Knepley   Notes:
51693a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
517062a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
51713a93e3b7SToby Isaac 
51723a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
517362a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
51743a93e3b7SToby Isaac 
51753a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
51763a93e3b7SToby Isaac 
517762a38674SMatthew G. Knepley $    const PetscSFNode *cells;
517862a38674SMatthew G. Knepley $    PetscInt           nFound;
5179a6216909SToby Isaac $    const PetscInt    *found;
518062a38674SMatthew G. Knepley $
5181a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
51823a93e3b7SToby Isaac 
51833a93e3b7SToby 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
51843a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
51853a93e3b7SToby Isaac 
518661e3bb9bSMatthew G Knepley .keywords: point location, mesh
518762a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
518861e3bb9bSMatthew G Knepley @*/
518962a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5190e87bb0d3SMatthew G Knepley {
5191735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5192735aa83eSMatthew G Knepley 
5193e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5194e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5195e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5196e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
51973a93e3b7SToby Isaac   if (*cellSF) {
51983a93e3b7SToby Isaac     PetscMPIInt result;
51993a93e3b7SToby Isaac 
5200e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5201a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
52023a93e3b7SToby 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");
5203e0fc9d1bSMatthew G. Knepley   } else {
52043a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
52053a93e3b7SToby Isaac   }
520647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5207735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
520862a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
520982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
521047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5211e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5212e87bb0d3SMatthew G Knepley }
521314f150ffSMatthew G. Knepley 
5214f4d763aaSMatthew G. Knepley /*@
5215f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5216f4d763aaSMatthew G. Knepley 
5217f4d763aaSMatthew G. Knepley   Input Parameter:
5218f4d763aaSMatthew G. Knepley . dm - The original DM
5219f4d763aaSMatthew G. Knepley 
5220f4d763aaSMatthew G. Knepley   Output Parameter:
5221f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5222f4d763aaSMatthew G. Knepley 
5223f4d763aaSMatthew G. Knepley   Level: intermediate
5224f4d763aaSMatthew G. Knepley 
5225e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
5226f4d763aaSMatthew G. Knepley @*/
522714f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
522814f150ffSMatthew G. Knepley {
5229c26acbdeSMatthew G. Knepley   PetscSection   section;
52302d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
523114f150ffSMatthew G. Knepley   PetscErrorCode ierr;
523214f150ffSMatthew G. Knepley 
523314f150ffSMatthew G. Knepley   PetscFunctionBegin;
523414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
523514f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5236e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5237c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5238127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
52392d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5240c26acbdeSMatthew G. Knepley     *odm = dm;
5241c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5242c26acbdeSMatthew G. Knepley   }
524314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
52440305aff6SMatthew G. Knepley     PetscDS      ds;
5245c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
524614f150ffSMatthew G. Knepley     PetscSF      sf;
524714f150ffSMatthew G. Knepley 
524814f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
52490305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
52500305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
525114f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5252e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
525314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
525414f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
525515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5256e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
525714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
525814f150ffSMatthew G. Knepley   }
525914f150ffSMatthew G. Knepley   *odm = dm->dmBC;
526014f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
526114f150ffSMatthew G. Knepley }
5262f4d763aaSMatthew G. Knepley 
5263f4d763aaSMatthew G. Knepley /*@
5264cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5265f4d763aaSMatthew G. Knepley 
5266f4d763aaSMatthew G. Knepley   Input Parameter:
5267f4d763aaSMatthew G. Knepley . dm - The original DM
5268f4d763aaSMatthew G. Knepley 
5269cdb7a50dSMatthew G. Knepley   Output Parameters:
5270cdb7a50dSMatthew G. Knepley + num - The output sequence number
5271cdb7a50dSMatthew G. Knepley - val - The output sequence value
5272f4d763aaSMatthew G. Knepley 
5273f4d763aaSMatthew G. Knepley   Level: intermediate
5274f4d763aaSMatthew G. Knepley 
5275f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5276f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5277f4d763aaSMatthew G. Knepley 
5278f4d763aaSMatthew G. Knepley .seealso: VecView()
5279f4d763aaSMatthew G. Knepley @*/
5280cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5281f4d763aaSMatthew G. Knepley {
5282f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5283f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5284cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5285cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5286f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5287f4d763aaSMatthew G. Knepley }
5288f4d763aaSMatthew G. Knepley 
5289f4d763aaSMatthew G. Knepley /*@
5290cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5291f4d763aaSMatthew G. Knepley 
5292f4d763aaSMatthew G. Knepley   Input Parameters:
5293f4d763aaSMatthew G. Knepley + dm - The original DM
5294cdb7a50dSMatthew G. Knepley . num - The output sequence number
5295cdb7a50dSMatthew G. Knepley - val - The output sequence value
5296f4d763aaSMatthew G. Knepley 
5297f4d763aaSMatthew G. Knepley   Level: intermediate
5298f4d763aaSMatthew G. Knepley 
5299f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5300f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5301f4d763aaSMatthew G. Knepley 
5302f4d763aaSMatthew G. Knepley .seealso: VecView()
5303f4d763aaSMatthew G. Knepley @*/
5304cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5305f4d763aaSMatthew G. Knepley {
5306f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5307f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5308f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5309cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5310cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5311cdb7a50dSMatthew G. Knepley }
5312cdb7a50dSMatthew G. Knepley 
5313cdb7a50dSMatthew G. Knepley /*@C
5314cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5315cdb7a50dSMatthew G. Knepley 
5316cdb7a50dSMatthew G. Knepley   Input Parameters:
5317cdb7a50dSMatthew G. Knepley + dm   - The original DM
5318cdb7a50dSMatthew G. Knepley . name - The sequence name
5319cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5320cdb7a50dSMatthew G. Knepley 
5321cdb7a50dSMatthew G. Knepley   Output Parameter:
5322cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5323cdb7a50dSMatthew G. Knepley 
5324cdb7a50dSMatthew G. Knepley   Level: intermediate
5325cdb7a50dSMatthew G. Knepley 
5326cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5327cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5328cdb7a50dSMatthew G. Knepley 
5329cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5330cdb7a50dSMatthew G. Knepley @*/
5331cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5332cdb7a50dSMatthew G. Knepley {
5333cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5334cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5335cdb7a50dSMatthew G. Knepley 
5336cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5337cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5338cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5339cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5340cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5341cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5342cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5343cdb7a50dSMatthew G. Knepley     PetscScalar value;
5344cdb7a50dSMatthew G. Knepley 
534539d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
53464aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5347cdb7a50dSMatthew G. Knepley #endif
5348cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5349f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5350f4d763aaSMatthew G. Knepley }
53518e4ac7eaSMatthew G. Knepley 
53528e4ac7eaSMatthew G. Knepley /*@
53538e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
53548e4ac7eaSMatthew G. Knepley 
53558e4ac7eaSMatthew G. Knepley   Not collective
53568e4ac7eaSMatthew G. Knepley 
53578e4ac7eaSMatthew G. Knepley   Input Parameter:
53588e4ac7eaSMatthew G. Knepley . dm - The DM
53598e4ac7eaSMatthew G. Knepley 
53608e4ac7eaSMatthew G. Knepley   Output Parameter:
53618e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
53628e4ac7eaSMatthew G. Knepley 
53638e4ac7eaSMatthew G. Knepley   Level: beginner
53648e4ac7eaSMatthew G. Knepley 
53658e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
53668e4ac7eaSMatthew G. Knepley @*/
53678e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
53688e4ac7eaSMatthew G. Knepley {
53698e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
53708e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53718e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
53728e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
53738e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
53748e4ac7eaSMatthew G. Knepley }
53758e4ac7eaSMatthew G. Knepley 
53768e4ac7eaSMatthew G. Knepley /*@
53778e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
53788e4ac7eaSMatthew G. Knepley 
53798e4ac7eaSMatthew G. Knepley   Collective on dm
53808e4ac7eaSMatthew G. Knepley 
53818e4ac7eaSMatthew G. Knepley   Input Parameters:
53828e4ac7eaSMatthew G. Knepley + dm - The DM
53838e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
53848e4ac7eaSMatthew G. Knepley 
53858e4ac7eaSMatthew G. Knepley   Level: beginner
53868e4ac7eaSMatthew G. Knepley 
53878e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
53888e4ac7eaSMatthew G. Knepley @*/
53898e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
53908e4ac7eaSMatthew G. Knepley {
53918e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
53928e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53938833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
53948e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
53958e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
53968e4ac7eaSMatthew G. Knepley }
5397c58f1c22SToby Isaac 
5398c58f1c22SToby Isaac 
5399c58f1c22SToby Isaac /*@C
5400c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5401c58f1c22SToby Isaac 
5402c58f1c22SToby Isaac   Not Collective
5403c58f1c22SToby Isaac 
5404c58f1c22SToby Isaac   Input Parameters:
5405c58f1c22SToby Isaac + dm   - The DM object
5406c58f1c22SToby Isaac - name - The label name
5407c58f1c22SToby Isaac 
5408c58f1c22SToby Isaac   Level: intermediate
5409c58f1c22SToby Isaac 
5410c58f1c22SToby Isaac .keywords: mesh
5411c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5412c58f1c22SToby Isaac @*/
5413c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5414c58f1c22SToby Isaac {
5415c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5416c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5417c58f1c22SToby Isaac   PetscErrorCode ierr;
5418c58f1c22SToby Isaac 
5419c58f1c22SToby Isaac   PetscFunctionBegin;
5420c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5421c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5422c58f1c22SToby Isaac   while (next) {
5423c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5424c58f1c22SToby Isaac     if (flg) break;
5425c58f1c22SToby Isaac     next = next->next;
5426c58f1c22SToby Isaac   }
5427c58f1c22SToby Isaac   if (!flg) {
5428c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5429c58f1c22SToby Isaac 
5430c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5431c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5432c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5433c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5434c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5435c58f1c22SToby Isaac   }
5436c58f1c22SToby Isaac   PetscFunctionReturn(0);
5437c58f1c22SToby Isaac }
5438c58f1c22SToby Isaac 
5439c58f1c22SToby Isaac /*@C
5440c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5441c58f1c22SToby Isaac 
5442c58f1c22SToby Isaac   Not Collective
5443c58f1c22SToby Isaac 
5444c58f1c22SToby Isaac   Input Parameters:
5445c58f1c22SToby Isaac + dm   - The DM object
5446c58f1c22SToby Isaac . name - The label name
5447c58f1c22SToby Isaac - point - The mesh point
5448c58f1c22SToby Isaac 
5449c58f1c22SToby Isaac   Output Parameter:
5450c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5451c58f1c22SToby Isaac 
5452c58f1c22SToby Isaac   Level: beginner
5453c58f1c22SToby Isaac 
5454c58f1c22SToby Isaac .keywords: mesh
5455c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5456c58f1c22SToby Isaac @*/
5457c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5458c58f1c22SToby Isaac {
5459c58f1c22SToby Isaac   DMLabel        label;
5460c58f1c22SToby Isaac   PetscErrorCode ierr;
5461c58f1c22SToby Isaac 
5462c58f1c22SToby Isaac   PetscFunctionBegin;
5463c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5464c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5465c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
546613903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5467c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5468c58f1c22SToby Isaac   PetscFunctionReturn(0);
5469c58f1c22SToby Isaac }
5470c58f1c22SToby Isaac 
5471c58f1c22SToby Isaac /*@C
5472c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5473c58f1c22SToby Isaac 
5474c58f1c22SToby Isaac   Not Collective
5475c58f1c22SToby Isaac 
5476c58f1c22SToby Isaac   Input Parameters:
5477c58f1c22SToby Isaac + dm   - The DM object
5478c58f1c22SToby Isaac . name - The label name
5479c58f1c22SToby Isaac . point - The mesh point
5480c58f1c22SToby Isaac - value - The label value for this point
5481c58f1c22SToby Isaac 
5482c58f1c22SToby Isaac   Output Parameter:
5483c58f1c22SToby Isaac 
5484c58f1c22SToby Isaac   Level: beginner
5485c58f1c22SToby Isaac 
5486c58f1c22SToby Isaac .keywords: mesh
5487c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5488c58f1c22SToby Isaac @*/
5489c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5490c58f1c22SToby Isaac {
5491c58f1c22SToby Isaac   DMLabel        label;
5492c58f1c22SToby Isaac   PetscErrorCode ierr;
5493c58f1c22SToby Isaac 
5494c58f1c22SToby Isaac   PetscFunctionBegin;
5495c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5496c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5497c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5498c58f1c22SToby Isaac   if (!label) {
5499c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5500c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5501c58f1c22SToby Isaac   }
5502c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5503c58f1c22SToby Isaac   PetscFunctionReturn(0);
5504c58f1c22SToby Isaac }
5505c58f1c22SToby Isaac 
5506c58f1c22SToby Isaac /*@C
5507c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5508c58f1c22SToby Isaac 
5509c58f1c22SToby Isaac   Not Collective
5510c58f1c22SToby Isaac 
5511c58f1c22SToby Isaac   Input Parameters:
5512c58f1c22SToby Isaac + dm   - The DM object
5513c58f1c22SToby Isaac . name - The label name
5514c58f1c22SToby Isaac . point - The mesh point
5515c58f1c22SToby Isaac - value - The label value for this point
5516c58f1c22SToby Isaac 
5517c58f1c22SToby Isaac   Output Parameter:
5518c58f1c22SToby Isaac 
5519c58f1c22SToby Isaac   Level: beginner
5520c58f1c22SToby Isaac 
5521c58f1c22SToby Isaac .keywords: mesh
5522c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5523c58f1c22SToby Isaac @*/
5524c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5525c58f1c22SToby Isaac {
5526c58f1c22SToby Isaac   DMLabel        label;
5527c58f1c22SToby Isaac   PetscErrorCode ierr;
5528c58f1c22SToby Isaac 
5529c58f1c22SToby Isaac   PetscFunctionBegin;
5530c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5531c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5532c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5533c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5534c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5535c58f1c22SToby Isaac   PetscFunctionReturn(0);
5536c58f1c22SToby Isaac }
5537c58f1c22SToby Isaac 
5538c58f1c22SToby Isaac /*@C
5539c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5540c58f1c22SToby Isaac 
5541c58f1c22SToby Isaac   Not Collective
5542c58f1c22SToby Isaac 
5543c58f1c22SToby Isaac   Input Parameters:
5544c58f1c22SToby Isaac + dm   - The DM object
5545c58f1c22SToby Isaac - name - The label name
5546c58f1c22SToby Isaac 
5547c58f1c22SToby Isaac   Output Parameter:
5548c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5549c58f1c22SToby Isaac 
5550c58f1c22SToby Isaac   Level: beginner
5551c58f1c22SToby Isaac 
5552c58f1c22SToby Isaac .keywords: mesh
5553c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5554c58f1c22SToby Isaac @*/
5555c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5556c58f1c22SToby Isaac {
5557c58f1c22SToby Isaac   DMLabel        label;
5558c58f1c22SToby Isaac   PetscErrorCode ierr;
5559c58f1c22SToby Isaac 
5560c58f1c22SToby Isaac   PetscFunctionBegin;
5561c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5562c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5563c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5564c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5565c58f1c22SToby Isaac   *size = 0;
5566c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5567c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5568c58f1c22SToby Isaac   PetscFunctionReturn(0);
5569c58f1c22SToby Isaac }
5570c58f1c22SToby Isaac 
5571c58f1c22SToby Isaac /*@C
5572c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5573c58f1c22SToby Isaac 
5574c58f1c22SToby Isaac   Not Collective
5575c58f1c22SToby Isaac 
5576c58f1c22SToby Isaac   Input Parameters:
5577c58f1c22SToby Isaac + mesh - The DM object
5578c58f1c22SToby Isaac - name - The label name
5579c58f1c22SToby Isaac 
5580c58f1c22SToby Isaac   Output Parameter:
5581c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5582c58f1c22SToby Isaac 
5583c58f1c22SToby Isaac   Level: beginner
5584c58f1c22SToby Isaac 
5585c58f1c22SToby Isaac .keywords: mesh
5586c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5587c58f1c22SToby Isaac @*/
5588c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5589c58f1c22SToby Isaac {
5590c58f1c22SToby Isaac   DMLabel        label;
5591c58f1c22SToby Isaac   PetscErrorCode ierr;
5592c58f1c22SToby Isaac 
5593c58f1c22SToby Isaac   PetscFunctionBegin;
5594c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5595c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5596c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5597c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5598c58f1c22SToby Isaac   *ids = NULL;
5599dab2e251SBlaise Bourdin  if (label) {
5600c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5601dab2e251SBlaise Bourdin   } else {
5602dab2e251SBlaise Bourdin     /* returning an empty IS */
5603dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5604dab2e251SBlaise Bourdin   }
5605c58f1c22SToby Isaac   PetscFunctionReturn(0);
5606c58f1c22SToby Isaac }
5607c58f1c22SToby Isaac 
5608c58f1c22SToby Isaac /*@C
5609c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5610c58f1c22SToby Isaac 
5611c58f1c22SToby Isaac   Not Collective
5612c58f1c22SToby Isaac 
5613c58f1c22SToby Isaac   Input Parameters:
5614c58f1c22SToby Isaac + dm - The DM object
5615c58f1c22SToby Isaac . name - The label name
5616c58f1c22SToby Isaac - value - The stratum value
5617c58f1c22SToby Isaac 
5618c58f1c22SToby Isaac   Output Parameter:
5619c58f1c22SToby Isaac . size - The stratum size
5620c58f1c22SToby Isaac 
5621c58f1c22SToby Isaac   Level: beginner
5622c58f1c22SToby Isaac 
5623c58f1c22SToby Isaac .keywords: mesh
5624c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5625c58f1c22SToby Isaac @*/
5626c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5627c58f1c22SToby Isaac {
5628c58f1c22SToby Isaac   DMLabel        label;
5629c58f1c22SToby Isaac   PetscErrorCode ierr;
5630c58f1c22SToby Isaac 
5631c58f1c22SToby Isaac   PetscFunctionBegin;
5632c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5633c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5634c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5635c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5636c58f1c22SToby Isaac   *size = 0;
5637c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5638c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5639c58f1c22SToby Isaac   PetscFunctionReturn(0);
5640c58f1c22SToby Isaac }
5641c58f1c22SToby Isaac 
5642c58f1c22SToby Isaac /*@C
5643c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5644c58f1c22SToby Isaac 
5645c58f1c22SToby Isaac   Not Collective
5646c58f1c22SToby Isaac 
5647c58f1c22SToby Isaac   Input Parameters:
5648c58f1c22SToby Isaac + dm - The DM object
5649c58f1c22SToby Isaac . name - The label name
5650c58f1c22SToby Isaac - value - The stratum value
5651c58f1c22SToby Isaac 
5652c58f1c22SToby Isaac   Output Parameter:
5653c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5654c58f1c22SToby Isaac 
5655c58f1c22SToby Isaac   Level: beginner
5656c58f1c22SToby Isaac 
5657c58f1c22SToby Isaac .keywords: mesh
5658c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5659c58f1c22SToby Isaac @*/
5660c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5661c58f1c22SToby Isaac {
5662c58f1c22SToby Isaac   DMLabel        label;
5663c58f1c22SToby Isaac   PetscErrorCode ierr;
5664c58f1c22SToby Isaac 
5665c58f1c22SToby Isaac   PetscFunctionBegin;
5666c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5667c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5668c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5669c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5670c58f1c22SToby Isaac   *points = NULL;
5671c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5672c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5673c58f1c22SToby Isaac   PetscFunctionReturn(0);
5674c58f1c22SToby Isaac }
5675c58f1c22SToby Isaac 
56764de306b1SToby Isaac /*@C
56779044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
56784de306b1SToby Isaac 
56794de306b1SToby Isaac   Not Collective
56804de306b1SToby Isaac 
56814de306b1SToby Isaac   Input Parameters:
56824de306b1SToby Isaac + dm - The DM object
56834de306b1SToby Isaac . name - The label name
56844de306b1SToby Isaac . value - The stratum value
56854de306b1SToby Isaac - points - The stratum points
56864de306b1SToby Isaac 
56874de306b1SToby Isaac   Level: beginner
56884de306b1SToby Isaac 
56894de306b1SToby Isaac .keywords: mesh
56904de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
56914de306b1SToby Isaac @*/
56924de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
56934de306b1SToby Isaac {
56944de306b1SToby Isaac   DMLabel        label;
56954de306b1SToby Isaac   PetscErrorCode ierr;
56964de306b1SToby Isaac 
56974de306b1SToby Isaac   PetscFunctionBegin;
56984de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56994de306b1SToby Isaac   PetscValidCharPointer(name, 2);
57004de306b1SToby Isaac   PetscValidPointer(points, 4);
57014de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
57024de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
57034de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
57044de306b1SToby Isaac   PetscFunctionReturn(0);
57054de306b1SToby Isaac }
57064de306b1SToby Isaac 
5707c58f1c22SToby Isaac /*@C
5708c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5709c58f1c22SToby Isaac 
5710c58f1c22SToby Isaac   Not Collective
5711c58f1c22SToby Isaac 
5712c58f1c22SToby Isaac   Input Parameters:
5713c58f1c22SToby Isaac + dm   - The DM object
5714c58f1c22SToby Isaac . name - The label name
5715c58f1c22SToby Isaac - value - The label value for this point
5716c58f1c22SToby Isaac 
5717c58f1c22SToby Isaac   Output Parameter:
5718c58f1c22SToby Isaac 
5719c58f1c22SToby Isaac   Level: beginner
5720c58f1c22SToby Isaac 
5721c58f1c22SToby Isaac .keywords: mesh
5722c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5723c58f1c22SToby Isaac @*/
5724c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5725c58f1c22SToby Isaac {
5726c58f1c22SToby Isaac   DMLabel        label;
5727c58f1c22SToby Isaac   PetscErrorCode ierr;
5728c58f1c22SToby Isaac 
5729c58f1c22SToby Isaac   PetscFunctionBegin;
5730c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5731c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5732c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5733c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5734c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5735c58f1c22SToby Isaac   PetscFunctionReturn(0);
5736c58f1c22SToby Isaac }
5737c58f1c22SToby Isaac 
5738c58f1c22SToby Isaac /*@
5739c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5740c58f1c22SToby Isaac 
5741c58f1c22SToby Isaac   Not Collective
5742c58f1c22SToby Isaac 
5743c58f1c22SToby Isaac   Input Parameter:
5744c58f1c22SToby Isaac . dm   - The DM object
5745c58f1c22SToby Isaac 
5746c58f1c22SToby Isaac   Output Parameter:
5747c58f1c22SToby Isaac . numLabels - the number of Labels
5748c58f1c22SToby Isaac 
5749c58f1c22SToby Isaac   Level: intermediate
5750c58f1c22SToby Isaac 
5751c58f1c22SToby Isaac .keywords: mesh
5752c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5753c58f1c22SToby Isaac @*/
5754c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5755c58f1c22SToby Isaac {
5756c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5757c58f1c22SToby Isaac   PetscInt  n    = 0;
5758c58f1c22SToby Isaac 
5759c58f1c22SToby Isaac   PetscFunctionBegin;
5760c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5761c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5762c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5763c58f1c22SToby Isaac   *numLabels = n;
5764c58f1c22SToby Isaac   PetscFunctionReturn(0);
5765c58f1c22SToby Isaac }
5766c58f1c22SToby Isaac 
5767c58f1c22SToby Isaac /*@C
5768c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5769c58f1c22SToby Isaac 
5770c58f1c22SToby Isaac   Not Collective
5771c58f1c22SToby Isaac 
5772c58f1c22SToby Isaac   Input Parameters:
5773c58f1c22SToby Isaac + dm - The DM object
5774c58f1c22SToby Isaac - n  - the label number
5775c58f1c22SToby Isaac 
5776c58f1c22SToby Isaac   Output Parameter:
5777c58f1c22SToby Isaac . name - the label name
5778c58f1c22SToby Isaac 
5779c58f1c22SToby Isaac   Level: intermediate
5780c58f1c22SToby Isaac 
5781c58f1c22SToby Isaac .keywords: mesh
5782c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5783c58f1c22SToby Isaac @*/
5784c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5785c58f1c22SToby Isaac {
5786c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5787c58f1c22SToby Isaac   PetscInt  l    = 0;
5788c58f1c22SToby Isaac 
5789c58f1c22SToby Isaac   PetscFunctionBegin;
5790c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5791c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5792c58f1c22SToby Isaac   while (next) {
5793c58f1c22SToby Isaac     if (l == n) {
5794c58f1c22SToby Isaac       *name = next->label->name;
5795c58f1c22SToby Isaac       PetscFunctionReturn(0);
5796c58f1c22SToby Isaac     }
5797c58f1c22SToby Isaac     ++l;
5798c58f1c22SToby Isaac     next = next->next;
5799c58f1c22SToby Isaac   }
5800c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5801c58f1c22SToby Isaac }
5802c58f1c22SToby Isaac 
5803c58f1c22SToby Isaac /*@C
5804c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5805c58f1c22SToby Isaac 
5806c58f1c22SToby Isaac   Not Collective
5807c58f1c22SToby Isaac 
5808c58f1c22SToby Isaac   Input Parameters:
5809c58f1c22SToby Isaac + dm   - The DM object
5810c58f1c22SToby Isaac - name - The label name
5811c58f1c22SToby Isaac 
5812c58f1c22SToby Isaac   Output Parameter:
5813c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5814c58f1c22SToby Isaac 
5815c58f1c22SToby Isaac   Level: intermediate
5816c58f1c22SToby Isaac 
5817c58f1c22SToby Isaac .keywords: mesh
5818c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5819c58f1c22SToby Isaac @*/
5820c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5821c58f1c22SToby Isaac {
5822c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5823c58f1c22SToby Isaac   PetscErrorCode ierr;
5824c58f1c22SToby Isaac 
5825c58f1c22SToby Isaac   PetscFunctionBegin;
5826c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5827c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5828c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5829c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5830c58f1c22SToby Isaac   while (next) {
5831c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5832c58f1c22SToby Isaac     if (*hasLabel) break;
5833c58f1c22SToby Isaac     next = next->next;
5834c58f1c22SToby Isaac   }
5835c58f1c22SToby Isaac   PetscFunctionReturn(0);
5836c58f1c22SToby Isaac }
5837c58f1c22SToby Isaac 
5838c58f1c22SToby Isaac /*@C
5839c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5840c58f1c22SToby Isaac 
5841c58f1c22SToby Isaac   Not Collective
5842c58f1c22SToby Isaac 
5843c58f1c22SToby Isaac   Input Parameters:
5844c58f1c22SToby Isaac + dm   - The DM object
5845c58f1c22SToby Isaac - name - The label name
5846c58f1c22SToby Isaac 
5847c58f1c22SToby Isaac   Output Parameter:
5848c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5849c58f1c22SToby Isaac 
5850c58f1c22SToby Isaac   Level: intermediate
5851c58f1c22SToby Isaac 
5852c58f1c22SToby Isaac .keywords: mesh
5853c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5854c58f1c22SToby Isaac @*/
5855c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5856c58f1c22SToby Isaac {
5857c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5858c58f1c22SToby Isaac   PetscBool      hasLabel;
5859c58f1c22SToby Isaac   PetscErrorCode ierr;
5860c58f1c22SToby Isaac 
5861c58f1c22SToby Isaac   PetscFunctionBegin;
5862c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5863c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5864c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5865c58f1c22SToby Isaac   *label = NULL;
5866c58f1c22SToby Isaac   while (next) {
5867c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5868c58f1c22SToby Isaac     if (hasLabel) {
5869c58f1c22SToby Isaac       *label = next->label;
5870c58f1c22SToby Isaac       break;
5871c58f1c22SToby Isaac     }
5872c58f1c22SToby Isaac     next = next->next;
5873c58f1c22SToby Isaac   }
5874c58f1c22SToby Isaac   PetscFunctionReturn(0);
5875c58f1c22SToby Isaac }
5876c58f1c22SToby Isaac 
5877c58f1c22SToby Isaac /*@C
5878c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5879c58f1c22SToby Isaac 
5880c58f1c22SToby Isaac   Not Collective
5881c58f1c22SToby Isaac 
5882c58f1c22SToby Isaac   Input Parameters:
5883c58f1c22SToby Isaac + dm - The DM object
5884c58f1c22SToby Isaac - n  - the label number
5885c58f1c22SToby Isaac 
5886c58f1c22SToby Isaac   Output Parameter:
5887c58f1c22SToby Isaac . label - the label
5888c58f1c22SToby Isaac 
5889c58f1c22SToby Isaac   Level: intermediate
5890c58f1c22SToby Isaac 
5891c58f1c22SToby Isaac .keywords: mesh
5892c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5893c58f1c22SToby Isaac @*/
5894c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5895c58f1c22SToby Isaac {
5896c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5897c58f1c22SToby Isaac   PetscInt    l    = 0;
5898c58f1c22SToby Isaac 
5899c58f1c22SToby Isaac   PetscFunctionBegin;
5900c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5901c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5902c58f1c22SToby Isaac   while (next) {
5903c58f1c22SToby Isaac     if (l == n) {
5904c58f1c22SToby Isaac       *label = next->label;
5905c58f1c22SToby Isaac       PetscFunctionReturn(0);
5906c58f1c22SToby Isaac     }
5907c58f1c22SToby Isaac     ++l;
5908c58f1c22SToby Isaac     next = next->next;
5909c58f1c22SToby Isaac   }
5910c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5911c58f1c22SToby Isaac }
5912c58f1c22SToby Isaac 
5913c58f1c22SToby Isaac /*@C
5914c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5915c58f1c22SToby Isaac 
5916c58f1c22SToby Isaac   Not Collective
5917c58f1c22SToby Isaac 
5918c58f1c22SToby Isaac   Input Parameters:
5919c58f1c22SToby Isaac + dm   - The DM object
5920c58f1c22SToby Isaac - label - The DMLabel
5921c58f1c22SToby Isaac 
5922c58f1c22SToby Isaac   Level: developer
5923c58f1c22SToby Isaac 
5924c58f1c22SToby Isaac .keywords: mesh
5925c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5926c58f1c22SToby Isaac @*/
5927c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5928c58f1c22SToby Isaac {
5929c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5930c58f1c22SToby Isaac   PetscBool      hasLabel;
5931c58f1c22SToby Isaac   PetscErrorCode ierr;
5932c58f1c22SToby Isaac 
5933c58f1c22SToby Isaac   PetscFunctionBegin;
5934c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5935c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5936c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5937c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5938c58f1c22SToby Isaac   tmpLabel->label  = label;
5939c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5940c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5941c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5942c58f1c22SToby Isaac   PetscFunctionReturn(0);
5943c58f1c22SToby Isaac }
5944c58f1c22SToby Isaac 
5945c58f1c22SToby Isaac /*@C
5946c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5947c58f1c22SToby Isaac 
5948c58f1c22SToby Isaac   Not Collective
5949c58f1c22SToby Isaac 
5950c58f1c22SToby Isaac   Input Parameters:
5951c58f1c22SToby Isaac + dm   - The DM object
5952c58f1c22SToby Isaac - name - The label name
5953c58f1c22SToby Isaac 
5954c58f1c22SToby Isaac   Output Parameter:
5955c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5956c58f1c22SToby Isaac 
5957c58f1c22SToby Isaac   Level: developer
5958c58f1c22SToby Isaac 
5959c58f1c22SToby Isaac .keywords: mesh
5960c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5961c58f1c22SToby Isaac @*/
5962c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5963c58f1c22SToby Isaac {
5964c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5965c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5966c58f1c22SToby Isaac   PetscBool      hasLabel;
5967c58f1c22SToby Isaac   PetscErrorCode ierr;
5968c58f1c22SToby Isaac 
5969c58f1c22SToby Isaac   PetscFunctionBegin;
5970c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5971c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5972c58f1c22SToby Isaac   *label = NULL;
5973c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5974c58f1c22SToby Isaac   while (next) {
5975c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5976c58f1c22SToby Isaac     if (hasLabel) {
5977c58f1c22SToby Isaac       if (last) last->next       = next->next;
5978c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5979c58f1c22SToby Isaac       next->next = NULL;
5980c58f1c22SToby Isaac       *label     = next->label;
5981c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5982c58f1c22SToby Isaac       if (hasLabel) {
5983c58f1c22SToby Isaac         dm->depthLabel = NULL;
5984c58f1c22SToby Isaac       }
5985c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5986c58f1c22SToby Isaac       break;
5987c58f1c22SToby Isaac     }
5988c58f1c22SToby Isaac     last = next;
5989c58f1c22SToby Isaac     next = next->next;
5990c58f1c22SToby Isaac   }
5991c58f1c22SToby Isaac   PetscFunctionReturn(0);
5992c58f1c22SToby Isaac }
5993c58f1c22SToby Isaac 
5994c58f1c22SToby Isaac /*@C
5995c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5996c58f1c22SToby Isaac 
5997c58f1c22SToby Isaac   Not Collective
5998c58f1c22SToby Isaac 
5999c58f1c22SToby Isaac   Input Parameters:
6000c58f1c22SToby Isaac + dm   - The DM object
6001c58f1c22SToby Isaac - name - The label name
6002c58f1c22SToby Isaac 
6003c58f1c22SToby Isaac   Output Parameter:
6004c58f1c22SToby Isaac . output - The flag for output
6005c58f1c22SToby Isaac 
6006c58f1c22SToby Isaac   Level: developer
6007c58f1c22SToby Isaac 
6008c58f1c22SToby Isaac .keywords: mesh
6009c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6010c58f1c22SToby Isaac @*/
6011c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6012c58f1c22SToby Isaac {
6013c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6014c58f1c22SToby Isaac   PetscErrorCode ierr;
6015c58f1c22SToby Isaac 
6016c58f1c22SToby Isaac   PetscFunctionBegin;
6017c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6018c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6019c58f1c22SToby Isaac   PetscValidPointer(output, 3);
6020c58f1c22SToby Isaac   while (next) {
6021c58f1c22SToby Isaac     PetscBool flg;
6022c58f1c22SToby Isaac 
6023c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
6024c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6025c58f1c22SToby Isaac     next = next->next;
6026c58f1c22SToby Isaac   }
6027c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6028c58f1c22SToby Isaac }
6029c58f1c22SToby Isaac 
6030c58f1c22SToby Isaac /*@C
6031c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
6032c58f1c22SToby Isaac 
6033c58f1c22SToby Isaac   Not Collective
6034c58f1c22SToby Isaac 
6035c58f1c22SToby Isaac   Input Parameters:
6036c58f1c22SToby Isaac + dm     - The DM object
6037c58f1c22SToby Isaac . name   - The label name
6038c58f1c22SToby Isaac - output - The flag for output
6039c58f1c22SToby Isaac 
6040c58f1c22SToby Isaac   Level: developer
6041c58f1c22SToby Isaac 
6042c58f1c22SToby Isaac .keywords: mesh
6043c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6044c58f1c22SToby Isaac @*/
6045c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
6046c58f1c22SToby Isaac {
6047c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6048c58f1c22SToby Isaac   PetscErrorCode ierr;
6049c58f1c22SToby Isaac 
6050c58f1c22SToby Isaac   PetscFunctionBegin;
6051c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6052c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6053c58f1c22SToby Isaac   while (next) {
6054c58f1c22SToby Isaac     PetscBool flg;
6055c58f1c22SToby Isaac 
6056c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
6057c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
6058c58f1c22SToby Isaac     next = next->next;
6059c58f1c22SToby Isaac   }
6060c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6061c58f1c22SToby Isaac }
6062c58f1c22SToby Isaac 
6063c58f1c22SToby Isaac 
6064c58f1c22SToby Isaac /*@
6065c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
6066c58f1c22SToby Isaac 
6067c58f1c22SToby Isaac   Collective on DM
6068c58f1c22SToby Isaac 
6069c58f1c22SToby Isaac   Input Parameter:
60702e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
6071c58f1c22SToby Isaac 
6072c58f1c22SToby Isaac   Output Parameter:
60732e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
6074c58f1c22SToby Isaac 
6075c58f1c22SToby Isaac   Level: intermediate
6076c58f1c22SToby Isaac 
6077c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
6078c58f1c22SToby Isaac 
6079c58f1c22SToby Isaac .keywords: mesh
6080c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
6081c58f1c22SToby Isaac @*/
6082c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
6083c58f1c22SToby Isaac {
6084c58f1c22SToby Isaac   PetscInt       numLabels, l;
6085c58f1c22SToby Isaac   PetscErrorCode ierr;
6086c58f1c22SToby Isaac 
6087c58f1c22SToby Isaac   PetscFunctionBegin;
6088c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
6089c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
6090c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
6091c58f1c22SToby Isaac     DMLabel     label, labelNew;
6092c58f1c22SToby Isaac     const char *name;
6093c58f1c22SToby Isaac     PetscBool   flg;
6094c58f1c22SToby Isaac 
6095c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
6096c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
6097c58f1c22SToby Isaac     if (flg) continue;
60987d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
60997d5acc75SStefano Zampini     if (flg) continue;
6100c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
6101c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
6102c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
6103c58f1c22SToby Isaac   }
6104c58f1c22SToby Isaac   PetscFunctionReturn(0);
6105c58f1c22SToby Isaac }
6106a8fb8f29SToby Isaac 
6107a8fb8f29SToby Isaac /*@
6108a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
6109a8fb8f29SToby Isaac 
6110a8fb8f29SToby Isaac   Input Parameter:
6111a8fb8f29SToby Isaac . dm - The DM object
6112a8fb8f29SToby Isaac 
6113a8fb8f29SToby Isaac   Output Parameter:
6114a8fb8f29SToby Isaac . cdm - The coarse DM
6115a8fb8f29SToby Isaac 
6116a8fb8f29SToby Isaac   Level: intermediate
6117a8fb8f29SToby Isaac 
6118a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
6119a8fb8f29SToby Isaac @*/
6120a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6121a8fb8f29SToby Isaac {
6122a8fb8f29SToby Isaac   PetscFunctionBegin;
6123a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6124a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
6125a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
6126a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6127a8fb8f29SToby Isaac }
6128a8fb8f29SToby Isaac 
6129a8fb8f29SToby Isaac /*@
6130a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6131a8fb8f29SToby Isaac 
6132a8fb8f29SToby Isaac   Input Parameters:
6133a8fb8f29SToby Isaac + dm - The DM object
6134a8fb8f29SToby Isaac - cdm - The coarse DM
6135a8fb8f29SToby Isaac 
6136a8fb8f29SToby Isaac   Level: intermediate
6137a8fb8f29SToby Isaac 
6138a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
6139a8fb8f29SToby Isaac @*/
6140a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6141a8fb8f29SToby Isaac {
6142a8fb8f29SToby Isaac   PetscErrorCode ierr;
6143a8fb8f29SToby Isaac 
6144a8fb8f29SToby Isaac   PetscFunctionBegin;
6145a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6146a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6147a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6148a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6149a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6150a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6151a8fb8f29SToby Isaac }
6152a8fb8f29SToby Isaac 
615388bdff64SToby Isaac /*@
615488bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
615588bdff64SToby Isaac 
615688bdff64SToby Isaac   Input Parameter:
615788bdff64SToby Isaac . dm - The DM object
615888bdff64SToby Isaac 
615988bdff64SToby Isaac   Output Parameter:
616088bdff64SToby Isaac . fdm - The fine DM
616188bdff64SToby Isaac 
616288bdff64SToby Isaac   Level: intermediate
616388bdff64SToby Isaac 
616488bdff64SToby Isaac .seealso: DMSetFineDM()
616588bdff64SToby Isaac @*/
616688bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
616788bdff64SToby Isaac {
616888bdff64SToby Isaac   PetscFunctionBegin;
616988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
617088bdff64SToby Isaac   PetscValidPointer(fdm, 2);
617188bdff64SToby Isaac   *fdm = dm->fineMesh;
617288bdff64SToby Isaac   PetscFunctionReturn(0);
617388bdff64SToby Isaac }
617488bdff64SToby Isaac 
617588bdff64SToby Isaac /*@
617688bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
617788bdff64SToby Isaac 
617888bdff64SToby Isaac   Input Parameters:
617988bdff64SToby Isaac + dm - The DM object
618088bdff64SToby Isaac - fdm - The fine DM
618188bdff64SToby Isaac 
618288bdff64SToby Isaac   Level: intermediate
618388bdff64SToby Isaac 
618488bdff64SToby Isaac .seealso: DMGetFineDM()
618588bdff64SToby Isaac @*/
618688bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
618788bdff64SToby Isaac {
618888bdff64SToby Isaac   PetscErrorCode ierr;
618988bdff64SToby Isaac 
619088bdff64SToby Isaac   PetscFunctionBegin;
619188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
619288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
619388bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
619488bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
619588bdff64SToby Isaac   dm->fineMesh = fdm;
619688bdff64SToby Isaac   PetscFunctionReturn(0);
619788bdff64SToby Isaac }
619888bdff64SToby Isaac 
6199a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6200a6ba4734SToby Isaac 
6201a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6202a6ba4734SToby Isaac {
6203a6ba4734SToby Isaac   PetscErrorCode ierr;
6204a6ba4734SToby Isaac 
6205a6ba4734SToby Isaac   PetscFunctionBegin;
6206e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6207a6ba4734SToby Isaac   PetscFunctionReturn(0);
6208a6ba4734SToby Isaac }
6209a6ba4734SToby Isaac 
6210a6ba4734SToby Isaac /*@C
6211a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6212a6ba4734SToby Isaac 
6213a6ba4734SToby Isaac   Input Parameters:
62144c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6215f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6216a6ba4734SToby Isaac . name        - The BC name
6217a6ba4734SToby Isaac . labelname   - The label defining constrained points
6218a6ba4734SToby Isaac . field       - The field to constrain
6219a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6220a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6221a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6222a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6223a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6224a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6225a6ba4734SToby Isaac 
6226a6ba4734SToby Isaac   Options Database Keys:
6227a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6228a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6229a6ba4734SToby Isaac 
6230a6ba4734SToby Isaac   Level: developer
6231a6ba4734SToby Isaac 
6232a6ba4734SToby Isaac .seealso: DMGetBoundary()
6233a6ba4734SToby Isaac @*/
6234a30ec4eaSSatish 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)
6235a6ba4734SToby Isaac {
6236a6ba4734SToby Isaac   PetscErrorCode ierr;
6237a6ba4734SToby Isaac 
6238a6ba4734SToby Isaac   PetscFunctionBegin;
6239a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6240f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6241a6ba4734SToby Isaac   PetscFunctionReturn(0);
6242a6ba4734SToby Isaac }
6243a6ba4734SToby Isaac 
6244a6ba4734SToby Isaac /*@
6245a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6246a6ba4734SToby Isaac 
6247a6ba4734SToby Isaac   Input Parameters:
6248a6ba4734SToby Isaac . dm - The mesh object
6249a6ba4734SToby Isaac 
6250a6ba4734SToby Isaac   Output Parameters:
6251a6ba4734SToby Isaac . numBd - The number of BC
6252a6ba4734SToby Isaac 
6253a6ba4734SToby Isaac   Level: intermediate
6254a6ba4734SToby Isaac 
6255a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6256a6ba4734SToby Isaac @*/
6257a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6258a6ba4734SToby Isaac {
625958ebd649SToby Isaac   PetscErrorCode ierr;
6260a6ba4734SToby Isaac 
6261a6ba4734SToby Isaac   PetscFunctionBegin;
6262a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
626358ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6264a6ba4734SToby Isaac   PetscFunctionReturn(0);
6265a6ba4734SToby Isaac }
6266a6ba4734SToby Isaac 
6267a6ba4734SToby Isaac /*@C
62681c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6269a6ba4734SToby Isaac 
6270a6ba4734SToby Isaac   Input Parameters:
6271a6ba4734SToby Isaac + dm          - The mesh object
6272a6ba4734SToby Isaac - bd          - The BC number
6273a6ba4734SToby Isaac 
6274a6ba4734SToby Isaac   Output Parameters:
6275f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6276a6ba4734SToby Isaac . name        - The BC name
6277a6ba4734SToby Isaac . labelname   - The label defining constrained points
6278a6ba4734SToby Isaac . field       - The field to constrain
6279a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6280a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6281a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6282a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6283a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6284a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6285a6ba4734SToby Isaac 
6286a6ba4734SToby Isaac   Options Database Keys:
6287a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6288a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6289a6ba4734SToby Isaac 
6290a6ba4734SToby Isaac   Level: developer
6291a6ba4734SToby Isaac 
6292a6ba4734SToby Isaac .seealso: DMAddBoundary()
6293a6ba4734SToby Isaac @*/
6294a30ec4eaSSatish 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)
6295a6ba4734SToby Isaac {
629658ebd649SToby Isaac   PetscErrorCode ierr;
6297a6ba4734SToby Isaac 
6298a6ba4734SToby Isaac   PetscFunctionBegin;
6299a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6300f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6301a6ba4734SToby Isaac   PetscFunctionReturn(0);
6302a6ba4734SToby Isaac }
6303a6ba4734SToby Isaac 
6304e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6305e6f8dbb6SToby Isaac {
6306dff059c6SToby Isaac   DMBoundary *lastnext;
6307e6f8dbb6SToby Isaac   DSBoundary dsbound;
6308e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6309e6f8dbb6SToby Isaac 
6310e6f8dbb6SToby Isaac   PetscFunctionBegin;
6311e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
631247a1f5adSToby Isaac   if (dm->boundary) {
631347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
631447a1f5adSToby Isaac 
631547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
631647a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
631747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
631847a1f5adSToby Isaac     while (next) {
631947a1f5adSToby Isaac       DMBoundary b = next;
632047a1f5adSToby Isaac 
632147a1f5adSToby Isaac       next = b->next;
632247a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6323a6ba4734SToby Isaac     }
632447a1f5adSToby Isaac     dm->boundary = NULL;
6325a6ba4734SToby Isaac   }
632647a1f5adSToby Isaac 
6327dff059c6SToby Isaac   lastnext = &(dm->boundary);
6328e6f8dbb6SToby Isaac   while (dsbound) {
6329e6f8dbb6SToby Isaac     DMBoundary dmbound;
6330e6f8dbb6SToby Isaac 
6331e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6332e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6333e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6334994fe344SLisandro 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);}
633547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6336dff059c6SToby Isaac     *lastnext = dmbound;
6337dff059c6SToby Isaac     lastnext = &(dmbound->next);
6338dff059c6SToby Isaac     dsbound = dsbound->next;
6339a6ba4734SToby Isaac   }
6340a6ba4734SToby Isaac   PetscFunctionReturn(0);
6341a6ba4734SToby Isaac }
6342a6ba4734SToby Isaac 
6343a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6344a6ba4734SToby Isaac {
6345b95f2879SToby Isaac   DMBoundary     b;
6346a6ba4734SToby Isaac   PetscErrorCode ierr;
6347a6ba4734SToby Isaac 
6348a6ba4734SToby Isaac   PetscFunctionBegin;
6349a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6350a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6351a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6352e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6353b95f2879SToby Isaac   b = dm->boundary;
6354a6ba4734SToby Isaac   while (b && !(*isBd)) {
6355e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6356e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
63573424c85cSToby Isaac 
63583424c85cSToby Isaac     if (label) {
6359a6ba4734SToby Isaac       PetscInt i;
6360a6ba4734SToby Isaac 
6361e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6362e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6363a6ba4734SToby Isaac       }
6364a6ba4734SToby Isaac     }
6365a6ba4734SToby Isaac     b = b->next;
6366a6ba4734SToby Isaac   }
6367a6ba4734SToby Isaac   PetscFunctionReturn(0);
6368a6ba4734SToby Isaac }
63694d6f44ffSToby Isaac 
63704d6f44ffSToby Isaac /*@C
63714d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
63724d6f44ffSToby Isaac 
63734d6f44ffSToby Isaac   Input Parameters:
63744d6f44ffSToby Isaac + dm      - The DM
63750709b2feSToby Isaac . time    - The time
63764d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
63774d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
63784d6f44ffSToby Isaac - mode    - The insertion mode for values
63794d6f44ffSToby Isaac 
63804d6f44ffSToby Isaac   Output Parameter:
63814d6f44ffSToby Isaac . X - vector
63824d6f44ffSToby Isaac 
63834d6f44ffSToby Isaac    Calling sequence of func:
63840709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
63854d6f44ffSToby Isaac 
63864d6f44ffSToby Isaac +  dim - The spatial dimension
63874d6f44ffSToby Isaac .  x   - The coordinates
63884d6f44ffSToby Isaac .  Nf  - The number of fields
63894d6f44ffSToby Isaac .  u   - The output field values
63904d6f44ffSToby Isaac -  ctx - optional user-defined function context
63914d6f44ffSToby Isaac 
63924d6f44ffSToby Isaac   Level: developer
63934d6f44ffSToby Isaac 
63942716604bSToby Isaac .seealso: DMComputeL2Diff()
63954d6f44ffSToby Isaac @*/
63960709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
63974d6f44ffSToby Isaac {
63984d6f44ffSToby Isaac   Vec            localX;
63994d6f44ffSToby Isaac   PetscErrorCode ierr;
64004d6f44ffSToby Isaac 
64014d6f44ffSToby Isaac   PetscFunctionBegin;
64024d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64034d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
64040709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
64054d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
64064d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
64074d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
64084d6f44ffSToby Isaac   PetscFunctionReturn(0);
64094d6f44ffSToby Isaac }
64104d6f44ffSToby Isaac 
64110709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
64124d6f44ffSToby Isaac {
64134d6f44ffSToby Isaac   PetscErrorCode ierr;
64144d6f44ffSToby Isaac 
64154d6f44ffSToby Isaac   PetscFunctionBegin;
64164d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64174d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
64180918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
64190709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
64204d6f44ffSToby Isaac   PetscFunctionReturn(0);
64214d6f44ffSToby Isaac }
64224d6f44ffSToby Isaac 
64232c53366bSMatthew 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)
64242c53366bSMatthew G. Knepley {
64252c53366bSMatthew G. Knepley   Vec            localX;
64262c53366bSMatthew G. Knepley   PetscErrorCode ierr;
64272c53366bSMatthew G. Knepley 
64282c53366bSMatthew G. Knepley   PetscFunctionBegin;
64292c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64302c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
64312c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
64322c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
64332c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
64342c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
64352c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
64362c53366bSMatthew G. Knepley }
64372c53366bSMatthew G. Knepley 
64381c531cf8SMatthew 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)
64394d6f44ffSToby Isaac {
64404d6f44ffSToby Isaac   PetscErrorCode ierr;
64414d6f44ffSToby Isaac 
64424d6f44ffSToby Isaac   PetscFunctionBegin;
64434d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64444d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
64450918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
64461c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
64474d6f44ffSToby Isaac   PetscFunctionReturn(0);
64484d6f44ffSToby Isaac }
64492716604bSToby Isaac 
64508c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
64518c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
64528c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
64538c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6454191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
64558c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
64568c6c5593SMatthew G. Knepley {
64578c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
64588c6c5593SMatthew G. Knepley 
64598c6c5593SMatthew G. Knepley   PetscFunctionBegin;
64608c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64618c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
64628c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
64630918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
64648c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
64658c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
64668c6c5593SMatthew G. Knepley }
64678c6c5593SMatthew G. Knepley 
64681c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
64698c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
64708c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
64718c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6472191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
64738c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
64748c6c5593SMatthew G. Knepley {
64758c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
64768c6c5593SMatthew G. Knepley 
64778c6c5593SMatthew G. Knepley   PetscFunctionBegin;
64788c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64798c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
64808c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
64810918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
64821c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
64838c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
64848c6c5593SMatthew G. Knepley }
64858c6c5593SMatthew G. Knepley 
64862716604bSToby Isaac /*@C
64872716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
64882716604bSToby Isaac 
64892716604bSToby Isaac   Input Parameters:
64902716604bSToby Isaac + dm    - The DM
64910709b2feSToby Isaac . time  - The time
64922716604bSToby Isaac . funcs - The functions to evaluate for each field component
64932716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6494574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
64952716604bSToby Isaac 
64962716604bSToby Isaac   Output Parameter:
64972716604bSToby Isaac . diff - The diff ||u - u_h||_2
64982716604bSToby Isaac 
64992716604bSToby Isaac   Level: developer
65002716604bSToby Isaac 
65011189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
65022716604bSToby Isaac @*/
65030709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
65042716604bSToby Isaac {
65052716604bSToby Isaac   PetscErrorCode ierr;
65062716604bSToby Isaac 
65072716604bSToby Isaac   PetscFunctionBegin;
65082716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6509b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
65100918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
65110709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
65122716604bSToby Isaac   PetscFunctionReturn(0);
65132716604bSToby Isaac }
6514b698f381SToby Isaac 
6515b698f381SToby Isaac /*@C
6516b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6517b698f381SToby Isaac 
6518b698f381SToby Isaac   Input Parameters:
6519b698f381SToby Isaac + dm    - The DM
6520b698f381SToby Isaac , time  - The time
6521b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6522b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6523574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6524b698f381SToby Isaac - n     - The vector to project along
6525b698f381SToby Isaac 
6526b698f381SToby Isaac   Output Parameter:
6527b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6528b698f381SToby Isaac 
6529b698f381SToby Isaac   Level: developer
6530b698f381SToby Isaac 
6531b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6532b698f381SToby Isaac @*/
6533b698f381SToby 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)
6534b698f381SToby Isaac {
6535b698f381SToby Isaac   PetscErrorCode ierr;
6536b698f381SToby Isaac 
6537b698f381SToby Isaac   PetscFunctionBegin;
6538b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6539b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6540b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6541b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6542b698f381SToby Isaac   PetscFunctionReturn(0);
6543b698f381SToby Isaac }
6544b698f381SToby Isaac 
65452a16baeaSToby Isaac /*@C
65462a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
65472a16baeaSToby Isaac 
65482a16baeaSToby Isaac   Input Parameters:
65492a16baeaSToby Isaac + dm    - The DM
65502a16baeaSToby Isaac . time  - The time
65512a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
65522a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6553574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
65542a16baeaSToby Isaac 
65552a16baeaSToby Isaac   Output Parameter:
65562a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
65572a16baeaSToby Isaac 
65582a16baeaSToby Isaac   Level: developer
65592a16baeaSToby Isaac 
65601189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
65612a16baeaSToby Isaac @*/
65621189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
65632a16baeaSToby Isaac {
65642a16baeaSToby Isaac   PetscErrorCode ierr;
65652a16baeaSToby Isaac 
65662a16baeaSToby Isaac   PetscFunctionBegin;
65672a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65682a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
65690918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
65702a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
65712a16baeaSToby Isaac   PetscFunctionReturn(0);
65722a16baeaSToby Isaac }
65732a16baeaSToby Isaac 
6574df0b854cSToby Isaac /*@C
6575df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6576cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6577df0b854cSToby Isaac 
6578df0b854cSToby Isaac   Collective on dm
6579df0b854cSToby Isaac 
6580df0b854cSToby Isaac   Input parameters:
6581df0b854cSToby Isaac + dm - the pre-adaptation DM object
6582a1b0c543SToby Isaac - label - label with the flags
6583df0b854cSToby Isaac 
6584df0b854cSToby Isaac   Output parameters:
65850d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6586df0b854cSToby Isaac 
6587df0b854cSToby Isaac   Level: intermediate
65880d1cd5e0SMatthew G. Knepley 
65890d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6590df0b854cSToby Isaac @*/
65910d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6592df0b854cSToby Isaac {
6593df0b854cSToby Isaac   PetscErrorCode ierr;
6594df0b854cSToby Isaac 
6595df0b854cSToby Isaac   PetscFunctionBegin;
6596df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6597a1b0c543SToby Isaac   PetscValidPointer(label,2);
65980d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
65990d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
66006f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
66010d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
66020d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
66030d1cd5e0SMatthew G. Knepley }
66040d1cd5e0SMatthew G. Knepley 
66050d1cd5e0SMatthew G. Knepley /*@C
66060d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
66070d1cd5e0SMatthew G. Knepley 
66080d1cd5e0SMatthew G. Knepley   Input Parameters:
66090d1cd5e0SMatthew G. Knepley + dm - The DM object
66100d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
66116f25b0d8SLisandro 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_".
66120d1cd5e0SMatthew G. Knepley 
66130d1cd5e0SMatthew G. Knepley   Output Parameter:
66140d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
66150d1cd5e0SMatthew G. Knepley 
66160d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
66170d1cd5e0SMatthew G. Knepley 
66180d1cd5e0SMatthew G. Knepley   Level: advanced
66190d1cd5e0SMatthew G. Knepley 
66200d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
66210d1cd5e0SMatthew G. Knepley @*/
66220d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
66230d1cd5e0SMatthew G. Knepley {
66240d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
66250d1cd5e0SMatthew G. Knepley 
66260d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
66270d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66280d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
66290d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
66300d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
66316f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
66326f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
66330d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6634df0b854cSToby Isaac   PetscFunctionReturn(0);
6635df0b854cSToby Isaac }
6636c4088d22SMatthew G. Knepley 
6637502a2867SDave May /*@C
6638502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6639502a2867SDave May 
6640502a2867SDave May  Not Collective
6641502a2867SDave May 
6642502a2867SDave May  Input Parameter:
6643502a2867SDave May  . dm    - The DM
6644502a2867SDave May 
6645502a2867SDave May  Output Parameter:
6646502a2867SDave May  . nranks - the number of neighbours
6647502a2867SDave May  . ranks - the neighbors ranks
6648502a2867SDave May 
6649502a2867SDave May  Notes:
6650502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6651502a2867SDave May 
6652502a2867SDave May  Level: beginner
6653502a2867SDave May 
6654dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6655502a2867SDave May @*/
6656502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6657502a2867SDave May {
6658502a2867SDave May   PetscErrorCode ierr;
6659502a2867SDave May 
6660502a2867SDave May   PetscFunctionBegin;
6661502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66620918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6663502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6664502a2867SDave May   PetscFunctionReturn(0);
6665502a2867SDave May }
6666502a2867SDave May 
6667531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6668531c7667SBarry Smith 
6669531c7667SBarry Smith /*
6670531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6671531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6672531c7667SBarry Smith */
6673531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6674531c7667SBarry Smith {
6675531c7667SBarry Smith   PetscErrorCode ierr;
6676531c7667SBarry Smith 
6677531c7667SBarry Smith   PetscFunctionBegin;
6678531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6679531c7667SBarry Smith     Vec x1local;
6680531c7667SBarry Smith     DM  dm;
6681531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6682531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6683531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6684531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6685531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6686531c7667SBarry Smith     x1   = x1local;
6687531c7667SBarry Smith   }
6688531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6689531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6690531c7667SBarry Smith     DM  dm;
6691531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6692531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6693531c7667SBarry Smith   }
6694531c7667SBarry Smith   PetscFunctionReturn(0);
6695531c7667SBarry Smith }
6696531c7667SBarry Smith 
6697531c7667SBarry Smith /*@
6698531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6699531c7667SBarry Smith 
6700531c7667SBarry Smith     Input Parameter:
6701531c7667SBarry Smith .    coloring - the MatFDColoring object
6702531c7667SBarry Smith 
670395452b02SPatrick Sanan     Developer Notes:
670495452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
6705531c7667SBarry Smith 
67061b266c99SBarry Smith     Level: advanced
67071b266c99SBarry Smith 
6708531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6709531c7667SBarry Smith @*/
6710531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6711531c7667SBarry Smith {
6712531c7667SBarry Smith   PetscFunctionBegin;
6713531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6714531c7667SBarry Smith   PetscFunctionReturn(0);
6715531c7667SBarry Smith }
67168320bc6fSPatrick Sanan 
67178320bc6fSPatrick Sanan /*@
67188320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
67198320bc6fSPatrick Sanan 
67208320bc6fSPatrick Sanan     Collective
67218320bc6fSPatrick Sanan 
67228320bc6fSPatrick Sanan     Input Parameters:
67238320bc6fSPatrick Sanan +    dm - the first DM
67248320bc6fSPatrick Sanan -    dm2 - the second DM
67258320bc6fSPatrick Sanan 
67268320bc6fSPatrick Sanan     Output Parameters:
67278320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
67288320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
67298320bc6fSPatrick Sanan 
67308320bc6fSPatrick Sanan     Notes:
67318320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
67328320bc6fSPatrick Sanan     of the same topology. This implies that the the section (field data) on one
67338320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
67348320bc6fSPatrick Sanan     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
67358320bc6fSPatrick Sanan     decomposition, with different data.
67368320bc6fSPatrick Sanan 
67378320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
67388320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
67398320bc6fSPatrick Sanan 
67408320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
67418320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
67428320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
67438320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
67448320bc6fSPatrick Sanan 
67458320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
67468320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
67478320bc6fSPatrick Sanan .vb
67488320bc6fSPatrick Sanan   ...
67498320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
67508320bc6fSPatrick Sanan   if (set && compatible)  {
67518320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
67528320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
67538320bc6fSPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
67548320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
67558320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
67568320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
67578320bc6fSPatrick Sanan       }
67588320bc6fSPatrick Sanan     }
67598320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
67608320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
67618320bc6fSPatrick Sanan   } else {
67628320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
67638320bc6fSPatrick Sanan   }
67648320bc6fSPatrick Sanan   ...
67658320bc6fSPatrick Sanan .ve
67668320bc6fSPatrick Sanan 
67678320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
67688320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
67698320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
67708320bc6fSPatrick Sanan     always check the "set" output parameter.
67718320bc6fSPatrick Sanan 
67728320bc6fSPatrick Sanan     A DM is always compatible with itself.
67738320bc6fSPatrick Sanan 
67748320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
67758320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
67768320bc6fSPatrick Sanan     incompatible.
67778320bc6fSPatrick Sanan 
67788320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
67798320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
67808320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
67818320bc6fSPatrick Sanan 
67828320bc6fSPatrick Sanan     Developer Notes:
67838320bc6fSPatrick Sanan     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
67848320bc6fSPatrick Sanan     the DM B is compatible with DM A. Thus, this function checks the implementations
67858320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
67868320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
67878320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
67888320bc6fSPatrick Sanan     logic for this function, to check for existing logic in the implementation
67898320bc6fSPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found; the logic of this
67908320bc6fSPatrick Sanan     function will then call that logic.
67918320bc6fSPatrick Sanan 
67928320bc6fSPatrick Sanan     Level: advanced
67938320bc6fSPatrick Sanan 
67948320bc6fSPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA()
67958320bc6fSPatrick Sanan @*/
67968320bc6fSPatrick Sanan 
67978320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
67988320bc6fSPatrick Sanan {
67998320bc6fSPatrick Sanan   PetscErrorCode ierr;
68008320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
68018320bc6fSPatrick Sanan   DMType         type,type2;
68028320bc6fSPatrick Sanan   PetscBool      sameType;
68038320bc6fSPatrick Sanan 
68048320bc6fSPatrick Sanan   PetscFunctionBegin;
68058320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68068320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
68078320bc6fSPatrick Sanan 
68088320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
68098320bc6fSPatrick Sanan   if (dm == dm2) {
68108320bc6fSPatrick Sanan     *set = PETSC_TRUE;
68118320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
68128320bc6fSPatrick Sanan     PetscFunctionReturn(0);
68138320bc6fSPatrick Sanan   }
68148320bc6fSPatrick Sanan 
68158320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
68168320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
68178320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
68188320bc6fSPatrick Sanan      determined by the implementation-specific logic */
68198320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
68208320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
68218320bc6fSPatrick Sanan     *set = PETSC_TRUE;
68228320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
68238320bc6fSPatrick Sanan     PetscFunctionReturn(0);
68248320bc6fSPatrick Sanan   }
68258320bc6fSPatrick Sanan 
68268320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
68278320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
68288320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
68298320bc6fSPatrick Sanan     if (*set) {
68308320bc6fSPatrick Sanan       PetscFunctionReturn(0);
68318320bc6fSPatrick Sanan     }
68328320bc6fSPatrick Sanan   }
68338320bc6fSPatrick Sanan 
68348320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
68358320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
68368320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
68378320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
68388320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
68398320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
68408320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
68418320bc6fSPatrick Sanan   } else {
68428320bc6fSPatrick Sanan     *set = PETSC_FALSE;
68438320bc6fSPatrick Sanan   }
68448320bc6fSPatrick Sanan   PetscFunctionReturn(0);
68458320bc6fSPatrick Sanan }
6846