xref: /petsc/src/dm/interface/dm.c (revision 83bfc06f60be43e5a700cf436d281a24eccd012e)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
10d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
111ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1267a56275SMatthew G Knepley 
13e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
1440967b3bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_",0};
15bff4a2f0SMatthew G. Knepley 
164a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
174a7a4c06SLawrence Mitchell {
184a7a4c06SLawrence Mitchell   PetscFunctionBegin;
194a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
204a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
214a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
224a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
234a7a4c06SLawrence Mitchell }
244a7a4c06SLawrence Mitchell 
25a4121054SBarry Smith /*@
26de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
27a4121054SBarry Smith 
28a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
29a4121054SBarry Smith    error when you try to use the vector.
30a4121054SBarry Smith 
31a4121054SBarry Smith   Collective on MPI_Comm
32a4121054SBarry Smith 
33a4121054SBarry Smith   Input Parameter:
34a4121054SBarry Smith . comm - The communicator for the DM object
35a4121054SBarry Smith 
36a4121054SBarry Smith   Output Parameter:
37a4121054SBarry Smith . dm - The DM object
38a4121054SBarry Smith 
39a4121054SBarry Smith   Level: beginner
40a4121054SBarry Smith 
418472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
42a4121054SBarry Smith @*/
437087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
44a4121054SBarry Smith {
45a4121054SBarry Smith   DM             v;
46e5e52638SMatthew G. Knepley   PetscDS        ds;
47a4121054SBarry Smith   PetscErrorCode ierr;
48a4121054SBarry Smith 
49a4121054SBarry Smith   PetscFunctionBegin;
501411c6eeSJed Brown   PetscValidPointer(dm,2);
510298fd71SBarry Smith   *dm = NULL;
52607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
53a4121054SBarry Smith 
5473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
55e7c4fc90SDmitry Karpeev 
5649be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5749be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
580298fd71SBarry Smith   v->ltogmap                  = NULL;
591411c6eeSJed Brown   v->bs                       = 1;
60171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
6188ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
6288ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
63c58f1c22SToby Isaac   v->labels                   = NULL;
64c58f1c22SToby Isaac   v->depthLabel               = NULL;
650298fd71SBarry Smith   v->defaultSection           = NULL;
660298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
67fba222abSToby Isaac   v->defaultConstraintSection = NULL;
68fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
69c6b900c6SMatthew G. Knepley   v->L                        = NULL;
70c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
715dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
729a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7396173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
74435a35e8SMatthew G Knepley   {
75435a35e8SMatthew G Knepley     PetscInt i;
76435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
770298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
78f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
79435a35e8SMatthew G Knepley     }
80435a35e8SMatthew G Knepley   }
81e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
82e5e52638SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, ds);CHKERRQ(ierr);
83e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
8414f150ffSMatthew G. Knepley   v->dmBC = NULL;
85a8fb8f29SToby Isaac   v->coarseMesh = NULL;
86f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
87cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
88c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
89b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
90bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
91c58f1c22SToby Isaac   v->labels->refct = 1;
924a7a4c06SLawrence Mitchell 
934a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
944a7a4c06SLawrence Mitchell 
951411c6eeSJed Brown   *dm = v;
96a4121054SBarry Smith   PetscFunctionReturn(0);
97a4121054SBarry Smith }
98a4121054SBarry Smith 
9938221697SMatthew G. Knepley /*@
10038221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10138221697SMatthew G. Knepley 
10238221697SMatthew G. Knepley   Collective on MPI_Comm
10338221697SMatthew G. Knepley 
10438221697SMatthew G. Knepley   Input Parameter:
10538221697SMatthew G. Knepley . dm - The original DM object
10638221697SMatthew G. Knepley 
10738221697SMatthew G. Knepley   Output Parameter:
10838221697SMatthew G. Knepley . newdm  - The new DM object
10938221697SMatthew G. Knepley 
11038221697SMatthew G. Knepley   Level: beginner
11138221697SMatthew G. Knepley 
11238221697SMatthew G. Knepley .keywords: DM, topology, create
11338221697SMatthew G. Knepley @*/
11438221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11538221697SMatthew G. Knepley {
11638221697SMatthew G. Knepley   PetscSF        sf;
11738221697SMatthew G. Knepley   Vec            coords;
11838221697SMatthew G. Knepley   void          *ctx;
119a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12038221697SMatthew G. Knepley   PetscErrorCode ierr;
12138221697SMatthew G. Knepley 
12238221697SMatthew G. Knepley   PetscFunctionBegin;
12338221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12438221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12538221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
126c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
127c58f1c22SToby Isaac   dm->labels->refct++;
128c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
129c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
130ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
131ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1321de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1331de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13438221697SMatthew G. Knepley   if (dm->ops->clone) {
13538221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13638221697SMatthew G. Knepley   }
1373f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13838221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
142be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
143be4c1c3eSMatthew G. Knepley     DM           ncdm;
144be4c1c3eSMatthew G. Knepley     PetscSection cs;
1455a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
146be4c1c3eSMatthew G. Knepley 
147e87a4003SBarry Smith     ierr = DMGetSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
148be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1495a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1505a0206caSToby Isaac     if (pEndMax >= 0) {
151be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
152*83bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
153e87a4003SBarry Smith       ierr = DMSetSection(ncdm, cs);CHKERRQ(ierr);
154a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
155be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
156be4c1c3eSMatthew G. Knepley     }
157be4c1c3eSMatthew G. Knepley   }
158a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
159a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
16038221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16138221697SMatthew G. Knepley   if (coords) {
16238221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16338221697SMatthew G. Knepley   } else {
16438221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16538221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16638221697SMatthew G. Knepley   }
16790b157c4SStefano Zampini   {
16890b157c4SStefano Zampini     PetscBool             isper;
169c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1705dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17190b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17290b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
173c6b900c6SMatthew G. Knepley   }
17438221697SMatthew G. Knepley   PetscFunctionReturn(0);
17538221697SMatthew G. Knepley }
17638221697SMatthew G. Knepley 
1779a42bb27SBarry Smith /*@C
178564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1799a42bb27SBarry Smith 
1808472ad0fSDave May    Logically Collective on DM
1819a42bb27SBarry Smith 
1829a42bb27SBarry Smith    Input Parameter:
1839a42bb27SBarry Smith +  da - initial distributed array
184e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1859a42bb27SBarry Smith 
1869a42bb27SBarry Smith    Options Database:
187dd85299cSBarry Smith .   -dm_vec_type ctype
1889a42bb27SBarry Smith 
1899a42bb27SBarry Smith    Level: intermediate
1909a42bb27SBarry Smith 
191a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1929a42bb27SBarry Smith @*/
19319fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1949a42bb27SBarry Smith {
1959a42bb27SBarry Smith   PetscErrorCode ierr;
1969a42bb27SBarry Smith 
1979a42bb27SBarry Smith   PetscFunctionBegin;
1989a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1999a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2019a42bb27SBarry Smith   PetscFunctionReturn(0);
2029a42bb27SBarry Smith }
2039a42bb27SBarry Smith 
204c0dedaeaSBarry Smith /*@C
205c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
206c0dedaeaSBarry Smith 
2078472ad0fSDave May    Logically Collective on DM
208c0dedaeaSBarry Smith 
209c0dedaeaSBarry Smith    Input Parameter:
210c0dedaeaSBarry Smith .  da - initial distributed array
211c0dedaeaSBarry Smith 
212c0dedaeaSBarry Smith    Output Parameter:
213c0dedaeaSBarry Smith .  ctype - the vector type
214c0dedaeaSBarry Smith 
215c0dedaeaSBarry Smith    Level: intermediate
216c0dedaeaSBarry Smith 
217a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
218c0dedaeaSBarry Smith @*/
219c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
220c0dedaeaSBarry Smith {
221c0dedaeaSBarry Smith   PetscFunctionBegin;
222c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
223c0dedaeaSBarry Smith   *ctype = da->vectype;
224c0dedaeaSBarry Smith   PetscFunctionReturn(0);
225c0dedaeaSBarry Smith }
226c0dedaeaSBarry Smith 
2275f1ad066SMatthew G Knepley /*@
22834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley   Not collective
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley   Input Parameter:
2335f1ad066SMatthew G Knepley . v - The Vec
2345f1ad066SMatthew G Knepley 
2355f1ad066SMatthew G Knepley   Output Parameter:
2365f1ad066SMatthew G Knepley . dm - The DM
2375f1ad066SMatthew G Knepley 
2385f1ad066SMatthew G Knepley   Level: intermediate
2395f1ad066SMatthew G Knepley 
2405f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2415f1ad066SMatthew G Knepley @*/
2425f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2435f1ad066SMatthew G Knepley {
2445f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   PetscFunctionBegin;
2475f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2485f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2495f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2505f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2515f1ad066SMatthew G Knepley }
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley /*@
254d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley   Not collective
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley   Input Parameters:
2595f1ad066SMatthew G Knepley + v - The Vec
2605f1ad066SMatthew G Knepley - dm - The DM
2615f1ad066SMatthew G Knepley 
262d9805387SMatthew 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.
263d9805387SMatthew G. Knepley 
2645f1ad066SMatthew G Knepley   Level: intermediate
2655f1ad066SMatthew G Knepley 
2665f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2675f1ad066SMatthew G Knepley @*/
2685f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2695f1ad066SMatthew G Knepley {
2705f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2715f1ad066SMatthew G Knepley 
2725f1ad066SMatthew G Knepley   PetscFunctionBegin;
2735f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
274d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2755f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2765f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2775f1ad066SMatthew G Knepley }
2785f1ad066SMatthew G Knepley 
279521d9a4cSLisandro Dalcin /*@C
2808f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2818f1509bcSBarry Smith 
2828f1509bcSBarry Smith    Logically Collective on DM
2838f1509bcSBarry Smith 
2848f1509bcSBarry Smith    Input Parameters:
2858f1509bcSBarry Smith +  dm - the DM context
2868f1509bcSBarry Smith -  ctype - the matrix type
2878f1509bcSBarry Smith 
2888f1509bcSBarry Smith    Options Database:
2898f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2908f1509bcSBarry Smith 
2918f1509bcSBarry Smith    Level: intermediate
2928f1509bcSBarry Smith 
2938f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2948f1509bcSBarry Smith           DMGetISColoringType()
2958f1509bcSBarry Smith @*/
2968f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2978f1509bcSBarry Smith {
2988f1509bcSBarry Smith   PetscFunctionBegin;
2998f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3008f1509bcSBarry Smith   dm->coloringtype = ctype;
3018f1509bcSBarry Smith   PetscFunctionReturn(0);
3028f1509bcSBarry Smith }
3038f1509bcSBarry Smith 
3048f1509bcSBarry Smith /*@C
3058f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
306521d9a4cSLisandro Dalcin 
307521d9a4cSLisandro Dalcin    Logically Collective on DM
308521d9a4cSLisandro Dalcin 
309521d9a4cSLisandro Dalcin    Input Parameter:
3108f1509bcSBarry Smith .  dm - the DM context
3118f1509bcSBarry Smith 
3128f1509bcSBarry Smith    Output Parameter:
3138f1509bcSBarry Smith .  ctype - the matrix type
3148f1509bcSBarry Smith 
3158f1509bcSBarry Smith    Options Database:
3168f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3178f1509bcSBarry Smith 
3188f1509bcSBarry Smith    Level: intermediate
3198f1509bcSBarry Smith 
3208f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3218f1509bcSBarry Smith           DMGetISColoringType()
3228f1509bcSBarry Smith @*/
3238f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3248f1509bcSBarry Smith {
3258f1509bcSBarry Smith   PetscFunctionBegin;
3268f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3278f1509bcSBarry Smith   *ctype = dm->coloringtype;
3288f1509bcSBarry Smith   PetscFunctionReturn(0);
3298f1509bcSBarry Smith }
3308f1509bcSBarry Smith 
3318f1509bcSBarry Smith /*@C
3328f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3338f1509bcSBarry Smith 
3348f1509bcSBarry Smith    Logically Collective on DM
3358f1509bcSBarry Smith 
3368f1509bcSBarry Smith    Input Parameters:
337521d9a4cSLisandro Dalcin +  dm - the DM context
338a2b5a043SBarry Smith -  ctype - the matrix type
339521d9a4cSLisandro Dalcin 
340521d9a4cSLisandro Dalcin    Options Database:
341521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
342521d9a4cSLisandro Dalcin 
343521d9a4cSLisandro Dalcin    Level: intermediate
344521d9a4cSLisandro Dalcin 
345a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
346521d9a4cSLisandro Dalcin @*/
34719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
348521d9a4cSLisandro Dalcin {
349521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35088f0584fSBarry Smith 
351521d9a4cSLisandro Dalcin   PetscFunctionBegin;
352521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
353521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
355521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
356521d9a4cSLisandro Dalcin }
357521d9a4cSLisandro Dalcin 
358c0dedaeaSBarry Smith /*@C
359c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
360c0dedaeaSBarry Smith 
361c0dedaeaSBarry Smith    Logically Collective on DM
362c0dedaeaSBarry Smith 
363c0dedaeaSBarry Smith    Input Parameter:
364c0dedaeaSBarry Smith .  dm - the DM context
365c0dedaeaSBarry Smith 
366c0dedaeaSBarry Smith    Output Parameter:
367c0dedaeaSBarry Smith .  ctype - the matrix type
368c0dedaeaSBarry Smith 
369c0dedaeaSBarry Smith    Options Database:
370c0dedaeaSBarry Smith .   -dm_mat_type ctype
371c0dedaeaSBarry Smith 
372c0dedaeaSBarry Smith    Level: intermediate
373c0dedaeaSBarry Smith 
374a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
375c0dedaeaSBarry Smith @*/
376c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
377c0dedaeaSBarry Smith {
378c0dedaeaSBarry Smith   PetscFunctionBegin;
379c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
380c0dedaeaSBarry Smith   *ctype = dm->mattype;
381c0dedaeaSBarry Smith   PetscFunctionReturn(0);
382c0dedaeaSBarry Smith }
383c0dedaeaSBarry Smith 
384c688c046SMatthew G Knepley /*@
38534f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
386c688c046SMatthew G Knepley 
387c688c046SMatthew G Knepley   Not collective
388c688c046SMatthew G Knepley 
389c688c046SMatthew G Knepley   Input Parameter:
390c688c046SMatthew G Knepley . A - The Mat
391c688c046SMatthew G Knepley 
392c688c046SMatthew G Knepley   Output Parameter:
393c688c046SMatthew G Knepley . dm - The DM
394c688c046SMatthew G Knepley 
395c688c046SMatthew G Knepley   Level: intermediate
396c688c046SMatthew G Knepley 
3978f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3988f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3998f1509bcSBarry Smith 
400c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
401c688c046SMatthew G Knepley @*/
402c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
403c688c046SMatthew G Knepley {
404c688c046SMatthew G Knepley   PetscErrorCode ierr;
405c688c046SMatthew G Knepley 
406c688c046SMatthew G Knepley   PetscFunctionBegin;
407c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
408c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
409c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
410c688c046SMatthew G Knepley   PetscFunctionReturn(0);
411c688c046SMatthew G Knepley }
412c688c046SMatthew G Knepley 
413c688c046SMatthew G Knepley /*@
414c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
415c688c046SMatthew G Knepley 
416c688c046SMatthew G Knepley   Not collective
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley   Input Parameters:
419c688c046SMatthew G Knepley + A - The Mat
420c688c046SMatthew G Knepley - dm - The DM
421c688c046SMatthew G Knepley 
422c688c046SMatthew G Knepley   Level: intermediate
423c688c046SMatthew G Knepley 
4248f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4258f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4268f1509bcSBarry Smith 
4278f1509bcSBarry Smith 
428c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
429c688c046SMatthew G Knepley @*/
430c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
431c688c046SMatthew G Knepley {
432c688c046SMatthew G Knepley   PetscErrorCode ierr;
433c688c046SMatthew G Knepley 
434c688c046SMatthew G Knepley   PetscFunctionBegin;
435c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4368865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
437c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
438c688c046SMatthew G Knepley   PetscFunctionReturn(0);
439c688c046SMatthew G Knepley }
440c688c046SMatthew G Knepley 
4419a42bb27SBarry Smith /*@C
4429a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4436757b960SDave May    DM options in the database.
4449a42bb27SBarry Smith 
4458353ddbbSDave May    Logically Collective on DM
4469a42bb27SBarry Smith 
4479a42bb27SBarry Smith    Input Parameter:
4488353ddbbSDave May +  da - the DM context
4499a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4509a42bb27SBarry Smith 
4519a42bb27SBarry Smith    Notes:
4529a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4539a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4549a42bb27SBarry Smith 
4559a42bb27SBarry Smith    Level: advanced
4569a42bb27SBarry Smith 
4578353ddbbSDave May .keywords: DM, set, options, prefix, database
4589a42bb27SBarry Smith 
4599a42bb27SBarry Smith .seealso: DMSetFromOptions()
4609a42bb27SBarry Smith @*/
4617087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4629a42bb27SBarry Smith {
4639a42bb27SBarry Smith   PetscErrorCode ierr;
4649a42bb27SBarry Smith 
4659a42bb27SBarry Smith   PetscFunctionBegin;
4669a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4679a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
468691be533SLawrence Mitchell   if (dm->sf) {
469691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
470691be533SLawrence Mitchell   }
471691be533SLawrence Mitchell   if (dm->defaultSF) {
472691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
473691be533SLawrence Mitchell   }
4749a42bb27SBarry Smith   PetscFunctionReturn(0);
4759a42bb27SBarry Smith }
4769a42bb27SBarry Smith 
47731697293SDave May /*@C
47831697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47931697293SDave May    DM options in the database.
48031697293SDave May 
48131697293SDave May    Logically Collective on DM
48231697293SDave May 
48331697293SDave May    Input Parameters:
48431697293SDave May +  dm - the DM context
48531697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48631697293SDave May 
48731697293SDave May    Notes:
48831697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48931697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49031697293SDave May 
49131697293SDave May    Level: advanced
49231697293SDave May 
49331697293SDave May .keywords: DM, append, options, prefix, database
49431697293SDave May 
49531697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49631697293SDave May @*/
49731697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49831697293SDave May {
49931697293SDave May   PetscErrorCode ierr;
50031697293SDave May 
50131697293SDave May   PetscFunctionBegin;
50231697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50331697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50431697293SDave May   PetscFunctionReturn(0);
50531697293SDave May }
50631697293SDave May 
50731697293SDave May /*@C
50831697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50931697293SDave May    DM options in the database.
51031697293SDave May 
51131697293SDave May    Not Collective
51231697293SDave May 
51331697293SDave May    Input Parameters:
51431697293SDave May .  dm - the DM context
51531697293SDave May 
51631697293SDave May    Output Parameters:
51731697293SDave May .  prefix - pointer to the prefix string used is returned
51831697293SDave May 
51995452b02SPatrick Sanan    Notes:
52095452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52131697293SDave May    sufficient length to hold the prefix.
52231697293SDave May 
52331697293SDave May    Level: advanced
52431697293SDave May 
52531697293SDave May .keywords: DM, set, options, prefix, database
52631697293SDave May 
52731697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52831697293SDave May @*/
52931697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
53031697293SDave May {
53131697293SDave May   PetscErrorCode ierr;
53231697293SDave May 
53331697293SDave May   PetscFunctionBegin;
53431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53531697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53631697293SDave May   PetscFunctionReturn(0);
53731697293SDave May }
53831697293SDave May 
53988bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
54088bdff64SToby Isaac {
54188bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
54288bdff64SToby Isaac   DMNamedVecLink nlink;
54388bdff64SToby Isaac   PetscErrorCode ierr;
54488bdff64SToby Isaac 
54588bdff64SToby Isaac   PetscFunctionBegin;
546aab5bcd8SJed Brown   *ncrefct = 0;
54788bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
54888bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54988bdff64SToby Isaac     if (dm->localin[i])  refct--;
55088bdff64SToby Isaac     if (dm->globalin[i]) refct--;
55188bdff64SToby Isaac   }
55288bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
55388bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
55488bdff64SToby Isaac   if (dm->x) {
55588bdff64SToby Isaac     DM obj;
55688bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
55788bdff64SToby Isaac     if (obj == dm) refct--;
55888bdff64SToby Isaac   }
55988bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56088bdff64SToby Isaac     refct--;
56188bdff64SToby Isaac     if (recurseCoarse) {
56288bdff64SToby Isaac       PetscInt coarseCount;
56388bdff64SToby Isaac 
56488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
56588bdff64SToby Isaac       refct += coarseCount;
56688bdff64SToby Isaac     }
56788bdff64SToby Isaac   }
56888bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56988bdff64SToby Isaac     refct--;
57088bdff64SToby Isaac     if (recurseFine) {
57188bdff64SToby Isaac       PetscInt fineCount;
57288bdff64SToby Isaac 
57388bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
57488bdff64SToby Isaac       refct += fineCount;
57588bdff64SToby Isaac     }
57688bdff64SToby Isaac   }
57788bdff64SToby Isaac   *ncrefct = refct;
57888bdff64SToby Isaac   PetscFunctionReturn(0);
57988bdff64SToby Isaac }
58088bdff64SToby Isaac 
581354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
582354557abSToby Isaac {
583354557abSToby Isaac   PetscErrorCode ierr;
584354557abSToby Isaac 
585354557abSToby Isaac   PetscFunctionBegin;
586354557abSToby Isaac   if (!--(dm->labels->refct)) {
587354557abSToby Isaac     DMLabelLink next = dm->labels->next;
588354557abSToby Isaac 
589354557abSToby Isaac     /* destroy the labels */
590354557abSToby Isaac     while (next) {
591354557abSToby Isaac       DMLabelLink tmp = next->next;
592354557abSToby Isaac 
593354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
594354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
595354557abSToby Isaac       next = tmp;
596354557abSToby Isaac     }
597354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
598354557abSToby Isaac   }
599354557abSToby Isaac   PetscFunctionReturn(0);
600354557abSToby Isaac }
601354557abSToby Isaac 
60247c6ae99SBarry Smith /*@
6038472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith     Collective on DM
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith     Input Parameter:
60847c6ae99SBarry Smith .   dm - the DM object to destroy
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith     Level: developer
61147c6ae99SBarry Smith 
612e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith @*/
615fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
61647c6ae99SBarry Smith {
61788bdff64SToby Isaac   PetscInt       i, cnt;
618dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61947c6ae99SBarry Smith   PetscErrorCode ierr;
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith   PetscFunctionBegin;
6226bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6236bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
62487e657c6SBarry Smith 
62588bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
62688bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62788bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
62888bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
629732e2eb9SMatthew G Knepley   /*
630732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
631732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
632732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
633732e2eb9SMatthew G Knepley   */
6346bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6356bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
636732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6376bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6386bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
639732e2eb9SMatthew G Knepley   }
640f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6410298fd71SBarry Smith   (*dm)->namedglobal = NULL;
642f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6432348bcf4SPeter Brune     nnext = nlink->next;
6442348bcf4SPeter 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);
6452348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6462348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6472348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6482348bcf4SPeter Brune   }
649f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6500298fd71SBarry Smith   (*dm)->namedlocal = NULL;
651f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
652f490541aSPeter Brune     nnext = nlink->next;
653f490541aSPeter 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);
654f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
655f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
656f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
657f490541aSPeter Brune   }
6582348bcf4SPeter Brune 
659b17ce1afSJed Brown   /* Destroy the list of hooks */
660c833c3b5SJed Brown   {
661c833c3b5SJed Brown     DMCoarsenHookLink link,next;
662b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
663b17ce1afSJed Brown       next = link->next;
664b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
665b17ce1afSJed Brown     }
6660298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
667c833c3b5SJed Brown   }
668c833c3b5SJed Brown   {
669c833c3b5SJed Brown     DMRefineHookLink link,next;
670c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
671c833c3b5SJed Brown       next = link->next;
672c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
673c833c3b5SJed Brown     }
6740298fd71SBarry Smith     (*dm)->refinehook = NULL;
675c833c3b5SJed Brown   }
676be081cd6SPeter Brune   {
677be081cd6SPeter Brune     DMSubDomainHookLink link,next;
678be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
679be081cd6SPeter Brune       next = link->next;
680be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
681be081cd6SPeter Brune     }
6820298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
683be081cd6SPeter Brune   }
684baf369e7SPeter Brune   {
685baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
686baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
687baf369e7SPeter Brune       next = link->next;
688baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
689baf369e7SPeter Brune     }
6900298fd71SBarry Smith     (*dm)->gtolhook = NULL;
691baf369e7SPeter Brune   }
692d4d07f1eSToby Isaac   {
693d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
694d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
695d4d07f1eSToby Isaac       next = link->next;
696d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
697d4d07f1eSToby Isaac     }
698d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
699d4d07f1eSToby Isaac   }
700aa1993deSMatthew G Knepley   /* Destroy the work arrays */
701aa1993deSMatthew G Knepley   {
702aa1993deSMatthew G Knepley     DMWorkLink link,next;
703aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
704aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
705aa1993deSMatthew G Knepley       next = link->next;
706aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
707aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
708aa1993deSMatthew G Knepley     }
7090298fd71SBarry Smith     (*dm)->workin = NULL;
710aa1993deSMatthew G Knepley   }
711c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
712c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
713c58f1c22SToby Isaac 
714c58f1c22SToby Isaac     /* destroy the labels */
715c58f1c22SToby Isaac     while (next) {
716c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
717c58f1c22SToby Isaac 
718c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
719c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
720c58f1c22SToby Isaac       next = tmp;
721c58f1c22SToby Isaac     }
722c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
723c58f1c22SToby Isaac   }
724e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
725e6f8dbb6SToby Isaac   {
726e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
727e6f8dbb6SToby Isaac     while (next) {
728e6f8dbb6SToby Isaac       DMBoundary b = next;
729e6f8dbb6SToby Isaac 
730e6f8dbb6SToby Isaac       next = b->next;
731e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
732e6f8dbb6SToby Isaac     }
733e6f8dbb6SToby Isaac   }
734b17ce1afSJed Brown 
73552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
73652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
73752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
73852536dc3SBarry Smith 
7391a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7401a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7411a266240SBarry Smith   }
74287e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
74371cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7444dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7456bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7466bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
747073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
74888ed4aceSMatthew G Knepley 
74988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
75088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
7518b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
752fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
753fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
75488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
75588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
756736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
757736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7588e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
759736995cdSBlaise Bourdin     }
760736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
761736995cdSBlaise Bourdin   }
76288bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
76388bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
76488bdff64SToby Isaac   }
765a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
76688bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
76788bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76888bdff64SToby Isaac   }
76988bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
770f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7716636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7726636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7736636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7745dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
7756636e97aSMatthew G Knepley 
776e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
77714f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
778e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
779e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
780732e2eb9SMatthew G Knepley 
781ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7826bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
783ed3c66a1SDave May   }
784435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
785732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
78647c6ae99SBarry Smith   PetscFunctionReturn(0);
78747c6ae99SBarry Smith }
78847c6ae99SBarry Smith 
789d7bf68aeSBarry Smith /*@
790d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith     Collective on DM
793d7bf68aeSBarry Smith 
794d7bf68aeSBarry Smith     Input Parameter:
795d7bf68aeSBarry Smith .   dm - the DM object to setup
796d7bf68aeSBarry Smith 
797d7bf68aeSBarry Smith     Level: developer
798d7bf68aeSBarry Smith 
799e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
800d7bf68aeSBarry Smith 
801d7bf68aeSBarry Smith @*/
8027087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
803d7bf68aeSBarry Smith {
804d7bf68aeSBarry Smith   PetscErrorCode ierr;
805d7bf68aeSBarry Smith 
806d7bf68aeSBarry Smith   PetscFunctionBegin;
807171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8088387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
809d7bf68aeSBarry Smith   if (dm->ops->setup) {
810d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
811d7bf68aeSBarry Smith   }
8128387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
813d7bf68aeSBarry Smith   PetscFunctionReturn(0);
814d7bf68aeSBarry Smith }
815d7bf68aeSBarry Smith 
816d7bf68aeSBarry Smith /*@
817d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
818d7bf68aeSBarry Smith 
819d7bf68aeSBarry Smith     Collective on DM
820d7bf68aeSBarry Smith 
821d7bf68aeSBarry Smith     Input Parameter:
822d7bf68aeSBarry Smith .   dm - the DM object to set options for
823d7bf68aeSBarry Smith 
824732e2eb9SMatthew G Knepley     Options Database:
8254164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8264164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8274164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8288f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
829732e2eb9SMatthew G Knepley 
830d7bf68aeSBarry Smith     Level: developer
831d7bf68aeSBarry Smith 
832e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
833d7bf68aeSBarry Smith 
834d7bf68aeSBarry Smith @*/
8357087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
836d7bf68aeSBarry Smith {
8377781c08eSBarry Smith   char           typeName[256];
838ca266f36SBarry Smith   PetscBool      flg;
839d7bf68aeSBarry Smith   PetscErrorCode ierr;
840d7bf68aeSBarry Smith 
841d7bf68aeSBarry Smith   PetscFunctionBegin;
842171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
84449be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
84549be4549SMatthew G. Knepley   if (dm->defaultSF) {ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);}
8463194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8470298fd71SBarry 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);
848a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
849f9ba7244SBarry Smith   if (flg) {
850f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
851f9ba7244SBarry Smith   }
852a264d7a6SBarry 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);
853073dac72SJed Brown   if (flg) {
854521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
855073dac72SJed Brown   }
8568f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
857f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
858e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
859f9ba7244SBarry Smith   }
860f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8610633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
86282fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
863d7bf68aeSBarry Smith   PetscFunctionReturn(0);
864d7bf68aeSBarry Smith }
865d7bf68aeSBarry Smith 
866fc9bc008SSatish Balay /*@C
867224748a4SBarry Smith     DMView - Views a DM
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith     Collective on DM
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith     Input Parameter:
87247c6ae99SBarry Smith +   dm - the DM object to view
87347c6ae99SBarry Smith -   v - the viewer
87447c6ae99SBarry Smith 
875224748a4SBarry Smith     Level: beginner
87647c6ae99SBarry Smith 
877e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith @*/
8807087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
88147c6ae99SBarry Smith {
88247c6ae99SBarry Smith   PetscErrorCode    ierr;
88332c0f0efSBarry Smith   PetscBool         isbinary;
88476a8abe0SBarry Smith   PetscMPIInt       size;
88576a8abe0SBarry Smith   PetscViewerFormat format;
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith   PetscFunctionBegin;
888171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8893014e516SBarry Smith   if (!v) {
890ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8913014e516SBarry Smith   }
8928bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
89376a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
89476a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
89576a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
89698c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
89732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
89832c0f0efSBarry Smith   if (isbinary) {
89955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
90032c0f0efSBarry Smith     char     type[256];
90132c0f0efSBarry Smith 
90232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
90332c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
90432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
90532c0f0efSBarry Smith   }
9060c010503SBarry Smith   if (dm->ops->view) {
9070c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
90847c6ae99SBarry Smith   }
90947c6ae99SBarry Smith   PetscFunctionReturn(0);
91047c6ae99SBarry Smith }
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith /*@
9138472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith     Collective on DM
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith     Input Parameter:
91847c6ae99SBarry Smith .   dm - the DM object
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith     Output Parameter:
92147c6ae99SBarry Smith .   vec - the global vector
92247c6ae99SBarry Smith 
923073dac72SJed Brown     Level: beginner
92447c6ae99SBarry Smith 
925e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith @*/
9287087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
92947c6ae99SBarry Smith {
93047c6ae99SBarry Smith   PetscErrorCode ierr;
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith   PetscFunctionBegin;
933171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
93547c6ae99SBarry Smith   PetscFunctionReturn(0);
93647c6ae99SBarry Smith }
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith /*@
9398472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith     Not Collective
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith     Input Parameter:
94447c6ae99SBarry Smith .   dm - the DM object
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith     Output Parameter:
94747c6ae99SBarry Smith .   vec - the local vector
94847c6ae99SBarry Smith 
949073dac72SJed Brown     Level: beginner
95047c6ae99SBarry Smith 
951e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith @*/
9547087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
95547c6ae99SBarry Smith {
95647c6ae99SBarry Smith   PetscErrorCode ierr;
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith   PetscFunctionBegin;
959171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
96047c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
96147c6ae99SBarry Smith   PetscFunctionReturn(0);
96247c6ae99SBarry Smith }
96347c6ae99SBarry Smith 
9641411c6eeSJed Brown /*@
9651411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9661411c6eeSJed Brown 
9671411c6eeSJed Brown    Collective on DM
9681411c6eeSJed Brown 
9691411c6eeSJed Brown    Input Parameter:
9701411c6eeSJed Brown .  dm - the DM that provides the mapping
9711411c6eeSJed Brown 
9721411c6eeSJed Brown    Output Parameter:
9731411c6eeSJed Brown .  ltog - the mapping
9741411c6eeSJed Brown 
9751411c6eeSJed Brown    Level: intermediate
9761411c6eeSJed Brown 
9771411c6eeSJed Brown    Notes:
9781411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9791411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9801411c6eeSJed Brown 
981fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9821411c6eeSJed Brown @*/
9837087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9841411c6eeSJed Brown {
9850be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9861411c6eeSJed Brown   PetscErrorCode ierr;
9871411c6eeSJed Brown 
9881411c6eeSJed Brown   PetscFunctionBegin;
9891411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9901411c6eeSJed Brown   PetscValidPointer(ltog,2);
9911411c6eeSJed Brown   if (!dm->ltogmap) {
99237d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
99337d0c07bSMatthew G Knepley 
994e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
99537d0c07bSMatthew G Knepley     if (section) {
996a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
99737d0c07bSMatthew G Knepley       PetscInt       *ltog;
998ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
99937d0c07bSMatthew G Knepley 
1000e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
100137d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1002ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1003ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
100437d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1005a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
100637d0c07bSMatthew G Knepley 
100737d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
100837d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1009a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1010a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
101137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10121a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10131a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10141a7dc684SMatthew G. Knepley         if (dof) {
1015b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1016b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10171a7dc684SMatthew G. Knepley         }
101837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1019a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1020a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
102137d0c07bSMatthew G Knepley         }
102237d0c07bSMatthew G Knepley       }
1023bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10240be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10250be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10260be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10270be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10287591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10297591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1030ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1031ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1032ccf3bd66SMatthew G. Knepley         n /= bs;
1033ccf3bd66SMatthew G. Knepley       }
1034ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10353bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
103637d0c07bSMatthew G Knepley     } else {
1037184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1038184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10391411c6eeSJed Brown     }
104037d0c07bSMatthew G Knepley   }
10411411c6eeSJed Brown   *ltog = dm->ltogmap;
10421411c6eeSJed Brown   PetscFunctionReturn(0);
10431411c6eeSJed Brown }
10441411c6eeSJed Brown 
10451411c6eeSJed Brown /*@
10461411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10471411c6eeSJed Brown 
10481411c6eeSJed Brown    Not Collective
10491411c6eeSJed Brown 
10501411c6eeSJed Brown    Input Parameter:
10511411c6eeSJed Brown .  dm - the DM with block structure
10521411c6eeSJed Brown 
10531411c6eeSJed Brown    Output Parameter:
10541411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10551411c6eeSJed Brown 
10561411c6eeSJed Brown    Level: intermediate
10571411c6eeSJed Brown 
1058fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10591411c6eeSJed Brown @*/
10607087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10611411c6eeSJed Brown {
10621411c6eeSJed Brown   PetscFunctionBegin;
10631411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10641411c6eeSJed Brown   PetscValidPointer(bs,2);
10651411c6eeSJed 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");
10661411c6eeSJed Brown   *bs = dm->bs;
10671411c6eeSJed Brown   PetscFunctionReturn(0);
10681411c6eeSJed Brown }
10691411c6eeSJed Brown 
107047c6ae99SBarry Smith /*@
10718472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith     Collective on DM
107447c6ae99SBarry Smith 
107547c6ae99SBarry Smith     Input Parameter:
107647c6ae99SBarry Smith +   dm1 - the DM object
107747c6ae99SBarry Smith -   dm2 - the second, finer DM object
107847c6ae99SBarry Smith 
107947c6ae99SBarry Smith     Output Parameter:
108047c6ae99SBarry Smith +  mat - the interpolation
108147c6ae99SBarry Smith -  vec - the scaling (optional)
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith     Level: developer
108447c6ae99SBarry Smith 
108595452b02SPatrick Sanan     Notes:
108695452b02SPatrick 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
108785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1088d52bd9f3SBarry Smith 
10891f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1090d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
109185afcc9aSBarry Smith 
109285afcc9aSBarry Smith 
10933ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
109447c6ae99SBarry Smith 
109547c6ae99SBarry Smith @*/
1096e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
109747c6ae99SBarry Smith {
109847c6ae99SBarry Smith   PetscErrorCode ierr;
109947c6ae99SBarry Smith 
110047c6ae99SBarry Smith   PetscFunctionBegin;
1101171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1102171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1103c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1104c7d20fa0SStefano Zampini   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInterpolation not implemented for type %s",((PetscObject)dm1)->type_name);
1105cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1107cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110847c6ae99SBarry Smith   PetscFunctionReturn(0);
110947c6ae99SBarry Smith }
111047c6ae99SBarry Smith 
11113ad4599aSBarry Smith /*@
11123ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11133ad4599aSBarry Smith 
11143ad4599aSBarry Smith     Collective on DM
11153ad4599aSBarry Smith 
11163ad4599aSBarry Smith     Input Parameter:
11173ad4599aSBarry Smith +   dm1 - the DM object
11183ad4599aSBarry Smith -   dm2 - the second, finer DM object
11193ad4599aSBarry Smith 
11203ad4599aSBarry Smith     Output Parameter:
11213ad4599aSBarry Smith .  mat - the restriction
11223ad4599aSBarry Smith 
11233ad4599aSBarry Smith 
11243ad4599aSBarry Smith     Level: developer
11253ad4599aSBarry Smith 
112695452b02SPatrick Sanan     Notes:
112795452b02SPatrick 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
11283ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11293ad4599aSBarry Smith 
11303ad4599aSBarry Smith 
11313ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11323ad4599aSBarry Smith 
11333ad4599aSBarry Smith @*/
11343ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11353ad4599aSBarry Smith {
11363ad4599aSBarry Smith   PetscErrorCode ierr;
11373ad4599aSBarry Smith 
11383ad4599aSBarry Smith   PetscFunctionBegin;
11393ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11403ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11413ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11427294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11433ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11443ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11453ad4599aSBarry Smith   PetscFunctionReturn(0);
11463ad4599aSBarry Smith }
11473ad4599aSBarry Smith 
114847c6ae99SBarry Smith /*@
11498472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith     Collective on DM
115247c6ae99SBarry Smith 
115347c6ae99SBarry Smith     Input Parameter:
115447c6ae99SBarry Smith +   dm1 - the DM object
115547c6ae99SBarry Smith -   dm2 - the second, finer DM object
115647c6ae99SBarry Smith 
115747c6ae99SBarry Smith     Output Parameter:
11586dbf9973SLawrence Mitchell .   mat - the injection
115947c6ae99SBarry Smith 
116047c6ae99SBarry Smith     Level: developer
116147c6ae99SBarry Smith 
116295452b02SPatrick Sanan    Notes:
116395452b02SPatrick 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
116485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
116585afcc9aSBarry Smith 
1166e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
116747c6ae99SBarry Smith 
116847c6ae99SBarry Smith @*/
11696dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
117047c6ae99SBarry Smith {
117147c6ae99SBarry Smith   PetscErrorCode ierr;
117247c6ae99SBarry Smith 
117347c6ae99SBarry Smith   PetscFunctionBegin;
1174171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1175171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11760c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11776dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
117847c6ae99SBarry Smith   PetscFunctionReturn(0);
117947c6ae99SBarry Smith }
118047c6ae99SBarry Smith 
1181b412c318SBarry Smith /*@
1182bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1183bd041c0cSMatthew G. Knepley 
1184bd041c0cSMatthew G. Knepley   Collective on DM
1185bd041c0cSMatthew G. Knepley 
1186bd041c0cSMatthew G. Knepley   Input Parameter:
1187bd041c0cSMatthew G. Knepley + dm1 - the DM object
1188bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1189bd041c0cSMatthew G. Knepley 
1190bd041c0cSMatthew G. Knepley   Output Parameter:
1191bd041c0cSMatthew G. Knepley . mat - the interpolation
1192bd041c0cSMatthew G. Knepley 
1193bd041c0cSMatthew G. Knepley   Level: developer
1194bd041c0cSMatthew G. Knepley 
1195bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1196bd041c0cSMatthew G. Knepley @*/
1197bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1198bd041c0cSMatthew G. Knepley {
1199bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1200bd041c0cSMatthew G. Knepley 
1201bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1202bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1203bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1204bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1205bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1206bd041c0cSMatthew G. Knepley }
1207bd041c0cSMatthew G. Knepley 
1208bd041c0cSMatthew G. Knepley /*@
1209b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
121047c6ae99SBarry Smith 
121147c6ae99SBarry Smith     Collective on DM
121247c6ae99SBarry Smith 
121347c6ae99SBarry Smith     Input Parameter:
121447c6ae99SBarry Smith +   dm - the DM object
12155bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
121647c6ae99SBarry Smith 
121747c6ae99SBarry Smith     Output Parameter:
121847c6ae99SBarry Smith .   coloring - the coloring
121947c6ae99SBarry Smith 
122047c6ae99SBarry Smith     Level: developer
122147c6ae99SBarry Smith 
1222b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
122347c6ae99SBarry Smith 
1224aab9d709SJed Brown @*/
1225b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
122647c6ae99SBarry Smith {
122747c6ae99SBarry Smith   PetscErrorCode ierr;
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith   PetscFunctionBegin;
1230171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1231ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1232b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
123347c6ae99SBarry Smith   PetscFunctionReturn(0);
123447c6ae99SBarry Smith }
123547c6ae99SBarry Smith 
1236b412c318SBarry Smith /*@
12378472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
123847c6ae99SBarry Smith 
123947c6ae99SBarry Smith     Collective on DM
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith     Input Parameter:
1242b412c318SBarry Smith .   dm - the DM object
124347c6ae99SBarry Smith 
124447c6ae99SBarry Smith     Output Parameter:
124547c6ae99SBarry Smith .   mat - the empty Jacobian
124647c6ae99SBarry Smith 
1247073dac72SJed Brown     Level: beginner
124847c6ae99SBarry Smith 
124995452b02SPatrick Sanan     Notes:
125095452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
125194013140SBarry Smith        do not need to do it yourself.
125294013140SBarry Smith 
125394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12547889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
125594013140SBarry Smith 
125694013140SBarry 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
125794013140SBarry Smith        internally by PETSc.
125894013140SBarry Smith 
125994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1260aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
126194013140SBarry Smith 
1262b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
126347c6ae99SBarry Smith 
1264aab9d709SJed Brown @*/
1265b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
126647c6ae99SBarry Smith {
126747c6ae99SBarry Smith   PetscErrorCode ierr;
126847c6ae99SBarry Smith 
126947c6ae99SBarry Smith   PetscFunctionBegin;
1270171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1271607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1272c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1273c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1274b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1275e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1276e5e52638SMatthew G. Knepley   if (dm->Nf) {
1277e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1278e571a35bSMatthew G. Knepley     PetscInt     Nf;
1279e571a35bSMatthew G. Knepley 
1280e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1281e571a35bSMatthew G. Knepley     if (Nf == 1) {
1282e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1283e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1284e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1285e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1286e571a35bSMatthew G. Knepley       }
1287e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1288e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1289e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1290e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1291e571a35bSMatthew G. Knepley       }
1292e571a35bSMatthew G. Knepley     }
1293e571a35bSMatthew G. Knepley   }
129447c6ae99SBarry Smith   PetscFunctionReturn(0);
129547c6ae99SBarry Smith }
129647c6ae99SBarry Smith 
1297732e2eb9SMatthew G Knepley /*@
1298950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1299732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1300732e2eb9SMatthew G Knepley 
13018472ad0fSDave May   Logically Collective on DM
1302732e2eb9SMatthew G Knepley 
1303732e2eb9SMatthew G Knepley   Input Parameter:
1304732e2eb9SMatthew G Knepley + dm - the DM
1305732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1306732e2eb9SMatthew G Knepley 
1307732e2eb9SMatthew G Knepley   Level: developer
1308b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1309732e2eb9SMatthew G Knepley @*/
1310732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1311732e2eb9SMatthew G Knepley {
1312732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1313732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1314732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1315732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1316732e2eb9SMatthew G Knepley }
1317732e2eb9SMatthew G Knepley 
1318b06ff27eSHong Zhang /*@
1319b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1320b06ff27eSHong Zhang     but the array for values will not be allocated.
1321b06ff27eSHong Zhang 
1322b06ff27eSHong Zhang   Logically Collective on DM
1323b06ff27eSHong Zhang 
1324b06ff27eSHong Zhang   Input Parameter:
1325b06ff27eSHong Zhang + dm - the DM
1326b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1327b06ff27eSHong Zhang 
1328b06ff27eSHong Zhang   Level: developer
1329b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1330b06ff27eSHong Zhang @*/
1331b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1332b06ff27eSHong Zhang {
1333b06ff27eSHong Zhang   PetscFunctionBegin;
1334b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1335b06ff27eSHong Zhang   dm->structure_only = only;
1336b06ff27eSHong Zhang   PetscFunctionReturn(0);
1337b06ff27eSHong Zhang }
1338b06ff27eSHong Zhang 
1339a89ea682SMatthew G Knepley /*@C
1340aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1341a89ea682SMatthew G Knepley 
1342a89ea682SMatthew G Knepley   Not Collective
1343a89ea682SMatthew G Knepley 
1344a89ea682SMatthew G Knepley   Input Parameters:
1345a89ea682SMatthew G Knepley + dm - the DM object
1346aa1993deSMatthew G Knepley . count - The minium size
134769291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1348a89ea682SMatthew G Knepley 
1349a89ea682SMatthew G Knepley   Output Parameter:
1350a89ea682SMatthew G Knepley . array - the work array
1351a89ea682SMatthew G Knepley 
1352a89ea682SMatthew G Knepley   Level: developer
1353a89ea682SMatthew G Knepley 
1354a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1355a89ea682SMatthew G Knepley @*/
135669291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1357a89ea682SMatthew G Knepley {
1358a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1359aa1993deSMatthew G Knepley   DMWorkLink     link;
136069291d52SBarry Smith   PetscMPIInt    dsize;
1361a89ea682SMatthew G Knepley 
1362a89ea682SMatthew G Knepley   PetscFunctionBegin;
1363a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1364aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1365aa1993deSMatthew G Knepley   if (dm->workin) {
1366aa1993deSMatthew G Knepley     link       = dm->workin;
1367aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1368aa1993deSMatthew G Knepley   } else {
1369b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1370a89ea682SMatthew G Knepley   }
137169291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13725056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1373aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1374854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1375854ce69bSBarry Smith     link->bytes = dsize*count;
1376aa1993deSMatthew G Knepley   }
1377aa1993deSMatthew G Knepley   link->next   = dm->workout;
1378aa1993deSMatthew G Knepley   dm->workout  = link;
1379aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1380a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1381a89ea682SMatthew G Knepley }
1382a89ea682SMatthew G Knepley 
1383aa1993deSMatthew G Knepley /*@C
1384aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1385aa1993deSMatthew G Knepley 
1386aa1993deSMatthew G Knepley   Not Collective
1387aa1993deSMatthew G Knepley 
1388aa1993deSMatthew G Knepley   Input Parameters:
1389aa1993deSMatthew G Knepley + dm - the DM object
1390aa1993deSMatthew G Knepley . count - The minium size
139169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1392aa1993deSMatthew G Knepley 
1393aa1993deSMatthew G Knepley   Output Parameter:
1394aa1993deSMatthew G Knepley . array - the work array
1395aa1993deSMatthew G Knepley 
1396aa1993deSMatthew G Knepley   Level: developer
1397aa1993deSMatthew G Knepley 
139895452b02SPatrick Sanan   Developer Notes:
139995452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1400aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1401aa1993deSMatthew G Knepley @*/
140269291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1403aa1993deSMatthew G Knepley {
1404aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1405aa1993deSMatthew G Knepley 
1406aa1993deSMatthew G Knepley   PetscFunctionBegin;
1407aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1408aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1409aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1410aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1411aa1993deSMatthew G Knepley       *p           = link->next;
1412aa1993deSMatthew G Knepley       link->next   = dm->workin;
1413aa1993deSMatthew G Knepley       dm->workin   = link;
14140298fd71SBarry Smith       *(void**)mem = NULL;
1415aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1416aa1993deSMatthew G Knepley     }
1417aa1993deSMatthew G Knepley   }
1418aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1419aa1993deSMatthew G Knepley }
1420e7c4fc90SDmitry Karpeev 
1421435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1422435a35e8SMatthew G Knepley {
1423435a35e8SMatthew G Knepley   PetscFunctionBegin;
1424435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
142582f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1426435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1427435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1428435a35e8SMatthew G Knepley }
1429435a35e8SMatthew G Knepley 
14300a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14310a50eb56SMatthew G. Knepley {
14320a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14330a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1434f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14350a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14360a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14370a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14380a50eb56SMatthew G. Knepley }
14390a50eb56SMatthew G. Knepley 
1440f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1441f9d4088aSMatthew G. Knepley {
1442f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1443f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1444f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1445f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1446f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1447f9d4088aSMatthew G. Knepley }
1448f9d4088aSMatthew G. Knepley 
1449f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1450f9d4088aSMatthew G. Knepley {
1451f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1452f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1453f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1454f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1455f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1456f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1457f9d4088aSMatthew G. Knepley }
1458f9d4088aSMatthew G. Knepley 
14594f3b5142SJed Brown /*@C
14604d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14614d343eeaSMatthew G Knepley 
14624d343eeaSMatthew G Knepley   Not collective
14634d343eeaSMatthew G Knepley 
14644d343eeaSMatthew G Knepley   Input Parameter:
14654d343eeaSMatthew G Knepley . dm - the DM object
14664d343eeaSMatthew G Knepley 
14674d343eeaSMatthew G Knepley   Output Parameters:
14680298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14690298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14700298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14714d343eeaSMatthew G Knepley 
14724d343eeaSMatthew G Knepley   Level: intermediate
14734d343eeaSMatthew G Knepley 
147421c9b008SJed Brown   Notes:
147521c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
147621c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
147721c9b008SJed Brown   PetscFree().
147821c9b008SJed Brown 
14794d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14804d343eeaSMatthew G Knepley @*/
148137d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14824d343eeaSMatthew G Knepley {
148337d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14844d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14854d343eeaSMatthew G Knepley 
14864d343eeaSMatthew G Knepley   PetscFunctionBegin;
14874d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
148869ca1f37SDmitry Karpeev   if (numFields) {
148969ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
149069ca1f37SDmitry Karpeev     *numFields = 0;
149169ca1f37SDmitry Karpeev   }
149237d0c07bSMatthew G Knepley   if (fieldNames) {
149337d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14940298fd71SBarry Smith     *fieldNames = NULL;
149569ca1f37SDmitry Karpeev   }
149669ca1f37SDmitry Karpeev   if (fields) {
149769ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
14980298fd71SBarry Smith     *fields = NULL;
149969ca1f37SDmitry Karpeev   }
1500e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
150137d0c07bSMatthew G Knepley   if (section) {
15023a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
150337d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
150437d0c07bSMatthew G Knepley 
1505e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
150637d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15073a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
150837d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
150937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
151037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15113a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
151237d0c07bSMatthew G Knepley     }
151337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
151437d0c07bSMatthew G Knepley       PetscInt gdof;
151537d0c07bSMatthew G Knepley 
151637d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
151737d0c07bSMatthew G Knepley       if (gdof > 0) {
151837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15193a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
152037d0c07bSMatthew G Knepley 
152137d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
152237d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15233a544194SStefano Zampini           fpdof = fdof-fcdof;
15243a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15253a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15263a544194SStefano Zampini             fieldNc[f] = 1;
15273a544194SStefano Zampini           }
15283a544194SStefano Zampini           fieldSizes[f] += fpdof;
152937d0c07bSMatthew G Knepley         }
153037d0c07bSMatthew G Knepley       }
153137d0c07bSMatthew G Knepley     }
153237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1533785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
153437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
153537d0c07bSMatthew G Knepley     }
153637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
153737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
153837d0c07bSMatthew G Knepley 
153937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
154037d0c07bSMatthew G Knepley       if (gdof > 0) {
154137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
154237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
154337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
154437d0c07bSMatthew G Knepley 
154537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
154637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
154737d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
154837d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
154937d0c07bSMatthew G Knepley           }
155037d0c07bSMatthew G Knepley         }
155137d0c07bSMatthew G Knepley       }
155237d0c07bSMatthew G Knepley     }
15538865f1eaSKarl Rupp     if (numFields) *numFields = nF;
155437d0c07bSMatthew G Knepley     if (fieldNames) {
1555785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
155637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
155737d0c07bSMatthew G Knepley         const char *fieldName;
155837d0c07bSMatthew G Knepley 
155937d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
156037d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
156137d0c07bSMatthew G Knepley       }
156237d0c07bSMatthew G Knepley     }
156337d0c07bSMatthew G Knepley     if (fields) {
1564785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
156537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15663a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15673a544194SStefano Zampini 
156882f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15693a544194SStefano Zampini         in[0] = -fieldNc[f];
15703a544194SStefano Zampini         in[1] = fieldNc[f];
15713a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15723a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15733a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
157437d0c07bSMatthew G Knepley       }
157537d0c07bSMatthew G Knepley     }
15763a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15778865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15788865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
157969ca1f37SDmitry Karpeev   }
15804d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15814d343eeaSMatthew G Knepley }
15824d343eeaSMatthew G Knepley 
158316621825SDmitry Karpeev 
158416621825SDmitry Karpeev /*@C
158516621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
158616621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
158716621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1588e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1589e7c4fc90SDmitry Karpeev 
1590e7c4fc90SDmitry Karpeev   Not collective
1591e7c4fc90SDmitry Karpeev 
1592e7c4fc90SDmitry Karpeev   Input Parameter:
1593e7c4fc90SDmitry Karpeev . dm - the DM object
1594e7c4fc90SDmitry Karpeev 
1595e7c4fc90SDmitry Karpeev   Output Parameters:
15960298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15970298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15980298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15990298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1600e7c4fc90SDmitry Karpeev 
1601e7c4fc90SDmitry Karpeev   Level: intermediate
1602e7c4fc90SDmitry Karpeev 
1603e7c4fc90SDmitry Karpeev   Notes:
1604e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1605e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1606e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1607e7c4fc90SDmitry Karpeev 
1608e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1609e7c4fc90SDmitry Karpeev @*/
161016621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1611e7c4fc90SDmitry Karpeev {
1612e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1613e7c4fc90SDmitry Karpeev 
1614e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1615e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16168865f1eaSKarl Rupp   if (len) {
16178865f1eaSKarl Rupp     PetscValidPointer(len,2);
16188865f1eaSKarl Rupp     *len = 0;
16198865f1eaSKarl Rupp   }
16208865f1eaSKarl Rupp   if (namelist) {
16218865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16228865f1eaSKarl Rupp     *namelist = 0;
16238865f1eaSKarl Rupp   }
16248865f1eaSKarl Rupp   if (islist) {
16258865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16268865f1eaSKarl Rupp     *islist = 0;
16278865f1eaSKarl Rupp   }
16288865f1eaSKarl Rupp   if (dmlist) {
16298865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16308865f1eaSKarl Rupp     *dmlist = 0;
16318865f1eaSKarl Rupp   }
1632f3f0edfdSDmitry Karpeev   /*
1633f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1634f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1635f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1636f3f0edfdSDmitry Karpeev    */
1637ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
163816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1639435a35e8SMatthew G Knepley     PetscSection section;
1640435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1641435a35e8SMatthew G Knepley 
1642e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1643435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1644435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1645f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
164603dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
164703dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
164803dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1649435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1650435a35e8SMatthew G Knepley         const char *fieldName;
1651435a35e8SMatthew G Knepley 
165203dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
165303dc3394SMatthew G. Knepley         if (namelist) {
1654435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1655435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1656435a35e8SMatthew G Knepley         }
165703dc3394SMatthew G. Knepley       }
1658435a35e8SMatthew G Knepley     } else {
165969ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1660e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16610298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1662e7c4fc90SDmitry Karpeev     }
16638865f1eaSKarl Rupp   } else {
166416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
166516621825SDmitry Karpeev   }
166616621825SDmitry Karpeev   PetscFunctionReturn(0);
166716621825SDmitry Karpeev }
166816621825SDmitry Karpeev 
1669564cec59SMatthew G. Knepley /*@
1670435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1671435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1672435a35e8SMatthew G Knepley 
1673435a35e8SMatthew G Knepley   Not collective
1674435a35e8SMatthew G Knepley 
1675435a35e8SMatthew G Knepley   Input Parameters:
16762adcc780SMatthew G. Knepley + dm        - The DM object
16772adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16782adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1679435a35e8SMatthew G Knepley 
1680435a35e8SMatthew G Knepley   Output Parameters:
16812adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16822adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1683435a35e8SMatthew G Knepley 
16845d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16855d3b26e6SMatthew G. Knepley 
1686435a35e8SMatthew G Knepley   Level: intermediate
1687435a35e8SMatthew G Knepley 
16885d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1689435a35e8SMatthew G Knepley @*/
169037bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1691435a35e8SMatthew G Knepley {
1692435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1693435a35e8SMatthew G Knepley 
1694435a35e8SMatthew G Knepley   PetscFunctionBegin;
1695435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1696435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16978865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16988865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1699435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1700435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
170182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1702435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1703435a35e8SMatthew G Knepley }
1704435a35e8SMatthew G Knepley 
17052adcc780SMatthew G. Knepley /*@C
17062adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17072adcc780SMatthew G. Knepley 
17082adcc780SMatthew G. Knepley   Not collective
17092adcc780SMatthew G. Knepley 
17102adcc780SMatthew G. Knepley   Input Parameter:
17112adcc780SMatthew G. Knepley + dms - The DM objects
17122adcc780SMatthew G. Knepley - len - The number of DMs
17132adcc780SMatthew G. Knepley 
17142adcc780SMatthew G. Knepley   Output Parameters:
1715a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17162adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17172adcc780SMatthew G. Knepley 
17185d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17195d3b26e6SMatthew G. Knepley 
17202adcc780SMatthew G. Knepley   Level: intermediate
17212adcc780SMatthew G. Knepley 
17225d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17232adcc780SMatthew G. Knepley @*/
17242adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17252adcc780SMatthew G. Knepley {
17262adcc780SMatthew G. Knepley   PetscInt       i;
17272adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17282adcc780SMatthew G. Knepley 
17292adcc780SMatthew G. Knepley   PetscFunctionBegin;
17302adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17312adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17322adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1733a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17342adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17352adcc780SMatthew G. Knepley   if (len) {
1736a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1737a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17382adcc780SMatthew G. Knepley   }
17392adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17402adcc780SMatthew G. Knepley }
17412adcc780SMatthew G. Knepley 
174216621825SDmitry Karpeev 
174316621825SDmitry Karpeev /*@C
17448d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17458d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17468d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17478d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17488d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
174916621825SDmitry Karpeev 
175016621825SDmitry Karpeev   Not collective
175116621825SDmitry Karpeev 
175216621825SDmitry Karpeev   Input Parameter:
175316621825SDmitry Karpeev . dm - the DM object
175416621825SDmitry Karpeev 
175516621825SDmitry Karpeev   Output Parameters:
17560298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17570298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17580298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17590298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17600298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
176116621825SDmitry Karpeev 
176216621825SDmitry Karpeev   Level: intermediate
176316621825SDmitry Karpeev 
176416621825SDmitry Karpeev   Notes:
176516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
176616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
176716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
176816621825SDmitry Karpeev 
17698d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
177016621825SDmitry Karpeev @*/
17718d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
177216621825SDmitry Karpeev {
177316621825SDmitry Karpeev   PetscErrorCode      ierr;
1774be081cd6SPeter Brune   DMSubDomainHookLink link;
1775be081cd6SPeter Brune   PetscInt            i,l;
177616621825SDmitry Karpeev 
177716621825SDmitry Karpeev   PetscFunctionBegin;
177816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
177914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17800298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17810298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17820298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17830298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1784f3f0edfdSDmitry Karpeev   /*
1785f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1786f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1787f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1788f3f0edfdSDmitry Karpeev    */
1789ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
179016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1791be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
179214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1793f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1794be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1795be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1796be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1797be081cd6SPeter Brune         }
1798648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1799e7c4fc90SDmitry Karpeev       }
180014a18fd3SPeter Brune     }
180114a18fd3SPeter Brune     if (len) *len = l;
180214a18fd3SPeter Brune   }
1803e30e807fSPeter Brune   PetscFunctionReturn(0);
1804e30e807fSPeter Brune }
1805e30e807fSPeter Brune 
1806e30e807fSPeter Brune 
1807e30e807fSPeter Brune /*@C
1808e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1809e30e807fSPeter Brune 
1810e30e807fSPeter Brune   Not collective
1811e30e807fSPeter Brune 
1812e30e807fSPeter Brune   Input Parameters:
1813e30e807fSPeter Brune + dm - the DM object
1814e30e807fSPeter Brune . n  - the number of subdomain scatters
1815e30e807fSPeter Brune - subdms - the local subdomains
1816e30e807fSPeter Brune 
1817e30e807fSPeter Brune   Output Parameters:
1818e30e807fSPeter Brune + n     - the number of scatters returned
1819e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1820e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1821e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1822e30e807fSPeter Brune 
182395452b02SPatrick Sanan   Notes:
182495452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1825e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1826e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1827e30e807fSPeter Brune   solution and residual data.
1828e30e807fSPeter Brune 
1829e30e807fSPeter Brune   Level: developer
1830e30e807fSPeter Brune 
1831e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1832e30e807fSPeter Brune @*/
1833e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1834e30e807fSPeter Brune {
1835e30e807fSPeter Brune   PetscErrorCode ierr;
1836e30e807fSPeter Brune 
1837e30e807fSPeter Brune   PetscFunctionBegin;
1838e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1839e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1840e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1841e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18426668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1843e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1844e7c4fc90SDmitry Karpeev }
1845e7c4fc90SDmitry Karpeev 
184647c6ae99SBarry Smith /*@
184747c6ae99SBarry Smith   DMRefine - Refines a DM object
184847c6ae99SBarry Smith 
184947c6ae99SBarry Smith   Collective on DM
185047c6ae99SBarry Smith 
185147c6ae99SBarry Smith   Input Parameter:
185247c6ae99SBarry Smith + dm   - the DM object
185391d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
185447c6ae99SBarry Smith 
185547c6ae99SBarry Smith   Output Parameter:
18560298fd71SBarry Smith . dmf - the refined DM, or NULL
1857ae0a1c52SMatthew G Knepley 
18580298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
185947c6ae99SBarry Smith 
186047c6ae99SBarry Smith   Level: developer
186147c6ae99SBarry Smith 
1862e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
186347c6ae99SBarry Smith @*/
18647087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
186547c6ae99SBarry Smith {
186647c6ae99SBarry Smith   PetscErrorCode   ierr;
1867c833c3b5SJed Brown   DMRefineHookLink link;
186847c6ae99SBarry Smith 
186947c6ae99SBarry Smith   PetscFunctionBegin;
1870732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18711ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1872fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
187347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18744057135bSMatthew G Knepley   if (*dmf) {
187543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18768865f1eaSKarl Rupp 
18778cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18788865f1eaSKarl Rupp 
1879644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18800598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1881656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18828865f1eaSKarl Rupp 
1883e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1884c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18858865f1eaSKarl Rupp       if (link->refinehook) {
18868865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18878865f1eaSKarl Rupp       }
1888c833c3b5SJed Brown     }
1889c833c3b5SJed Brown   }
18901ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1891c833c3b5SJed Brown   PetscFunctionReturn(0);
1892c833c3b5SJed Brown }
1893c833c3b5SJed Brown 
1894bb9467b5SJed Brown /*@C
1895c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1896c833c3b5SJed Brown 
1897c833c3b5SJed Brown    Logically Collective
1898c833c3b5SJed Brown 
1899c833c3b5SJed Brown    Input Arguments:
1900c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1901c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1902c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19030298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1904c833c3b5SJed Brown 
1905c833c3b5SJed Brown    Calling sequence of refinehook:
1906c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1907c833c3b5SJed Brown 
1908c833c3b5SJed Brown +  coarse - coarse level DM
1909c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1910c833c3b5SJed Brown -  ctx - optional user-defined function context
1911c833c3b5SJed Brown 
1912c833c3b5SJed Brown    Calling sequence for interphook:
1913c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1914c833c3b5SJed Brown 
1915c833c3b5SJed Brown +  coarse - coarse level DM
1916c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1917c833c3b5SJed Brown .  fine - fine level DM to update
1918c833c3b5SJed Brown -  ctx - optional user-defined function context
1919c833c3b5SJed Brown 
1920c833c3b5SJed Brown    Level: advanced
1921c833c3b5SJed Brown 
1922c833c3b5SJed Brown    Notes:
1923c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1924c833c3b5SJed Brown 
1925c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1926c833c3b5SJed Brown 
1927bb9467b5SJed Brown    This function is currently not available from Fortran.
1928bb9467b5SJed Brown 
1929c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1930c833c3b5SJed Brown @*/
1931c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1932c833c3b5SJed Brown {
1933c833c3b5SJed Brown   PetscErrorCode   ierr;
1934c833c3b5SJed Brown   DMRefineHookLink link,*p;
1935c833c3b5SJed Brown 
1936c833c3b5SJed Brown   PetscFunctionBegin;
1937c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19383d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19393d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19403d8e3701SJed Brown   }
194195dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1942c833c3b5SJed Brown   link->refinehook = refinehook;
1943c833c3b5SJed Brown   link->interphook = interphook;
1944c833c3b5SJed Brown   link->ctx        = ctx;
19450298fd71SBarry Smith   link->next       = NULL;
1946c833c3b5SJed Brown   *p               = link;
1947c833c3b5SJed Brown   PetscFunctionReturn(0);
1948c833c3b5SJed Brown }
1949c833c3b5SJed Brown 
19503d8e3701SJed Brown /*@C
19513d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19523d8e3701SJed Brown 
19533d8e3701SJed Brown    Logically Collective
19543d8e3701SJed Brown 
19553d8e3701SJed Brown    Input Arguments:
19563d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19573d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19583d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19593d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19603d8e3701SJed Brown 
19613d8e3701SJed Brown    Level: advanced
19623d8e3701SJed Brown 
19633d8e3701SJed Brown    Notes:
19643d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19653d8e3701SJed Brown 
19663d8e3701SJed Brown    This function is currently not available from Fortran.
19673d8e3701SJed Brown 
19683d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19693d8e3701SJed Brown @*/
19703d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19713d8e3701SJed Brown {
19723d8e3701SJed Brown   PetscErrorCode   ierr;
19733d8e3701SJed Brown   DMRefineHookLink link,*p;
19743d8e3701SJed Brown 
19753d8e3701SJed Brown   PetscFunctionBegin;
19763d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19773d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19783d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19793d8e3701SJed Brown       link = *p;
19803d8e3701SJed Brown       *p = link->next;
19813d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19823d8e3701SJed Brown       break;
19833d8e3701SJed Brown     }
19843d8e3701SJed Brown   }
19853d8e3701SJed Brown   PetscFunctionReturn(0);
19863d8e3701SJed Brown }
19873d8e3701SJed Brown 
1988c833c3b5SJed Brown /*@
1989c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1990c833c3b5SJed Brown 
1991c833c3b5SJed Brown    Collective if any hooks are
1992c833c3b5SJed Brown 
1993c833c3b5SJed Brown    Input Arguments:
1994c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1995e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1996c833c3b5SJed Brown -  fine - finer DM to update
1997c833c3b5SJed Brown 
1998c833c3b5SJed Brown    Level: developer
1999c833c3b5SJed Brown 
2000c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2001c833c3b5SJed Brown @*/
2002c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2003c833c3b5SJed Brown {
2004c833c3b5SJed Brown   PetscErrorCode   ierr;
2005c833c3b5SJed Brown   DMRefineHookLink link;
2006c833c3b5SJed Brown 
2007c833c3b5SJed Brown   PetscFunctionBegin;
2008c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20098865f1eaSKarl Rupp     if (link->interphook) {
20108865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20118865f1eaSKarl Rupp     }
20124057135bSMatthew G Knepley   }
201347c6ae99SBarry Smith   PetscFunctionReturn(0);
201447c6ae99SBarry Smith }
201547c6ae99SBarry Smith 
2016eb3f98d2SBarry Smith /*@
2017eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2018eb3f98d2SBarry Smith 
2019eb3f98d2SBarry Smith     Not Collective
2020eb3f98d2SBarry Smith 
2021eb3f98d2SBarry Smith     Input Parameter:
2022eb3f98d2SBarry Smith .   dm - the DM object
2023eb3f98d2SBarry Smith 
2024eb3f98d2SBarry Smith     Output Parameter:
2025eb3f98d2SBarry Smith .   level - number of refinements
2026eb3f98d2SBarry Smith 
2027eb3f98d2SBarry Smith     Level: developer
2028eb3f98d2SBarry Smith 
20296a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2030eb3f98d2SBarry Smith 
2031eb3f98d2SBarry Smith @*/
2032eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2033eb3f98d2SBarry Smith {
2034eb3f98d2SBarry Smith   PetscFunctionBegin;
2035eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2036eb3f98d2SBarry Smith   *level = dm->levelup;
2037eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2038eb3f98d2SBarry Smith }
2039eb3f98d2SBarry Smith 
2040fef3a512SBarry Smith /*@
2041fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2042fef3a512SBarry Smith 
2043fef3a512SBarry Smith     Not Collective
2044fef3a512SBarry Smith 
2045fef3a512SBarry Smith     Input Parameter:
2046fef3a512SBarry Smith +   dm - the DM object
2047fef3a512SBarry Smith -   level - number of refinements
2048fef3a512SBarry Smith 
2049fef3a512SBarry Smith     Level: advanced
2050fef3a512SBarry Smith 
205195452b02SPatrick Sanan     Notes:
205295452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2053fef3a512SBarry Smith 
2054fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2055fef3a512SBarry Smith 
2056fef3a512SBarry Smith @*/
2057fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2058fef3a512SBarry Smith {
2059fef3a512SBarry Smith   PetscFunctionBegin;
2060fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2061fef3a512SBarry Smith   dm->levelup = level;
2062fef3a512SBarry Smith   PetscFunctionReturn(0);
2063fef3a512SBarry Smith }
2064fef3a512SBarry Smith 
2065bb9467b5SJed Brown /*@C
2066baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2067baf369e7SPeter Brune 
2068baf369e7SPeter Brune    Logically Collective
2069baf369e7SPeter Brune 
2070baf369e7SPeter Brune    Input Arguments:
2071baf369e7SPeter Brune +  dm - the DM
2072baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2073baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20740298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2075baf369e7SPeter Brune 
2076baf369e7SPeter Brune    Calling sequence for beginhook:
2077baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2078baf369e7SPeter Brune 
2079baf369e7SPeter Brune +  dm - global DM
2080baf369e7SPeter Brune .  g - global vector
2081baf369e7SPeter Brune .  mode - mode
2082baf369e7SPeter Brune .  l - local vector
2083baf369e7SPeter Brune -  ctx - optional user-defined function context
2084baf369e7SPeter Brune 
2085baf369e7SPeter Brune 
2086baf369e7SPeter Brune    Calling sequence for endhook:
2087ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2088baf369e7SPeter Brune 
2089baf369e7SPeter Brune +  global - global DM
2090baf369e7SPeter Brune -  ctx - optional user-defined function context
2091baf369e7SPeter Brune 
2092baf369e7SPeter Brune    Level: advanced
2093baf369e7SPeter Brune 
2094baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2095baf369e7SPeter Brune @*/
2096baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2097baf369e7SPeter Brune {
2098baf369e7SPeter Brune   PetscErrorCode          ierr;
2099baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2100baf369e7SPeter Brune 
2101baf369e7SPeter Brune   PetscFunctionBegin;
2102baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2103baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
210495dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2105baf369e7SPeter Brune   link->beginhook = beginhook;
2106baf369e7SPeter Brune   link->endhook   = endhook;
2107baf369e7SPeter Brune   link->ctx       = ctx;
21080298fd71SBarry Smith   link->next      = NULL;
2109baf369e7SPeter Brune   *p              = link;
2110baf369e7SPeter Brune   PetscFunctionReturn(0);
2111baf369e7SPeter Brune }
2112baf369e7SPeter Brune 
21134c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
21144c274da1SToby Isaac {
21154c274da1SToby Isaac   Mat cMat;
21164c274da1SToby Isaac   Vec cVec;
21174c274da1SToby Isaac   PetscSection section, cSec;
21184c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21194c274da1SToby Isaac   PetscErrorCode ierr;
21204c274da1SToby Isaac 
21214c274da1SToby Isaac   PetscFunctionBegin;
21224c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21234c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21244c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
21255db9a05bSToby Isaac     PetscInt nRows;
21265db9a05bSToby Isaac 
21275db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21285db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2129e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
21307711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21314c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
21324c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21334c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21344c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21354c274da1SToby Isaac       if (dof) {
21364c274da1SToby Isaac         PetscScalar *vals;
21374c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
21384c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
21394c274da1SToby Isaac       }
21404c274da1SToby Isaac     }
21414c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21424c274da1SToby Isaac   }
21434c274da1SToby Isaac   PetscFunctionReturn(0);
21444c274da1SToby Isaac }
21454c274da1SToby Isaac 
214647c6ae99SBarry Smith /*@
214701729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
214801729b5cSPatrick Sanan 
214901729b5cSPatrick Sanan     Neighbor-wise Collective on DM
215001729b5cSPatrick Sanan 
215101729b5cSPatrick Sanan     Input Parameters:
215201729b5cSPatrick Sanan +   dm - the DM object
215301729b5cSPatrick Sanan .   g - the global vector
215401729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
215501729b5cSPatrick Sanan -   l - the local vector
215601729b5cSPatrick Sanan 
215701729b5cSPatrick Sanan     Notes:
215801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
215901729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
216001729b5cSPatrick Sanan 
216101729b5cSPatrick Sanan     Level: beginner
216201729b5cSPatrick Sanan 
216301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
216401729b5cSPatrick Sanan 
216501729b5cSPatrick Sanan @*/
216601729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
216701729b5cSPatrick Sanan {
216801729b5cSPatrick Sanan   PetscErrorCode ierr;
216901729b5cSPatrick Sanan 
217001729b5cSPatrick Sanan   PetscFunctionBegin;
217101729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
217201729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
217301729b5cSPatrick Sanan   PetscFunctionReturn(0);
217401729b5cSPatrick Sanan }
217501729b5cSPatrick Sanan 
217601729b5cSPatrick Sanan /*@
217747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
217847c6ae99SBarry Smith 
217947c6ae99SBarry Smith     Neighbor-wise Collective on DM
218047c6ae99SBarry Smith 
218147c6ae99SBarry Smith     Input Parameters:
218247c6ae99SBarry Smith +   dm - the DM object
218347c6ae99SBarry Smith .   g - the global vector
218447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
218547c6ae99SBarry Smith -   l - the local vector
218647c6ae99SBarry Smith 
218701729b5cSPatrick Sanan     Level: intermediate
218847c6ae99SBarry Smith 
218901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
219047c6ae99SBarry Smith 
219147c6ae99SBarry Smith @*/
21927087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
219347c6ae99SBarry Smith {
21947128ae9fSMatthew G Knepley   PetscSF                 sf;
219547c6ae99SBarry Smith   PetscErrorCode          ierr;
2196baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
219747c6ae99SBarry Smith 
219847c6ae99SBarry Smith   PetscFunctionBegin;
2199171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2200baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
22018865f1eaSKarl Rupp     if (link->beginhook) {
22028865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
22038865f1eaSKarl Rupp     }
2204baf369e7SPeter Brune   }
22057128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22067128ae9fSMatthew G Knepley   if (sf) {
2207ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2208ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
22097128ae9fSMatthew G Knepley 
221082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22117128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2212ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22137128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22147128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2215ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22167128ae9fSMatthew G Knepley   } else {
221733907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2218843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22197128ae9fSMatthew G Knepley   }
222047c6ae99SBarry Smith   PetscFunctionReturn(0);
222147c6ae99SBarry Smith }
222247c6ae99SBarry Smith 
222347c6ae99SBarry Smith /*@
222447c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
222547c6ae99SBarry Smith 
222647c6ae99SBarry Smith     Neighbor-wise Collective on DM
222747c6ae99SBarry Smith 
222847c6ae99SBarry Smith     Input Parameters:
222947c6ae99SBarry Smith +   dm - the DM object
223047c6ae99SBarry Smith .   g - the global vector
223147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
223247c6ae99SBarry Smith -   l - the local vector
223347c6ae99SBarry Smith 
223401729b5cSPatrick Sanan     Level: intermediate
223547c6ae99SBarry Smith 
223601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
223747c6ae99SBarry Smith 
223847c6ae99SBarry Smith @*/
22397087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
224047c6ae99SBarry Smith {
22417128ae9fSMatthew G Knepley   PetscSF                 sf;
224247c6ae99SBarry Smith   PetscErrorCode          ierr;
2243ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2244ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2245baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
224647c6ae99SBarry Smith 
224747c6ae99SBarry Smith   PetscFunctionBegin;
2248171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22497128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22507128ae9fSMatthew G Knepley   if (sf) {
225182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22527128ae9fSMatthew G Knepley 
22537128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2254ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22557128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22567128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2257ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22587128ae9fSMatthew G Knepley   } else {
225933907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2260843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22617128ae9fSMatthew G Knepley   }
22624c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2263baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2264baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2265baf369e7SPeter Brune   }
226647c6ae99SBarry Smith   PetscFunctionReturn(0);
226747c6ae99SBarry Smith }
226847c6ae99SBarry Smith 
2269d4d07f1eSToby Isaac /*@C
2270d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2271d4d07f1eSToby Isaac 
2272d4d07f1eSToby Isaac    Logically Collective
2273d4d07f1eSToby Isaac 
2274d4d07f1eSToby Isaac    Input Arguments:
2275d4d07f1eSToby Isaac +  dm - the DM
2276d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2277d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2278d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2279d4d07f1eSToby Isaac 
2280d4d07f1eSToby Isaac    Calling sequence for beginhook:
2281d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2282d4d07f1eSToby Isaac 
2283d4d07f1eSToby Isaac +  dm - global DM
2284d4d07f1eSToby Isaac .  l - local vector
2285d4d07f1eSToby Isaac .  mode - mode
2286d4d07f1eSToby Isaac .  g - global vector
2287d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2288d4d07f1eSToby Isaac 
2289d4d07f1eSToby Isaac 
2290d4d07f1eSToby Isaac    Calling sequence for endhook:
2291d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2292d4d07f1eSToby Isaac 
2293d4d07f1eSToby Isaac +  global - global DM
2294d4d07f1eSToby Isaac .  l - local vector
2295d4d07f1eSToby Isaac .  mode - mode
2296d4d07f1eSToby Isaac .  g - global vector
2297d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2298d4d07f1eSToby Isaac 
2299d4d07f1eSToby Isaac    Level: advanced
2300d4d07f1eSToby Isaac 
2301d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2302d4d07f1eSToby Isaac @*/
2303d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2304d4d07f1eSToby Isaac {
2305d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2306d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2307d4d07f1eSToby Isaac 
2308d4d07f1eSToby Isaac   PetscFunctionBegin;
2309d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2310d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
231195dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2312d4d07f1eSToby Isaac   link->beginhook = beginhook;
2313d4d07f1eSToby Isaac   link->endhook   = endhook;
2314d4d07f1eSToby Isaac   link->ctx       = ctx;
2315d4d07f1eSToby Isaac   link->next      = NULL;
2316d4d07f1eSToby Isaac   *p              = link;
2317d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2318d4d07f1eSToby Isaac }
2319d4d07f1eSToby Isaac 
23204c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
23214c274da1SToby Isaac {
23224c274da1SToby Isaac   Mat cMat;
23234c274da1SToby Isaac   Vec cVec;
23244c274da1SToby Isaac   PetscSection section, cSec;
23254c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23264c274da1SToby Isaac   PetscErrorCode ierr;
23274c274da1SToby Isaac 
23284c274da1SToby Isaac   PetscFunctionBegin;
23294c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23304c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23314c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
23325db9a05bSToby Isaac     PetscInt nRows;
23335db9a05bSToby Isaac 
23345db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23355db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2336e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
23377711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23384c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23394c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23404c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23414c274da1SToby Isaac       if (dof) {
23424c274da1SToby Isaac         PetscInt d;
23434c274da1SToby Isaac         PetscScalar *vals;
23444c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
23454c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
23464c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
23474c274da1SToby Isaac          * we just extracted */
23484c274da1SToby Isaac         for (d = 0; d < dof; d++) {
23494c274da1SToby Isaac           vals[d] = 0.;
23504c274da1SToby Isaac         }
23514c274da1SToby Isaac       }
23524c274da1SToby Isaac     }
23534c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
23544c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23554c274da1SToby Isaac   }
23564c274da1SToby Isaac   PetscFunctionReturn(0);
23574c274da1SToby Isaac }
235801729b5cSPatrick Sanan /*@
235901729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
236001729b5cSPatrick Sanan 
236101729b5cSPatrick Sanan     Neighbor-wise Collective on DM
236201729b5cSPatrick Sanan 
236301729b5cSPatrick Sanan     Input Parameters:
236401729b5cSPatrick Sanan +   dm - the DM object
236501729b5cSPatrick Sanan .   l - the local vector
236601729b5cSPatrick Sanan .   mode - if INSERT_VALUES then no parallel communication is used, if ADD_VALUES then all ghost points from the same base point accumulate into that base point.
236701729b5cSPatrick Sanan -   g - the global vector
236801729b5cSPatrick Sanan 
236901729b5cSPatrick Sanan     Notes:
237001729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
237101729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
237201729b5cSPatrick Sanan 
237301729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
237401729b5cSPatrick Sanan            INSERT_VALUES is not supported for DMDA; in that case simply compute the values directly into a global vector instead of a local one.
237501729b5cSPatrick Sanan 
237601729b5cSPatrick Sanan     Level: beginner
237701729b5cSPatrick Sanan 
237801729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
237901729b5cSPatrick Sanan 
238001729b5cSPatrick Sanan @*/
238101729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
238201729b5cSPatrick Sanan {
238301729b5cSPatrick Sanan   PetscErrorCode ierr;
238401729b5cSPatrick Sanan 
238501729b5cSPatrick Sanan   PetscFunctionBegin;
238601729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
238701729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
238801729b5cSPatrick Sanan   PetscFunctionReturn(0);
238901729b5cSPatrick Sanan }
23904c274da1SToby Isaac 
239147c6ae99SBarry Smith /*@
239201729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
23939a42bb27SBarry Smith 
23949a42bb27SBarry Smith     Neighbor-wise Collective on DM
23959a42bb27SBarry Smith 
23969a42bb27SBarry Smith     Input Parameters:
23979a42bb27SBarry Smith +   dm - the DM object
2398f6813fd5SJed Brown .   l - the local vector
23991eb28f2eSBarry 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.
24001eb28f2eSBarry Smith -   g - the global vector
24019a42bb27SBarry Smith 
240295452b02SPatrick Sanan     Notes:
240395452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
240484330215SMatthew 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.
24059a42bb27SBarry Smith 
240601729b5cSPatrick Sanan     Level: intermediate
24079a42bb27SBarry Smith 
240801729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
24099a42bb27SBarry Smith 
24109a42bb27SBarry Smith @*/
24117087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
24129a42bb27SBarry Smith {
24137128ae9fSMatthew G Knepley   PetscSF                 sf;
241484330215SMatthew G. Knepley   PetscSection            s, gs;
2415d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2416ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2417ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
241884330215SMatthew G. Knepley   PetscBool               isInsert;
241984330215SMatthew G. Knepley   PetscErrorCode          ierr;
24209a42bb27SBarry Smith 
24219a42bb27SBarry Smith   PetscFunctionBegin;
2422171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2423d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2424d4d07f1eSToby Isaac     if (link->beginhook) {
2425d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2426d4d07f1eSToby Isaac     }
2427d4d07f1eSToby Isaac   }
24284c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
24297128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2430e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24317128ae9fSMatthew G Knepley   switch (mode) {
24327128ae9fSMatthew G Knepley   case INSERT_VALUES:
24337128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2434304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
243584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24367128ae9fSMatthew G Knepley   case ADD_VALUES:
24377128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2438304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
243984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
24407128ae9fSMatthew G Knepley   default:
244182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24427128ae9fSMatthew G Knepley   }
244384330215SMatthew G. Knepley   if (sf && !isInsert) {
2444ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24457128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2446a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2447ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
244884330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
244984330215SMatthew G. Knepley   } else if (s && isInsert) {
245084330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
245184330215SMatthew G. Knepley 
2452e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
245384330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
245484330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2455ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
245684330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
245784330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2458b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
245984330215SMatthew G. Knepley 
246084330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
246103442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
246284330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2463b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
246484330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
246584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2466b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
246703442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2468b3b16f48SMatthew 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);
2469b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2470b3b16f48SMatthew G. Knepley       if (!gcdof) {
247184330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2472b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2473b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
247484330215SMatthew G. Knepley         const PetscInt *cdofs;
247584330215SMatthew G. Knepley         PetscInt        cind = 0;
247684330215SMatthew G. Knepley 
247784330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2478b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
247984330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2480b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
248184330215SMatthew G. Knepley         }
2482b3b16f48SMatthew 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);
248384330215SMatthew G. Knepley     }
2484ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24857128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
24867128ae9fSMatthew G Knepley   } else {
2487843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24887128ae9fSMatthew G Knepley   }
24899a42bb27SBarry Smith   PetscFunctionReturn(0);
24909a42bb27SBarry Smith }
24919a42bb27SBarry Smith 
24929a42bb27SBarry Smith /*@
24939a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
249447c6ae99SBarry Smith 
249547c6ae99SBarry Smith     Neighbor-wise Collective on DM
249647c6ae99SBarry Smith 
249747c6ae99SBarry Smith     Input Parameters:
249847c6ae99SBarry Smith +   dm - the DM object
2499f6813fd5SJed Brown .   l - the local vector
250047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2501f6813fd5SJed Brown -   g - the global vector
250247c6ae99SBarry Smith 
250301729b5cSPatrick Sanan     Level: intermediate
250447c6ae99SBarry Smith 
2505e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
250647c6ae99SBarry Smith 
250747c6ae99SBarry Smith @*/
25087087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
250947c6ae99SBarry Smith {
25107128ae9fSMatthew G Knepley   PetscSF                 sf;
251184330215SMatthew G. Knepley   PetscSection            s;
2512d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
251384330215SMatthew G. Knepley   PetscBool               isInsert;
251484330215SMatthew G. Knepley   PetscErrorCode          ierr;
251547c6ae99SBarry Smith 
251647c6ae99SBarry Smith   PetscFunctionBegin;
2517171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25187128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2519e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
25207128ae9fSMatthew G Knepley   switch (mode) {
25217128ae9fSMatthew G Knepley   case INSERT_VALUES:
25227128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
252384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25247128ae9fSMatthew G Knepley   case ADD_VALUES:
25257128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
252684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25277128ae9fSMatthew G Knepley   default:
252882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25297128ae9fSMatthew G Knepley   }
253084330215SMatthew G. Knepley   if (sf && !isInsert) {
2531ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2532ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
253384330215SMatthew G. Knepley 
2534ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
25357128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2536a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2537ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
25387128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
253984330215SMatthew G. Knepley   } else if (s && isInsert) {
25407128ae9fSMatthew G Knepley   } else {
2541843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
25427128ae9fSMatthew G Knepley   }
2543d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2544d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2545d4d07f1eSToby Isaac   }
254647c6ae99SBarry Smith   PetscFunctionReturn(0);
254747c6ae99SBarry Smith }
254847c6ae99SBarry Smith 
2549f089877aSRichard Tran Mills /*@
2550bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2551bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2552d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2553f089877aSRichard Tran Mills 
2554bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2555f089877aSRichard Tran Mills 
2556f089877aSRichard Tran Mills    Input Parameters:
2557f089877aSRichard Tran Mills +  dm - the DM object
2558bc0a1609SRichard Tran Mills .  g - the original local vector
2559bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2560f089877aSRichard Tran Mills 
2561bc0a1609SRichard Tran Mills    Output Parameter:
2562bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2563f089877aSRichard Tran Mills 
2564f089877aSRichard Tran Mills    Level: intermediate
2565f089877aSRichard Tran Mills 
2566bc0a1609SRichard Tran Mills    Notes:
2567bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2568bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2569bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2570bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2571bc0a1609SRichard Tran Mills 
2572bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2573bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2574f089877aSRichard Tran Mills 
2575f089877aSRichard Tran Mills @*/
2576f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2577f089877aSRichard Tran Mills {
2578f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2579f089877aSRichard Tran Mills 
2580f089877aSRichard Tran Mills   PetscFunctionBegin;
2581f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2582bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2583f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2584f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2585f089877aSRichard Tran Mills }
2586f089877aSRichard Tran Mills 
2587f089877aSRichard Tran Mills /*@
2588bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2589bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2590d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2591f089877aSRichard Tran Mills 
2592bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2593f089877aSRichard Tran Mills 
2594f089877aSRichard Tran Mills    Input Parameters:
2595bc0a1609SRichard Tran Mills +  da - the DM object
2596bc0a1609SRichard Tran Mills .  g - the original local vector
2597bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2598f089877aSRichard Tran Mills 
2599bc0a1609SRichard Tran Mills    Output Parameter:
2600bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2601f089877aSRichard Tran Mills 
2602f089877aSRichard Tran Mills    Level: intermediate
2603f089877aSRichard Tran Mills 
2604bc0a1609SRichard Tran Mills    Notes:
2605bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2606bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2607bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2608bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2609bc0a1609SRichard Tran Mills 
2610bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2611bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2612f089877aSRichard Tran Mills 
2613f089877aSRichard Tran Mills @*/
2614f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2615f089877aSRichard Tran Mills {
2616f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2617f089877aSRichard Tran Mills 
2618f089877aSRichard Tran Mills   PetscFunctionBegin;
2619f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2620bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2621f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2622f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2623f089877aSRichard Tran Mills }
2624f089877aSRichard Tran Mills 
2625f089877aSRichard Tran Mills 
262647c6ae99SBarry Smith /*@
262747c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
262847c6ae99SBarry Smith 
262947c6ae99SBarry Smith     Collective on DM
263047c6ae99SBarry Smith 
263147c6ae99SBarry Smith     Input Parameter:
263247c6ae99SBarry Smith +   dm - the DM object
263391d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
263447c6ae99SBarry Smith 
263547c6ae99SBarry Smith     Output Parameter:
263647c6ae99SBarry Smith .   dmc - the coarsened DM
263747c6ae99SBarry Smith 
263847c6ae99SBarry Smith     Level: developer
263947c6ae99SBarry Smith 
2640e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
264147c6ae99SBarry Smith 
264247c6ae99SBarry Smith @*/
26437087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
264447c6ae99SBarry Smith {
264547c6ae99SBarry Smith   PetscErrorCode    ierr;
2646b17ce1afSJed Brown   DMCoarsenHookLink link;
264747c6ae99SBarry Smith 
264847c6ae99SBarry Smith   PetscFunctionBegin;
2649171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2650fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
265147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
265247c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
26538892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2654a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
265543842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
26568cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2657644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
26580598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2659656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2660e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2661b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2662b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2663b17ce1afSJed Brown   }
266447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2665b17ce1afSJed Brown   PetscFunctionReturn(0);
2666b17ce1afSJed Brown }
2667b17ce1afSJed Brown 
2668bb9467b5SJed Brown /*@C
2669b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2670b17ce1afSJed Brown 
2671b17ce1afSJed Brown    Logically Collective
2672b17ce1afSJed Brown 
2673b17ce1afSJed Brown    Input Arguments:
2674b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2675b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2676b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
26770298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2678b17ce1afSJed Brown 
2679b17ce1afSJed Brown    Calling sequence of coarsenhook:
2680b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2681b17ce1afSJed Brown 
2682b17ce1afSJed Brown +  fine - fine level DM
2683b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2684b17ce1afSJed Brown -  ctx - optional user-defined function context
2685b17ce1afSJed Brown 
2686b17ce1afSJed Brown    Calling sequence for restricthook:
2687c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2688b17ce1afSJed Brown 
2689b17ce1afSJed Brown +  fine - fine level DM
2690b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2691c833c3b5SJed Brown .  rscale - scaling vector for restriction
2692c833c3b5SJed Brown .  inject - matrix restricting by injection
2693b17ce1afSJed Brown .  coarse - coarse level DM to update
2694b17ce1afSJed Brown -  ctx - optional user-defined function context
2695b17ce1afSJed Brown 
2696b17ce1afSJed Brown    Level: advanced
2697b17ce1afSJed Brown 
2698b17ce1afSJed Brown    Notes:
2699b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2700b17ce1afSJed Brown 
2701b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2702b17ce1afSJed Brown 
2703b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2704b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2705b17ce1afSJed Brown 
2706bb9467b5SJed Brown    This function is currently not available from Fortran.
2707bb9467b5SJed Brown 
2708dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2709b17ce1afSJed Brown @*/
2710b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2711b17ce1afSJed Brown {
2712b17ce1afSJed Brown   PetscErrorCode    ierr;
2713b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2714b17ce1afSJed Brown 
2715b17ce1afSJed Brown   PetscFunctionBegin;
2716b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
27171e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
27181e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
27191e3d8eccSJed Brown   }
272095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2721b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2722b17ce1afSJed Brown   link->restricthook = restricthook;
2723b17ce1afSJed Brown   link->ctx          = ctx;
27240298fd71SBarry Smith   link->next         = NULL;
2725b17ce1afSJed Brown   *p                 = link;
2726b17ce1afSJed Brown   PetscFunctionReturn(0);
2727b17ce1afSJed Brown }
2728b17ce1afSJed Brown 
2729dc822a44SJed Brown /*@C
2730dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2731dc822a44SJed Brown 
2732dc822a44SJed Brown    Logically Collective
2733dc822a44SJed Brown 
2734dc822a44SJed Brown    Input Arguments:
2735dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2736dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2737dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2738dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2739dc822a44SJed Brown 
2740dc822a44SJed Brown    Level: advanced
2741dc822a44SJed Brown 
2742dc822a44SJed Brown    Notes:
2743dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2744dc822a44SJed Brown 
2745dc822a44SJed Brown    This function is currently not available from Fortran.
2746dc822a44SJed Brown 
2747dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2748dc822a44SJed Brown @*/
2749dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2750dc822a44SJed Brown {
2751dc822a44SJed Brown   PetscErrorCode    ierr;
2752dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2753dc822a44SJed Brown 
2754dc822a44SJed Brown   PetscFunctionBegin;
2755dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2756dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2757dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2758dc822a44SJed Brown       link = *p;
2759dc822a44SJed Brown       *p = link->next;
2760dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2761dc822a44SJed Brown       break;
2762dc822a44SJed Brown     }
2763dc822a44SJed Brown   }
2764dc822a44SJed Brown   PetscFunctionReturn(0);
2765dc822a44SJed Brown }
2766dc822a44SJed Brown 
2767dc822a44SJed Brown 
2768b17ce1afSJed Brown /*@
2769b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2770b17ce1afSJed Brown 
2771b17ce1afSJed Brown    Collective if any hooks are
2772b17ce1afSJed Brown 
2773b17ce1afSJed Brown    Input Arguments:
2774b17ce1afSJed Brown +  fine - finer DM to use as a base
2775b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2776e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2777b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2778e91eccc2SStefano Zampini -  coarse - coarser DM to update
2779b17ce1afSJed Brown 
2780b17ce1afSJed Brown    Level: developer
2781b17ce1afSJed Brown 
2782b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2783b17ce1afSJed Brown @*/
2784b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2785b17ce1afSJed Brown {
2786b17ce1afSJed Brown   PetscErrorCode    ierr;
2787b17ce1afSJed Brown   DMCoarsenHookLink link;
2788b17ce1afSJed Brown 
2789b17ce1afSJed Brown   PetscFunctionBegin;
2790b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
27918865f1eaSKarl Rupp     if (link->restricthook) {
27928865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
27938865f1eaSKarl Rupp     }
2794b17ce1afSJed Brown   }
279547c6ae99SBarry Smith   PetscFunctionReturn(0);
279647c6ae99SBarry Smith }
279747c6ae99SBarry Smith 
2798bb9467b5SJed Brown /*@C
2799be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
28005dbd56e3SPeter Brune 
28015dbd56e3SPeter Brune    Logically Collective
28025dbd56e3SPeter Brune 
28035dbd56e3SPeter Brune    Input Arguments:
28045dbd56e3SPeter Brune +  global - global DM
2805ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
28065dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
28070298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
28085dbd56e3SPeter Brune 
2809ec4806b8SPeter Brune 
2810ec4806b8SPeter Brune    Calling sequence for ddhook:
2811ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2812ec4806b8SPeter Brune 
2813ec4806b8SPeter Brune +  global - global DM
2814ec4806b8SPeter Brune .  block  - block DM
2815ec4806b8SPeter Brune -  ctx - optional user-defined function context
2816ec4806b8SPeter Brune 
28175dbd56e3SPeter Brune    Calling sequence for restricthook:
2818ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
28195dbd56e3SPeter Brune 
28205dbd56e3SPeter Brune +  global - global DM
28215dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
28225dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2823ec4806b8SPeter Brune .  block  - block DM
28245dbd56e3SPeter Brune -  ctx - optional user-defined function context
28255dbd56e3SPeter Brune 
28265dbd56e3SPeter Brune    Level: advanced
28275dbd56e3SPeter Brune 
28285dbd56e3SPeter Brune    Notes:
2829ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
28305dbd56e3SPeter Brune 
28315dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
28325dbd56e3SPeter Brune 
28335dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2834ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
28355dbd56e3SPeter Brune 
2836bb9467b5SJed Brown    This function is currently not available from Fortran.
2837bb9467b5SJed Brown 
28385dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
28395dbd56e3SPeter Brune @*/
2840be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
28415dbd56e3SPeter Brune {
28425dbd56e3SPeter Brune   PetscErrorCode      ierr;
2843be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
28445dbd56e3SPeter Brune 
28455dbd56e3SPeter Brune   PetscFunctionBegin;
28465dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2847b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2848b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2849b3a6b972SJed Brown   }
285095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
28515dbd56e3SPeter Brune   link->restricthook = restricthook;
2852be081cd6SPeter Brune   link->ddhook       = ddhook;
28535dbd56e3SPeter Brune   link->ctx          = ctx;
28540298fd71SBarry Smith   link->next         = NULL;
28555dbd56e3SPeter Brune   *p                 = link;
28565dbd56e3SPeter Brune   PetscFunctionReturn(0);
28575dbd56e3SPeter Brune }
28585dbd56e3SPeter Brune 
2859b3a6b972SJed Brown /*@C
2860b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2861b3a6b972SJed Brown 
2862b3a6b972SJed Brown    Logically Collective
2863b3a6b972SJed Brown 
2864b3a6b972SJed Brown    Input Arguments:
2865b3a6b972SJed Brown +  global - global DM
2866b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2867b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2868b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2869b3a6b972SJed Brown 
2870b3a6b972SJed Brown    Level: advanced
2871b3a6b972SJed Brown 
2872b3a6b972SJed Brown    Notes:
2873b3a6b972SJed Brown 
2874b3a6b972SJed Brown    This function is currently not available from Fortran.
2875b3a6b972SJed Brown 
2876b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2877b3a6b972SJed Brown @*/
2878b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2879b3a6b972SJed Brown {
2880b3a6b972SJed Brown   PetscErrorCode      ierr;
2881b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2882b3a6b972SJed Brown 
2883b3a6b972SJed Brown   PetscFunctionBegin;
2884b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2885b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2886b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2887b3a6b972SJed Brown       link = *p;
2888b3a6b972SJed Brown       *p = link->next;
2889b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2890b3a6b972SJed Brown       break;
2891b3a6b972SJed Brown     }
2892b3a6b972SJed Brown   }
2893b3a6b972SJed Brown   PetscFunctionReturn(0);
2894b3a6b972SJed Brown }
2895b3a6b972SJed Brown 
28965dbd56e3SPeter Brune /*@
2897be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
28985dbd56e3SPeter Brune 
28995dbd56e3SPeter Brune    Collective if any hooks are
29005dbd56e3SPeter Brune 
29015dbd56e3SPeter Brune    Input Arguments:
29025dbd56e3SPeter Brune +  fine - finer DM to use as a base
2903be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2904be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
29055dbd56e3SPeter Brune -  coarse - coarer DM to update
29065dbd56e3SPeter Brune 
29075dbd56e3SPeter Brune    Level: developer
29085dbd56e3SPeter Brune 
29095dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
29105dbd56e3SPeter Brune @*/
2911be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
29125dbd56e3SPeter Brune {
29135dbd56e3SPeter Brune   PetscErrorCode      ierr;
2914be081cd6SPeter Brune   DMSubDomainHookLink link;
29155dbd56e3SPeter Brune 
29165dbd56e3SPeter Brune   PetscFunctionBegin;
2917be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
29188865f1eaSKarl Rupp     if (link->restricthook) {
29198865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
29208865f1eaSKarl Rupp     }
29215dbd56e3SPeter Brune   }
29225dbd56e3SPeter Brune   PetscFunctionReturn(0);
29235dbd56e3SPeter Brune }
29245dbd56e3SPeter Brune 
29255fe1f584SPeter Brune /*@
29266a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
29275fe1f584SPeter Brune 
29285fe1f584SPeter Brune     Not Collective
29295fe1f584SPeter Brune 
29305fe1f584SPeter Brune     Input Parameter:
29315fe1f584SPeter Brune .   dm - the DM object
29325fe1f584SPeter Brune 
29335fe1f584SPeter Brune     Output Parameter:
29346a7d9d85SPeter Brune .   level - number of coarsenings
29355fe1f584SPeter Brune 
29365fe1f584SPeter Brune     Level: developer
29375fe1f584SPeter Brune 
29386a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
29395fe1f584SPeter Brune 
29405fe1f584SPeter Brune @*/
29415fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
29425fe1f584SPeter Brune {
29435fe1f584SPeter Brune   PetscFunctionBegin;
29445fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29455fe1f584SPeter Brune   *level = dm->leveldown;
29465fe1f584SPeter Brune   PetscFunctionReturn(0);
29475fe1f584SPeter Brune }
29485fe1f584SPeter Brune 
29499a64c4a8SMatthew G. Knepley /*@
29509a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
29519a64c4a8SMatthew G. Knepley 
29529a64c4a8SMatthew G. Knepley     Not Collective
29539a64c4a8SMatthew G. Knepley 
29549a64c4a8SMatthew G. Knepley     Input Parameters:
29559a64c4a8SMatthew G. Knepley +   dm - the DM object
29569a64c4a8SMatthew G. Knepley -   level - number of coarsenings
29579a64c4a8SMatthew G. Knepley 
29589a64c4a8SMatthew G. Knepley     Level: developer
29599a64c4a8SMatthew G. Knepley 
29609a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
29619a64c4a8SMatthew G. Knepley @*/
29629a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
29639a64c4a8SMatthew G. Knepley {
29649a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
29659a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29669a64c4a8SMatthew G. Knepley   dm->leveldown = level;
29679a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
29689a64c4a8SMatthew G. Knepley }
29699a64c4a8SMatthew G. Knepley 
29705fe1f584SPeter Brune 
29715fe1f584SPeter Brune 
297247c6ae99SBarry Smith /*@C
297347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
297447c6ae99SBarry Smith 
297547c6ae99SBarry Smith     Collective on DM
297647c6ae99SBarry Smith 
297747c6ae99SBarry Smith     Input Parameter:
297847c6ae99SBarry Smith +   dm - the DM object
297947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
298047c6ae99SBarry Smith 
298147c6ae99SBarry Smith     Output Parameter:
298247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
298347c6ae99SBarry Smith 
298447c6ae99SBarry Smith     Level: developer
298547c6ae99SBarry Smith 
2986e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
298747c6ae99SBarry Smith 
298847c6ae99SBarry Smith @*/
29897087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
299047c6ae99SBarry Smith {
299147c6ae99SBarry Smith   PetscErrorCode ierr;
299247c6ae99SBarry Smith 
299347c6ae99SBarry Smith   PetscFunctionBegin;
2994171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2995ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
299647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
299747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
299847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
299947c6ae99SBarry Smith   } else if (dm->ops->refine) {
300047c6ae99SBarry Smith     PetscInt i;
300147c6ae99SBarry Smith 
3002ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
300347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3004ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
300547c6ae99SBarry Smith     }
3006ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
300747c6ae99SBarry Smith   PetscFunctionReturn(0);
300847c6ae99SBarry Smith }
300947c6ae99SBarry Smith 
301047c6ae99SBarry Smith /*@C
301147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
301247c6ae99SBarry Smith 
301347c6ae99SBarry Smith     Collective on DM
301447c6ae99SBarry Smith 
301547c6ae99SBarry Smith     Input Parameter:
301647c6ae99SBarry Smith +   dm - the DM object
301747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
301847c6ae99SBarry Smith 
301947c6ae99SBarry Smith     Output Parameter:
302047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
302147c6ae99SBarry Smith 
302247c6ae99SBarry Smith     Level: developer
302347c6ae99SBarry Smith 
3024e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
302547c6ae99SBarry Smith 
302647c6ae99SBarry Smith @*/
30277087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
302847c6ae99SBarry Smith {
302947c6ae99SBarry Smith   PetscErrorCode ierr;
303047c6ae99SBarry Smith 
303147c6ae99SBarry Smith   PetscFunctionBegin;
3032171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3033ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
303447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
303547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
303647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
303747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
303847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
303947c6ae99SBarry Smith     PetscInt i;
304047c6ae99SBarry Smith 
3041ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
304247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3043ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
304447c6ae99SBarry Smith     }
3045ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
304647c6ae99SBarry Smith   PetscFunctionReturn(0);
304747c6ae99SBarry Smith }
304847c6ae99SBarry Smith 
304947c6ae99SBarry Smith /*@
3050e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
305147c6ae99SBarry Smith    grids associated with two DMs.
305247c6ae99SBarry Smith 
305347c6ae99SBarry Smith    Collective on DM
305447c6ae99SBarry Smith 
305547c6ae99SBarry Smith    Input Parameters:
305647c6ae99SBarry Smith +  dmc - the coarse grid DM
305747c6ae99SBarry Smith -  dmf - the fine grid DM
305847c6ae99SBarry Smith 
305947c6ae99SBarry Smith    Output Parameters:
306047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
306147c6ae99SBarry Smith 
306247c6ae99SBarry Smith    Level: intermediate
306347c6ae99SBarry Smith 
306447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
306547c6ae99SBarry Smith 
3066e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
306747c6ae99SBarry Smith @*/
3068e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
306947c6ae99SBarry Smith {
307047c6ae99SBarry Smith   PetscErrorCode ierr;
307147c6ae99SBarry Smith 
307247c6ae99SBarry Smith   PetscFunctionBegin;
3073171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3074171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
307547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
307647c6ae99SBarry Smith   PetscFunctionReturn(0);
307747c6ae99SBarry Smith }
307847c6ae99SBarry Smith 
30791a266240SBarry Smith /*@C
30801a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
30811a266240SBarry Smith 
30821a266240SBarry Smith     Not Collective
30831a266240SBarry Smith 
30841a266240SBarry Smith     Input Parameters:
30851a266240SBarry Smith +   dm - the DM object
30861a266240SBarry Smith -   destroy - the destroy function
30871a266240SBarry Smith 
30881a266240SBarry Smith     Level: intermediate
30891a266240SBarry Smith 
3090e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
30911a266240SBarry Smith 
3092f07f9ceaSJed Brown @*/
30931a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
30941a266240SBarry Smith {
30951a266240SBarry Smith   PetscFunctionBegin;
3096171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30971a266240SBarry Smith   dm->ctxdestroy = destroy;
30981a266240SBarry Smith   PetscFunctionReturn(0);
30991a266240SBarry Smith }
31001a266240SBarry Smith 
3101b07ff414SBarry Smith /*@
31021b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
310347c6ae99SBarry Smith 
310447c6ae99SBarry Smith     Not Collective
310547c6ae99SBarry Smith 
310647c6ae99SBarry Smith     Input Parameters:
310747c6ae99SBarry Smith +   dm - the DM object
310847c6ae99SBarry Smith -   ctx - the user context
310947c6ae99SBarry Smith 
311047c6ae99SBarry Smith     Level: intermediate
311147c6ae99SBarry Smith 
3112e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
311347c6ae99SBarry Smith 
311447c6ae99SBarry Smith @*/
31151b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
311647c6ae99SBarry Smith {
311747c6ae99SBarry Smith   PetscFunctionBegin;
3118171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
311947c6ae99SBarry Smith   dm->ctx = ctx;
312047c6ae99SBarry Smith   PetscFunctionReturn(0);
312147c6ae99SBarry Smith }
312247c6ae99SBarry Smith 
312347c6ae99SBarry Smith /*@
31241b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
312547c6ae99SBarry Smith 
312647c6ae99SBarry Smith     Not Collective
312747c6ae99SBarry Smith 
312847c6ae99SBarry Smith     Input Parameter:
312947c6ae99SBarry Smith .   dm - the DM object
313047c6ae99SBarry Smith 
313147c6ae99SBarry Smith     Output Parameter:
313247c6ae99SBarry Smith .   ctx - the user context
313347c6ae99SBarry Smith 
313447c6ae99SBarry Smith     Level: intermediate
313547c6ae99SBarry Smith 
3136e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
313747c6ae99SBarry Smith 
313847c6ae99SBarry Smith @*/
31391b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
314047c6ae99SBarry Smith {
314147c6ae99SBarry Smith   PetscFunctionBegin;
3142171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31431b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
314447c6ae99SBarry Smith   PetscFunctionReturn(0);
314547c6ae99SBarry Smith }
314647c6ae99SBarry Smith 
314708da532bSDmitry Karpeev /*@C
3148df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
314908da532bSDmitry Karpeev 
315008da532bSDmitry Karpeev     Logically Collective on DM
315108da532bSDmitry Karpeev 
315208da532bSDmitry Karpeev     Input Parameter:
315308da532bSDmitry Karpeev +   dm - the DM object
31540298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
315508da532bSDmitry Karpeev 
315608da532bSDmitry Karpeev     Level: intermediate
315708da532bSDmitry Karpeev 
3158835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
315908da532bSDmitry Karpeev          DMSetJacobian()
316008da532bSDmitry Karpeev 
316108da532bSDmitry Karpeev @*/
316208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
316308da532bSDmitry Karpeev {
316408da532bSDmitry Karpeev   PetscFunctionBegin;
316508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
316608da532bSDmitry Karpeev   PetscFunctionReturn(0);
316708da532bSDmitry Karpeev }
316808da532bSDmitry Karpeev 
316908da532bSDmitry Karpeev /*@
317008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
317108da532bSDmitry Karpeev 
317208da532bSDmitry Karpeev     Not Collective
317308da532bSDmitry Karpeev 
317408da532bSDmitry Karpeev     Input Parameter:
317508da532bSDmitry Karpeev .   dm - the DM object to destroy
317608da532bSDmitry Karpeev 
317708da532bSDmitry Karpeev     Output Parameter:
317808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
317908da532bSDmitry Karpeev 
318008da532bSDmitry Karpeev     Level: developer
318108da532bSDmitry Karpeev 
318274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
318308da532bSDmitry Karpeev 
318408da532bSDmitry Karpeev @*/
318508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
318608da532bSDmitry Karpeev {
318708da532bSDmitry Karpeev   PetscFunctionBegin;
318808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
318908da532bSDmitry Karpeev   PetscFunctionReturn(0);
319008da532bSDmitry Karpeev }
319108da532bSDmitry Karpeev 
319208da532bSDmitry Karpeev /*@C
319308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
319408da532bSDmitry Karpeev 
319508da532bSDmitry Karpeev     Logically Collective on DM
319608da532bSDmitry Karpeev 
319708da532bSDmitry Karpeev     Input Parameters:
3198907376e6SBarry Smith .   dm - the DM object
319908da532bSDmitry Karpeev 
320008da532bSDmitry Karpeev     Output parameters:
320108da532bSDmitry Karpeev +   xl - lower bound
320208da532bSDmitry Karpeev -   xu - upper bound
320308da532bSDmitry Karpeev 
3204907376e6SBarry Smith     Level: advanced
3205907376e6SBarry Smith 
320695452b02SPatrick Sanan     Notes:
320795452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
320808da532bSDmitry Karpeev 
320974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
321008da532bSDmitry Karpeev 
321108da532bSDmitry Karpeev @*/
321208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
321308da532bSDmitry Karpeev {
321408da532bSDmitry Karpeev   PetscErrorCode ierr;
32155fd66863SKarl Rupp 
321608da532bSDmitry Karpeev   PetscFunctionBegin;
321708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
321808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
321908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
322008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
32218865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
322208da532bSDmitry Karpeev   PetscFunctionReturn(0);
322308da532bSDmitry Karpeev }
322408da532bSDmitry Karpeev 
3225b0ae01b7SPeter Brune /*@
3226b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3227b0ae01b7SPeter Brune 
3228b0ae01b7SPeter Brune     Not Collective
3229b0ae01b7SPeter Brune 
3230b0ae01b7SPeter Brune     Input Parameter:
3231b0ae01b7SPeter Brune .   dm - the DM object
3232b0ae01b7SPeter Brune 
3233b0ae01b7SPeter Brune     Output Parameter:
3234b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3235b0ae01b7SPeter Brune 
3236b0ae01b7SPeter Brune     Level: developer
3237b0ae01b7SPeter Brune 
3238b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3239b0ae01b7SPeter Brune 
3240b0ae01b7SPeter Brune @*/
3241b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3242b0ae01b7SPeter Brune {
3243b0ae01b7SPeter Brune   PetscFunctionBegin;
3244b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3245b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3246b0ae01b7SPeter Brune }
3247b0ae01b7SPeter Brune 
32483ad4599aSBarry Smith /*@
32493ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
32503ad4599aSBarry Smith 
32513ad4599aSBarry Smith     Not Collective
32523ad4599aSBarry Smith 
32533ad4599aSBarry Smith     Input Parameter:
32543ad4599aSBarry Smith .   dm - the DM object
32553ad4599aSBarry Smith 
32563ad4599aSBarry Smith     Output Parameter:
32573ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
32583ad4599aSBarry Smith 
32593ad4599aSBarry Smith     Level: developer
32603ad4599aSBarry Smith 
32613ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
32623ad4599aSBarry Smith 
32633ad4599aSBarry Smith @*/
32643ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
32653ad4599aSBarry Smith {
32663ad4599aSBarry Smith   PetscFunctionBegin;
32673ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
32683ad4599aSBarry Smith   PetscFunctionReturn(0);
32693ad4599aSBarry Smith }
32703ad4599aSBarry Smith 
3271a7058e45SLawrence Mitchell 
3272a7058e45SLawrence Mitchell /*@
3273a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3274a7058e45SLawrence Mitchell 
3275a7058e45SLawrence Mitchell     Not Collective
3276a7058e45SLawrence Mitchell 
3277a7058e45SLawrence Mitchell     Input Parameter:
3278a7058e45SLawrence Mitchell .   dm - the DM object
3279a7058e45SLawrence Mitchell 
3280a7058e45SLawrence Mitchell     Output Parameter:
3281a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3282a7058e45SLawrence Mitchell 
3283a7058e45SLawrence Mitchell     Level: developer
3284a7058e45SLawrence Mitchell 
3285a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3286a7058e45SLawrence Mitchell 
3287a7058e45SLawrence Mitchell @*/
3288a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3289a7058e45SLawrence Mitchell {
32904a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3291a7058e45SLawrence Mitchell   PetscFunctionBegin;
32924a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
32934a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3294a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3295a7058e45SLawrence Mitchell }
3296a7058e45SLawrence Mitchell 
3297748fac09SDmitry Karpeev /*@C
329808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
329908da532bSDmitry Karpeev 
330008da532bSDmitry Karpeev     Collective on DM
330108da532bSDmitry Karpeev 
330208da532bSDmitry Karpeev     Input Parameter:
330308da532bSDmitry Karpeev +   dm - the DM object
33040298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
330508da532bSDmitry Karpeev 
330608da532bSDmitry Karpeev     Level: developer
330708da532bSDmitry Karpeev 
330874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
330908da532bSDmitry Karpeev 
331008da532bSDmitry Karpeev @*/
331108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
331208da532bSDmitry Karpeev {
331308da532bSDmitry Karpeev   PetscErrorCode ierr;
33145fd66863SKarl Rupp 
331508da532bSDmitry Karpeev   PetscFunctionBegin;
331608da532bSDmitry Karpeev   if (x) {
331708da532bSDmitry Karpeev     if (!dm->x) {
331808da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
331908da532bSDmitry Karpeev     }
332008da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
33218865f1eaSKarl Rupp   } else if (dm->x) {
332208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
332308da532bSDmitry Karpeev   }
332408da532bSDmitry Karpeev   PetscFunctionReturn(0);
332508da532bSDmitry Karpeev }
332608da532bSDmitry Karpeev 
33270298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3328264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3329264ace61SBarry Smith 
3330264ace61SBarry Smith /*@C
3331264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3332264ace61SBarry Smith 
3333264ace61SBarry Smith   Collective on DM
3334264ace61SBarry Smith 
3335264ace61SBarry Smith   Input Parameters:
3336264ace61SBarry Smith + dm     - The DM object
3337264ace61SBarry Smith - method - The name of the DM type
3338264ace61SBarry Smith 
3339264ace61SBarry Smith   Options Database Key:
3340264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3341264ace61SBarry Smith 
3342264ace61SBarry Smith   Notes:
3343e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3344264ace61SBarry Smith 
3345264ace61SBarry Smith   Level: intermediate
3346264ace61SBarry Smith 
3347264ace61SBarry Smith .keywords: DM, set, type
3348264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3349264ace61SBarry Smith @*/
335019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3351264ace61SBarry Smith {
3352264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3353264ace61SBarry Smith   PetscBool      match;
3354264ace61SBarry Smith   PetscErrorCode ierr;
3355264ace61SBarry Smith 
3356264ace61SBarry Smith   PetscFunctionBegin;
3357264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3358251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3359264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3360264ace61SBarry Smith 
33610f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
33621c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3363ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3364264ace61SBarry Smith 
3365264ace61SBarry Smith   if (dm->ops->destroy) {
3366264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
33670298fd71SBarry Smith     dm->ops->destroy = NULL;
3368264ace61SBarry Smith   }
3369264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3370264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3371264ace61SBarry Smith   PetscFunctionReturn(0);
3372264ace61SBarry Smith }
3373264ace61SBarry Smith 
3374264ace61SBarry Smith /*@C
3375264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3376264ace61SBarry Smith 
3377264ace61SBarry Smith   Not Collective
3378264ace61SBarry Smith 
3379264ace61SBarry Smith   Input Parameter:
3380264ace61SBarry Smith . dm  - The DM
3381264ace61SBarry Smith 
3382264ace61SBarry Smith   Output Parameter:
3383264ace61SBarry Smith . type - The DM type name
3384264ace61SBarry Smith 
3385264ace61SBarry Smith   Level: intermediate
3386264ace61SBarry Smith 
3387264ace61SBarry Smith .keywords: DM, get, type, name
3388264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3389264ace61SBarry Smith @*/
339019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3391264ace61SBarry Smith {
3392264ace61SBarry Smith   PetscErrorCode ierr;
3393264ace61SBarry Smith 
3394264ace61SBarry Smith   PetscFunctionBegin;
3395264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3396c959eef4SJed Brown   PetscValidPointer(type,2);
3397607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3398264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3399264ace61SBarry Smith   PetscFunctionReturn(0);
3400264ace61SBarry Smith }
3401264ace61SBarry Smith 
340267a56275SMatthew G Knepley /*@C
340367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
340467a56275SMatthew G Knepley 
340567a56275SMatthew G Knepley   Collective on DM
340667a56275SMatthew G Knepley 
340767a56275SMatthew G Knepley   Input Parameters:
340867a56275SMatthew G Knepley + dm - the DM
340967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
341067a56275SMatthew G Knepley 
341167a56275SMatthew G Knepley   Output Parameter:
341267a56275SMatthew G Knepley . M - pointer to new DM
341367a56275SMatthew G Knepley 
341467a56275SMatthew G Knepley   Notes:
341567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
341667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
341767a56275SMatthew G Knepley   of the input DM.
341867a56275SMatthew G Knepley 
341967a56275SMatthew G Knepley   Level: intermediate
342067a56275SMatthew G Knepley 
342167a56275SMatthew G Knepley .seealso: DMCreate()
342267a56275SMatthew G Knepley @*/
342319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
342467a56275SMatthew G Knepley {
342567a56275SMatthew G Knepley   DM             B;
342667a56275SMatthew G Knepley   char           convname[256];
3427c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
342867a56275SMatthew G Knepley   PetscErrorCode ierr;
342967a56275SMatthew G Knepley 
343067a56275SMatthew G Knepley   PetscFunctionBegin;
343167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
343267a56275SMatthew G Knepley   PetscValidType(dm,1);
343367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3434251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3435c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3436c067b6caSMatthew G. Knepley   if (sametype) {
3437c067b6caSMatthew G. Knepley     *M   = dm;
3438c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3439c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3440c067b6caSMatthew G. Knepley   } else {
34410298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
344267a56275SMatthew G Knepley 
344367a56275SMatthew G Knepley     /*
344467a56275SMatthew G Knepley        Order of precedence:
344567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
344667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
344767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
344867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
344967a56275SMatthew G Knepley        5) Use a really basic converter.
345067a56275SMatthew G Knepley     */
345167a56275SMatthew G Knepley 
345267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3453a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3454a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3455a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3456a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3457a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34580005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
345967a56275SMatthew G Knepley     if (conv) goto foundconv;
346067a56275SMatthew G Knepley 
346167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
346282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
346367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3464a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3465a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3466a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3467a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3468a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34690005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
347067a56275SMatthew G Knepley     if (conv) {
3471fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
347267a56275SMatthew G Knepley       goto foundconv;
347367a56275SMatthew G Knepley     }
347467a56275SMatthew G Knepley 
347567a56275SMatthew G Knepley #if 0
347667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
347767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3478fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
347967a56275SMatthew G Knepley     if (conv) goto foundconv;
348067a56275SMatthew G Knepley 
348167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
348267a56275SMatthew G Knepley     if (dm->ops->convert) {
348367a56275SMatthew G Knepley       conv = dm->ops->convert;
348467a56275SMatthew G Knepley     }
348567a56275SMatthew G Knepley     if (conv) goto foundconv;
348667a56275SMatthew G Knepley #endif
348767a56275SMatthew G Knepley 
348867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
348982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
349067a56275SMatthew G Knepley 
349167a56275SMatthew G Knepley foundconv:
349267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
349367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
349412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
349590b157c4SStefano Zampini     {
349690b157c4SStefano Zampini       PetscBool             isper;
349712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
349812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
349990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
350090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
350112fa691eSMatthew G. Knepley     }
350267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
350367a56275SMatthew G Knepley   }
350467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
350567a56275SMatthew G Knepley   PetscFunctionReturn(0);
350667a56275SMatthew G Knepley }
3507264ace61SBarry Smith 
3508264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3509264ace61SBarry Smith 
3510264ace61SBarry Smith /*@C
35111c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
35121c84c290SBarry Smith 
35131c84c290SBarry Smith   Not Collective
35141c84c290SBarry Smith 
35151c84c290SBarry Smith   Input Parameters:
35161c84c290SBarry Smith + name        - The name of a new user-defined creation routine
35171c84c290SBarry Smith - create_func - The creation routine itself
35181c84c290SBarry Smith 
35191c84c290SBarry Smith   Notes:
35201c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
35211c84c290SBarry Smith 
35221c84c290SBarry Smith 
35231c84c290SBarry Smith   Sample usage:
35241c84c290SBarry Smith .vb
3525bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
35261c84c290SBarry Smith .ve
35271c84c290SBarry Smith 
35281c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
35291c84c290SBarry Smith .vb
35301c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
35311c84c290SBarry Smith     DMSetType(DM,"my_da");
35321c84c290SBarry Smith .ve
35331c84c290SBarry Smith    or at runtime via the option
35341c84c290SBarry Smith .vb
35351c84c290SBarry Smith     -da_type my_da
35361c84c290SBarry Smith .ve
3537264ace61SBarry Smith 
3538264ace61SBarry Smith   Level: advanced
35391c84c290SBarry Smith 
35401c84c290SBarry Smith .keywords: DM, register
3541bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
35421c84c290SBarry Smith 
3543264ace61SBarry Smith @*/
3544bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3545264ace61SBarry Smith {
3546264ace61SBarry Smith   PetscErrorCode ierr;
3547264ace61SBarry Smith 
3548264ace61SBarry Smith   PetscFunctionBegin;
35491d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3550a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3551264ace61SBarry Smith   PetscFunctionReturn(0);
3552264ace61SBarry Smith }
3553264ace61SBarry Smith 
3554b859378eSBarry Smith /*@C
355555849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3556b859378eSBarry Smith 
3557b859378eSBarry Smith   Collective on PetscViewer
3558b859378eSBarry Smith 
3559b859378eSBarry Smith   Input Parameters:
3560b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3561b859378eSBarry Smith            some related function before a call to DMLoad().
3562b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3563b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3564b859378eSBarry Smith 
3565b859378eSBarry Smith    Level: intermediate
3566b859378eSBarry Smith 
3567b859378eSBarry Smith   Notes:
356855849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3569b859378eSBarry Smith 
3570b859378eSBarry Smith   Notes for advanced users:
3571b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3572b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3573b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3574b859378eSBarry Smith   format is
3575b859378eSBarry Smith .vb
3576b859378eSBarry Smith      has not yet been determined
3577b859378eSBarry Smith .ve
3578b859378eSBarry Smith 
3579b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3580b859378eSBarry Smith @*/
3581b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3582b859378eSBarry Smith {
35839331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3584b859378eSBarry Smith   PetscErrorCode ierr;
3585b859378eSBarry Smith 
3586b859378eSBarry Smith   PetscFunctionBegin;
3587b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3588b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3589fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
359032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
35919331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
35929331c7a4SMatthew G. Knepley   if (isbinary) {
35939331c7a4SMatthew G. Knepley     PetscInt classid;
35949331c7a4SMatthew G. Knepley     char     type[256];
3595b859378eSBarry Smith 
3596060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
35979200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3598060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
359932c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
36009331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
36019331c7a4SMatthew G. Knepley   } else if (ishdf5) {
36029331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
36039331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3604b859378eSBarry Smith   PetscFunctionReturn(0);
3605b859378eSBarry Smith }
3606b859378eSBarry Smith 
36077da65231SMatthew G Knepley /******************************** FEM Support **********************************/
36087da65231SMatthew G Knepley 
3609a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3610a6dfd86eSKarl Rupp {
36111d47ebbbSSatish Balay   PetscInt       f;
36121b30c384SMatthew G Knepley   PetscErrorCode ierr;
36131b30c384SMatthew G Knepley 
36147da65231SMatthew G Knepley   PetscFunctionBegin;
361574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
36161d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
361757622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
36187da65231SMatthew G Knepley   }
36197da65231SMatthew G Knepley   PetscFunctionReturn(0);
36207da65231SMatthew G Knepley }
36217da65231SMatthew G Knepley 
3622a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3623a6dfd86eSKarl Rupp {
36241b30c384SMatthew G Knepley   PetscInt       f, g;
36257da65231SMatthew G Knepley   PetscErrorCode ierr;
36267da65231SMatthew G Knepley 
36277da65231SMatthew G Knepley   PetscFunctionBegin;
362874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
36291d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
363074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
36311d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3632e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
36337da65231SMatthew G Knepley     }
363474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
36357da65231SMatthew G Knepley   }
36367da65231SMatthew G Knepley   PetscFunctionReturn(0);
36377da65231SMatthew G Knepley }
3638e7c4fc90SDmitry Karpeev 
36396113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3640e759306cSMatthew G. Knepley {
36410c5b8624SToby Isaac   PetscInt          localSize, bs;
36420c5b8624SToby Isaac   PetscMPIInt       size;
36430c5b8624SToby Isaac   Vec               x, xglob;
36440c5b8624SToby Isaac   const PetscScalar *xarray;
3645e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3646e759306cSMatthew G. Knepley 
3647e759306cSMatthew G. Knepley   PetscFunctionBegin;
36489852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3649e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3650e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
36516113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
36520c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
36530c5b8624SToby Isaac   if (size > 1) {
36540c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
36550c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
36560c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
36570c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
36580c5b8624SToby Isaac   } else {
36590c5b8624SToby Isaac     xglob = x;
36600c5b8624SToby Isaac   }
36610c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
36620c5b8624SToby Isaac   if (size > 1) {
36630c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
36640c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
36650c5b8624SToby Isaac   }
3666e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3667e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3668e759306cSMatthew G. Knepley }
3669e759306cSMatthew G. Knepley 
367088ed4aceSMatthew G Knepley /*@
3671e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
367288ed4aceSMatthew G Knepley 
367388ed4aceSMatthew G Knepley   Input Parameter:
367488ed4aceSMatthew G Knepley . dm - The DM
367588ed4aceSMatthew G Knepley 
367688ed4aceSMatthew G Knepley   Output Parameter:
367788ed4aceSMatthew G Knepley . section - The PetscSection
367888ed4aceSMatthew G Knepley 
3679e5893cccSMatthew G. Knepley   Options Database Keys:
3680e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3681e5893cccSMatthew G. Knepley 
368288ed4aceSMatthew G Knepley   Level: intermediate
368388ed4aceSMatthew G Knepley 
368488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
368588ed4aceSMatthew G Knepley 
3686e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
368788ed4aceSMatthew G Knepley @*/
3688e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
36890adebc6cSBarry Smith {
3690fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3691fd59a867SMatthew G. Knepley 
369288ed4aceSMatthew G Knepley   PetscFunctionBegin;
369388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
36952f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
3696e5e52638SMatthew G. Knepley     PetscInt d;
3697e5e52638SMatthew G. Knepley 
3698e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
36992f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3700ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
37012f0f8703SMatthew G. Knepley   }
370288ed4aceSMatthew G Knepley   *section = dm->defaultSection;
370388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
370488ed4aceSMatthew G Knepley }
370588ed4aceSMatthew G Knepley 
370688ed4aceSMatthew G Knepley /*@
3707e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
370888ed4aceSMatthew G Knepley 
370988ed4aceSMatthew G Knepley   Input Parameters:
371088ed4aceSMatthew G Knepley + dm - The DM
371188ed4aceSMatthew G Knepley - section - The PetscSection
371288ed4aceSMatthew G Knepley 
371388ed4aceSMatthew G Knepley   Level: intermediate
371488ed4aceSMatthew G Knepley 
371588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
371688ed4aceSMatthew G Knepley 
3717e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
371888ed4aceSMatthew G Knepley @*/
3719e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
37200adebc6cSBarry Smith {
3721c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3722af122d2aSMatthew G Knepley   PetscInt       f;
372388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
372488ed4aceSMatthew G Knepley 
372588ed4aceSMatthew G Knepley   PetscFunctionBegin;
372688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3727c473ab19SMatthew G. Knepley   if (section) {
37281d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
37291d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3730c473ab19SMatthew G. Knepley   }
373188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
373288ed4aceSMatthew G Knepley   dm->defaultSection = section;
3733c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3734af122d2aSMatthew G Knepley   if (numFields) {
3735af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3736af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
37370f21e855SMatthew G. Knepley       PetscObject disc;
3738af122d2aSMatthew G Knepley       const char *name;
3739af122d2aSMatthew G Knepley 
3740af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
374144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
37420f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3743af122d2aSMatthew G Knepley     }
3744af122d2aSMatthew G Knepley   }
3745e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
37461d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
374788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
374888ed4aceSMatthew G Knepley }
374988ed4aceSMatthew G Knepley 
37509435951eSToby Isaac /*@
37519435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
37529435951eSToby Isaac 
3753e228b242SToby Isaac   not collective
3754e228b242SToby Isaac 
37559435951eSToby Isaac   Input Parameter:
37569435951eSToby Isaac . dm - The DM
37579435951eSToby Isaac 
37589435951eSToby Isaac   Output Parameter:
37599435951eSToby 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.
37609435951eSToby 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.
37619435951eSToby Isaac 
37629435951eSToby Isaac   Level: advanced
37639435951eSToby Isaac 
37649435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
37659435951eSToby Isaac 
37669435951eSToby Isaac .seealso: DMSetDefaultConstraints()
37679435951eSToby Isaac @*/
37689435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
37699435951eSToby Isaac {
37709435951eSToby Isaac   PetscErrorCode ierr;
37719435951eSToby Isaac 
37729435951eSToby Isaac   PetscFunctionBegin;
37739435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37749435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
377545a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
377645a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
37779435951eSToby Isaac   PetscFunctionReturn(0);
37789435951eSToby Isaac }
37799435951eSToby Isaac 
37809435951eSToby Isaac /*@
37819435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
37829435951eSToby Isaac 
37839435951eSToby 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().
37849435951eSToby Isaac 
37859435951eSToby 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.
37869435951eSToby Isaac 
3787e228b242SToby Isaac   collective on dm
3788e228b242SToby Isaac 
37899435951eSToby Isaac   Input Parameters:
37909435951eSToby Isaac + dm - The DM
3791e228b242SToby 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).
3792e228b242SToby 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).
37939435951eSToby Isaac 
37949435951eSToby Isaac   Level: advanced
37959435951eSToby Isaac 
37969435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
37979435951eSToby Isaac 
37989435951eSToby Isaac .seealso: DMGetDefaultConstraints()
37999435951eSToby Isaac @*/
38009435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
38019435951eSToby Isaac {
3802e228b242SToby Isaac   PetscMPIInt result;
38039435951eSToby Isaac   PetscErrorCode ierr;
38049435951eSToby Isaac 
38059435951eSToby Isaac   PetscFunctionBegin;
38069435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3807e228b242SToby Isaac   if (section) {
3808e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3809e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3810f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3811e228b242SToby Isaac   }
3812e228b242SToby Isaac   if (mat) {
3813e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3814e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3815f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3816e228b242SToby Isaac   }
38179435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
38189435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
38199435951eSToby Isaac   dm->defaultConstraintSection = section;
38209435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
38219435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
38229435951eSToby Isaac   dm->defaultConstraintMat = mat;
38239435951eSToby Isaac   PetscFunctionReturn(0);
38249435951eSToby Isaac }
38259435951eSToby Isaac 
3826497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3827507e4973SMatthew G. Knepley /*
3828507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3829507e4973SMatthew G. Knepley 
3830507e4973SMatthew G. Knepley   Input Parameters:
3831507e4973SMatthew G. Knepley + dm - The DM
3832507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3833507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3834507e4973SMatthew G. Knepley 
3835507e4973SMatthew G. Knepley   Level: intermediate
3836507e4973SMatthew G. Knepley 
3837507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3838507e4973SMatthew G. Knepley */
3839f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3840507e4973SMatthew G. Knepley {
3841507e4973SMatthew G. Knepley   MPI_Comm        comm;
3842507e4973SMatthew G. Knepley   PetscLayout     layout;
3843507e4973SMatthew G. Knepley   const PetscInt *ranges;
3844507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3845507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3846507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3847507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3848507e4973SMatthew G. Knepley 
3849507e4973SMatthew G. Knepley   PetscFunctionBegin;
3850507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3851507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3852507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3853507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3854507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3855507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3856507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3857507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3858507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3859507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3860507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3861507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3862f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3863507e4973SMatthew G. Knepley 
3864507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3865507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3866507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3867507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3868507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3869507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3870507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3871507e4973SMatthew 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;}
3872507e4973SMatthew 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;}
3873507e4973SMatthew G. Knepley     if (gdof < 0) {
3874507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3875507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3876507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3877507e4973SMatthew G. Knepley 
3878507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3879507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3880507e4973SMatthew 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;}
3881507e4973SMatthew G. Knepley       }
3882507e4973SMatthew G. Knepley     }
3883507e4973SMatthew G. Knepley   }
3884507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3885507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3886b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3887507e4973SMatthew G. Knepley   if (!gvalid) {
3888507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3889507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3890507e4973SMatthew G. Knepley   }
3891507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3892507e4973SMatthew G. Knepley }
3893f741bcd2SMatthew G. Knepley #endif
3894507e4973SMatthew G. Knepley 
389588ed4aceSMatthew G Knepley /*@
3896e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
389788ed4aceSMatthew G Knepley 
38988b1ab98fSJed Brown   Collective on DM
38998b1ab98fSJed Brown 
390088ed4aceSMatthew G Knepley   Input Parameter:
390188ed4aceSMatthew G Knepley . dm - The DM
390288ed4aceSMatthew G Knepley 
390388ed4aceSMatthew G Knepley   Output Parameter:
390488ed4aceSMatthew G Knepley . section - The PetscSection
390588ed4aceSMatthew G Knepley 
390688ed4aceSMatthew G Knepley   Level: intermediate
390788ed4aceSMatthew G Knepley 
390888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
390988ed4aceSMatthew G Knepley 
3910e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
391188ed4aceSMatthew G Knepley @*/
3912e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
39130adebc6cSBarry Smith {
391488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
391588ed4aceSMatthew G Knepley 
391688ed4aceSMatthew G Knepley   PetscFunctionBegin;
391788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
391888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
391988ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3920fd59a867SMatthew G. Knepley     PetscSection s;
3921fd59a867SMatthew G. Knepley 
3922e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3923fd59a867SMatthew 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");
392433907cc2SStefano Zampini     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
392515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3926cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3927ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3928685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
392988ed4aceSMatthew G Knepley   }
393088ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
393188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
393288ed4aceSMatthew G Knepley }
393388ed4aceSMatthew G Knepley 
3934b21d0597SMatthew G Knepley /*@
3935e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3936b21d0597SMatthew G Knepley 
3937b21d0597SMatthew G Knepley   Input Parameters:
3938b21d0597SMatthew G Knepley + dm - The DM
39395080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3940b21d0597SMatthew G Knepley 
3941b21d0597SMatthew G Knepley   Level: intermediate
3942b21d0597SMatthew G Knepley 
3943b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3944b21d0597SMatthew G Knepley 
3945e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3946b21d0597SMatthew G Knepley @*/
3947e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
39480adebc6cSBarry Smith {
3949b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3950b21d0597SMatthew G Knepley 
3951b21d0597SMatthew G Knepley   PetscFunctionBegin;
3952b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39535080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
39541d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3955b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3956b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3957497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3958f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3959507e4973SMatthew G. Knepley #endif
3960b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3961b21d0597SMatthew G Knepley }
3962b21d0597SMatthew G Knepley 
396388ed4aceSMatthew G Knepley /*@
396488ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
396588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
396688ed4aceSMatthew G Knepley 
396788ed4aceSMatthew G Knepley   Input Parameter:
396888ed4aceSMatthew G Knepley . dm - The DM
396988ed4aceSMatthew G Knepley 
397088ed4aceSMatthew G Knepley   Output Parameter:
397188ed4aceSMatthew G Knepley . sf - The PetscSF
397288ed4aceSMatthew G Knepley 
397388ed4aceSMatthew G Knepley   Level: intermediate
397488ed4aceSMatthew G Knepley 
397588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
397688ed4aceSMatthew G Knepley 
397788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
397888ed4aceSMatthew G Knepley @*/
39790adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
39800adebc6cSBarry Smith {
398188ed4aceSMatthew G Knepley   PetscInt       nroots;
398288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
398388ed4aceSMatthew G Knepley 
398488ed4aceSMatthew G Knepley   PetscFunctionBegin;
398588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
398688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
398733907cc2SStefano Zampini   if (!dm->defaultSF) {
398833907cc2SStefano Zampini     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->defaultSF);CHKERRQ(ierr);
398933907cc2SStefano Zampini   }
39900298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
399188ed4aceSMatthew G Knepley   if (nroots < 0) {
399288ed4aceSMatthew G Knepley     PetscSection section, gSection;
399388ed4aceSMatthew G Knepley 
3994e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
399531ea6d37SMatthew G Knepley     if (section) {
3996e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
399788ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
399831ea6d37SMatthew G Knepley     } else {
39990298fd71SBarry Smith       *sf = NULL;
400031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
400131ea6d37SMatthew G Knepley     }
400288ed4aceSMatthew G Knepley   }
400388ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
400488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
400588ed4aceSMatthew G Knepley }
400688ed4aceSMatthew G Knepley 
400788ed4aceSMatthew G Knepley /*@
400888ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
400988ed4aceSMatthew G Knepley 
401088ed4aceSMatthew G Knepley   Input Parameters:
401188ed4aceSMatthew G Knepley + dm - The DM
401288ed4aceSMatthew G Knepley - sf - The PetscSF
401388ed4aceSMatthew G Knepley 
401488ed4aceSMatthew G Knepley   Level: intermediate
401588ed4aceSMatthew G Knepley 
401688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
401788ed4aceSMatthew G Knepley 
401888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
401988ed4aceSMatthew G Knepley @*/
40200adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
40210adebc6cSBarry Smith {
402288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
402388ed4aceSMatthew G Knepley 
402488ed4aceSMatthew G Knepley   PetscFunctionBegin;
402588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
402633907cc2SStefano Zampini   if (sf) {
402788ed4aceSMatthew G Knepley     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
402833907cc2SStefano Zampini     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
402933907cc2SStefano Zampini   }
403088ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
403188ed4aceSMatthew G Knepley   dm->defaultSF = sf;
403288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
403388ed4aceSMatthew G Knepley }
403488ed4aceSMatthew G Knepley 
403588ed4aceSMatthew G Knepley /*@C
403688ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
403788ed4aceSMatthew G Knepley   describing the data layout.
403888ed4aceSMatthew G Knepley 
403988ed4aceSMatthew G Knepley   Input Parameters:
404088ed4aceSMatthew G Knepley + dm - The DM
404188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
404288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
404388ed4aceSMatthew G Knepley 
404488ed4aceSMatthew G Knepley   Level: intermediate
404588ed4aceSMatthew G Knepley 
404688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
404788ed4aceSMatthew G Knepley @*/
404888ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
404988ed4aceSMatthew G Knepley {
405082f516ccSBarry Smith   MPI_Comm       comm;
405188ed4aceSMatthew G Knepley   PetscLayout    layout;
405288ed4aceSMatthew G Knepley   const PetscInt *ranges;
405388ed4aceSMatthew G Knepley   PetscInt       *local;
405488ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4055ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
405688ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
405788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
405888ed4aceSMatthew G Knepley 
405988ed4aceSMatthew G Knepley   PetscFunctionBegin;
406088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4061367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
406288ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
406388ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
406488ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
406588ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
406688ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
406788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
406888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
406988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
407088ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4071ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
40726636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
407388ed4aceSMatthew G Knepley 
40746636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40756636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4076235fbf56SMatthew 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));
40776636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
407888ed4aceSMatthew G Knepley   }
4079785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4080785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
408188ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
40821f588964SMatthew G Knepley     const PetscInt *cind;
40836636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
408488ed4aceSMatthew G Knepley 
408588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
408688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
408788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
408888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
408988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40906636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
409188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
40926636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
40936636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
40946636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4095057b4bcdSMatthew 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);
40966636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
40976636e97aSMatthew G Knepley     }
409888ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
409988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
410088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
410188ed4aceSMatthew G Knepley     }
410288ed4aceSMatthew G Knepley     if (gdof < 0) {
41036636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
410488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
410588ed4aceSMatthew G Knepley 
410605376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
410731d3f06eSJed Brown         if (r < 0) r = -(r+2);
410805376888SMatthew 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);
410988ed4aceSMatthew G Knepley         remote[l].rank  = r;
411088ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
411188ed4aceSMatthew G Knepley       }
411288ed4aceSMatthew G Knepley     } else {
41136636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
411488ed4aceSMatthew G Knepley         remote[l].rank  = rank;
411588ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
411688ed4aceSMatthew G Knepley       }
411788ed4aceSMatthew G Knepley     }
411888ed4aceSMatthew G Knepley   }
41196636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
412088ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
412188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
412288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
412388ed4aceSMatthew G Knepley }
4124af122d2aSMatthew G Knepley 
4125b21d0597SMatthew G Knepley /*@
4126b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4127b21d0597SMatthew G Knepley 
4128b21d0597SMatthew G Knepley   Input Parameter:
4129b21d0597SMatthew G Knepley . dm - The DM
4130b21d0597SMatthew G Knepley 
4131b21d0597SMatthew G Knepley   Output Parameter:
4132b21d0597SMatthew G Knepley . sf - The PetscSF
4133b21d0597SMatthew G Knepley 
4134b21d0597SMatthew G Knepley   Level: intermediate
4135b21d0597SMatthew G Knepley 
4136b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4137b21d0597SMatthew G Knepley 
4138057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4139b21d0597SMatthew G Knepley @*/
41400adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
41410adebc6cSBarry Smith {
4142b21d0597SMatthew G Knepley   PetscFunctionBegin;
4143b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4144b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4145b21d0597SMatthew G Knepley   *sf = dm->sf;
4146b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4147b21d0597SMatthew G Knepley }
4148b21d0597SMatthew G Knepley 
4149057b4bcdSMatthew G Knepley /*@
4150057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4151057b4bcdSMatthew G Knepley 
4152057b4bcdSMatthew G Knepley   Input Parameters:
4153057b4bcdSMatthew G Knepley + dm - The DM
4154057b4bcdSMatthew G Knepley - sf - The PetscSF
4155057b4bcdSMatthew G Knepley 
4156057b4bcdSMatthew G Knepley   Level: intermediate
4157057b4bcdSMatthew G Knepley 
4158057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4159057b4bcdSMatthew G Knepley @*/
41600adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
41610adebc6cSBarry Smith {
4162057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4163057b4bcdSMatthew G Knepley 
4164057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4165057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
416633907cc2SStefano Zampini   if (sf) {
416733907cc2SStefano Zampini     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4168057b4bcdSMatthew G Knepley     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
416933907cc2SStefano Zampini   }
417033907cc2SStefano Zampini   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4171057b4bcdSMatthew G Knepley   dm->sf = sf;
4172057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4173057b4bcdSMatthew G Knepley }
4174057b4bcdSMatthew G Knepley 
417544a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
417644a7f3ddSMatthew G. Knepley {
417744a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
417844a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
417944a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
418044a7f3ddSMatthew G. Knepley 
418144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
418244a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
418344a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
418444a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
418544a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
418644a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
418744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
418844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
418944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
419044a7f3ddSMatthew G. Knepley }
419144a7f3ddSMatthew G. Knepley 
419244a7f3ddSMatthew G. Knepley /*@
419344a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
419444a7f3ddSMatthew G. Knepley 
419544a7f3ddSMatthew G. Knepley   Logically collective on DM
419644a7f3ddSMatthew G. Knepley 
419744a7f3ddSMatthew G. Knepley   Input Parameter:
419844a7f3ddSMatthew G. Knepley . dm - The DM
419944a7f3ddSMatthew G. Knepley 
420044a7f3ddSMatthew G. Knepley   Level: intermediate
420144a7f3ddSMatthew G. Knepley 
420244a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
420344a7f3ddSMatthew G. Knepley @*/
420444a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
420544a7f3ddSMatthew G. Knepley {
420644a7f3ddSMatthew G. Knepley   PetscInt       f;
420744a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
420844a7f3ddSMatthew G. Knepley 
420944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
421044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
421144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
421244a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
421344a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
421444a7f3ddSMatthew G. Knepley   }
421544a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
421644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
421744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
421844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
421944a7f3ddSMatthew G. Knepley }
422044a7f3ddSMatthew G. Knepley 
4221689b5837SMatthew G. Knepley /*@
4222689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4223689b5837SMatthew G. Knepley 
4224689b5837SMatthew G. Knepley   Not collective
4225689b5837SMatthew G. Knepley 
4226689b5837SMatthew G. Knepley   Input Parameter:
4227689b5837SMatthew G. Knepley . dm - The DM
4228689b5837SMatthew G. Knepley 
4229689b5837SMatthew G. Knepley   Output Parameter:
4230689b5837SMatthew G. Knepley . Nf - The number of fields
4231689b5837SMatthew G. Knepley 
4232689b5837SMatthew G. Knepley   Level: intermediate
4233689b5837SMatthew G. Knepley 
4234689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4235689b5837SMatthew G. Knepley @*/
42360f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
42370f21e855SMatthew G. Knepley {
42380f21e855SMatthew G. Knepley   PetscFunctionBegin;
42390f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424044a7f3ddSMatthew G. Knepley   PetscValidPointer(numFields, 2);
424144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4242af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4243af122d2aSMatthew G Knepley }
4244af122d2aSMatthew G Knepley 
4245689b5837SMatthew G. Knepley /*@
4246689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4247689b5837SMatthew G. Knepley 
4248689b5837SMatthew G. Knepley   Logically collective on DM
4249689b5837SMatthew G. Knepley 
4250689b5837SMatthew G. Knepley   Input Parameters:
4251689b5837SMatthew G. Knepley + dm - The DM
4252689b5837SMatthew G. Knepley - Nf - The number of fields
4253689b5837SMatthew G. Knepley 
4254689b5837SMatthew G. Knepley   Level: intermediate
4255689b5837SMatthew G. Knepley 
4256689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4257689b5837SMatthew G. Knepley @*/
4258af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4259af122d2aSMatthew G Knepley {
42600f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4261af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4262af122d2aSMatthew G Knepley 
4263af122d2aSMatthew G Knepley   PetscFunctionBegin;
4264af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
426544a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
42660f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
42670f21e855SMatthew G. Knepley     PetscContainer obj;
42680f21e855SMatthew G. Knepley 
42690f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
427044a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
42710f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4272af122d2aSMatthew G Knepley   }
4273af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4274af122d2aSMatthew G Knepley }
4275af122d2aSMatthew G Knepley 
4276c1929be8SMatthew G. Knepley /*@
4277c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4278c1929be8SMatthew G. Knepley 
4279c1929be8SMatthew G. Knepley   Not collective
4280c1929be8SMatthew G. Knepley 
4281c1929be8SMatthew G. Knepley   Input Parameters:
4282c1929be8SMatthew G. Knepley + dm - The DM
4283c1929be8SMatthew G. Knepley - f  - The field number
4284c1929be8SMatthew G. Knepley 
428544a7f3ddSMatthew G. Knepley   Output Parameters:
428644a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
428744a7f3ddSMatthew G. Knepley - field - The discretization object
4288c1929be8SMatthew G. Knepley 
428944a7f3ddSMatthew G. Knepley   Level: intermediate
4290c1929be8SMatthew G. Knepley 
429144a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4292c1929be8SMatthew G. Knepley @*/
429344a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4294af122d2aSMatthew G Knepley {
4295af122d2aSMatthew G Knepley   PetscFunctionBegin;
4296af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429744a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
429844a7f3ddSMatthew G. Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
429944a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
430044a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4301decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4302decb47aaSMatthew G. Knepley }
4303decb47aaSMatthew G. Knepley 
4304c1929be8SMatthew G. Knepley /*@
4305c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4306c1929be8SMatthew G. Knepley 
4307c1929be8SMatthew G. Knepley   Logically collective on DM
4308c1929be8SMatthew G. Knepley 
4309c1929be8SMatthew G. Knepley   Input Parameters:
4310c1929be8SMatthew G. Knepley + dm    - The DM
4311c1929be8SMatthew G. Knepley . f     - The field number
431244a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4313c1929be8SMatthew G. Knepley - field - The discretization object
4314c1929be8SMatthew G. Knepley 
431544a7f3ddSMatthew G. Knepley   Level: intermediate
4316c1929be8SMatthew G. Knepley 
431744a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4318c1929be8SMatthew G. Knepley @*/
431944a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4320decb47aaSMatthew G. Knepley {
4321decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4322decb47aaSMatthew G. Knepley 
4323decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4324decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4325e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
432644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4327e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
432844a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
432944a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
433044a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
433144a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
433244a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
433344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
433444a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
43352df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
433644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
433744a7f3ddSMatthew G. Knepley }
433844a7f3ddSMatthew G. Knepley 
433944a7f3ddSMatthew G. Knepley /*@
434044a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
434144a7f3ddSMatthew G. Knepley 
434244a7f3ddSMatthew G. Knepley   Logically collective on DM
434344a7f3ddSMatthew G. Knepley 
434444a7f3ddSMatthew G. Knepley   Input Parameters:
434544a7f3ddSMatthew G. Knepley + dm    - The DM
434644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
434744a7f3ddSMatthew G. Knepley - field - The discretization object
434844a7f3ddSMatthew G. Knepley 
434944a7f3ddSMatthew G. Knepley   Level: intermediate
435044a7f3ddSMatthew G. Knepley 
435144a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
435244a7f3ddSMatthew G. Knepley @*/
435344a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
435444a7f3ddSMatthew G. Knepley {
435544a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
435644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
435744a7f3ddSMatthew G. Knepley 
435844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
435944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
436044a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
436144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
436244a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
436344a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
436444a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
436544a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
436644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
43672df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4368af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4369af122d2aSMatthew G Knepley }
43706636e97aSMatthew G Knepley 
4371e5e52638SMatthew G. Knepley /*@
4372e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4373e5e52638SMatthew G. Knepley 
4374e5e52638SMatthew G. Knepley   Collective on DM
4375e5e52638SMatthew G. Knepley 
4376e5e52638SMatthew G. Knepley   Input Parameter:
4377e5e52638SMatthew G. Knepley . dm - The DM
4378e5e52638SMatthew G. Knepley 
4379e5e52638SMatthew G. Knepley   Output Parameter:
4380e5e52638SMatthew G. Knepley . newdm - The DM
4381e5e52638SMatthew G. Knepley 
4382e5e52638SMatthew G. Knepley   Level: advanced
4383e5e52638SMatthew G. Knepley 
4384e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4385e5e52638SMatthew G. Knepley @*/
4386e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4387e5e52638SMatthew G. Knepley {
4388e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4389e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4390e5e52638SMatthew G. Knepley 
4391e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4392e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4393e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4394e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4395e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4396e5e52638SMatthew G. Knepley     DMLabel     label;
4397e5e52638SMatthew G. Knepley     PetscObject field;
4398e5e52638SMatthew G. Knepley 
4399e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4400e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
4401e5e52638SMatthew G. Knepley   }
4402e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4403e5e52638SMatthew G. Knepley }
4404e5e52638SMatthew G. Knepley 
4405e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4406e5e52638SMatthew G. Knepley {
4407e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4408e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4409e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4410e5e52638SMatthew G. Knepley 
4411e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4412e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4413e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4414e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4415e5e52638SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL;}
4416e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4417e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4418e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4419e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4420e5e52638SMatthew G. Knepley }
4421e5e52638SMatthew G. Knepley 
4422e5e52638SMatthew G. Knepley /*@
4423e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4424e5e52638SMatthew G. Knepley 
4425e5e52638SMatthew G. Knepley   Not collective
4426e5e52638SMatthew G. Knepley 
4427e5e52638SMatthew G. Knepley   Input Parameter:
4428e5e52638SMatthew G. Knepley . dm - The DM
4429e5e52638SMatthew G. Knepley 
4430e5e52638SMatthew G. Knepley   Output Parameter:
4431e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4432e5e52638SMatthew G. Knepley 
4433e5e52638SMatthew G. Knepley   Level: intermediate
4434e5e52638SMatthew G. Knepley 
4435e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4436e5e52638SMatthew G. Knepley @*/
4437e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4438e5e52638SMatthew G. Knepley {
4439e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4440e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4441e5e52638SMatthew G. Knepley   PetscValidPointer(Nds, 2);
4442e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4443e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4444e5e52638SMatthew G. Knepley }
4445e5e52638SMatthew G. Knepley 
4446e5e52638SMatthew G. Knepley /*@
4447e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4448e5e52638SMatthew G. Knepley 
4449e5e52638SMatthew G. Knepley   Logically collective on DM
4450e5e52638SMatthew G. Knepley 
4451e5e52638SMatthew G. Knepley   Input Parameter:
4452e5e52638SMatthew G. Knepley . dm - The DM
4453e5e52638SMatthew G. Knepley 
4454e5e52638SMatthew G. Knepley   Level: intermediate
4455e5e52638SMatthew G. Knepley 
4456e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4457e5e52638SMatthew G. Knepley @*/
4458e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4459e5e52638SMatthew G. Knepley {
4460e5e52638SMatthew G. Knepley   PetscInt       s;
4461e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4462e5e52638SMatthew G. Knepley 
4463e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4464e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4465e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4466e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4467e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4468e5e52638SMatthew G. Knepley   }
4469e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4470e5e52638SMatthew G. Knepley   dm->probs = NULL;
4471e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4472e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4473e5e52638SMatthew G. Knepley }
4474e5e52638SMatthew G. Knepley 
4475e5e52638SMatthew G. Knepley /*@
4476e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4477e5e52638SMatthew G. Knepley 
4478e5e52638SMatthew G. Knepley   Not collective
4479e5e52638SMatthew G. Knepley 
4480e5e52638SMatthew G. Knepley   Input Parameter:
4481e5e52638SMatthew G. Knepley . dm    - The DM
4482e5e52638SMatthew G. Knepley 
4483e5e52638SMatthew G. Knepley   Output Parameter:
4484e5e52638SMatthew G. Knepley . prob - The default PetscDS
4485e5e52638SMatthew G. Knepley 
4486e5e52638SMatthew G. Knepley   Level: intermediate
4487e5e52638SMatthew G. Knepley 
4488e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4489e5e52638SMatthew G. Knepley @*/
4490e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4491e5e52638SMatthew G. Knepley {
4492b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4493b0143b4dSMatthew G. Knepley 
4494e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4495e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4496e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4497b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4498b0143b4dSMatthew G. Knepley     PetscDS ds;
4499b0143b4dSMatthew G. Knepley 
4500b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4501b0143b4dSMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, ds);CHKERRQ(ierr);
4502b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4503b0143b4dSMatthew G. Knepley   }
4504b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4505e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4506e5e52638SMatthew G. Knepley }
4507e5e52638SMatthew G. Knepley 
4508e5e52638SMatthew G. Knepley /*@
4509e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4510e5e52638SMatthew G. Knepley 
4511e5e52638SMatthew G. Knepley   Not collective
4512e5e52638SMatthew G. Knepley 
4513e5e52638SMatthew G. Knepley   Input Parameters:
4514e5e52638SMatthew G. Knepley + dm    - The DM
4515e5e52638SMatthew G. Knepley - point - Cell for the DS
4516e5e52638SMatthew G. Knepley 
4517e5e52638SMatthew G. Knepley   Output Parameter:
4518e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4519e5e52638SMatthew G. Knepley 
4520e5e52638SMatthew G. Knepley   Level: developer
4521e5e52638SMatthew G. Knepley 
4522b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
4523e5e52638SMatthew G. Knepley @*/
4524e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4525e5e52638SMatthew G. Knepley {
4526e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
4527e5e52638SMatthew G. Knepley   PetscInt       s;
4528e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4529e5e52638SMatthew G. Knepley 
4530e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4531e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4532e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
4533e5e52638SMatthew G. Knepley   *prob = NULL;
4534e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4535e5e52638SMatthew G. Knepley     PetscInt val;
4536e5e52638SMatthew G. Knepley 
4537e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
4538e5e52638SMatthew G. Knepley     else {
4539e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
4540e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
4541e5e52638SMatthew G. Knepley     }
4542e5e52638SMatthew G. Knepley   }
4543e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
4544e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4545e5e52638SMatthew G. Knepley }
4546e5e52638SMatthew G. Knepley 
4547e5e52638SMatthew G. Knepley /*@
4548e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
4549e5e52638SMatthew G. Knepley 
4550e5e52638SMatthew G. Knepley   Not collective
4551e5e52638SMatthew G. Knepley 
4552e5e52638SMatthew G. Knepley   Input Parameters:
4553e5e52638SMatthew G. Knepley + dm    - The DM
4554e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
4555e5e52638SMatthew G. Knepley 
4556e5e52638SMatthew G. Knepley   Output Parameter:
4557e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given region
4558e5e52638SMatthew G. Knepley 
4559e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
4560e5e52638SMatthew G. Knepley 
4561e5e52638SMatthew G. Knepley   Level: advanced
4562e5e52638SMatthew G. Knepley 
4563e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4564e5e52638SMatthew G. Knepley @*/
4565e5e52638SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, PetscDS *ds)
4566e5e52638SMatthew G. Knepley {
4567e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
4568e5e52638SMatthew G. Knepley 
4569e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4570e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4571e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4572e5e52638SMatthew G. Knepley   PetscValidPointer(ds, 3);
4573e5e52638SMatthew G. Knepley   *ds = NULL;
4574e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4575e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {*ds = dm->probs[s].ds; PetscFunctionReturn(0);}
4576e5e52638SMatthew G. Knepley   }
45772df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
4578e5e52638SMatthew G. Knepley }
4579e5e52638SMatthew G. Knepley 
4580e5e52638SMatthew G. Knepley /*@
4581e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
4582e5e52638SMatthew G. Knepley 
4583e5e52638SMatthew G. Knepley   Not collective
4584e5e52638SMatthew G. Knepley 
4585e5e52638SMatthew G. Knepley   Input Parameters:
4586e5e52638SMatthew G. Knepley + dm  - The DM
4587e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
4588e5e52638SMatthew G. Knepley 
4589e5e52638SMatthew G. Knepley   Output Parameters:
4590e5e52638SMatthew G. Knepley + label - The region label
4591e5e52638SMatthew G. Knepley - prob  - The PetscDS defined on the given region
4592e5e52638SMatthew G. Knepley 
4593e5e52638SMatthew G. Knepley   Level: advanced
4594e5e52638SMatthew G. Knepley 
4595e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4596e5e52638SMatthew G. Knepley @*/
4597e5e52638SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, PetscDS *ds)
4598e5e52638SMatthew G. Knepley {
4599e5e52638SMatthew G. Knepley   PetscInt       Nds;
4600e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4601e5e52638SMatthew G. Knepley 
4602e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4603e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4604e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4605e5e52638SMatthew G. Knepley   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
4606e5e52638SMatthew G. Knepley   if (label) {
4607e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
4608e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
4609e5e52638SMatthew G. Knepley   }
4610e5e52638SMatthew G. Knepley   if (ds) {
4611e5e52638SMatthew G. Knepley     PetscValidPointer(ds, 4);
4612e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
4613e5e52638SMatthew G. Knepley   }
4614e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4615e5e52638SMatthew G. Knepley }
4616e5e52638SMatthew G. Knepley 
4617e5e52638SMatthew G. Knepley /*@
4618e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
4619e5e52638SMatthew G. Knepley 
4620e5e52638SMatthew G. Knepley   Collective on DM
4621e5e52638SMatthew G. Knepley 
4622e5e52638SMatthew G. Knepley   Input Parameters:
4623e5e52638SMatthew G. Knepley + dm    - The DM
4624e5e52638SMatthew G. Knepley . label - The DMLabel defining the mesh region, or NULL for the entire mesh
4625e5e52638SMatthew G. Knepley - prob - The PetscDS defined on the given cell
4626e5e52638SMatthew G. Knepley 
4627e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
4628e5e52638SMatthew G. Knepley 
4629e5e52638SMatthew G. Knepley   Level: advanced
4630e5e52638SMatthew G. Knepley 
4631e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
4632e5e52638SMatthew G. Knepley @*/
4633e5e52638SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, PetscDS ds)
4634e5e52638SMatthew G. Knepley {
4635e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4636e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4637e5e52638SMatthew G. Knepley 
4638e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4639e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4640e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4641e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
4642e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4643e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
4644e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
4645e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
4646e5e52638SMatthew G. Knepley     }
4647e5e52638SMatthew G. Knepley   }
46481b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
4649e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4650e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
4651e5e52638SMatthew G. Knepley   if (!label) {
4652e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
4653e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
4654e5e52638SMatthew G. Knepley     Nds = 0;
4655e5e52638SMatthew G. Knepley   }
4656e5e52638SMatthew G. Knepley   dm->probs[Nds].label = label;
4657e5e52638SMatthew G. Knepley   dm->probs[Nds].ds    = ds;
4658e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4659e5e52638SMatthew G. Knepley }
4660e5e52638SMatthew G. Knepley 
4661e5e52638SMatthew G. Knepley /*@
4662e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
4663e5e52638SMatthew G. Knepley 
4664e5e52638SMatthew G. Knepley   Collective on DM
4665e5e52638SMatthew G. Knepley 
4666e5e52638SMatthew G. Knepley   Input Parameter:
4667e5e52638SMatthew G. Knepley . dm - The DM
4668e5e52638SMatthew G. Knepley 
4669e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
4670e5e52638SMatthew G. Knepley 
4671e5e52638SMatthew G. Knepley   Level: intermediate
4672e5e52638SMatthew G. Knepley 
4673e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
4674e5e52638SMatthew G. Knepley @*/
4675e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
4676e5e52638SMatthew G. Knepley {
4677e5e52638SMatthew G. Knepley   MPI_Comm       comm;
4678e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
46790122748bSMatthew G. Knepley   PetscInt       dimEmbed, f, s, field = 0, fieldh = 0;
4680e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
4681e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4682e5e52638SMatthew G. Knepley 
4683e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4684e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4685e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
4686e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
4687e5e52638SMatthew G. Knepley    1) NULL
4688e5e52638SMatthew G. Knepley    2) Hybrid cells
4689e5e52638SMatthew G. Knepley   */
4690e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
4691e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4692e5e52638SMatthew G. Knepley   /* Create default DS */
4693e5e52638SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, &prob);CHKERRQ(ierr);
46942df9ee95SMatthew G. Knepley   if (!prob) {
46952df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
46962df9ee95SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, prob);CHKERRQ(ierr);
46972df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
46982df9ee95SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, &prob);CHKERRQ(ierr);
46992df9ee95SMatthew G. Knepley   }
4700e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
4701e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
4702e5e52638SMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
4703e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
4704e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
4705e5e52638SMatthew G. Knepley 
4706e5e52638SMatthew G. Knepley     if (label) {
47070122748bSMatthew G. Knepley       DM       plex;
47080122748bSMatthew G. Knepley       PetscInt depth, pMax[4];
47090122748bSMatthew G. Knepley 
47100122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
47110122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
47120122748bSMatthew G. Knepley       ierr = DMPlexGetHybridBounds(plex, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
47130122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
47140122748bSMatthew G. Knepley 
4715e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
4716e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
4717e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
4718e5e52638SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, probh);CHKERRQ(ierr);
4719e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
4720e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
4721e5e52638SMatthew G. Knepley       break;
4722e5e52638SMatthew G. Knepley     }
4723e5e52638SMatthew G. Knepley   }
4724e5e52638SMatthew G. Knepley   /* Set fields in DSes */
4725e5e52638SMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
4726e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
4727e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
4728e5e52638SMatthew G. Knepley 
4729e5e52638SMatthew G. Knepley     if (!label) {
4730e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
4731e5e52638SMatthew G. Knepley       if (probh) {
4732e5e52638SMatthew G. Knepley         PetscFE subfe;
4733e5e52638SMatthew G. Knepley 
4734e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
4735e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
4736e5e52638SMatthew G. Knepley       }
4737e5e52638SMatthew G. Knepley     } else {
4738e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
4739e5e52638SMatthew G. Knepley     }
4740e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
4741e5e52638SMatthew G. Knepley     {
4742e5e52638SMatthew G. Knepley       PetscClassId id;
4743e5e52638SMatthew G. Knepley 
4744e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
4745e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
4746e5e52638SMatthew G. Knepley     }
4747e5e52638SMatthew G. Knepley   }
4748e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
4749e5e52638SMatthew G. Knepley   /* Setup DSes */
4750e5e52638SMatthew G. Knepley   if (doSetup) {
4751e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
4752e5e52638SMatthew G. Knepley   }
4753e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4754e5e52638SMatthew G. Knepley }
4755e5e52638SMatthew G. Knepley 
4756e5e52638SMatthew G. Knepley /*@
4757e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
4758e5e52638SMatthew G. Knepley 
4759e5e52638SMatthew G. Knepley   Collective on DM
4760e5e52638SMatthew G. Knepley 
4761e5e52638SMatthew G. Knepley   Input Parameter:
4762e5e52638SMatthew G. Knepley . dm - The DM
4763e5e52638SMatthew G. Knepley 
4764e5e52638SMatthew G. Knepley   Output Parameter:
4765e5e52638SMatthew G. Knepley . newdm - The DM
4766e5e52638SMatthew G. Knepley 
4767e5e52638SMatthew G. Knepley   Level: advanced
4768e5e52638SMatthew G. Knepley 
4769e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
4770e5e52638SMatthew G. Knepley @*/
4771e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
4772e5e52638SMatthew G. Knepley {
4773e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
4774e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4775e5e52638SMatthew G. Knepley 
4776e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4777e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4778e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4779e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
4780e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4781e5e52638SMatthew G. Knepley     DMLabel label;
4782e5e52638SMatthew G. Knepley     PetscDS ds;
4783e5e52638SMatthew G. Knepley 
4784e5e52638SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &ds);CHKERRQ(ierr);
4785e5e52638SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, ds);CHKERRQ(ierr);
4786e5e52638SMatthew G. Knepley   }
4787e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4788e5e52638SMatthew G. Knepley }
4789e5e52638SMatthew G. Knepley 
4790e5e52638SMatthew G. Knepley /*@
4791e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
4792e5e52638SMatthew G. Knepley 
4793e5e52638SMatthew G. Knepley   Collective on DM
4794e5e52638SMatthew G. Knepley 
4795e5e52638SMatthew G. Knepley   Input Parameter:
4796e5e52638SMatthew G. Knepley . dm - The DM
4797e5e52638SMatthew G. Knepley 
4798e5e52638SMatthew G. Knepley   Output Parameter:
4799e5e52638SMatthew G. Knepley . newdm - The DM
4800e5e52638SMatthew G. Knepley 
4801e5e52638SMatthew G. Knepley   Level: advanced
4802e5e52638SMatthew G. Knepley 
4803e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
4804e5e52638SMatthew G. Knepley @*/
4805e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
4806e5e52638SMatthew G. Knepley {
4807e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4808e5e52638SMatthew G. Knepley 
4809e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4810e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4811e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
4812e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
4813e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4814e5e52638SMatthew G. Knepley }
4815e5e52638SMatthew G. Knepley 
4816b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4817b64e0483SPeter Brune {
4818b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4819b64e0483SPeter Brune   PetscErrorCode ierr;
4820b64e0483SPeter Brune   Vec coords,ccoords;
48216dbf9973SLawrence Mitchell   Mat inject;
4822b64e0483SPeter Brune   PetscFunctionBegin;
4823b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4824b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4825b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4826b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4827b64e0483SPeter Brune   if (coords && !ccoords) {
4828b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
48296668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
48306dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
48312adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
48326dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4833b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4834b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4835b64e0483SPeter Brune   }
4836b64e0483SPeter Brune   PetscFunctionReturn(0);
4837b64e0483SPeter Brune }
4838b64e0483SPeter Brune 
483903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
484003dadc2fSPeter Brune {
484103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
484203dadc2fSPeter Brune   PetscErrorCode ierr;
484303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
484403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
484503dadc2fSPeter Brune   PetscFunctionBegin;
484603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
484703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
484803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
484903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
485003dadc2fSPeter Brune   if (coords && !ccoords) {
485103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
48526668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
485303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
485424640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
485503dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
485603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
485703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
485803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
485903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
486003dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
486103dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
486203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
486303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
486403dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
486503dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
486603dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
486703dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
486803dadc2fSPeter Brune   }
486903dadc2fSPeter Brune   PetscFunctionReturn(0);
487003dadc2fSPeter Brune }
487103dadc2fSPeter Brune 
4872c73cfb54SMatthew G. Knepley /*@
4873c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4874c73cfb54SMatthew G. Knepley 
4875c73cfb54SMatthew G. Knepley   Not collective
4876c73cfb54SMatthew G. Knepley 
4877c73cfb54SMatthew G. Knepley   Input Parameter:
4878c73cfb54SMatthew G. Knepley . dm - The DM
4879c73cfb54SMatthew G. Knepley 
4880c73cfb54SMatthew G. Knepley   Output Parameter:
4881c73cfb54SMatthew G. Knepley . dim - The topological dimension
4882c73cfb54SMatthew G. Knepley 
4883c73cfb54SMatthew G. Knepley   Level: beginner
4884c73cfb54SMatthew G. Knepley 
4885c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4886c73cfb54SMatthew G. Knepley @*/
4887c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4888c73cfb54SMatthew G. Knepley {
4889c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4890c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4891c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4892c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4893c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4894c73cfb54SMatthew G. Knepley }
4895c73cfb54SMatthew G. Knepley 
4896c73cfb54SMatthew G. Knepley /*@
4897c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4898c73cfb54SMatthew G. Knepley 
4899c73cfb54SMatthew G. Knepley   Collective on dm
4900c73cfb54SMatthew G. Knepley 
4901c73cfb54SMatthew G. Knepley   Input Parameters:
4902c73cfb54SMatthew G. Knepley + dm - The DM
4903c73cfb54SMatthew G. Knepley - dim - The topological dimension
4904c73cfb54SMatthew G. Knepley 
4905c73cfb54SMatthew G. Knepley   Level: beginner
4906c73cfb54SMatthew G. Knepley 
4907c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4908c73cfb54SMatthew G. Knepley @*/
4909c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4910c73cfb54SMatthew G. Knepley {
4911e5e52638SMatthew G. Knepley   PetscDS        ds;
4912f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4913f17e8794SMatthew G. Knepley 
4914c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4915c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4916c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4917c73cfb54SMatthew G. Knepley   dm->dim = dim;
4918e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
4919e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
4920c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4921c73cfb54SMatthew G. Knepley }
4922c73cfb54SMatthew G. Knepley 
4923793f3fe5SMatthew G. Knepley /*@
4924793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4925793f3fe5SMatthew G. Knepley 
4926793f3fe5SMatthew G. Knepley   Collective on DM
4927793f3fe5SMatthew G. Knepley 
4928793f3fe5SMatthew G. Knepley   Input Parameters:
4929793f3fe5SMatthew G. Knepley + dm - the DM
4930793f3fe5SMatthew G. Knepley - dim - the dimension
4931793f3fe5SMatthew G. Knepley 
4932793f3fe5SMatthew G. Knepley   Output Parameters:
4933793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4934793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4935793f3fe5SMatthew G. Knepley 
4936793f3fe5SMatthew G. Knepley   Note:
4937793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4938ebfe4b0dSMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
4939793f3fe5SMatthew G. Knepley   then the interval is empty.
4940793f3fe5SMatthew G. Knepley 
4941793f3fe5SMatthew G. Knepley   Level: intermediate
4942793f3fe5SMatthew G. Knepley 
4943793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4944793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4945793f3fe5SMatthew G. Knepley @*/
4946793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4947793f3fe5SMatthew G. Knepley {
4948793f3fe5SMatthew G. Knepley   PetscInt       d;
4949793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4950793f3fe5SMatthew G. Knepley 
4951793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4952793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4953793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4954793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4955793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4956793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4957793f3fe5SMatthew G. Knepley }
4958793f3fe5SMatthew G. Knepley 
49596636e97aSMatthew G Knepley /*@
49606636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
49616636e97aSMatthew G Knepley 
49626636e97aSMatthew G Knepley   Collective on DM
49636636e97aSMatthew G Knepley 
49646636e97aSMatthew G Knepley   Input Parameters:
49656636e97aSMatthew G Knepley + dm - the DM
49666636e97aSMatthew G Knepley - c - coordinate vector
49676636e97aSMatthew G Knepley 
49682dd40e9bSPatrick Sanan   Notes:
49692dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
49702dd40e9bSPatrick Sanan 
49712dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
49726636e97aSMatthew G Knepley 
49736636e97aSMatthew G Knepley   Level: intermediate
49746636e97aSMatthew G Knepley 
49756636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
49762dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
49776636e97aSMatthew G Knepley @*/
49786636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
49796636e97aSMatthew G Knepley {
49806636e97aSMatthew G Knepley   PetscErrorCode ierr;
49816636e97aSMatthew G Knepley 
49826636e97aSMatthew G Knepley   PetscFunctionBegin;
49836636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49846636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
49856636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
49866636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
49876636e97aSMatthew G Knepley   dm->coordinates = c;
49886636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4989b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
499003dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
49916636e97aSMatthew G Knepley   PetscFunctionReturn(0);
49926636e97aSMatthew G Knepley }
49936636e97aSMatthew G Knepley 
49946636e97aSMatthew G Knepley /*@
49956636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
49966636e97aSMatthew G Knepley 
49977058e716SVaclav Hapla   Not collective
49986636e97aSMatthew G Knepley 
49996636e97aSMatthew G Knepley    Input Parameters:
50006636e97aSMatthew G Knepley +  dm - the DM
50016636e97aSMatthew G Knepley -  c - coordinate vector
50026636e97aSMatthew G Knepley 
50032dd40e9bSPatrick Sanan   Notes:
50046636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
50056636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
50066636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
50076636e97aSMatthew G Knepley 
50082dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
50092dd40e9bSPatrick Sanan 
50106636e97aSMatthew G Knepley   Level: intermediate
50116636e97aSMatthew G Knepley 
50126636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
50136636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
50146636e97aSMatthew G Knepley @*/
50156636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
50166636e97aSMatthew G Knepley {
50176636e97aSMatthew G Knepley   PetscErrorCode ierr;
50186636e97aSMatthew G Knepley 
50196636e97aSMatthew G Knepley   PetscFunctionBegin;
50206636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50216636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
50226636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
50236636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
50248865f1eaSKarl Rupp 
50256636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
50268865f1eaSKarl Rupp 
50276636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
50286636e97aSMatthew G Knepley   PetscFunctionReturn(0);
50296636e97aSMatthew G Knepley }
50306636e97aSMatthew G Knepley 
50316636e97aSMatthew G Knepley /*@
50326636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
50336636e97aSMatthew G Knepley 
5034c4a54dccSVaclav Hapla   Collective on DM
50356636e97aSMatthew G Knepley 
50366636e97aSMatthew G Knepley   Input Parameter:
50376636e97aSMatthew G Knepley . dm - the DM
50386636e97aSMatthew G Knepley 
50396636e97aSMatthew G Knepley   Output Parameter:
50406636e97aSMatthew G Knepley . c - global coordinate vector
50416636e97aSMatthew G Knepley 
50426636e97aSMatthew G Knepley   Note:
50436636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
50446636e97aSMatthew G Knepley 
50456636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
50466636e97aSMatthew G Knepley 
50476636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
50486636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
50496636e97aSMatthew G Knepley 
50506636e97aSMatthew G Knepley   Level: intermediate
50516636e97aSMatthew G Knepley 
50526636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
50536636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
50546636e97aSMatthew G Knepley @*/
50556636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
50566636e97aSMatthew G Knepley {
50576636e97aSMatthew G Knepley   PetscErrorCode ierr;
50586636e97aSMatthew G Knepley 
50596636e97aSMatthew G Knepley   PetscFunctionBegin;
50606636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50616636e97aSMatthew G Knepley   PetscValidPointer(c,2);
50621f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
50630298fd71SBarry Smith     DM cdm = NULL;
50646636e97aSMatthew G Knepley 
50656636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
50666636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
50676636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
50686636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
50696636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
50706636e97aSMatthew G Knepley   }
50716636e97aSMatthew G Knepley   *c = dm->coordinates;
50726636e97aSMatthew G Knepley   PetscFunctionReturn(0);
50736636e97aSMatthew G Knepley }
50746636e97aSMatthew G Knepley 
50756636e97aSMatthew G Knepley /*@
507681e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
507781e9a530SVaclav Hapla 
507881e9a530SVaclav Hapla   Collective on DM
507981e9a530SVaclav Hapla 
508081e9a530SVaclav Hapla   Input Parameter:
508181e9a530SVaclav Hapla . dm - the DM
508281e9a530SVaclav Hapla 
508381e9a530SVaclav Hapla   Level: advanced
508481e9a530SVaclav Hapla 
508581e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
508681e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
508781e9a530SVaclav Hapla @*/
508881e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
508981e9a530SVaclav Hapla {
509081e9a530SVaclav Hapla   DM cdm = NULL;
509181e9a530SVaclav Hapla   PetscErrorCode ierr;
509281e9a530SVaclav Hapla 
509381e9a530SVaclav Hapla   PetscFunctionBegin;
509481e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
509581e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
509681e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
509781e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
509881e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
509981e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
510081e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
510181e9a530SVaclav Hapla   }
510281e9a530SVaclav Hapla   PetscFunctionReturn(0);
510381e9a530SVaclav Hapla }
510481e9a530SVaclav Hapla 
510581e9a530SVaclav Hapla /*@
51066636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
51076636e97aSMatthew G Knepley 
51086636e97aSMatthew G Knepley   Collective on DM
51096636e97aSMatthew G Knepley 
51106636e97aSMatthew G Knepley   Input Parameter:
51116636e97aSMatthew G Knepley . dm - the DM
51126636e97aSMatthew G Knepley 
51136636e97aSMatthew G Knepley   Output Parameter:
51146636e97aSMatthew G Knepley . c - coordinate vector
51156636e97aSMatthew G Knepley 
51166636e97aSMatthew G Knepley   Note:
51176636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
51186636e97aSMatthew G Knepley 
51196636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
51206636e97aSMatthew G Knepley 
51216636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
51226636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
51236636e97aSMatthew G Knepley 
51246636e97aSMatthew G Knepley   Level: intermediate
51256636e97aSMatthew G Knepley 
51266636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
512781e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
51286636e97aSMatthew G Knepley @*/
51296636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
51306636e97aSMatthew G Knepley {
51316636e97aSMatthew G Knepley   PetscErrorCode ierr;
51326636e97aSMatthew G Knepley 
51336636e97aSMatthew G Knepley   PetscFunctionBegin;
51346636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51356636e97aSMatthew G Knepley   PetscValidPointer(c,2);
513681e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
51376636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
51386636e97aSMatthew G Knepley   PetscFunctionReturn(0);
51396636e97aSMatthew G Knepley }
51406636e97aSMatthew G Knepley 
514181e9a530SVaclav Hapla /*@
514281e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
514381e9a530SVaclav Hapla 
514481e9a530SVaclav Hapla   Not collective
514581e9a530SVaclav Hapla 
514681e9a530SVaclav Hapla   Input Parameter:
514781e9a530SVaclav Hapla . dm - the DM
514881e9a530SVaclav Hapla 
514981e9a530SVaclav Hapla   Output Parameter:
515081e9a530SVaclav Hapla . c - coordinate vector
515181e9a530SVaclav Hapla 
515281e9a530SVaclav Hapla   Level: advanced
515381e9a530SVaclav Hapla 
515481e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
515581e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
515681e9a530SVaclav Hapla @*/
515781e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
515881e9a530SVaclav Hapla {
515981e9a530SVaclav Hapla   PetscFunctionBegin;
516081e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
516181e9a530SVaclav Hapla   PetscValidPointer(c,2);
516281e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
51636636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
51646636e97aSMatthew G Knepley   PetscFunctionReturn(0);
51656636e97aSMatthew G Knepley }
51666636e97aSMatthew G Knepley 
51672db98f8dSVaclav Hapla /*@
51682db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
51692db98f8dSVaclav Hapla 
51702db98f8dSVaclav Hapla   Not collective
51712db98f8dSVaclav Hapla 
51722db98f8dSVaclav Hapla   Input Parameter:
51732db98f8dSVaclav Hapla + dm - the DM
51742db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
51752db98f8dSVaclav Hapla 
51762db98f8dSVaclav Hapla   Output Parameter:
51772db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
51782db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
51792db98f8dSVaclav Hapla 
51802db98f8dSVaclav Hapla   Note:
51812db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
51822db98f8dSVaclav Hapla 
51832db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
51842db98f8dSVaclav Hapla 
51852db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
51862db98f8dSVaclav Hapla 
51872db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
51882db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
51892db98f8dSVaclav Hapla 
51902db98f8dSVaclav Hapla   Level: advanced
51912db98f8dSVaclav Hapla 
51922db98f8dSVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
51932db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
51942db98f8dSVaclav Hapla @*/
51952db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
51962db98f8dSVaclav Hapla {
51972db98f8dSVaclav Hapla   PetscSection        cs, newcs;
51982db98f8dSVaclav Hapla   Vec                 coords;
51992db98f8dSVaclav Hapla   const PetscScalar   *arr;
52002db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
52012db98f8dSVaclav Hapla   PetscInt            n;
52022db98f8dSVaclav Hapla   PetscErrorCode      ierr;
52032db98f8dSVaclav Hapla 
52042db98f8dSVaclav Hapla   PetscFunctionBegin;
52052db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52062db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
52072db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
52082db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
52092db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
52102db98f8dSVaclav Hapla   if (!dm->coordinateDM || !dm->coordinateDM->defaultSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
52112db98f8dSVaclav Hapla   cs = dm->coordinateDM->defaultSection;
52122db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
52132db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
52142db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
52152db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
52162db98f8dSVaclav Hapla   if (pCoord) {
52172db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
52182db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
52192db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
52202db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5221ad9ac99dSVaclav Hapla   } else {
5222ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
52232db98f8dSVaclav Hapla   }
5224ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5225ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
52262db98f8dSVaclav Hapla   PetscFunctionReturn(0);
52272db98f8dSVaclav Hapla }
52282db98f8dSVaclav Hapla 
5229f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5230f19dbd58SToby Isaac {
5231f19dbd58SToby Isaac   PetscErrorCode ierr;
5232f19dbd58SToby Isaac 
5233f19dbd58SToby Isaac   PetscFunctionBegin;
5234f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5235f19dbd58SToby Isaac   PetscValidPointer(field,2);
5236f19dbd58SToby Isaac   if (!dm->coordinateField) {
5237f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5238f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5239f19dbd58SToby Isaac     }
5240f19dbd58SToby Isaac   }
5241f19dbd58SToby Isaac   *field = dm->coordinateField;
5242f19dbd58SToby Isaac   PetscFunctionReturn(0);
5243f19dbd58SToby Isaac }
5244f19dbd58SToby Isaac 
5245f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5246f19dbd58SToby Isaac {
5247f19dbd58SToby Isaac   PetscErrorCode ierr;
5248f19dbd58SToby Isaac 
5249f19dbd58SToby Isaac   PetscFunctionBegin;
5250f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5251f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5252c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5253f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5254f19dbd58SToby Isaac   dm->coordinateField = field;
5255f19dbd58SToby Isaac   PetscFunctionReturn(0);
5256f19dbd58SToby Isaac }
5257f19dbd58SToby Isaac 
52586636e97aSMatthew G Knepley /*@
52591cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
52606636e97aSMatthew G Knepley 
52616636e97aSMatthew G Knepley   Collective on DM
52626636e97aSMatthew G Knepley 
52636636e97aSMatthew G Knepley   Input Parameter:
52646636e97aSMatthew G Knepley . dm - the DM
52656636e97aSMatthew G Knepley 
52666636e97aSMatthew G Knepley   Output Parameter:
52676636e97aSMatthew G Knepley . cdm - coordinate DM
52686636e97aSMatthew G Knepley 
52696636e97aSMatthew G Knepley   Level: intermediate
52706636e97aSMatthew G Knepley 
52716636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
52721cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
52736636e97aSMatthew G Knepley @*/
52746636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
52756636e97aSMatthew G Knepley {
52766636e97aSMatthew G Knepley   PetscErrorCode ierr;
52776636e97aSMatthew G Knepley 
52786636e97aSMatthew G Knepley   PetscFunctionBegin;
52796636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52806636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
52816636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5282308f8a94SToby Isaac     DM cdm;
5283308f8a94SToby Isaac 
528482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5285308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5286308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5287308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5288308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5289308f8a94SToby Isaac     dm->coordinateDM = cdm;
52906636e97aSMatthew G Knepley   }
52916636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
52926636e97aSMatthew G Knepley   PetscFunctionReturn(0);
52936636e97aSMatthew G Knepley }
5294e87bb0d3SMatthew G Knepley 
52951cfe2091SMatthew G. Knepley /*@
52961cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
52971cfe2091SMatthew G. Knepley 
52981cfe2091SMatthew G. Knepley   Logically Collective on DM
52991cfe2091SMatthew G. Knepley 
53001cfe2091SMatthew G. Knepley   Input Parameters:
53011cfe2091SMatthew G. Knepley + dm - the DM
53021cfe2091SMatthew G. Knepley - cdm - coordinate DM
53031cfe2091SMatthew G. Knepley 
53041cfe2091SMatthew G. Knepley   Level: intermediate
53051cfe2091SMatthew G. Knepley 
53061cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
53071cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
53081cfe2091SMatthew G. Knepley @*/
53091cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
53101cfe2091SMatthew G. Knepley {
53111cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
53121cfe2091SMatthew G. Knepley 
53131cfe2091SMatthew G. Knepley   PetscFunctionBegin;
53141cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53151cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5316f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
53171cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
53181cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
53191cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
53201cfe2091SMatthew G. Knepley }
53211cfe2091SMatthew G. Knepley 
532246e270d4SMatthew G. Knepley /*@
532346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
532446e270d4SMatthew G. Knepley 
532546e270d4SMatthew G. Knepley   Not Collective
532646e270d4SMatthew G. Knepley 
532746e270d4SMatthew G. Knepley   Input Parameter:
532846e270d4SMatthew G. Knepley . dm - The DM object
532946e270d4SMatthew G. Knepley 
533046e270d4SMatthew G. Knepley   Output Parameter:
533146e270d4SMatthew G. Knepley . dim - The embedding dimension
533246e270d4SMatthew G. Knepley 
533346e270d4SMatthew G. Knepley   Level: intermediate
533446e270d4SMatthew G. Knepley 
533546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
5336e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
533746e270d4SMatthew G. Knepley @*/
533846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
533946e270d4SMatthew G. Knepley {
534046e270d4SMatthew G. Knepley   PetscFunctionBegin;
534146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
534246e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
53439a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
53449a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
53459a9a41abSToby Isaac   }
534646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
534746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
534846e270d4SMatthew G. Knepley }
534946e270d4SMatthew G. Knepley 
535046e270d4SMatthew G. Knepley /*@
535146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
535246e270d4SMatthew G. Knepley 
535346e270d4SMatthew G. Knepley   Not Collective
535446e270d4SMatthew G. Knepley 
535546e270d4SMatthew G. Knepley   Input Parameters:
535646e270d4SMatthew G. Knepley + dm  - The DM object
535746e270d4SMatthew G. Knepley - dim - The embedding dimension
535846e270d4SMatthew G. Knepley 
535946e270d4SMatthew G. Knepley   Level: intermediate
536046e270d4SMatthew G. Knepley 
536146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
5362e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
536346e270d4SMatthew G. Knepley @*/
536446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
536546e270d4SMatthew G. Knepley {
5366e5e52638SMatthew G. Knepley   PetscDS        ds;
5367f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5368f17e8794SMatthew G. Knepley 
536946e270d4SMatthew G. Knepley   PetscFunctionBegin;
537046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
537146e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5372e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5373e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
537446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
537546e270d4SMatthew G. Knepley }
537646e270d4SMatthew G. Knepley 
5377e8abe2deSMatthew G. Knepley /*@
5378e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5379e8abe2deSMatthew G. Knepley 
5380116501dfSVaclav Hapla   Collective on DM
5381e8abe2deSMatthew G. Knepley 
5382e8abe2deSMatthew G. Knepley   Input Parameter:
5383e8abe2deSMatthew G. Knepley . dm - The DM object
5384e8abe2deSMatthew G. Knepley 
5385e8abe2deSMatthew G. Knepley   Output Parameter:
5386e8abe2deSMatthew G. Knepley . section - The PetscSection object
5387e8abe2deSMatthew G. Knepley 
5388e8abe2deSMatthew G. Knepley   Level: intermediate
5389e8abe2deSMatthew G. Knepley 
5390e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
5391e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
5392e8abe2deSMatthew G. Knepley @*/
5393e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5394e8abe2deSMatthew G. Knepley {
5395e8abe2deSMatthew G. Knepley   DM             cdm;
5396e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5397e8abe2deSMatthew G. Knepley 
5398e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5399e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5400e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5401e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5402e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
5403e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5404e8abe2deSMatthew G. Knepley }
5405e8abe2deSMatthew G. Knepley 
5406e8abe2deSMatthew G. Knepley /*@
5407e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5408e8abe2deSMatthew G. Knepley 
5409e8abe2deSMatthew G. Knepley   Not Collective
5410e8abe2deSMatthew G. Knepley 
5411e8abe2deSMatthew G. Knepley   Input Parameters:
5412e8abe2deSMatthew G. Knepley + dm      - The DM object
541346e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5414e8abe2deSMatthew G. Knepley - section - The PetscSection object
5415e8abe2deSMatthew G. Knepley 
5416e8abe2deSMatthew G. Knepley   Level: intermediate
5417e8abe2deSMatthew G. Knepley 
5418e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
5419e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
5420e8abe2deSMatthew G. Knepley @*/
542146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5422e8abe2deSMatthew G. Knepley {
5423e8abe2deSMatthew G. Knepley   DM             cdm;
5424e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5425e8abe2deSMatthew G. Knepley 
5426e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5427e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
542846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5429e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5430e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
543146e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
54324c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
543346e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
543446e270d4SMatthew G. Knepley 
543546e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
543646e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
543746e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
543846e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
543946e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
544046e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
544146e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
544246e270d4SMatthew G. Knepley     }
5443ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
544446e270d4SMatthew G. Knepley   }
5445e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5446e8abe2deSMatthew G. Knepley }
5447e8abe2deSMatthew G. Knepley 
54485dc8c3f7SMatthew G. Knepley /*@C
544990b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
54505dc8c3f7SMatthew G. Knepley 
54515dc8c3f7SMatthew G. Knepley   Input Parameters:
545290b157c4SStefano Zampini . dm      - The DM object
545390b157c4SStefano Zampini 
545490b157c4SStefano Zampini   Output Parameters:
545590b157c4SStefano Zampini + per     - Whether the DM is periodic or not
54565dc8c3f7SMatthew 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
54575dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
54585dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
54595dc8c3f7SMatthew G. Knepley 
54605dc8c3f7SMatthew G. Knepley   Level: developer
54615dc8c3f7SMatthew G. Knepley 
54625dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
54635dc8c3f7SMatthew G. Knepley @*/
546490b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5465c6b900c6SMatthew G. Knepley {
5466c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5467c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
546890b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5469c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5470c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
54715dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5472c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5473c6b900c6SMatthew G. Knepley }
5474c6b900c6SMatthew G. Knepley 
54755dc8c3f7SMatthew G. Knepley /*@C
54765dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
54775dc8c3f7SMatthew G. Knepley 
54785dc8c3f7SMatthew G. Knepley   Input Parameters:
54795dc8c3f7SMatthew G. Knepley + dm      - The DM object
548090b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
54815dc8c3f7SMatthew 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
54825dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
54835dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
54845dc8c3f7SMatthew G. Knepley 
54855dc8c3f7SMatthew G. Knepley   Level: developer
54865dc8c3f7SMatthew G. Knepley 
54875dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
54885dc8c3f7SMatthew G. Knepley @*/
548990b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5490c6b900c6SMatthew G. Knepley {
5491c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
5492c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
5493c6b900c6SMatthew G. Knepley 
5494c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5495c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
549690b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
549790b157c4SStefano Zampini   if (maxCell) {
549890b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
549990b157c4SStefano Zampini     PetscValidPointer(L,4);
550090b157c4SStefano Zampini     PetscValidPointer(bd,5);
550190b157c4SStefano Zampini   }
55025dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
55035dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
550490b157c4SStefano Zampini   if (maxCell) {
55055dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
55065dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
550790b157c4SStefano Zampini   }
5508072d7d67SStefano Zampini   dm->periodic = per;
5509c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5510c6b900c6SMatthew G. Knepley }
5511c6b900c6SMatthew G. Knepley 
55122e17dfb7SMatthew G. Knepley /*@
55132e17dfb7SMatthew 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.
55142e17dfb7SMatthew G. Knepley 
55152e17dfb7SMatthew G. Knepley   Input Parameters:
55162e17dfb7SMatthew G. Knepley + dm     - The DM
551765da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
551865da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
55192e17dfb7SMatthew G. Knepley 
55202e17dfb7SMatthew G. Knepley   Output Parameter:
55212e17dfb7SMatthew G. Knepley . out - The localized coordinate point
55222e17dfb7SMatthew G. Knepley 
55232e17dfb7SMatthew G. Knepley   Level: developer
55242e17dfb7SMatthew G. Knepley 
55252e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
55262e17dfb7SMatthew G. Knepley @*/
552765da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
55282e17dfb7SMatthew G. Knepley {
55292e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
55302e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
55312e17dfb7SMatthew G. Knepley 
55322e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
55332e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
55342e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
55352e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
55362e17dfb7SMatthew G. Knepley   } else {
553765da65dcSMatthew G. Knepley     if (endpoint) {
553865da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
5539da3333bfSMatthew 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)) {
5540da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
554165da65dcSMatthew G. Knepley         } else {
5542da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
554365da65dcSMatthew G. Knepley         }
554465da65dcSMatthew G. Knepley       }
554565da65dcSMatthew G. Knepley     } else {
55462e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
55471118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
55482e17dfb7SMatthew G. Knepley       }
55492e17dfb7SMatthew G. Knepley     }
555065da65dcSMatthew G. Knepley   }
55512e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
55522e17dfb7SMatthew G. Knepley }
55532e17dfb7SMatthew G. Knepley 
55542e17dfb7SMatthew G. Knepley /*
55552e17dfb7SMatthew 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.
55562e17dfb7SMatthew G. Knepley 
55572e17dfb7SMatthew G. Knepley   Input Parameters:
55582e17dfb7SMatthew G. Knepley + dm     - The DM
55592e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
55602e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
55612e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
55622e17dfb7SMatthew G. Knepley 
55632e17dfb7SMatthew G. Knepley   Output Parameter:
55642e17dfb7SMatthew G. Knepley . out - The localized coordinate point
55652e17dfb7SMatthew G. Knepley 
55662e17dfb7SMatthew G. Knepley   Level: developer
55672e17dfb7SMatthew G. Knepley 
55682e17dfb7SMatthew 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
55692e17dfb7SMatthew G. Knepley 
55702e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
55712e17dfb7SMatthew G. Knepley */
55722e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
55732e17dfb7SMatthew G. Knepley {
55742e17dfb7SMatthew G. Knepley   PetscInt d;
55752e17dfb7SMatthew G. Knepley 
55762e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
55772e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
55782e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
55792e17dfb7SMatthew G. Knepley   } else {
55802e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5581908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
55822e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
55832e17dfb7SMatthew G. Knepley       } else {
55842e17dfb7SMatthew G. Knepley         out[d] = in[d];
55852e17dfb7SMatthew G. Knepley       }
55862e17dfb7SMatthew G. Knepley     }
55872e17dfb7SMatthew G. Knepley   }
55882e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
55892e17dfb7SMatthew G. Knepley }
55902e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
55912e17dfb7SMatthew G. Knepley {
55922e17dfb7SMatthew G. Knepley   PetscInt d;
55932e17dfb7SMatthew G. Knepley 
55942e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
55952e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
55962e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
55972e17dfb7SMatthew G. Knepley   } else {
55982e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5599908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
56002e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
56012e17dfb7SMatthew G. Knepley       } else {
56022e17dfb7SMatthew G. Knepley         out[d] = in[d];
56032e17dfb7SMatthew G. Knepley       }
56042e17dfb7SMatthew G. Knepley     }
56052e17dfb7SMatthew G. Knepley   }
56062e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
56072e17dfb7SMatthew G. Knepley }
56082e17dfb7SMatthew G. Knepley 
56092e17dfb7SMatthew G. Knepley /*
56102e17dfb7SMatthew 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.
56112e17dfb7SMatthew G. Knepley 
56122e17dfb7SMatthew G. Knepley   Input Parameters:
56132e17dfb7SMatthew G. Knepley + dm     - The DM
56142e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
56152e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
56162e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
56172e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
56182e17dfb7SMatthew G. Knepley 
56192e17dfb7SMatthew G. Knepley   Output Parameter:
56202e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
56212e17dfb7SMatthew G. Knepley 
56222e17dfb7SMatthew G. Knepley   Level: developer
56232e17dfb7SMatthew G. Knepley 
56242e17dfb7SMatthew 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
56252e17dfb7SMatthew G. Knepley 
56262e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
56272e17dfb7SMatthew G. Knepley */
56282e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
56292e17dfb7SMatthew G. Knepley {
56302e17dfb7SMatthew G. Knepley   PetscInt d;
56312e17dfb7SMatthew G. Knepley 
56322e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
56332e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
56342e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
56352e17dfb7SMatthew G. Knepley   } else {
56362e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5637908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
56382e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
56392e17dfb7SMatthew G. Knepley       } else {
56402e17dfb7SMatthew G. Knepley         out[d] += in[d];
56412e17dfb7SMatthew G. Knepley       }
56422e17dfb7SMatthew G. Knepley     }
56432e17dfb7SMatthew G. Knepley   }
56442e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
56452e17dfb7SMatthew G. Knepley }
56462e17dfb7SMatthew G. Knepley 
564736447a5eSToby Isaac /*@
56488f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
56498f700142SStefano Zampini 
56508f700142SStefano Zampini   Not collective
565136447a5eSToby Isaac 
565236447a5eSToby Isaac   Input Parameter:
565336447a5eSToby Isaac . dm - The DM
565436447a5eSToby Isaac 
565536447a5eSToby Isaac   Output Parameter:
565636447a5eSToby Isaac   areLocalized - True if localized
565736447a5eSToby Isaac 
565836447a5eSToby Isaac   Level: developer
565936447a5eSToby Isaac 
56608f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
566136447a5eSToby Isaac @*/
56628f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
566336447a5eSToby Isaac {
566436447a5eSToby Isaac   DM             cdm;
566536447a5eSToby Isaac   PetscSection   coordSection;
566646a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
566746a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
566836447a5eSToby Isaac   PetscErrorCode ierr;
566936447a5eSToby Isaac 
567036447a5eSToby Isaac   PetscFunctionBegin;
567136447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
567246a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
56738b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
567446a3a80fSLisandro Dalcin 
567536447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
567636447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
567736447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
567846a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
567946a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
568046a3a80fSLisandro Dalcin 
568146a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
568236447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
568336447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
568446a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
568546a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
568636447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
568746a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
568836447a5eSToby Isaac   }
56898f700142SStefano Zampini   *areLocalized = alreadyLocalized;
569036447a5eSToby Isaac   PetscFunctionReturn(0);
569136447a5eSToby Isaac }
569236447a5eSToby Isaac 
56938f700142SStefano Zampini /*@
56948f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
56958f700142SStefano Zampini 
56968f700142SStefano Zampini   Collective on dm
56978f700142SStefano Zampini 
56988f700142SStefano Zampini   Input Parameter:
56998f700142SStefano Zampini . dm - The DM
57008f700142SStefano Zampini 
57018f700142SStefano Zampini   Output Parameter:
57028f700142SStefano Zampini   areLocalized - True if localized
57038f700142SStefano Zampini 
57048f700142SStefano Zampini   Level: developer
57058f700142SStefano Zampini 
57068f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
57078f700142SStefano Zampini @*/
57088f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
57098f700142SStefano Zampini {
57108f700142SStefano Zampini   PetscBool      localized;
57118f700142SStefano Zampini   PetscErrorCode ierr;
57128f700142SStefano Zampini 
57138f700142SStefano Zampini   PetscFunctionBegin;
57148f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57158f700142SStefano Zampini   PetscValidPointer(areLocalized, 2);
57168f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
57178f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
57188f700142SStefano Zampini   PetscFunctionReturn(0);
57198f700142SStefano Zampini }
572036447a5eSToby Isaac 
57212e17dfb7SMatthew G. Knepley /*@
5722492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
57232e17dfb7SMatthew G. Knepley 
57248f700142SStefano Zampini   Collective on dm
57258f700142SStefano Zampini 
57262e17dfb7SMatthew G. Knepley   Input Parameter:
57272e17dfb7SMatthew G. Knepley . dm - The DM
57282e17dfb7SMatthew G. Knepley 
57292e17dfb7SMatthew G. Knepley   Level: developer
57302e17dfb7SMatthew G. Knepley 
57318f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
57322e17dfb7SMatthew G. Knepley @*/
57332e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
57342e17dfb7SMatthew G. Knepley {
57352e17dfb7SMatthew G. Knepley   DM             cdm;
57362e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
57372e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
57383e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
57393e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
5740e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
57413e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
57423e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
57432e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
57442e17dfb7SMatthew G. Knepley 
57452e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
57462e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
574792c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
5748f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
5749f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
5750f7cbd40bSStefano Zampini 
57512e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
57522e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
57532e17dfb7SMatthew G. Knepley   {
57542e17dfb7SMatthew G. Knepley     PetscBool isplex;
57552e17dfb7SMatthew G. Knepley 
57562e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
57572e17dfb7SMatthew G. Knepley     if (isplex) {
57582e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
57593e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
576069291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
57613e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
57623e922f36SToby Isaac       newStart = vStart;
57633e922f36SToby Isaac       newEnd   = vEnd;
57643e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
57653e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
57663e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
57673e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
57683e922f36SToby Isaac       }
57692e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
57702e17dfb7SMatthew G. Knepley   }
57712e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
577243eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
57732e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
57743e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
5775e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
57763e922f36SToby Isaac 
57772e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
57782e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
57792e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
57802e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
57813e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
57823e922f36SToby Isaac 
578369291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
57843e922f36SToby Isaac   localized = &anchor[bs];
57853e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
57863e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
57873e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
57883e922f36SToby Isaac 
57893e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
57903e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
57913e922f36SToby Isaac       PetscInt     b;
57923e922f36SToby Isaac 
57933e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
57943e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
57953e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
57963e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
57973e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
57983e922f36SToby Isaac         for (b = 0; b < bs; b++) {
57993e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
58003e922f36SToby Isaac         }
58013e922f36SToby Isaac         if (b < bs) break;
58023e922f36SToby Isaac       }
58033e922f36SToby Isaac       if (d < dof/bs) {
58043e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
58053e922f36SToby Isaac           PetscInt cdof;
58063e922f36SToby Isaac 
58073e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
58083e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
58093e922f36SToby Isaac         }
58103e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
58113e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
58123e922f36SToby Isaac       }
58133e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
58143e922f36SToby Isaac     }
58153e922f36SToby Isaac   }
58163e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
58173e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
581869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
58193e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
582069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
58213e922f36SToby Isaac     PetscFunctionReturn(0);
58223e922f36SToby Isaac   }
58232e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
58242e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
58252e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
58262e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
58272e17dfb7SMatthew G. Knepley   }
58282e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
58292e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5830c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
58312e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
58322e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
58332e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
58342e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5835c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
58362e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
58372e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
58382e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
58392e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
58402e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
58412e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
58422e17dfb7SMatthew G. Knepley   }
58433e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
58443e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
58453e922f36SToby Isaac 
58462e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
58472e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
58483e922f36SToby Isaac       PetscInt     b, cdof;
58492e17dfb7SMatthew G. Knepley 
58503e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
58513e922f36SToby Isaac       if (!cdof) continue;
58522e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
58532e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
58542e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
58552e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
58562e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
58572e17dfb7SMatthew G. Knepley     }
58583e922f36SToby Isaac   }
585969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
586069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5861c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
58622e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
58632e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
58642e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
58652e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
58662e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
58672e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
58682e17dfb7SMatthew G. Knepley }
58692e17dfb7SMatthew G. Knepley 
5870e87bb0d3SMatthew G Knepley /*@
58713a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5872e87bb0d3SMatthew G Knepley 
58733a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5874e87bb0d3SMatthew G Knepley 
5875e87bb0d3SMatthew G Knepley   Input Parameters:
5876e87bb0d3SMatthew G Knepley + dm - The DM
58773a93e3b7SToby Isaac . v - The Vec of points
587862a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
58793a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5880e87bb0d3SMatthew G Knepley 
588161e3bb9bSMatthew G Knepley   Output Parameter:
588262a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
588362a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
58843a93e3b7SToby Isaac 
5885e87bb0d3SMatthew G Knepley 
5886e87bb0d3SMatthew G Knepley   Level: developer
588761e3bb9bSMatthew G Knepley 
588862a38674SMatthew G. Knepley   Notes:
58893a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
589062a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
58913a93e3b7SToby Isaac 
58923a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
589362a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
58943a93e3b7SToby Isaac 
58953a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
58963a93e3b7SToby Isaac 
589762a38674SMatthew G. Knepley $    const PetscSFNode *cells;
589862a38674SMatthew G. Knepley $    PetscInt           nFound;
5899a6216909SToby Isaac $    const PetscInt    *found;
590062a38674SMatthew G. Knepley $
5901a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
59023a93e3b7SToby Isaac 
59033a93e3b7SToby 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
59043a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
59053a93e3b7SToby Isaac 
590661e3bb9bSMatthew G Knepley .keywords: point location, mesh
590762a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
590861e3bb9bSMatthew G Knepley @*/
590962a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5910e87bb0d3SMatthew G Knepley {
5911735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5912735aa83eSMatthew G Knepley 
5913e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5914e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5915e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5916e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
59173a93e3b7SToby Isaac   if (*cellSF) {
59183a93e3b7SToby Isaac     PetscMPIInt result;
59193a93e3b7SToby Isaac 
5920e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5921a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
59223a93e3b7SToby 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");
5923e0fc9d1bSMatthew G. Knepley   } else {
59243a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
59253a93e3b7SToby Isaac   }
592647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5927735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
592862a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
592982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
593047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5931e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5932e87bb0d3SMatthew G Knepley }
593314f150ffSMatthew G. Knepley 
5934f4d763aaSMatthew G. Knepley /*@
5935f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5936f4d763aaSMatthew G. Knepley 
59378f700142SStefano Zampini   Collective on dm
59388f700142SStefano Zampini 
5939f4d763aaSMatthew G. Knepley   Input Parameter:
5940f4d763aaSMatthew G. Knepley . dm - The original DM
5941f4d763aaSMatthew G. Knepley 
5942f4d763aaSMatthew G. Knepley   Output Parameter:
5943f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5944f4d763aaSMatthew G. Knepley 
5945f4d763aaSMatthew G. Knepley   Level: intermediate
5946f4d763aaSMatthew G. Knepley 
5947e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
5948f4d763aaSMatthew G. Knepley @*/
594914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
595014f150ffSMatthew G. Knepley {
5951c26acbdeSMatthew G. Knepley   PetscSection   section;
59522d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
595314f150ffSMatthew G. Knepley   PetscErrorCode ierr;
595414f150ffSMatthew G. Knepley 
595514f150ffSMatthew G. Knepley   PetscFunctionBegin;
595614f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
595714f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5958e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5959c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5960127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
59612d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5962c26acbdeSMatthew G. Knepley     *odm = dm;
5963c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5964c26acbdeSMatthew G. Knepley   }
596514f150ffSMatthew G. Knepley   if (!dm->dmBC) {
5966c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
596714f150ffSMatthew G. Knepley     PetscSF      sf;
596814f150ffSMatthew G. Knepley 
596914f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
5970e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
597114f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5972e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
597314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
597414f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
597515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5976e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
597714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
597814f150ffSMatthew G. Knepley   }
597914f150ffSMatthew G. Knepley   *odm = dm->dmBC;
598014f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
598114f150ffSMatthew G. Knepley }
5982f4d763aaSMatthew G. Knepley 
5983f4d763aaSMatthew G. Knepley /*@
5984cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5985f4d763aaSMatthew G. Knepley 
5986f4d763aaSMatthew G. Knepley   Input Parameter:
5987f4d763aaSMatthew G. Knepley . dm - The original DM
5988f4d763aaSMatthew G. Knepley 
5989cdb7a50dSMatthew G. Knepley   Output Parameters:
5990cdb7a50dSMatthew G. Knepley + num - The output sequence number
5991cdb7a50dSMatthew G. Knepley - val - The output sequence value
5992f4d763aaSMatthew G. Knepley 
5993f4d763aaSMatthew G. Knepley   Level: intermediate
5994f4d763aaSMatthew G. Knepley 
5995f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5996f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5997f4d763aaSMatthew G. Knepley 
5998f4d763aaSMatthew G. Knepley .seealso: VecView()
5999f4d763aaSMatthew G. Knepley @*/
6000cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6001f4d763aaSMatthew G. Knepley {
6002f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6003f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6004cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
6005cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
6006f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6007f4d763aaSMatthew G. Knepley }
6008f4d763aaSMatthew G. Knepley 
6009f4d763aaSMatthew G. Knepley /*@
6010cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6011f4d763aaSMatthew G. Knepley 
6012f4d763aaSMatthew G. Knepley   Input Parameters:
6013f4d763aaSMatthew G. Knepley + dm - The original DM
6014cdb7a50dSMatthew G. Knepley . num - The output sequence number
6015cdb7a50dSMatthew G. Knepley - val - The output sequence value
6016f4d763aaSMatthew G. Knepley 
6017f4d763aaSMatthew G. Knepley   Level: intermediate
6018f4d763aaSMatthew G. Knepley 
6019f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6020f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6021f4d763aaSMatthew G. Knepley 
6022f4d763aaSMatthew G. Knepley .seealso: VecView()
6023f4d763aaSMatthew G. Knepley @*/
6024cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6025f4d763aaSMatthew G. Knepley {
6026f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6027f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6028f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6029cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6030cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6031cdb7a50dSMatthew G. Knepley }
6032cdb7a50dSMatthew G. Knepley 
6033cdb7a50dSMatthew G. Knepley /*@C
6034cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6035cdb7a50dSMatthew G. Knepley 
6036cdb7a50dSMatthew G. Knepley   Input Parameters:
6037cdb7a50dSMatthew G. Knepley + dm   - The original DM
6038cdb7a50dSMatthew G. Knepley . name - The sequence name
6039cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6040cdb7a50dSMatthew G. Knepley 
6041cdb7a50dSMatthew G. Knepley   Output Parameter:
6042cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6043cdb7a50dSMatthew G. Knepley 
6044cdb7a50dSMatthew G. Knepley   Level: intermediate
6045cdb7a50dSMatthew G. Knepley 
6046cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6047cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6048cdb7a50dSMatthew G. Knepley 
6049cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6050cdb7a50dSMatthew G. Knepley @*/
6051cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6052cdb7a50dSMatthew G. Knepley {
6053cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6054cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6055cdb7a50dSMatthew G. Knepley 
6056cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6057cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6058cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6059cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
6060cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6061cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6062cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6063cdb7a50dSMatthew G. Knepley     PetscScalar value;
6064cdb7a50dSMatthew G. Knepley 
606539d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
60664aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6067cdb7a50dSMatthew G. Knepley #endif
6068cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6069f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6070f4d763aaSMatthew G. Knepley }
60718e4ac7eaSMatthew G. Knepley 
60728e4ac7eaSMatthew G. Knepley /*@
60738e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
60748e4ac7eaSMatthew G. Knepley 
60758e4ac7eaSMatthew G. Knepley   Not collective
60768e4ac7eaSMatthew G. Knepley 
60778e4ac7eaSMatthew G. Knepley   Input Parameter:
60788e4ac7eaSMatthew G. Knepley . dm - The DM
60798e4ac7eaSMatthew G. Knepley 
60808e4ac7eaSMatthew G. Knepley   Output Parameter:
60818e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
60828e4ac7eaSMatthew G. Knepley 
60838e4ac7eaSMatthew G. Knepley   Level: beginner
60848e4ac7eaSMatthew G. Knepley 
60858e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
60868e4ac7eaSMatthew G. Knepley @*/
60878e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
60888e4ac7eaSMatthew G. Knepley {
60898e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
60908e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60918e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
60928e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
60938e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
60948e4ac7eaSMatthew G. Knepley }
60958e4ac7eaSMatthew G. Knepley 
60968e4ac7eaSMatthew G. Knepley /*@
60975d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
60988e4ac7eaSMatthew G. Knepley 
60998e4ac7eaSMatthew G. Knepley   Collective on dm
61008e4ac7eaSMatthew G. Knepley 
61018e4ac7eaSMatthew G. Knepley   Input Parameters:
61028e4ac7eaSMatthew G. Knepley + dm - The DM
61038e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
61048e4ac7eaSMatthew G. Knepley 
61055d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
61065d3b26e6SMatthew G. Knepley 
61078e4ac7eaSMatthew G. Knepley   Level: beginner
61088e4ac7eaSMatthew G. Knepley 
61095d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
61108e4ac7eaSMatthew G. Knepley @*/
61118e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
61128e4ac7eaSMatthew G. Knepley {
61138e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
61148e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61158833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
61168e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
61178e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
61188e4ac7eaSMatthew G. Knepley }
6119c58f1c22SToby Isaac 
6120c58f1c22SToby Isaac 
6121c58f1c22SToby Isaac /*@C
6122c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6123c58f1c22SToby Isaac 
6124c58f1c22SToby Isaac   Not Collective
6125c58f1c22SToby Isaac 
6126c58f1c22SToby Isaac   Input Parameters:
6127c58f1c22SToby Isaac + dm   - The DM object
6128c58f1c22SToby Isaac - name - The label name
6129c58f1c22SToby Isaac 
6130c58f1c22SToby Isaac   Level: intermediate
6131c58f1c22SToby Isaac 
6132c58f1c22SToby Isaac .keywords: mesh
6133c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6134c58f1c22SToby Isaac @*/
6135c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6136c58f1c22SToby Isaac {
6137c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
6138c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
6139d67d17b1SMatthew G. Knepley   const char    *lname;
6140c58f1c22SToby Isaac   PetscErrorCode ierr;
6141c58f1c22SToby Isaac 
6142c58f1c22SToby Isaac   PetscFunctionBegin;
6143c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6144c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6145c58f1c22SToby Isaac   while (next) {
6146d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6147d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6148c58f1c22SToby Isaac     if (flg) break;
6149c58f1c22SToby Isaac     next = next->next;
6150c58f1c22SToby Isaac   }
6151c58f1c22SToby Isaac   if (!flg) {
6152c58f1c22SToby Isaac     DMLabelLink tmpLabel;
6153c58f1c22SToby Isaac 
6154c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6155d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
6156c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
6157c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
6158c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
6159c58f1c22SToby Isaac   }
6160c58f1c22SToby Isaac   PetscFunctionReturn(0);
6161c58f1c22SToby Isaac }
6162c58f1c22SToby Isaac 
6163c58f1c22SToby Isaac /*@C
6164c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6165c58f1c22SToby Isaac 
6166c58f1c22SToby Isaac   Not Collective
6167c58f1c22SToby Isaac 
6168c58f1c22SToby Isaac   Input Parameters:
6169c58f1c22SToby Isaac + dm   - The DM object
6170c58f1c22SToby Isaac . name - The label name
6171c58f1c22SToby Isaac - point - The mesh point
6172c58f1c22SToby Isaac 
6173c58f1c22SToby Isaac   Output Parameter:
6174c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6175c58f1c22SToby Isaac 
6176c58f1c22SToby Isaac   Level: beginner
6177c58f1c22SToby Isaac 
6178c58f1c22SToby Isaac .keywords: mesh
6179c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6180c58f1c22SToby Isaac @*/
6181c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6182c58f1c22SToby Isaac {
6183c58f1c22SToby Isaac   DMLabel        label;
6184c58f1c22SToby Isaac   PetscErrorCode ierr;
6185c58f1c22SToby Isaac 
6186c58f1c22SToby Isaac   PetscFunctionBegin;
6187c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6188c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6189c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
619013903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6191c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6192c58f1c22SToby Isaac   PetscFunctionReturn(0);
6193c58f1c22SToby Isaac }
6194c58f1c22SToby Isaac 
6195c58f1c22SToby Isaac /*@C
6196c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6197c58f1c22SToby Isaac 
6198c58f1c22SToby Isaac   Not Collective
6199c58f1c22SToby Isaac 
6200c58f1c22SToby Isaac   Input Parameters:
6201c58f1c22SToby Isaac + dm   - The DM object
6202c58f1c22SToby Isaac . name - The label name
6203c58f1c22SToby Isaac . point - The mesh point
6204c58f1c22SToby Isaac - value - The label value for this point
6205c58f1c22SToby Isaac 
6206c58f1c22SToby Isaac   Output Parameter:
6207c58f1c22SToby Isaac 
6208c58f1c22SToby Isaac   Level: beginner
6209c58f1c22SToby Isaac 
6210c58f1c22SToby Isaac .keywords: mesh
6211c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6212c58f1c22SToby Isaac @*/
6213c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6214c58f1c22SToby Isaac {
6215c58f1c22SToby Isaac   DMLabel        label;
6216c58f1c22SToby Isaac   PetscErrorCode ierr;
6217c58f1c22SToby Isaac 
6218c58f1c22SToby Isaac   PetscFunctionBegin;
6219c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6220c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6221c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6222c58f1c22SToby Isaac   if (!label) {
6223c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6224c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6225c58f1c22SToby Isaac   }
6226c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6227c58f1c22SToby Isaac   PetscFunctionReturn(0);
6228c58f1c22SToby Isaac }
6229c58f1c22SToby Isaac 
6230c58f1c22SToby Isaac /*@C
6231c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6232c58f1c22SToby Isaac 
6233c58f1c22SToby Isaac   Not Collective
6234c58f1c22SToby Isaac 
6235c58f1c22SToby Isaac   Input Parameters:
6236c58f1c22SToby Isaac + dm   - The DM object
6237c58f1c22SToby Isaac . name - The label name
6238c58f1c22SToby Isaac . point - The mesh point
6239c58f1c22SToby Isaac - value - The label value for this point
6240c58f1c22SToby Isaac 
6241c58f1c22SToby Isaac   Output Parameter:
6242c58f1c22SToby Isaac 
6243c58f1c22SToby Isaac   Level: beginner
6244c58f1c22SToby Isaac 
6245c58f1c22SToby Isaac .keywords: mesh
6246c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6247c58f1c22SToby Isaac @*/
6248c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6249c58f1c22SToby Isaac {
6250c58f1c22SToby Isaac   DMLabel        label;
6251c58f1c22SToby Isaac   PetscErrorCode ierr;
6252c58f1c22SToby Isaac 
6253c58f1c22SToby Isaac   PetscFunctionBegin;
6254c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6255c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6256c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6257c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6258c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6259c58f1c22SToby Isaac   PetscFunctionReturn(0);
6260c58f1c22SToby Isaac }
6261c58f1c22SToby Isaac 
6262c58f1c22SToby Isaac /*@C
6263c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6264c58f1c22SToby Isaac 
6265c58f1c22SToby Isaac   Not Collective
6266c58f1c22SToby Isaac 
6267c58f1c22SToby Isaac   Input Parameters:
6268c58f1c22SToby Isaac + dm   - The DM object
6269c58f1c22SToby Isaac - name - The label name
6270c58f1c22SToby Isaac 
6271c58f1c22SToby Isaac   Output Parameter:
6272c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6273c58f1c22SToby Isaac 
6274c58f1c22SToby Isaac   Level: beginner
6275c58f1c22SToby Isaac 
6276c58f1c22SToby Isaac .keywords: mesh
6277df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6278c58f1c22SToby Isaac @*/
6279c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6280c58f1c22SToby Isaac {
6281c58f1c22SToby Isaac   DMLabel        label;
6282c58f1c22SToby Isaac   PetscErrorCode ierr;
6283c58f1c22SToby Isaac 
6284c58f1c22SToby Isaac   PetscFunctionBegin;
6285c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6286c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6287c58f1c22SToby Isaac   PetscValidPointer(size, 3);
6288c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6289c58f1c22SToby Isaac   *size = 0;
6290c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6291c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6292c58f1c22SToby Isaac   PetscFunctionReturn(0);
6293c58f1c22SToby Isaac }
6294c58f1c22SToby Isaac 
6295c58f1c22SToby Isaac /*@C
6296c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6297c58f1c22SToby Isaac 
6298c58f1c22SToby Isaac   Not Collective
6299c58f1c22SToby Isaac 
6300c58f1c22SToby Isaac   Input Parameters:
6301c58f1c22SToby Isaac + mesh - The DM object
6302c58f1c22SToby Isaac - name - The label name
6303c58f1c22SToby Isaac 
6304c58f1c22SToby Isaac   Output Parameter:
6305c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6306c58f1c22SToby Isaac 
6307c58f1c22SToby Isaac   Level: beginner
6308c58f1c22SToby Isaac 
6309c58f1c22SToby Isaac .keywords: mesh
6310c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6311c58f1c22SToby Isaac @*/
6312c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6313c58f1c22SToby Isaac {
6314c58f1c22SToby Isaac   DMLabel        label;
6315c58f1c22SToby Isaac   PetscErrorCode ierr;
6316c58f1c22SToby Isaac 
6317c58f1c22SToby Isaac   PetscFunctionBegin;
6318c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6319c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6320c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6321c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6322c58f1c22SToby Isaac   *ids = NULL;
6323dab2e251SBlaise Bourdin  if (label) {
6324c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6325dab2e251SBlaise Bourdin   } else {
6326dab2e251SBlaise Bourdin     /* returning an empty IS */
6327dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6328dab2e251SBlaise Bourdin   }
6329c58f1c22SToby Isaac   PetscFunctionReturn(0);
6330c58f1c22SToby Isaac }
6331c58f1c22SToby Isaac 
6332c58f1c22SToby Isaac /*@C
6333c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6334c58f1c22SToby Isaac 
6335c58f1c22SToby Isaac   Not Collective
6336c58f1c22SToby Isaac 
6337c58f1c22SToby Isaac   Input Parameters:
6338c58f1c22SToby Isaac + dm - The DM object
6339c58f1c22SToby Isaac . name - The label name
6340c58f1c22SToby Isaac - value - The stratum value
6341c58f1c22SToby Isaac 
6342c58f1c22SToby Isaac   Output Parameter:
6343c58f1c22SToby Isaac . size - The stratum size
6344c58f1c22SToby Isaac 
6345c58f1c22SToby Isaac   Level: beginner
6346c58f1c22SToby Isaac 
6347c58f1c22SToby Isaac .keywords: mesh
6348c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6349c58f1c22SToby Isaac @*/
6350c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6351c58f1c22SToby Isaac {
6352c58f1c22SToby Isaac   DMLabel        label;
6353c58f1c22SToby Isaac   PetscErrorCode ierr;
6354c58f1c22SToby Isaac 
6355c58f1c22SToby Isaac   PetscFunctionBegin;
6356c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6357c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6358c58f1c22SToby Isaac   PetscValidPointer(size, 4);
6359c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6360c58f1c22SToby Isaac   *size = 0;
6361c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6362c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6363c58f1c22SToby Isaac   PetscFunctionReturn(0);
6364c58f1c22SToby Isaac }
6365c58f1c22SToby Isaac 
6366c58f1c22SToby Isaac /*@C
6367c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6368c58f1c22SToby Isaac 
6369c58f1c22SToby Isaac   Not Collective
6370c58f1c22SToby Isaac 
6371c58f1c22SToby Isaac   Input Parameters:
6372c58f1c22SToby Isaac + dm - The DM object
6373c58f1c22SToby Isaac . name - The label name
6374c58f1c22SToby Isaac - value - The stratum value
6375c58f1c22SToby Isaac 
6376c58f1c22SToby Isaac   Output Parameter:
6377c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6378c58f1c22SToby Isaac 
6379c58f1c22SToby Isaac   Level: beginner
6380c58f1c22SToby Isaac 
6381c58f1c22SToby Isaac .keywords: mesh
6382c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6383c58f1c22SToby Isaac @*/
6384c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6385c58f1c22SToby Isaac {
6386c58f1c22SToby Isaac   DMLabel        label;
6387c58f1c22SToby Isaac   PetscErrorCode ierr;
6388c58f1c22SToby Isaac 
6389c58f1c22SToby Isaac   PetscFunctionBegin;
6390c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6391c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6392c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6393c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6394c58f1c22SToby Isaac   *points = NULL;
6395c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6396c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6397c58f1c22SToby Isaac   PetscFunctionReturn(0);
6398c58f1c22SToby Isaac }
6399c58f1c22SToby Isaac 
64004de306b1SToby Isaac /*@C
64019044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
64024de306b1SToby Isaac 
64034de306b1SToby Isaac   Not Collective
64044de306b1SToby Isaac 
64054de306b1SToby Isaac   Input Parameters:
64064de306b1SToby Isaac + dm - The DM object
64074de306b1SToby Isaac . name - The label name
64084de306b1SToby Isaac . value - The stratum value
64094de306b1SToby Isaac - points - The stratum points
64104de306b1SToby Isaac 
64114de306b1SToby Isaac   Level: beginner
64124de306b1SToby Isaac 
64134de306b1SToby Isaac .keywords: mesh
64144de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
64154de306b1SToby Isaac @*/
64164de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
64174de306b1SToby Isaac {
64184de306b1SToby Isaac   DMLabel        label;
64194de306b1SToby Isaac   PetscErrorCode ierr;
64204de306b1SToby Isaac 
64214de306b1SToby Isaac   PetscFunctionBegin;
64224de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64234de306b1SToby Isaac   PetscValidCharPointer(name, 2);
64244de306b1SToby Isaac   PetscValidPointer(points, 4);
64254de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
64264de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
64274de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
64284de306b1SToby Isaac   PetscFunctionReturn(0);
64294de306b1SToby Isaac }
64304de306b1SToby Isaac 
6431c58f1c22SToby Isaac /*@C
6432c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6433c58f1c22SToby Isaac 
6434c58f1c22SToby Isaac   Not Collective
6435c58f1c22SToby Isaac 
6436c58f1c22SToby Isaac   Input Parameters:
6437c58f1c22SToby Isaac + dm   - The DM object
6438c58f1c22SToby Isaac . name - The label name
6439c58f1c22SToby Isaac - value - The label value for this point
6440c58f1c22SToby Isaac 
6441c58f1c22SToby Isaac   Output Parameter:
6442c58f1c22SToby Isaac 
6443c58f1c22SToby Isaac   Level: beginner
6444c58f1c22SToby Isaac 
6445c58f1c22SToby Isaac .keywords: mesh
6446c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6447c58f1c22SToby Isaac @*/
6448c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6449c58f1c22SToby Isaac {
6450c58f1c22SToby Isaac   DMLabel        label;
6451c58f1c22SToby Isaac   PetscErrorCode ierr;
6452c58f1c22SToby Isaac 
6453c58f1c22SToby Isaac   PetscFunctionBegin;
6454c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6455c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6456c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6457c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6458c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6459c58f1c22SToby Isaac   PetscFunctionReturn(0);
6460c58f1c22SToby Isaac }
6461c58f1c22SToby Isaac 
6462c58f1c22SToby Isaac /*@
6463c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6464c58f1c22SToby Isaac 
6465c58f1c22SToby Isaac   Not Collective
6466c58f1c22SToby Isaac 
6467c58f1c22SToby Isaac   Input Parameter:
6468c58f1c22SToby Isaac . dm   - The DM object
6469c58f1c22SToby Isaac 
6470c58f1c22SToby Isaac   Output Parameter:
6471c58f1c22SToby Isaac . numLabels - the number of Labels
6472c58f1c22SToby Isaac 
6473c58f1c22SToby Isaac   Level: intermediate
6474c58f1c22SToby Isaac 
6475c58f1c22SToby Isaac .keywords: mesh
6476c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6477c58f1c22SToby Isaac @*/
6478c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6479c58f1c22SToby Isaac {
6480c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6481c58f1c22SToby Isaac   PetscInt  n    = 0;
6482c58f1c22SToby Isaac 
6483c58f1c22SToby Isaac   PetscFunctionBegin;
6484c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6485c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
6486c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6487c58f1c22SToby Isaac   *numLabels = n;
6488c58f1c22SToby Isaac   PetscFunctionReturn(0);
6489c58f1c22SToby Isaac }
6490c58f1c22SToby Isaac 
6491c58f1c22SToby Isaac /*@C
6492c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6493c58f1c22SToby Isaac 
6494c58f1c22SToby Isaac   Not Collective
6495c58f1c22SToby Isaac 
6496c58f1c22SToby Isaac   Input Parameters:
6497c58f1c22SToby Isaac + dm - The DM object
6498c58f1c22SToby Isaac - n  - the label number
6499c58f1c22SToby Isaac 
6500c58f1c22SToby Isaac   Output Parameter:
6501c58f1c22SToby Isaac . name - the label name
6502c58f1c22SToby Isaac 
6503c58f1c22SToby Isaac   Level: intermediate
6504c58f1c22SToby Isaac 
6505c58f1c22SToby Isaac .keywords: mesh
6506c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6507c58f1c22SToby Isaac @*/
6508c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6509c58f1c22SToby Isaac {
6510c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6511c58f1c22SToby Isaac   PetscInt       l    = 0;
6512d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
6513c58f1c22SToby Isaac 
6514c58f1c22SToby Isaac   PetscFunctionBegin;
6515c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6516c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6517c58f1c22SToby Isaac   while (next) {
6518c58f1c22SToby Isaac     if (l == n) {
6519d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6520c58f1c22SToby Isaac       PetscFunctionReturn(0);
6521c58f1c22SToby Isaac     }
6522c58f1c22SToby Isaac     ++l;
6523c58f1c22SToby Isaac     next = next->next;
6524c58f1c22SToby Isaac   }
6525c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6526c58f1c22SToby Isaac }
6527c58f1c22SToby Isaac 
6528c58f1c22SToby Isaac /*@C
6529c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6530c58f1c22SToby Isaac 
6531c58f1c22SToby Isaac   Not Collective
6532c58f1c22SToby Isaac 
6533c58f1c22SToby Isaac   Input Parameters:
6534c58f1c22SToby Isaac + dm   - The DM object
6535c58f1c22SToby Isaac - name - The label name
6536c58f1c22SToby Isaac 
6537c58f1c22SToby Isaac   Output Parameter:
6538c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6539c58f1c22SToby Isaac 
6540c58f1c22SToby Isaac   Level: intermediate
6541c58f1c22SToby Isaac 
6542c58f1c22SToby Isaac .keywords: mesh
6543c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6544c58f1c22SToby Isaac @*/
6545c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6546c58f1c22SToby Isaac {
6547c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6548d67d17b1SMatthew G. Knepley   const char    *lname;
6549c58f1c22SToby Isaac   PetscErrorCode ierr;
6550c58f1c22SToby Isaac 
6551c58f1c22SToby Isaac   PetscFunctionBegin;
6552c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6553c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6554c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
6555c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6556c58f1c22SToby Isaac   while (next) {
6557d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6558d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
6559c58f1c22SToby Isaac     if (*hasLabel) break;
6560c58f1c22SToby Isaac     next = next->next;
6561c58f1c22SToby Isaac   }
6562c58f1c22SToby Isaac   PetscFunctionReturn(0);
6563c58f1c22SToby Isaac }
6564c58f1c22SToby Isaac 
6565c58f1c22SToby Isaac /*@C
6566c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6567c58f1c22SToby Isaac 
6568c58f1c22SToby Isaac   Not Collective
6569c58f1c22SToby Isaac 
6570c58f1c22SToby Isaac   Input Parameters:
6571c58f1c22SToby Isaac + dm   - The DM object
6572c58f1c22SToby Isaac - name - The label name
6573c58f1c22SToby Isaac 
6574c58f1c22SToby Isaac   Output Parameter:
6575c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6576c58f1c22SToby Isaac 
6577c58f1c22SToby Isaac   Level: intermediate
6578c58f1c22SToby Isaac 
6579c58f1c22SToby Isaac .keywords: mesh
6580c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6581c58f1c22SToby Isaac @*/
6582c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6583c58f1c22SToby Isaac {
6584c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6585c58f1c22SToby Isaac   PetscBool      hasLabel;
6586d67d17b1SMatthew G. Knepley   const char    *lname;
6587c58f1c22SToby Isaac   PetscErrorCode ierr;
6588c58f1c22SToby Isaac 
6589c58f1c22SToby Isaac   PetscFunctionBegin;
6590c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6591c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6592c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6593c58f1c22SToby Isaac   *label = NULL;
6594c58f1c22SToby Isaac   while (next) {
6595d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6596d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6597c58f1c22SToby Isaac     if (hasLabel) {
6598c58f1c22SToby Isaac       *label = next->label;
6599c58f1c22SToby Isaac       break;
6600c58f1c22SToby Isaac     }
6601c58f1c22SToby Isaac     next = next->next;
6602c58f1c22SToby Isaac   }
6603c58f1c22SToby Isaac   PetscFunctionReturn(0);
6604c58f1c22SToby Isaac }
6605c58f1c22SToby Isaac 
6606c58f1c22SToby Isaac /*@C
6607c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6608c58f1c22SToby Isaac 
6609c58f1c22SToby Isaac   Not Collective
6610c58f1c22SToby Isaac 
6611c58f1c22SToby Isaac   Input Parameters:
6612c58f1c22SToby Isaac + dm - The DM object
6613c58f1c22SToby Isaac - n  - the label number
6614c58f1c22SToby Isaac 
6615c58f1c22SToby Isaac   Output Parameter:
6616c58f1c22SToby Isaac . label - the label
6617c58f1c22SToby Isaac 
6618c58f1c22SToby Isaac   Level: intermediate
6619c58f1c22SToby Isaac 
6620c58f1c22SToby Isaac .keywords: mesh
6621c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6622c58f1c22SToby Isaac @*/
6623c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6624c58f1c22SToby Isaac {
6625c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6626c58f1c22SToby Isaac   PetscInt    l    = 0;
6627c58f1c22SToby Isaac 
6628c58f1c22SToby Isaac   PetscFunctionBegin;
6629c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6630c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6631c58f1c22SToby Isaac   while (next) {
6632c58f1c22SToby Isaac     if (l == n) {
6633c58f1c22SToby Isaac       *label = next->label;
6634c58f1c22SToby Isaac       PetscFunctionReturn(0);
6635c58f1c22SToby Isaac     }
6636c58f1c22SToby Isaac     ++l;
6637c58f1c22SToby Isaac     next = next->next;
6638c58f1c22SToby Isaac   }
6639c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6640c58f1c22SToby Isaac }
6641c58f1c22SToby Isaac 
6642c58f1c22SToby Isaac /*@C
6643c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
6644c58f1c22SToby Isaac 
6645c58f1c22SToby Isaac   Not Collective
6646c58f1c22SToby Isaac 
6647c58f1c22SToby Isaac   Input Parameters:
6648c58f1c22SToby Isaac + dm   - The DM object
6649c58f1c22SToby Isaac - label - The DMLabel
6650c58f1c22SToby Isaac 
6651c58f1c22SToby Isaac   Level: developer
6652c58f1c22SToby Isaac 
6653c58f1c22SToby Isaac .keywords: mesh
6654c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6655c58f1c22SToby Isaac @*/
6656c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6657c58f1c22SToby Isaac {
6658c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
6659c58f1c22SToby Isaac   PetscBool      hasLabel;
6660d67d17b1SMatthew G. Knepley   const char    *lname;
6661c58f1c22SToby Isaac   PetscErrorCode ierr;
6662c58f1c22SToby Isaac 
6663c58f1c22SToby Isaac   PetscFunctionBegin;
6664c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6665d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
6666d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
6667d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
6668c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6669c58f1c22SToby Isaac   tmpLabel->label  = label;
6670c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
6671c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
6672c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
6673c58f1c22SToby Isaac   PetscFunctionReturn(0);
6674c58f1c22SToby Isaac }
6675c58f1c22SToby Isaac 
6676c58f1c22SToby Isaac /*@C
6677c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
6678c58f1c22SToby Isaac 
6679c58f1c22SToby Isaac   Not Collective
6680c58f1c22SToby Isaac 
6681c58f1c22SToby Isaac   Input Parameters:
6682c58f1c22SToby Isaac + dm   - The DM object
6683c58f1c22SToby Isaac - name - The label name
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac   Output Parameter:
6686c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6687c58f1c22SToby Isaac 
6688c58f1c22SToby Isaac   Level: developer
6689c58f1c22SToby Isaac 
6690c58f1c22SToby Isaac .keywords: mesh
6691c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6692c58f1c22SToby Isaac @*/
6693c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
6694c58f1c22SToby Isaac {
6695c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6696c58f1c22SToby Isaac   DMLabelLink    last = NULL;
6697c58f1c22SToby Isaac   PetscBool      hasLabel;
6698d67d17b1SMatthew G. Knepley   const char    *lname;
6699c58f1c22SToby Isaac   PetscErrorCode ierr;
6700c58f1c22SToby Isaac 
6701c58f1c22SToby Isaac   PetscFunctionBegin;
6702c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6703c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
6704c58f1c22SToby Isaac   *label = NULL;
6705c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
6706c58f1c22SToby Isaac   while (next) {
6707d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6708d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6709c58f1c22SToby Isaac     if (hasLabel) {
6710c58f1c22SToby Isaac       if (last) last->next       = next->next;
6711c58f1c22SToby Isaac       else      dm->labels->next = next->next;
6712c58f1c22SToby Isaac       next->next = NULL;
6713c58f1c22SToby Isaac       *label     = next->label;
6714c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
6715c58f1c22SToby Isaac       if (hasLabel) {
6716c58f1c22SToby Isaac         dm->depthLabel = NULL;
6717c58f1c22SToby Isaac       }
6718c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
6719c58f1c22SToby Isaac       break;
6720c58f1c22SToby Isaac     }
6721c58f1c22SToby Isaac     last = next;
6722c58f1c22SToby Isaac     next = next->next;
6723c58f1c22SToby Isaac   }
6724c58f1c22SToby Isaac   PetscFunctionReturn(0);
6725c58f1c22SToby Isaac }
6726c58f1c22SToby Isaac 
6727c58f1c22SToby Isaac /*@C
6728c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6729c58f1c22SToby Isaac 
6730c58f1c22SToby Isaac   Not Collective
6731c58f1c22SToby Isaac 
6732c58f1c22SToby Isaac   Input Parameters:
6733c58f1c22SToby Isaac + dm   - The DM object
6734c58f1c22SToby Isaac - name - The label name
6735c58f1c22SToby Isaac 
6736c58f1c22SToby Isaac   Output Parameter:
6737c58f1c22SToby Isaac . output - The flag for output
6738c58f1c22SToby Isaac 
6739c58f1c22SToby Isaac   Level: developer
6740c58f1c22SToby Isaac 
6741c58f1c22SToby Isaac .keywords: mesh
6742c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6743c58f1c22SToby Isaac @*/
6744c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6745c58f1c22SToby Isaac {
6746c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6747d67d17b1SMatthew G. Knepley   const char    *lname;
6748c58f1c22SToby Isaac   PetscErrorCode ierr;
6749c58f1c22SToby Isaac 
6750c58f1c22SToby Isaac   PetscFunctionBegin;
6751c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6752c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6753c58f1c22SToby Isaac   PetscValidPointer(output, 3);
6754c58f1c22SToby Isaac   while (next) {
6755c58f1c22SToby Isaac     PetscBool flg;
6756c58f1c22SToby Isaac 
6757d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6758d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6759c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6760c58f1c22SToby Isaac     next = next->next;
6761c58f1c22SToby Isaac   }
6762c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6763c58f1c22SToby Isaac }
6764c58f1c22SToby Isaac 
6765c58f1c22SToby Isaac /*@C
6766c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
6767c58f1c22SToby Isaac 
6768c58f1c22SToby Isaac   Not Collective
6769c58f1c22SToby Isaac 
6770c58f1c22SToby Isaac   Input Parameters:
6771c58f1c22SToby Isaac + dm     - The DM object
6772c58f1c22SToby Isaac . name   - The label name
6773c58f1c22SToby Isaac - output - The flag for output
6774c58f1c22SToby Isaac 
6775c58f1c22SToby Isaac   Level: developer
6776c58f1c22SToby Isaac 
6777c58f1c22SToby Isaac .keywords: mesh
6778c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6779c58f1c22SToby Isaac @*/
6780c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
6781c58f1c22SToby Isaac {
6782c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6783d67d17b1SMatthew G. Knepley   const char    *lname;
6784c58f1c22SToby Isaac   PetscErrorCode ierr;
6785c58f1c22SToby Isaac 
6786c58f1c22SToby Isaac   PetscFunctionBegin;
6787c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6788c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6789c58f1c22SToby Isaac   while (next) {
6790c58f1c22SToby Isaac     PetscBool flg;
6791c58f1c22SToby Isaac 
6792d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6793d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6794c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
6795c58f1c22SToby Isaac     next = next->next;
6796c58f1c22SToby Isaac   }
6797c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6798c58f1c22SToby Isaac }
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac 
6801c58f1c22SToby Isaac /*@
6802c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
6803c58f1c22SToby Isaac 
6804c58f1c22SToby Isaac   Collective on DM
6805c58f1c22SToby Isaac 
6806c58f1c22SToby Isaac   Input Parameter:
68072e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
6808c58f1c22SToby Isaac 
6809c58f1c22SToby Isaac   Output Parameter:
68102e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
6811c58f1c22SToby Isaac 
6812c58f1c22SToby Isaac   Level: intermediate
6813c58f1c22SToby Isaac 
6814c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
6815c58f1c22SToby Isaac 
6816c58f1c22SToby Isaac .keywords: mesh
6817367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
6818c58f1c22SToby Isaac @*/
6819c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
6820c58f1c22SToby Isaac {
6821c58f1c22SToby Isaac   PetscInt       numLabels, l;
6822c58f1c22SToby Isaac   PetscErrorCode ierr;
6823c58f1c22SToby Isaac 
6824c58f1c22SToby Isaac   PetscFunctionBegin;
6825c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
6826c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
6827c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
6828c58f1c22SToby Isaac     DMLabel     label, labelNew;
6829c58f1c22SToby Isaac     const char *name;
6830c58f1c22SToby Isaac     PetscBool   flg;
6831c58f1c22SToby Isaac 
6832c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
6833c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
6834c58f1c22SToby Isaac     if (flg) continue;
68357d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
68367d5acc75SStefano Zampini     if (flg) continue;
6837c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
6838c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
6839c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
6840c58f1c22SToby Isaac   }
6841c58f1c22SToby Isaac   PetscFunctionReturn(0);
6842c58f1c22SToby Isaac }
6843a8fb8f29SToby Isaac 
6844a8fb8f29SToby Isaac /*@
6845a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
6846a8fb8f29SToby Isaac 
6847a8fb8f29SToby Isaac   Input Parameter:
6848a8fb8f29SToby Isaac . dm - The DM object
6849a8fb8f29SToby Isaac 
6850a8fb8f29SToby Isaac   Output Parameter:
6851a8fb8f29SToby Isaac . cdm - The coarse DM
6852a8fb8f29SToby Isaac 
6853a8fb8f29SToby Isaac   Level: intermediate
6854a8fb8f29SToby Isaac 
6855a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
6856a8fb8f29SToby Isaac @*/
6857a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6858a8fb8f29SToby Isaac {
6859a8fb8f29SToby Isaac   PetscFunctionBegin;
6860a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6861a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
6862a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
6863a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6864a8fb8f29SToby Isaac }
6865a8fb8f29SToby Isaac 
6866a8fb8f29SToby Isaac /*@
6867a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6868a8fb8f29SToby Isaac 
6869a8fb8f29SToby Isaac   Input Parameters:
6870a8fb8f29SToby Isaac + dm - The DM object
6871a8fb8f29SToby Isaac - cdm - The coarse DM
6872a8fb8f29SToby Isaac 
6873a8fb8f29SToby Isaac   Level: intermediate
6874a8fb8f29SToby Isaac 
6875a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
6876a8fb8f29SToby Isaac @*/
6877a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6878a8fb8f29SToby Isaac {
6879a8fb8f29SToby Isaac   PetscErrorCode ierr;
6880a8fb8f29SToby Isaac 
6881a8fb8f29SToby Isaac   PetscFunctionBegin;
6882a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6883a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6884a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6885a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6886a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6887a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6888a8fb8f29SToby Isaac }
6889a8fb8f29SToby Isaac 
689088bdff64SToby Isaac /*@
689188bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
689288bdff64SToby Isaac 
689388bdff64SToby Isaac   Input Parameter:
689488bdff64SToby Isaac . dm - The DM object
689588bdff64SToby Isaac 
689688bdff64SToby Isaac   Output Parameter:
689788bdff64SToby Isaac . fdm - The fine DM
689888bdff64SToby Isaac 
689988bdff64SToby Isaac   Level: intermediate
690088bdff64SToby Isaac 
690188bdff64SToby Isaac .seealso: DMSetFineDM()
690288bdff64SToby Isaac @*/
690388bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
690488bdff64SToby Isaac {
690588bdff64SToby Isaac   PetscFunctionBegin;
690688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
690788bdff64SToby Isaac   PetscValidPointer(fdm, 2);
690888bdff64SToby Isaac   *fdm = dm->fineMesh;
690988bdff64SToby Isaac   PetscFunctionReturn(0);
691088bdff64SToby Isaac }
691188bdff64SToby Isaac 
691288bdff64SToby Isaac /*@
691388bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
691488bdff64SToby Isaac 
691588bdff64SToby Isaac   Input Parameters:
691688bdff64SToby Isaac + dm - The DM object
691788bdff64SToby Isaac - fdm - The fine DM
691888bdff64SToby Isaac 
691988bdff64SToby Isaac   Level: intermediate
692088bdff64SToby Isaac 
692188bdff64SToby Isaac .seealso: DMGetFineDM()
692288bdff64SToby Isaac @*/
692388bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
692488bdff64SToby Isaac {
692588bdff64SToby Isaac   PetscErrorCode ierr;
692688bdff64SToby Isaac 
692788bdff64SToby Isaac   PetscFunctionBegin;
692888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
692988bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
693088bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
693188bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
693288bdff64SToby Isaac   dm->fineMesh = fdm;
693388bdff64SToby Isaac   PetscFunctionReturn(0);
693488bdff64SToby Isaac }
693588bdff64SToby Isaac 
6936a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6937a6ba4734SToby Isaac 
6938a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6939a6ba4734SToby Isaac {
6940e5e52638SMatthew G. Knepley   PetscInt       d;
6941a6ba4734SToby Isaac   PetscErrorCode ierr;
6942a6ba4734SToby Isaac 
6943a6ba4734SToby Isaac   PetscFunctionBegin;
6944e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
6945e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
6946e5e52638SMatthew G. Knepley   }
6947a6ba4734SToby Isaac   PetscFunctionReturn(0);
6948a6ba4734SToby Isaac }
6949a6ba4734SToby Isaac 
6950a6ba4734SToby Isaac /*@C
6951a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6952a6ba4734SToby Isaac 
6953a6ba4734SToby Isaac   Input Parameters:
69544c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6955f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6956a6ba4734SToby Isaac . name        - The BC name
6957a6ba4734SToby Isaac . labelname   - The label defining constrained points
6958a6ba4734SToby Isaac . field       - The field to constrain
6959e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
6960a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6961a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6962a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6963a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6964a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6965a6ba4734SToby Isaac 
6966a6ba4734SToby Isaac   Options Database Keys:
6967a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6968a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6969a6ba4734SToby Isaac 
6970a6ba4734SToby Isaac   Level: developer
6971a6ba4734SToby Isaac 
6972a6ba4734SToby Isaac .seealso: DMGetBoundary()
6973a6ba4734SToby Isaac @*/
6974a30ec4eaSSatish 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)
6975a6ba4734SToby Isaac {
6976e5e52638SMatthew G. Knepley   PetscDS        ds;
6977a6ba4734SToby Isaac   PetscErrorCode ierr;
6978a6ba4734SToby Isaac 
6979a6ba4734SToby Isaac   PetscFunctionBegin;
6980a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6981e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6982e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
6983a6ba4734SToby Isaac   PetscFunctionReturn(0);
6984a6ba4734SToby Isaac }
6985a6ba4734SToby Isaac 
6986a6ba4734SToby Isaac /*@
6987a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6988a6ba4734SToby Isaac 
6989a6ba4734SToby Isaac   Input Parameters:
6990a6ba4734SToby Isaac . dm - The mesh object
6991a6ba4734SToby Isaac 
6992a6ba4734SToby Isaac   Output Parameters:
6993a6ba4734SToby Isaac . numBd - The number of BC
6994a6ba4734SToby Isaac 
6995a6ba4734SToby Isaac   Level: intermediate
6996a6ba4734SToby Isaac 
6997a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6998a6ba4734SToby Isaac @*/
6999a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7000a6ba4734SToby Isaac {
7001e5e52638SMatthew G. Knepley   PetscDS        ds;
700258ebd649SToby Isaac   PetscErrorCode ierr;
7003a6ba4734SToby Isaac 
7004a6ba4734SToby Isaac   PetscFunctionBegin;
7005a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7006e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7007e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7008a6ba4734SToby Isaac   PetscFunctionReturn(0);
7009a6ba4734SToby Isaac }
7010a6ba4734SToby Isaac 
7011a6ba4734SToby Isaac /*@C
70121c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7013a6ba4734SToby Isaac 
7014a6ba4734SToby Isaac   Input Parameters:
7015a6ba4734SToby Isaac + dm          - The mesh object
7016a6ba4734SToby Isaac - bd          - The BC number
7017a6ba4734SToby Isaac 
7018a6ba4734SToby Isaac   Output Parameters:
7019f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7020a6ba4734SToby Isaac . name        - The BC name
7021a6ba4734SToby Isaac . labelname   - The label defining constrained points
7022a6ba4734SToby Isaac . field       - The field to constrain
7023a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7024a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7025a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7026a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7027a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7028a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7029a6ba4734SToby Isaac 
7030a6ba4734SToby Isaac   Options Database Keys:
7031a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7032a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7033a6ba4734SToby Isaac 
7034a6ba4734SToby Isaac   Level: developer
7035a6ba4734SToby Isaac 
7036a6ba4734SToby Isaac .seealso: DMAddBoundary()
7037a6ba4734SToby Isaac @*/
7038a30ec4eaSSatish 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)
7039a6ba4734SToby Isaac {
7040e5e52638SMatthew G. Knepley   PetscDS        ds;
704158ebd649SToby Isaac   PetscErrorCode ierr;
7042a6ba4734SToby Isaac 
7043a6ba4734SToby Isaac   PetscFunctionBegin;
7044a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7045e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7046e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7047a6ba4734SToby Isaac   PetscFunctionReturn(0);
7048a6ba4734SToby Isaac }
7049a6ba4734SToby Isaac 
7050e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7051e6f8dbb6SToby Isaac {
7052e5e52638SMatthew G. Knepley   PetscDS        ds;
7053dff059c6SToby Isaac   DMBoundary    *lastnext;
7054e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7055e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7056e6f8dbb6SToby Isaac 
7057e6f8dbb6SToby Isaac   PetscFunctionBegin;
7058e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7059e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
706047a1f5adSToby Isaac   if (dm->boundary) {
706147a1f5adSToby Isaac     DMBoundary next = dm->boundary;
706247a1f5adSToby Isaac 
706347a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
706447a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
706547a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
706647a1f5adSToby Isaac     while (next) {
706747a1f5adSToby Isaac       DMBoundary b = next;
706847a1f5adSToby Isaac 
706947a1f5adSToby Isaac       next = b->next;
707047a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7071a6ba4734SToby Isaac     }
707247a1f5adSToby Isaac     dm->boundary = NULL;
7073a6ba4734SToby Isaac   }
707447a1f5adSToby Isaac 
7075dff059c6SToby Isaac   lastnext = &(dm->boundary);
7076e6f8dbb6SToby Isaac   while (dsbound) {
7077e6f8dbb6SToby Isaac     DMBoundary dmbound;
7078e6f8dbb6SToby Isaac 
7079e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7080e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7081e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7082994fe344SLisandro 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);}
708347a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7084dff059c6SToby Isaac     *lastnext = dmbound;
7085dff059c6SToby Isaac     lastnext = &(dmbound->next);
7086dff059c6SToby Isaac     dsbound = dsbound->next;
7087a6ba4734SToby Isaac   }
7088a6ba4734SToby Isaac   PetscFunctionReturn(0);
7089a6ba4734SToby Isaac }
7090a6ba4734SToby Isaac 
7091a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7092a6ba4734SToby Isaac {
7093b95f2879SToby Isaac   DMBoundary     b;
7094a6ba4734SToby Isaac   PetscErrorCode ierr;
7095a6ba4734SToby Isaac 
7096a6ba4734SToby Isaac   PetscFunctionBegin;
7097a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7098a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
7099a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7100e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7101b95f2879SToby Isaac   b = dm->boundary;
7102a6ba4734SToby Isaac   while (b && !(*isBd)) {
7103e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7104e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
71053424c85cSToby Isaac 
71063424c85cSToby Isaac     if (label) {
7107a6ba4734SToby Isaac       PetscInt i;
7108a6ba4734SToby Isaac 
7109e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7110e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7111a6ba4734SToby Isaac       }
7112a6ba4734SToby Isaac     }
7113a6ba4734SToby Isaac     b = b->next;
7114a6ba4734SToby Isaac   }
7115a6ba4734SToby Isaac   PetscFunctionReturn(0);
7116a6ba4734SToby Isaac }
71174d6f44ffSToby Isaac 
71184d6f44ffSToby Isaac /*@C
71194d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
71204d6f44ffSToby Isaac 
71214d6f44ffSToby Isaac   Input Parameters:
71224d6f44ffSToby Isaac + dm      - The DM
71230709b2feSToby Isaac . time    - The time
71244d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
71254d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
71264d6f44ffSToby Isaac - mode    - The insertion mode for values
71274d6f44ffSToby Isaac 
71284d6f44ffSToby Isaac   Output Parameter:
71294d6f44ffSToby Isaac . X - vector
71304d6f44ffSToby Isaac 
71314d6f44ffSToby Isaac    Calling sequence of func:
71320709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
71334d6f44ffSToby Isaac 
71344d6f44ffSToby Isaac +  dim - The spatial dimension
71354d6f44ffSToby Isaac .  x   - The coordinates
71364d6f44ffSToby Isaac .  Nf  - The number of fields
71374d6f44ffSToby Isaac .  u   - The output field values
71384d6f44ffSToby Isaac -  ctx - optional user-defined function context
71394d6f44ffSToby Isaac 
71404d6f44ffSToby Isaac   Level: developer
71414d6f44ffSToby Isaac 
71422716604bSToby Isaac .seealso: DMComputeL2Diff()
71434d6f44ffSToby Isaac @*/
71440709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
71454d6f44ffSToby Isaac {
71464d6f44ffSToby Isaac   Vec            localX;
71474d6f44ffSToby Isaac   PetscErrorCode ierr;
71484d6f44ffSToby Isaac 
71494d6f44ffSToby Isaac   PetscFunctionBegin;
71504d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71514d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
71520709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
71534d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
71544d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
71554d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
71564d6f44ffSToby Isaac   PetscFunctionReturn(0);
71574d6f44ffSToby Isaac }
71584d6f44ffSToby Isaac 
71590709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
71604d6f44ffSToby Isaac {
71614d6f44ffSToby Isaac   PetscErrorCode ierr;
71624d6f44ffSToby Isaac 
71634d6f44ffSToby Isaac   PetscFunctionBegin;
71644d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71654d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
71660918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
71670709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
71684d6f44ffSToby Isaac   PetscFunctionReturn(0);
71694d6f44ffSToby Isaac }
71704d6f44ffSToby Isaac 
71712c53366bSMatthew 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)
71722c53366bSMatthew G. Knepley {
71732c53366bSMatthew G. Knepley   Vec            localX;
71742c53366bSMatthew G. Knepley   PetscErrorCode ierr;
71752c53366bSMatthew G. Knepley 
71762c53366bSMatthew G. Knepley   PetscFunctionBegin;
71772c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71782c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
71792c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
71802c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
71812c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
71822c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
71832c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
71842c53366bSMatthew G. Knepley }
71852c53366bSMatthew G. Knepley 
71861c531cf8SMatthew 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)
71874d6f44ffSToby Isaac {
71884d6f44ffSToby Isaac   PetscErrorCode ierr;
71894d6f44ffSToby Isaac 
71904d6f44ffSToby Isaac   PetscFunctionBegin;
71914d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71924d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
71930918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
71941c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
71954d6f44ffSToby Isaac   PetscFunctionReturn(0);
71964d6f44ffSToby Isaac }
71972716604bSToby Isaac 
71988c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
71998c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
72008c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
72018c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7202191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
72038c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
72048c6c5593SMatthew G. Knepley {
72058c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
72068c6c5593SMatthew G. Knepley 
72078c6c5593SMatthew G. Knepley   PetscFunctionBegin;
72088c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
72098c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
72108c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
72110918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
72128c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
72138c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
72148c6c5593SMatthew G. Knepley }
72158c6c5593SMatthew G. Knepley 
72161c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
72178c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
72188c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
72198c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7220191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
72218c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
72228c6c5593SMatthew G. Knepley {
72238c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
72248c6c5593SMatthew G. Knepley 
72258c6c5593SMatthew G. Knepley   PetscFunctionBegin;
72268c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
72278c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
72288c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
72290918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
72301c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
72318c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
72328c6c5593SMatthew G. Knepley }
72338c6c5593SMatthew G. Knepley 
72342716604bSToby Isaac /*@C
72352716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
72362716604bSToby Isaac 
72372716604bSToby Isaac   Input Parameters:
72382716604bSToby Isaac + dm    - The DM
72390709b2feSToby Isaac . time  - The time
72402716604bSToby Isaac . funcs - The functions to evaluate for each field component
72412716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7242574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
72432716604bSToby Isaac 
72442716604bSToby Isaac   Output Parameter:
72452716604bSToby Isaac . diff - The diff ||u - u_h||_2
72462716604bSToby Isaac 
72472716604bSToby Isaac   Level: developer
72482716604bSToby Isaac 
72491189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
72502716604bSToby Isaac @*/
72510709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
72522716604bSToby Isaac {
72532716604bSToby Isaac   PetscErrorCode ierr;
72542716604bSToby Isaac 
72552716604bSToby Isaac   PetscFunctionBegin;
72562716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7257b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
72580918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
72590709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
72602716604bSToby Isaac   PetscFunctionReturn(0);
72612716604bSToby Isaac }
7262b698f381SToby Isaac 
7263b698f381SToby Isaac /*@C
7264b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7265b698f381SToby Isaac 
7266b698f381SToby Isaac   Input Parameters:
7267b698f381SToby Isaac + dm    - The DM
7268b698f381SToby Isaac , time  - The time
7269b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
7270b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7271574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
7272b698f381SToby Isaac - n     - The vector to project along
7273b698f381SToby Isaac 
7274b698f381SToby Isaac   Output Parameter:
7275b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
7276b698f381SToby Isaac 
7277b698f381SToby Isaac   Level: developer
7278b698f381SToby Isaac 
7279b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
7280b698f381SToby Isaac @*/
7281b698f381SToby 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)
7282b698f381SToby Isaac {
7283b698f381SToby Isaac   PetscErrorCode ierr;
7284b698f381SToby Isaac 
7285b698f381SToby Isaac   PetscFunctionBegin;
7286b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7287b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7288b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7289b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7290b698f381SToby Isaac   PetscFunctionReturn(0);
7291b698f381SToby Isaac }
7292b698f381SToby Isaac 
72932a16baeaSToby Isaac /*@C
72942a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
72952a16baeaSToby Isaac 
72962a16baeaSToby Isaac   Input Parameters:
72972a16baeaSToby Isaac + dm    - The DM
72982a16baeaSToby Isaac . time  - The time
72992a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
73002a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7301574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
73022a16baeaSToby Isaac 
73032a16baeaSToby Isaac   Output Parameter:
73042a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
73052a16baeaSToby Isaac 
73062a16baeaSToby Isaac   Level: developer
73072a16baeaSToby Isaac 
73081189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
73092a16baeaSToby Isaac @*/
73101189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
73112a16baeaSToby Isaac {
73122a16baeaSToby Isaac   PetscErrorCode ierr;
73132a16baeaSToby Isaac 
73142a16baeaSToby Isaac   PetscFunctionBegin;
73152a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
73162a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
73170918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
73182a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
73192a16baeaSToby Isaac   PetscFunctionReturn(0);
73202a16baeaSToby Isaac }
73212a16baeaSToby Isaac 
7322df0b854cSToby Isaac /*@C
7323df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7324cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7325df0b854cSToby Isaac 
7326df0b854cSToby Isaac   Collective on dm
7327df0b854cSToby Isaac 
7328df0b854cSToby Isaac   Input parameters:
7329df0b854cSToby Isaac + dm - the pre-adaptation DM object
7330a1b0c543SToby Isaac - label - label with the flags
7331df0b854cSToby Isaac 
7332df0b854cSToby Isaac   Output parameters:
73330d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7334df0b854cSToby Isaac 
7335df0b854cSToby Isaac   Level: intermediate
73360d1cd5e0SMatthew G. Knepley 
73370d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7338df0b854cSToby Isaac @*/
73390d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7340df0b854cSToby Isaac {
7341df0b854cSToby Isaac   PetscErrorCode ierr;
7342df0b854cSToby Isaac 
7343df0b854cSToby Isaac   PetscFunctionBegin;
7344df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7345a1b0c543SToby Isaac   PetscValidPointer(label,2);
73460d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
73470d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
73486f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
73490d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
73500d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
73510d1cd5e0SMatthew G. Knepley }
73520d1cd5e0SMatthew G. Knepley 
73530d1cd5e0SMatthew G. Knepley /*@C
73540d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
73550d1cd5e0SMatthew G. Knepley 
73560d1cd5e0SMatthew G. Knepley   Input Parameters:
73570d1cd5e0SMatthew G. Knepley + dm - The DM object
73580d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
73596f25b0d8SLisandro 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_".
73600d1cd5e0SMatthew G. Knepley 
73610d1cd5e0SMatthew G. Knepley   Output Parameter:
73620d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
73630d1cd5e0SMatthew G. Knepley 
73640d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
73650d1cd5e0SMatthew G. Knepley 
73660d1cd5e0SMatthew G. Knepley   Level: advanced
73670d1cd5e0SMatthew G. Knepley 
73680d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
73690d1cd5e0SMatthew G. Knepley @*/
73700d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
73710d1cd5e0SMatthew G. Knepley {
73720d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
73730d1cd5e0SMatthew G. Knepley 
73740d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
73750d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73760d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
73770d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
73780d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
73796f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
73806f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
73810d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7382df0b854cSToby Isaac   PetscFunctionReturn(0);
7383df0b854cSToby Isaac }
7384c4088d22SMatthew G. Knepley 
7385502a2867SDave May /*@C
7386502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7387502a2867SDave May 
7388502a2867SDave May  Not Collective
7389502a2867SDave May 
7390502a2867SDave May  Input Parameter:
7391502a2867SDave May  . dm    - The DM
7392502a2867SDave May 
7393502a2867SDave May  Output Parameter:
7394502a2867SDave May  . nranks - the number of neighbours
7395502a2867SDave May  . ranks - the neighbors ranks
7396502a2867SDave May 
7397502a2867SDave May  Notes:
7398502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
7399502a2867SDave May 
7400502a2867SDave May  Level: beginner
7401502a2867SDave May 
7402dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
7403502a2867SDave May @*/
7404502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7405502a2867SDave May {
7406502a2867SDave May   PetscErrorCode ierr;
7407502a2867SDave May 
7408502a2867SDave May   PetscFunctionBegin;
7409502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
74100918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7411502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7412502a2867SDave May   PetscFunctionReturn(0);
7413502a2867SDave May }
7414502a2867SDave May 
7415531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7416531c7667SBarry Smith 
7417531c7667SBarry Smith /*
7418531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7419531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
7420531c7667SBarry Smith */
7421531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7422531c7667SBarry Smith {
7423531c7667SBarry Smith   PetscErrorCode ierr;
7424531c7667SBarry Smith 
7425531c7667SBarry Smith   PetscFunctionBegin;
7426531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7427531c7667SBarry Smith     Vec x1local;
7428531c7667SBarry Smith     DM  dm;
7429531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7430531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7431531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7432531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7433531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7434531c7667SBarry Smith     x1   = x1local;
7435531c7667SBarry Smith   }
7436531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7437531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7438531c7667SBarry Smith     DM  dm;
7439531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7440531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7441531c7667SBarry Smith   }
7442531c7667SBarry Smith   PetscFunctionReturn(0);
7443531c7667SBarry Smith }
7444531c7667SBarry Smith 
7445531c7667SBarry Smith /*@
7446531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7447531c7667SBarry Smith 
7448531c7667SBarry Smith     Input Parameter:
7449531c7667SBarry Smith .    coloring - the MatFDColoring object
7450531c7667SBarry Smith 
745195452b02SPatrick Sanan     Developer Notes:
745295452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
7453531c7667SBarry Smith 
74541b266c99SBarry Smith     Level: advanced
74551b266c99SBarry Smith 
7456531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7457531c7667SBarry Smith @*/
7458531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7459531c7667SBarry Smith {
7460531c7667SBarry Smith   PetscFunctionBegin;
7461531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7462531c7667SBarry Smith   PetscFunctionReturn(0);
7463531c7667SBarry Smith }
74648320bc6fSPatrick Sanan 
74658320bc6fSPatrick Sanan /*@
74668320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
74678320bc6fSPatrick Sanan 
74688320bc6fSPatrick Sanan     Collective
74698320bc6fSPatrick Sanan 
74708320bc6fSPatrick Sanan     Input Parameters:
74718320bc6fSPatrick Sanan +    dm - the first DM
74728320bc6fSPatrick Sanan -    dm2 - the second DM
74738320bc6fSPatrick Sanan 
74748320bc6fSPatrick Sanan     Output Parameters:
74758320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
74768320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
74778320bc6fSPatrick Sanan 
74788320bc6fSPatrick Sanan     Notes:
74798320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
74808320bc6fSPatrick Sanan     of the same topology. This implies that the the section (field data) on one
74818320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
74828320bc6fSPatrick Sanan     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
74838320bc6fSPatrick Sanan     decomposition, with different data.
74848320bc6fSPatrick Sanan 
74858320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
74868320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
74878320bc6fSPatrick Sanan 
74888320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
74898320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
74908320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
74918320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
74928320bc6fSPatrick Sanan 
74938320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
74948320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
74958320bc6fSPatrick Sanan .vb
74968320bc6fSPatrick Sanan   ...
74978320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
74988320bc6fSPatrick Sanan   if (set && compatible)  {
74998320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
75008320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
75018320bc6fSPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
75028320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
75038320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
75048320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
75058320bc6fSPatrick Sanan       }
75068320bc6fSPatrick Sanan     }
75078320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
75088320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
75098320bc6fSPatrick Sanan   } else {
75108320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
75118320bc6fSPatrick Sanan   }
75128320bc6fSPatrick Sanan   ...
75138320bc6fSPatrick Sanan .ve
75148320bc6fSPatrick Sanan 
75158320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
75168320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
75178320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
75188320bc6fSPatrick Sanan     always check the "set" output parameter.
75198320bc6fSPatrick Sanan 
75208320bc6fSPatrick Sanan     A DM is always compatible with itself.
75218320bc6fSPatrick Sanan 
75228320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
75238320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
75248320bc6fSPatrick Sanan     incompatible.
75258320bc6fSPatrick Sanan 
75268320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
75278320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
75288320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
75298320bc6fSPatrick Sanan 
75308320bc6fSPatrick Sanan     Developer Notes:
75318320bc6fSPatrick Sanan     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
75328320bc6fSPatrick Sanan     the DM B is compatible with DM A. Thus, this function checks the implementations
75338320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
75348320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
75358320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
75368320bc6fSPatrick Sanan     logic for this function, to check for existing logic in the implementation
75378320bc6fSPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found; the logic of this
75388320bc6fSPatrick Sanan     function will then call that logic.
75398320bc6fSPatrick Sanan 
75408320bc6fSPatrick Sanan     Level: advanced
75418320bc6fSPatrick Sanan 
75428320bc6fSPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA()
75438320bc6fSPatrick Sanan @*/
75448320bc6fSPatrick Sanan 
75458320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
75468320bc6fSPatrick Sanan {
75478320bc6fSPatrick Sanan   PetscErrorCode ierr;
75488320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
75498320bc6fSPatrick Sanan   DMType         type,type2;
75508320bc6fSPatrick Sanan   PetscBool      sameType;
75518320bc6fSPatrick Sanan 
75528320bc6fSPatrick Sanan   PetscFunctionBegin;
75538320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75548320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
75558320bc6fSPatrick Sanan 
75568320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
75578320bc6fSPatrick Sanan   if (dm == dm2) {
75588320bc6fSPatrick Sanan     *set = PETSC_TRUE;
75598320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
75608320bc6fSPatrick Sanan     PetscFunctionReturn(0);
75618320bc6fSPatrick Sanan   }
75628320bc6fSPatrick Sanan 
75638320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
75648320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
75658320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
75668320bc6fSPatrick Sanan      determined by the implementation-specific logic */
75678320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
75688320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
75698320bc6fSPatrick Sanan     *set = PETSC_TRUE;
75708320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
75718320bc6fSPatrick Sanan     PetscFunctionReturn(0);
75728320bc6fSPatrick Sanan   }
75738320bc6fSPatrick Sanan 
75748320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
75758320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
75768320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
75778320bc6fSPatrick Sanan     if (*set) {
75788320bc6fSPatrick Sanan       PetscFunctionReturn(0);
75798320bc6fSPatrick Sanan     }
75808320bc6fSPatrick Sanan   }
75818320bc6fSPatrick Sanan 
75828320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
75838320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
75848320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
75858320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
75868320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
75878320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
75888320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
75898320bc6fSPatrick Sanan   } else {
75908320bc6fSPatrick Sanan     *set = PETSC_FALSE;
75918320bc6fSPatrick Sanan   }
75928320bc6fSPatrick Sanan   PetscFunctionReturn(0);
75938320bc6fSPatrick Sanan }
7594