xref: /petsc/src/dm/interface/dm.c (revision 3a544194e8a43e6d3230d9fd481781affc315f41)
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) {
1494*3a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **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);
1499*3a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,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;
1503*3a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
150437d0c07bSMatthew G Knepley     }
150537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
150637d0c07bSMatthew G Knepley       PetscInt gdof;
150737d0c07bSMatthew G Knepley 
150837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
150937d0c07bSMatthew G Knepley       if (gdof > 0) {
151037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
1511*3a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
151237d0c07bSMatthew G Knepley 
151337d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
151437d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
1515*3a544194SStefano Zampini           fpdof = fdof-fcdof;
1516*3a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
1517*3a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
1518*3a544194SStefano Zampini             fieldNc[f] = 1;
1519*3a544194SStefano Zampini           }
1520*3a544194SStefano Zampini           fieldSizes[f] += fpdof;
152137d0c07bSMatthew G Knepley         }
152237d0c07bSMatthew G Knepley       }
152337d0c07bSMatthew G Knepley     }
152437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1525785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
152637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
152737d0c07bSMatthew G Knepley     }
152837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
152937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
153037d0c07bSMatthew G Knepley 
153137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
153237d0c07bSMatthew G Knepley       if (gdof > 0) {
153337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
153437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
153537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
153637d0c07bSMatthew G Knepley 
153737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
153837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
153937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
154037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
154137d0c07bSMatthew G Knepley           }
154237d0c07bSMatthew G Knepley         }
154337d0c07bSMatthew G Knepley       }
154437d0c07bSMatthew G Knepley     }
15458865f1eaSKarl Rupp     if (numFields) *numFields = nF;
154637d0c07bSMatthew G Knepley     if (fieldNames) {
1547785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
154837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
154937d0c07bSMatthew G Knepley         const char *fieldName;
155037d0c07bSMatthew G Knepley 
155137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
155237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
155337d0c07bSMatthew G Knepley       }
155437d0c07bSMatthew G Knepley     }
155537d0c07bSMatthew G Knepley     if (fields) {
1556785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
155737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
1558*3a544194SStefano Zampini         PetscInt bs, in[2], out[2];
1559*3a544194SStefano Zampini 
156082f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
1561*3a544194SStefano Zampini         in[0] = -fieldNc[f];
1562*3a544194SStefano Zampini         in[1] = fieldNc[f];
1563*3a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1564*3a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
1565*3a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
156637d0c07bSMatthew G Knepley       }
156737d0c07bSMatthew G Knepley     }
1568*3a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15698865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15708865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
157169ca1f37SDmitry Karpeev   }
15724d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15734d343eeaSMatthew G Knepley }
15744d343eeaSMatthew G Knepley 
157516621825SDmitry Karpeev 
157616621825SDmitry Karpeev /*@C
157716621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
157816621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
157916621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1580e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1581e7c4fc90SDmitry Karpeev 
1582e7c4fc90SDmitry Karpeev   Not collective
1583e7c4fc90SDmitry Karpeev 
1584e7c4fc90SDmitry Karpeev   Input Parameter:
1585e7c4fc90SDmitry Karpeev . dm - the DM object
1586e7c4fc90SDmitry Karpeev 
1587e7c4fc90SDmitry Karpeev   Output Parameters:
15880298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15890298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15900298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15910298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1592e7c4fc90SDmitry Karpeev 
1593e7c4fc90SDmitry Karpeev   Level: intermediate
1594e7c4fc90SDmitry Karpeev 
1595e7c4fc90SDmitry Karpeev   Notes:
1596e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1597e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1598e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1599e7c4fc90SDmitry Karpeev 
1600e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1601e7c4fc90SDmitry Karpeev @*/
160216621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1603e7c4fc90SDmitry Karpeev {
1604e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1605e7c4fc90SDmitry Karpeev 
1606e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1607e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16088865f1eaSKarl Rupp   if (len) {
16098865f1eaSKarl Rupp     PetscValidPointer(len,2);
16108865f1eaSKarl Rupp     *len = 0;
16118865f1eaSKarl Rupp   }
16128865f1eaSKarl Rupp   if (namelist) {
16138865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16148865f1eaSKarl Rupp     *namelist = 0;
16158865f1eaSKarl Rupp   }
16168865f1eaSKarl Rupp   if (islist) {
16178865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16188865f1eaSKarl Rupp     *islist = 0;
16198865f1eaSKarl Rupp   }
16208865f1eaSKarl Rupp   if (dmlist) {
16218865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16228865f1eaSKarl Rupp     *dmlist = 0;
16238865f1eaSKarl Rupp   }
1624f3f0edfdSDmitry Karpeev   /*
1625f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1626f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1627f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1628f3f0edfdSDmitry Karpeev    */
1629ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
163016621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1631435a35e8SMatthew G Knepley     PetscSection section;
1632435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1633435a35e8SMatthew G Knepley 
1634e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1635435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1636435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1637f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
163803dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
163903dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
164003dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1641435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1642435a35e8SMatthew G Knepley         const char *fieldName;
1643435a35e8SMatthew G Knepley 
164403dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
164503dc3394SMatthew G. Knepley         if (namelist) {
1646435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1647435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1648435a35e8SMatthew G Knepley         }
164903dc3394SMatthew G. Knepley       }
1650435a35e8SMatthew G Knepley     } else {
165169ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1652e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16530298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1654e7c4fc90SDmitry Karpeev     }
16558865f1eaSKarl Rupp   } else {
165616621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
165716621825SDmitry Karpeev   }
165816621825SDmitry Karpeev   PetscFunctionReturn(0);
165916621825SDmitry Karpeev }
166016621825SDmitry Karpeev 
1661564cec59SMatthew G. Knepley /*@
1662435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1663435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1664435a35e8SMatthew G Knepley 
1665435a35e8SMatthew G Knepley   Not collective
1666435a35e8SMatthew G Knepley 
1667435a35e8SMatthew G Knepley   Input Parameters:
16682adcc780SMatthew G. Knepley + dm        - The DM object
16692adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16702adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1671435a35e8SMatthew G Knepley 
1672435a35e8SMatthew G Knepley   Output Parameters:
16732adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16742adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1675435a35e8SMatthew G Knepley 
1676435a35e8SMatthew G Knepley   Level: intermediate
1677435a35e8SMatthew G Knepley 
1678435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1679435a35e8SMatthew G Knepley @*/
168037bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1681435a35e8SMatthew G Knepley {
1682435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1683435a35e8SMatthew G Knepley 
1684435a35e8SMatthew G Knepley   PetscFunctionBegin;
1685435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1686435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16878865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16888865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1689435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1690435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
169182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1692435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1693435a35e8SMatthew G Knepley }
1694435a35e8SMatthew G Knepley 
16952adcc780SMatthew G. Knepley /*@C
16962adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
16972adcc780SMatthew G. Knepley 
16982adcc780SMatthew G. Knepley   Not collective
16992adcc780SMatthew G. Knepley 
17002adcc780SMatthew G. Knepley   Input Parameter:
17012adcc780SMatthew G. Knepley + dms - The DM objects
17022adcc780SMatthew G. Knepley - len - The number of DMs
17032adcc780SMatthew G. Knepley 
17042adcc780SMatthew G. Knepley   Output Parameters:
1705a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17062adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17072adcc780SMatthew G. Knepley 
17082adcc780SMatthew G. Knepley   Level: intermediate
17092adcc780SMatthew G. Knepley 
17102adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17112adcc780SMatthew G. Knepley @*/
17122adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17132adcc780SMatthew G. Knepley {
17142adcc780SMatthew G. Knepley   PetscInt       i;
17152adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17162adcc780SMatthew G. Knepley 
17172adcc780SMatthew G. Knepley   PetscFunctionBegin;
17182adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17192adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17202adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1721a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17222adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17232adcc780SMatthew G. Knepley   if (len) {
1724a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1725a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17262adcc780SMatthew G. Knepley   }
17272adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17282adcc780SMatthew G. Knepley }
17292adcc780SMatthew G. Knepley 
173016621825SDmitry Karpeev 
173116621825SDmitry Karpeev /*@C
17328d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17338d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17348d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17358d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17368d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
173716621825SDmitry Karpeev 
173816621825SDmitry Karpeev   Not collective
173916621825SDmitry Karpeev 
174016621825SDmitry Karpeev   Input Parameter:
174116621825SDmitry Karpeev . dm - the DM object
174216621825SDmitry Karpeev 
174316621825SDmitry Karpeev   Output Parameters:
17440298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17450298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17460298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17470298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17480298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
174916621825SDmitry Karpeev 
175016621825SDmitry Karpeev   Level: intermediate
175116621825SDmitry Karpeev 
175216621825SDmitry Karpeev   Notes:
175316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
175416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
175516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
175616621825SDmitry Karpeev 
17578d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
175816621825SDmitry Karpeev @*/
17598d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
176016621825SDmitry Karpeev {
176116621825SDmitry Karpeev   PetscErrorCode      ierr;
1762be081cd6SPeter Brune   DMSubDomainHookLink link;
1763be081cd6SPeter Brune   PetscInt            i,l;
176416621825SDmitry Karpeev 
176516621825SDmitry Karpeev   PetscFunctionBegin;
176616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
176714a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17680298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17690298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17700298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17710298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1772f3f0edfdSDmitry Karpeev   /*
1773f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1774f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1775f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1776f3f0edfdSDmitry Karpeev    */
1777ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
177816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1779be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
178014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1781f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1782be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1783be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1784be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1785be081cd6SPeter Brune         }
1786648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1787e7c4fc90SDmitry Karpeev       }
178814a18fd3SPeter Brune     }
178914a18fd3SPeter Brune     if (len) *len = l;
179014a18fd3SPeter Brune   }
1791e30e807fSPeter Brune   PetscFunctionReturn(0);
1792e30e807fSPeter Brune }
1793e30e807fSPeter Brune 
1794e30e807fSPeter Brune 
1795e30e807fSPeter Brune /*@C
1796e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1797e30e807fSPeter Brune 
1798e30e807fSPeter Brune   Not collective
1799e30e807fSPeter Brune 
1800e30e807fSPeter Brune   Input Parameters:
1801e30e807fSPeter Brune + dm - the DM object
1802e30e807fSPeter Brune . n  - the number of subdomain scatters
1803e30e807fSPeter Brune - subdms - the local subdomains
1804e30e807fSPeter Brune 
1805e30e807fSPeter Brune   Output Parameters:
1806e30e807fSPeter Brune + n     - the number of scatters returned
1807e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1808e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1809e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1810e30e807fSPeter Brune 
181195452b02SPatrick Sanan   Notes:
181295452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1813e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1814e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1815e30e807fSPeter Brune   solution and residual data.
1816e30e807fSPeter Brune 
1817e30e807fSPeter Brune   Level: developer
1818e30e807fSPeter Brune 
1819e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1820e30e807fSPeter Brune @*/
1821e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1822e30e807fSPeter Brune {
1823e30e807fSPeter Brune   PetscErrorCode ierr;
1824e30e807fSPeter Brune 
1825e30e807fSPeter Brune   PetscFunctionBegin;
1826e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1827e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1828e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1829e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18306668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1831e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1832e7c4fc90SDmitry Karpeev }
1833e7c4fc90SDmitry Karpeev 
183447c6ae99SBarry Smith /*@
183547c6ae99SBarry Smith   DMRefine - Refines a DM object
183647c6ae99SBarry Smith 
183747c6ae99SBarry Smith   Collective on DM
183847c6ae99SBarry Smith 
183947c6ae99SBarry Smith   Input Parameter:
184047c6ae99SBarry Smith + dm   - the DM object
184191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
184247c6ae99SBarry Smith 
184347c6ae99SBarry Smith   Output Parameter:
18440298fd71SBarry Smith . dmf - the refined DM, or NULL
1845ae0a1c52SMatthew G Knepley 
18460298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
184747c6ae99SBarry Smith 
184847c6ae99SBarry Smith   Level: developer
184947c6ae99SBarry Smith 
1850e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
185147c6ae99SBarry Smith @*/
18527087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
185347c6ae99SBarry Smith {
185447c6ae99SBarry Smith   PetscErrorCode   ierr;
1855c833c3b5SJed Brown   DMRefineHookLink link;
185647c6ae99SBarry Smith 
185747c6ae99SBarry Smith   PetscFunctionBegin;
1858732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18591ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1860fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
186147c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18624057135bSMatthew G Knepley   if (*dmf) {
186343842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18648865f1eaSKarl Rupp 
18658cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18668865f1eaSKarl Rupp 
1867644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18680598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1869656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18708865f1eaSKarl Rupp 
1871e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1872c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18738865f1eaSKarl Rupp       if (link->refinehook) {
18748865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18758865f1eaSKarl Rupp       }
1876c833c3b5SJed Brown     }
1877c833c3b5SJed Brown   }
18781ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1879c833c3b5SJed Brown   PetscFunctionReturn(0);
1880c833c3b5SJed Brown }
1881c833c3b5SJed Brown 
1882bb9467b5SJed Brown /*@C
1883c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1884c833c3b5SJed Brown 
1885c833c3b5SJed Brown    Logically Collective
1886c833c3b5SJed Brown 
1887c833c3b5SJed Brown    Input Arguments:
1888c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1889c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1890c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18910298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1892c833c3b5SJed Brown 
1893c833c3b5SJed Brown    Calling sequence of refinehook:
1894c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1895c833c3b5SJed Brown 
1896c833c3b5SJed Brown +  coarse - coarse level DM
1897c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1898c833c3b5SJed Brown -  ctx - optional user-defined function context
1899c833c3b5SJed Brown 
1900c833c3b5SJed Brown    Calling sequence for interphook:
1901c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1902c833c3b5SJed Brown 
1903c833c3b5SJed Brown +  coarse - coarse level DM
1904c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1905c833c3b5SJed Brown .  fine - fine level DM to update
1906c833c3b5SJed Brown -  ctx - optional user-defined function context
1907c833c3b5SJed Brown 
1908c833c3b5SJed Brown    Level: advanced
1909c833c3b5SJed Brown 
1910c833c3b5SJed Brown    Notes:
1911c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1912c833c3b5SJed Brown 
1913c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1914c833c3b5SJed Brown 
1915bb9467b5SJed Brown    This function is currently not available from Fortran.
1916bb9467b5SJed Brown 
1917c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1918c833c3b5SJed Brown @*/
1919c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1920c833c3b5SJed Brown {
1921c833c3b5SJed Brown   PetscErrorCode   ierr;
1922c833c3b5SJed Brown   DMRefineHookLink link,*p;
1923c833c3b5SJed Brown 
1924c833c3b5SJed Brown   PetscFunctionBegin;
1925c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19263d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19273d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19283d8e3701SJed Brown   }
192995dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1930c833c3b5SJed Brown   link->refinehook = refinehook;
1931c833c3b5SJed Brown   link->interphook = interphook;
1932c833c3b5SJed Brown   link->ctx        = ctx;
19330298fd71SBarry Smith   link->next       = NULL;
1934c833c3b5SJed Brown   *p               = link;
1935c833c3b5SJed Brown   PetscFunctionReturn(0);
1936c833c3b5SJed Brown }
1937c833c3b5SJed Brown 
19383d8e3701SJed Brown /*@C
19393d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19403d8e3701SJed Brown 
19413d8e3701SJed Brown    Logically Collective
19423d8e3701SJed Brown 
19433d8e3701SJed Brown    Input Arguments:
19443d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19453d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19463d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19473d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19483d8e3701SJed Brown 
19493d8e3701SJed Brown    Level: advanced
19503d8e3701SJed Brown 
19513d8e3701SJed Brown    Notes:
19523d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19533d8e3701SJed Brown 
19543d8e3701SJed Brown    This function is currently not available from Fortran.
19553d8e3701SJed Brown 
19563d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19573d8e3701SJed Brown @*/
19583d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19593d8e3701SJed Brown {
19603d8e3701SJed Brown   PetscErrorCode   ierr;
19613d8e3701SJed Brown   DMRefineHookLink link,*p;
19623d8e3701SJed Brown 
19633d8e3701SJed Brown   PetscFunctionBegin;
19643d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19653d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19663d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19673d8e3701SJed Brown       link = *p;
19683d8e3701SJed Brown       *p = link->next;
19693d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19703d8e3701SJed Brown       break;
19713d8e3701SJed Brown     }
19723d8e3701SJed Brown   }
19733d8e3701SJed Brown   PetscFunctionReturn(0);
19743d8e3701SJed Brown }
19753d8e3701SJed Brown 
1976c833c3b5SJed Brown /*@
1977c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1978c833c3b5SJed Brown 
1979c833c3b5SJed Brown    Collective if any hooks are
1980c833c3b5SJed Brown 
1981c833c3b5SJed Brown    Input Arguments:
1982c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1983e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1984c833c3b5SJed Brown -  fine - finer DM to update
1985c833c3b5SJed Brown 
1986c833c3b5SJed Brown    Level: developer
1987c833c3b5SJed Brown 
1988c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1989c833c3b5SJed Brown @*/
1990c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1991c833c3b5SJed Brown {
1992c833c3b5SJed Brown   PetscErrorCode   ierr;
1993c833c3b5SJed Brown   DMRefineHookLink link;
1994c833c3b5SJed Brown 
1995c833c3b5SJed Brown   PetscFunctionBegin;
1996c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
19978865f1eaSKarl Rupp     if (link->interphook) {
19988865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
19998865f1eaSKarl Rupp     }
20004057135bSMatthew G Knepley   }
200147c6ae99SBarry Smith   PetscFunctionReturn(0);
200247c6ae99SBarry Smith }
200347c6ae99SBarry Smith 
2004eb3f98d2SBarry Smith /*@
2005eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2006eb3f98d2SBarry Smith 
2007eb3f98d2SBarry Smith     Not Collective
2008eb3f98d2SBarry Smith 
2009eb3f98d2SBarry Smith     Input Parameter:
2010eb3f98d2SBarry Smith .   dm - the DM object
2011eb3f98d2SBarry Smith 
2012eb3f98d2SBarry Smith     Output Parameter:
2013eb3f98d2SBarry Smith .   level - number of refinements
2014eb3f98d2SBarry Smith 
2015eb3f98d2SBarry Smith     Level: developer
2016eb3f98d2SBarry Smith 
20176a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2018eb3f98d2SBarry Smith 
2019eb3f98d2SBarry Smith @*/
2020eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2021eb3f98d2SBarry Smith {
2022eb3f98d2SBarry Smith   PetscFunctionBegin;
2023eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2024eb3f98d2SBarry Smith   *level = dm->levelup;
2025eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2026eb3f98d2SBarry Smith }
2027eb3f98d2SBarry Smith 
2028fef3a512SBarry Smith /*@
2029fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2030fef3a512SBarry Smith 
2031fef3a512SBarry Smith     Not Collective
2032fef3a512SBarry Smith 
2033fef3a512SBarry Smith     Input Parameter:
2034fef3a512SBarry Smith +   dm - the DM object
2035fef3a512SBarry Smith -   level - number of refinements
2036fef3a512SBarry Smith 
2037fef3a512SBarry Smith     Level: advanced
2038fef3a512SBarry Smith 
203995452b02SPatrick Sanan     Notes:
204095452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2041fef3a512SBarry Smith 
2042fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2043fef3a512SBarry Smith 
2044fef3a512SBarry Smith @*/
2045fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2046fef3a512SBarry Smith {
2047fef3a512SBarry Smith   PetscFunctionBegin;
2048fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2049fef3a512SBarry Smith   dm->levelup = level;
2050fef3a512SBarry Smith   PetscFunctionReturn(0);
2051fef3a512SBarry Smith }
2052fef3a512SBarry Smith 
2053bb9467b5SJed Brown /*@C
2054baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2055baf369e7SPeter Brune 
2056baf369e7SPeter Brune    Logically Collective
2057baf369e7SPeter Brune 
2058baf369e7SPeter Brune    Input Arguments:
2059baf369e7SPeter Brune +  dm - the DM
2060baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2061baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20620298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2063baf369e7SPeter Brune 
2064baf369e7SPeter Brune    Calling sequence for beginhook:
2065baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2066baf369e7SPeter Brune 
2067baf369e7SPeter Brune +  dm - global DM
2068baf369e7SPeter Brune .  g - global vector
2069baf369e7SPeter Brune .  mode - mode
2070baf369e7SPeter Brune .  l - local vector
2071baf369e7SPeter Brune -  ctx - optional user-defined function context
2072baf369e7SPeter Brune 
2073baf369e7SPeter Brune 
2074baf369e7SPeter Brune    Calling sequence for endhook:
2075ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2076baf369e7SPeter Brune 
2077baf369e7SPeter Brune +  global - global DM
2078baf369e7SPeter Brune -  ctx - optional user-defined function context
2079baf369e7SPeter Brune 
2080baf369e7SPeter Brune    Level: advanced
2081baf369e7SPeter Brune 
2082baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2083baf369e7SPeter Brune @*/
2084baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2085baf369e7SPeter Brune {
2086baf369e7SPeter Brune   PetscErrorCode          ierr;
2087baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2088baf369e7SPeter Brune 
2089baf369e7SPeter Brune   PetscFunctionBegin;
2090baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2091baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
209295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2093baf369e7SPeter Brune   link->beginhook = beginhook;
2094baf369e7SPeter Brune   link->endhook   = endhook;
2095baf369e7SPeter Brune   link->ctx       = ctx;
20960298fd71SBarry Smith   link->next      = NULL;
2097baf369e7SPeter Brune   *p              = link;
2098baf369e7SPeter Brune   PetscFunctionReturn(0);
2099baf369e7SPeter Brune }
2100baf369e7SPeter Brune 
21014c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
21024c274da1SToby Isaac {
21034c274da1SToby Isaac   Mat cMat;
21044c274da1SToby Isaac   Vec cVec;
21054c274da1SToby Isaac   PetscSection section, cSec;
21064c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21074c274da1SToby Isaac   PetscErrorCode ierr;
21084c274da1SToby Isaac 
21094c274da1SToby Isaac   PetscFunctionBegin;
21104c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21114c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21124c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
21135db9a05bSToby Isaac     PetscInt nRows;
21145db9a05bSToby Isaac 
21155db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21165db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2117e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
21187711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21194c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
21204c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21214c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21224c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21234c274da1SToby Isaac       if (dof) {
21244c274da1SToby Isaac         PetscScalar *vals;
21254c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
21264c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
21274c274da1SToby Isaac       }
21284c274da1SToby Isaac     }
21294c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21304c274da1SToby Isaac   }
21314c274da1SToby Isaac   PetscFunctionReturn(0);
21324c274da1SToby Isaac }
21334c274da1SToby Isaac 
213447c6ae99SBarry Smith /*@
213547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith     Neighbor-wise Collective on DM
213847c6ae99SBarry Smith 
213947c6ae99SBarry Smith     Input Parameters:
214047c6ae99SBarry Smith +   dm - the DM object
214147c6ae99SBarry Smith .   g - the global vector
214247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
214347c6ae99SBarry Smith -   l - the local vector
214447c6ae99SBarry Smith 
214547c6ae99SBarry Smith 
214647c6ae99SBarry Smith     Level: beginner
214747c6ae99SBarry Smith 
2148e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
214947c6ae99SBarry Smith 
215047c6ae99SBarry Smith @*/
21517087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
215247c6ae99SBarry Smith {
21537128ae9fSMatthew G Knepley   PetscSF                 sf;
215447c6ae99SBarry Smith   PetscErrorCode          ierr;
2155baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
215647c6ae99SBarry Smith 
215747c6ae99SBarry Smith   PetscFunctionBegin;
2158171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2159baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
21608865f1eaSKarl Rupp     if (link->beginhook) {
21618865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
21628865f1eaSKarl Rupp     }
2163baf369e7SPeter Brune   }
21647128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21657128ae9fSMatthew G Knepley   if (sf) {
2166ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2167ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
21687128ae9fSMatthew G Knepley 
216982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21707128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2171ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
21727128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
21737128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2174ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
21757128ae9fSMatthew G Knepley   } else {
2176843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
21777128ae9fSMatthew G Knepley   }
217847c6ae99SBarry Smith   PetscFunctionReturn(0);
217947c6ae99SBarry Smith }
218047c6ae99SBarry Smith 
218147c6ae99SBarry Smith /*@
218247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
218347c6ae99SBarry Smith 
218447c6ae99SBarry Smith     Neighbor-wise Collective on DM
218547c6ae99SBarry Smith 
218647c6ae99SBarry Smith     Input Parameters:
218747c6ae99SBarry Smith +   dm - the DM object
218847c6ae99SBarry Smith .   g - the global vector
218947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
219047c6ae99SBarry Smith -   l - the local vector
219147c6ae99SBarry Smith 
219247c6ae99SBarry Smith 
219347c6ae99SBarry Smith     Level: beginner
219447c6ae99SBarry Smith 
2195e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
219647c6ae99SBarry Smith 
219747c6ae99SBarry Smith @*/
21987087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
219947c6ae99SBarry Smith {
22007128ae9fSMatthew G Knepley   PetscSF                 sf;
220147c6ae99SBarry Smith   PetscErrorCode          ierr;
2202ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2203ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2204baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
220547c6ae99SBarry Smith 
220647c6ae99SBarry Smith   PetscFunctionBegin;
2207171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22087128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22097128ae9fSMatthew G Knepley   if (sf) {
221082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22117128ae9fSMatthew G Knepley 
22127128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2213ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22147128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22157128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2216ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22177128ae9fSMatthew G Knepley   } else {
2218843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22197128ae9fSMatthew G Knepley   }
22204c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2221baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2222baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2223baf369e7SPeter Brune   }
222447c6ae99SBarry Smith   PetscFunctionReturn(0);
222547c6ae99SBarry Smith }
222647c6ae99SBarry Smith 
2227d4d07f1eSToby Isaac /*@C
2228d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2229d4d07f1eSToby Isaac 
2230d4d07f1eSToby Isaac    Logically Collective
2231d4d07f1eSToby Isaac 
2232d4d07f1eSToby Isaac    Input Arguments:
2233d4d07f1eSToby Isaac +  dm - the DM
2234d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2235d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2236d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2237d4d07f1eSToby Isaac 
2238d4d07f1eSToby Isaac    Calling sequence for beginhook:
2239d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2240d4d07f1eSToby Isaac 
2241d4d07f1eSToby Isaac +  dm - global DM
2242d4d07f1eSToby Isaac .  l - local vector
2243d4d07f1eSToby Isaac .  mode - mode
2244d4d07f1eSToby Isaac .  g - global vector
2245d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2246d4d07f1eSToby Isaac 
2247d4d07f1eSToby Isaac 
2248d4d07f1eSToby Isaac    Calling sequence for endhook:
2249d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2250d4d07f1eSToby Isaac 
2251d4d07f1eSToby Isaac +  global - global DM
2252d4d07f1eSToby Isaac .  l - local vector
2253d4d07f1eSToby Isaac .  mode - mode
2254d4d07f1eSToby Isaac .  g - global vector
2255d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2256d4d07f1eSToby Isaac 
2257d4d07f1eSToby Isaac    Level: advanced
2258d4d07f1eSToby Isaac 
2259d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2260d4d07f1eSToby Isaac @*/
2261d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2262d4d07f1eSToby Isaac {
2263d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2264d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2265d4d07f1eSToby Isaac 
2266d4d07f1eSToby Isaac   PetscFunctionBegin;
2267d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2268d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
226995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2270d4d07f1eSToby Isaac   link->beginhook = beginhook;
2271d4d07f1eSToby Isaac   link->endhook   = endhook;
2272d4d07f1eSToby Isaac   link->ctx       = ctx;
2273d4d07f1eSToby Isaac   link->next      = NULL;
2274d4d07f1eSToby Isaac   *p              = link;
2275d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2276d4d07f1eSToby Isaac }
2277d4d07f1eSToby Isaac 
22784c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
22794c274da1SToby Isaac {
22804c274da1SToby Isaac   Mat cMat;
22814c274da1SToby Isaac   Vec cVec;
22824c274da1SToby Isaac   PetscSection section, cSec;
22834c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22844c274da1SToby Isaac   PetscErrorCode ierr;
22854c274da1SToby Isaac 
22864c274da1SToby Isaac   PetscFunctionBegin;
22874c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22884c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22894c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
22905db9a05bSToby Isaac     PetscInt nRows;
22915db9a05bSToby Isaac 
22925db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22935db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2294e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
22957711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22964c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22974c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22984c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22994c274da1SToby Isaac       if (dof) {
23004c274da1SToby Isaac         PetscInt d;
23014c274da1SToby Isaac         PetscScalar *vals;
23024c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
23034c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
23044c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
23054c274da1SToby Isaac          * we just extracted */
23064c274da1SToby Isaac         for (d = 0; d < dof; d++) {
23074c274da1SToby Isaac           vals[d] = 0.;
23084c274da1SToby Isaac         }
23094c274da1SToby Isaac       }
23104c274da1SToby Isaac     }
23114c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
23124c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23134c274da1SToby Isaac   }
23144c274da1SToby Isaac   PetscFunctionReturn(0);
23154c274da1SToby Isaac }
23164c274da1SToby Isaac 
231747c6ae99SBarry Smith /*@
23189a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
23199a42bb27SBarry Smith 
23209a42bb27SBarry Smith     Neighbor-wise Collective on DM
23219a42bb27SBarry Smith 
23229a42bb27SBarry Smith     Input Parameters:
23239a42bb27SBarry Smith +   dm - the DM object
2324f6813fd5SJed Brown .   l - the local vector
23251eb28f2eSBarry 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.
23261eb28f2eSBarry Smith -   g - the global vector
23279a42bb27SBarry Smith 
232895452b02SPatrick Sanan     Notes:
232995452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
233084330215SMatthew 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.
23319a42bb27SBarry Smith 
23329a42bb27SBarry Smith     Level: beginner
23339a42bb27SBarry Smith 
2334e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
23359a42bb27SBarry Smith 
23369a42bb27SBarry Smith @*/
23377087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
23389a42bb27SBarry Smith {
23397128ae9fSMatthew G Knepley   PetscSF                 sf;
234084330215SMatthew G. Knepley   PetscSection            s, gs;
2341d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2342ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2343ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
234484330215SMatthew G. Knepley   PetscBool               isInsert;
234584330215SMatthew G. Knepley   PetscErrorCode          ierr;
23469a42bb27SBarry Smith 
23479a42bb27SBarry Smith   PetscFunctionBegin;
2348171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2349d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2350d4d07f1eSToby Isaac     if (link->beginhook) {
2351d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2352d4d07f1eSToby Isaac     }
2353d4d07f1eSToby Isaac   }
23544c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
23557128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2356e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
23577128ae9fSMatthew G Knepley   switch (mode) {
23587128ae9fSMatthew G Knepley   case INSERT_VALUES:
23597128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2360304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
236184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23627128ae9fSMatthew G Knepley   case ADD_VALUES:
23637128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2364304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
236584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23667128ae9fSMatthew G Knepley   default:
236782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23687128ae9fSMatthew G Knepley   }
236984330215SMatthew G. Knepley   if (sf && !isInsert) {
2370ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23717128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2372a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2373ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
237484330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
237584330215SMatthew G. Knepley   } else if (s && isInsert) {
237684330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
237784330215SMatthew G. Knepley 
2378e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
237984330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
238084330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2381ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
238284330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
238384330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2384b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
238584330215SMatthew G. Knepley 
238684330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
238703442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
238884330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2389b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
239084330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
239184330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2392b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
239303442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2394b3b16f48SMatthew 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);
2395b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2396b3b16f48SMatthew G. Knepley       if (!gcdof) {
239784330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2398b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2399b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
240084330215SMatthew G. Knepley         const PetscInt *cdofs;
240184330215SMatthew G. Knepley         PetscInt        cind = 0;
240284330215SMatthew G. Knepley 
240384330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2404b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
240584330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2406b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
240784330215SMatthew G. Knepley         }
2408b3b16f48SMatthew 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);
240984330215SMatthew G. Knepley     }
2410ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24117128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
24127128ae9fSMatthew G Knepley   } else {
2413843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24147128ae9fSMatthew G Knepley   }
24159a42bb27SBarry Smith   PetscFunctionReturn(0);
24169a42bb27SBarry Smith }
24179a42bb27SBarry Smith 
24189a42bb27SBarry Smith /*@
24199a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
242047c6ae99SBarry Smith 
242147c6ae99SBarry Smith     Neighbor-wise Collective on DM
242247c6ae99SBarry Smith 
242347c6ae99SBarry Smith     Input Parameters:
242447c6ae99SBarry Smith +   dm - the DM object
2425f6813fd5SJed Brown .   l - the local vector
242647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2427f6813fd5SJed Brown -   g - the global vector
242847c6ae99SBarry Smith 
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith     Level: beginner
243147c6ae99SBarry Smith 
2432e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
243347c6ae99SBarry Smith 
243447c6ae99SBarry Smith @*/
24357087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
243647c6ae99SBarry Smith {
24377128ae9fSMatthew G Knepley   PetscSF                 sf;
243884330215SMatthew G. Knepley   PetscSection            s;
2439d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
244084330215SMatthew G. Knepley   PetscBool               isInsert;
244184330215SMatthew G. Knepley   PetscErrorCode          ierr;
244247c6ae99SBarry Smith 
244347c6ae99SBarry Smith   PetscFunctionBegin;
2444171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24457128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2446e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24477128ae9fSMatthew G Knepley   switch (mode) {
24487128ae9fSMatthew G Knepley   case INSERT_VALUES:
24497128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
245084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24517128ae9fSMatthew G Knepley   case ADD_VALUES:
24527128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
245384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
24547128ae9fSMatthew G Knepley   default:
245582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24567128ae9fSMatthew G Knepley   }
245784330215SMatthew G. Knepley   if (sf && !isInsert) {
2458ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2459ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
246084330215SMatthew G. Knepley 
2461ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24627128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2463a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2464ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24657128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
246684330215SMatthew G. Knepley   } else if (s && isInsert) {
24677128ae9fSMatthew G Knepley   } else {
2468843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24697128ae9fSMatthew G Knepley   }
2470d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2471d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2472d4d07f1eSToby Isaac   }
247347c6ae99SBarry Smith   PetscFunctionReturn(0);
247447c6ae99SBarry Smith }
247547c6ae99SBarry Smith 
2476f089877aSRichard Tran Mills /*@
2477bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2478bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2479d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2480f089877aSRichard Tran Mills 
2481bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2482f089877aSRichard Tran Mills 
2483f089877aSRichard Tran Mills    Input Parameters:
2484f089877aSRichard Tran Mills +  dm - the DM object
2485bc0a1609SRichard Tran Mills .  g - the original local vector
2486bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2487f089877aSRichard Tran Mills 
2488bc0a1609SRichard Tran Mills    Output Parameter:
2489bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2490f089877aSRichard Tran Mills 
2491f089877aSRichard Tran Mills    Level: intermediate
2492f089877aSRichard Tran Mills 
2493bc0a1609SRichard Tran Mills    Notes:
2494bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2495bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2496bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2497bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2498bc0a1609SRichard Tran Mills 
2499bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2500bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2501f089877aSRichard Tran Mills 
2502f089877aSRichard Tran Mills @*/
2503f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2504f089877aSRichard Tran Mills {
2505f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2506f089877aSRichard Tran Mills 
2507f089877aSRichard Tran Mills   PetscFunctionBegin;
2508f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2509bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2510f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2511f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2512f089877aSRichard Tran Mills }
2513f089877aSRichard Tran Mills 
2514f089877aSRichard Tran Mills /*@
2515bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2516bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2517d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2518f089877aSRichard Tran Mills 
2519bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2520f089877aSRichard Tran Mills 
2521f089877aSRichard Tran Mills    Input Parameters:
2522bc0a1609SRichard Tran Mills +  da - the DM object
2523bc0a1609SRichard Tran Mills .  g - the original local vector
2524bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2525f089877aSRichard Tran Mills 
2526bc0a1609SRichard Tran Mills    Output Parameter:
2527bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2528f089877aSRichard Tran Mills 
2529f089877aSRichard Tran Mills    Level: intermediate
2530f089877aSRichard Tran Mills 
2531bc0a1609SRichard Tran Mills    Notes:
2532bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2533bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2534bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2535bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2536bc0a1609SRichard Tran Mills 
2537bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2538bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2539f089877aSRichard Tran Mills 
2540f089877aSRichard Tran Mills @*/
2541f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2542f089877aSRichard Tran Mills {
2543f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2544f089877aSRichard Tran Mills 
2545f089877aSRichard Tran Mills   PetscFunctionBegin;
2546f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2547bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2548f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2549f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2550f089877aSRichard Tran Mills }
2551f089877aSRichard Tran Mills 
2552f089877aSRichard Tran Mills 
255347c6ae99SBarry Smith /*@
255447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
255547c6ae99SBarry Smith 
255647c6ae99SBarry Smith     Collective on DM
255747c6ae99SBarry Smith 
255847c6ae99SBarry Smith     Input Parameter:
255947c6ae99SBarry Smith +   dm - the DM object
256091d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
256147c6ae99SBarry Smith 
256247c6ae99SBarry Smith     Output Parameter:
256347c6ae99SBarry Smith .   dmc - the coarsened DM
256447c6ae99SBarry Smith 
256547c6ae99SBarry Smith     Level: developer
256647c6ae99SBarry Smith 
2567e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
256847c6ae99SBarry Smith 
256947c6ae99SBarry Smith @*/
25707087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
257147c6ae99SBarry Smith {
257247c6ae99SBarry Smith   PetscErrorCode    ierr;
2573b17ce1afSJed Brown   DMCoarsenHookLink link;
257447c6ae99SBarry Smith 
257547c6ae99SBarry Smith   PetscFunctionBegin;
2576171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2577fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
257847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
257947c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
25808892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2581a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
258243842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
25838cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2584644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
25850598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2586656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2587e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2588b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2589b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2590b17ce1afSJed Brown   }
259147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2592b17ce1afSJed Brown   PetscFunctionReturn(0);
2593b17ce1afSJed Brown }
2594b17ce1afSJed Brown 
2595bb9467b5SJed Brown /*@C
2596b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2597b17ce1afSJed Brown 
2598b17ce1afSJed Brown    Logically Collective
2599b17ce1afSJed Brown 
2600b17ce1afSJed Brown    Input Arguments:
2601b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2602b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2603b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
26040298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2605b17ce1afSJed Brown 
2606b17ce1afSJed Brown    Calling sequence of coarsenhook:
2607b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2608b17ce1afSJed Brown 
2609b17ce1afSJed Brown +  fine - fine level DM
2610b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2611b17ce1afSJed Brown -  ctx - optional user-defined function context
2612b17ce1afSJed Brown 
2613b17ce1afSJed Brown    Calling sequence for restricthook:
2614c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2615b17ce1afSJed Brown 
2616b17ce1afSJed Brown +  fine - fine level DM
2617b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2618c833c3b5SJed Brown .  rscale - scaling vector for restriction
2619c833c3b5SJed Brown .  inject - matrix restricting by injection
2620b17ce1afSJed Brown .  coarse - coarse level DM to update
2621b17ce1afSJed Brown -  ctx - optional user-defined function context
2622b17ce1afSJed Brown 
2623b17ce1afSJed Brown    Level: advanced
2624b17ce1afSJed Brown 
2625b17ce1afSJed Brown    Notes:
2626b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2627b17ce1afSJed Brown 
2628b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2629b17ce1afSJed Brown 
2630b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2631b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2632b17ce1afSJed Brown 
2633bb9467b5SJed Brown    This function is currently not available from Fortran.
2634bb9467b5SJed Brown 
2635dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2636b17ce1afSJed Brown @*/
2637b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2638b17ce1afSJed Brown {
2639b17ce1afSJed Brown   PetscErrorCode    ierr;
2640b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2641b17ce1afSJed Brown 
2642b17ce1afSJed Brown   PetscFunctionBegin;
2643b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
26441e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
26451e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
26461e3d8eccSJed Brown   }
264795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2648b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2649b17ce1afSJed Brown   link->restricthook = restricthook;
2650b17ce1afSJed Brown   link->ctx          = ctx;
26510298fd71SBarry Smith   link->next         = NULL;
2652b17ce1afSJed Brown   *p                 = link;
2653b17ce1afSJed Brown   PetscFunctionReturn(0);
2654b17ce1afSJed Brown }
2655b17ce1afSJed Brown 
2656dc822a44SJed Brown /*@C
2657dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2658dc822a44SJed Brown 
2659dc822a44SJed Brown    Logically Collective
2660dc822a44SJed Brown 
2661dc822a44SJed Brown    Input Arguments:
2662dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2663dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2664dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2665dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2666dc822a44SJed Brown 
2667dc822a44SJed Brown    Level: advanced
2668dc822a44SJed Brown 
2669dc822a44SJed Brown    Notes:
2670dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2671dc822a44SJed Brown 
2672dc822a44SJed Brown    This function is currently not available from Fortran.
2673dc822a44SJed Brown 
2674dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2675dc822a44SJed Brown @*/
2676dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2677dc822a44SJed Brown {
2678dc822a44SJed Brown   PetscErrorCode    ierr;
2679dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2680dc822a44SJed Brown 
2681dc822a44SJed Brown   PetscFunctionBegin;
2682dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2683dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2684dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2685dc822a44SJed Brown       link = *p;
2686dc822a44SJed Brown       *p = link->next;
2687dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2688dc822a44SJed Brown       break;
2689dc822a44SJed Brown     }
2690dc822a44SJed Brown   }
2691dc822a44SJed Brown   PetscFunctionReturn(0);
2692dc822a44SJed Brown }
2693dc822a44SJed Brown 
2694dc822a44SJed Brown 
2695b17ce1afSJed Brown /*@
2696b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2697b17ce1afSJed Brown 
2698b17ce1afSJed Brown    Collective if any hooks are
2699b17ce1afSJed Brown 
2700b17ce1afSJed Brown    Input Arguments:
2701b17ce1afSJed Brown +  fine - finer DM to use as a base
2702b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2703e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2704b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2705e91eccc2SStefano Zampini -  coarse - coarser DM to update
2706b17ce1afSJed Brown 
2707b17ce1afSJed Brown    Level: developer
2708b17ce1afSJed Brown 
2709b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2710b17ce1afSJed Brown @*/
2711b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2712b17ce1afSJed Brown {
2713b17ce1afSJed Brown   PetscErrorCode    ierr;
2714b17ce1afSJed Brown   DMCoarsenHookLink link;
2715b17ce1afSJed Brown 
2716b17ce1afSJed Brown   PetscFunctionBegin;
2717b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
27188865f1eaSKarl Rupp     if (link->restricthook) {
27198865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
27208865f1eaSKarl Rupp     }
2721b17ce1afSJed Brown   }
272247c6ae99SBarry Smith   PetscFunctionReturn(0);
272347c6ae99SBarry Smith }
272447c6ae99SBarry Smith 
2725bb9467b5SJed Brown /*@C
2726be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
27275dbd56e3SPeter Brune 
27285dbd56e3SPeter Brune    Logically Collective
27295dbd56e3SPeter Brune 
27305dbd56e3SPeter Brune    Input Arguments:
27315dbd56e3SPeter Brune +  global - global DM
2732ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
27335dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
27340298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
27355dbd56e3SPeter Brune 
2736ec4806b8SPeter Brune 
2737ec4806b8SPeter Brune    Calling sequence for ddhook:
2738ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2739ec4806b8SPeter Brune 
2740ec4806b8SPeter Brune +  global - global DM
2741ec4806b8SPeter Brune .  block  - block DM
2742ec4806b8SPeter Brune -  ctx - optional user-defined function context
2743ec4806b8SPeter Brune 
27445dbd56e3SPeter Brune    Calling sequence for restricthook:
2745ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
27465dbd56e3SPeter Brune 
27475dbd56e3SPeter Brune +  global - global DM
27485dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
27495dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2750ec4806b8SPeter Brune .  block  - block DM
27515dbd56e3SPeter Brune -  ctx - optional user-defined function context
27525dbd56e3SPeter Brune 
27535dbd56e3SPeter Brune    Level: advanced
27545dbd56e3SPeter Brune 
27555dbd56e3SPeter Brune    Notes:
2756ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
27575dbd56e3SPeter Brune 
27585dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
27595dbd56e3SPeter Brune 
27605dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2761ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
27625dbd56e3SPeter Brune 
2763bb9467b5SJed Brown    This function is currently not available from Fortran.
2764bb9467b5SJed Brown 
27655dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
27665dbd56e3SPeter Brune @*/
2767be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
27685dbd56e3SPeter Brune {
27695dbd56e3SPeter Brune   PetscErrorCode      ierr;
2770be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
27715dbd56e3SPeter Brune 
27725dbd56e3SPeter Brune   PetscFunctionBegin;
27735dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2774b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2775b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2776b3a6b972SJed Brown   }
277795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
27785dbd56e3SPeter Brune   link->restricthook = restricthook;
2779be081cd6SPeter Brune   link->ddhook       = ddhook;
27805dbd56e3SPeter Brune   link->ctx          = ctx;
27810298fd71SBarry Smith   link->next         = NULL;
27825dbd56e3SPeter Brune   *p                 = link;
27835dbd56e3SPeter Brune   PetscFunctionReturn(0);
27845dbd56e3SPeter Brune }
27855dbd56e3SPeter Brune 
2786b3a6b972SJed Brown /*@C
2787b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2788b3a6b972SJed Brown 
2789b3a6b972SJed Brown    Logically Collective
2790b3a6b972SJed Brown 
2791b3a6b972SJed Brown    Input Arguments:
2792b3a6b972SJed Brown +  global - global DM
2793b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2794b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2795b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2796b3a6b972SJed Brown 
2797b3a6b972SJed Brown    Level: advanced
2798b3a6b972SJed Brown 
2799b3a6b972SJed Brown    Notes:
2800b3a6b972SJed Brown 
2801b3a6b972SJed Brown    This function is currently not available from Fortran.
2802b3a6b972SJed Brown 
2803b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2804b3a6b972SJed Brown @*/
2805b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2806b3a6b972SJed Brown {
2807b3a6b972SJed Brown   PetscErrorCode      ierr;
2808b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2809b3a6b972SJed Brown 
2810b3a6b972SJed Brown   PetscFunctionBegin;
2811b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2812b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2813b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2814b3a6b972SJed Brown       link = *p;
2815b3a6b972SJed Brown       *p = link->next;
2816b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2817b3a6b972SJed Brown       break;
2818b3a6b972SJed Brown     }
2819b3a6b972SJed Brown   }
2820b3a6b972SJed Brown   PetscFunctionReturn(0);
2821b3a6b972SJed Brown }
2822b3a6b972SJed Brown 
28235dbd56e3SPeter Brune /*@
2824be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
28255dbd56e3SPeter Brune 
28265dbd56e3SPeter Brune    Collective if any hooks are
28275dbd56e3SPeter Brune 
28285dbd56e3SPeter Brune    Input Arguments:
28295dbd56e3SPeter Brune +  fine - finer DM to use as a base
2830be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2831be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
28325dbd56e3SPeter Brune -  coarse - coarer DM to update
28335dbd56e3SPeter Brune 
28345dbd56e3SPeter Brune    Level: developer
28355dbd56e3SPeter Brune 
28365dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
28375dbd56e3SPeter Brune @*/
2838be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
28395dbd56e3SPeter Brune {
28405dbd56e3SPeter Brune   PetscErrorCode      ierr;
2841be081cd6SPeter Brune   DMSubDomainHookLink link;
28425dbd56e3SPeter Brune 
28435dbd56e3SPeter Brune   PetscFunctionBegin;
2844be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
28458865f1eaSKarl Rupp     if (link->restricthook) {
28468865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
28478865f1eaSKarl Rupp     }
28485dbd56e3SPeter Brune   }
28495dbd56e3SPeter Brune   PetscFunctionReturn(0);
28505dbd56e3SPeter Brune }
28515dbd56e3SPeter Brune 
28525fe1f584SPeter Brune /*@
28536a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
28545fe1f584SPeter Brune 
28555fe1f584SPeter Brune     Not Collective
28565fe1f584SPeter Brune 
28575fe1f584SPeter Brune     Input Parameter:
28585fe1f584SPeter Brune .   dm - the DM object
28595fe1f584SPeter Brune 
28605fe1f584SPeter Brune     Output Parameter:
28616a7d9d85SPeter Brune .   level - number of coarsenings
28625fe1f584SPeter Brune 
28635fe1f584SPeter Brune     Level: developer
28645fe1f584SPeter Brune 
28656a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
28665fe1f584SPeter Brune 
28675fe1f584SPeter Brune @*/
28685fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
28695fe1f584SPeter Brune {
28705fe1f584SPeter Brune   PetscFunctionBegin;
28715fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28725fe1f584SPeter Brune   *level = dm->leveldown;
28735fe1f584SPeter Brune   PetscFunctionReturn(0);
28745fe1f584SPeter Brune }
28755fe1f584SPeter Brune 
28765fe1f584SPeter Brune 
28775fe1f584SPeter Brune 
287847c6ae99SBarry Smith /*@C
287947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
288047c6ae99SBarry Smith 
288147c6ae99SBarry Smith     Collective on DM
288247c6ae99SBarry Smith 
288347c6ae99SBarry Smith     Input Parameter:
288447c6ae99SBarry Smith +   dm - the DM object
288547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
288647c6ae99SBarry Smith 
288747c6ae99SBarry Smith     Output Parameter:
288847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
288947c6ae99SBarry Smith 
289047c6ae99SBarry Smith     Level: developer
289147c6ae99SBarry Smith 
2892e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
289347c6ae99SBarry Smith 
289447c6ae99SBarry Smith @*/
28957087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
289647c6ae99SBarry Smith {
289747c6ae99SBarry Smith   PetscErrorCode ierr;
289847c6ae99SBarry Smith 
289947c6ae99SBarry Smith   PetscFunctionBegin;
2900171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2901ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
290247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
290347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
290447c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
290547c6ae99SBarry Smith   } else if (dm->ops->refine) {
290647c6ae99SBarry Smith     PetscInt i;
290747c6ae99SBarry Smith 
2908ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
290947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2910ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
291147c6ae99SBarry Smith     }
2912ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
291347c6ae99SBarry Smith   PetscFunctionReturn(0);
291447c6ae99SBarry Smith }
291547c6ae99SBarry Smith 
291647c6ae99SBarry Smith /*@C
291747c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
291847c6ae99SBarry Smith 
291947c6ae99SBarry Smith     Collective on DM
292047c6ae99SBarry Smith 
292147c6ae99SBarry Smith     Input Parameter:
292247c6ae99SBarry Smith +   dm - the DM object
292347c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
292447c6ae99SBarry Smith 
292547c6ae99SBarry Smith     Output Parameter:
292647c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
292747c6ae99SBarry Smith 
292847c6ae99SBarry Smith     Level: developer
292947c6ae99SBarry Smith 
2930e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
293147c6ae99SBarry Smith 
293247c6ae99SBarry Smith @*/
29337087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
293447c6ae99SBarry Smith {
293547c6ae99SBarry Smith   PetscErrorCode ierr;
293647c6ae99SBarry Smith 
293747c6ae99SBarry Smith   PetscFunctionBegin;
2938171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2939ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
294047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
294147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
294247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
294347c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
294447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
294547c6ae99SBarry Smith     PetscInt i;
294647c6ae99SBarry Smith 
2947ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
294847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2949ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
295047c6ae99SBarry Smith     }
2951ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
295247c6ae99SBarry Smith   PetscFunctionReturn(0);
295347c6ae99SBarry Smith }
295447c6ae99SBarry Smith 
295547c6ae99SBarry Smith /*@
2956e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
295747c6ae99SBarry Smith    grids associated with two DMs.
295847c6ae99SBarry Smith 
295947c6ae99SBarry Smith    Collective on DM
296047c6ae99SBarry Smith 
296147c6ae99SBarry Smith    Input Parameters:
296247c6ae99SBarry Smith +  dmc - the coarse grid DM
296347c6ae99SBarry Smith -  dmf - the fine grid DM
296447c6ae99SBarry Smith 
296547c6ae99SBarry Smith    Output Parameters:
296647c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
296747c6ae99SBarry Smith 
296847c6ae99SBarry Smith    Level: intermediate
296947c6ae99SBarry Smith 
297047c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
297147c6ae99SBarry Smith 
2972e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
297347c6ae99SBarry Smith @*/
2974e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
297547c6ae99SBarry Smith {
297647c6ae99SBarry Smith   PetscErrorCode ierr;
297747c6ae99SBarry Smith 
297847c6ae99SBarry Smith   PetscFunctionBegin;
2979171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2980171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
298147c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
298247c6ae99SBarry Smith   PetscFunctionReturn(0);
298347c6ae99SBarry Smith }
298447c6ae99SBarry Smith 
29851a266240SBarry Smith /*@C
29861a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
29871a266240SBarry Smith 
29881a266240SBarry Smith     Not Collective
29891a266240SBarry Smith 
29901a266240SBarry Smith     Input Parameters:
29911a266240SBarry Smith +   dm - the DM object
29921a266240SBarry Smith -   destroy - the destroy function
29931a266240SBarry Smith 
29941a266240SBarry Smith     Level: intermediate
29951a266240SBarry Smith 
2996e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
29971a266240SBarry Smith 
2998f07f9ceaSJed Brown @*/
29991a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
30001a266240SBarry Smith {
30011a266240SBarry Smith   PetscFunctionBegin;
3002171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30031a266240SBarry Smith   dm->ctxdestroy = destroy;
30041a266240SBarry Smith   PetscFunctionReturn(0);
30051a266240SBarry Smith }
30061a266240SBarry Smith 
3007b07ff414SBarry Smith /*@
30081b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
300947c6ae99SBarry Smith 
301047c6ae99SBarry Smith     Not Collective
301147c6ae99SBarry Smith 
301247c6ae99SBarry Smith     Input Parameters:
301347c6ae99SBarry Smith +   dm - the DM object
301447c6ae99SBarry Smith -   ctx - the user context
301547c6ae99SBarry Smith 
301647c6ae99SBarry Smith     Level: intermediate
301747c6ae99SBarry Smith 
3018e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
301947c6ae99SBarry Smith 
302047c6ae99SBarry Smith @*/
30211b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
302247c6ae99SBarry Smith {
302347c6ae99SBarry Smith   PetscFunctionBegin;
3024171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
302547c6ae99SBarry Smith   dm->ctx = ctx;
302647c6ae99SBarry Smith   PetscFunctionReturn(0);
302747c6ae99SBarry Smith }
302847c6ae99SBarry Smith 
302947c6ae99SBarry Smith /*@
30301b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
303147c6ae99SBarry Smith 
303247c6ae99SBarry Smith     Not Collective
303347c6ae99SBarry Smith 
303447c6ae99SBarry Smith     Input Parameter:
303547c6ae99SBarry Smith .   dm - the DM object
303647c6ae99SBarry Smith 
303747c6ae99SBarry Smith     Output Parameter:
303847c6ae99SBarry Smith .   ctx - the user context
303947c6ae99SBarry Smith 
304047c6ae99SBarry Smith     Level: intermediate
304147c6ae99SBarry Smith 
3042e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
304347c6ae99SBarry Smith 
304447c6ae99SBarry Smith @*/
30451b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
304647c6ae99SBarry Smith {
304747c6ae99SBarry Smith   PetscFunctionBegin;
3048171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30491b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
305047c6ae99SBarry Smith   PetscFunctionReturn(0);
305147c6ae99SBarry Smith }
305247c6ae99SBarry Smith 
305308da532bSDmitry Karpeev /*@C
3054df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
305508da532bSDmitry Karpeev 
305608da532bSDmitry Karpeev     Logically Collective on DM
305708da532bSDmitry Karpeev 
305808da532bSDmitry Karpeev     Input Parameter:
305908da532bSDmitry Karpeev +   dm - the DM object
30600298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
306108da532bSDmitry Karpeev 
306208da532bSDmitry Karpeev     Level: intermediate
306308da532bSDmitry Karpeev 
3064835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
306508da532bSDmitry Karpeev          DMSetJacobian()
306608da532bSDmitry Karpeev 
306708da532bSDmitry Karpeev @*/
306808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
306908da532bSDmitry Karpeev {
307008da532bSDmitry Karpeev   PetscFunctionBegin;
307108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
307208da532bSDmitry Karpeev   PetscFunctionReturn(0);
307308da532bSDmitry Karpeev }
307408da532bSDmitry Karpeev 
307508da532bSDmitry Karpeev /*@
307608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
307708da532bSDmitry Karpeev 
307808da532bSDmitry Karpeev     Not Collective
307908da532bSDmitry Karpeev 
308008da532bSDmitry Karpeev     Input Parameter:
308108da532bSDmitry Karpeev .   dm - the DM object to destroy
308208da532bSDmitry Karpeev 
308308da532bSDmitry Karpeev     Output Parameter:
308408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
308508da532bSDmitry Karpeev 
308608da532bSDmitry Karpeev     Level: developer
308708da532bSDmitry Karpeev 
308874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
308908da532bSDmitry Karpeev 
309008da532bSDmitry Karpeev @*/
309108da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
309208da532bSDmitry Karpeev {
309308da532bSDmitry Karpeev   PetscFunctionBegin;
309408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
309508da532bSDmitry Karpeev   PetscFunctionReturn(0);
309608da532bSDmitry Karpeev }
309708da532bSDmitry Karpeev 
309808da532bSDmitry Karpeev /*@C
309908da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
310008da532bSDmitry Karpeev 
310108da532bSDmitry Karpeev     Logically Collective on DM
310208da532bSDmitry Karpeev 
310308da532bSDmitry Karpeev     Input Parameters:
3104907376e6SBarry Smith .   dm - the DM object
310508da532bSDmitry Karpeev 
310608da532bSDmitry Karpeev     Output parameters:
310708da532bSDmitry Karpeev +   xl - lower bound
310808da532bSDmitry Karpeev -   xu - upper bound
310908da532bSDmitry Karpeev 
3110907376e6SBarry Smith     Level: advanced
3111907376e6SBarry Smith 
311295452b02SPatrick Sanan     Notes:
311395452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
311408da532bSDmitry Karpeev 
311574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
311608da532bSDmitry Karpeev 
311708da532bSDmitry Karpeev @*/
311808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
311908da532bSDmitry Karpeev {
312008da532bSDmitry Karpeev   PetscErrorCode ierr;
31215fd66863SKarl Rupp 
312208da532bSDmitry Karpeev   PetscFunctionBegin;
312308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
312408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
312508da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
312608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
31278865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
312808da532bSDmitry Karpeev   PetscFunctionReturn(0);
312908da532bSDmitry Karpeev }
313008da532bSDmitry Karpeev 
3131b0ae01b7SPeter Brune /*@
3132b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3133b0ae01b7SPeter Brune 
3134b0ae01b7SPeter Brune     Not Collective
3135b0ae01b7SPeter Brune 
3136b0ae01b7SPeter Brune     Input Parameter:
3137b0ae01b7SPeter Brune .   dm - the DM object
3138b0ae01b7SPeter Brune 
3139b0ae01b7SPeter Brune     Output Parameter:
3140b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3141b0ae01b7SPeter Brune 
3142b0ae01b7SPeter Brune     Level: developer
3143b0ae01b7SPeter Brune 
3144b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3145b0ae01b7SPeter Brune 
3146b0ae01b7SPeter Brune @*/
3147b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3148b0ae01b7SPeter Brune {
3149b0ae01b7SPeter Brune   PetscFunctionBegin;
3150b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3151b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3152b0ae01b7SPeter Brune }
3153b0ae01b7SPeter Brune 
31543ad4599aSBarry Smith /*@
31553ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
31563ad4599aSBarry Smith 
31573ad4599aSBarry Smith     Not Collective
31583ad4599aSBarry Smith 
31593ad4599aSBarry Smith     Input Parameter:
31603ad4599aSBarry Smith .   dm - the DM object
31613ad4599aSBarry Smith 
31623ad4599aSBarry Smith     Output Parameter:
31633ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
31643ad4599aSBarry Smith 
31653ad4599aSBarry Smith     Level: developer
31663ad4599aSBarry Smith 
31673ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
31683ad4599aSBarry Smith 
31693ad4599aSBarry Smith @*/
31703ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
31713ad4599aSBarry Smith {
31723ad4599aSBarry Smith   PetscFunctionBegin;
31733ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
31743ad4599aSBarry Smith   PetscFunctionReturn(0);
31753ad4599aSBarry Smith }
31763ad4599aSBarry Smith 
3177a7058e45SLawrence Mitchell 
3178a7058e45SLawrence Mitchell /*@
3179a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3180a7058e45SLawrence Mitchell 
3181a7058e45SLawrence Mitchell     Not Collective
3182a7058e45SLawrence Mitchell 
3183a7058e45SLawrence Mitchell     Input Parameter:
3184a7058e45SLawrence Mitchell .   dm - the DM object
3185a7058e45SLawrence Mitchell 
3186a7058e45SLawrence Mitchell     Output Parameter:
3187a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3188a7058e45SLawrence Mitchell 
3189a7058e45SLawrence Mitchell     Level: developer
3190a7058e45SLawrence Mitchell 
3191a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3192a7058e45SLawrence Mitchell 
3193a7058e45SLawrence Mitchell @*/
3194a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3195a7058e45SLawrence Mitchell {
31964a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3197a7058e45SLawrence Mitchell   PetscFunctionBegin;
31984a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
31994a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3200a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3201a7058e45SLawrence Mitchell }
3202a7058e45SLawrence Mitchell 
3203748fac09SDmitry Karpeev /*@C
320408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
320508da532bSDmitry Karpeev 
320608da532bSDmitry Karpeev     Collective on DM
320708da532bSDmitry Karpeev 
320808da532bSDmitry Karpeev     Input Parameter:
320908da532bSDmitry Karpeev +   dm - the DM object
32100298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
321108da532bSDmitry Karpeev 
321208da532bSDmitry Karpeev     Level: developer
321308da532bSDmitry Karpeev 
321474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
321508da532bSDmitry Karpeev 
321608da532bSDmitry Karpeev @*/
321708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
321808da532bSDmitry Karpeev {
321908da532bSDmitry Karpeev   PetscErrorCode ierr;
32205fd66863SKarl Rupp 
322108da532bSDmitry Karpeev   PetscFunctionBegin;
322208da532bSDmitry Karpeev   if (x) {
322308da532bSDmitry Karpeev     if (!dm->x) {
322408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
322508da532bSDmitry Karpeev     }
322608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
32278865f1eaSKarl Rupp   } else if (dm->x) {
322808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
322908da532bSDmitry Karpeev   }
323008da532bSDmitry Karpeev   PetscFunctionReturn(0);
323108da532bSDmitry Karpeev }
323208da532bSDmitry Karpeev 
32330298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3234264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3235264ace61SBarry Smith 
3236264ace61SBarry Smith /*@C
3237264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3238264ace61SBarry Smith 
3239264ace61SBarry Smith   Collective on DM
3240264ace61SBarry Smith 
3241264ace61SBarry Smith   Input Parameters:
3242264ace61SBarry Smith + dm     - The DM object
3243264ace61SBarry Smith - method - The name of the DM type
3244264ace61SBarry Smith 
3245264ace61SBarry Smith   Options Database Key:
3246264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3247264ace61SBarry Smith 
3248264ace61SBarry Smith   Notes:
3249e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3250264ace61SBarry Smith 
3251264ace61SBarry Smith   Level: intermediate
3252264ace61SBarry Smith 
3253264ace61SBarry Smith .keywords: DM, set, type
3254264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3255264ace61SBarry Smith @*/
325619fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3257264ace61SBarry Smith {
3258264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3259264ace61SBarry Smith   PetscBool      match;
3260264ace61SBarry Smith   PetscErrorCode ierr;
3261264ace61SBarry Smith 
3262264ace61SBarry Smith   PetscFunctionBegin;
3263264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3264251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3265264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3266264ace61SBarry Smith 
32670f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
32681c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3269ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3270264ace61SBarry Smith 
3271264ace61SBarry Smith   if (dm->ops->destroy) {
3272264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
32730298fd71SBarry Smith     dm->ops->destroy = NULL;
3274264ace61SBarry Smith   }
3275264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3276264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3277264ace61SBarry Smith   PetscFunctionReturn(0);
3278264ace61SBarry Smith }
3279264ace61SBarry Smith 
3280264ace61SBarry Smith /*@C
3281264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3282264ace61SBarry Smith 
3283264ace61SBarry Smith   Not Collective
3284264ace61SBarry Smith 
3285264ace61SBarry Smith   Input Parameter:
3286264ace61SBarry Smith . dm  - The DM
3287264ace61SBarry Smith 
3288264ace61SBarry Smith   Output Parameter:
3289264ace61SBarry Smith . type - The DM type name
3290264ace61SBarry Smith 
3291264ace61SBarry Smith   Level: intermediate
3292264ace61SBarry Smith 
3293264ace61SBarry Smith .keywords: DM, get, type, name
3294264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3295264ace61SBarry Smith @*/
329619fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3297264ace61SBarry Smith {
3298264ace61SBarry Smith   PetscErrorCode ierr;
3299264ace61SBarry Smith 
3300264ace61SBarry Smith   PetscFunctionBegin;
3301264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3302c959eef4SJed Brown   PetscValidPointer(type,2);
3303607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3304264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3305264ace61SBarry Smith   PetscFunctionReturn(0);
3306264ace61SBarry Smith }
3307264ace61SBarry Smith 
330867a56275SMatthew G Knepley /*@C
330967a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
331067a56275SMatthew G Knepley 
331167a56275SMatthew G Knepley   Collective on DM
331267a56275SMatthew G Knepley 
331367a56275SMatthew G Knepley   Input Parameters:
331467a56275SMatthew G Knepley + dm - the DM
331567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
331667a56275SMatthew G Knepley 
331767a56275SMatthew G Knepley   Output Parameter:
331867a56275SMatthew G Knepley . M - pointer to new DM
331967a56275SMatthew G Knepley 
332067a56275SMatthew G Knepley   Notes:
332167a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
332267a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
332367a56275SMatthew G Knepley   of the input DM.
332467a56275SMatthew G Knepley 
332567a56275SMatthew G Knepley   Level: intermediate
332667a56275SMatthew G Knepley 
332767a56275SMatthew G Knepley .seealso: DMCreate()
332867a56275SMatthew G Knepley @*/
332919fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
333067a56275SMatthew G Knepley {
333167a56275SMatthew G Knepley   DM             B;
333267a56275SMatthew G Knepley   char           convname[256];
3333c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
333467a56275SMatthew G Knepley   PetscErrorCode ierr;
333567a56275SMatthew G Knepley 
333667a56275SMatthew G Knepley   PetscFunctionBegin;
333767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
333867a56275SMatthew G Knepley   PetscValidType(dm,1);
333967a56275SMatthew G Knepley   PetscValidPointer(M,3);
3340251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3341c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3342c067b6caSMatthew G. Knepley   if (sametype) {
3343c067b6caSMatthew G. Knepley     *M   = dm;
3344c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3345c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3346c067b6caSMatthew G. Knepley   } else {
33470298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
334867a56275SMatthew G Knepley 
334967a56275SMatthew G Knepley     /*
335067a56275SMatthew G Knepley        Order of precedence:
335167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
335267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
335367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
335467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
335567a56275SMatthew G Knepley        5) Use a really basic converter.
335667a56275SMatthew G Knepley     */
335767a56275SMatthew G Knepley 
335867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3359a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3360a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3361a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3362a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3363a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
33640005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
336567a56275SMatthew G Knepley     if (conv) goto foundconv;
336667a56275SMatthew G Knepley 
336767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
336882f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
336967a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3370a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3371a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3372a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3373a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3374a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
33750005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
337667a56275SMatthew G Knepley     if (conv) {
3377fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
337867a56275SMatthew G Knepley       goto foundconv;
337967a56275SMatthew G Knepley     }
338067a56275SMatthew G Knepley 
338167a56275SMatthew G Knepley #if 0
338267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
338367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3384fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
338567a56275SMatthew G Knepley     if (conv) goto foundconv;
338667a56275SMatthew G Knepley 
338767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
338867a56275SMatthew G Knepley     if (dm->ops->convert) {
338967a56275SMatthew G Knepley       conv = dm->ops->convert;
339067a56275SMatthew G Knepley     }
339167a56275SMatthew G Knepley     if (conv) goto foundconv;
339267a56275SMatthew G Knepley #endif
339367a56275SMatthew G Knepley 
339467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
339582f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
339667a56275SMatthew G Knepley 
339767a56275SMatthew G Knepley foundconv:
339867a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
339967a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
340012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
340190b157c4SStefano Zampini     {
340290b157c4SStefano Zampini       PetscBool             isper;
340312fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
340412fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
340590b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
340690b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
340712fa691eSMatthew G. Knepley     }
340867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
340967a56275SMatthew G Knepley   }
341067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
341167a56275SMatthew G Knepley   PetscFunctionReturn(0);
341267a56275SMatthew G Knepley }
3413264ace61SBarry Smith 
3414264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3415264ace61SBarry Smith 
3416264ace61SBarry Smith /*@C
34171c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
34181c84c290SBarry Smith 
34191c84c290SBarry Smith   Not Collective
34201c84c290SBarry Smith 
34211c84c290SBarry Smith   Input Parameters:
34221c84c290SBarry Smith + name        - The name of a new user-defined creation routine
34231c84c290SBarry Smith - create_func - The creation routine itself
34241c84c290SBarry Smith 
34251c84c290SBarry Smith   Notes:
34261c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
34271c84c290SBarry Smith 
34281c84c290SBarry Smith 
34291c84c290SBarry Smith   Sample usage:
34301c84c290SBarry Smith .vb
3431bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
34321c84c290SBarry Smith .ve
34331c84c290SBarry Smith 
34341c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
34351c84c290SBarry Smith .vb
34361c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
34371c84c290SBarry Smith     DMSetType(DM,"my_da");
34381c84c290SBarry Smith .ve
34391c84c290SBarry Smith    or at runtime via the option
34401c84c290SBarry Smith .vb
34411c84c290SBarry Smith     -da_type my_da
34421c84c290SBarry Smith .ve
3443264ace61SBarry Smith 
3444264ace61SBarry Smith   Level: advanced
34451c84c290SBarry Smith 
34461c84c290SBarry Smith .keywords: DM, register
3447bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
34481c84c290SBarry Smith 
3449264ace61SBarry Smith @*/
3450bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3451264ace61SBarry Smith {
3452264ace61SBarry Smith   PetscErrorCode ierr;
3453264ace61SBarry Smith 
3454264ace61SBarry Smith   PetscFunctionBegin;
34551d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3456a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3457264ace61SBarry Smith   PetscFunctionReturn(0);
3458264ace61SBarry Smith }
3459264ace61SBarry Smith 
3460b859378eSBarry Smith /*@C
346155849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3462b859378eSBarry Smith 
3463b859378eSBarry Smith   Collective on PetscViewer
3464b859378eSBarry Smith 
3465b859378eSBarry Smith   Input Parameters:
3466b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3467b859378eSBarry Smith            some related function before a call to DMLoad().
3468b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3469b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3470b859378eSBarry Smith 
3471b859378eSBarry Smith    Level: intermediate
3472b859378eSBarry Smith 
3473b859378eSBarry Smith   Notes:
347455849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3475b859378eSBarry Smith 
3476b859378eSBarry Smith   Notes for advanced users:
3477b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3478b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3479b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3480b859378eSBarry Smith   format is
3481b859378eSBarry Smith .vb
3482b859378eSBarry Smith      has not yet been determined
3483b859378eSBarry Smith .ve
3484b859378eSBarry Smith 
3485b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3486b859378eSBarry Smith @*/
3487b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3488b859378eSBarry Smith {
34899331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3490b859378eSBarry Smith   PetscErrorCode ierr;
3491b859378eSBarry Smith 
3492b859378eSBarry Smith   PetscFunctionBegin;
3493b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3494b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
349532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
34969331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
34979331c7a4SMatthew G. Knepley   if (isbinary) {
34989331c7a4SMatthew G. Knepley     PetscInt classid;
34999331c7a4SMatthew G. Knepley     char     type[256];
3500b859378eSBarry Smith 
3501060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
35029200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3503060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
350432c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
35059331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35069331c7a4SMatthew G. Knepley   } else if (ishdf5) {
35079331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35089331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3509b859378eSBarry Smith   PetscFunctionReturn(0);
3510b859378eSBarry Smith }
3511b859378eSBarry Smith 
35127da65231SMatthew G Knepley /******************************** FEM Support **********************************/
35137da65231SMatthew G Knepley 
3514a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3515a6dfd86eSKarl Rupp {
35161d47ebbbSSatish Balay   PetscInt       f;
35171b30c384SMatthew G Knepley   PetscErrorCode ierr;
35181b30c384SMatthew G Knepley 
35197da65231SMatthew G Knepley   PetscFunctionBegin;
352074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35211d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
352257622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
35237da65231SMatthew G Knepley   }
35247da65231SMatthew G Knepley   PetscFunctionReturn(0);
35257da65231SMatthew G Knepley }
35267da65231SMatthew G Knepley 
3527a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3528a6dfd86eSKarl Rupp {
35291b30c384SMatthew G Knepley   PetscInt       f, g;
35307da65231SMatthew G Knepley   PetscErrorCode ierr;
35317da65231SMatthew G Knepley 
35327da65231SMatthew G Knepley   PetscFunctionBegin;
353374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35341d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
353574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
35361d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3537e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
35387da65231SMatthew G Knepley     }
353974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
35407da65231SMatthew G Knepley   }
35417da65231SMatthew G Knepley   PetscFunctionReturn(0);
35427da65231SMatthew G Knepley }
3543e7c4fc90SDmitry Karpeev 
35446113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3545e759306cSMatthew G. Knepley {
35460c5b8624SToby Isaac   PetscInt          localSize, bs;
35470c5b8624SToby Isaac   PetscMPIInt       size;
35480c5b8624SToby Isaac   Vec               x, xglob;
35490c5b8624SToby Isaac   const PetscScalar *xarray;
3550e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3551e759306cSMatthew G. Knepley 
3552e759306cSMatthew G. Knepley   PetscFunctionBegin;
35539852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3554e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3555e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
35566113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
35570c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
35580c5b8624SToby Isaac   if (size > 1) {
35590c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
35600c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
35610c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
35620c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
35630c5b8624SToby Isaac   } else {
35640c5b8624SToby Isaac     xglob = x;
35650c5b8624SToby Isaac   }
35660c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
35670c5b8624SToby Isaac   if (size > 1) {
35680c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
35690c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
35700c5b8624SToby Isaac   }
3571e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3572e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3573e759306cSMatthew G. Knepley }
3574e759306cSMatthew G. Knepley 
357588ed4aceSMatthew G Knepley /*@
3576e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
357788ed4aceSMatthew G Knepley 
357888ed4aceSMatthew G Knepley   Input Parameter:
357988ed4aceSMatthew G Knepley . dm - The DM
358088ed4aceSMatthew G Knepley 
358188ed4aceSMatthew G Knepley   Output Parameter:
358288ed4aceSMatthew G Knepley . section - The PetscSection
358388ed4aceSMatthew G Knepley 
3584e5893cccSMatthew G. Knepley   Options Database Keys:
3585e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3586e5893cccSMatthew G. Knepley 
358788ed4aceSMatthew G Knepley   Level: intermediate
358888ed4aceSMatthew G Knepley 
358988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
359088ed4aceSMatthew G Knepley 
3591e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
359288ed4aceSMatthew G Knepley @*/
3593e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
35940adebc6cSBarry Smith {
3595fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3596fd59a867SMatthew G. Knepley 
359788ed4aceSMatthew G Knepley   PetscFunctionBegin;
359888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
359988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
36002f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
36012f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3602ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
36032f0f8703SMatthew G. Knepley   }
360488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
360588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
360688ed4aceSMatthew G Knepley }
360788ed4aceSMatthew G Knepley 
360888ed4aceSMatthew G Knepley /*@
3609e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
361088ed4aceSMatthew G Knepley 
361188ed4aceSMatthew G Knepley   Input Parameters:
361288ed4aceSMatthew G Knepley + dm - The DM
361388ed4aceSMatthew G Knepley - section - The PetscSection
361488ed4aceSMatthew G Knepley 
361588ed4aceSMatthew G Knepley   Level: intermediate
361688ed4aceSMatthew G Knepley 
361788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
361888ed4aceSMatthew G Knepley 
3619e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
362088ed4aceSMatthew G Knepley @*/
3621e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
36220adebc6cSBarry Smith {
3623c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3624af122d2aSMatthew G Knepley   PetscInt       f;
362588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
362688ed4aceSMatthew G Knepley 
362788ed4aceSMatthew G Knepley   PetscFunctionBegin;
362888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3629c473ab19SMatthew G. Knepley   if (section) {
36301d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36311d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3632c473ab19SMatthew G. Knepley   }
363388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
363488ed4aceSMatthew G Knepley   dm->defaultSection = section;
3635c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3636af122d2aSMatthew G Knepley   if (numFields) {
3637af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3638af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
36390f21e855SMatthew G. Knepley       PetscObject disc;
3640af122d2aSMatthew G Knepley       const char *name;
3641af122d2aSMatthew G Knepley 
3642af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
36430f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
36440f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3645af122d2aSMatthew G Knepley     }
3646af122d2aSMatthew G Knepley   }
3647e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
36481d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
364988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
365088ed4aceSMatthew G Knepley }
365188ed4aceSMatthew G Knepley 
36529435951eSToby Isaac /*@
36539435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
36549435951eSToby Isaac 
3655e228b242SToby Isaac   not collective
3656e228b242SToby Isaac 
36579435951eSToby Isaac   Input Parameter:
36589435951eSToby Isaac . dm - The DM
36599435951eSToby Isaac 
36609435951eSToby Isaac   Output Parameter:
36619435951eSToby 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.
36629435951eSToby 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.
36639435951eSToby Isaac 
36649435951eSToby Isaac   Level: advanced
36659435951eSToby Isaac 
36669435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
36679435951eSToby Isaac 
36689435951eSToby Isaac .seealso: DMSetDefaultConstraints()
36699435951eSToby Isaac @*/
36709435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
36719435951eSToby Isaac {
36729435951eSToby Isaac   PetscErrorCode ierr;
36739435951eSToby Isaac 
36749435951eSToby Isaac   PetscFunctionBegin;
36759435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36769435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
367745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
367845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
36799435951eSToby Isaac   PetscFunctionReturn(0);
36809435951eSToby Isaac }
36819435951eSToby Isaac 
36829435951eSToby Isaac /*@
36839435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
36849435951eSToby Isaac 
36859435951eSToby 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().
36869435951eSToby Isaac 
36879435951eSToby 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.
36889435951eSToby Isaac 
3689e228b242SToby Isaac   collective on dm
3690e228b242SToby Isaac 
36919435951eSToby Isaac   Input Parameters:
36929435951eSToby Isaac + dm - The DM
3693e228b242SToby 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).
3694e228b242SToby 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).
36959435951eSToby Isaac 
36969435951eSToby Isaac   Level: advanced
36979435951eSToby Isaac 
36989435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
36999435951eSToby Isaac 
37009435951eSToby Isaac .seealso: DMGetDefaultConstraints()
37019435951eSToby Isaac @*/
37029435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
37039435951eSToby Isaac {
3704e228b242SToby Isaac   PetscMPIInt result;
37059435951eSToby Isaac   PetscErrorCode ierr;
37069435951eSToby Isaac 
37079435951eSToby Isaac   PetscFunctionBegin;
37089435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3709e228b242SToby Isaac   if (section) {
3710e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3711e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3712f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3713e228b242SToby Isaac   }
3714e228b242SToby Isaac   if (mat) {
3715e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3716e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3717f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3718e228b242SToby Isaac   }
37199435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
37209435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
37219435951eSToby Isaac   dm->defaultConstraintSection = section;
37229435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
37239435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
37249435951eSToby Isaac   dm->defaultConstraintMat = mat;
37259435951eSToby Isaac   PetscFunctionReturn(0);
37269435951eSToby Isaac }
37279435951eSToby Isaac 
3728497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3729507e4973SMatthew G. Knepley /*
3730507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3731507e4973SMatthew G. Knepley 
3732507e4973SMatthew G. Knepley   Input Parameters:
3733507e4973SMatthew G. Knepley + dm - The DM
3734507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3735507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3736507e4973SMatthew G. Knepley 
3737507e4973SMatthew G. Knepley   Level: intermediate
3738507e4973SMatthew G. Knepley 
3739507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3740507e4973SMatthew G. Knepley */
3741f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3742507e4973SMatthew G. Knepley {
3743507e4973SMatthew G. Knepley   MPI_Comm        comm;
3744507e4973SMatthew G. Knepley   PetscLayout     layout;
3745507e4973SMatthew G. Knepley   const PetscInt *ranges;
3746507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3747507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3748507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3749507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3750507e4973SMatthew G. Knepley 
3751507e4973SMatthew G. Knepley   PetscFunctionBegin;
3752507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3753507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3754507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3755507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3756507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3757507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3758507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3759507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3760507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3761507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3762507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3763507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3764f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3765507e4973SMatthew G. Knepley 
3766507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3767507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3768507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3769507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3770507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3771507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3772507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3773507e4973SMatthew 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;}
3774507e4973SMatthew 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;}
3775507e4973SMatthew G. Knepley     if (gdof < 0) {
3776507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3777507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3778507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3779507e4973SMatthew G. Knepley 
3780507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3781507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3782507e4973SMatthew 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;}
3783507e4973SMatthew G. Knepley       }
3784507e4973SMatthew G. Knepley     }
3785507e4973SMatthew G. Knepley   }
3786507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3787507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3788b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3789507e4973SMatthew G. Knepley   if (!gvalid) {
3790507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3791507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3792507e4973SMatthew G. Knepley   }
3793507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3794507e4973SMatthew G. Knepley }
3795f741bcd2SMatthew G. Knepley #endif
3796507e4973SMatthew G. Knepley 
379788ed4aceSMatthew G Knepley /*@
3798e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
379988ed4aceSMatthew G Knepley 
38008b1ab98fSJed Brown   Collective on DM
38018b1ab98fSJed Brown 
380288ed4aceSMatthew G Knepley   Input Parameter:
380388ed4aceSMatthew G Knepley . dm - The DM
380488ed4aceSMatthew G Knepley 
380588ed4aceSMatthew G Knepley   Output Parameter:
380688ed4aceSMatthew G Knepley . section - The PetscSection
380788ed4aceSMatthew G Knepley 
380888ed4aceSMatthew G Knepley   Level: intermediate
380988ed4aceSMatthew G Knepley 
381088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
381188ed4aceSMatthew G Knepley 
3812e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
381388ed4aceSMatthew G Knepley @*/
3814e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
38150adebc6cSBarry Smith {
381688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
381788ed4aceSMatthew G Knepley 
381888ed4aceSMatthew G Knepley   PetscFunctionBegin;
381988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
382088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
382188ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3822fd59a867SMatthew G. Knepley     PetscSection s;
3823fd59a867SMatthew G. Knepley 
3824e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3825fd59a867SMatthew 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");
3826fd59a867SMatthew 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");
382715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3828cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3829ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3830685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
383188ed4aceSMatthew G Knepley   }
383288ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
383388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
383488ed4aceSMatthew G Knepley }
383588ed4aceSMatthew G Knepley 
3836b21d0597SMatthew G Knepley /*@
3837e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3838b21d0597SMatthew G Knepley 
3839b21d0597SMatthew G Knepley   Input Parameters:
3840b21d0597SMatthew G Knepley + dm - The DM
38415080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3842b21d0597SMatthew G Knepley 
3843b21d0597SMatthew G Knepley   Level: intermediate
3844b21d0597SMatthew G Knepley 
3845b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3846b21d0597SMatthew G Knepley 
3847e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3848b21d0597SMatthew G Knepley @*/
3849e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
38500adebc6cSBarry Smith {
3851b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3852b21d0597SMatthew G Knepley 
3853b21d0597SMatthew G Knepley   PetscFunctionBegin;
3854b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38555080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
38561d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3857b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3858b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3859497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3860f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3861507e4973SMatthew G. Knepley #endif
3862b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3863b21d0597SMatthew G Knepley }
3864b21d0597SMatthew G Knepley 
386588ed4aceSMatthew G Knepley /*@
386688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
386788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
386888ed4aceSMatthew G Knepley 
386988ed4aceSMatthew G Knepley   Input Parameter:
387088ed4aceSMatthew G Knepley . dm - The DM
387188ed4aceSMatthew G Knepley 
387288ed4aceSMatthew G Knepley   Output Parameter:
387388ed4aceSMatthew G Knepley . sf - The PetscSF
387488ed4aceSMatthew G Knepley 
387588ed4aceSMatthew G Knepley   Level: intermediate
387688ed4aceSMatthew G Knepley 
387788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
387888ed4aceSMatthew G Knepley 
387988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
388088ed4aceSMatthew G Knepley @*/
38810adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
38820adebc6cSBarry Smith {
388388ed4aceSMatthew G Knepley   PetscInt       nroots;
388488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
388588ed4aceSMatthew G Knepley 
388688ed4aceSMatthew G Knepley   PetscFunctionBegin;
388788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
388888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
38890298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
389088ed4aceSMatthew G Knepley   if (nroots < 0) {
389188ed4aceSMatthew G Knepley     PetscSection section, gSection;
389288ed4aceSMatthew G Knepley 
3893e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
389431ea6d37SMatthew G Knepley     if (section) {
3895e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
389688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
389731ea6d37SMatthew G Knepley     } else {
38980298fd71SBarry Smith       *sf = NULL;
389931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
390031ea6d37SMatthew G Knepley     }
390188ed4aceSMatthew G Knepley   }
390288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
390388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
390488ed4aceSMatthew G Knepley }
390588ed4aceSMatthew G Knepley 
390688ed4aceSMatthew G Knepley /*@
390788ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
390888ed4aceSMatthew G Knepley 
390988ed4aceSMatthew G Knepley   Input Parameters:
391088ed4aceSMatthew G Knepley + dm - The DM
391188ed4aceSMatthew G Knepley - sf - The PetscSF
391288ed4aceSMatthew G Knepley 
391388ed4aceSMatthew G Knepley   Level: intermediate
391488ed4aceSMatthew G Knepley 
391588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
391688ed4aceSMatthew G Knepley 
391788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
391888ed4aceSMatthew G Knepley @*/
39190adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
39200adebc6cSBarry Smith {
392188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
392288ed4aceSMatthew G Knepley 
392388ed4aceSMatthew G Knepley   PetscFunctionBegin;
392488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
392588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
392688ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
392788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
392888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
392988ed4aceSMatthew G Knepley }
393088ed4aceSMatthew G Knepley 
393188ed4aceSMatthew G Knepley /*@C
393288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
393388ed4aceSMatthew G Knepley   describing the data layout.
393488ed4aceSMatthew G Knepley 
393588ed4aceSMatthew G Knepley   Input Parameters:
393688ed4aceSMatthew G Knepley + dm - The DM
393788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
393888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
393988ed4aceSMatthew G Knepley 
394088ed4aceSMatthew G Knepley   Level: intermediate
394188ed4aceSMatthew G Knepley 
394288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
394388ed4aceSMatthew G Knepley @*/
394488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
394588ed4aceSMatthew G Knepley {
394682f516ccSBarry Smith   MPI_Comm       comm;
394788ed4aceSMatthew G Knepley   PetscLayout    layout;
394888ed4aceSMatthew G Knepley   const PetscInt *ranges;
394988ed4aceSMatthew G Knepley   PetscInt       *local;
395088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3951ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
395288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
395388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
395488ed4aceSMatthew G Knepley 
395588ed4aceSMatthew G Knepley   PetscFunctionBegin;
395682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
395788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
395888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
395988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
396088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
396188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
396288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
396388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
396488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
396588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
396688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3967ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
39686636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
396988ed4aceSMatthew G Knepley 
39706636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
39716636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3972235fbf56SMatthew 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));
39736636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
397488ed4aceSMatthew G Knepley   }
3975785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3976785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
397788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
39781f588964SMatthew G Knepley     const PetscInt *cind;
39796636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
398088ed4aceSMatthew G Knepley 
398188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
398288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
398388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
398488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
398588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
39866636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
398788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
39886636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
39896636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
39906636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3991057b4bcdSMatthew 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);
39926636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
39936636e97aSMatthew G Knepley     }
399488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
399588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
399688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
399788ed4aceSMatthew G Knepley     }
399888ed4aceSMatthew G Knepley     if (gdof < 0) {
39996636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
400088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
400188ed4aceSMatthew G Knepley 
400205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
400331d3f06eSJed Brown         if (r < 0) r = -(r+2);
400405376888SMatthew 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);
400588ed4aceSMatthew G Knepley         remote[l].rank  = r;
400688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
400788ed4aceSMatthew G Knepley       }
400888ed4aceSMatthew G Knepley     } else {
40096636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
401088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
401188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
401288ed4aceSMatthew G Knepley       }
401388ed4aceSMatthew G Knepley     }
401488ed4aceSMatthew G Knepley   }
40156636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
401688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
401788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
401888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
401988ed4aceSMatthew G Knepley }
4020af122d2aSMatthew G Knepley 
4021b21d0597SMatthew G Knepley /*@
4022b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4023b21d0597SMatthew G Knepley 
4024b21d0597SMatthew G Knepley   Input Parameter:
4025b21d0597SMatthew G Knepley . dm - The DM
4026b21d0597SMatthew G Knepley 
4027b21d0597SMatthew G Knepley   Output Parameter:
4028b21d0597SMatthew G Knepley . sf - The PetscSF
4029b21d0597SMatthew G Knepley 
4030b21d0597SMatthew G Knepley   Level: intermediate
4031b21d0597SMatthew G Knepley 
4032b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4033b21d0597SMatthew G Knepley 
4034057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4035b21d0597SMatthew G Knepley @*/
40360adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
40370adebc6cSBarry Smith {
4038b21d0597SMatthew G Knepley   PetscFunctionBegin;
4039b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4040b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4041b21d0597SMatthew G Knepley   *sf = dm->sf;
4042b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4043b21d0597SMatthew G Knepley }
4044b21d0597SMatthew G Knepley 
4045057b4bcdSMatthew G Knepley /*@
4046057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4047057b4bcdSMatthew G Knepley 
4048057b4bcdSMatthew G Knepley   Input Parameters:
4049057b4bcdSMatthew G Knepley + dm - The DM
4050057b4bcdSMatthew G Knepley - sf - The PetscSF
4051057b4bcdSMatthew G Knepley 
4052057b4bcdSMatthew G Knepley   Level: intermediate
4053057b4bcdSMatthew G Knepley 
4054057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4055057b4bcdSMatthew G Knepley @*/
40560adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
40570adebc6cSBarry Smith {
4058057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4059057b4bcdSMatthew G Knepley 
4060057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4061057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4062057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
4063057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4064057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4065057b4bcdSMatthew G Knepley   dm->sf = sf;
4066057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4067057b4bcdSMatthew G Knepley }
4068057b4bcdSMatthew G Knepley 
40692764a2aaSMatthew G. Knepley /*@
40702764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
40712764a2aaSMatthew G. Knepley 
40722764a2aaSMatthew G. Knepley   Input Parameter:
40732764a2aaSMatthew G. Knepley . dm - The DM
40742764a2aaSMatthew G. Knepley 
40752764a2aaSMatthew G. Knepley   Output Parameter:
40762764a2aaSMatthew G. Knepley . prob - The PetscDS
40772764a2aaSMatthew G. Knepley 
40782764a2aaSMatthew G. Knepley   Level: developer
40792764a2aaSMatthew G. Knepley 
40802764a2aaSMatthew G. Knepley .seealso: DMSetDS()
40812764a2aaSMatthew G. Knepley @*/
40822764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4083af122d2aSMatthew G Knepley {
4084af122d2aSMatthew G Knepley   PetscFunctionBegin;
4085af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40860f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
40870f21e855SMatthew G. Knepley   *prob = dm->prob;
40880f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
40890f21e855SMatthew G. Knepley }
40900f21e855SMatthew G. Knepley 
40912764a2aaSMatthew G. Knepley /*@
40922764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
40932764a2aaSMatthew G. Knepley 
40942764a2aaSMatthew G. Knepley   Input Parameters:
40952764a2aaSMatthew G. Knepley + dm - The DM
40962764a2aaSMatthew G. Knepley - prob - The PetscDS
40972764a2aaSMatthew G. Knepley 
40982764a2aaSMatthew G. Knepley   Level: developer
40992764a2aaSMatthew G. Knepley 
41002764a2aaSMatthew G. Knepley .seealso: DMGetDS()
41012764a2aaSMatthew G. Knepley @*/
41022764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
41030f21e855SMatthew G. Knepley {
4104f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
41050f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41060f21e855SMatthew G. Knepley 
41070f21e855SMatthew G. Knepley   PetscFunctionBegin;
41080f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41092764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
411037316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
41112764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
41120f21e855SMatthew G. Knepley   dm->prob = prob;
4113f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4114f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
41150f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41160f21e855SMatthew G. Knepley }
41170f21e855SMatthew G. Knepley 
41180f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
41190f21e855SMatthew G. Knepley {
41200f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41210f21e855SMatthew G. Knepley 
41220f21e855SMatthew G. Knepley   PetscFunctionBegin;
41230f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41242764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
4125af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4126af122d2aSMatthew G Knepley }
4127af122d2aSMatthew G Knepley 
4128af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4129af122d2aSMatthew G Knepley {
41300f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4131af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4132af122d2aSMatthew G Knepley 
4133af122d2aSMatthew G Knepley   PetscFunctionBegin;
4134af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41352764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
41360f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
41370f21e855SMatthew G. Knepley     PetscContainer obj;
41380f21e855SMatthew G. Knepley 
41390f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
41402764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
41410f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4142af122d2aSMatthew G Knepley   }
4143af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4144af122d2aSMatthew G Knepley }
4145af122d2aSMatthew G Knepley 
4146c1929be8SMatthew G. Knepley /*@
4147c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4148c1929be8SMatthew G. Knepley 
4149c1929be8SMatthew G. Knepley   Not collective
4150c1929be8SMatthew G. Knepley 
4151c1929be8SMatthew G. Knepley   Input Parameters:
4152c1929be8SMatthew G. Knepley + dm - The DM
4153c1929be8SMatthew G. Knepley - f  - The field number
4154c1929be8SMatthew G. Knepley 
4155c1929be8SMatthew G. Knepley   Output Parameter:
4156c1929be8SMatthew G. Knepley . field - The discretization object
4157c1929be8SMatthew G. Knepley 
4158c1929be8SMatthew G. Knepley   Level: developer
4159c1929be8SMatthew G. Knepley 
4160c1929be8SMatthew G. Knepley .seealso: DMSetField()
4161c1929be8SMatthew G. Knepley @*/
4162af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4163af122d2aSMatthew G Knepley {
41640f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41650f21e855SMatthew G. Knepley 
4166af122d2aSMatthew G Knepley   PetscFunctionBegin;
4167af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41682764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4169decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4170decb47aaSMatthew G. Knepley }
4171decb47aaSMatthew G. Knepley 
4172c1929be8SMatthew G. Knepley /*@
4173c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4174c1929be8SMatthew G. Knepley 
4175c1929be8SMatthew G. Knepley   Logically collective on DM
4176c1929be8SMatthew G. Knepley 
4177c1929be8SMatthew G. Knepley   Input Parameters:
4178c1929be8SMatthew G. Knepley + dm - The DM
4179c1929be8SMatthew G. Knepley . f  - The field number
4180c1929be8SMatthew G. Knepley - field - The discretization object
4181c1929be8SMatthew G. Knepley 
4182c1929be8SMatthew G. Knepley   Level: developer
4183c1929be8SMatthew G. Knepley 
4184c1929be8SMatthew G. Knepley .seealso: DMGetField()
4185c1929be8SMatthew G. Knepley @*/
4186decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4187decb47aaSMatthew G. Knepley {
4188decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4189decb47aaSMatthew G. Knepley 
4190decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4191decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41922764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4193af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4194af122d2aSMatthew G Knepley }
41956636e97aSMatthew G Knepley 
4196b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4197b64e0483SPeter Brune {
4198b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4199b64e0483SPeter Brune   PetscErrorCode ierr;
4200b64e0483SPeter Brune   Vec coords,ccoords;
42016dbf9973SLawrence Mitchell   Mat inject;
4202b64e0483SPeter Brune   PetscFunctionBegin;
4203b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4204b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4205b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4206b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4207b64e0483SPeter Brune   if (coords && !ccoords) {
4208b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
42096668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
42106dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
42112adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
42126dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4213b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4214b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4215b64e0483SPeter Brune   }
4216b64e0483SPeter Brune   PetscFunctionReturn(0);
4217b64e0483SPeter Brune }
4218b64e0483SPeter Brune 
421903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
422003dadc2fSPeter Brune {
422103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
422203dadc2fSPeter Brune   PetscErrorCode ierr;
422303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
422403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
422503dadc2fSPeter Brune   PetscFunctionBegin;
422603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
422703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
422803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
422903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
423003dadc2fSPeter Brune   if (coords && !ccoords) {
423103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
42326668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
423303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
423424640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
423503dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
423603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
423703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
423803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
423903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
424003dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
424103dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
424203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
424303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
424403dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
424503dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
424603dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
424703dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
424803dadc2fSPeter Brune   }
424903dadc2fSPeter Brune   PetscFunctionReturn(0);
425003dadc2fSPeter Brune }
425103dadc2fSPeter Brune 
4252c73cfb54SMatthew G. Knepley /*@
4253c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4254c73cfb54SMatthew G. Knepley 
4255c73cfb54SMatthew G. Knepley   Not collective
4256c73cfb54SMatthew G. Knepley 
4257c73cfb54SMatthew G. Knepley   Input Parameter:
4258c73cfb54SMatthew G. Knepley . dm - The DM
4259c73cfb54SMatthew G. Knepley 
4260c73cfb54SMatthew G. Knepley   Output Parameter:
4261c73cfb54SMatthew G. Knepley . dim - The topological dimension
4262c73cfb54SMatthew G. Knepley 
4263c73cfb54SMatthew G. Knepley   Level: beginner
4264c73cfb54SMatthew G. Knepley 
4265c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4266c73cfb54SMatthew G. Knepley @*/
4267c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4268c73cfb54SMatthew G. Knepley {
4269c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4270c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4271c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4272c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4273c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4274c73cfb54SMatthew G. Knepley }
4275c73cfb54SMatthew G. Knepley 
4276c73cfb54SMatthew G. Knepley /*@
4277c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4278c73cfb54SMatthew G. Knepley 
4279c73cfb54SMatthew G. Knepley   Collective on dm
4280c73cfb54SMatthew G. Knepley 
4281c73cfb54SMatthew G. Knepley   Input Parameters:
4282c73cfb54SMatthew G. Knepley + dm - The DM
4283c73cfb54SMatthew G. Knepley - dim - The topological dimension
4284c73cfb54SMatthew G. Knepley 
4285c73cfb54SMatthew G. Knepley   Level: beginner
4286c73cfb54SMatthew G. Knepley 
4287c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4288c73cfb54SMatthew G. Knepley @*/
4289c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4290c73cfb54SMatthew G. Knepley {
4291f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4292f17e8794SMatthew G. Knepley 
4293c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4294c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4295c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4296c73cfb54SMatthew G. Knepley   dm->dim = dim;
4297f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4298c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4299c73cfb54SMatthew G. Knepley }
4300c73cfb54SMatthew G. Knepley 
4301793f3fe5SMatthew G. Knepley /*@
4302793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4303793f3fe5SMatthew G. Knepley 
4304793f3fe5SMatthew G. Knepley   Collective on DM
4305793f3fe5SMatthew G. Knepley 
4306793f3fe5SMatthew G. Knepley   Input Parameters:
4307793f3fe5SMatthew G. Knepley + dm - the DM
4308793f3fe5SMatthew G. Knepley - dim - the dimension
4309793f3fe5SMatthew G. Knepley 
4310793f3fe5SMatthew G. Knepley   Output Parameters:
4311793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4312793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4313793f3fe5SMatthew G. Knepley 
4314793f3fe5SMatthew G. Knepley   Note:
4315793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4316793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4317793f3fe5SMatthew G. Knepley   then the interval is empty.
4318793f3fe5SMatthew G. Knepley 
4319793f3fe5SMatthew G. Knepley   Level: intermediate
4320793f3fe5SMatthew G. Knepley 
4321793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4322793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4323793f3fe5SMatthew G. Knepley @*/
4324793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4325793f3fe5SMatthew G. Knepley {
4326793f3fe5SMatthew G. Knepley   PetscInt       d;
4327793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4328793f3fe5SMatthew G. Knepley 
4329793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4330793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4331793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4332793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4333793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4334793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4335793f3fe5SMatthew G. Knepley }
4336793f3fe5SMatthew G. Knepley 
43376636e97aSMatthew G Knepley /*@
43386636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
43396636e97aSMatthew G Knepley 
43406636e97aSMatthew G Knepley   Collective on DM
43416636e97aSMatthew G Knepley 
43426636e97aSMatthew G Knepley   Input Parameters:
43436636e97aSMatthew G Knepley + dm - the DM
43446636e97aSMatthew G Knepley - c - coordinate vector
43456636e97aSMatthew G Knepley 
43462dd40e9bSPatrick Sanan   Notes:
43472dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
43482dd40e9bSPatrick Sanan 
43492dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
43506636e97aSMatthew G Knepley 
43516636e97aSMatthew G Knepley   Level: intermediate
43526636e97aSMatthew G Knepley 
43536636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43542dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
43556636e97aSMatthew G Knepley @*/
43566636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
43576636e97aSMatthew G Knepley {
43586636e97aSMatthew G Knepley   PetscErrorCode ierr;
43596636e97aSMatthew G Knepley 
43606636e97aSMatthew G Knepley   PetscFunctionBegin;
43616636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43626636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
43636636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
43646636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
43656636e97aSMatthew G Knepley   dm->coordinates = c;
43666636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4367b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
436803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
43696636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43706636e97aSMatthew G Knepley }
43716636e97aSMatthew G Knepley 
43726636e97aSMatthew G Knepley /*@
43736636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
43746636e97aSMatthew G Knepley 
43756636e97aSMatthew G Knepley   Collective on DM
43766636e97aSMatthew G Knepley 
43776636e97aSMatthew G Knepley    Input Parameters:
43786636e97aSMatthew G Knepley +  dm - the DM
43796636e97aSMatthew G Knepley -  c - coordinate vector
43806636e97aSMatthew G Knepley 
43812dd40e9bSPatrick Sanan   Notes:
43826636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
43836636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
43846636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
43856636e97aSMatthew G Knepley 
43862dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
43872dd40e9bSPatrick Sanan 
43886636e97aSMatthew G Knepley   Level: intermediate
43896636e97aSMatthew G Knepley 
43906636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43916636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
43926636e97aSMatthew G Knepley @*/
43936636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
43946636e97aSMatthew G Knepley {
43956636e97aSMatthew G Knepley   PetscErrorCode ierr;
43966636e97aSMatthew G Knepley 
43976636e97aSMatthew G Knepley   PetscFunctionBegin;
43986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43996636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
44006636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
44016636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
44028865f1eaSKarl Rupp 
44036636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
44048865f1eaSKarl Rupp 
44056636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
44066636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44076636e97aSMatthew G Knepley }
44086636e97aSMatthew G Knepley 
44096636e97aSMatthew G Knepley /*@
44106636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
44116636e97aSMatthew G Knepley 
44126636e97aSMatthew G Knepley   Not Collective
44136636e97aSMatthew G Knepley 
44146636e97aSMatthew G Knepley   Input Parameter:
44156636e97aSMatthew G Knepley . dm - the DM
44166636e97aSMatthew G Knepley 
44176636e97aSMatthew G Knepley   Output Parameter:
44186636e97aSMatthew G Knepley . c - global coordinate vector
44196636e97aSMatthew G Knepley 
44206636e97aSMatthew G Knepley   Note:
44216636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
44226636e97aSMatthew G Knepley 
44236636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
44246636e97aSMatthew G Knepley 
44256636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
44266636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
44276636e97aSMatthew G Knepley 
44286636e97aSMatthew G Knepley   Level: intermediate
44296636e97aSMatthew G Knepley 
44306636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44316636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
44326636e97aSMatthew G Knepley @*/
44336636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
44346636e97aSMatthew G Knepley {
44356636e97aSMatthew G Knepley   PetscErrorCode ierr;
44366636e97aSMatthew G Knepley 
44376636e97aSMatthew G Knepley   PetscFunctionBegin;
44386636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44396636e97aSMatthew G Knepley   PetscValidPointer(c,2);
44401f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
44410298fd71SBarry Smith     DM cdm = NULL;
44426636e97aSMatthew G Knepley 
44436636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
44446636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
44456636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
44466636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
44476636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
44486636e97aSMatthew G Knepley   }
44496636e97aSMatthew G Knepley   *c = dm->coordinates;
44506636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44516636e97aSMatthew G Knepley }
44526636e97aSMatthew G Knepley 
44536636e97aSMatthew G Knepley /*@
44546636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
44556636e97aSMatthew G Knepley 
44566636e97aSMatthew G Knepley   Collective on DM
44576636e97aSMatthew G Knepley 
44586636e97aSMatthew G Knepley   Input Parameter:
44596636e97aSMatthew G Knepley . dm - the DM
44606636e97aSMatthew G Knepley 
44616636e97aSMatthew G Knepley   Output Parameter:
44626636e97aSMatthew G Knepley . c - coordinate vector
44636636e97aSMatthew G Knepley 
44646636e97aSMatthew G Knepley   Note:
44656636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
44666636e97aSMatthew G Knepley 
44676636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
44686636e97aSMatthew G Knepley 
44696636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
44706636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
44716636e97aSMatthew G Knepley 
44726636e97aSMatthew G Knepley   Level: intermediate
44736636e97aSMatthew G Knepley 
44746636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44756636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
44766636e97aSMatthew G Knepley @*/
44776636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
44786636e97aSMatthew G Knepley {
44796636e97aSMatthew G Knepley   PetscErrorCode ierr;
44806636e97aSMatthew G Knepley 
44816636e97aSMatthew G Knepley   PetscFunctionBegin;
44826636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44836636e97aSMatthew G Knepley   PetscValidPointer(c,2);
44841f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
44850298fd71SBarry Smith     DM cdm = NULL;
44866636e97aSMatthew G Knepley 
44876636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
44886636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
44896636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
44906636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
44916636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
44926636e97aSMatthew G Knepley   }
44936636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
44946636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44956636e97aSMatthew G Knepley }
44966636e97aSMatthew G Knepley 
4497f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
4498f19dbd58SToby Isaac {
4499f19dbd58SToby Isaac   PetscErrorCode ierr;
4500f19dbd58SToby Isaac 
4501f19dbd58SToby Isaac   PetscFunctionBegin;
4502f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4503f19dbd58SToby Isaac   PetscValidPointer(field,2);
4504f19dbd58SToby Isaac   if (!dm->coordinateField) {
4505f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
4506f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
4507f19dbd58SToby Isaac     }
4508f19dbd58SToby Isaac   }
4509f19dbd58SToby Isaac   *field = dm->coordinateField;
4510f19dbd58SToby Isaac   PetscFunctionReturn(0);
4511f19dbd58SToby Isaac }
4512f19dbd58SToby Isaac 
4513f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
4514f19dbd58SToby Isaac {
4515f19dbd58SToby Isaac   PetscErrorCode ierr;
4516f19dbd58SToby Isaac 
4517f19dbd58SToby Isaac   PetscFunctionBegin;
4518f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4519f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
4520c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
4521f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
4522f19dbd58SToby Isaac   dm->coordinateField = field;
4523f19dbd58SToby Isaac   PetscFunctionReturn(0);
4524f19dbd58SToby Isaac }
4525f19dbd58SToby Isaac 
45266636e97aSMatthew G Knepley /*@
45271cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
45286636e97aSMatthew G Knepley 
45296636e97aSMatthew G Knepley   Collective on DM
45306636e97aSMatthew G Knepley 
45316636e97aSMatthew G Knepley   Input Parameter:
45326636e97aSMatthew G Knepley . dm - the DM
45336636e97aSMatthew G Knepley 
45346636e97aSMatthew G Knepley   Output Parameter:
45356636e97aSMatthew G Knepley . cdm - coordinate DM
45366636e97aSMatthew G Knepley 
45376636e97aSMatthew G Knepley   Level: intermediate
45386636e97aSMatthew G Knepley 
45396636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45401cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
45416636e97aSMatthew G Knepley @*/
45426636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
45436636e97aSMatthew G Knepley {
45446636e97aSMatthew G Knepley   PetscErrorCode ierr;
45456636e97aSMatthew G Knepley 
45466636e97aSMatthew G Knepley   PetscFunctionBegin;
45476636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45486636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
45496636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
455082f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
45516636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
45526636e97aSMatthew G Knepley   }
45536636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
45546636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45556636e97aSMatthew G Knepley }
4556e87bb0d3SMatthew G Knepley 
45571cfe2091SMatthew G. Knepley /*@
45581cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
45591cfe2091SMatthew G. Knepley 
45601cfe2091SMatthew G. Knepley   Logically Collective on DM
45611cfe2091SMatthew G. Knepley 
45621cfe2091SMatthew G. Knepley   Input Parameters:
45631cfe2091SMatthew G. Knepley + dm - the DM
45641cfe2091SMatthew G. Knepley - cdm - coordinate DM
45651cfe2091SMatthew G. Knepley 
45661cfe2091SMatthew G. Knepley   Level: intermediate
45671cfe2091SMatthew G. Knepley 
45681cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45691cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
45701cfe2091SMatthew G. Knepley @*/
45711cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
45721cfe2091SMatthew G. Knepley {
45731cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
45741cfe2091SMatthew G. Knepley 
45751cfe2091SMatthew G. Knepley   PetscFunctionBegin;
45761cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45771cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4578f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
45791cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
45801cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
45811cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
45821cfe2091SMatthew G. Knepley }
45831cfe2091SMatthew G. Knepley 
458446e270d4SMatthew G. Knepley /*@
458546e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
458646e270d4SMatthew G. Knepley 
458746e270d4SMatthew G. Knepley   Not Collective
458846e270d4SMatthew G. Knepley 
458946e270d4SMatthew G. Knepley   Input Parameter:
459046e270d4SMatthew G. Knepley . dm - The DM object
459146e270d4SMatthew G. Knepley 
459246e270d4SMatthew G. Knepley   Output Parameter:
459346e270d4SMatthew G. Knepley . dim - The embedding dimension
459446e270d4SMatthew G. Knepley 
459546e270d4SMatthew G. Knepley   Level: intermediate
459646e270d4SMatthew G. Knepley 
459746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4598e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
459946e270d4SMatthew G. Knepley @*/
460046e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
460146e270d4SMatthew G. Knepley {
460246e270d4SMatthew G. Knepley   PetscFunctionBegin;
460346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
460446e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
46059a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
46069a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
46079a9a41abSToby Isaac   }
460846e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
460946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
461046e270d4SMatthew G. Knepley }
461146e270d4SMatthew G. Knepley 
461246e270d4SMatthew G. Knepley /*@
461346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
461446e270d4SMatthew G. Knepley 
461546e270d4SMatthew G. Knepley   Not Collective
461646e270d4SMatthew G. Knepley 
461746e270d4SMatthew G. Knepley   Input Parameters:
461846e270d4SMatthew G. Knepley + dm  - The DM object
461946e270d4SMatthew G. Knepley - dim - The embedding dimension
462046e270d4SMatthew G. Knepley 
462146e270d4SMatthew G. Knepley   Level: intermediate
462246e270d4SMatthew G. Knepley 
462346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4624e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
462546e270d4SMatthew G. Knepley @*/
462646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
462746e270d4SMatthew G. Knepley {
4628f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4629f17e8794SMatthew G. Knepley 
463046e270d4SMatthew G. Knepley   PetscFunctionBegin;
463146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
463246e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4633f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
463446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
463546e270d4SMatthew G. Knepley }
463646e270d4SMatthew G. Knepley 
4637e8abe2deSMatthew G. Knepley /*@
4638e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4639e8abe2deSMatthew G. Knepley 
4640e8abe2deSMatthew G. Knepley   Not Collective
4641e8abe2deSMatthew G. Knepley 
4642e8abe2deSMatthew G. Knepley   Input Parameter:
4643e8abe2deSMatthew G. Knepley . dm - The DM object
4644e8abe2deSMatthew G. Knepley 
4645e8abe2deSMatthew G. Knepley   Output Parameter:
4646e8abe2deSMatthew G. Knepley . section - The PetscSection object
4647e8abe2deSMatthew G. Knepley 
4648e8abe2deSMatthew G. Knepley   Level: intermediate
4649e8abe2deSMatthew G. Knepley 
4650e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4651e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
4652e8abe2deSMatthew G. Knepley @*/
4653e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4654e8abe2deSMatthew G. Knepley {
4655e8abe2deSMatthew G. Knepley   DM             cdm;
4656e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4657e8abe2deSMatthew G. Knepley 
4658e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4659e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4660e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4661e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4662e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
4663e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4664e8abe2deSMatthew G. Knepley }
4665e8abe2deSMatthew G. Knepley 
4666e8abe2deSMatthew G. Knepley /*@
4667e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4668e8abe2deSMatthew G. Knepley 
4669e8abe2deSMatthew G. Knepley   Not Collective
4670e8abe2deSMatthew G. Knepley 
4671e8abe2deSMatthew G. Knepley   Input Parameters:
4672e8abe2deSMatthew G. Knepley + dm      - The DM object
467346e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4674e8abe2deSMatthew G. Knepley - section - The PetscSection object
4675e8abe2deSMatthew G. Knepley 
4676e8abe2deSMatthew G. Knepley   Level: intermediate
4677e8abe2deSMatthew G. Knepley 
4678e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4679e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
4680e8abe2deSMatthew G. Knepley @*/
468146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4682e8abe2deSMatthew G. Knepley {
4683e8abe2deSMatthew G. Knepley   DM             cdm;
4684e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4685e8abe2deSMatthew G. Knepley 
4686e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4687e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
468846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4689e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4690e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
469146e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
46924c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
469346e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
469446e270d4SMatthew G. Knepley 
469546e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
469646e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
469746e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
469846e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
469946e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
470046e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
470146e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
470246e270d4SMatthew G. Knepley     }
47036be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
470446e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
470546e270d4SMatthew G. Knepley   }
4706e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4707e8abe2deSMatthew G. Knepley }
4708e8abe2deSMatthew G. Knepley 
47095dc8c3f7SMatthew G. Knepley /*@C
471090b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
47115dc8c3f7SMatthew G. Knepley 
47125dc8c3f7SMatthew G. Knepley   Input Parameters:
471390b157c4SStefano Zampini . dm      - The DM object
471490b157c4SStefano Zampini 
471590b157c4SStefano Zampini   Output Parameters:
471690b157c4SStefano Zampini + per     - Whether the DM is periodic or not
47175dc8c3f7SMatthew 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
47185dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
47195dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
47205dc8c3f7SMatthew G. Knepley 
47215dc8c3f7SMatthew G. Knepley   Level: developer
47225dc8c3f7SMatthew G. Knepley 
47235dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
47245dc8c3f7SMatthew G. Knepley @*/
472590b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4726c6b900c6SMatthew G. Knepley {
4727c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4728c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
472990b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4730c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4731c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
47325dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4733c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4734c6b900c6SMatthew G. Knepley }
4735c6b900c6SMatthew G. Knepley 
47365dc8c3f7SMatthew G. Knepley /*@C
47375dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
47385dc8c3f7SMatthew G. Knepley 
47395dc8c3f7SMatthew G. Knepley   Input Parameters:
47405dc8c3f7SMatthew G. Knepley + dm      - The DM object
474190b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
47425dc8c3f7SMatthew 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
47435dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
47445dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
47455dc8c3f7SMatthew G. Knepley 
47465dc8c3f7SMatthew G. Knepley   Level: developer
47475dc8c3f7SMatthew G. Knepley 
47485dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
47495dc8c3f7SMatthew G. Knepley @*/
475090b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4751c6b900c6SMatthew G. Knepley {
4752c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4753c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4754c6b900c6SMatthew G. Knepley 
4755c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4756c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
475790b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
475890b157c4SStefano Zampini   if (maxCell) {
475990b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
476090b157c4SStefano Zampini     PetscValidPointer(L,4);
476190b157c4SStefano Zampini     PetscValidPointer(bd,5);
476290b157c4SStefano Zampini   }
47635dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
47645dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
476590b157c4SStefano Zampini   if (maxCell) {
47665dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
47675dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
476890b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
476990b157c4SStefano Zampini   } else {
477090b157c4SStefano Zampini     dm->periodic = per;
477190b157c4SStefano Zampini   }
4772c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4773c6b900c6SMatthew G. Knepley }
4774c6b900c6SMatthew G. Knepley 
47752e17dfb7SMatthew G. Knepley /*@
47762e17dfb7SMatthew 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.
47772e17dfb7SMatthew G. Knepley 
47782e17dfb7SMatthew G. Knepley   Input Parameters:
47792e17dfb7SMatthew G. Knepley + dm     - The DM
478065da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
478165da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
47822e17dfb7SMatthew G. Knepley 
47832e17dfb7SMatthew G. Knepley   Output Parameter:
47842e17dfb7SMatthew G. Knepley . out - The localized coordinate point
47852e17dfb7SMatthew G. Knepley 
47862e17dfb7SMatthew G. Knepley   Level: developer
47872e17dfb7SMatthew G. Knepley 
47882e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
47892e17dfb7SMatthew G. Knepley @*/
479065da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
47912e17dfb7SMatthew G. Knepley {
47922e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
47932e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
47942e17dfb7SMatthew G. Knepley 
47952e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47962e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
47972e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47982e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
47992e17dfb7SMatthew G. Knepley   } else {
480065da65dcSMatthew G. Knepley     if (endpoint) {
480165da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4802da3333bfSMatthew 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)) {
4803da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
480465da65dcSMatthew G. Knepley         } else {
4805da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
480665da65dcSMatthew G. Knepley         }
480765da65dcSMatthew G. Knepley       }
480865da65dcSMatthew G. Knepley     } else {
48092e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
48101118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
48112e17dfb7SMatthew G. Knepley       }
48122e17dfb7SMatthew G. Knepley     }
481365da65dcSMatthew G. Knepley   }
48142e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48152e17dfb7SMatthew G. Knepley }
48162e17dfb7SMatthew G. Knepley 
48172e17dfb7SMatthew G. Knepley /*
48182e17dfb7SMatthew 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.
48192e17dfb7SMatthew G. Knepley 
48202e17dfb7SMatthew G. Knepley   Input Parameters:
48212e17dfb7SMatthew G. Knepley + dm     - The DM
48222e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
48232e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
48242e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
48252e17dfb7SMatthew G. Knepley 
48262e17dfb7SMatthew G. Knepley   Output Parameter:
48272e17dfb7SMatthew G. Knepley . out - The localized coordinate point
48282e17dfb7SMatthew G. Knepley 
48292e17dfb7SMatthew G. Knepley   Level: developer
48302e17dfb7SMatthew G. Knepley 
48312e17dfb7SMatthew 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
48322e17dfb7SMatthew G. Knepley 
48332e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
48342e17dfb7SMatthew G. Knepley */
48352e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
48362e17dfb7SMatthew G. Knepley {
48372e17dfb7SMatthew G. Knepley   PetscInt d;
48382e17dfb7SMatthew G. Knepley 
48392e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48402e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48412e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
48422e17dfb7SMatthew G. Knepley   } else {
48432e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4844908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
48452e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
48462e17dfb7SMatthew G. Knepley       } else {
48472e17dfb7SMatthew G. Knepley         out[d] = in[d];
48482e17dfb7SMatthew G. Knepley       }
48492e17dfb7SMatthew G. Knepley     }
48502e17dfb7SMatthew G. Knepley   }
48512e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48522e17dfb7SMatthew G. Knepley }
48532e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
48542e17dfb7SMatthew G. Knepley {
48552e17dfb7SMatthew G. Knepley   PetscInt d;
48562e17dfb7SMatthew G. Knepley 
48572e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48582e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48592e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
48602e17dfb7SMatthew G. Knepley   } else {
48612e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4862908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
48632e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
48642e17dfb7SMatthew G. Knepley       } else {
48652e17dfb7SMatthew G. Knepley         out[d] = in[d];
48662e17dfb7SMatthew G. Knepley       }
48672e17dfb7SMatthew G. Knepley     }
48682e17dfb7SMatthew G. Knepley   }
48692e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48702e17dfb7SMatthew G. Knepley }
48712e17dfb7SMatthew G. Knepley 
48722e17dfb7SMatthew G. Knepley /*
48732e17dfb7SMatthew 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.
48742e17dfb7SMatthew G. Knepley 
48752e17dfb7SMatthew G. Knepley   Input Parameters:
48762e17dfb7SMatthew G. Knepley + dm     - The DM
48772e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
48782e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
48792e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
48802e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
48812e17dfb7SMatthew G. Knepley 
48822e17dfb7SMatthew G. Knepley   Output Parameter:
48832e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
48842e17dfb7SMatthew G. Knepley 
48852e17dfb7SMatthew G. Knepley   Level: developer
48862e17dfb7SMatthew G. Knepley 
48872e17dfb7SMatthew 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
48882e17dfb7SMatthew G. Knepley 
48892e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
48902e17dfb7SMatthew G. Knepley */
48912e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
48922e17dfb7SMatthew G. Knepley {
48932e17dfb7SMatthew G. Knepley   PetscInt d;
48942e17dfb7SMatthew G. Knepley 
48952e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48962e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48972e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
48982e17dfb7SMatthew G. Knepley   } else {
48992e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4900908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
49012e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
49022e17dfb7SMatthew G. Knepley       } else {
49032e17dfb7SMatthew G. Knepley         out[d] += in[d];
49042e17dfb7SMatthew G. Knepley       }
49052e17dfb7SMatthew G. Knepley     }
49062e17dfb7SMatthew G. Knepley   }
49072e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
49082e17dfb7SMatthew G. Knepley }
49092e17dfb7SMatthew G. Knepley 
491036447a5eSToby Isaac /*@
491136447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
491236447a5eSToby Isaac 
491336447a5eSToby Isaac   Input Parameter:
491436447a5eSToby Isaac . dm - The DM
491536447a5eSToby Isaac 
491636447a5eSToby Isaac   Output Parameter:
491736447a5eSToby Isaac   areLocalized - True if localized
491836447a5eSToby Isaac 
491936447a5eSToby Isaac   Level: developer
492036447a5eSToby Isaac 
492136447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
492236447a5eSToby Isaac @*/
492336447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
492436447a5eSToby Isaac {
492536447a5eSToby Isaac   DM             cdm;
492636447a5eSToby Isaac   PetscSection   coordSection;
492746a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
492846a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
492936447a5eSToby Isaac   PetscErrorCode ierr;
493036447a5eSToby Isaac 
493136447a5eSToby Isaac   PetscFunctionBegin;
493236447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
493346a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
493446a3a80fSLisandro Dalcin 
49358b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
493646a3a80fSLisandro Dalcin   if (!dm->periodic) PetscFunctionReturn(0); /* This is a hideous optimization hack! */
493746a3a80fSLisandro Dalcin 
493836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
493936447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
494036447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
494146a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
494246a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
494346a3a80fSLisandro Dalcin 
494446a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
494536447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
494636447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
494746a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
494846a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
494936447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
495046a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
495136447a5eSToby Isaac   }
495246a3a80fSLisandro Dalcin   ierr = MPIU_Allreduce(&alreadyLocalized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
495336447a5eSToby Isaac   PetscFunctionReturn(0);
495436447a5eSToby Isaac }
495536447a5eSToby Isaac 
495636447a5eSToby Isaac 
49572e17dfb7SMatthew G. Knepley /*@
4958492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
49592e17dfb7SMatthew G. Knepley 
49602e17dfb7SMatthew G. Knepley   Input Parameter:
49612e17dfb7SMatthew G. Knepley . dm - The DM
49622e17dfb7SMatthew G. Knepley 
49632e17dfb7SMatthew G. Knepley   Level: developer
49642e17dfb7SMatthew G. Knepley 
49652e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
49662e17dfb7SMatthew G. Knepley @*/
49672e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
49682e17dfb7SMatthew G. Knepley {
49692e17dfb7SMatthew G. Knepley   DM             cdm;
49702e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
49712e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
49723e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
49733e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4974e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
49753e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
49763e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
49772e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
49782e17dfb7SMatthew G. Knepley 
49792e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
49802e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
498192c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
4982f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
4983f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
4984f7cbd40bSStefano Zampini 
49852e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
49862e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
49872e17dfb7SMatthew G. Knepley   {
49882e17dfb7SMatthew G. Knepley     PetscBool isplex;
49892e17dfb7SMatthew G. Knepley 
49902e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
49912e17dfb7SMatthew G. Knepley     if (isplex) {
49922e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
49933e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
499469291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
49953e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
49963e922f36SToby Isaac       newStart = vStart;
49973e922f36SToby Isaac       newEnd   = vEnd;
49983e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
49993e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
50003e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
50013e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
50023e922f36SToby Isaac       }
50032e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
50042e17dfb7SMatthew G. Knepley   }
50052e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
500643eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
50072e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
50083e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
5009e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
50103e922f36SToby Isaac 
50112e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
50122e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
50132e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
50142e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
50153e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
50163e922f36SToby Isaac 
501769291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
50183e922f36SToby Isaac   localized = &anchor[bs];
50193e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
50203e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
50213e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
50223e922f36SToby Isaac 
50233e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
50243e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
50253e922f36SToby Isaac       PetscInt     b;
50263e922f36SToby Isaac 
50273e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
50283e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50293e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
50303e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
50313e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
50323e922f36SToby Isaac         for (b = 0; b < bs; b++) {
50333e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
50343e922f36SToby Isaac         }
50353e922f36SToby Isaac         if (b < bs) break;
50363e922f36SToby Isaac       }
50373e922f36SToby Isaac       if (d < dof/bs) {
50383e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
50393e922f36SToby Isaac           PetscInt cdof;
50403e922f36SToby Isaac 
50413e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
50423e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
50433e922f36SToby Isaac         }
50443e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
50453e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
50463e922f36SToby Isaac       }
50473e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50483e922f36SToby Isaac     }
50493e922f36SToby Isaac   }
50503e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
50513e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
505269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
50533e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
505469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
50553e922f36SToby Isaac     PetscFunctionReturn(0);
50563e922f36SToby Isaac   }
50572e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
50582e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
50592e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
50602e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
50612e17dfb7SMatthew G. Knepley   }
50622e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
50632e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5064c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
50652e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
50662e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
50672e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
50682e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5069c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
50702e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
50712e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
50722e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
50732e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
50742e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
50752e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
50762e17dfb7SMatthew G. Knepley   }
50773e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
50783e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
50793e922f36SToby Isaac 
50802e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
50812e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
50823e922f36SToby Isaac       PetscInt     b, cdof;
50832e17dfb7SMatthew G. Knepley 
50843e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
50853e922f36SToby Isaac       if (!cdof) continue;
50862e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50872e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
50882e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
50892e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
50902e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50912e17dfb7SMatthew G. Knepley     }
50923e922f36SToby Isaac   }
509369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
509469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5095c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
50962e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
50972e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
50982e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
50992e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
51002e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
51012e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
51022e17dfb7SMatthew G. Knepley }
51032e17dfb7SMatthew G. Knepley 
5104e87bb0d3SMatthew G Knepley /*@
51053a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5106e87bb0d3SMatthew G Knepley 
51073a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5108e87bb0d3SMatthew G Knepley 
5109e87bb0d3SMatthew G Knepley   Input Parameters:
5110e87bb0d3SMatthew G Knepley + dm - The DM
51113a93e3b7SToby Isaac . v - The Vec of points
511262a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
51133a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5114e87bb0d3SMatthew G Knepley 
511561e3bb9bSMatthew G Knepley   Output Parameter:
511662a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
511762a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
51183a93e3b7SToby Isaac 
5119e87bb0d3SMatthew G Knepley 
5120e87bb0d3SMatthew G Knepley   Level: developer
512161e3bb9bSMatthew G Knepley 
512262a38674SMatthew G. Knepley   Notes:
51233a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
512462a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
51253a93e3b7SToby Isaac 
51263a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
512762a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
51283a93e3b7SToby Isaac 
51293a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
51303a93e3b7SToby Isaac 
513162a38674SMatthew G. Knepley $    const PetscSFNode *cells;
513262a38674SMatthew G. Knepley $    PetscInt           nFound;
5133a6216909SToby Isaac $    const PetscInt    *found;
513462a38674SMatthew G. Knepley $
5135a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
51363a93e3b7SToby Isaac 
51373a93e3b7SToby 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
51383a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
51393a93e3b7SToby Isaac 
514061e3bb9bSMatthew G Knepley .keywords: point location, mesh
514162a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
514261e3bb9bSMatthew G Knepley @*/
514362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5144e87bb0d3SMatthew G Knepley {
5145735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5146735aa83eSMatthew G Knepley 
5147e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5148e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5149e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5150e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
51513a93e3b7SToby Isaac   if (*cellSF) {
51523a93e3b7SToby Isaac     PetscMPIInt result;
51533a93e3b7SToby Isaac 
5154e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5155a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
51563a93e3b7SToby 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");
5157e0fc9d1bSMatthew G. Knepley   } else {
51583a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
51593a93e3b7SToby Isaac   }
516047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5161735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
516262a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
516382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
516447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5165e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5166e87bb0d3SMatthew G Knepley }
516714f150ffSMatthew G. Knepley 
5168f4d763aaSMatthew G. Knepley /*@
5169f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5170f4d763aaSMatthew G. Knepley 
5171f4d763aaSMatthew G. Knepley   Input Parameter:
5172f4d763aaSMatthew G. Knepley . dm - The original DM
5173f4d763aaSMatthew G. Knepley 
5174f4d763aaSMatthew G. Knepley   Output Parameter:
5175f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5176f4d763aaSMatthew G. Knepley 
5177f4d763aaSMatthew G. Knepley   Level: intermediate
5178f4d763aaSMatthew G. Knepley 
5179e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
5180f4d763aaSMatthew G. Knepley @*/
518114f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
518214f150ffSMatthew G. Knepley {
5183c26acbdeSMatthew G. Knepley   PetscSection   section;
51842d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
518514f150ffSMatthew G. Knepley   PetscErrorCode ierr;
518614f150ffSMatthew G. Knepley 
518714f150ffSMatthew G. Knepley   PetscFunctionBegin;
518814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
518914f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5190e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5191c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5192127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
51932d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5194c26acbdeSMatthew G. Knepley     *odm = dm;
5195c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5196c26acbdeSMatthew G. Knepley   }
519714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
51980305aff6SMatthew G. Knepley     PetscDS      ds;
5199c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
520014f150ffSMatthew G. Knepley     PetscSF      sf;
520114f150ffSMatthew G. Knepley 
520214f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
52030305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
52040305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
520514f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5206e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
520714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
520814f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
520915b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5210e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
521114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
521214f150ffSMatthew G. Knepley   }
521314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
521414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
521514f150ffSMatthew G. Knepley }
5216f4d763aaSMatthew G. Knepley 
5217f4d763aaSMatthew G. Knepley /*@
5218cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5219f4d763aaSMatthew G. Knepley 
5220f4d763aaSMatthew G. Knepley   Input Parameter:
5221f4d763aaSMatthew G. Knepley . dm - The original DM
5222f4d763aaSMatthew G. Knepley 
5223cdb7a50dSMatthew G. Knepley   Output Parameters:
5224cdb7a50dSMatthew G. Knepley + num - The output sequence number
5225cdb7a50dSMatthew G. Knepley - val - The output sequence value
5226f4d763aaSMatthew G. Knepley 
5227f4d763aaSMatthew G. Knepley   Level: intermediate
5228f4d763aaSMatthew G. Knepley 
5229f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5230f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5231f4d763aaSMatthew G. Knepley 
5232f4d763aaSMatthew G. Knepley .seealso: VecView()
5233f4d763aaSMatthew G. Knepley @*/
5234cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5235f4d763aaSMatthew G. Knepley {
5236f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5237f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5238cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5239cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5240f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5241f4d763aaSMatthew G. Knepley }
5242f4d763aaSMatthew G. Knepley 
5243f4d763aaSMatthew G. Knepley /*@
5244cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5245f4d763aaSMatthew G. Knepley 
5246f4d763aaSMatthew G. Knepley   Input Parameters:
5247f4d763aaSMatthew G. Knepley + dm - The original DM
5248cdb7a50dSMatthew G. Knepley . num - The output sequence number
5249cdb7a50dSMatthew G. Knepley - val - The output sequence value
5250f4d763aaSMatthew G. Knepley 
5251f4d763aaSMatthew G. Knepley   Level: intermediate
5252f4d763aaSMatthew G. Knepley 
5253f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5254f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5255f4d763aaSMatthew G. Knepley 
5256f4d763aaSMatthew G. Knepley .seealso: VecView()
5257f4d763aaSMatthew G. Knepley @*/
5258cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5259f4d763aaSMatthew G. Knepley {
5260f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5261f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5262f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5263cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5264cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5265cdb7a50dSMatthew G. Knepley }
5266cdb7a50dSMatthew G. Knepley 
5267cdb7a50dSMatthew G. Knepley /*@C
5268cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5269cdb7a50dSMatthew G. Knepley 
5270cdb7a50dSMatthew G. Knepley   Input Parameters:
5271cdb7a50dSMatthew G. Knepley + dm   - The original DM
5272cdb7a50dSMatthew G. Knepley . name - The sequence name
5273cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5274cdb7a50dSMatthew G. Knepley 
5275cdb7a50dSMatthew G. Knepley   Output Parameter:
5276cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5277cdb7a50dSMatthew G. Knepley 
5278cdb7a50dSMatthew G. Knepley   Level: intermediate
5279cdb7a50dSMatthew G. Knepley 
5280cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5281cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5282cdb7a50dSMatthew G. Knepley 
5283cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5284cdb7a50dSMatthew G. Knepley @*/
5285cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5286cdb7a50dSMatthew G. Knepley {
5287cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5288cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5289cdb7a50dSMatthew G. Knepley 
5290cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5291cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5292cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5293cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5294cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5295cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5296cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5297cdb7a50dSMatthew G. Knepley     PetscScalar value;
5298cdb7a50dSMatthew G. Knepley 
529939d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
53004aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5301cdb7a50dSMatthew G. Knepley #endif
5302cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5303f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5304f4d763aaSMatthew G. Knepley }
53058e4ac7eaSMatthew G. Knepley 
53068e4ac7eaSMatthew G. Knepley /*@
53078e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
53088e4ac7eaSMatthew G. Knepley 
53098e4ac7eaSMatthew G. Knepley   Not collective
53108e4ac7eaSMatthew G. Knepley 
53118e4ac7eaSMatthew G. Knepley   Input Parameter:
53128e4ac7eaSMatthew G. Knepley . dm - The DM
53138e4ac7eaSMatthew G. Knepley 
53148e4ac7eaSMatthew G. Knepley   Output Parameter:
53158e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
53168e4ac7eaSMatthew G. Knepley 
53178e4ac7eaSMatthew G. Knepley   Level: beginner
53188e4ac7eaSMatthew G. Knepley 
53198e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
53208e4ac7eaSMatthew G. Knepley @*/
53218e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
53228e4ac7eaSMatthew G. Knepley {
53238e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
53248e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53258e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
53268e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
53278e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
53288e4ac7eaSMatthew G. Knepley }
53298e4ac7eaSMatthew G. Knepley 
53308e4ac7eaSMatthew G. Knepley /*@
53318e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
53328e4ac7eaSMatthew G. Knepley 
53338e4ac7eaSMatthew G. Knepley   Collective on dm
53348e4ac7eaSMatthew G. Knepley 
53358e4ac7eaSMatthew G. Knepley   Input Parameters:
53368e4ac7eaSMatthew G. Knepley + dm - The DM
53378e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
53388e4ac7eaSMatthew G. Knepley 
53398e4ac7eaSMatthew G. Knepley   Level: beginner
53408e4ac7eaSMatthew G. Knepley 
53418e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
53428e4ac7eaSMatthew G. Knepley @*/
53438e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
53448e4ac7eaSMatthew G. Knepley {
53458e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
53468e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53478833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
53488e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
53498e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
53508e4ac7eaSMatthew G. Knepley }
5351c58f1c22SToby Isaac 
5352c58f1c22SToby Isaac 
5353c58f1c22SToby Isaac /*@C
5354c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5355c58f1c22SToby Isaac 
5356c58f1c22SToby Isaac   Not Collective
5357c58f1c22SToby Isaac 
5358c58f1c22SToby Isaac   Input Parameters:
5359c58f1c22SToby Isaac + dm   - The DM object
5360c58f1c22SToby Isaac - name - The label name
5361c58f1c22SToby Isaac 
5362c58f1c22SToby Isaac   Level: intermediate
5363c58f1c22SToby Isaac 
5364c58f1c22SToby Isaac .keywords: mesh
5365c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5366c58f1c22SToby Isaac @*/
5367c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5368c58f1c22SToby Isaac {
5369c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5370c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5371c58f1c22SToby Isaac   PetscErrorCode ierr;
5372c58f1c22SToby Isaac 
5373c58f1c22SToby Isaac   PetscFunctionBegin;
5374c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5375c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5376c58f1c22SToby Isaac   while (next) {
5377c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5378c58f1c22SToby Isaac     if (flg) break;
5379c58f1c22SToby Isaac     next = next->next;
5380c58f1c22SToby Isaac   }
5381c58f1c22SToby Isaac   if (!flg) {
5382c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5383c58f1c22SToby Isaac 
5384c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5385c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5386c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5387c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5388c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5389c58f1c22SToby Isaac   }
5390c58f1c22SToby Isaac   PetscFunctionReturn(0);
5391c58f1c22SToby Isaac }
5392c58f1c22SToby Isaac 
5393c58f1c22SToby Isaac /*@C
5394c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5395c58f1c22SToby Isaac 
5396c58f1c22SToby Isaac   Not Collective
5397c58f1c22SToby Isaac 
5398c58f1c22SToby Isaac   Input Parameters:
5399c58f1c22SToby Isaac + dm   - The DM object
5400c58f1c22SToby Isaac . name - The label name
5401c58f1c22SToby Isaac - point - The mesh point
5402c58f1c22SToby Isaac 
5403c58f1c22SToby Isaac   Output Parameter:
5404c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5405c58f1c22SToby Isaac 
5406c58f1c22SToby Isaac   Level: beginner
5407c58f1c22SToby Isaac 
5408c58f1c22SToby Isaac .keywords: mesh
5409c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5410c58f1c22SToby Isaac @*/
5411c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5412c58f1c22SToby Isaac {
5413c58f1c22SToby Isaac   DMLabel        label;
5414c58f1c22SToby Isaac   PetscErrorCode ierr;
5415c58f1c22SToby Isaac 
5416c58f1c22SToby Isaac   PetscFunctionBegin;
5417c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5418c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5419c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
542013903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5421c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5422c58f1c22SToby Isaac   PetscFunctionReturn(0);
5423c58f1c22SToby Isaac }
5424c58f1c22SToby Isaac 
5425c58f1c22SToby Isaac /*@C
5426c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5427c58f1c22SToby Isaac 
5428c58f1c22SToby Isaac   Not Collective
5429c58f1c22SToby Isaac 
5430c58f1c22SToby Isaac   Input Parameters:
5431c58f1c22SToby Isaac + dm   - The DM object
5432c58f1c22SToby Isaac . name - The label name
5433c58f1c22SToby Isaac . point - The mesh point
5434c58f1c22SToby Isaac - value - The label value for this point
5435c58f1c22SToby Isaac 
5436c58f1c22SToby Isaac   Output Parameter:
5437c58f1c22SToby Isaac 
5438c58f1c22SToby Isaac   Level: beginner
5439c58f1c22SToby Isaac 
5440c58f1c22SToby Isaac .keywords: mesh
5441c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5442c58f1c22SToby Isaac @*/
5443c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5444c58f1c22SToby Isaac {
5445c58f1c22SToby Isaac   DMLabel        label;
5446c58f1c22SToby Isaac   PetscErrorCode ierr;
5447c58f1c22SToby Isaac 
5448c58f1c22SToby Isaac   PetscFunctionBegin;
5449c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5450c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5451c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5452c58f1c22SToby Isaac   if (!label) {
5453c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5454c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5455c58f1c22SToby Isaac   }
5456c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5457c58f1c22SToby Isaac   PetscFunctionReturn(0);
5458c58f1c22SToby Isaac }
5459c58f1c22SToby Isaac 
5460c58f1c22SToby Isaac /*@C
5461c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5462c58f1c22SToby Isaac 
5463c58f1c22SToby Isaac   Not Collective
5464c58f1c22SToby Isaac 
5465c58f1c22SToby Isaac   Input Parameters:
5466c58f1c22SToby Isaac + dm   - The DM object
5467c58f1c22SToby Isaac . name - The label name
5468c58f1c22SToby Isaac . point - The mesh point
5469c58f1c22SToby Isaac - value - The label value for this point
5470c58f1c22SToby Isaac 
5471c58f1c22SToby Isaac   Output Parameter:
5472c58f1c22SToby Isaac 
5473c58f1c22SToby Isaac   Level: beginner
5474c58f1c22SToby Isaac 
5475c58f1c22SToby Isaac .keywords: mesh
5476c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5477c58f1c22SToby Isaac @*/
5478c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5479c58f1c22SToby Isaac {
5480c58f1c22SToby Isaac   DMLabel        label;
5481c58f1c22SToby Isaac   PetscErrorCode ierr;
5482c58f1c22SToby Isaac 
5483c58f1c22SToby Isaac   PetscFunctionBegin;
5484c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5485c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5486c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5487c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5488c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5489c58f1c22SToby Isaac   PetscFunctionReturn(0);
5490c58f1c22SToby Isaac }
5491c58f1c22SToby Isaac 
5492c58f1c22SToby Isaac /*@C
5493c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5494c58f1c22SToby Isaac 
5495c58f1c22SToby Isaac   Not Collective
5496c58f1c22SToby Isaac 
5497c58f1c22SToby Isaac   Input Parameters:
5498c58f1c22SToby Isaac + dm   - The DM object
5499c58f1c22SToby Isaac - name - The label name
5500c58f1c22SToby Isaac 
5501c58f1c22SToby Isaac   Output Parameter:
5502c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5503c58f1c22SToby Isaac 
5504c58f1c22SToby Isaac   Level: beginner
5505c58f1c22SToby Isaac 
5506c58f1c22SToby Isaac .keywords: mesh
5507c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5508c58f1c22SToby Isaac @*/
5509c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5510c58f1c22SToby Isaac {
5511c58f1c22SToby Isaac   DMLabel        label;
5512c58f1c22SToby Isaac   PetscErrorCode ierr;
5513c58f1c22SToby Isaac 
5514c58f1c22SToby Isaac   PetscFunctionBegin;
5515c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5516c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5517c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5518c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5519c58f1c22SToby Isaac   *size = 0;
5520c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5521c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5522c58f1c22SToby Isaac   PetscFunctionReturn(0);
5523c58f1c22SToby Isaac }
5524c58f1c22SToby Isaac 
5525c58f1c22SToby Isaac /*@C
5526c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5527c58f1c22SToby Isaac 
5528c58f1c22SToby Isaac   Not Collective
5529c58f1c22SToby Isaac 
5530c58f1c22SToby Isaac   Input Parameters:
5531c58f1c22SToby Isaac + mesh - The DM object
5532c58f1c22SToby Isaac - name - The label name
5533c58f1c22SToby Isaac 
5534c58f1c22SToby Isaac   Output Parameter:
5535c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5536c58f1c22SToby Isaac 
5537c58f1c22SToby Isaac   Level: beginner
5538c58f1c22SToby Isaac 
5539c58f1c22SToby Isaac .keywords: mesh
5540c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5541c58f1c22SToby Isaac @*/
5542c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5543c58f1c22SToby Isaac {
5544c58f1c22SToby Isaac   DMLabel        label;
5545c58f1c22SToby Isaac   PetscErrorCode ierr;
5546c58f1c22SToby Isaac 
5547c58f1c22SToby Isaac   PetscFunctionBegin;
5548c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5549c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5550c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5551c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5552c58f1c22SToby Isaac   *ids = NULL;
5553dab2e251SBlaise Bourdin  if (label) {
5554c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5555dab2e251SBlaise Bourdin   } else {
5556dab2e251SBlaise Bourdin     /* returning an empty IS */
5557dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5558dab2e251SBlaise Bourdin   }
5559c58f1c22SToby Isaac   PetscFunctionReturn(0);
5560c58f1c22SToby Isaac }
5561c58f1c22SToby Isaac 
5562c58f1c22SToby Isaac /*@C
5563c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5564c58f1c22SToby Isaac 
5565c58f1c22SToby Isaac   Not Collective
5566c58f1c22SToby Isaac 
5567c58f1c22SToby Isaac   Input Parameters:
5568c58f1c22SToby Isaac + dm - The DM object
5569c58f1c22SToby Isaac . name - The label name
5570c58f1c22SToby Isaac - value - The stratum value
5571c58f1c22SToby Isaac 
5572c58f1c22SToby Isaac   Output Parameter:
5573c58f1c22SToby Isaac . size - The stratum size
5574c58f1c22SToby Isaac 
5575c58f1c22SToby Isaac   Level: beginner
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac .keywords: mesh
5578c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5579c58f1c22SToby Isaac @*/
5580c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5581c58f1c22SToby Isaac {
5582c58f1c22SToby Isaac   DMLabel        label;
5583c58f1c22SToby Isaac   PetscErrorCode ierr;
5584c58f1c22SToby Isaac 
5585c58f1c22SToby Isaac   PetscFunctionBegin;
5586c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5587c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5588c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5589c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5590c58f1c22SToby Isaac   *size = 0;
5591c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5592c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5593c58f1c22SToby Isaac   PetscFunctionReturn(0);
5594c58f1c22SToby Isaac }
5595c58f1c22SToby Isaac 
5596c58f1c22SToby Isaac /*@C
5597c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5598c58f1c22SToby Isaac 
5599c58f1c22SToby Isaac   Not Collective
5600c58f1c22SToby Isaac 
5601c58f1c22SToby Isaac   Input Parameters:
5602c58f1c22SToby Isaac + dm - The DM object
5603c58f1c22SToby Isaac . name - The label name
5604c58f1c22SToby Isaac - value - The stratum value
5605c58f1c22SToby Isaac 
5606c58f1c22SToby Isaac   Output Parameter:
5607c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5608c58f1c22SToby Isaac 
5609c58f1c22SToby Isaac   Level: beginner
5610c58f1c22SToby Isaac 
5611c58f1c22SToby Isaac .keywords: mesh
5612c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5613c58f1c22SToby Isaac @*/
5614c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5615c58f1c22SToby Isaac {
5616c58f1c22SToby Isaac   DMLabel        label;
5617c58f1c22SToby Isaac   PetscErrorCode ierr;
5618c58f1c22SToby Isaac 
5619c58f1c22SToby Isaac   PetscFunctionBegin;
5620c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5621c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5622c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5623c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5624c58f1c22SToby Isaac   *points = NULL;
5625c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5626c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5627c58f1c22SToby Isaac   PetscFunctionReturn(0);
5628c58f1c22SToby Isaac }
5629c58f1c22SToby Isaac 
56304de306b1SToby Isaac /*@C
56319044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
56324de306b1SToby Isaac 
56334de306b1SToby Isaac   Not Collective
56344de306b1SToby Isaac 
56354de306b1SToby Isaac   Input Parameters:
56364de306b1SToby Isaac + dm - The DM object
56374de306b1SToby Isaac . name - The label name
56384de306b1SToby Isaac . value - The stratum value
56394de306b1SToby Isaac - points - The stratum points
56404de306b1SToby Isaac 
56414de306b1SToby Isaac   Level: beginner
56424de306b1SToby Isaac 
56434de306b1SToby Isaac .keywords: mesh
56444de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
56454de306b1SToby Isaac @*/
56464de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
56474de306b1SToby Isaac {
56484de306b1SToby Isaac   DMLabel        label;
56494de306b1SToby Isaac   PetscErrorCode ierr;
56504de306b1SToby Isaac 
56514de306b1SToby Isaac   PetscFunctionBegin;
56524de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56534de306b1SToby Isaac   PetscValidCharPointer(name, 2);
56544de306b1SToby Isaac   PetscValidPointer(points, 4);
56554de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
56564de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
56574de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
56584de306b1SToby Isaac   PetscFunctionReturn(0);
56594de306b1SToby Isaac }
56604de306b1SToby Isaac 
5661c58f1c22SToby Isaac /*@C
5662c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5663c58f1c22SToby Isaac 
5664c58f1c22SToby Isaac   Not Collective
5665c58f1c22SToby Isaac 
5666c58f1c22SToby Isaac   Input Parameters:
5667c58f1c22SToby Isaac + dm   - The DM object
5668c58f1c22SToby Isaac . name - The label name
5669c58f1c22SToby Isaac - value - The label value for this point
5670c58f1c22SToby Isaac 
5671c58f1c22SToby Isaac   Output Parameter:
5672c58f1c22SToby Isaac 
5673c58f1c22SToby Isaac   Level: beginner
5674c58f1c22SToby Isaac 
5675c58f1c22SToby Isaac .keywords: mesh
5676c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5677c58f1c22SToby Isaac @*/
5678c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5679c58f1c22SToby Isaac {
5680c58f1c22SToby Isaac   DMLabel        label;
5681c58f1c22SToby Isaac   PetscErrorCode ierr;
5682c58f1c22SToby Isaac 
5683c58f1c22SToby Isaac   PetscFunctionBegin;
5684c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5685c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5686c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5687c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5688c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5689c58f1c22SToby Isaac   PetscFunctionReturn(0);
5690c58f1c22SToby Isaac }
5691c58f1c22SToby Isaac 
5692c58f1c22SToby Isaac /*@
5693c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5694c58f1c22SToby Isaac 
5695c58f1c22SToby Isaac   Not Collective
5696c58f1c22SToby Isaac 
5697c58f1c22SToby Isaac   Input Parameter:
5698c58f1c22SToby Isaac . dm   - The DM object
5699c58f1c22SToby Isaac 
5700c58f1c22SToby Isaac   Output Parameter:
5701c58f1c22SToby Isaac . numLabels - the number of Labels
5702c58f1c22SToby Isaac 
5703c58f1c22SToby Isaac   Level: intermediate
5704c58f1c22SToby Isaac 
5705c58f1c22SToby Isaac .keywords: mesh
5706c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5707c58f1c22SToby Isaac @*/
5708c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5709c58f1c22SToby Isaac {
5710c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5711c58f1c22SToby Isaac   PetscInt  n    = 0;
5712c58f1c22SToby Isaac 
5713c58f1c22SToby Isaac   PetscFunctionBegin;
5714c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5715c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5716c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5717c58f1c22SToby Isaac   *numLabels = n;
5718c58f1c22SToby Isaac   PetscFunctionReturn(0);
5719c58f1c22SToby Isaac }
5720c58f1c22SToby Isaac 
5721c58f1c22SToby Isaac /*@C
5722c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5723c58f1c22SToby Isaac 
5724c58f1c22SToby Isaac   Not Collective
5725c58f1c22SToby Isaac 
5726c58f1c22SToby Isaac   Input Parameters:
5727c58f1c22SToby Isaac + dm - The DM object
5728c58f1c22SToby Isaac - n  - the label number
5729c58f1c22SToby Isaac 
5730c58f1c22SToby Isaac   Output Parameter:
5731c58f1c22SToby Isaac . name - the label name
5732c58f1c22SToby Isaac 
5733c58f1c22SToby Isaac   Level: intermediate
5734c58f1c22SToby Isaac 
5735c58f1c22SToby Isaac .keywords: mesh
5736c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5737c58f1c22SToby Isaac @*/
5738c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5739c58f1c22SToby Isaac {
5740c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5741c58f1c22SToby Isaac   PetscInt  l    = 0;
5742c58f1c22SToby Isaac 
5743c58f1c22SToby Isaac   PetscFunctionBegin;
5744c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5745c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5746c58f1c22SToby Isaac   while (next) {
5747c58f1c22SToby Isaac     if (l == n) {
5748c58f1c22SToby Isaac       *name = next->label->name;
5749c58f1c22SToby Isaac       PetscFunctionReturn(0);
5750c58f1c22SToby Isaac     }
5751c58f1c22SToby Isaac     ++l;
5752c58f1c22SToby Isaac     next = next->next;
5753c58f1c22SToby Isaac   }
5754c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5755c58f1c22SToby Isaac }
5756c58f1c22SToby Isaac 
5757c58f1c22SToby Isaac /*@C
5758c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5759c58f1c22SToby Isaac 
5760c58f1c22SToby Isaac   Not Collective
5761c58f1c22SToby Isaac 
5762c58f1c22SToby Isaac   Input Parameters:
5763c58f1c22SToby Isaac + dm   - The DM object
5764c58f1c22SToby Isaac - name - The label name
5765c58f1c22SToby Isaac 
5766c58f1c22SToby Isaac   Output Parameter:
5767c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5768c58f1c22SToby Isaac 
5769c58f1c22SToby Isaac   Level: intermediate
5770c58f1c22SToby Isaac 
5771c58f1c22SToby Isaac .keywords: mesh
5772c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5773c58f1c22SToby Isaac @*/
5774c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5775c58f1c22SToby Isaac {
5776c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5777c58f1c22SToby Isaac   PetscErrorCode ierr;
5778c58f1c22SToby Isaac 
5779c58f1c22SToby Isaac   PetscFunctionBegin;
5780c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5781c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5782c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5783c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5784c58f1c22SToby Isaac   while (next) {
5785c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5786c58f1c22SToby Isaac     if (*hasLabel) break;
5787c58f1c22SToby Isaac     next = next->next;
5788c58f1c22SToby Isaac   }
5789c58f1c22SToby Isaac   PetscFunctionReturn(0);
5790c58f1c22SToby Isaac }
5791c58f1c22SToby Isaac 
5792c58f1c22SToby Isaac /*@C
5793c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5794c58f1c22SToby Isaac 
5795c58f1c22SToby Isaac   Not Collective
5796c58f1c22SToby Isaac 
5797c58f1c22SToby Isaac   Input Parameters:
5798c58f1c22SToby Isaac + dm   - The DM object
5799c58f1c22SToby Isaac - name - The label name
5800c58f1c22SToby Isaac 
5801c58f1c22SToby Isaac   Output Parameter:
5802c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5803c58f1c22SToby Isaac 
5804c58f1c22SToby Isaac   Level: intermediate
5805c58f1c22SToby Isaac 
5806c58f1c22SToby Isaac .keywords: mesh
5807c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5808c58f1c22SToby Isaac @*/
5809c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5810c58f1c22SToby Isaac {
5811c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5812c58f1c22SToby Isaac   PetscBool      hasLabel;
5813c58f1c22SToby Isaac   PetscErrorCode ierr;
5814c58f1c22SToby Isaac 
5815c58f1c22SToby Isaac   PetscFunctionBegin;
5816c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5817c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5818c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5819c58f1c22SToby Isaac   *label = NULL;
5820c58f1c22SToby Isaac   while (next) {
5821c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5822c58f1c22SToby Isaac     if (hasLabel) {
5823c58f1c22SToby Isaac       *label = next->label;
5824c58f1c22SToby Isaac       break;
5825c58f1c22SToby Isaac     }
5826c58f1c22SToby Isaac     next = next->next;
5827c58f1c22SToby Isaac   }
5828c58f1c22SToby Isaac   PetscFunctionReturn(0);
5829c58f1c22SToby Isaac }
5830c58f1c22SToby Isaac 
5831c58f1c22SToby Isaac /*@C
5832c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5833c58f1c22SToby Isaac 
5834c58f1c22SToby Isaac   Not Collective
5835c58f1c22SToby Isaac 
5836c58f1c22SToby Isaac   Input Parameters:
5837c58f1c22SToby Isaac + dm - The DM object
5838c58f1c22SToby Isaac - n  - the label number
5839c58f1c22SToby Isaac 
5840c58f1c22SToby Isaac   Output Parameter:
5841c58f1c22SToby Isaac . label - the label
5842c58f1c22SToby Isaac 
5843c58f1c22SToby Isaac   Level: intermediate
5844c58f1c22SToby Isaac 
5845c58f1c22SToby Isaac .keywords: mesh
5846c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5847c58f1c22SToby Isaac @*/
5848c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5849c58f1c22SToby Isaac {
5850c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5851c58f1c22SToby Isaac   PetscInt    l    = 0;
5852c58f1c22SToby Isaac 
5853c58f1c22SToby Isaac   PetscFunctionBegin;
5854c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5855c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5856c58f1c22SToby Isaac   while (next) {
5857c58f1c22SToby Isaac     if (l == n) {
5858c58f1c22SToby Isaac       *label = next->label;
5859c58f1c22SToby Isaac       PetscFunctionReturn(0);
5860c58f1c22SToby Isaac     }
5861c58f1c22SToby Isaac     ++l;
5862c58f1c22SToby Isaac     next = next->next;
5863c58f1c22SToby Isaac   }
5864c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5865c58f1c22SToby Isaac }
5866c58f1c22SToby Isaac 
5867c58f1c22SToby Isaac /*@C
5868c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5869c58f1c22SToby Isaac 
5870c58f1c22SToby Isaac   Not Collective
5871c58f1c22SToby Isaac 
5872c58f1c22SToby Isaac   Input Parameters:
5873c58f1c22SToby Isaac + dm   - The DM object
5874c58f1c22SToby Isaac - label - The DMLabel
5875c58f1c22SToby Isaac 
5876c58f1c22SToby Isaac   Level: developer
5877c58f1c22SToby Isaac 
5878c58f1c22SToby Isaac .keywords: mesh
5879c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5880c58f1c22SToby Isaac @*/
5881c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5882c58f1c22SToby Isaac {
5883c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5884c58f1c22SToby Isaac   PetscBool      hasLabel;
5885c58f1c22SToby Isaac   PetscErrorCode ierr;
5886c58f1c22SToby Isaac 
5887c58f1c22SToby Isaac   PetscFunctionBegin;
5888c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5889c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5890c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5891c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5892c58f1c22SToby Isaac   tmpLabel->label  = label;
5893c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5894c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5895c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5896c58f1c22SToby Isaac   PetscFunctionReturn(0);
5897c58f1c22SToby Isaac }
5898c58f1c22SToby Isaac 
5899c58f1c22SToby Isaac /*@C
5900c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5901c58f1c22SToby Isaac 
5902c58f1c22SToby Isaac   Not Collective
5903c58f1c22SToby Isaac 
5904c58f1c22SToby Isaac   Input Parameters:
5905c58f1c22SToby Isaac + dm   - The DM object
5906c58f1c22SToby Isaac - name - The label name
5907c58f1c22SToby Isaac 
5908c58f1c22SToby Isaac   Output Parameter:
5909c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5910c58f1c22SToby Isaac 
5911c58f1c22SToby Isaac   Level: developer
5912c58f1c22SToby Isaac 
5913c58f1c22SToby Isaac .keywords: mesh
5914c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5915c58f1c22SToby Isaac @*/
5916c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5917c58f1c22SToby Isaac {
5918c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5919c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5920c58f1c22SToby Isaac   PetscBool      hasLabel;
5921c58f1c22SToby Isaac   PetscErrorCode ierr;
5922c58f1c22SToby Isaac 
5923c58f1c22SToby Isaac   PetscFunctionBegin;
5924c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5925c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5926c58f1c22SToby Isaac   *label = NULL;
5927c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5928c58f1c22SToby Isaac   while (next) {
5929c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5930c58f1c22SToby Isaac     if (hasLabel) {
5931c58f1c22SToby Isaac       if (last) last->next       = next->next;
5932c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5933c58f1c22SToby Isaac       next->next = NULL;
5934c58f1c22SToby Isaac       *label     = next->label;
5935c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5936c58f1c22SToby Isaac       if (hasLabel) {
5937c58f1c22SToby Isaac         dm->depthLabel = NULL;
5938c58f1c22SToby Isaac       }
5939c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5940c58f1c22SToby Isaac       break;
5941c58f1c22SToby Isaac     }
5942c58f1c22SToby Isaac     last = next;
5943c58f1c22SToby Isaac     next = next->next;
5944c58f1c22SToby Isaac   }
5945c58f1c22SToby Isaac   PetscFunctionReturn(0);
5946c58f1c22SToby Isaac }
5947c58f1c22SToby Isaac 
5948c58f1c22SToby Isaac /*@C
5949c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5950c58f1c22SToby Isaac 
5951c58f1c22SToby Isaac   Not Collective
5952c58f1c22SToby Isaac 
5953c58f1c22SToby Isaac   Input Parameters:
5954c58f1c22SToby Isaac + dm   - The DM object
5955c58f1c22SToby Isaac - name - The label name
5956c58f1c22SToby Isaac 
5957c58f1c22SToby Isaac   Output Parameter:
5958c58f1c22SToby Isaac . output - The flag for output
5959c58f1c22SToby Isaac 
5960c58f1c22SToby Isaac   Level: developer
5961c58f1c22SToby Isaac 
5962c58f1c22SToby Isaac .keywords: mesh
5963c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5964c58f1c22SToby Isaac @*/
5965c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5966c58f1c22SToby Isaac {
5967c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5968c58f1c22SToby Isaac   PetscErrorCode ierr;
5969c58f1c22SToby Isaac 
5970c58f1c22SToby Isaac   PetscFunctionBegin;
5971c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5972c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5973c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5974c58f1c22SToby Isaac   while (next) {
5975c58f1c22SToby Isaac     PetscBool flg;
5976c58f1c22SToby Isaac 
5977c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5978c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5979c58f1c22SToby Isaac     next = next->next;
5980c58f1c22SToby Isaac   }
5981c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5982c58f1c22SToby Isaac }
5983c58f1c22SToby Isaac 
5984c58f1c22SToby Isaac /*@C
5985c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5986c58f1c22SToby Isaac 
5987c58f1c22SToby Isaac   Not Collective
5988c58f1c22SToby Isaac 
5989c58f1c22SToby Isaac   Input Parameters:
5990c58f1c22SToby Isaac + dm     - The DM object
5991c58f1c22SToby Isaac . name   - The label name
5992c58f1c22SToby Isaac - output - The flag for output
5993c58f1c22SToby Isaac 
5994c58f1c22SToby Isaac   Level: developer
5995c58f1c22SToby Isaac 
5996c58f1c22SToby Isaac .keywords: mesh
5997c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5998c58f1c22SToby Isaac @*/
5999c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
6000c58f1c22SToby Isaac {
6001c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6002c58f1c22SToby Isaac   PetscErrorCode ierr;
6003c58f1c22SToby Isaac 
6004c58f1c22SToby Isaac   PetscFunctionBegin;
6005c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6006c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6007c58f1c22SToby Isaac   while (next) {
6008c58f1c22SToby Isaac     PetscBool flg;
6009c58f1c22SToby Isaac 
6010c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
6011c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
6012c58f1c22SToby Isaac     next = next->next;
6013c58f1c22SToby Isaac   }
6014c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6015c58f1c22SToby Isaac }
6016c58f1c22SToby Isaac 
6017c58f1c22SToby Isaac 
6018c58f1c22SToby Isaac /*@
6019c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
6020c58f1c22SToby Isaac 
6021c58f1c22SToby Isaac   Collective on DM
6022c58f1c22SToby Isaac 
6023c58f1c22SToby Isaac   Input Parameter:
60242e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
6025c58f1c22SToby Isaac 
6026c58f1c22SToby Isaac   Output Parameter:
60272e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
6028c58f1c22SToby Isaac 
6029c58f1c22SToby Isaac   Level: intermediate
6030c58f1c22SToby Isaac 
6031c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
6032c58f1c22SToby Isaac 
6033c58f1c22SToby Isaac .keywords: mesh
6034c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
6035c58f1c22SToby Isaac @*/
6036c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
6037c58f1c22SToby Isaac {
6038c58f1c22SToby Isaac   PetscInt       numLabels, l;
6039c58f1c22SToby Isaac   PetscErrorCode ierr;
6040c58f1c22SToby Isaac 
6041c58f1c22SToby Isaac   PetscFunctionBegin;
6042c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
6043c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
6044c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
6045c58f1c22SToby Isaac     DMLabel     label, labelNew;
6046c58f1c22SToby Isaac     const char *name;
6047c58f1c22SToby Isaac     PetscBool   flg;
6048c58f1c22SToby Isaac 
6049c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
6050c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
6051c58f1c22SToby Isaac     if (flg) continue;
60527d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
60537d5acc75SStefano Zampini     if (flg) continue;
6054c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
6055c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
6056c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
6057c58f1c22SToby Isaac   }
6058c58f1c22SToby Isaac   PetscFunctionReturn(0);
6059c58f1c22SToby Isaac }
6060a8fb8f29SToby Isaac 
6061a8fb8f29SToby Isaac /*@
6062a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
6063a8fb8f29SToby Isaac 
6064a8fb8f29SToby Isaac   Input Parameter:
6065a8fb8f29SToby Isaac . dm - The DM object
6066a8fb8f29SToby Isaac 
6067a8fb8f29SToby Isaac   Output Parameter:
6068a8fb8f29SToby Isaac . cdm - The coarse DM
6069a8fb8f29SToby Isaac 
6070a8fb8f29SToby Isaac   Level: intermediate
6071a8fb8f29SToby Isaac 
6072a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
6073a8fb8f29SToby Isaac @*/
6074a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6075a8fb8f29SToby Isaac {
6076a8fb8f29SToby Isaac   PetscFunctionBegin;
6077a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6078a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
6079a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
6080a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6081a8fb8f29SToby Isaac }
6082a8fb8f29SToby Isaac 
6083a8fb8f29SToby Isaac /*@
6084a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6085a8fb8f29SToby Isaac 
6086a8fb8f29SToby Isaac   Input Parameters:
6087a8fb8f29SToby Isaac + dm - The DM object
6088a8fb8f29SToby Isaac - cdm - The coarse DM
6089a8fb8f29SToby Isaac 
6090a8fb8f29SToby Isaac   Level: intermediate
6091a8fb8f29SToby Isaac 
6092a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
6093a8fb8f29SToby Isaac @*/
6094a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6095a8fb8f29SToby Isaac {
6096a8fb8f29SToby Isaac   PetscErrorCode ierr;
6097a8fb8f29SToby Isaac 
6098a8fb8f29SToby Isaac   PetscFunctionBegin;
6099a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6100a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6101a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6102a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6103a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6104a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6105a8fb8f29SToby Isaac }
6106a8fb8f29SToby Isaac 
610788bdff64SToby Isaac /*@
610888bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
610988bdff64SToby Isaac 
611088bdff64SToby Isaac   Input Parameter:
611188bdff64SToby Isaac . dm - The DM object
611288bdff64SToby Isaac 
611388bdff64SToby Isaac   Output Parameter:
611488bdff64SToby Isaac . fdm - The fine DM
611588bdff64SToby Isaac 
611688bdff64SToby Isaac   Level: intermediate
611788bdff64SToby Isaac 
611888bdff64SToby Isaac .seealso: DMSetFineDM()
611988bdff64SToby Isaac @*/
612088bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
612188bdff64SToby Isaac {
612288bdff64SToby Isaac   PetscFunctionBegin;
612388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
612488bdff64SToby Isaac   PetscValidPointer(fdm, 2);
612588bdff64SToby Isaac   *fdm = dm->fineMesh;
612688bdff64SToby Isaac   PetscFunctionReturn(0);
612788bdff64SToby Isaac }
612888bdff64SToby Isaac 
612988bdff64SToby Isaac /*@
613088bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
613188bdff64SToby Isaac 
613288bdff64SToby Isaac   Input Parameters:
613388bdff64SToby Isaac + dm - The DM object
613488bdff64SToby Isaac - fdm - The fine DM
613588bdff64SToby Isaac 
613688bdff64SToby Isaac   Level: intermediate
613788bdff64SToby Isaac 
613888bdff64SToby Isaac .seealso: DMGetFineDM()
613988bdff64SToby Isaac @*/
614088bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
614188bdff64SToby Isaac {
614288bdff64SToby Isaac   PetscErrorCode ierr;
614388bdff64SToby Isaac 
614488bdff64SToby Isaac   PetscFunctionBegin;
614588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
614688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
614788bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
614888bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
614988bdff64SToby Isaac   dm->fineMesh = fdm;
615088bdff64SToby Isaac   PetscFunctionReturn(0);
615188bdff64SToby Isaac }
615288bdff64SToby Isaac 
6153a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6154a6ba4734SToby Isaac 
6155a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6156a6ba4734SToby Isaac {
6157a6ba4734SToby Isaac   PetscErrorCode ierr;
6158a6ba4734SToby Isaac 
6159a6ba4734SToby Isaac   PetscFunctionBegin;
6160e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6161a6ba4734SToby Isaac   PetscFunctionReturn(0);
6162a6ba4734SToby Isaac }
6163a6ba4734SToby Isaac 
6164a6ba4734SToby Isaac /*@C
6165a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6166a6ba4734SToby Isaac 
6167a6ba4734SToby Isaac   Input Parameters:
61684c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6169f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6170a6ba4734SToby Isaac . name        - The BC name
6171a6ba4734SToby Isaac . labelname   - The label defining constrained points
6172a6ba4734SToby Isaac . field       - The field to constrain
6173a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6174a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6175a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6176a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6177a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6178a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6179a6ba4734SToby Isaac 
6180a6ba4734SToby Isaac   Options Database Keys:
6181a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6182a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6183a6ba4734SToby Isaac 
6184a6ba4734SToby Isaac   Level: developer
6185a6ba4734SToby Isaac 
6186a6ba4734SToby Isaac .seealso: DMGetBoundary()
6187a6ba4734SToby Isaac @*/
6188a30ec4eaSSatish 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)
6189a6ba4734SToby Isaac {
6190a6ba4734SToby Isaac   PetscErrorCode ierr;
6191a6ba4734SToby Isaac 
6192a6ba4734SToby Isaac   PetscFunctionBegin;
6193a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6194f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6195a6ba4734SToby Isaac   PetscFunctionReturn(0);
6196a6ba4734SToby Isaac }
6197a6ba4734SToby Isaac 
6198a6ba4734SToby Isaac /*@
6199a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6200a6ba4734SToby Isaac 
6201a6ba4734SToby Isaac   Input Parameters:
6202a6ba4734SToby Isaac . dm - The mesh object
6203a6ba4734SToby Isaac 
6204a6ba4734SToby Isaac   Output Parameters:
6205a6ba4734SToby Isaac . numBd - The number of BC
6206a6ba4734SToby Isaac 
6207a6ba4734SToby Isaac   Level: intermediate
6208a6ba4734SToby Isaac 
6209a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6210a6ba4734SToby Isaac @*/
6211a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6212a6ba4734SToby Isaac {
621358ebd649SToby Isaac   PetscErrorCode ierr;
6214a6ba4734SToby Isaac 
6215a6ba4734SToby Isaac   PetscFunctionBegin;
6216a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
621758ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6218a6ba4734SToby Isaac   PetscFunctionReturn(0);
6219a6ba4734SToby Isaac }
6220a6ba4734SToby Isaac 
6221a6ba4734SToby Isaac /*@C
62221c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6223a6ba4734SToby Isaac 
6224a6ba4734SToby Isaac   Input Parameters:
6225a6ba4734SToby Isaac + dm          - The mesh object
6226a6ba4734SToby Isaac - bd          - The BC number
6227a6ba4734SToby Isaac 
6228a6ba4734SToby Isaac   Output Parameters:
6229f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6230a6ba4734SToby Isaac . name        - The BC name
6231a6ba4734SToby Isaac . labelname   - The label defining constrained points
6232a6ba4734SToby Isaac . field       - The field to constrain
6233a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6234a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6235a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6236a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6237a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6238a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6239a6ba4734SToby Isaac 
6240a6ba4734SToby Isaac   Options Database Keys:
6241a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6242a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6243a6ba4734SToby Isaac 
6244a6ba4734SToby Isaac   Level: developer
6245a6ba4734SToby Isaac 
6246a6ba4734SToby Isaac .seealso: DMAddBoundary()
6247a6ba4734SToby Isaac @*/
6248a30ec4eaSSatish 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)
6249a6ba4734SToby Isaac {
625058ebd649SToby Isaac   PetscErrorCode ierr;
6251a6ba4734SToby Isaac 
6252a6ba4734SToby Isaac   PetscFunctionBegin;
6253a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6254f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6255a6ba4734SToby Isaac   PetscFunctionReturn(0);
6256a6ba4734SToby Isaac }
6257a6ba4734SToby Isaac 
6258e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6259e6f8dbb6SToby Isaac {
6260dff059c6SToby Isaac   DMBoundary *lastnext;
6261e6f8dbb6SToby Isaac   DSBoundary dsbound;
6262e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6263e6f8dbb6SToby Isaac 
6264e6f8dbb6SToby Isaac   PetscFunctionBegin;
6265e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
626647a1f5adSToby Isaac   if (dm->boundary) {
626747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
626847a1f5adSToby Isaac 
626947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
627047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
627147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
627247a1f5adSToby Isaac     while (next) {
627347a1f5adSToby Isaac       DMBoundary b = next;
627447a1f5adSToby Isaac 
627547a1f5adSToby Isaac       next = b->next;
627647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6277a6ba4734SToby Isaac     }
627847a1f5adSToby Isaac     dm->boundary = NULL;
6279a6ba4734SToby Isaac   }
628047a1f5adSToby Isaac 
6281dff059c6SToby Isaac   lastnext = &(dm->boundary);
6282e6f8dbb6SToby Isaac   while (dsbound) {
6283e6f8dbb6SToby Isaac     DMBoundary dmbound;
6284e6f8dbb6SToby Isaac 
6285e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6286e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6287e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6288994fe344SLisandro 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);}
628947a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6290dff059c6SToby Isaac     *lastnext = dmbound;
6291dff059c6SToby Isaac     lastnext = &(dmbound->next);
6292dff059c6SToby Isaac     dsbound = dsbound->next;
6293a6ba4734SToby Isaac   }
6294a6ba4734SToby Isaac   PetscFunctionReturn(0);
6295a6ba4734SToby Isaac }
6296a6ba4734SToby Isaac 
6297a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6298a6ba4734SToby Isaac {
6299b95f2879SToby Isaac   DMBoundary     b;
6300a6ba4734SToby Isaac   PetscErrorCode ierr;
6301a6ba4734SToby Isaac 
6302a6ba4734SToby Isaac   PetscFunctionBegin;
6303a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6304a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6305a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6306e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6307b95f2879SToby Isaac   b = dm->boundary;
6308a6ba4734SToby Isaac   while (b && !(*isBd)) {
6309e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6310e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
63113424c85cSToby Isaac 
63123424c85cSToby Isaac     if (label) {
6313a6ba4734SToby Isaac       PetscInt i;
6314a6ba4734SToby Isaac 
6315e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6316e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6317a6ba4734SToby Isaac       }
6318a6ba4734SToby Isaac     }
6319a6ba4734SToby Isaac     b = b->next;
6320a6ba4734SToby Isaac   }
6321a6ba4734SToby Isaac   PetscFunctionReturn(0);
6322a6ba4734SToby Isaac }
63234d6f44ffSToby Isaac 
63244d6f44ffSToby Isaac /*@C
63254d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
63264d6f44ffSToby Isaac 
63274d6f44ffSToby Isaac   Input Parameters:
63284d6f44ffSToby Isaac + dm      - The DM
63290709b2feSToby Isaac . time    - The time
63304d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
63314d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
63324d6f44ffSToby Isaac - mode    - The insertion mode for values
63334d6f44ffSToby Isaac 
63344d6f44ffSToby Isaac   Output Parameter:
63354d6f44ffSToby Isaac . X - vector
63364d6f44ffSToby Isaac 
63374d6f44ffSToby Isaac    Calling sequence of func:
63380709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
63394d6f44ffSToby Isaac 
63404d6f44ffSToby Isaac +  dim - The spatial dimension
63414d6f44ffSToby Isaac .  x   - The coordinates
63424d6f44ffSToby Isaac .  Nf  - The number of fields
63434d6f44ffSToby Isaac .  u   - The output field values
63444d6f44ffSToby Isaac -  ctx - optional user-defined function context
63454d6f44ffSToby Isaac 
63464d6f44ffSToby Isaac   Level: developer
63474d6f44ffSToby Isaac 
63482716604bSToby Isaac .seealso: DMComputeL2Diff()
63494d6f44ffSToby Isaac @*/
63500709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
63514d6f44ffSToby Isaac {
63524d6f44ffSToby Isaac   Vec            localX;
63534d6f44ffSToby Isaac   PetscErrorCode ierr;
63544d6f44ffSToby Isaac 
63554d6f44ffSToby Isaac   PetscFunctionBegin;
63564d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63574d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
63580709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63594d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
63604d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
63614d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
63624d6f44ffSToby Isaac   PetscFunctionReturn(0);
63634d6f44ffSToby Isaac }
63644d6f44ffSToby Isaac 
63650709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
63664d6f44ffSToby Isaac {
63674d6f44ffSToby Isaac   PetscErrorCode ierr;
63684d6f44ffSToby Isaac 
63694d6f44ffSToby Isaac   PetscFunctionBegin;
63704d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63714d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
63720918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
63730709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63744d6f44ffSToby Isaac   PetscFunctionReturn(0);
63754d6f44ffSToby Isaac }
63764d6f44ffSToby Isaac 
63772c53366bSMatthew 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)
63782c53366bSMatthew G. Knepley {
63792c53366bSMatthew G. Knepley   Vec            localX;
63802c53366bSMatthew G. Knepley   PetscErrorCode ierr;
63812c53366bSMatthew G. Knepley 
63822c53366bSMatthew G. Knepley   PetscFunctionBegin;
63832c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63842c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
63852c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63862c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
63872c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
63882c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
63892c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
63902c53366bSMatthew G. Knepley }
63912c53366bSMatthew G. Knepley 
63921c531cf8SMatthew 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)
63934d6f44ffSToby Isaac {
63944d6f44ffSToby Isaac   PetscErrorCode ierr;
63954d6f44ffSToby Isaac 
63964d6f44ffSToby Isaac   PetscFunctionBegin;
63974d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63984d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
63990918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
64001c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
64014d6f44ffSToby Isaac   PetscFunctionReturn(0);
64024d6f44ffSToby Isaac }
64032716604bSToby Isaac 
64048c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
64058c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
64068c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
64078c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6408191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
64098c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
64108c6c5593SMatthew G. Knepley {
64118c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
64128c6c5593SMatthew G. Knepley 
64138c6c5593SMatthew G. Knepley   PetscFunctionBegin;
64148c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64158c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
64168c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
64170918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
64188c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
64198c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
64208c6c5593SMatthew G. Knepley }
64218c6c5593SMatthew G. Knepley 
64221c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
64238c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
64248c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
64258c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6426191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
64278c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
64288c6c5593SMatthew G. Knepley {
64298c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
64308c6c5593SMatthew G. Knepley 
64318c6c5593SMatthew G. Knepley   PetscFunctionBegin;
64328c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64338c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
64348c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
64350918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
64361c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
64378c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
64388c6c5593SMatthew G. Knepley }
64398c6c5593SMatthew G. Knepley 
64402716604bSToby Isaac /*@C
64412716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
64422716604bSToby Isaac 
64432716604bSToby Isaac   Input Parameters:
64442716604bSToby Isaac + dm    - The DM
64450709b2feSToby Isaac . time  - The time
64462716604bSToby Isaac . funcs - The functions to evaluate for each field component
64472716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6448574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
64492716604bSToby Isaac 
64502716604bSToby Isaac   Output Parameter:
64512716604bSToby Isaac . diff - The diff ||u - u_h||_2
64522716604bSToby Isaac 
64532716604bSToby Isaac   Level: developer
64542716604bSToby Isaac 
64551189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
64562716604bSToby Isaac @*/
64570709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
64582716604bSToby Isaac {
64592716604bSToby Isaac   PetscErrorCode ierr;
64602716604bSToby Isaac 
64612716604bSToby Isaac   PetscFunctionBegin;
64622716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6463b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
64640918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
64650709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
64662716604bSToby Isaac   PetscFunctionReturn(0);
64672716604bSToby Isaac }
6468b698f381SToby Isaac 
6469b698f381SToby Isaac /*@C
6470b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6471b698f381SToby Isaac 
6472b698f381SToby Isaac   Input Parameters:
6473b698f381SToby Isaac + dm    - The DM
6474b698f381SToby Isaac , time  - The time
6475b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6476b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6477574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6478b698f381SToby Isaac - n     - The vector to project along
6479b698f381SToby Isaac 
6480b698f381SToby Isaac   Output Parameter:
6481b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6482b698f381SToby Isaac 
6483b698f381SToby Isaac   Level: developer
6484b698f381SToby Isaac 
6485b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6486b698f381SToby Isaac @*/
6487b698f381SToby 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)
6488b698f381SToby Isaac {
6489b698f381SToby Isaac   PetscErrorCode ierr;
6490b698f381SToby Isaac 
6491b698f381SToby Isaac   PetscFunctionBegin;
6492b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6493b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6494b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6495b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6496b698f381SToby Isaac   PetscFunctionReturn(0);
6497b698f381SToby Isaac }
6498b698f381SToby Isaac 
64992a16baeaSToby Isaac /*@C
65002a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
65012a16baeaSToby Isaac 
65022a16baeaSToby Isaac   Input Parameters:
65032a16baeaSToby Isaac + dm    - The DM
65042a16baeaSToby Isaac . time  - The time
65052a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
65062a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6507574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
65082a16baeaSToby Isaac 
65092a16baeaSToby Isaac   Output Parameter:
65102a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
65112a16baeaSToby Isaac 
65122a16baeaSToby Isaac   Level: developer
65132a16baeaSToby Isaac 
65141189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
65152a16baeaSToby Isaac @*/
65161189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
65172a16baeaSToby Isaac {
65182a16baeaSToby Isaac   PetscErrorCode ierr;
65192a16baeaSToby Isaac 
65202a16baeaSToby Isaac   PetscFunctionBegin;
65212a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65222a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
65230918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
65242a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
65252a16baeaSToby Isaac   PetscFunctionReturn(0);
65262a16baeaSToby Isaac }
65272a16baeaSToby Isaac 
6528df0b854cSToby Isaac /*@C
6529df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6530cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6531df0b854cSToby Isaac 
6532df0b854cSToby Isaac   Collective on dm
6533df0b854cSToby Isaac 
6534df0b854cSToby Isaac   Input parameters:
6535df0b854cSToby Isaac + dm - the pre-adaptation DM object
6536a1b0c543SToby Isaac - label - label with the flags
6537df0b854cSToby Isaac 
6538df0b854cSToby Isaac   Output parameters:
65390d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6540df0b854cSToby Isaac 
6541df0b854cSToby Isaac   Level: intermediate
65420d1cd5e0SMatthew G. Knepley 
65430d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6544df0b854cSToby Isaac @*/
65450d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6546df0b854cSToby Isaac {
6547df0b854cSToby Isaac   PetscErrorCode ierr;
6548df0b854cSToby Isaac 
6549df0b854cSToby Isaac   PetscFunctionBegin;
6550df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6551a1b0c543SToby Isaac   PetscValidPointer(label,2);
65520d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
65530d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
65546f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
65550d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
65560d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
65570d1cd5e0SMatthew G. Knepley }
65580d1cd5e0SMatthew G. Knepley 
65590d1cd5e0SMatthew G. Knepley /*@C
65600d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
65610d1cd5e0SMatthew G. Knepley 
65620d1cd5e0SMatthew G. Knepley   Input Parameters:
65630d1cd5e0SMatthew G. Knepley + dm - The DM object
65640d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
65656f25b0d8SLisandro 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_".
65660d1cd5e0SMatthew G. Knepley 
65670d1cd5e0SMatthew G. Knepley   Output Parameter:
65680d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
65690d1cd5e0SMatthew G. Knepley 
65700d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
65710d1cd5e0SMatthew G. Knepley 
65720d1cd5e0SMatthew G. Knepley   Level: advanced
65730d1cd5e0SMatthew G. Knepley 
65740d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
65750d1cd5e0SMatthew G. Knepley @*/
65760d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
65770d1cd5e0SMatthew G. Knepley {
65780d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
65790d1cd5e0SMatthew G. Knepley 
65800d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
65810d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65820d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
65830d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
65840d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
65856f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
65866f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
65870d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6588df0b854cSToby Isaac   PetscFunctionReturn(0);
6589df0b854cSToby Isaac }
6590c4088d22SMatthew G. Knepley 
6591502a2867SDave May /*@C
6592502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6593502a2867SDave May 
6594502a2867SDave May  Not Collective
6595502a2867SDave May 
6596502a2867SDave May  Input Parameter:
6597502a2867SDave May  . dm    - The DM
6598502a2867SDave May 
6599502a2867SDave May  Output Parameter:
6600502a2867SDave May  . nranks - the number of neighbours
6601502a2867SDave May  . ranks - the neighbors ranks
6602502a2867SDave May 
6603502a2867SDave May  Notes:
6604502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6605502a2867SDave May 
6606502a2867SDave May  Level: beginner
6607502a2867SDave May 
6608dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6609502a2867SDave May @*/
6610502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6611502a2867SDave May {
6612502a2867SDave May   PetscErrorCode ierr;
6613502a2867SDave May 
6614502a2867SDave May   PetscFunctionBegin;
6615502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66160918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6617502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6618502a2867SDave May   PetscFunctionReturn(0);
6619502a2867SDave May }
6620502a2867SDave May 
6621531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6622531c7667SBarry Smith 
6623531c7667SBarry Smith /*
6624531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6625531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6626531c7667SBarry Smith */
6627531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6628531c7667SBarry Smith {
6629531c7667SBarry Smith   PetscErrorCode ierr;
6630531c7667SBarry Smith 
6631531c7667SBarry Smith   PetscFunctionBegin;
6632531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6633531c7667SBarry Smith     Vec x1local;
6634531c7667SBarry Smith     DM  dm;
6635531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6636531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6637531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6638531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6639531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6640531c7667SBarry Smith     x1   = x1local;
6641531c7667SBarry Smith   }
6642531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6643531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6644531c7667SBarry Smith     DM  dm;
6645531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6646531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6647531c7667SBarry Smith   }
6648531c7667SBarry Smith   PetscFunctionReturn(0);
6649531c7667SBarry Smith }
6650531c7667SBarry Smith 
6651531c7667SBarry Smith /*@
6652531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6653531c7667SBarry Smith 
6654531c7667SBarry Smith     Input Parameter:
6655531c7667SBarry Smith .    coloring - the MatFDColoring object
6656531c7667SBarry Smith 
665795452b02SPatrick Sanan     Developer Notes:
665895452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
6659531c7667SBarry Smith 
66601b266c99SBarry Smith     Level: advanced
66611b266c99SBarry Smith 
6662531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6663531c7667SBarry Smith @*/
6664531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6665531c7667SBarry Smith {
6666531c7667SBarry Smith   PetscFunctionBegin;
6667531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6668531c7667SBarry Smith   PetscFunctionReturn(0);
6669531c7667SBarry Smith }
66708320bc6fSPatrick Sanan 
66718320bc6fSPatrick Sanan /*@
66728320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
66738320bc6fSPatrick Sanan 
66748320bc6fSPatrick Sanan     Collective
66758320bc6fSPatrick Sanan 
66768320bc6fSPatrick Sanan     Input Parameters:
66778320bc6fSPatrick Sanan +    dm - the first DM
66788320bc6fSPatrick Sanan -    dm2 - the second DM
66798320bc6fSPatrick Sanan 
66808320bc6fSPatrick Sanan     Output Parameters:
66818320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
66828320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
66838320bc6fSPatrick Sanan 
66848320bc6fSPatrick Sanan     Notes:
66858320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
66868320bc6fSPatrick Sanan     of the same topology. This implies that the the section (field data) on one
66878320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
66888320bc6fSPatrick Sanan     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
66898320bc6fSPatrick Sanan     decomposition, with different data.
66908320bc6fSPatrick Sanan 
66918320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
66928320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
66938320bc6fSPatrick Sanan 
66948320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
66958320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
66968320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
66978320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
66988320bc6fSPatrick Sanan 
66998320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
67008320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
67018320bc6fSPatrick Sanan .vb
67028320bc6fSPatrick Sanan   ...
67038320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
67048320bc6fSPatrick Sanan   if (set && compatible)  {
67058320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
67068320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
67078320bc6fSPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
67088320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
67098320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
67108320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
67118320bc6fSPatrick Sanan       }
67128320bc6fSPatrick Sanan     }
67138320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
67148320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
67158320bc6fSPatrick Sanan   } else {
67168320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
67178320bc6fSPatrick Sanan   }
67188320bc6fSPatrick Sanan   ...
67198320bc6fSPatrick Sanan .ve
67208320bc6fSPatrick Sanan 
67218320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
67228320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
67238320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
67248320bc6fSPatrick Sanan     always check the "set" output parameter.
67258320bc6fSPatrick Sanan 
67268320bc6fSPatrick Sanan     A DM is always compatible with itself.
67278320bc6fSPatrick Sanan 
67288320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
67298320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
67308320bc6fSPatrick Sanan     incompatible.
67318320bc6fSPatrick Sanan 
67328320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
67338320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
67348320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
67358320bc6fSPatrick Sanan 
67368320bc6fSPatrick Sanan     Developer Notes:
67378320bc6fSPatrick Sanan     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
67388320bc6fSPatrick Sanan     the DM B is compatible with DM A. Thus, this function checks the implementations
67398320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
67408320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
67418320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
67428320bc6fSPatrick Sanan     logic for this function, to check for existing logic in the implementation
67438320bc6fSPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found; the logic of this
67448320bc6fSPatrick Sanan     function will then call that logic.
67458320bc6fSPatrick Sanan 
67468320bc6fSPatrick Sanan     Level: advanced
67478320bc6fSPatrick Sanan 
67488320bc6fSPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA()
67498320bc6fSPatrick Sanan @*/
67508320bc6fSPatrick Sanan 
67518320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
67528320bc6fSPatrick Sanan {
67538320bc6fSPatrick Sanan   PetscErrorCode ierr;
67548320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
67558320bc6fSPatrick Sanan   DMType         type,type2;
67568320bc6fSPatrick Sanan   PetscBool      sameType;
67578320bc6fSPatrick Sanan 
67588320bc6fSPatrick Sanan   PetscFunctionBegin;
67598320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67608320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
67618320bc6fSPatrick Sanan 
67628320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
67638320bc6fSPatrick Sanan   if (dm == dm2) {
67648320bc6fSPatrick Sanan     *set = PETSC_TRUE;
67658320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
67668320bc6fSPatrick Sanan     PetscFunctionReturn(0);
67678320bc6fSPatrick Sanan   }
67688320bc6fSPatrick Sanan 
67698320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
67708320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
67718320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
67728320bc6fSPatrick Sanan      determined by the implementation-specific logic */
67738320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
67748320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
67758320bc6fSPatrick Sanan     *set = PETSC_TRUE;
67768320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
67778320bc6fSPatrick Sanan     PetscFunctionReturn(0);
67788320bc6fSPatrick Sanan   }
67798320bc6fSPatrick Sanan 
67808320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
67818320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
67828320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
67838320bc6fSPatrick Sanan     if (*set) {
67848320bc6fSPatrick Sanan       PetscFunctionReturn(0);
67858320bc6fSPatrick Sanan     }
67868320bc6fSPatrick Sanan   }
67878320bc6fSPatrick Sanan 
67888320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
67898320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
67908320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
67918320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
67928320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
67938320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
67948320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
67958320bc6fSPatrick Sanan   } else {
67968320bc6fSPatrick Sanan     *set = PETSC_FALSE;
67978320bc6fSPatrick Sanan   }
67988320bc6fSPatrick Sanan   PetscFunctionReturn(0);
67998320bc6fSPatrick Sanan }
6800