xref: /petsc/src/dm/interface/dm.c (revision abe1f95cdb6cf2ce27fea5cdb9dd9c84840ab27c)
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 
31d083f849SBarry Smith   Collective
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;
6434aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6534aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
66c58f1c22SToby Isaac   v->depthLabel               = NULL;
670298fd71SBarry Smith   v->defaultSection           = NULL;
680298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
69fba222abSToby Isaac   v->defaultConstraintSection = NULL;
70fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
71c6b900c6SMatthew G. Knepley   v->L                        = NULL;
72c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
735dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
749a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7596173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
76435a35e8SMatthew G Knepley   {
77435a35e8SMatthew G Knepley     PetscInt i;
78435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
790298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
80f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
81435a35e8SMatthew G Knepley     }
82435a35e8SMatthew G Knepley   }
83e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
84b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
85e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
8614f150ffSMatthew G. Knepley   v->dmBC = NULL;
87a8fb8f29SToby Isaac   v->coarseMesh = NULL;
88f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
89cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
90c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
91b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
92bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
93c58f1c22SToby Isaac   v->labels->refct = 1;
944a7a4c06SLawrence Mitchell 
954a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
964a7a4c06SLawrence Mitchell 
971411c6eeSJed Brown   *dm = v;
98a4121054SBarry Smith   PetscFunctionReturn(0);
99a4121054SBarry Smith }
100a4121054SBarry Smith 
10138221697SMatthew G. Knepley /*@
10238221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10338221697SMatthew G. Knepley 
104d083f849SBarry Smith   Collective
10538221697SMatthew G. Knepley 
10638221697SMatthew G. Knepley   Input Parameter:
10738221697SMatthew G. Knepley . dm - The original DM object
10838221697SMatthew G. Knepley 
10938221697SMatthew G. Knepley   Output Parameter:
11038221697SMatthew G. Knepley . newdm  - The new DM object
11138221697SMatthew G. Knepley 
11238221697SMatthew G. Knepley   Level: beginner
11338221697SMatthew G. Knepley 
11438221697SMatthew G. Knepley @*/
11538221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11638221697SMatthew G. Knepley {
11738221697SMatthew G. Knepley   PetscSF        sf;
11838221697SMatthew G. Knepley   Vec            coords;
11938221697SMatthew G. Knepley   void          *ctx;
120a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12138221697SMatthew G. Knepley   PetscErrorCode ierr;
12238221697SMatthew G. Knepley 
12338221697SMatthew G. Knepley   PetscFunctionBegin;
12438221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12538221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12638221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
127c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
128c58f1c22SToby Isaac   dm->labels->refct++;
129c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
130c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
131ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
132ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1331de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1341de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   if (dm->ops->clone) {
13638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13738221697SMatthew G. Knepley   }
1383f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
143be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
144be4c1c3eSMatthew G. Knepley     DM           ncdm;
145be4c1c3eSMatthew G. Knepley     PetscSection cs;
1465a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
147be4c1c3eSMatthew G. Knepley 
148e87a4003SBarry Smith     ierr = DMGetSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
149be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1505a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1515a0206caSToby Isaac     if (pEndMax >= 0) {
152be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15383bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
154e87a4003SBarry Smith       ierr = DMSetSection(ncdm, cs);CHKERRQ(ierr);
155a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
156be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
157be4c1c3eSMatthew G. Knepley     }
158be4c1c3eSMatthew G. Knepley   }
159a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
160a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
16138221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16238221697SMatthew G. Knepley   if (coords) {
16338221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16438221697SMatthew G. Knepley   } else {
16538221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16638221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16738221697SMatthew G. Knepley   }
16890b157c4SStefano Zampini   {
16990b157c4SStefano Zampini     PetscBool             isper;
170c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1715dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17290b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17390b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
174c6b900c6SMatthew G. Knepley   }
17534aa8a36SMatthew G. Knepley   {
17634aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17734aa8a36SMatthew G. Knepley 
17834aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
18034aa8a36SMatthew G. Knepley   }
18138221697SMatthew G. Knepley   PetscFunctionReturn(0);
18238221697SMatthew G. Knepley }
18338221697SMatthew G. Knepley 
1849a42bb27SBarry Smith /*@C
185564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1869a42bb27SBarry Smith 
187d083f849SBarry Smith    Logically Collective on da
1889a42bb27SBarry Smith 
1899a42bb27SBarry Smith    Input Parameter:
1909a42bb27SBarry Smith +  da - initial distributed array
191e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1929a42bb27SBarry Smith 
1939a42bb27SBarry Smith    Options Database:
194dd85299cSBarry Smith .   -dm_vec_type ctype
1959a42bb27SBarry Smith 
1969a42bb27SBarry Smith    Level: intermediate
1979a42bb27SBarry Smith 
198a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1999a42bb27SBarry Smith @*/
20019fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2019a42bb27SBarry Smith {
2029a42bb27SBarry Smith   PetscErrorCode ierr;
2039a42bb27SBarry Smith 
2049a42bb27SBarry Smith   PetscFunctionBegin;
2059a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2069a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2089a42bb27SBarry Smith   PetscFunctionReturn(0);
2099a42bb27SBarry Smith }
2109a42bb27SBarry Smith 
211c0dedaeaSBarry Smith /*@C
212c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
213c0dedaeaSBarry Smith 
214d083f849SBarry Smith    Logically Collective on da
215c0dedaeaSBarry Smith 
216c0dedaeaSBarry Smith    Input Parameter:
217c0dedaeaSBarry Smith .  da - initial distributed array
218c0dedaeaSBarry Smith 
219c0dedaeaSBarry Smith    Output Parameter:
220c0dedaeaSBarry Smith .  ctype - the vector type
221c0dedaeaSBarry Smith 
222c0dedaeaSBarry Smith    Level: intermediate
223c0dedaeaSBarry Smith 
224a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
225c0dedaeaSBarry Smith @*/
226c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
227c0dedaeaSBarry Smith {
228c0dedaeaSBarry Smith   PetscFunctionBegin;
229c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
230c0dedaeaSBarry Smith   *ctype = da->vectype;
231c0dedaeaSBarry Smith   PetscFunctionReturn(0);
232c0dedaeaSBarry Smith }
233c0dedaeaSBarry Smith 
2345f1ad066SMatthew G Knepley /*@
23534f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley   Not collective
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley   Input Parameter:
2405f1ad066SMatthew G Knepley . v - The Vec
2415f1ad066SMatthew G Knepley 
2425f1ad066SMatthew G Knepley   Output Parameter:
2435f1ad066SMatthew G Knepley . dm - The DM
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley   Level: intermediate
2465f1ad066SMatthew G Knepley 
2475f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2485f1ad066SMatthew G Knepley @*/
2495f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2505f1ad066SMatthew G Knepley {
2515f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley   PetscFunctionBegin;
2545f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2555f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2565f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2575f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2585f1ad066SMatthew G Knepley }
2595f1ad066SMatthew G Knepley 
2605f1ad066SMatthew G Knepley /*@
261d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley   Not collective
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley   Input Parameters:
2665f1ad066SMatthew G Knepley + v - The Vec
2675f1ad066SMatthew G Knepley - dm - The DM
2685f1ad066SMatthew G Knepley 
269d9805387SMatthew 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.
270d9805387SMatthew G. Knepley 
2715f1ad066SMatthew G Knepley   Level: intermediate
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2745f1ad066SMatthew G Knepley @*/
2755f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2765f1ad066SMatthew G Knepley {
2775f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2785f1ad066SMatthew G Knepley 
2795f1ad066SMatthew G Knepley   PetscFunctionBegin;
2805f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
281d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2825f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2835f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2845f1ad066SMatthew G Knepley }
2855f1ad066SMatthew G Knepley 
286521d9a4cSLisandro Dalcin /*@C
2878f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2888f1509bcSBarry Smith 
289d083f849SBarry Smith    Logically Collective on dm
2908f1509bcSBarry Smith 
2918f1509bcSBarry Smith    Input Parameters:
2928f1509bcSBarry Smith +  dm - the DM context
2938f1509bcSBarry Smith -  ctype - the matrix type
2948f1509bcSBarry Smith 
2958f1509bcSBarry Smith    Options Database:
2968f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2978f1509bcSBarry Smith 
2988f1509bcSBarry Smith    Level: intermediate
2998f1509bcSBarry Smith 
3008f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3018f1509bcSBarry Smith           DMGetISColoringType()
3028f1509bcSBarry Smith @*/
3038f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3048f1509bcSBarry Smith {
3058f1509bcSBarry Smith   PetscFunctionBegin;
3068f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3078f1509bcSBarry Smith   dm->coloringtype = ctype;
3088f1509bcSBarry Smith   PetscFunctionReturn(0);
3098f1509bcSBarry Smith }
3108f1509bcSBarry Smith 
3118f1509bcSBarry Smith /*@C
3128f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
313521d9a4cSLisandro Dalcin 
314d083f849SBarry Smith    Logically Collective on dm
315521d9a4cSLisandro Dalcin 
316521d9a4cSLisandro Dalcin    Input Parameter:
3178f1509bcSBarry Smith .  dm - the DM context
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith    Output Parameter:
3208f1509bcSBarry Smith .  ctype - the matrix type
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith    Options Database:
3238f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith    Level: intermediate
3268f1509bcSBarry Smith 
3278f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3288f1509bcSBarry Smith           DMGetISColoringType()
3298f1509bcSBarry Smith @*/
3308f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3318f1509bcSBarry Smith {
3328f1509bcSBarry Smith   PetscFunctionBegin;
3338f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3348f1509bcSBarry Smith   *ctype = dm->coloringtype;
3358f1509bcSBarry Smith   PetscFunctionReturn(0);
3368f1509bcSBarry Smith }
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith /*@C
3398f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3408f1509bcSBarry Smith 
341d083f849SBarry Smith    Logically Collective on dm
3428f1509bcSBarry Smith 
3438f1509bcSBarry Smith    Input Parameters:
344521d9a4cSLisandro Dalcin +  dm - the DM context
345a2b5a043SBarry Smith -  ctype - the matrix type
346521d9a4cSLisandro Dalcin 
347521d9a4cSLisandro Dalcin    Options Database:
348521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
349521d9a4cSLisandro Dalcin 
350521d9a4cSLisandro Dalcin    Level: intermediate
351521d9a4cSLisandro Dalcin 
352a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
353521d9a4cSLisandro Dalcin @*/
35419fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
355521d9a4cSLisandro Dalcin {
356521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35788f0584fSBarry Smith 
358521d9a4cSLisandro Dalcin   PetscFunctionBegin;
359521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
360521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
36119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
362521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
363521d9a4cSLisandro Dalcin }
364521d9a4cSLisandro Dalcin 
365c0dedaeaSBarry Smith /*@C
366c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
367c0dedaeaSBarry Smith 
368d083f849SBarry Smith    Logically Collective on dm
369c0dedaeaSBarry Smith 
370c0dedaeaSBarry Smith    Input Parameter:
371c0dedaeaSBarry Smith .  dm - the DM context
372c0dedaeaSBarry Smith 
373c0dedaeaSBarry Smith    Output Parameter:
374c0dedaeaSBarry Smith .  ctype - the matrix type
375c0dedaeaSBarry Smith 
376c0dedaeaSBarry Smith    Options Database:
377c0dedaeaSBarry Smith .   -dm_mat_type ctype
378c0dedaeaSBarry Smith 
379c0dedaeaSBarry Smith    Level: intermediate
380c0dedaeaSBarry Smith 
381a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
382c0dedaeaSBarry Smith @*/
383c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
384c0dedaeaSBarry Smith {
385c0dedaeaSBarry Smith   PetscFunctionBegin;
386c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
387c0dedaeaSBarry Smith   *ctype = dm->mattype;
388c0dedaeaSBarry Smith   PetscFunctionReturn(0);
389c0dedaeaSBarry Smith }
390c0dedaeaSBarry Smith 
391c688c046SMatthew G Knepley /*@
39234f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
393c688c046SMatthew G Knepley 
394c688c046SMatthew G Knepley   Not collective
395c688c046SMatthew G Knepley 
396c688c046SMatthew G Knepley   Input Parameter:
397c688c046SMatthew G Knepley . A - The Mat
398c688c046SMatthew G Knepley 
399c688c046SMatthew G Knepley   Output Parameter:
400c688c046SMatthew G Knepley . dm - The DM
401c688c046SMatthew G Knepley 
402c688c046SMatthew G Knepley   Level: intermediate
403c688c046SMatthew G Knepley 
4048f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4058f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4068f1509bcSBarry Smith 
407c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
408c688c046SMatthew G Knepley @*/
409c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
410c688c046SMatthew G Knepley {
411c688c046SMatthew G Knepley   PetscErrorCode ierr;
412c688c046SMatthew G Knepley 
413c688c046SMatthew G Knepley   PetscFunctionBegin;
414c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
415c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
416c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
417c688c046SMatthew G Knepley   PetscFunctionReturn(0);
418c688c046SMatthew G Knepley }
419c688c046SMatthew G Knepley 
420c688c046SMatthew G Knepley /*@
421c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Not collective
424c688c046SMatthew G Knepley 
425c688c046SMatthew G Knepley   Input Parameters:
426c688c046SMatthew G Knepley + A - The Mat
427c688c046SMatthew G Knepley - dm - The DM
428c688c046SMatthew G Knepley 
429c688c046SMatthew G Knepley   Level: intermediate
430c688c046SMatthew G Knepley 
4318f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4328f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4338f1509bcSBarry Smith 
4348f1509bcSBarry Smith 
435c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
436c688c046SMatthew G Knepley @*/
437c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
438c688c046SMatthew G Knepley {
439c688c046SMatthew G Knepley   PetscErrorCode ierr;
440c688c046SMatthew G Knepley 
441c688c046SMatthew G Knepley   PetscFunctionBegin;
442c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4438865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
444c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
445c688c046SMatthew G Knepley   PetscFunctionReturn(0);
446c688c046SMatthew G Knepley }
447c688c046SMatthew G Knepley 
4489a42bb27SBarry Smith /*@C
4499a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4506757b960SDave May    DM options in the database.
4519a42bb27SBarry Smith 
452d083f849SBarry Smith    Logically Collective on dm
4539a42bb27SBarry Smith 
4549a42bb27SBarry Smith    Input Parameter:
4558353ddbbSDave May +  da - the DM context
4569a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4579a42bb27SBarry Smith 
4589a42bb27SBarry Smith    Notes:
4599a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4609a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4619a42bb27SBarry Smith 
4629a42bb27SBarry Smith    Level: advanced
4639a42bb27SBarry Smith 
4649a42bb27SBarry Smith .seealso: DMSetFromOptions()
4659a42bb27SBarry Smith @*/
4667087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4679a42bb27SBarry Smith {
4689a42bb27SBarry Smith   PetscErrorCode ierr;
4699a42bb27SBarry Smith 
4709a42bb27SBarry Smith   PetscFunctionBegin;
4719a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4729a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
473691be533SLawrence Mitchell   if (dm->sf) {
474691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
475691be533SLawrence Mitchell   }
476691be533SLawrence Mitchell   if (dm->defaultSF) {
477691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
478691be533SLawrence Mitchell   }
4799a42bb27SBarry Smith   PetscFunctionReturn(0);
4809a42bb27SBarry Smith }
4819a42bb27SBarry Smith 
48231697293SDave May /*@C
48331697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48431697293SDave May    DM options in the database.
48531697293SDave May 
486d083f849SBarry Smith    Logically Collective on dm
48731697293SDave May 
48831697293SDave May    Input Parameters:
48931697293SDave May +  dm - the DM context
49031697293SDave May -  prefix - the prefix string to prepend to all DM option requests
49131697293SDave May 
49231697293SDave May    Notes:
49331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49531697293SDave May 
49631697293SDave May    Level: advanced
49731697293SDave May 
49831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49931697293SDave May @*/
50031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
50131697293SDave May {
50231697293SDave May   PetscErrorCode ierr;
50331697293SDave May 
50431697293SDave May   PetscFunctionBegin;
50531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50731697293SDave May   PetscFunctionReturn(0);
50831697293SDave May }
50931697293SDave May 
51031697293SDave May /*@C
51131697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51231697293SDave May    DM options in the database.
51331697293SDave May 
51431697293SDave May    Not Collective
51531697293SDave May 
51631697293SDave May    Input Parameters:
51731697293SDave May .  dm - the DM context
51831697293SDave May 
51931697293SDave May    Output Parameters:
52031697293SDave May .  prefix - pointer to the prefix string used is returned
52131697293SDave May 
52295452b02SPatrick Sanan    Notes:
52395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52431697293SDave May    sufficient length to hold the prefix.
52531697293SDave May 
52631697293SDave May    Level: advanced
52731697293SDave May 
52831697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52931697293SDave May @*/
53031697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
53131697293SDave May {
53231697293SDave May   PetscErrorCode ierr;
53331697293SDave May 
53431697293SDave May   PetscFunctionBegin;
53531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53631697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53731697293SDave May   PetscFunctionReturn(0);
53831697293SDave May }
53931697293SDave May 
54088bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
54188bdff64SToby Isaac {
54288bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
54388bdff64SToby Isaac   DMNamedVecLink nlink;
54488bdff64SToby Isaac   PetscErrorCode ierr;
54588bdff64SToby Isaac 
54688bdff64SToby Isaac   PetscFunctionBegin;
547aab5bcd8SJed Brown   *ncrefct = 0;
54888bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
54988bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
55088bdff64SToby Isaac     if (dm->localin[i])  refct--;
55188bdff64SToby Isaac     if (dm->globalin[i]) refct--;
55288bdff64SToby Isaac   }
55388bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
55488bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
55588bdff64SToby Isaac   if (dm->x) {
55688bdff64SToby Isaac     DM obj;
55788bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
55888bdff64SToby Isaac     if (obj == dm) refct--;
55988bdff64SToby Isaac   }
56088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56188bdff64SToby Isaac     refct--;
56288bdff64SToby Isaac     if (recurseCoarse) {
56388bdff64SToby Isaac       PetscInt coarseCount;
56488bdff64SToby Isaac 
56588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
56688bdff64SToby Isaac       refct += coarseCount;
56788bdff64SToby Isaac     }
56888bdff64SToby Isaac   }
56988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
57088bdff64SToby Isaac     refct--;
57188bdff64SToby Isaac     if (recurseFine) {
57288bdff64SToby Isaac       PetscInt fineCount;
57388bdff64SToby Isaac 
57488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
57588bdff64SToby Isaac       refct += fineCount;
57688bdff64SToby Isaac     }
57788bdff64SToby Isaac   }
57888bdff64SToby Isaac   *ncrefct = refct;
57988bdff64SToby Isaac   PetscFunctionReturn(0);
58088bdff64SToby Isaac }
58188bdff64SToby Isaac 
582354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
583354557abSToby Isaac {
584354557abSToby Isaac   PetscErrorCode ierr;
585354557abSToby Isaac 
586354557abSToby Isaac   PetscFunctionBegin;
587354557abSToby Isaac   if (!--(dm->labels->refct)) {
588354557abSToby Isaac     DMLabelLink next = dm->labels->next;
589354557abSToby Isaac 
590354557abSToby Isaac     /* destroy the labels */
591354557abSToby Isaac     while (next) {
592354557abSToby Isaac       DMLabelLink tmp = next->next;
593354557abSToby Isaac 
594354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
595354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
596354557abSToby Isaac       next = tmp;
597354557abSToby Isaac     }
598354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
599354557abSToby Isaac   }
600354557abSToby Isaac   PetscFunctionReturn(0);
601354557abSToby Isaac }
602354557abSToby Isaac 
60347c6ae99SBarry Smith /*@
6048472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
60547c6ae99SBarry Smith 
606d083f849SBarry Smith     Collective on dm
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith     Input Parameter:
60947c6ae99SBarry Smith .   dm - the DM object to destroy
61047c6ae99SBarry Smith 
61147c6ae99SBarry Smith     Level: developer
61247c6ae99SBarry Smith 
613e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith @*/
616fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
61747c6ae99SBarry Smith {
61888bdff64SToby Isaac   PetscInt       i, cnt;
619dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
62047c6ae99SBarry Smith   PetscErrorCode ierr;
62147c6ae99SBarry Smith 
62247c6ae99SBarry Smith   PetscFunctionBegin;
6236bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6246bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
62587e657c6SBarry Smith 
62688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
62788bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62888bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
62988bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
630732e2eb9SMatthew G Knepley   /*
631732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
632732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
633732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
634732e2eb9SMatthew G Knepley   */
6356bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6366bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
637732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6386bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6396bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
640732e2eb9SMatthew G Knepley   }
641f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6420298fd71SBarry Smith   (*dm)->namedglobal = NULL;
643f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6442348bcf4SPeter Brune     nnext = nlink->next;
6452348bcf4SPeter 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);
6462348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6472348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6482348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6492348bcf4SPeter Brune   }
650f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6510298fd71SBarry Smith   (*dm)->namedlocal = NULL;
652f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
653f490541aSPeter Brune     nnext = nlink->next;
654f490541aSPeter 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);
655f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
656f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
657f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
658f490541aSPeter Brune   }
6592348bcf4SPeter Brune 
660b17ce1afSJed Brown   /* Destroy the list of hooks */
661c833c3b5SJed Brown   {
662c833c3b5SJed Brown     DMCoarsenHookLink link,next;
663b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
664b17ce1afSJed Brown       next = link->next;
665b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
666b17ce1afSJed Brown     }
6670298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
668c833c3b5SJed Brown   }
669c833c3b5SJed Brown   {
670c833c3b5SJed Brown     DMRefineHookLink link,next;
671c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
672c833c3b5SJed Brown       next = link->next;
673c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
674c833c3b5SJed Brown     }
6750298fd71SBarry Smith     (*dm)->refinehook = NULL;
676c833c3b5SJed Brown   }
677be081cd6SPeter Brune   {
678be081cd6SPeter Brune     DMSubDomainHookLink link,next;
679be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
680be081cd6SPeter Brune       next = link->next;
681be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
682be081cd6SPeter Brune     }
6830298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
684be081cd6SPeter Brune   }
685baf369e7SPeter Brune   {
686baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
687baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
688baf369e7SPeter Brune       next = link->next;
689baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
690baf369e7SPeter Brune     }
6910298fd71SBarry Smith     (*dm)->gtolhook = NULL;
692baf369e7SPeter Brune   }
693d4d07f1eSToby Isaac   {
694d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
695d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
696d4d07f1eSToby Isaac       next = link->next;
697d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
698d4d07f1eSToby Isaac     }
699d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
700d4d07f1eSToby Isaac   }
701aa1993deSMatthew G Knepley   /* Destroy the work arrays */
702aa1993deSMatthew G Knepley   {
703aa1993deSMatthew G Knepley     DMWorkLink link,next;
704aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
705aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
706aa1993deSMatthew G Knepley       next = link->next;
707aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
708aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
709aa1993deSMatthew G Knepley     }
7100298fd71SBarry Smith     (*dm)->workin = NULL;
711aa1993deSMatthew G Knepley   }
712c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
713c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
714c58f1c22SToby Isaac 
715c58f1c22SToby Isaac     /* destroy the labels */
716c58f1c22SToby Isaac     while (next) {
717c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
718c58f1c22SToby Isaac 
719c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
720c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
721c58f1c22SToby Isaac       next = tmp;
722c58f1c22SToby Isaac     }
723c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
724c58f1c22SToby Isaac   }
725e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
726e6f8dbb6SToby Isaac   {
727e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
728e6f8dbb6SToby Isaac     while (next) {
729e6f8dbb6SToby Isaac       DMBoundary b = next;
730e6f8dbb6SToby Isaac 
731e6f8dbb6SToby Isaac       next = b->next;
732e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
733e6f8dbb6SToby Isaac     }
734e6f8dbb6SToby Isaac   }
735b17ce1afSJed Brown 
73652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
73752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
73852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
73952536dc3SBarry Smith 
7401a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7411a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7421a266240SBarry Smith   }
74387e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
74471cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7454dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7466bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7476bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
748073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
74988ed4aceSMatthew G Knepley 
75088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
75188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
7528b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
753fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
754fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
75588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
75688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
757736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
758736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7598e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
760736995cdSBlaise Bourdin     }
761736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
762736995cdSBlaise Bourdin   }
76388bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
76488bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
76588bdff64SToby Isaac   }
766a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
76788bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
76888bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76988bdff64SToby Isaac   }
77088bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
771f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7726636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7736636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7746636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7755dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
776ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
777ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
778ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7796636e97aSMatthew G Knepley 
780e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
78114f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
782e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
783e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
784732e2eb9SMatthew G Knepley 
785ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7866bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
787ed3c66a1SDave May   }
788435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
789732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
79047c6ae99SBarry Smith   PetscFunctionReturn(0);
79147c6ae99SBarry Smith }
79247c6ae99SBarry Smith 
793d7bf68aeSBarry Smith /*@
794d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
795d7bf68aeSBarry Smith 
796d083f849SBarry Smith     Collective on dm
797d7bf68aeSBarry Smith 
798d7bf68aeSBarry Smith     Input Parameter:
799d7bf68aeSBarry Smith .   dm - the DM object to setup
800d7bf68aeSBarry Smith 
801d7bf68aeSBarry Smith     Level: developer
802d7bf68aeSBarry Smith 
803e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith @*/
8067087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
807d7bf68aeSBarry Smith {
808d7bf68aeSBarry Smith   PetscErrorCode ierr;
809d7bf68aeSBarry Smith 
810d7bf68aeSBarry Smith   PetscFunctionBegin;
811171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8128387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
813d7bf68aeSBarry Smith   if (dm->ops->setup) {
814d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
815d7bf68aeSBarry Smith   }
8168387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
817d7bf68aeSBarry Smith   PetscFunctionReturn(0);
818d7bf68aeSBarry Smith }
819d7bf68aeSBarry Smith 
820d7bf68aeSBarry Smith /*@
821d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
822d7bf68aeSBarry Smith 
823d083f849SBarry Smith     Collective on dm
824d7bf68aeSBarry Smith 
825d7bf68aeSBarry Smith     Input Parameter:
826d7bf68aeSBarry Smith .   dm - the DM object to set options for
827d7bf68aeSBarry Smith 
828732e2eb9SMatthew G Knepley     Options Database:
8294164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8304164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8314164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8328f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
833732e2eb9SMatthew G Knepley 
834d7bf68aeSBarry Smith     Level: developer
835d7bf68aeSBarry Smith 
836e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
837d7bf68aeSBarry Smith 
838d7bf68aeSBarry Smith @*/
8397087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
840d7bf68aeSBarry Smith {
8417781c08eSBarry Smith   char           typeName[256];
842ca266f36SBarry Smith   PetscBool      flg;
843d7bf68aeSBarry Smith   PetscErrorCode ierr;
844d7bf68aeSBarry Smith 
845d7bf68aeSBarry Smith   PetscFunctionBegin;
846171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84749be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
84849be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
84949be4549SMatthew G. Knepley   if (dm->defaultSF) {ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);}
8503194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8510298fd71SBarry 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);
852a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
853f9ba7244SBarry Smith   if (flg) {
854f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
855f9ba7244SBarry Smith   }
856a264d7a6SBarry 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);
857073dac72SJed Brown   if (flg) {
858521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
859073dac72SJed Brown   }
8608f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
861f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
862e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
863f9ba7244SBarry Smith   }
864f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8650633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
86682fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
867d7bf68aeSBarry Smith   PetscFunctionReturn(0);
868d7bf68aeSBarry Smith }
869d7bf68aeSBarry Smith 
870fc9bc008SSatish Balay /*@C
871224748a4SBarry Smith     DMView - Views a DM
87247c6ae99SBarry Smith 
873d083f849SBarry Smith     Collective on dm
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith     Input Parameter:
87647c6ae99SBarry Smith +   dm - the DM object to view
87747c6ae99SBarry Smith -   v - the viewer
87847c6ae99SBarry Smith 
879224748a4SBarry Smith     Level: beginner
88047c6ae99SBarry Smith 
881e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith @*/
8847087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
88547c6ae99SBarry Smith {
88647c6ae99SBarry Smith   PetscErrorCode    ierr;
88732c0f0efSBarry Smith   PetscBool         isbinary;
88876a8abe0SBarry Smith   PetscMPIInt       size;
88976a8abe0SBarry Smith   PetscViewerFormat format;
89047c6ae99SBarry Smith 
89147c6ae99SBarry Smith   PetscFunctionBegin;
892171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8933014e516SBarry Smith   if (!v) {
894ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8953014e516SBarry Smith   }
8968bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
89776a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
89876a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
89976a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
90098c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
90132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
90232c0f0efSBarry Smith   if (isbinary) {
90355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
90432c0f0efSBarry Smith     char     type[256];
90532c0f0efSBarry Smith 
90632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
90732c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
90832c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
90932c0f0efSBarry Smith   }
9100c010503SBarry Smith   if (dm->ops->view) {
9110c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
91247c6ae99SBarry Smith   }
91347c6ae99SBarry Smith   PetscFunctionReturn(0);
91447c6ae99SBarry Smith }
91547c6ae99SBarry Smith 
91647c6ae99SBarry Smith /*@
9178472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
91847c6ae99SBarry Smith 
919d083f849SBarry Smith     Collective on dm
92047c6ae99SBarry Smith 
92147c6ae99SBarry Smith     Input Parameter:
92247c6ae99SBarry Smith .   dm - the DM object
92347c6ae99SBarry Smith 
92447c6ae99SBarry Smith     Output Parameter:
92547c6ae99SBarry Smith .   vec - the global vector
92647c6ae99SBarry Smith 
927073dac72SJed Brown     Level: beginner
92847c6ae99SBarry Smith 
929e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith @*/
9327087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
93347c6ae99SBarry Smith {
93447c6ae99SBarry Smith   PetscErrorCode ierr;
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith   PetscFunctionBegin;
937171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93847c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
93947c6ae99SBarry Smith   PetscFunctionReturn(0);
94047c6ae99SBarry Smith }
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith /*@
9438472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith     Not Collective
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith     Input Parameter:
94847c6ae99SBarry Smith .   dm - the DM object
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith     Output Parameter:
95147c6ae99SBarry Smith .   vec - the local vector
95247c6ae99SBarry Smith 
953073dac72SJed Brown     Level: beginner
95447c6ae99SBarry Smith 
955e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith @*/
9587087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
95947c6ae99SBarry Smith {
96047c6ae99SBarry Smith   PetscErrorCode ierr;
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith   PetscFunctionBegin;
963171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
96447c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
96547c6ae99SBarry Smith   PetscFunctionReturn(0);
96647c6ae99SBarry Smith }
96747c6ae99SBarry Smith 
9681411c6eeSJed Brown /*@
9691411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9701411c6eeSJed Brown 
971d083f849SBarry Smith    Collective on dm
9721411c6eeSJed Brown 
9731411c6eeSJed Brown    Input Parameter:
9741411c6eeSJed Brown .  dm - the DM that provides the mapping
9751411c6eeSJed Brown 
9761411c6eeSJed Brown    Output Parameter:
9771411c6eeSJed Brown .  ltog - the mapping
9781411c6eeSJed Brown 
9791411c6eeSJed Brown    Level: intermediate
9801411c6eeSJed Brown 
9811411c6eeSJed Brown    Notes:
9821411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9831411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9841411c6eeSJed Brown 
985fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9861411c6eeSJed Brown @*/
9877087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9881411c6eeSJed Brown {
9890be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9901411c6eeSJed Brown   PetscErrorCode ierr;
9911411c6eeSJed Brown 
9921411c6eeSJed Brown   PetscFunctionBegin;
9931411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9941411c6eeSJed Brown   PetscValidPointer(ltog,2);
9951411c6eeSJed Brown   if (!dm->ltogmap) {
99637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
99737d0c07bSMatthew G Knepley 
998e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
99937d0c07bSMatthew G Knepley     if (section) {
1000a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
100137d0c07bSMatthew G Knepley       PetscInt       *ltog;
1002ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
100337d0c07bSMatthew G Knepley 
1004e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
100537d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1006ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1007ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
100837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1009a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
101037d0c07bSMatthew G Knepley 
101137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
101237d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1013a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1014a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
101537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10161a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10171a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10181a7dc684SMatthew G. Knepley         if (dof) {
1019b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1020b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10211a7dc684SMatthew G. Knepley         }
102237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1023a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1024a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
102537d0c07bSMatthew G Knepley         }
102637d0c07bSMatthew G Knepley       }
1027bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10280be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10290be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10300be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10310be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10327591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10337591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1034ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1035ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1036ccf3bd66SMatthew G. Knepley         n /= bs;
1037ccf3bd66SMatthew G. Knepley       }
1038ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10393bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
104037d0c07bSMatthew G Knepley     } else {
1041184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1042184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10431411c6eeSJed Brown     }
104437d0c07bSMatthew G Knepley   }
10451411c6eeSJed Brown   *ltog = dm->ltogmap;
10461411c6eeSJed Brown   PetscFunctionReturn(0);
10471411c6eeSJed Brown }
10481411c6eeSJed Brown 
10491411c6eeSJed Brown /*@
10501411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10511411c6eeSJed Brown 
10521411c6eeSJed Brown    Not Collective
10531411c6eeSJed Brown 
10541411c6eeSJed Brown    Input Parameter:
10551411c6eeSJed Brown .  dm - the DM with block structure
10561411c6eeSJed Brown 
10571411c6eeSJed Brown    Output Parameter:
10581411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10591411c6eeSJed Brown 
10601411c6eeSJed Brown    Level: intermediate
10611411c6eeSJed Brown 
1062fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10631411c6eeSJed Brown @*/
10647087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10651411c6eeSJed Brown {
10661411c6eeSJed Brown   PetscFunctionBegin;
10671411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10681411c6eeSJed Brown   PetscValidPointer(bs,2);
10691411c6eeSJed 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");
10701411c6eeSJed Brown   *bs = dm->bs;
10711411c6eeSJed Brown   PetscFunctionReturn(0);
10721411c6eeSJed Brown }
10731411c6eeSJed Brown 
107447c6ae99SBarry Smith /*@
10758472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
107647c6ae99SBarry Smith 
1077d083f849SBarry Smith     Collective on dm1
107847c6ae99SBarry Smith 
107947c6ae99SBarry Smith     Input Parameter:
108047c6ae99SBarry Smith +   dm1 - the DM object
108147c6ae99SBarry Smith -   dm2 - the second, finer DM object
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith     Output Parameter:
108447c6ae99SBarry Smith +  mat - the interpolation
108547c6ae99SBarry Smith -  vec - the scaling (optional)
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith     Level: developer
108847c6ae99SBarry Smith 
108995452b02SPatrick Sanan     Notes:
109095452b02SPatrick 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
109185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1092d52bd9f3SBarry Smith 
10931f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1094d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
109585afcc9aSBarry Smith 
109685afcc9aSBarry Smith 
10973ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith @*/
1100e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
110147c6ae99SBarry Smith {
110247c6ae99SBarry Smith   PetscErrorCode ierr;
110347c6ae99SBarry Smith 
110447c6ae99SBarry Smith   PetscFunctionBegin;
1105171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1106171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1107c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1108c7d20fa0SStefano Zampini   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInterpolation not implemented for type %s",((PetscObject)dm1)->type_name);
1109cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1111cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111247c6ae99SBarry Smith   PetscFunctionReturn(0);
111347c6ae99SBarry Smith }
111447c6ae99SBarry Smith 
11153ad4599aSBarry Smith /*@
11163ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11173ad4599aSBarry Smith 
1118d083f849SBarry Smith     Collective on dm1
11193ad4599aSBarry Smith 
11203ad4599aSBarry Smith     Input Parameter:
11213ad4599aSBarry Smith +   dm1 - the DM object
11223ad4599aSBarry Smith -   dm2 - the second, finer DM object
11233ad4599aSBarry Smith 
11243ad4599aSBarry Smith     Output Parameter:
11253ad4599aSBarry Smith .  mat - the restriction
11263ad4599aSBarry Smith 
11273ad4599aSBarry Smith 
11283ad4599aSBarry Smith     Level: developer
11293ad4599aSBarry Smith 
113095452b02SPatrick Sanan     Notes:
113195452b02SPatrick 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
11323ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11333ad4599aSBarry Smith 
11343ad4599aSBarry Smith 
11353ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11363ad4599aSBarry Smith 
11373ad4599aSBarry Smith @*/
11383ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11393ad4599aSBarry Smith {
11403ad4599aSBarry Smith   PetscErrorCode ierr;
11413ad4599aSBarry Smith 
11423ad4599aSBarry Smith   PetscFunctionBegin;
11433ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11443ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11453ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11467294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11473ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11483ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11493ad4599aSBarry Smith   PetscFunctionReturn(0);
11503ad4599aSBarry Smith }
11513ad4599aSBarry Smith 
115247c6ae99SBarry Smith /*@
11538472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
115447c6ae99SBarry Smith 
1155d083f849SBarry Smith     Collective on dm1
115647c6ae99SBarry Smith 
115747c6ae99SBarry Smith     Input Parameter:
115847c6ae99SBarry Smith +   dm1 - the DM object
115947c6ae99SBarry Smith -   dm2 - the second, finer DM object
116047c6ae99SBarry Smith 
116147c6ae99SBarry Smith     Output Parameter:
11626dbf9973SLawrence Mitchell .   mat - the injection
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith     Level: developer
116547c6ae99SBarry Smith 
116695452b02SPatrick Sanan    Notes:
116795452b02SPatrick 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
116885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
116985afcc9aSBarry Smith 
1170e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
117147c6ae99SBarry Smith 
117247c6ae99SBarry Smith @*/
11736dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
117447c6ae99SBarry Smith {
117547c6ae99SBarry Smith   PetscErrorCode ierr;
117647c6ae99SBarry Smith 
117747c6ae99SBarry Smith   PetscFunctionBegin;
1178171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1179171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11800c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11816dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
118247c6ae99SBarry Smith   PetscFunctionReturn(0);
118347c6ae99SBarry Smith }
118447c6ae99SBarry Smith 
1185b412c318SBarry Smith /*@
1186bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1187bd041c0cSMatthew G. Knepley 
1188d083f849SBarry Smith   Collective on dm1
1189bd041c0cSMatthew G. Knepley 
1190bd041c0cSMatthew G. Knepley   Input Parameter:
1191bd041c0cSMatthew G. Knepley + dm1 - the DM object
1192bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1193bd041c0cSMatthew G. Knepley 
1194bd041c0cSMatthew G. Knepley   Output Parameter:
1195bd041c0cSMatthew G. Knepley . mat - the interpolation
1196bd041c0cSMatthew G. Knepley 
1197bd041c0cSMatthew G. Knepley   Level: developer
1198bd041c0cSMatthew G. Knepley 
1199bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1200bd041c0cSMatthew G. Knepley @*/
1201bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1202bd041c0cSMatthew G. Knepley {
1203bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1204bd041c0cSMatthew G. Knepley 
1205bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1206bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1207bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1208bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1209bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1210bd041c0cSMatthew G. Knepley }
1211bd041c0cSMatthew G. Knepley 
1212bd041c0cSMatthew G. Knepley /*@
1213b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
121447c6ae99SBarry Smith 
1215d083f849SBarry Smith     Collective on dm
121647c6ae99SBarry Smith 
121747c6ae99SBarry Smith     Input Parameter:
121847c6ae99SBarry Smith +   dm - the DM object
12195bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith     Output Parameter:
122247c6ae99SBarry Smith .   coloring - the coloring
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith     Level: developer
122547c6ae99SBarry Smith 
1226b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
122747c6ae99SBarry Smith 
1228aab9d709SJed Brown @*/
1229b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
123047c6ae99SBarry Smith {
123147c6ae99SBarry Smith   PetscErrorCode ierr;
123247c6ae99SBarry Smith 
123347c6ae99SBarry Smith   PetscFunctionBegin;
1234171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1235ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1236b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
123747c6ae99SBarry Smith   PetscFunctionReturn(0);
123847c6ae99SBarry Smith }
123947c6ae99SBarry Smith 
1240b412c318SBarry Smith /*@
12418472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
124247c6ae99SBarry Smith 
1243d083f849SBarry Smith     Collective on dm
124447c6ae99SBarry Smith 
124547c6ae99SBarry Smith     Input Parameter:
1246b412c318SBarry Smith .   dm - the DM object
124747c6ae99SBarry Smith 
124847c6ae99SBarry Smith     Output Parameter:
124947c6ae99SBarry Smith .   mat - the empty Jacobian
125047c6ae99SBarry Smith 
1251073dac72SJed Brown     Level: beginner
125247c6ae99SBarry Smith 
125395452b02SPatrick Sanan     Notes:
125495452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
125594013140SBarry Smith        do not need to do it yourself.
125694013140SBarry Smith 
125794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12587889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
125994013140SBarry Smith 
126094013140SBarry 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
126194013140SBarry Smith        internally by PETSc.
126294013140SBarry Smith 
126394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1264aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
126594013140SBarry Smith 
1266b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
126747c6ae99SBarry Smith 
1268aab9d709SJed Brown @*/
1269b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
127047c6ae99SBarry Smith {
127147c6ae99SBarry Smith   PetscErrorCode ierr;
127247c6ae99SBarry Smith 
127347c6ae99SBarry Smith   PetscFunctionBegin;
1274171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1275607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1276c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1277c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1278b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1279e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1280e5e52638SMatthew G. Knepley   if (dm->Nf) {
1281e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1282e571a35bSMatthew G. Knepley     PetscInt     Nf;
1283e571a35bSMatthew G. Knepley 
1284e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1285e571a35bSMatthew G. Knepley     if (Nf == 1) {
1286e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1287e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1288e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1289e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1290e571a35bSMatthew G. Knepley       }
1291e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1292e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1293e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1294e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1295e571a35bSMatthew G. Knepley       }
1296e571a35bSMatthew G. Knepley     }
1297e571a35bSMatthew G. Knepley   }
129847c6ae99SBarry Smith   PetscFunctionReturn(0);
129947c6ae99SBarry Smith }
130047c6ae99SBarry Smith 
1301732e2eb9SMatthew G Knepley /*@
1302950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1303732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1304732e2eb9SMatthew G Knepley 
1305d083f849SBarry Smith   Logically Collective on dm
1306732e2eb9SMatthew G Knepley 
1307732e2eb9SMatthew G Knepley   Input Parameter:
1308732e2eb9SMatthew G Knepley + dm - the DM
1309732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1310732e2eb9SMatthew G Knepley 
1311732e2eb9SMatthew G Knepley   Level: developer
1312b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1313732e2eb9SMatthew G Knepley @*/
1314732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1315732e2eb9SMatthew G Knepley {
1316732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1317732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1318732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1319732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1320732e2eb9SMatthew G Knepley }
1321732e2eb9SMatthew G Knepley 
1322b06ff27eSHong Zhang /*@
1323b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1324b06ff27eSHong Zhang     but the array for values will not be allocated.
1325b06ff27eSHong Zhang 
1326d083f849SBarry Smith   Logically Collective on dm
1327b06ff27eSHong Zhang 
1328b06ff27eSHong Zhang   Input Parameter:
1329b06ff27eSHong Zhang + dm - the DM
1330b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1331b06ff27eSHong Zhang 
1332b06ff27eSHong Zhang   Level: developer
1333b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1334b06ff27eSHong Zhang @*/
1335b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1336b06ff27eSHong Zhang {
1337b06ff27eSHong Zhang   PetscFunctionBegin;
1338b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1339b06ff27eSHong Zhang   dm->structure_only = only;
1340b06ff27eSHong Zhang   PetscFunctionReturn(0);
1341b06ff27eSHong Zhang }
1342b06ff27eSHong Zhang 
1343a89ea682SMatthew G Knepley /*@C
1344aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1345a89ea682SMatthew G Knepley 
1346a89ea682SMatthew G Knepley   Not Collective
1347a89ea682SMatthew G Knepley 
1348a89ea682SMatthew G Knepley   Input Parameters:
1349a89ea682SMatthew G Knepley + dm - the DM object
1350aa1993deSMatthew G Knepley . count - The minium size
135169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1352a89ea682SMatthew G Knepley 
1353a89ea682SMatthew G Knepley   Output Parameter:
1354a89ea682SMatthew G Knepley . array - the work array
1355a89ea682SMatthew G Knepley 
1356a89ea682SMatthew G Knepley   Level: developer
1357a89ea682SMatthew G Knepley 
1358a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1359a89ea682SMatthew G Knepley @*/
136069291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1361a89ea682SMatthew G Knepley {
1362a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1363aa1993deSMatthew G Knepley   DMWorkLink     link;
136469291d52SBarry Smith   PetscMPIInt    dsize;
1365a89ea682SMatthew G Knepley 
1366a89ea682SMatthew G Knepley   PetscFunctionBegin;
1367a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1368aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1369aa1993deSMatthew G Knepley   if (dm->workin) {
1370aa1993deSMatthew G Knepley     link       = dm->workin;
1371aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1372aa1993deSMatthew G Knepley   } else {
1373b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1374a89ea682SMatthew G Knepley   }
137569291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13765056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1377aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1378854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1379*abe1f95cSSatish Balay     ierr        = PetscMemzero(link->mem,dsize*count);CHKERRQ(ierr);
1380854ce69bSBarry Smith     link->bytes = dsize*count;
1381aa1993deSMatthew G Knepley   }
1382aa1993deSMatthew G Knepley   link->next   = dm->workout;
1383aa1993deSMatthew G Knepley   dm->workout  = link;
1384aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1385a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1386a89ea682SMatthew G Knepley }
1387a89ea682SMatthew G Knepley 
1388aa1993deSMatthew G Knepley /*@C
1389aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1390aa1993deSMatthew G Knepley 
1391aa1993deSMatthew G Knepley   Not Collective
1392aa1993deSMatthew G Knepley 
1393aa1993deSMatthew G Knepley   Input Parameters:
1394aa1993deSMatthew G Knepley + dm - the DM object
1395aa1993deSMatthew G Knepley . count - The minium size
139669291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1397aa1993deSMatthew G Knepley 
1398aa1993deSMatthew G Knepley   Output Parameter:
1399aa1993deSMatthew G Knepley . array - the work array
1400aa1993deSMatthew G Knepley 
1401aa1993deSMatthew G Knepley   Level: developer
1402aa1993deSMatthew G Knepley 
140395452b02SPatrick Sanan   Developer Notes:
140495452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1405aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1406aa1993deSMatthew G Knepley @*/
140769291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1408aa1993deSMatthew G Knepley {
1409aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1410aa1993deSMatthew G Knepley 
1411aa1993deSMatthew G Knepley   PetscFunctionBegin;
1412aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1413aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1414aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1415aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1416aa1993deSMatthew G Knepley       *p           = link->next;
1417aa1993deSMatthew G Knepley       link->next   = dm->workin;
1418aa1993deSMatthew G Knepley       dm->workin   = link;
14190298fd71SBarry Smith       *(void**)mem = NULL;
1420aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1421aa1993deSMatthew G Knepley     }
1422aa1993deSMatthew G Knepley   }
1423aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1424aa1993deSMatthew G Knepley }
1425e7c4fc90SDmitry Karpeev 
1426435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1427435a35e8SMatthew G Knepley {
1428435a35e8SMatthew G Knepley   PetscFunctionBegin;
1429435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
143082f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1431435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1432435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1433435a35e8SMatthew G Knepley }
1434435a35e8SMatthew G Knepley 
14350a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14360a50eb56SMatthew G. Knepley {
14370a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14380a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1439f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14400a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14410a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14420a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14430a50eb56SMatthew G. Knepley }
14440a50eb56SMatthew G. Knepley 
1445f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1446f9d4088aSMatthew G. Knepley {
1447f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1448f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1449f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1450f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1451f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1452f9d4088aSMatthew G. Knepley }
1453f9d4088aSMatthew G. Knepley 
1454f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1455f9d4088aSMatthew G. Knepley {
1456f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1457f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1458f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1459f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1460f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1461f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1462f9d4088aSMatthew G. Knepley }
1463f9d4088aSMatthew G. Knepley 
14644f3b5142SJed Brown /*@C
14654d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14664d343eeaSMatthew G Knepley 
14674d343eeaSMatthew G Knepley   Not collective
14684d343eeaSMatthew G Knepley 
14694d343eeaSMatthew G Knepley   Input Parameter:
14704d343eeaSMatthew G Knepley . dm - the DM object
14714d343eeaSMatthew G Knepley 
14724d343eeaSMatthew G Knepley   Output Parameters:
14730298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14740298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14750298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14764d343eeaSMatthew G Knepley 
14774d343eeaSMatthew G Knepley   Level: intermediate
14784d343eeaSMatthew G Knepley 
147921c9b008SJed Brown   Notes:
148021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
148121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
148221c9b008SJed Brown   PetscFree().
148321c9b008SJed Brown 
14844d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14854d343eeaSMatthew G Knepley @*/
148637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14874d343eeaSMatthew G Knepley {
148837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14894d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14904d343eeaSMatthew G Knepley 
14914d343eeaSMatthew G Knepley   PetscFunctionBegin;
14924d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
149369ca1f37SDmitry Karpeev   if (numFields) {
149469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
149569ca1f37SDmitry Karpeev     *numFields = 0;
149669ca1f37SDmitry Karpeev   }
149737d0c07bSMatthew G Knepley   if (fieldNames) {
149837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14990298fd71SBarry Smith     *fieldNames = NULL;
150069ca1f37SDmitry Karpeev   }
150169ca1f37SDmitry Karpeev   if (fields) {
150269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15030298fd71SBarry Smith     *fields = NULL;
150469ca1f37SDmitry Karpeev   }
1505e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
150637d0c07bSMatthew G Knepley   if (section) {
15073a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
150837d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
150937d0c07bSMatthew G Knepley 
1510e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
151137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15123a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
151337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
151437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
151537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15163a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
151737d0c07bSMatthew G Knepley     }
151837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
151937d0c07bSMatthew G Knepley       PetscInt gdof;
152037d0c07bSMatthew G Knepley 
152137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
152237d0c07bSMatthew G Knepley       if (gdof > 0) {
152337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15243a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
152537d0c07bSMatthew G Knepley 
152637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
152737d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15283a544194SStefano Zampini           fpdof = fdof-fcdof;
15293a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15303a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15313a544194SStefano Zampini             fieldNc[f] = 1;
15323a544194SStefano Zampini           }
15333a544194SStefano Zampini           fieldSizes[f] += fpdof;
153437d0c07bSMatthew G Knepley         }
153537d0c07bSMatthew G Knepley       }
153637d0c07bSMatthew G Knepley     }
153737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1538785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
153937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
154037d0c07bSMatthew G Knepley     }
154137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
154237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
154337d0c07bSMatthew G Knepley 
154437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
154537d0c07bSMatthew G Knepley       if (gdof > 0) {
154637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
154737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
154837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
154937d0c07bSMatthew G Knepley 
155037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
155137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
155237d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
155337d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
155437d0c07bSMatthew G Knepley           }
155537d0c07bSMatthew G Knepley         }
155637d0c07bSMatthew G Knepley       }
155737d0c07bSMatthew G Knepley     }
15588865f1eaSKarl Rupp     if (numFields) *numFields = nF;
155937d0c07bSMatthew G Knepley     if (fieldNames) {
1560785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
156137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
156237d0c07bSMatthew G Knepley         const char *fieldName;
156337d0c07bSMatthew G Knepley 
156437d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
156537d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
156637d0c07bSMatthew G Knepley       }
156737d0c07bSMatthew G Knepley     }
156837d0c07bSMatthew G Knepley     if (fields) {
1569785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
157037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15713a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15723a544194SStefano Zampini 
157382f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15743a544194SStefano Zampini         in[0] = -fieldNc[f];
15753a544194SStefano Zampini         in[1] = fieldNc[f];
15763a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15773a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15783a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
157937d0c07bSMatthew G Knepley       }
158037d0c07bSMatthew G Knepley     }
15813a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15828865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15838865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
158469ca1f37SDmitry Karpeev   }
15854d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15864d343eeaSMatthew G Knepley }
15874d343eeaSMatthew G Knepley 
158816621825SDmitry Karpeev 
158916621825SDmitry Karpeev /*@C
159016621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
159116621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
159216621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1593e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1594e7c4fc90SDmitry Karpeev 
1595e7c4fc90SDmitry Karpeev   Not collective
1596e7c4fc90SDmitry Karpeev 
1597e7c4fc90SDmitry Karpeev   Input Parameter:
1598e7c4fc90SDmitry Karpeev . dm - the DM object
1599e7c4fc90SDmitry Karpeev 
1600e7c4fc90SDmitry Karpeev   Output Parameters:
16010298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16020298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16030298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16040298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1605e7c4fc90SDmitry Karpeev 
1606e7c4fc90SDmitry Karpeev   Level: intermediate
1607e7c4fc90SDmitry Karpeev 
1608e7c4fc90SDmitry Karpeev   Notes:
1609e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1610e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1611e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1612e7c4fc90SDmitry Karpeev 
1613e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1614e7c4fc90SDmitry Karpeev @*/
161516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1616e7c4fc90SDmitry Karpeev {
1617e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1618e7c4fc90SDmitry Karpeev 
1619e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1620e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16218865f1eaSKarl Rupp   if (len) {
16228865f1eaSKarl Rupp     PetscValidPointer(len,2);
16238865f1eaSKarl Rupp     *len = 0;
16248865f1eaSKarl Rupp   }
16258865f1eaSKarl Rupp   if (namelist) {
16268865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16278865f1eaSKarl Rupp     *namelist = 0;
16288865f1eaSKarl Rupp   }
16298865f1eaSKarl Rupp   if (islist) {
16308865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16318865f1eaSKarl Rupp     *islist = 0;
16328865f1eaSKarl Rupp   }
16338865f1eaSKarl Rupp   if (dmlist) {
16348865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16358865f1eaSKarl Rupp     *dmlist = 0;
16368865f1eaSKarl Rupp   }
1637f3f0edfdSDmitry Karpeev   /*
1638f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1639f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1640f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1641f3f0edfdSDmitry Karpeev    */
1642ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
164316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1644435a35e8SMatthew G Knepley     PetscSection section;
1645435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1646435a35e8SMatthew G Knepley 
1647e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1648435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1649435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1650f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
165103dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
165203dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
165303dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1654435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1655435a35e8SMatthew G Knepley         const char *fieldName;
1656435a35e8SMatthew G Knepley 
165703dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
165803dc3394SMatthew G. Knepley         if (namelist) {
1659435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1660435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1661435a35e8SMatthew G Knepley         }
166203dc3394SMatthew G. Knepley       }
1663435a35e8SMatthew G Knepley     } else {
166469ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1665e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16660298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1667e7c4fc90SDmitry Karpeev     }
16688865f1eaSKarl Rupp   } else {
166916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
167016621825SDmitry Karpeev   }
167116621825SDmitry Karpeev   PetscFunctionReturn(0);
167216621825SDmitry Karpeev }
167316621825SDmitry Karpeev 
1674564cec59SMatthew G. Knepley /*@
1675435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1676435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1677435a35e8SMatthew G Knepley 
1678435a35e8SMatthew G Knepley   Not collective
1679435a35e8SMatthew G Knepley 
1680435a35e8SMatthew G Knepley   Input Parameters:
16812adcc780SMatthew G. Knepley + dm        - The DM object
16822adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16832adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1684435a35e8SMatthew G Knepley 
1685435a35e8SMatthew G Knepley   Output Parameters:
16862adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16872adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1688435a35e8SMatthew G Knepley 
16895d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16905d3b26e6SMatthew G. Knepley 
1691435a35e8SMatthew G Knepley   Level: intermediate
1692435a35e8SMatthew G Knepley 
16935d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1694435a35e8SMatthew G Knepley @*/
169537bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1696435a35e8SMatthew G Knepley {
1697435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1698435a35e8SMatthew G Knepley 
1699435a35e8SMatthew G Knepley   PetscFunctionBegin;
1700435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1701435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17028865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17038865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1704435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1705435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
170682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1707435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1708435a35e8SMatthew G Knepley }
1709435a35e8SMatthew G Knepley 
17102adcc780SMatthew G. Knepley /*@C
17112adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17122adcc780SMatthew G. Knepley 
17132adcc780SMatthew G. Knepley   Not collective
17142adcc780SMatthew G. Knepley 
17152adcc780SMatthew G. Knepley   Input Parameter:
17162adcc780SMatthew G. Knepley + dms - The DM objects
17172adcc780SMatthew G. Knepley - len - The number of DMs
17182adcc780SMatthew G. Knepley 
17192adcc780SMatthew G. Knepley   Output Parameters:
1720a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17212adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17222adcc780SMatthew G. Knepley 
17235d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17245d3b26e6SMatthew G. Knepley 
17252adcc780SMatthew G. Knepley   Level: intermediate
17262adcc780SMatthew G. Knepley 
17275d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17282adcc780SMatthew G. Knepley @*/
17292adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17302adcc780SMatthew G. Knepley {
17312adcc780SMatthew G. Knepley   PetscInt       i;
17322adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17332adcc780SMatthew G. Knepley 
17342adcc780SMatthew G. Knepley   PetscFunctionBegin;
17352adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17362adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17372adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1738a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17392adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17402adcc780SMatthew G. Knepley   if (len) {
1741a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1742a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17432adcc780SMatthew G. Knepley   }
17442adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17452adcc780SMatthew G. Knepley }
17462adcc780SMatthew G. Knepley 
174716621825SDmitry Karpeev 
174816621825SDmitry Karpeev /*@C
17498d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17508d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17518d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17528d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17538d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
175416621825SDmitry Karpeev 
175516621825SDmitry Karpeev   Not collective
175616621825SDmitry Karpeev 
175716621825SDmitry Karpeev   Input Parameter:
175816621825SDmitry Karpeev . dm - the DM object
175916621825SDmitry Karpeev 
176016621825SDmitry Karpeev   Output Parameters:
17610298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17620298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17630298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17640298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17650298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
176616621825SDmitry Karpeev 
176716621825SDmitry Karpeev   Level: intermediate
176816621825SDmitry Karpeev 
176916621825SDmitry Karpeev   Notes:
177016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
177116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
177216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
177316621825SDmitry Karpeev 
17748d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
177516621825SDmitry Karpeev @*/
17768d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
177716621825SDmitry Karpeev {
177816621825SDmitry Karpeev   PetscErrorCode      ierr;
1779be081cd6SPeter Brune   DMSubDomainHookLink link;
1780be081cd6SPeter Brune   PetscInt            i,l;
178116621825SDmitry Karpeev 
178216621825SDmitry Karpeev   PetscFunctionBegin;
178316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
178414a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17850298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17860298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17870298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17880298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1789f3f0edfdSDmitry Karpeev   /*
1790f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1791f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1792f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1793f3f0edfdSDmitry Karpeev    */
1794ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
179516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1796be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
179714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1798f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1799be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1800be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1801be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1802be081cd6SPeter Brune         }
1803648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1804e7c4fc90SDmitry Karpeev       }
180514a18fd3SPeter Brune     }
180614a18fd3SPeter Brune     if (len) *len = l;
180714a18fd3SPeter Brune   }
1808e30e807fSPeter Brune   PetscFunctionReturn(0);
1809e30e807fSPeter Brune }
1810e30e807fSPeter Brune 
1811e30e807fSPeter Brune 
1812e30e807fSPeter Brune /*@C
1813e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1814e30e807fSPeter Brune 
1815e30e807fSPeter Brune   Not collective
1816e30e807fSPeter Brune 
1817e30e807fSPeter Brune   Input Parameters:
1818e30e807fSPeter Brune + dm - the DM object
1819e30e807fSPeter Brune . n  - the number of subdomain scatters
1820e30e807fSPeter Brune - subdms - the local subdomains
1821e30e807fSPeter Brune 
1822e30e807fSPeter Brune   Output Parameters:
1823e30e807fSPeter Brune + n     - the number of scatters returned
1824e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1825e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1826e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1827e30e807fSPeter Brune 
182895452b02SPatrick Sanan   Notes:
182995452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1830e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1831e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1832e30e807fSPeter Brune   solution and residual data.
1833e30e807fSPeter Brune 
1834e30e807fSPeter Brune   Level: developer
1835e30e807fSPeter Brune 
1836e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1837e30e807fSPeter Brune @*/
1838e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1839e30e807fSPeter Brune {
1840e30e807fSPeter Brune   PetscErrorCode ierr;
1841e30e807fSPeter Brune 
1842e30e807fSPeter Brune   PetscFunctionBegin;
1843e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1844e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1845e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1846e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18476668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1848e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1849e7c4fc90SDmitry Karpeev }
1850e7c4fc90SDmitry Karpeev 
185147c6ae99SBarry Smith /*@
185247c6ae99SBarry Smith   DMRefine - Refines a DM object
185347c6ae99SBarry Smith 
1854d083f849SBarry Smith   Collective on dm
185547c6ae99SBarry Smith 
185647c6ae99SBarry Smith   Input Parameter:
185747c6ae99SBarry Smith + dm   - the DM object
185891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
185947c6ae99SBarry Smith 
186047c6ae99SBarry Smith   Output Parameter:
18610298fd71SBarry Smith . dmf - the refined DM, or NULL
1862ae0a1c52SMatthew G Knepley 
18630298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
186447c6ae99SBarry Smith 
186547c6ae99SBarry Smith   Level: developer
186647c6ae99SBarry Smith 
1867e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
186847c6ae99SBarry Smith @*/
18697087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
187047c6ae99SBarry Smith {
187147c6ae99SBarry Smith   PetscErrorCode   ierr;
1872c833c3b5SJed Brown   DMRefineHookLink link;
187347c6ae99SBarry Smith 
187447c6ae99SBarry Smith   PetscFunctionBegin;
1875732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18761ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1877fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
187847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18794057135bSMatthew G Knepley   if (*dmf) {
188043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18818865f1eaSKarl Rupp 
18828cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18838865f1eaSKarl Rupp 
1884644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18850598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1886656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18878865f1eaSKarl Rupp 
1888e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1889c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18908865f1eaSKarl Rupp       if (link->refinehook) {
18918865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18928865f1eaSKarl Rupp       }
1893c833c3b5SJed Brown     }
1894c833c3b5SJed Brown   }
18951ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1896c833c3b5SJed Brown   PetscFunctionReturn(0);
1897c833c3b5SJed Brown }
1898c833c3b5SJed Brown 
1899bb9467b5SJed Brown /*@C
1900c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1901c833c3b5SJed Brown 
1902c833c3b5SJed Brown    Logically Collective
1903c833c3b5SJed Brown 
1904c833c3b5SJed Brown    Input Arguments:
1905c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1906c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1907c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19080298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1909c833c3b5SJed Brown 
1910c833c3b5SJed Brown    Calling sequence of refinehook:
1911c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1912c833c3b5SJed Brown 
1913c833c3b5SJed Brown +  coarse - coarse level DM
1914c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1915c833c3b5SJed Brown -  ctx - optional user-defined function context
1916c833c3b5SJed Brown 
1917c833c3b5SJed Brown    Calling sequence for interphook:
1918c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1919c833c3b5SJed Brown 
1920c833c3b5SJed Brown +  coarse - coarse level DM
1921c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1922c833c3b5SJed Brown .  fine - fine level DM to update
1923c833c3b5SJed Brown -  ctx - optional user-defined function context
1924c833c3b5SJed Brown 
1925c833c3b5SJed Brown    Level: advanced
1926c833c3b5SJed Brown 
1927c833c3b5SJed Brown    Notes:
1928c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1929c833c3b5SJed Brown 
1930c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1931c833c3b5SJed Brown 
1932bb9467b5SJed Brown    This function is currently not available from Fortran.
1933bb9467b5SJed Brown 
1934c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1935c833c3b5SJed Brown @*/
1936c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1937c833c3b5SJed Brown {
1938c833c3b5SJed Brown   PetscErrorCode   ierr;
1939c833c3b5SJed Brown   DMRefineHookLink link,*p;
1940c833c3b5SJed Brown 
1941c833c3b5SJed Brown   PetscFunctionBegin;
1942c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19433d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19443d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19453d8e3701SJed Brown   }
194695dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1947c833c3b5SJed Brown   link->refinehook = refinehook;
1948c833c3b5SJed Brown   link->interphook = interphook;
1949c833c3b5SJed Brown   link->ctx        = ctx;
19500298fd71SBarry Smith   link->next       = NULL;
1951c833c3b5SJed Brown   *p               = link;
1952c833c3b5SJed Brown   PetscFunctionReturn(0);
1953c833c3b5SJed Brown }
1954c833c3b5SJed Brown 
19553d8e3701SJed Brown /*@C
19563d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19573d8e3701SJed Brown 
19583d8e3701SJed Brown    Logically Collective
19593d8e3701SJed Brown 
19603d8e3701SJed Brown    Input Arguments:
19613d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19623d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19633d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19643d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19653d8e3701SJed Brown 
19663d8e3701SJed Brown    Level: advanced
19673d8e3701SJed Brown 
19683d8e3701SJed Brown    Notes:
19693d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19703d8e3701SJed Brown 
19713d8e3701SJed Brown    This function is currently not available from Fortran.
19723d8e3701SJed Brown 
19733d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19743d8e3701SJed Brown @*/
19753d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19763d8e3701SJed Brown {
19773d8e3701SJed Brown   PetscErrorCode   ierr;
19783d8e3701SJed Brown   DMRefineHookLink link,*p;
19793d8e3701SJed Brown 
19803d8e3701SJed Brown   PetscFunctionBegin;
19813d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19823d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19833d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19843d8e3701SJed Brown       link = *p;
19853d8e3701SJed Brown       *p = link->next;
19863d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19873d8e3701SJed Brown       break;
19883d8e3701SJed Brown     }
19893d8e3701SJed Brown   }
19903d8e3701SJed Brown   PetscFunctionReturn(0);
19913d8e3701SJed Brown }
19923d8e3701SJed Brown 
1993c833c3b5SJed Brown /*@
1994c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1995c833c3b5SJed Brown 
1996c833c3b5SJed Brown    Collective if any hooks are
1997c833c3b5SJed Brown 
1998c833c3b5SJed Brown    Input Arguments:
1999c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2000e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2001c833c3b5SJed Brown -  fine - finer DM to update
2002c833c3b5SJed Brown 
2003c833c3b5SJed Brown    Level: developer
2004c833c3b5SJed Brown 
2005c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2006c833c3b5SJed Brown @*/
2007c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2008c833c3b5SJed Brown {
2009c833c3b5SJed Brown   PetscErrorCode   ierr;
2010c833c3b5SJed Brown   DMRefineHookLink link;
2011c833c3b5SJed Brown 
2012c833c3b5SJed Brown   PetscFunctionBegin;
2013c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20148865f1eaSKarl Rupp     if (link->interphook) {
20158865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20168865f1eaSKarl Rupp     }
20174057135bSMatthew G Knepley   }
201847c6ae99SBarry Smith   PetscFunctionReturn(0);
201947c6ae99SBarry Smith }
202047c6ae99SBarry Smith 
2021eb3f98d2SBarry Smith /*@
2022eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2023eb3f98d2SBarry Smith 
2024eb3f98d2SBarry Smith     Not Collective
2025eb3f98d2SBarry Smith 
2026eb3f98d2SBarry Smith     Input Parameter:
2027eb3f98d2SBarry Smith .   dm - the DM object
2028eb3f98d2SBarry Smith 
2029eb3f98d2SBarry Smith     Output Parameter:
2030eb3f98d2SBarry Smith .   level - number of refinements
2031eb3f98d2SBarry Smith 
2032eb3f98d2SBarry Smith     Level: developer
2033eb3f98d2SBarry Smith 
20346a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2035eb3f98d2SBarry Smith 
2036eb3f98d2SBarry Smith @*/
2037eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2038eb3f98d2SBarry Smith {
2039eb3f98d2SBarry Smith   PetscFunctionBegin;
2040eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2041eb3f98d2SBarry Smith   *level = dm->levelup;
2042eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2043eb3f98d2SBarry Smith }
2044eb3f98d2SBarry Smith 
2045fef3a512SBarry Smith /*@
2046fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2047fef3a512SBarry Smith 
2048fef3a512SBarry Smith     Not Collective
2049fef3a512SBarry Smith 
2050fef3a512SBarry Smith     Input Parameter:
2051fef3a512SBarry Smith +   dm - the DM object
2052fef3a512SBarry Smith -   level - number of refinements
2053fef3a512SBarry Smith 
2054fef3a512SBarry Smith     Level: advanced
2055fef3a512SBarry Smith 
205695452b02SPatrick Sanan     Notes:
205795452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2058fef3a512SBarry Smith 
2059fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2060fef3a512SBarry Smith 
2061fef3a512SBarry Smith @*/
2062fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2063fef3a512SBarry Smith {
2064fef3a512SBarry Smith   PetscFunctionBegin;
2065fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2066fef3a512SBarry Smith   dm->levelup = level;
2067fef3a512SBarry Smith   PetscFunctionReturn(0);
2068fef3a512SBarry Smith }
2069fef3a512SBarry Smith 
2070ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2071ca3d3a14SMatthew G. Knepley {
2072ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2073ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2074ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2075ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2076ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2077ca3d3a14SMatthew G. Knepley }
2078ca3d3a14SMatthew G. Knepley 
2079ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2080ca3d3a14SMatthew G. Knepley {
2081ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2082ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2083ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2084ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2085ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2086ca3d3a14SMatthew G. Knepley }
2087ca3d3a14SMatthew G. Knepley 
2088ca3d3a14SMatthew G. Knepley /*@
2089c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2090ca3d3a14SMatthew G. Knepley 
2091ca3d3a14SMatthew G. Knepley   Input Parameter:
2092ca3d3a14SMatthew G. Knepley . dm - The DM
2093ca3d3a14SMatthew G. Knepley 
2094ca3d3a14SMatthew G. Knepley   Output Parameter:
2095ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2096ca3d3a14SMatthew G. Knepley 
2097ca3d3a14SMatthew G. Knepley   Level: developer
2098ca3d3a14SMatthew G. Knepley 
2099ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2100ca3d3a14SMatthew G. Knepley @*/
2101ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2102ca3d3a14SMatthew G. Knepley {
2103ca3d3a14SMatthew G. Knepley   Vec            tv;
2104ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2105ca3d3a14SMatthew G. Knepley 
2106ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2107ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2108ca3d3a14SMatthew G. Knepley   PetscValidPointer(flg, 2);
2109ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2110ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2111ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2112ca3d3a14SMatthew G. Knepley }
2113ca3d3a14SMatthew G. Knepley 
2114ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2115ca3d3a14SMatthew G. Knepley {
2116ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2117ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2118ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2119ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2120ca3d3a14SMatthew G. Knepley 
2121ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2122ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
2123ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
2124ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2125ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2126ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
2127ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(dm->transformDM, &ts);CHKERRQ(ierr);
2128ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2129ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2130ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2131ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2132ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2133ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2134ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2135ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2136ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2137ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2138ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2139ca3d3a14SMatthew G. Knepley     }
2140ca3d3a14SMatthew G. Knepley   }
2141ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2142ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2143ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2144ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2145ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2146ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2147ca3d3a14SMatthew G. Knepley       if (dof) {
2148ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2149ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2150ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2151ca3d3a14SMatthew G. Knepley 
2152ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2153ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2154ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2155580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2156ca3d3a14SMatthew G. Knepley       }
2157ca3d3a14SMatthew G. Knepley     }
2158ca3d3a14SMatthew G. Knepley   }
2159ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2160ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2161ca3d3a14SMatthew G. Knepley }
2162ca3d3a14SMatthew G. Knepley 
2163ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2164ca3d3a14SMatthew G. Knepley {
2165ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2166ca3d3a14SMatthew G. Knepley 
2167ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2168ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2169ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2170ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2171ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2172ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2173ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2174ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2175ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2176ca3d3a14SMatthew G. Knepley }
2177ca3d3a14SMatthew G. Knepley 
2178bb9467b5SJed Brown /*@C
2179baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2180baf369e7SPeter Brune 
2181baf369e7SPeter Brune    Logically Collective
2182baf369e7SPeter Brune 
2183baf369e7SPeter Brune    Input Arguments:
2184baf369e7SPeter Brune +  dm - the DM
2185baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2186baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
21870298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2188baf369e7SPeter Brune 
2189baf369e7SPeter Brune    Calling sequence for beginhook:
2190baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2191baf369e7SPeter Brune 
2192baf369e7SPeter Brune +  dm - global DM
2193baf369e7SPeter Brune .  g - global vector
2194baf369e7SPeter Brune .  mode - mode
2195baf369e7SPeter Brune .  l - local vector
2196baf369e7SPeter Brune -  ctx - optional user-defined function context
2197baf369e7SPeter Brune 
2198baf369e7SPeter Brune 
2199baf369e7SPeter Brune    Calling sequence for endhook:
2200ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2201baf369e7SPeter Brune 
2202baf369e7SPeter Brune +  global - global DM
2203baf369e7SPeter Brune -  ctx - optional user-defined function context
2204baf369e7SPeter Brune 
2205baf369e7SPeter Brune    Level: advanced
2206baf369e7SPeter Brune 
2207baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2208baf369e7SPeter Brune @*/
2209baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2210baf369e7SPeter Brune {
2211baf369e7SPeter Brune   PetscErrorCode          ierr;
2212baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2213baf369e7SPeter Brune 
2214baf369e7SPeter Brune   PetscFunctionBegin;
2215baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2216baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
221795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2218baf369e7SPeter Brune   link->beginhook = beginhook;
2219baf369e7SPeter Brune   link->endhook   = endhook;
2220baf369e7SPeter Brune   link->ctx       = ctx;
22210298fd71SBarry Smith   link->next      = NULL;
2222baf369e7SPeter Brune   *p              = link;
2223baf369e7SPeter Brune   PetscFunctionReturn(0);
2224baf369e7SPeter Brune }
2225baf369e7SPeter Brune 
22264c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
22274c274da1SToby Isaac {
22284c274da1SToby Isaac   Mat cMat;
22294c274da1SToby Isaac   Vec cVec;
22304c274da1SToby Isaac   PetscSection section, cSec;
22314c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22324c274da1SToby Isaac   PetscErrorCode ierr;
22334c274da1SToby Isaac 
22344c274da1SToby Isaac   PetscFunctionBegin;
22354c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22364c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22374c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
22385db9a05bSToby Isaac     PetscInt nRows;
22395db9a05bSToby Isaac 
22405db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22415db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2242e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
22437711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22444c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
22454c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22464c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22474c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22484c274da1SToby Isaac       if (dof) {
22494c274da1SToby Isaac         PetscScalar *vals;
22504c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
22514c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
22524c274da1SToby Isaac       }
22534c274da1SToby Isaac     }
22544c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22554c274da1SToby Isaac   }
22564c274da1SToby Isaac   PetscFunctionReturn(0);
22574c274da1SToby Isaac }
22584c274da1SToby Isaac 
225947c6ae99SBarry Smith /*@
226001729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
226101729b5cSPatrick Sanan 
2262d083f849SBarry Smith     Neighbor-wise Collective on dm
226301729b5cSPatrick Sanan 
226401729b5cSPatrick Sanan     Input Parameters:
226501729b5cSPatrick Sanan +   dm - the DM object
226601729b5cSPatrick Sanan .   g - the global vector
226701729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
226801729b5cSPatrick Sanan -   l - the local vector
226901729b5cSPatrick Sanan 
227001729b5cSPatrick Sanan     Notes:
227101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
227201729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
227301729b5cSPatrick Sanan 
227401729b5cSPatrick Sanan     Level: beginner
227501729b5cSPatrick Sanan 
227601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
227701729b5cSPatrick Sanan 
227801729b5cSPatrick Sanan @*/
227901729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
228001729b5cSPatrick Sanan {
228101729b5cSPatrick Sanan   PetscErrorCode ierr;
228201729b5cSPatrick Sanan 
228301729b5cSPatrick Sanan   PetscFunctionBegin;
228401729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
228501729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
228601729b5cSPatrick Sanan   PetscFunctionReturn(0);
228701729b5cSPatrick Sanan }
228801729b5cSPatrick Sanan 
228901729b5cSPatrick Sanan /*@
229047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
229147c6ae99SBarry Smith 
2292d083f849SBarry Smith     Neighbor-wise Collective on dm
229347c6ae99SBarry Smith 
229447c6ae99SBarry Smith     Input Parameters:
229547c6ae99SBarry Smith +   dm - the DM object
229647c6ae99SBarry Smith .   g - the global vector
229747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
229847c6ae99SBarry Smith -   l - the local vector
229947c6ae99SBarry Smith 
230001729b5cSPatrick Sanan     Level: intermediate
230147c6ae99SBarry Smith 
230201729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
230347c6ae99SBarry Smith 
230447c6ae99SBarry Smith @*/
23057087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
230647c6ae99SBarry Smith {
23077128ae9fSMatthew G Knepley   PetscSF                 sf;
230847c6ae99SBarry Smith   PetscErrorCode          ierr;
2309baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
231047c6ae99SBarry Smith 
231147c6ae99SBarry Smith   PetscFunctionBegin;
2312171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2313baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23148865f1eaSKarl Rupp     if (link->beginhook) {
23158865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23168865f1eaSKarl Rupp     }
2317baf369e7SPeter Brune   }
23187128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
23197128ae9fSMatthew G Knepley   if (sf) {
2320ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2321ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23227128ae9fSMatthew G Knepley 
232382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23247128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2325ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23267128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23277128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2328ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
23297128ae9fSMatthew G Knepley   } else {
233033907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2331843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23327128ae9fSMatthew G Knepley   }
233347c6ae99SBarry Smith   PetscFunctionReturn(0);
233447c6ae99SBarry Smith }
233547c6ae99SBarry Smith 
233647c6ae99SBarry Smith /*@
233747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
233847c6ae99SBarry Smith 
2339d083f849SBarry Smith     Neighbor-wise Collective on dm
234047c6ae99SBarry Smith 
234147c6ae99SBarry Smith     Input Parameters:
234247c6ae99SBarry Smith +   dm - the DM object
234347c6ae99SBarry Smith .   g - the global vector
234447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
234547c6ae99SBarry Smith -   l - the local vector
234647c6ae99SBarry Smith 
234701729b5cSPatrick Sanan     Level: intermediate
234847c6ae99SBarry Smith 
234901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
235047c6ae99SBarry Smith 
235147c6ae99SBarry Smith @*/
23527087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
235347c6ae99SBarry Smith {
23547128ae9fSMatthew G Knepley   PetscSF                 sf;
235547c6ae99SBarry Smith   PetscErrorCode          ierr;
2356ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2357ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2358ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2359baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
236047c6ae99SBarry Smith 
236147c6ae99SBarry Smith   PetscFunctionBegin;
2362171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23637128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2364ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
23657128ae9fSMatthew G Knepley   if (sf) {
236682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23677128ae9fSMatthew G Knepley 
23687128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2369ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23707128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23717128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2372ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2373ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
23747128ae9fSMatthew G Knepley   } else {
237533907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2376843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23777128ae9fSMatthew G Knepley   }
23784c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2379baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2380baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2381baf369e7SPeter Brune   }
238247c6ae99SBarry Smith   PetscFunctionReturn(0);
238347c6ae99SBarry Smith }
238447c6ae99SBarry Smith 
2385d4d07f1eSToby Isaac /*@C
2386d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2387d4d07f1eSToby Isaac 
2388d4d07f1eSToby Isaac    Logically Collective
2389d4d07f1eSToby Isaac 
2390d4d07f1eSToby Isaac    Input Arguments:
2391d4d07f1eSToby Isaac +  dm - the DM
2392d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2393d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2394d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2395d4d07f1eSToby Isaac 
2396d4d07f1eSToby Isaac    Calling sequence for beginhook:
2397d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2398d4d07f1eSToby Isaac 
2399d4d07f1eSToby Isaac +  dm - global DM
2400d4d07f1eSToby Isaac .  l - local vector
2401d4d07f1eSToby Isaac .  mode - mode
2402d4d07f1eSToby Isaac .  g - global vector
2403d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2404d4d07f1eSToby Isaac 
2405d4d07f1eSToby Isaac 
2406d4d07f1eSToby Isaac    Calling sequence for endhook:
2407d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2408d4d07f1eSToby Isaac 
2409d4d07f1eSToby Isaac +  global - global DM
2410d4d07f1eSToby Isaac .  l - local vector
2411d4d07f1eSToby Isaac .  mode - mode
2412d4d07f1eSToby Isaac .  g - global vector
2413d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2414d4d07f1eSToby Isaac 
2415d4d07f1eSToby Isaac    Level: advanced
2416d4d07f1eSToby Isaac 
2417d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2418d4d07f1eSToby Isaac @*/
2419d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2420d4d07f1eSToby Isaac {
2421d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2422d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2423d4d07f1eSToby Isaac 
2424d4d07f1eSToby Isaac   PetscFunctionBegin;
2425d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2426d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
242795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2428d4d07f1eSToby Isaac   link->beginhook = beginhook;
2429d4d07f1eSToby Isaac   link->endhook   = endhook;
2430d4d07f1eSToby Isaac   link->ctx       = ctx;
2431d4d07f1eSToby Isaac   link->next      = NULL;
2432d4d07f1eSToby Isaac   *p              = link;
2433d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2434d4d07f1eSToby Isaac }
2435d4d07f1eSToby Isaac 
24364c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
24374c274da1SToby Isaac {
24384c274da1SToby Isaac   Mat cMat;
24394c274da1SToby Isaac   Vec cVec;
24404c274da1SToby Isaac   PetscSection section, cSec;
24414c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24424c274da1SToby Isaac   PetscErrorCode ierr;
24434c274da1SToby Isaac 
24444c274da1SToby Isaac   PetscFunctionBegin;
24454c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24464c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24474c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
24485db9a05bSToby Isaac     PetscInt nRows;
24495db9a05bSToby Isaac 
24505db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24515db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2452e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
24537711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24544c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24554c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24564c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24574c274da1SToby Isaac       if (dof) {
24584c274da1SToby Isaac         PetscInt d;
24594c274da1SToby Isaac         PetscScalar *vals;
24604c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
24614c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
24624c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
24634c274da1SToby Isaac          * we just extracted */
24644c274da1SToby Isaac         for (d = 0; d < dof; d++) {
24654c274da1SToby Isaac           vals[d] = 0.;
24664c274da1SToby Isaac         }
24674c274da1SToby Isaac       }
24684c274da1SToby Isaac     }
24694c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
24704c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24714c274da1SToby Isaac   }
24724c274da1SToby Isaac   PetscFunctionReturn(0);
24734c274da1SToby Isaac }
247401729b5cSPatrick Sanan /*@
247501729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
247601729b5cSPatrick Sanan 
2477d083f849SBarry Smith     Neighbor-wise Collective on dm
247801729b5cSPatrick Sanan 
247901729b5cSPatrick Sanan     Input Parameters:
248001729b5cSPatrick Sanan +   dm - the DM object
248101729b5cSPatrick Sanan .   l - the local vector
248201729b5cSPatrick 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.
248301729b5cSPatrick Sanan -   g - the global vector
248401729b5cSPatrick Sanan 
248501729b5cSPatrick Sanan     Notes:
248601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
248701729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
248801729b5cSPatrick Sanan 
248901729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
249001729b5cSPatrick 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.
249101729b5cSPatrick Sanan 
249201729b5cSPatrick Sanan     Level: beginner
249301729b5cSPatrick Sanan 
249401729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
249501729b5cSPatrick Sanan 
249601729b5cSPatrick Sanan @*/
249701729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
249801729b5cSPatrick Sanan {
249901729b5cSPatrick Sanan   PetscErrorCode ierr;
250001729b5cSPatrick Sanan 
250101729b5cSPatrick Sanan   PetscFunctionBegin;
250201729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
250301729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
250401729b5cSPatrick Sanan   PetscFunctionReturn(0);
250501729b5cSPatrick Sanan }
25064c274da1SToby Isaac 
250747c6ae99SBarry Smith /*@
250801729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25099a42bb27SBarry Smith 
2510d083f849SBarry Smith     Neighbor-wise Collective on dm
25119a42bb27SBarry Smith 
25129a42bb27SBarry Smith     Input Parameters:
25139a42bb27SBarry Smith +   dm - the DM object
2514f6813fd5SJed Brown .   l - the local vector
25151eb28f2eSBarry 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.
25161eb28f2eSBarry Smith -   g - the global vector
25179a42bb27SBarry Smith 
251895452b02SPatrick Sanan     Notes:
251995452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
252084330215SMatthew 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.
25219a42bb27SBarry Smith 
252201729b5cSPatrick Sanan     Level: intermediate
25239a42bb27SBarry Smith 
252401729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
25259a42bb27SBarry Smith 
25269a42bb27SBarry Smith @*/
25277087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
25289a42bb27SBarry Smith {
25297128ae9fSMatthew G Knepley   PetscSF                 sf;
253084330215SMatthew G. Knepley   PetscSection            s, gs;
2531d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2532ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2533ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2534ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2535ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
253684330215SMatthew G. Knepley   PetscErrorCode          ierr;
25379a42bb27SBarry Smith 
25389a42bb27SBarry Smith   PetscFunctionBegin;
2539171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2540d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2541d4d07f1eSToby Isaac     if (link->beginhook) {
2542d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2543d4d07f1eSToby Isaac     }
2544d4d07f1eSToby Isaac   }
25454c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
25467128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2547e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
25487128ae9fSMatthew G Knepley   switch (mode) {
25497128ae9fSMatthew G Knepley   case INSERT_VALUES:
25507128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2551304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
255284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25537128ae9fSMatthew G Knepley   case ADD_VALUES:
25547128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2555304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
255684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25577128ae9fSMatthew G Knepley   default:
255882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25597128ae9fSMatthew G Knepley   }
2560ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2561ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2562ca3d3a14SMatthew G. Knepley     if (transform) {
2563ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2564ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2565ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2566ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2567ca3d3a14SMatthew G. Knepley     } else {
2568ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2569ca3d3a14SMatthew G. Knepley     }
25707128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2571ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2572a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
257384330215SMatthew G. Knepley     } else if (s && isInsert) {
257484330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
257584330215SMatthew G. Knepley 
2576e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
257784330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
257884330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
257984330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2580b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
258184330215SMatthew G. Knepley 
258284330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
258303442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
258484330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2585b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
258684330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
258784330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2588b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
258903442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2590b3b16f48SMatthew 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);
2591b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2592b3b16f48SMatthew G. Knepley         if (!gcdof) {
259384330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2594b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2595b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
259684330215SMatthew G. Knepley           const PetscInt *cdofs;
259784330215SMatthew G. Knepley           PetscInt        cind = 0;
259884330215SMatthew G. Knepley 
259984330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2600b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
260184330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2602b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
260384330215SMatthew G. Knepley           }
2604b3b16f48SMatthew 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);
260584330215SMatthew G. Knepley       }
2606ca3d3a14SMatthew G. Knepley     }
26077128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2608ca3d3a14SMatthew G. Knepley     if (transform) {
2609ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2610ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2611ca3d3a14SMatthew G. Knepley     } else {
2612ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2613ca3d3a14SMatthew G. Knepley     }
26147128ae9fSMatthew G Knepley   } else {
2615843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26167128ae9fSMatthew G Knepley   }
26179a42bb27SBarry Smith   PetscFunctionReturn(0);
26189a42bb27SBarry Smith }
26199a42bb27SBarry Smith 
26209a42bb27SBarry Smith /*@
26219a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
262247c6ae99SBarry Smith 
2623d083f849SBarry Smith     Neighbor-wise Collective on dm
262447c6ae99SBarry Smith 
262547c6ae99SBarry Smith     Input Parameters:
262647c6ae99SBarry Smith +   dm - the DM object
2627f6813fd5SJed Brown .   l - the local vector
262847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2629f6813fd5SJed Brown -   g - the global vector
263047c6ae99SBarry Smith 
263101729b5cSPatrick Sanan     Level: intermediate
263247c6ae99SBarry Smith 
2633e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
263447c6ae99SBarry Smith 
263547c6ae99SBarry Smith @*/
26367087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
263747c6ae99SBarry Smith {
26387128ae9fSMatthew G Knepley   PetscSF                 sf;
263984330215SMatthew G. Knepley   PetscSection            s;
2640d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2641ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
264284330215SMatthew G. Knepley   PetscErrorCode          ierr;
264347c6ae99SBarry Smith 
264447c6ae99SBarry Smith   PetscFunctionBegin;
2645171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26467128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2647e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
26487128ae9fSMatthew G Knepley   switch (mode) {
26497128ae9fSMatthew G Knepley   case INSERT_VALUES:
26507128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
265184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26527128ae9fSMatthew G Knepley   case ADD_VALUES:
26537128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
265484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26557128ae9fSMatthew G Knepley   default:
265682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26577128ae9fSMatthew G Knepley   }
265884330215SMatthew G. Knepley   if (sf && !isInsert) {
2659ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2660ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2661ca3d3a14SMatthew G. Knepley     Vec                tmpl;
266284330215SMatthew G. Knepley 
2663ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2664ca3d3a14SMatthew G. Knepley     if (transform) {
2665ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2666ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2667ca3d3a14SMatthew G. Knepley     } else {
2668ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2669ca3d3a14SMatthew G. Knepley     }
26707128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2671a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2672ca3d3a14SMatthew G. Knepley     if (transform) {
2673ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2674ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2675ca3d3a14SMatthew G. Knepley     } else {
2676ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2677ca3d3a14SMatthew G. Knepley     }
26787128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
267984330215SMatthew G. Knepley   } else if (s && isInsert) {
26807128ae9fSMatthew G Knepley   } else {
2681843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26827128ae9fSMatthew G Knepley   }
2683d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2684d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2685d4d07f1eSToby Isaac   }
268647c6ae99SBarry Smith   PetscFunctionReturn(0);
268747c6ae99SBarry Smith }
268847c6ae99SBarry Smith 
2689f089877aSRichard Tran Mills /*@
2690bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2691bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2692d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2693f089877aSRichard Tran Mills 
2694d083f849SBarry Smith    Neighbor-wise Collective on dm
2695f089877aSRichard Tran Mills 
2696f089877aSRichard Tran Mills    Input Parameters:
2697f089877aSRichard Tran Mills +  dm - the DM object
2698bc0a1609SRichard Tran Mills .  g - the original local vector
2699bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2700f089877aSRichard Tran Mills 
2701bc0a1609SRichard Tran Mills    Output Parameter:
2702bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2703f089877aSRichard Tran Mills 
2704f089877aSRichard Tran Mills    Level: intermediate
2705f089877aSRichard Tran Mills 
2706bc0a1609SRichard Tran Mills    Notes:
2707bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2708bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2709bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2710bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2711bc0a1609SRichard Tran Mills 
2712bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2713f089877aSRichard Tran Mills 
2714f089877aSRichard Tran Mills @*/
2715f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2716f089877aSRichard Tran Mills {
2717f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2718f089877aSRichard Tran Mills 
2719f089877aSRichard Tran Mills   PetscFunctionBegin;
2720f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2721bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2722f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2723f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2724f089877aSRichard Tran Mills }
2725f089877aSRichard Tran Mills 
2726f089877aSRichard Tran Mills /*@
2727bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2728bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2729d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2730f089877aSRichard Tran Mills 
2731d083f849SBarry Smith    Neighbor-wise Collective on dm
2732f089877aSRichard Tran Mills 
2733f089877aSRichard Tran Mills    Input Parameters:
2734bc0a1609SRichard Tran Mills +  da - the DM object
2735bc0a1609SRichard Tran Mills .  g - the original local vector
2736bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2737f089877aSRichard Tran Mills 
2738bc0a1609SRichard Tran Mills    Output Parameter:
2739bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2740f089877aSRichard Tran Mills 
2741f089877aSRichard Tran Mills    Level: intermediate
2742f089877aSRichard Tran Mills 
2743bc0a1609SRichard Tran Mills    Notes:
2744bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2745bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2746bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2747bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2748bc0a1609SRichard Tran Mills 
2749bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2750f089877aSRichard Tran Mills 
2751f089877aSRichard Tran Mills @*/
2752f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2753f089877aSRichard Tran Mills {
2754f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2755f089877aSRichard Tran Mills 
2756f089877aSRichard Tran Mills   PetscFunctionBegin;
2757f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2758bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2759f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2760f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2761f089877aSRichard Tran Mills }
2762f089877aSRichard Tran Mills 
2763f089877aSRichard Tran Mills 
276447c6ae99SBarry Smith /*@
276547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
276647c6ae99SBarry Smith 
2767d083f849SBarry Smith     Collective on dm
276847c6ae99SBarry Smith 
276947c6ae99SBarry Smith     Input Parameter:
277047c6ae99SBarry Smith +   dm - the DM object
277191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
277247c6ae99SBarry Smith 
277347c6ae99SBarry Smith     Output Parameter:
277447c6ae99SBarry Smith .   dmc - the coarsened DM
277547c6ae99SBarry Smith 
277647c6ae99SBarry Smith     Level: developer
277747c6ae99SBarry Smith 
2778e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
277947c6ae99SBarry Smith 
278047c6ae99SBarry Smith @*/
27817087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
278247c6ae99SBarry Smith {
278347c6ae99SBarry Smith   PetscErrorCode    ierr;
2784b17ce1afSJed Brown   DMCoarsenHookLink link;
278547c6ae99SBarry Smith 
278647c6ae99SBarry Smith   PetscFunctionBegin;
2787171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2788fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
278947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
279047c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
27918892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2792a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
279343842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
27948cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2795644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
27960598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2797656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2798e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2799b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2800b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2801b17ce1afSJed Brown   }
280247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2803b17ce1afSJed Brown   PetscFunctionReturn(0);
2804b17ce1afSJed Brown }
2805b17ce1afSJed Brown 
2806bb9467b5SJed Brown /*@C
2807b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2808b17ce1afSJed Brown 
2809b17ce1afSJed Brown    Logically Collective
2810b17ce1afSJed Brown 
2811b17ce1afSJed Brown    Input Arguments:
2812b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2813b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2814b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28150298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2816b17ce1afSJed Brown 
2817b17ce1afSJed Brown    Calling sequence of coarsenhook:
2818b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2819b17ce1afSJed Brown 
2820b17ce1afSJed Brown +  fine - fine level DM
2821b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2822b17ce1afSJed Brown -  ctx - optional user-defined function context
2823b17ce1afSJed Brown 
2824b17ce1afSJed Brown    Calling sequence for restricthook:
2825c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2826b17ce1afSJed Brown 
2827b17ce1afSJed Brown +  fine - fine level DM
2828b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2829c833c3b5SJed Brown .  rscale - scaling vector for restriction
2830c833c3b5SJed Brown .  inject - matrix restricting by injection
2831b17ce1afSJed Brown .  coarse - coarse level DM to update
2832b17ce1afSJed Brown -  ctx - optional user-defined function context
2833b17ce1afSJed Brown 
2834b17ce1afSJed Brown    Level: advanced
2835b17ce1afSJed Brown 
2836b17ce1afSJed Brown    Notes:
2837b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2838b17ce1afSJed Brown 
2839b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2840b17ce1afSJed Brown 
2841b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2842b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2843b17ce1afSJed Brown 
2844bb9467b5SJed Brown    This function is currently not available from Fortran.
2845bb9467b5SJed Brown 
2846dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2847b17ce1afSJed Brown @*/
2848b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2849b17ce1afSJed Brown {
2850b17ce1afSJed Brown   PetscErrorCode    ierr;
2851b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2852b17ce1afSJed Brown 
2853b17ce1afSJed Brown   PetscFunctionBegin;
2854b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
28551e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
28561e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
28571e3d8eccSJed Brown   }
285895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2859b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2860b17ce1afSJed Brown   link->restricthook = restricthook;
2861b17ce1afSJed Brown   link->ctx          = ctx;
28620298fd71SBarry Smith   link->next         = NULL;
2863b17ce1afSJed Brown   *p                 = link;
2864b17ce1afSJed Brown   PetscFunctionReturn(0);
2865b17ce1afSJed Brown }
2866b17ce1afSJed Brown 
2867dc822a44SJed Brown /*@C
2868dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2869dc822a44SJed Brown 
2870dc822a44SJed Brown    Logically Collective
2871dc822a44SJed Brown 
2872dc822a44SJed Brown    Input Arguments:
2873dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2874dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2875dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2876dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2877dc822a44SJed Brown 
2878dc822a44SJed Brown    Level: advanced
2879dc822a44SJed Brown 
2880dc822a44SJed Brown    Notes:
2881dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2882dc822a44SJed Brown 
2883dc822a44SJed Brown    This function is currently not available from Fortran.
2884dc822a44SJed Brown 
2885dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2886dc822a44SJed Brown @*/
2887dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2888dc822a44SJed Brown {
2889dc822a44SJed Brown   PetscErrorCode    ierr;
2890dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2891dc822a44SJed Brown 
2892dc822a44SJed Brown   PetscFunctionBegin;
2893dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2894dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2895dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2896dc822a44SJed Brown       link = *p;
2897dc822a44SJed Brown       *p = link->next;
2898dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2899dc822a44SJed Brown       break;
2900dc822a44SJed Brown     }
2901dc822a44SJed Brown   }
2902dc822a44SJed Brown   PetscFunctionReturn(0);
2903dc822a44SJed Brown }
2904dc822a44SJed Brown 
2905dc822a44SJed Brown 
2906b17ce1afSJed Brown /*@
2907b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2908b17ce1afSJed Brown 
2909b17ce1afSJed Brown    Collective if any hooks are
2910b17ce1afSJed Brown 
2911b17ce1afSJed Brown    Input Arguments:
2912b17ce1afSJed Brown +  fine - finer DM to use as a base
2913b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2914e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2915b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2916e91eccc2SStefano Zampini -  coarse - coarser DM to update
2917b17ce1afSJed Brown 
2918b17ce1afSJed Brown    Level: developer
2919b17ce1afSJed Brown 
2920b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2921b17ce1afSJed Brown @*/
2922b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2923b17ce1afSJed Brown {
2924b17ce1afSJed Brown   PetscErrorCode    ierr;
2925b17ce1afSJed Brown   DMCoarsenHookLink link;
2926b17ce1afSJed Brown 
2927b17ce1afSJed Brown   PetscFunctionBegin;
2928b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
29298865f1eaSKarl Rupp     if (link->restricthook) {
29308865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
29318865f1eaSKarl Rupp     }
2932b17ce1afSJed Brown   }
293347c6ae99SBarry Smith   PetscFunctionReturn(0);
293447c6ae99SBarry Smith }
293547c6ae99SBarry Smith 
2936bb9467b5SJed Brown /*@C
2937be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
29385dbd56e3SPeter Brune 
2939d083f849SBarry Smith    Logically Collective on global
29405dbd56e3SPeter Brune 
29415dbd56e3SPeter Brune    Input Arguments:
29425dbd56e3SPeter Brune +  global - global DM
2943ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
29445dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
29450298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
29465dbd56e3SPeter Brune 
2947ec4806b8SPeter Brune 
2948ec4806b8SPeter Brune    Calling sequence for ddhook:
2949ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2950ec4806b8SPeter Brune 
2951ec4806b8SPeter Brune +  global - global DM
2952ec4806b8SPeter Brune .  block  - block DM
2953ec4806b8SPeter Brune -  ctx - optional user-defined function context
2954ec4806b8SPeter Brune 
29555dbd56e3SPeter Brune    Calling sequence for restricthook:
2956ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
29575dbd56e3SPeter Brune 
29585dbd56e3SPeter Brune +  global - global DM
29595dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
29605dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2961ec4806b8SPeter Brune .  block  - block DM
29625dbd56e3SPeter Brune -  ctx - optional user-defined function context
29635dbd56e3SPeter Brune 
29645dbd56e3SPeter Brune    Level: advanced
29655dbd56e3SPeter Brune 
29665dbd56e3SPeter Brune    Notes:
2967ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
29685dbd56e3SPeter Brune 
29695dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
29705dbd56e3SPeter Brune 
29715dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2972ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
29735dbd56e3SPeter Brune 
2974bb9467b5SJed Brown    This function is currently not available from Fortran.
2975bb9467b5SJed Brown 
29765dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
29775dbd56e3SPeter Brune @*/
2978be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
29795dbd56e3SPeter Brune {
29805dbd56e3SPeter Brune   PetscErrorCode      ierr;
2981be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
29825dbd56e3SPeter Brune 
29835dbd56e3SPeter Brune   PetscFunctionBegin;
29845dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2985b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2986b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2987b3a6b972SJed Brown   }
298895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
29895dbd56e3SPeter Brune   link->restricthook = restricthook;
2990be081cd6SPeter Brune   link->ddhook       = ddhook;
29915dbd56e3SPeter Brune   link->ctx          = ctx;
29920298fd71SBarry Smith   link->next         = NULL;
29935dbd56e3SPeter Brune   *p                 = link;
29945dbd56e3SPeter Brune   PetscFunctionReturn(0);
29955dbd56e3SPeter Brune }
29965dbd56e3SPeter Brune 
2997b3a6b972SJed Brown /*@C
2998b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2999b3a6b972SJed Brown 
3000b3a6b972SJed Brown    Logically Collective
3001b3a6b972SJed Brown 
3002b3a6b972SJed Brown    Input Arguments:
3003b3a6b972SJed Brown +  global - global DM
3004b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3005b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3006b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3007b3a6b972SJed Brown 
3008b3a6b972SJed Brown    Level: advanced
3009b3a6b972SJed Brown 
3010b3a6b972SJed Brown    Notes:
3011b3a6b972SJed Brown 
3012b3a6b972SJed Brown    This function is currently not available from Fortran.
3013b3a6b972SJed Brown 
3014b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3015b3a6b972SJed Brown @*/
3016b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3017b3a6b972SJed Brown {
3018b3a6b972SJed Brown   PetscErrorCode      ierr;
3019b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3020b3a6b972SJed Brown 
3021b3a6b972SJed Brown   PetscFunctionBegin;
3022b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3023b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3024b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3025b3a6b972SJed Brown       link = *p;
3026b3a6b972SJed Brown       *p = link->next;
3027b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3028b3a6b972SJed Brown       break;
3029b3a6b972SJed Brown     }
3030b3a6b972SJed Brown   }
3031b3a6b972SJed Brown   PetscFunctionReturn(0);
3032b3a6b972SJed Brown }
3033b3a6b972SJed Brown 
30345dbd56e3SPeter Brune /*@
3035be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
30365dbd56e3SPeter Brune 
30375dbd56e3SPeter Brune    Collective if any hooks are
30385dbd56e3SPeter Brune 
30395dbd56e3SPeter Brune    Input Arguments:
30405dbd56e3SPeter Brune +  fine - finer DM to use as a base
3041be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3042be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
30435dbd56e3SPeter Brune -  coarse - coarer DM to update
30445dbd56e3SPeter Brune 
30455dbd56e3SPeter Brune    Level: developer
30465dbd56e3SPeter Brune 
30475dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
30485dbd56e3SPeter Brune @*/
3049be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
30505dbd56e3SPeter Brune {
30515dbd56e3SPeter Brune   PetscErrorCode      ierr;
3052be081cd6SPeter Brune   DMSubDomainHookLink link;
30535dbd56e3SPeter Brune 
30545dbd56e3SPeter Brune   PetscFunctionBegin;
3055be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
30568865f1eaSKarl Rupp     if (link->restricthook) {
30578865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
30588865f1eaSKarl Rupp     }
30595dbd56e3SPeter Brune   }
30605dbd56e3SPeter Brune   PetscFunctionReturn(0);
30615dbd56e3SPeter Brune }
30625dbd56e3SPeter Brune 
30635fe1f584SPeter Brune /*@
30646a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
30655fe1f584SPeter Brune 
30665fe1f584SPeter Brune     Not Collective
30675fe1f584SPeter Brune 
30685fe1f584SPeter Brune     Input Parameter:
30695fe1f584SPeter Brune .   dm - the DM object
30705fe1f584SPeter Brune 
30715fe1f584SPeter Brune     Output Parameter:
30726a7d9d85SPeter Brune .   level - number of coarsenings
30735fe1f584SPeter Brune 
30745fe1f584SPeter Brune     Level: developer
30755fe1f584SPeter Brune 
30766a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
30775fe1f584SPeter Brune 
30785fe1f584SPeter Brune @*/
30795fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
30805fe1f584SPeter Brune {
30815fe1f584SPeter Brune   PetscFunctionBegin;
30825fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30835fe1f584SPeter Brune   *level = dm->leveldown;
30845fe1f584SPeter Brune   PetscFunctionReturn(0);
30855fe1f584SPeter Brune }
30865fe1f584SPeter Brune 
30879a64c4a8SMatthew G. Knepley /*@
30889a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
30899a64c4a8SMatthew G. Knepley 
30909a64c4a8SMatthew G. Knepley     Not Collective
30919a64c4a8SMatthew G. Knepley 
30929a64c4a8SMatthew G. Knepley     Input Parameters:
30939a64c4a8SMatthew G. Knepley +   dm - the DM object
30949a64c4a8SMatthew G. Knepley -   level - number of coarsenings
30959a64c4a8SMatthew G. Knepley 
30969a64c4a8SMatthew G. Knepley     Level: developer
30979a64c4a8SMatthew G. Knepley 
30989a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
30999a64c4a8SMatthew G. Knepley @*/
31009a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31019a64c4a8SMatthew G. Knepley {
31029a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31039a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31049a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31059a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31069a64c4a8SMatthew G. Knepley }
31079a64c4a8SMatthew G. Knepley 
31085fe1f584SPeter Brune 
31095fe1f584SPeter Brune 
311047c6ae99SBarry Smith /*@C
311147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
311247c6ae99SBarry Smith 
3113d083f849SBarry Smith     Collective on dm
311447c6ae99SBarry Smith 
311547c6ae99SBarry Smith     Input Parameter:
311647c6ae99SBarry Smith +   dm - the DM object
311747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
311847c6ae99SBarry Smith 
311947c6ae99SBarry Smith     Output Parameter:
312047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
312147c6ae99SBarry Smith 
312247c6ae99SBarry Smith     Level: developer
312347c6ae99SBarry Smith 
3124e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
312547c6ae99SBarry Smith 
312647c6ae99SBarry Smith @*/
31277087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
312847c6ae99SBarry Smith {
312947c6ae99SBarry Smith   PetscErrorCode ierr;
313047c6ae99SBarry Smith 
313147c6ae99SBarry Smith   PetscFunctionBegin;
3132171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3133ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
313447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
313547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
313647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
313747c6ae99SBarry Smith   } else if (dm->ops->refine) {
313847c6ae99SBarry Smith     PetscInt i;
313947c6ae99SBarry Smith 
3140ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
314147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3142ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
314347c6ae99SBarry Smith     }
3144ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
314547c6ae99SBarry Smith   PetscFunctionReturn(0);
314647c6ae99SBarry Smith }
314747c6ae99SBarry Smith 
314847c6ae99SBarry Smith /*@C
314947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
315047c6ae99SBarry Smith 
3151d083f849SBarry Smith     Collective on dm
315247c6ae99SBarry Smith 
315347c6ae99SBarry Smith     Input Parameter:
315447c6ae99SBarry Smith +   dm - the DM object
315547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
315647c6ae99SBarry Smith 
315747c6ae99SBarry Smith     Output Parameter:
315847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
315947c6ae99SBarry Smith 
316047c6ae99SBarry Smith     Level: developer
316147c6ae99SBarry Smith 
3162e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
316347c6ae99SBarry Smith 
316447c6ae99SBarry Smith @*/
31657087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
316647c6ae99SBarry Smith {
316747c6ae99SBarry Smith   PetscErrorCode ierr;
316847c6ae99SBarry Smith 
316947c6ae99SBarry Smith   PetscFunctionBegin;
3170171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3171ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
317247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
317347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
317447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
317547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
317647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
317747c6ae99SBarry Smith     PetscInt i;
317847c6ae99SBarry Smith 
3179ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
318047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3181ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
318247c6ae99SBarry Smith     }
3183ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
318447c6ae99SBarry Smith   PetscFunctionReturn(0);
318547c6ae99SBarry Smith }
318647c6ae99SBarry Smith 
318747c6ae99SBarry Smith /*@
3188e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
318947c6ae99SBarry Smith    grids associated with two DMs.
319047c6ae99SBarry Smith 
3191d083f849SBarry Smith    Collective on dmc
319247c6ae99SBarry Smith 
319347c6ae99SBarry Smith    Input Parameters:
319447c6ae99SBarry Smith +  dmc - the coarse grid DM
319547c6ae99SBarry Smith -  dmf - the fine grid DM
319647c6ae99SBarry Smith 
319747c6ae99SBarry Smith    Output Parameters:
319847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
319947c6ae99SBarry Smith 
320047c6ae99SBarry Smith    Level: intermediate
320147c6ae99SBarry Smith 
3202e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
320347c6ae99SBarry Smith @*/
3204e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
320547c6ae99SBarry Smith {
320647c6ae99SBarry Smith   PetscErrorCode ierr;
320747c6ae99SBarry Smith 
320847c6ae99SBarry Smith   PetscFunctionBegin;
3209171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3210171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
321147c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
321247c6ae99SBarry Smith   PetscFunctionReturn(0);
321347c6ae99SBarry Smith }
321447c6ae99SBarry Smith 
32151a266240SBarry Smith /*@C
32161a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32171a266240SBarry Smith 
32181a266240SBarry Smith     Not Collective
32191a266240SBarry Smith 
32201a266240SBarry Smith     Input Parameters:
32211a266240SBarry Smith +   dm - the DM object
32221a266240SBarry Smith -   destroy - the destroy function
32231a266240SBarry Smith 
32241a266240SBarry Smith     Level: intermediate
32251a266240SBarry Smith 
3226e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32271a266240SBarry Smith 
3228f07f9ceaSJed Brown @*/
32291a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32301a266240SBarry Smith {
32311a266240SBarry Smith   PetscFunctionBegin;
3232171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32331a266240SBarry Smith   dm->ctxdestroy = destroy;
32341a266240SBarry Smith   PetscFunctionReturn(0);
32351a266240SBarry Smith }
32361a266240SBarry Smith 
3237b07ff414SBarry Smith /*@
32381b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
323947c6ae99SBarry Smith 
324047c6ae99SBarry Smith     Not Collective
324147c6ae99SBarry Smith 
324247c6ae99SBarry Smith     Input Parameters:
324347c6ae99SBarry Smith +   dm - the DM object
324447c6ae99SBarry Smith -   ctx - the user context
324547c6ae99SBarry Smith 
324647c6ae99SBarry Smith     Level: intermediate
324747c6ae99SBarry Smith 
3248e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
324947c6ae99SBarry Smith 
325047c6ae99SBarry Smith @*/
32511b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
325247c6ae99SBarry Smith {
325347c6ae99SBarry Smith   PetscFunctionBegin;
3254171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
325547c6ae99SBarry Smith   dm->ctx = ctx;
325647c6ae99SBarry Smith   PetscFunctionReturn(0);
325747c6ae99SBarry Smith }
325847c6ae99SBarry Smith 
325947c6ae99SBarry Smith /*@
32601b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
326147c6ae99SBarry Smith 
326247c6ae99SBarry Smith     Not Collective
326347c6ae99SBarry Smith 
326447c6ae99SBarry Smith     Input Parameter:
326547c6ae99SBarry Smith .   dm - the DM object
326647c6ae99SBarry Smith 
326747c6ae99SBarry Smith     Output Parameter:
326847c6ae99SBarry Smith .   ctx - the user context
326947c6ae99SBarry Smith 
327047c6ae99SBarry Smith     Level: intermediate
327147c6ae99SBarry Smith 
3272e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
327347c6ae99SBarry Smith 
327447c6ae99SBarry Smith @*/
32751b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
327647c6ae99SBarry Smith {
327747c6ae99SBarry Smith   PetscFunctionBegin;
3278171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32791b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
328047c6ae99SBarry Smith   PetscFunctionReturn(0);
328147c6ae99SBarry Smith }
328247c6ae99SBarry Smith 
328308da532bSDmitry Karpeev /*@C
3284df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
328508da532bSDmitry Karpeev 
3286d083f849SBarry Smith     Logically Collective on dm
328708da532bSDmitry Karpeev 
328808da532bSDmitry Karpeev     Input Parameter:
328908da532bSDmitry Karpeev +   dm - the DM object
32900298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
329108da532bSDmitry Karpeev 
329208da532bSDmitry Karpeev     Level: intermediate
329308da532bSDmitry Karpeev 
3294835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
329508da532bSDmitry Karpeev          DMSetJacobian()
329608da532bSDmitry Karpeev 
329708da532bSDmitry Karpeev @*/
329808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
329908da532bSDmitry Karpeev {
330008da532bSDmitry Karpeev   PetscFunctionBegin;
330108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
330208da532bSDmitry Karpeev   PetscFunctionReturn(0);
330308da532bSDmitry Karpeev }
330408da532bSDmitry Karpeev 
330508da532bSDmitry Karpeev /*@
330608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
330708da532bSDmitry Karpeev 
330808da532bSDmitry Karpeev     Not Collective
330908da532bSDmitry Karpeev 
331008da532bSDmitry Karpeev     Input Parameter:
331108da532bSDmitry Karpeev .   dm - the DM object to destroy
331208da532bSDmitry Karpeev 
331308da532bSDmitry Karpeev     Output Parameter:
331408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
331508da532bSDmitry Karpeev 
331608da532bSDmitry Karpeev     Level: developer
331708da532bSDmitry Karpeev 
331874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
331908da532bSDmitry Karpeev 
332008da532bSDmitry Karpeev @*/
332108da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
332208da532bSDmitry Karpeev {
332308da532bSDmitry Karpeev   PetscFunctionBegin;
332408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
332508da532bSDmitry Karpeev   PetscFunctionReturn(0);
332608da532bSDmitry Karpeev }
332708da532bSDmitry Karpeev 
332808da532bSDmitry Karpeev /*@C
332908da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
333008da532bSDmitry Karpeev 
3331d083f849SBarry Smith     Logically Collective on dm
333208da532bSDmitry Karpeev 
333308da532bSDmitry Karpeev     Input Parameters:
3334907376e6SBarry Smith .   dm - the DM object
333508da532bSDmitry Karpeev 
333608da532bSDmitry Karpeev     Output parameters:
333708da532bSDmitry Karpeev +   xl - lower bound
333808da532bSDmitry Karpeev -   xu - upper bound
333908da532bSDmitry Karpeev 
3340907376e6SBarry Smith     Level: advanced
3341907376e6SBarry Smith 
334295452b02SPatrick Sanan     Notes:
334395452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
334408da532bSDmitry Karpeev 
334574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
334608da532bSDmitry Karpeev 
334708da532bSDmitry Karpeev @*/
334808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
334908da532bSDmitry Karpeev {
335008da532bSDmitry Karpeev   PetscErrorCode ierr;
33515fd66863SKarl Rupp 
335208da532bSDmitry Karpeev   PetscFunctionBegin;
335308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
335408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
335508da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
335608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
33578865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
335808da532bSDmitry Karpeev   PetscFunctionReturn(0);
335908da532bSDmitry Karpeev }
336008da532bSDmitry Karpeev 
3361b0ae01b7SPeter Brune /*@
3362b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3363b0ae01b7SPeter Brune 
3364b0ae01b7SPeter Brune     Not Collective
3365b0ae01b7SPeter Brune 
3366b0ae01b7SPeter Brune     Input Parameter:
3367b0ae01b7SPeter Brune .   dm - the DM object
3368b0ae01b7SPeter Brune 
3369b0ae01b7SPeter Brune     Output Parameter:
3370b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3371b0ae01b7SPeter Brune 
3372b0ae01b7SPeter Brune     Level: developer
3373b0ae01b7SPeter Brune 
33741565f0a7SPatrick Sanan .seealso DMCreateColoring()
3375b0ae01b7SPeter Brune 
3376b0ae01b7SPeter Brune @*/
3377b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3378b0ae01b7SPeter Brune {
3379b0ae01b7SPeter Brune   PetscFunctionBegin;
3380b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3381b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3382b0ae01b7SPeter Brune }
3383b0ae01b7SPeter Brune 
33843ad4599aSBarry Smith /*@
33853ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
33863ad4599aSBarry Smith 
33873ad4599aSBarry Smith     Not Collective
33883ad4599aSBarry Smith 
33893ad4599aSBarry Smith     Input Parameter:
33903ad4599aSBarry Smith .   dm - the DM object
33913ad4599aSBarry Smith 
33923ad4599aSBarry Smith     Output Parameter:
33933ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
33943ad4599aSBarry Smith 
33953ad4599aSBarry Smith     Level: developer
33963ad4599aSBarry Smith 
33971565f0a7SPatrick Sanan .seealso DMCreateRestriction()
33983ad4599aSBarry Smith 
33993ad4599aSBarry Smith @*/
34003ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
34013ad4599aSBarry Smith {
34023ad4599aSBarry Smith   PetscFunctionBegin;
34033ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34043ad4599aSBarry Smith   PetscFunctionReturn(0);
34053ad4599aSBarry Smith }
34063ad4599aSBarry Smith 
3407a7058e45SLawrence Mitchell 
3408a7058e45SLawrence Mitchell /*@
3409a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3410a7058e45SLawrence Mitchell 
3411a7058e45SLawrence Mitchell     Not Collective
3412a7058e45SLawrence Mitchell 
3413a7058e45SLawrence Mitchell     Input Parameter:
3414a7058e45SLawrence Mitchell .   dm - the DM object
3415a7058e45SLawrence Mitchell 
3416a7058e45SLawrence Mitchell     Output Parameter:
3417a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3418a7058e45SLawrence Mitchell 
3419a7058e45SLawrence Mitchell     Level: developer
3420a7058e45SLawrence Mitchell 
34211565f0a7SPatrick Sanan .seealso DMCreateInjection()
3422a7058e45SLawrence Mitchell 
3423a7058e45SLawrence Mitchell @*/
3424a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3425a7058e45SLawrence Mitchell {
34264a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3427a7058e45SLawrence Mitchell   PetscFunctionBegin;
34284a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
34294a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3430a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3431a7058e45SLawrence Mitchell }
3432a7058e45SLawrence Mitchell 
3433748fac09SDmitry Karpeev /*@C
343408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
343508da532bSDmitry Karpeev 
3436d083f849SBarry Smith     Collective on dm
343708da532bSDmitry Karpeev 
343808da532bSDmitry Karpeev     Input Parameter:
343908da532bSDmitry Karpeev +   dm - the DM object
34400298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
344108da532bSDmitry Karpeev 
344208da532bSDmitry Karpeev     Level: developer
344308da532bSDmitry Karpeev 
344474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
344508da532bSDmitry Karpeev 
344608da532bSDmitry Karpeev @*/
344708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
344808da532bSDmitry Karpeev {
344908da532bSDmitry Karpeev   PetscErrorCode ierr;
34505fd66863SKarl Rupp 
345108da532bSDmitry Karpeev   PetscFunctionBegin;
345208da532bSDmitry Karpeev   if (x) {
345308da532bSDmitry Karpeev     if (!dm->x) {
345408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
345508da532bSDmitry Karpeev     }
345608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
34578865f1eaSKarl Rupp   } else if (dm->x) {
345808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
345908da532bSDmitry Karpeev   }
346008da532bSDmitry Karpeev   PetscFunctionReturn(0);
346108da532bSDmitry Karpeev }
346208da532bSDmitry Karpeev 
34630298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3464264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3465264ace61SBarry Smith 
3466264ace61SBarry Smith /*@C
3467264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3468264ace61SBarry Smith 
3469d083f849SBarry Smith   Collective on dm
3470264ace61SBarry Smith 
3471264ace61SBarry Smith   Input Parameters:
3472264ace61SBarry Smith + dm     - The DM object
3473264ace61SBarry Smith - method - The name of the DM type
3474264ace61SBarry Smith 
3475264ace61SBarry Smith   Options Database Key:
3476264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3477264ace61SBarry Smith 
3478264ace61SBarry Smith   Notes:
3479e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3480264ace61SBarry Smith 
3481264ace61SBarry Smith   Level: intermediate
3482264ace61SBarry Smith 
3483264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3484264ace61SBarry Smith @*/
348519fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3486264ace61SBarry Smith {
3487264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3488264ace61SBarry Smith   PetscBool      match;
3489264ace61SBarry Smith   PetscErrorCode ierr;
3490264ace61SBarry Smith 
3491264ace61SBarry Smith   PetscFunctionBegin;
3492264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3493251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3494264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3495264ace61SBarry Smith 
34960f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
34971c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3498ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3499264ace61SBarry Smith 
3500264ace61SBarry Smith   if (dm->ops->destroy) {
3501264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
35020298fd71SBarry Smith     dm->ops->destroy = NULL;
3503264ace61SBarry Smith   }
3504264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3505264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3506264ace61SBarry Smith   PetscFunctionReturn(0);
3507264ace61SBarry Smith }
3508264ace61SBarry Smith 
3509264ace61SBarry Smith /*@C
3510264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3511264ace61SBarry Smith 
3512264ace61SBarry Smith   Not Collective
3513264ace61SBarry Smith 
3514264ace61SBarry Smith   Input Parameter:
3515264ace61SBarry Smith . dm  - The DM
3516264ace61SBarry Smith 
3517264ace61SBarry Smith   Output Parameter:
3518264ace61SBarry Smith . type - The DM type name
3519264ace61SBarry Smith 
3520264ace61SBarry Smith   Level: intermediate
3521264ace61SBarry Smith 
3522264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3523264ace61SBarry Smith @*/
352419fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3525264ace61SBarry Smith {
3526264ace61SBarry Smith   PetscErrorCode ierr;
3527264ace61SBarry Smith 
3528264ace61SBarry Smith   PetscFunctionBegin;
3529264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3530c959eef4SJed Brown   PetscValidPointer(type,2);
3531607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3532264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3533264ace61SBarry Smith   PetscFunctionReturn(0);
3534264ace61SBarry Smith }
3535264ace61SBarry Smith 
353667a56275SMatthew G Knepley /*@C
353767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
353867a56275SMatthew G Knepley 
3539d083f849SBarry Smith   Collective on dm
354067a56275SMatthew G Knepley 
354167a56275SMatthew G Knepley   Input Parameters:
354267a56275SMatthew G Knepley + dm - the DM
354367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
354467a56275SMatthew G Knepley 
354567a56275SMatthew G Knepley   Output Parameter:
354667a56275SMatthew G Knepley . M - pointer to new DM
354767a56275SMatthew G Knepley 
354867a56275SMatthew G Knepley   Notes:
354967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
355067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
355167a56275SMatthew G Knepley   of the input DM.
355267a56275SMatthew G Knepley 
355367a56275SMatthew G Knepley   Level: intermediate
355467a56275SMatthew G Knepley 
355567a56275SMatthew G Knepley .seealso: DMCreate()
355667a56275SMatthew G Knepley @*/
355719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
355867a56275SMatthew G Knepley {
355967a56275SMatthew G Knepley   DM             B;
356067a56275SMatthew G Knepley   char           convname[256];
3561c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
356267a56275SMatthew G Knepley   PetscErrorCode ierr;
356367a56275SMatthew G Knepley 
356467a56275SMatthew G Knepley   PetscFunctionBegin;
356567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
356667a56275SMatthew G Knepley   PetscValidType(dm,1);
356767a56275SMatthew G Knepley   PetscValidPointer(M,3);
3568251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3569c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3570c067b6caSMatthew G. Knepley   if (sametype) {
3571c067b6caSMatthew G. Knepley     *M   = dm;
3572c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3573c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3574c067b6caSMatthew G. Knepley   } else {
35750298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
357667a56275SMatthew G Knepley 
357767a56275SMatthew G Knepley     /*
357867a56275SMatthew G Knepley        Order of precedence:
357967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
358067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
358167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
358267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
358367a56275SMatthew G Knepley        5) Use a really basic converter.
358467a56275SMatthew G Knepley     */
358567a56275SMatthew G Knepley 
358667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3587a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3588a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3589a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3590a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3591a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
35920005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
359367a56275SMatthew G Knepley     if (conv) goto foundconv;
359467a56275SMatthew G Knepley 
359567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
359682f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
359767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3598a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3599a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3600a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3601a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3602a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36030005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
360467a56275SMatthew G Knepley     if (conv) {
3605fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
360667a56275SMatthew G Knepley       goto foundconv;
360767a56275SMatthew G Knepley     }
360867a56275SMatthew G Knepley 
360967a56275SMatthew G Knepley #if 0
361067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
361167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3612fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
361367a56275SMatthew G Knepley     if (conv) goto foundconv;
361467a56275SMatthew G Knepley 
361567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
361667a56275SMatthew G Knepley     if (dm->ops->convert) {
361767a56275SMatthew G Knepley       conv = dm->ops->convert;
361867a56275SMatthew G Knepley     }
361967a56275SMatthew G Knepley     if (conv) goto foundconv;
362067a56275SMatthew G Knepley #endif
362167a56275SMatthew G Knepley 
362267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
362382f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
362467a56275SMatthew G Knepley 
362567a56275SMatthew G Knepley foundconv:
362667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
362767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
362812fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
362990b157c4SStefano Zampini     {
363090b157c4SStefano Zampini       PetscBool             isper;
363112fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
363212fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
363390b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
363490b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
363512fa691eSMatthew G. Knepley     }
363667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
363767a56275SMatthew G Knepley   }
363867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
363967a56275SMatthew G Knepley   PetscFunctionReturn(0);
364067a56275SMatthew G Knepley }
3641264ace61SBarry Smith 
3642264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3643264ace61SBarry Smith 
3644264ace61SBarry Smith /*@C
36451c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
36461c84c290SBarry Smith 
36471c84c290SBarry Smith   Not Collective
36481c84c290SBarry Smith 
36491c84c290SBarry Smith   Input Parameters:
36501c84c290SBarry Smith + name        - The name of a new user-defined creation routine
36511c84c290SBarry Smith - create_func - The creation routine itself
36521c84c290SBarry Smith 
36531c84c290SBarry Smith   Notes:
36541c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
36551c84c290SBarry Smith 
36561c84c290SBarry Smith 
36571c84c290SBarry Smith   Sample usage:
36581c84c290SBarry Smith .vb
3659bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
36601c84c290SBarry Smith .ve
36611c84c290SBarry Smith 
36621c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
36631c84c290SBarry Smith .vb
36641c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
36651c84c290SBarry Smith     DMSetType(DM,"my_da");
36661c84c290SBarry Smith .ve
36671c84c290SBarry Smith    or at runtime via the option
36681c84c290SBarry Smith .vb
36691c84c290SBarry Smith     -da_type my_da
36701c84c290SBarry Smith .ve
3671264ace61SBarry Smith 
3672264ace61SBarry Smith   Level: advanced
36731c84c290SBarry Smith 
3674bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
36751c84c290SBarry Smith 
3676264ace61SBarry Smith @*/
3677bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3678264ace61SBarry Smith {
3679264ace61SBarry Smith   PetscErrorCode ierr;
3680264ace61SBarry Smith 
3681264ace61SBarry Smith   PetscFunctionBegin;
36821d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3683a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3684264ace61SBarry Smith   PetscFunctionReturn(0);
3685264ace61SBarry Smith }
3686264ace61SBarry Smith 
3687b859378eSBarry Smith /*@C
368855849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3689b859378eSBarry Smith 
3690d083f849SBarry Smith   Collective on viewer
3691b859378eSBarry Smith 
3692b859378eSBarry Smith   Input Parameters:
3693b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3694b859378eSBarry Smith            some related function before a call to DMLoad().
3695b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3696b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3697b859378eSBarry Smith 
3698b859378eSBarry Smith    Level: intermediate
3699b859378eSBarry Smith 
3700b859378eSBarry Smith   Notes:
370155849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3702b859378eSBarry Smith 
3703b859378eSBarry Smith   Notes for advanced users:
3704b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3705b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3706b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3707b859378eSBarry Smith   format is
3708b859378eSBarry Smith .vb
3709b859378eSBarry Smith      has not yet been determined
3710b859378eSBarry Smith .ve
3711b859378eSBarry Smith 
3712b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3713b859378eSBarry Smith @*/
3714b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3715b859378eSBarry Smith {
37169331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3717b859378eSBarry Smith   PetscErrorCode ierr;
3718b859378eSBarry Smith 
3719b859378eSBarry Smith   PetscFunctionBegin;
3720b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3721b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3722fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
372332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37249331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
37259331c7a4SMatthew G. Knepley   if (isbinary) {
37269331c7a4SMatthew G. Knepley     PetscInt classid;
37279331c7a4SMatthew G. Knepley     char     type[256];
3728b859378eSBarry Smith 
3729060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37309200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3731060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
373232c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37339331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37349331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37359331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37369331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3737b859378eSBarry Smith   PetscFunctionReturn(0);
3738b859378eSBarry Smith }
3739b859378eSBarry Smith 
37407da65231SMatthew G Knepley /******************************** FEM Support **********************************/
37417da65231SMatthew G Knepley 
3742a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3743a6dfd86eSKarl Rupp {
37441d47ebbbSSatish Balay   PetscInt       f;
37451b30c384SMatthew G Knepley   PetscErrorCode ierr;
37461b30c384SMatthew G Knepley 
37477da65231SMatthew G Knepley   PetscFunctionBegin;
374874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
37491d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
375057622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
37517da65231SMatthew G Knepley   }
37527da65231SMatthew G Knepley   PetscFunctionReturn(0);
37537da65231SMatthew G Knepley }
37547da65231SMatthew G Knepley 
3755a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3756a6dfd86eSKarl Rupp {
37571b30c384SMatthew G Knepley   PetscInt       f, g;
37587da65231SMatthew G Knepley   PetscErrorCode ierr;
37597da65231SMatthew G Knepley 
37607da65231SMatthew G Knepley   PetscFunctionBegin;
376174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
37621d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
376374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
37641d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3765e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
37667da65231SMatthew G Knepley     }
376774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
37687da65231SMatthew G Knepley   }
37697da65231SMatthew G Knepley   PetscFunctionReturn(0);
37707da65231SMatthew G Knepley }
3771e7c4fc90SDmitry Karpeev 
37726113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3773e759306cSMatthew G. Knepley {
37740c5b8624SToby Isaac   PetscInt          localSize, bs;
37750c5b8624SToby Isaac   PetscMPIInt       size;
37760c5b8624SToby Isaac   Vec               x, xglob;
37770c5b8624SToby Isaac   const PetscScalar *xarray;
3778e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3779e759306cSMatthew G. Knepley 
3780e759306cSMatthew G. Knepley   PetscFunctionBegin;
37819852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3782e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3783e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
37846113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
37850c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
37860c5b8624SToby Isaac   if (size > 1) {
37870c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
37880c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
37890c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
37900c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
37910c5b8624SToby Isaac   } else {
37920c5b8624SToby Isaac     xglob = x;
37930c5b8624SToby Isaac   }
37940c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
37950c5b8624SToby Isaac   if (size > 1) {
37960c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
37970c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
37980c5b8624SToby Isaac   }
3799e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3800e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3801e759306cSMatthew G. Knepley }
3802e759306cSMatthew G. Knepley 
380388ed4aceSMatthew G Knepley /*@
3804e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
380588ed4aceSMatthew G Knepley 
380688ed4aceSMatthew G Knepley   Input Parameter:
380788ed4aceSMatthew G Knepley . dm - The DM
380888ed4aceSMatthew G Knepley 
380988ed4aceSMatthew G Knepley   Output Parameter:
381088ed4aceSMatthew G Knepley . section - The PetscSection
381188ed4aceSMatthew G Knepley 
3812e5893cccSMatthew G. Knepley   Options Database Keys:
3813e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3814e5893cccSMatthew G. Knepley 
381588ed4aceSMatthew G Knepley   Level: intermediate
381688ed4aceSMatthew G Knepley 
381788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
381888ed4aceSMatthew G Knepley 
3819e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
382088ed4aceSMatthew G Knepley @*/
3821e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
38220adebc6cSBarry Smith {
3823fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3824fd59a867SMatthew G. Knepley 
382588ed4aceSMatthew G Knepley   PetscFunctionBegin;
382688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
382788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
38282f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
3829e5e52638SMatthew G. Knepley     PetscInt d;
3830e5e52638SMatthew G. Knepley 
3831e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
38322f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3833ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
38342f0f8703SMatthew G. Knepley   }
383588ed4aceSMatthew G Knepley   *section = dm->defaultSection;
383688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
383788ed4aceSMatthew G Knepley }
383888ed4aceSMatthew G Knepley 
383988ed4aceSMatthew G Knepley /*@
3840e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
384188ed4aceSMatthew G Knepley 
384288ed4aceSMatthew G Knepley   Input Parameters:
384388ed4aceSMatthew G Knepley + dm - The DM
384488ed4aceSMatthew G Knepley - section - The PetscSection
384588ed4aceSMatthew G Knepley 
384688ed4aceSMatthew G Knepley   Level: intermediate
384788ed4aceSMatthew G Knepley 
384888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
384988ed4aceSMatthew G Knepley 
3850e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
385188ed4aceSMatthew G Knepley @*/
3852e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
38530adebc6cSBarry Smith {
3854c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3855af122d2aSMatthew G Knepley   PetscInt       f;
385688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
385788ed4aceSMatthew G Knepley 
385888ed4aceSMatthew G Knepley   PetscFunctionBegin;
385988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3860c473ab19SMatthew G. Knepley   if (section) {
38611d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
38621d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3863c473ab19SMatthew G. Knepley   }
386488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
386588ed4aceSMatthew G Knepley   dm->defaultSection = section;
3866c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3867af122d2aSMatthew G Knepley   if (numFields) {
3868af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3869af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
38700f21e855SMatthew G. Knepley       PetscObject disc;
3871af122d2aSMatthew G Knepley       const char *name;
3872af122d2aSMatthew G Knepley 
3873af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
387444a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
38750f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3876af122d2aSMatthew G Knepley     }
3877af122d2aSMatthew G Knepley   }
3878e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
38791d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
388088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
388188ed4aceSMatthew G Knepley }
388288ed4aceSMatthew G Knepley 
38839435951eSToby Isaac /*@
38849435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
38859435951eSToby Isaac 
3886e228b242SToby Isaac   not collective
3887e228b242SToby Isaac 
38889435951eSToby Isaac   Input Parameter:
38899435951eSToby Isaac . dm - The DM
38909435951eSToby Isaac 
38919435951eSToby Isaac   Output Parameter:
38929435951eSToby 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.
38939435951eSToby 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.
38949435951eSToby Isaac 
38959435951eSToby Isaac   Level: advanced
38969435951eSToby Isaac 
38979435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
38989435951eSToby Isaac 
38999435951eSToby Isaac .seealso: DMSetDefaultConstraints()
39009435951eSToby Isaac @*/
39019435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
39029435951eSToby Isaac {
39039435951eSToby Isaac   PetscErrorCode ierr;
39049435951eSToby Isaac 
39059435951eSToby Isaac   PetscFunctionBegin;
39069435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39079435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
390845a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
390945a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
39109435951eSToby Isaac   PetscFunctionReturn(0);
39119435951eSToby Isaac }
39129435951eSToby Isaac 
39139435951eSToby Isaac /*@
39149435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
39159435951eSToby Isaac 
39169435951eSToby 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().
39179435951eSToby Isaac 
39189435951eSToby 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.
39199435951eSToby Isaac 
3920e228b242SToby Isaac   collective on dm
3921e228b242SToby Isaac 
39229435951eSToby Isaac   Input Parameters:
39239435951eSToby Isaac + dm - The DM
3924e228b242SToby 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).
3925e228b242SToby 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).
39269435951eSToby Isaac 
39279435951eSToby Isaac   Level: advanced
39289435951eSToby Isaac 
39299435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
39309435951eSToby Isaac 
39319435951eSToby Isaac .seealso: DMGetDefaultConstraints()
39329435951eSToby Isaac @*/
39339435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
39349435951eSToby Isaac {
3935e228b242SToby Isaac   PetscMPIInt result;
39369435951eSToby Isaac   PetscErrorCode ierr;
39379435951eSToby Isaac 
39389435951eSToby Isaac   PetscFunctionBegin;
39399435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3940e228b242SToby Isaac   if (section) {
3941e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3942e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3943f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3944e228b242SToby Isaac   }
3945e228b242SToby Isaac   if (mat) {
3946e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3947e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3948f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3949e228b242SToby Isaac   }
39509435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
39519435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
39529435951eSToby Isaac   dm->defaultConstraintSection = section;
39539435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
39549435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
39559435951eSToby Isaac   dm->defaultConstraintMat = mat;
39569435951eSToby Isaac   PetscFunctionReturn(0);
39579435951eSToby Isaac }
39589435951eSToby Isaac 
3959497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3960507e4973SMatthew G. Knepley /*
3961507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3962507e4973SMatthew G. Knepley 
3963507e4973SMatthew G. Knepley   Input Parameters:
3964507e4973SMatthew G. Knepley + dm - The DM
3965507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3966507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3967507e4973SMatthew G. Knepley 
3968507e4973SMatthew G. Knepley   Level: intermediate
3969507e4973SMatthew G. Knepley 
3970507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3971507e4973SMatthew G. Knepley */
3972f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3973507e4973SMatthew G. Knepley {
3974507e4973SMatthew G. Knepley   MPI_Comm        comm;
3975507e4973SMatthew G. Knepley   PetscLayout     layout;
3976507e4973SMatthew G. Knepley   const PetscInt *ranges;
3977507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3978507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3979507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3980507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3981507e4973SMatthew G. Knepley 
3982507e4973SMatthew G. Knepley   PetscFunctionBegin;
3983507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3984507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3985507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3986507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3987507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3988507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3989507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3990507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3991507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3992507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3993507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3994507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3995f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3996507e4973SMatthew G. Knepley 
3997507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3998507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3999507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4000507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4001507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4002507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4003507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4004507e4973SMatthew 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;}
4005507e4973SMatthew 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;}
4006507e4973SMatthew G. Knepley     if (gdof < 0) {
4007507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4008507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4009507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4010507e4973SMatthew G. Knepley 
4011507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4012507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4013507e4973SMatthew 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;}
4014507e4973SMatthew G. Knepley       }
4015507e4973SMatthew G. Knepley     }
4016507e4973SMatthew G. Knepley   }
4017507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4018507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4019b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4020507e4973SMatthew G. Knepley   if (!gvalid) {
4021507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4022507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4023507e4973SMatthew G. Knepley   }
4024507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4025507e4973SMatthew G. Knepley }
4026f741bcd2SMatthew G. Knepley #endif
4027507e4973SMatthew G. Knepley 
402888ed4aceSMatthew G Knepley /*@
4029e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
403088ed4aceSMatthew G Knepley 
4031d083f849SBarry Smith   Collective on dm
40328b1ab98fSJed Brown 
403388ed4aceSMatthew G Knepley   Input Parameter:
403488ed4aceSMatthew G Knepley . dm - The DM
403588ed4aceSMatthew G Knepley 
403688ed4aceSMatthew G Knepley   Output Parameter:
403788ed4aceSMatthew G Knepley . section - The PetscSection
403888ed4aceSMatthew G Knepley 
403988ed4aceSMatthew G Knepley   Level: intermediate
404088ed4aceSMatthew G Knepley 
404188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
404288ed4aceSMatthew G Knepley 
4043e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
404488ed4aceSMatthew G Knepley @*/
4045e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
40460adebc6cSBarry Smith {
404788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
404888ed4aceSMatthew G Knepley 
404988ed4aceSMatthew G Knepley   PetscFunctionBegin;
405088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
405188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
405288ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
4053fd59a867SMatthew G. Knepley     PetscSection s;
4054fd59a867SMatthew G. Knepley 
4055e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
4056fd59a867SMatthew 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");
405733907cc2SStefano 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");
405815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
4059cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
4060ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
4061685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
406288ed4aceSMatthew G Knepley   }
406388ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
406488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
406588ed4aceSMatthew G Knepley }
406688ed4aceSMatthew G Knepley 
4067b21d0597SMatthew G Knepley /*@
4068e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4069b21d0597SMatthew G Knepley 
4070b21d0597SMatthew G Knepley   Input Parameters:
4071b21d0597SMatthew G Knepley + dm - The DM
40725080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4073b21d0597SMatthew G Knepley 
4074b21d0597SMatthew G Knepley   Level: intermediate
4075b21d0597SMatthew G Knepley 
4076b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4077b21d0597SMatthew G Knepley 
4078e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
4079b21d0597SMatthew G Knepley @*/
4080e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
40810adebc6cSBarry Smith {
4082b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4083b21d0597SMatthew G Knepley 
4084b21d0597SMatthew G Knepley   PetscFunctionBegin;
4085b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40865080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40871d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
4088b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
4089b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
4090497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4091f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
4092507e4973SMatthew G. Knepley #endif
4093b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4094b21d0597SMatthew G Knepley }
4095b21d0597SMatthew G Knepley 
409688ed4aceSMatthew G Knepley /*@
409788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
409888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
409988ed4aceSMatthew G Knepley 
410088ed4aceSMatthew G Knepley   Input Parameter:
410188ed4aceSMatthew G Knepley . dm - The DM
410288ed4aceSMatthew G Knepley 
410388ed4aceSMatthew G Knepley   Output Parameter:
410488ed4aceSMatthew G Knepley . sf - The PetscSF
410588ed4aceSMatthew G Knepley 
410688ed4aceSMatthew G Knepley   Level: intermediate
410788ed4aceSMatthew G Knepley 
410888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
410988ed4aceSMatthew G Knepley 
411088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
411188ed4aceSMatthew G Knepley @*/
41120adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
41130adebc6cSBarry Smith {
411488ed4aceSMatthew G Knepley   PetscInt       nroots;
411588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
411688ed4aceSMatthew G Knepley 
411788ed4aceSMatthew G Knepley   PetscFunctionBegin;
411888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
411988ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
412033907cc2SStefano Zampini   if (!dm->defaultSF) {
412133907cc2SStefano Zampini     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->defaultSF);CHKERRQ(ierr);
412233907cc2SStefano Zampini   }
41230298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
412488ed4aceSMatthew G Knepley   if (nroots < 0) {
412588ed4aceSMatthew G Knepley     PetscSection section, gSection;
412688ed4aceSMatthew G Knepley 
4127e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
412831ea6d37SMatthew G Knepley     if (section) {
4129e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
413088ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
413131ea6d37SMatthew G Knepley     } else {
41320298fd71SBarry Smith       *sf = NULL;
413331ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
413431ea6d37SMatthew G Knepley     }
413588ed4aceSMatthew G Knepley   }
413688ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
413788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
413888ed4aceSMatthew G Knepley }
413988ed4aceSMatthew G Knepley 
414088ed4aceSMatthew G Knepley /*@
414188ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
414288ed4aceSMatthew G Knepley 
414388ed4aceSMatthew G Knepley   Input Parameters:
414488ed4aceSMatthew G Knepley + dm - The DM
414588ed4aceSMatthew G Knepley - sf - The PetscSF
414688ed4aceSMatthew G Knepley 
414788ed4aceSMatthew G Knepley   Level: intermediate
414888ed4aceSMatthew G Knepley 
414988ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
415088ed4aceSMatthew G Knepley 
415188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
415288ed4aceSMatthew G Knepley @*/
41530adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
41540adebc6cSBarry Smith {
415588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
415688ed4aceSMatthew G Knepley 
415788ed4aceSMatthew G Knepley   PetscFunctionBegin;
415888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
415933907cc2SStefano Zampini   if (sf) {
416088ed4aceSMatthew G Knepley     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
416133907cc2SStefano Zampini     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
416233907cc2SStefano Zampini   }
416388ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
416488ed4aceSMatthew G Knepley   dm->defaultSF = sf;
416588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
416688ed4aceSMatthew G Knepley }
416788ed4aceSMatthew G Knepley 
416888ed4aceSMatthew G Knepley /*@C
416988ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
417088ed4aceSMatthew G Knepley   describing the data layout.
417188ed4aceSMatthew G Knepley 
417288ed4aceSMatthew G Knepley   Input Parameters:
417388ed4aceSMatthew G Knepley + dm - The DM
417488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
417588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
417688ed4aceSMatthew G Knepley 
417788ed4aceSMatthew G Knepley   Level: intermediate
417888ed4aceSMatthew G Knepley 
417988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
418088ed4aceSMatthew G Knepley @*/
418188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
418288ed4aceSMatthew G Knepley {
418382f516ccSBarry Smith   MPI_Comm       comm;
418488ed4aceSMatthew G Knepley   PetscLayout    layout;
418588ed4aceSMatthew G Knepley   const PetscInt *ranges;
418688ed4aceSMatthew G Knepley   PetscInt       *local;
418788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4188ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
418988ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
419088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
419188ed4aceSMatthew G Knepley 
419288ed4aceSMatthew G Knepley   PetscFunctionBegin;
419388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4194367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
419588ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
419688ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
419788ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
419888ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
419988ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
420088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
420188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
420288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
420388ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4204ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
42056636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
420688ed4aceSMatthew G Knepley 
42076636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
42086636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4209235fbf56SMatthew 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));
42106636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
421188ed4aceSMatthew G Knepley   }
4212785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4213785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
421488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
42151f588964SMatthew G Knepley     const PetscInt *cind;
42166636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
421788ed4aceSMatthew G Knepley 
421888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
421988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
422088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
422188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
422288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
42236636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
422488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
42256636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
42266636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
42276636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4228057b4bcdSMatthew 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);
42296636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
42306636e97aSMatthew G Knepley     }
423188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
423288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
423388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
423488ed4aceSMatthew G Knepley     }
423588ed4aceSMatthew G Knepley     if (gdof < 0) {
42366636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
423788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
423888ed4aceSMatthew G Knepley 
423905376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
424031d3f06eSJed Brown         if (r < 0) r = -(r+2);
424105376888SMatthew 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);
424288ed4aceSMatthew G Knepley         remote[l].rank  = r;
424388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
424488ed4aceSMatthew G Knepley       }
424588ed4aceSMatthew G Knepley     } else {
42466636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
424788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
424888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
424988ed4aceSMatthew G Knepley       }
425088ed4aceSMatthew G Knepley     }
425188ed4aceSMatthew G Knepley   }
42526636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
425388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
425488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
425588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
425688ed4aceSMatthew G Knepley }
4257af122d2aSMatthew G Knepley 
4258b21d0597SMatthew G Knepley /*@
4259b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4260b21d0597SMatthew G Knepley 
4261b21d0597SMatthew G Knepley   Input Parameter:
4262b21d0597SMatthew G Knepley . dm - The DM
4263b21d0597SMatthew G Knepley 
4264b21d0597SMatthew G Knepley   Output Parameter:
4265b21d0597SMatthew G Knepley . sf - The PetscSF
4266b21d0597SMatthew G Knepley 
4267b21d0597SMatthew G Knepley   Level: intermediate
4268b21d0597SMatthew G Knepley 
4269b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4270b21d0597SMatthew G Knepley 
4271057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4272b21d0597SMatthew G Knepley @*/
42730adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
42740adebc6cSBarry Smith {
4275b21d0597SMatthew G Knepley   PetscFunctionBegin;
4276b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4277b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4278b21d0597SMatthew G Knepley   *sf = dm->sf;
4279b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4280b21d0597SMatthew G Knepley }
4281b21d0597SMatthew G Knepley 
4282057b4bcdSMatthew G Knepley /*@
4283057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4284057b4bcdSMatthew G Knepley 
4285057b4bcdSMatthew G Knepley   Input Parameters:
4286057b4bcdSMatthew G Knepley + dm - The DM
4287057b4bcdSMatthew G Knepley - sf - The PetscSF
4288057b4bcdSMatthew G Knepley 
4289057b4bcdSMatthew G Knepley   Level: intermediate
4290057b4bcdSMatthew G Knepley 
4291057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4292057b4bcdSMatthew G Knepley @*/
42930adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
42940adebc6cSBarry Smith {
4295057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4296057b4bcdSMatthew G Knepley 
4297057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4298057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429933907cc2SStefano Zampini   if (sf) {
430033907cc2SStefano Zampini     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4301057b4bcdSMatthew G Knepley     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
430233907cc2SStefano Zampini   }
430333907cc2SStefano Zampini   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4304057b4bcdSMatthew G Knepley   dm->sf = sf;
4305057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4306057b4bcdSMatthew G Knepley }
4307057b4bcdSMatthew G Knepley 
430834aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
430934aa8a36SMatthew G. Knepley {
431034aa8a36SMatthew G. Knepley   PetscClassId   id;
431134aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
431234aa8a36SMatthew G. Knepley 
431334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
431434aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
431534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
431634aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
431734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
431834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
431917c1d62eSMatthew G. Knepley   } else {
432017c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
432134aa8a36SMatthew G. Knepley   }
432234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
432334aa8a36SMatthew G. Knepley }
432434aa8a36SMatthew G. Knepley 
432544a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
432644a7f3ddSMatthew G. Knepley {
432744a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
432844a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
432944a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
433044a7f3ddSMatthew G. Knepley 
433144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
433244a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
433344a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
433444a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
433544a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
433644a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
433744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
433844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
433944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
434044a7f3ddSMatthew G. Knepley }
434144a7f3ddSMatthew G. Knepley 
434244a7f3ddSMatthew G. Knepley /*@
434344a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
434444a7f3ddSMatthew G. Knepley 
4345d083f849SBarry Smith   Logically collective on dm
434644a7f3ddSMatthew G. Knepley 
434744a7f3ddSMatthew G. Knepley   Input Parameter:
434844a7f3ddSMatthew G. Knepley . dm - The DM
434944a7f3ddSMatthew G. Knepley 
435044a7f3ddSMatthew G. Knepley   Level: intermediate
435144a7f3ddSMatthew G. Knepley 
435244a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
435344a7f3ddSMatthew G. Knepley @*/
435444a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
435544a7f3ddSMatthew G. Knepley {
435644a7f3ddSMatthew G. Knepley   PetscInt       f;
435744a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
435844a7f3ddSMatthew G. Knepley 
435944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
436044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
436144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
436244a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
436344a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
436444a7f3ddSMatthew G. Knepley   }
436544a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
436644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
436744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
436844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
436944a7f3ddSMatthew G. Knepley }
437044a7f3ddSMatthew G. Knepley 
4371689b5837SMatthew G. Knepley /*@
4372689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4373689b5837SMatthew G. Knepley 
4374689b5837SMatthew G. Knepley   Not collective
4375689b5837SMatthew G. Knepley 
4376689b5837SMatthew G. Knepley   Input Parameter:
4377689b5837SMatthew G. Knepley . dm - The DM
4378689b5837SMatthew G. Knepley 
4379689b5837SMatthew G. Knepley   Output Parameter:
4380689b5837SMatthew G. Knepley . Nf - The number of fields
4381689b5837SMatthew G. Knepley 
4382689b5837SMatthew G. Knepley   Level: intermediate
4383689b5837SMatthew G. Knepley 
4384689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4385689b5837SMatthew G. Knepley @*/
43860f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
43870f21e855SMatthew G. Knepley {
43880f21e855SMatthew G. Knepley   PetscFunctionBegin;
43890f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
439044a7f3ddSMatthew G. Knepley   PetscValidPointer(numFields, 2);
439144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4392af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4393af122d2aSMatthew G Knepley }
4394af122d2aSMatthew G Knepley 
4395689b5837SMatthew G. Knepley /*@
4396689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4397689b5837SMatthew G. Knepley 
4398d083f849SBarry Smith   Logically collective on dm
4399689b5837SMatthew G. Knepley 
4400689b5837SMatthew G. Knepley   Input Parameters:
4401689b5837SMatthew G. Knepley + dm - The DM
4402689b5837SMatthew G. Knepley - Nf - The number of fields
4403689b5837SMatthew G. Knepley 
4404689b5837SMatthew G. Knepley   Level: intermediate
4405689b5837SMatthew G. Knepley 
4406689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4407689b5837SMatthew G. Knepley @*/
4408af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4409af122d2aSMatthew G Knepley {
44100f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4411af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4412af122d2aSMatthew G Knepley 
4413af122d2aSMatthew G Knepley   PetscFunctionBegin;
4414af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
441544a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
44160f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
44170f21e855SMatthew G. Knepley     PetscContainer obj;
44180f21e855SMatthew G. Knepley 
44190f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
442044a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
44210f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4422af122d2aSMatthew G Knepley   }
4423af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4424af122d2aSMatthew G Knepley }
4425af122d2aSMatthew G Knepley 
4426c1929be8SMatthew G. Knepley /*@
4427c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4428c1929be8SMatthew G. Knepley 
4429c1929be8SMatthew G. Knepley   Not collective
4430c1929be8SMatthew G. Knepley 
4431c1929be8SMatthew G. Knepley   Input Parameters:
4432c1929be8SMatthew G. Knepley + dm - The DM
4433c1929be8SMatthew G. Knepley - f  - The field number
4434c1929be8SMatthew G. Knepley 
443544a7f3ddSMatthew G. Knepley   Output Parameters:
443644a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
443744a7f3ddSMatthew G. Knepley - field - The discretization object
4438c1929be8SMatthew G. Knepley 
443944a7f3ddSMatthew G. Knepley   Level: intermediate
4440c1929be8SMatthew G. Knepley 
444144a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4442c1929be8SMatthew G. Knepley @*/
444344a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4444af122d2aSMatthew G Knepley {
4445af122d2aSMatthew G Knepley   PetscFunctionBegin;
4446af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
444744a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
444844a7f3ddSMatthew 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);
444944a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
445044a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4451decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4452decb47aaSMatthew G. Knepley }
4453decb47aaSMatthew G. Knepley 
4454c1929be8SMatthew G. Knepley /*@
4455c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4456c1929be8SMatthew G. Knepley 
4457d083f849SBarry Smith   Logically collective on dm
4458c1929be8SMatthew G. Knepley 
4459c1929be8SMatthew G. Knepley   Input Parameters:
4460c1929be8SMatthew G. Knepley + dm    - The DM
4461c1929be8SMatthew G. Knepley . f     - The field number
446244a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4463c1929be8SMatthew G. Knepley - field - The discretization object
4464c1929be8SMatthew G. Knepley 
446544a7f3ddSMatthew G. Knepley   Level: intermediate
4466c1929be8SMatthew G. Knepley 
446744a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4468c1929be8SMatthew G. Knepley @*/
446944a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4470decb47aaSMatthew G. Knepley {
4471decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4472decb47aaSMatthew G. Knepley 
4473decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4474decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4475e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
447644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4477e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
447844a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
447944a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
448044a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
448144a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
448244a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
448344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
448444a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
448534aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
44862df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
448744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
448844a7f3ddSMatthew G. Knepley }
448944a7f3ddSMatthew G. Knepley 
449044a7f3ddSMatthew G. Knepley /*@
449144a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
449244a7f3ddSMatthew G. Knepley 
4493d083f849SBarry Smith   Logically collective on dm
449444a7f3ddSMatthew G. Knepley 
449544a7f3ddSMatthew G. Knepley   Input Parameters:
449644a7f3ddSMatthew G. Knepley + dm    - The DM
449744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
449844a7f3ddSMatthew G. Knepley - field - The discretization object
449944a7f3ddSMatthew G. Knepley 
450044a7f3ddSMatthew G. Knepley   Level: intermediate
450144a7f3ddSMatthew G. Knepley 
450244a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
450344a7f3ddSMatthew G. Knepley @*/
450444a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
450544a7f3ddSMatthew G. Knepley {
450644a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
450744a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
450844a7f3ddSMatthew G. Knepley 
450944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
451044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
451144a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
451244a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
451344a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
451444a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
451544a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
451644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
451744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
451834aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
45192df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4520af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4521af122d2aSMatthew G Knepley }
45226636e97aSMatthew G Knepley 
4523e5e52638SMatthew G. Knepley /*@
4524e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4525e5e52638SMatthew G. Knepley 
4526d083f849SBarry Smith   Collective on dm
4527e5e52638SMatthew G. Knepley 
4528e5e52638SMatthew G. Knepley   Input Parameter:
4529e5e52638SMatthew G. Knepley . dm - The DM
4530e5e52638SMatthew G. Knepley 
4531e5e52638SMatthew G. Knepley   Output Parameter:
4532e5e52638SMatthew G. Knepley . newdm - The DM
4533e5e52638SMatthew G. Knepley 
4534e5e52638SMatthew G. Knepley   Level: advanced
4535e5e52638SMatthew G. Knepley 
4536e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4537e5e52638SMatthew G. Knepley @*/
4538e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4539e5e52638SMatthew G. Knepley {
4540e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4541e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4542e5e52638SMatthew G. Knepley 
4543e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4544e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4545e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4546e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4547e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4548e5e52638SMatthew G. Knepley     DMLabel     label;
4549e5e52638SMatthew G. Knepley     PetscObject field;
455034aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4551e5e52638SMatthew G. Knepley 
4552e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4553e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
455434aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
455534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
455634aa8a36SMatthew G. Knepley   }
455734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
455834aa8a36SMatthew G. Knepley }
455934aa8a36SMatthew G. Knepley 
456034aa8a36SMatthew G. Knepley /*@
456134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
456234aa8a36SMatthew G. Knepley 
456334aa8a36SMatthew G. Knepley   Not collective
456434aa8a36SMatthew G. Knepley 
456534aa8a36SMatthew G. Knepley   Input Parameters:
456634aa8a36SMatthew G. Knepley + dm - The DM object
456734aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
456834aa8a36SMatthew G. Knepley 
456934aa8a36SMatthew G. Knepley   Output Parameter:
457034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
457134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
457234aa8a36SMatthew G. Knepley 
457334aa8a36SMatthew G. Knepley   Notes:
457434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
457534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
457634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4577979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
457834aa8a36SMatthew G. Knepley 
457934aa8a36SMatthew G. Knepley   Level: developer
458034aa8a36SMatthew G. Knepley 
458134aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
458234aa8a36SMatthew G. Knepley @*/
458334aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
458434aa8a36SMatthew G. Knepley {
458534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
458634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
458734aa8a36SMatthew G. Knepley   if (useCone) PetscValidPointer(useCone, 3);
458834aa8a36SMatthew G. Knepley   if (useClosure) PetscValidPointer(useClosure, 4);
458934aa8a36SMatthew G. Knepley   if (f < 0) {
459034aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
459134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
459234aa8a36SMatthew G. Knepley   } else {
459334aa8a36SMatthew G. Knepley     PetscInt       Nf;
459434aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
459534aa8a36SMatthew G. Knepley 
459634aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
459734aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
459834aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
459934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
460034aa8a36SMatthew G. Knepley   }
460134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
460234aa8a36SMatthew G. Knepley }
460334aa8a36SMatthew G. Knepley 
460434aa8a36SMatthew G. Knepley /*@
460534aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
460634aa8a36SMatthew G. Knepley 
460734aa8a36SMatthew G. Knepley   Not collective
460834aa8a36SMatthew G. Knepley 
460934aa8a36SMatthew G. Knepley   Input Parameters:
461034aa8a36SMatthew G. Knepley + dm         - The DM object
461134aa8a36SMatthew G. Knepley . f          - The field number
461234aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
461334aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
461434aa8a36SMatthew G. Knepley 
461534aa8a36SMatthew G. Knepley   Notes:
461634aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
461734aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
461834aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4619979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
462034aa8a36SMatthew G. Knepley 
462134aa8a36SMatthew G. Knepley   Level: developer
462234aa8a36SMatthew G. Knepley 
462334aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
462434aa8a36SMatthew G. Knepley @*/
462534aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
462634aa8a36SMatthew G. Knepley {
462734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
462834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462934aa8a36SMatthew G. Knepley   if (f < 0) {
463034aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
463134aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
463234aa8a36SMatthew G. Knepley   } else {
463334aa8a36SMatthew G. Knepley     PetscInt       Nf;
463434aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
463534aa8a36SMatthew G. Knepley 
463634aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
463734aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
463834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
463934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4640e5e52638SMatthew G. Knepley   }
4641e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4642e5e52638SMatthew G. Knepley }
4643e5e52638SMatthew G. Knepley 
4644b0441da4SMatthew G. Knepley /*@
4645b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4646b0441da4SMatthew G. Knepley 
4647b0441da4SMatthew G. Knepley   Not collective
4648b0441da4SMatthew G. Knepley 
4649b0441da4SMatthew G. Knepley   Input Parameters:
4650b0441da4SMatthew G. Knepley . dm - The DM object
4651b0441da4SMatthew G. Knepley 
4652b0441da4SMatthew G. Knepley   Output Parameter:
4653b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4654b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4655b0441da4SMatthew G. Knepley 
4656b0441da4SMatthew G. Knepley   Notes:
4657b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4658b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4659b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4660b0441da4SMatthew G. Knepley 
4661b0441da4SMatthew G. Knepley   Level: developer
4662b0441da4SMatthew G. Knepley 
4663b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4664b0441da4SMatthew G. Knepley @*/
4665b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4666b0441da4SMatthew G. Knepley {
4667b0441da4SMatthew G. Knepley   PetscInt       Nf;
4668b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4669b0441da4SMatthew G. Knepley 
4670b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4671b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4672b0441da4SMatthew G. Knepley   if (useCone) PetscValidPointer(useCone, 3);
4673b0441da4SMatthew G. Knepley   if (useClosure) PetscValidPointer(useClosure, 4);
4674b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4675b0441da4SMatthew G. Knepley   if (!Nf) {
4676b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4677b0441da4SMatthew G. Knepley   } else {
4678b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4679b0441da4SMatthew G. Knepley   }
4680b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4681b0441da4SMatthew G. Knepley }
4682b0441da4SMatthew G. Knepley 
4683b0441da4SMatthew G. Knepley /*@
4684b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4685b0441da4SMatthew G. Knepley 
4686b0441da4SMatthew G. Knepley   Not collective
4687b0441da4SMatthew G. Knepley 
4688b0441da4SMatthew G. Knepley   Input Parameters:
4689b0441da4SMatthew G. Knepley + dm         - The DM object
4690b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4691b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4692b0441da4SMatthew G. Knepley 
4693b0441da4SMatthew G. Knepley   Notes:
4694b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4695b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4696b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4697b0441da4SMatthew G. Knepley 
4698b0441da4SMatthew G. Knepley   Level: developer
4699b0441da4SMatthew G. Knepley 
4700b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4701b0441da4SMatthew G. Knepley @*/
4702b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4703b0441da4SMatthew G. Knepley {
4704b0441da4SMatthew G. Knepley   PetscInt       Nf;
4705b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4706b0441da4SMatthew G. Knepley 
4707b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4708b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4709b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4710b0441da4SMatthew G. Knepley   if (!Nf) {
4711b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4712b0441da4SMatthew G. Knepley   } else {
4713b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4714e5e52638SMatthew G. Knepley   }
4715e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4716e5e52638SMatthew G. Knepley }
4717e5e52638SMatthew G. Knepley 
4718e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4719e5e52638SMatthew G. Knepley {
4720e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4721e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4722e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4723e5e52638SMatthew G. Knepley 
4724e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4725e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4726e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4727e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4728b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4729e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4730e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4731e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4732e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4733e5e52638SMatthew G. Knepley }
4734e5e52638SMatthew G. Knepley 
4735e5e52638SMatthew G. Knepley /*@
4736e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4737e5e52638SMatthew G. Knepley 
4738e5e52638SMatthew G. Knepley   Not collective
4739e5e52638SMatthew G. Knepley 
4740e5e52638SMatthew G. Knepley   Input Parameter:
4741e5e52638SMatthew G. Knepley . dm - The DM
4742e5e52638SMatthew G. Knepley 
4743e5e52638SMatthew G. Knepley   Output Parameter:
4744e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4745e5e52638SMatthew G. Knepley 
4746e5e52638SMatthew G. Knepley   Level: intermediate
4747e5e52638SMatthew G. Knepley 
4748e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4749e5e52638SMatthew G. Knepley @*/
4750e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4751e5e52638SMatthew G. Knepley {
4752e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4753e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4754e5e52638SMatthew G. Knepley   PetscValidPointer(Nds, 2);
4755e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4756e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4757e5e52638SMatthew G. Knepley }
4758e5e52638SMatthew G. Knepley 
4759e5e52638SMatthew G. Knepley /*@
4760e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4761e5e52638SMatthew G. Knepley 
4762d083f849SBarry Smith   Logically collective on dm
4763e5e52638SMatthew G. Knepley 
4764e5e52638SMatthew G. Knepley   Input Parameter:
4765e5e52638SMatthew G. Knepley . dm - The DM
4766e5e52638SMatthew G. Knepley 
4767e5e52638SMatthew G. Knepley   Level: intermediate
4768e5e52638SMatthew G. Knepley 
4769e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4770e5e52638SMatthew G. Knepley @*/
4771e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4772e5e52638SMatthew G. Knepley {
4773e5e52638SMatthew G. Knepley   PetscInt       s;
4774e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4775e5e52638SMatthew G. Knepley 
4776e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4777e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4778e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4779e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4780e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4781b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4782e5e52638SMatthew G. Knepley   }
4783e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4784e5e52638SMatthew G. Knepley   dm->probs = NULL;
4785e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4786e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4787e5e52638SMatthew G. Knepley }
4788e5e52638SMatthew G. Knepley 
4789e5e52638SMatthew G. Knepley /*@
4790e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4791e5e52638SMatthew G. Knepley 
4792e5e52638SMatthew G. Knepley   Not collective
4793e5e52638SMatthew G. Knepley 
4794e5e52638SMatthew G. Knepley   Input Parameter:
4795e5e52638SMatthew G. Knepley . dm    - The DM
4796e5e52638SMatthew G. Knepley 
4797e5e52638SMatthew G. Knepley   Output Parameter:
4798e5e52638SMatthew G. Knepley . prob - The default PetscDS
4799e5e52638SMatthew G. Knepley 
4800e5e52638SMatthew G. Knepley   Level: intermediate
4801e5e52638SMatthew G. Knepley 
4802e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4803e5e52638SMatthew G. Knepley @*/
4804e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4805e5e52638SMatthew G. Knepley {
4806b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4807b0143b4dSMatthew G. Knepley 
4808e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4809e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4810e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4811b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4812b0143b4dSMatthew G. Knepley     PetscDS ds;
4813b0143b4dSMatthew G. Knepley 
4814b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4815b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4816b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4817b0143b4dSMatthew G. Knepley   }
4818b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4819e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4820e5e52638SMatthew G. Knepley }
4821e5e52638SMatthew G. Knepley 
4822e5e52638SMatthew G. Knepley /*@
4823e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4824e5e52638SMatthew G. Knepley 
4825e5e52638SMatthew G. Knepley   Not collective
4826e5e52638SMatthew G. Knepley 
4827e5e52638SMatthew G. Knepley   Input Parameters:
4828e5e52638SMatthew G. Knepley + dm    - The DM
4829e5e52638SMatthew G. Knepley - point - Cell for the DS
4830e5e52638SMatthew G. Knepley 
4831e5e52638SMatthew G. Knepley   Output Parameter:
4832e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4833e5e52638SMatthew G. Knepley 
4834e5e52638SMatthew G. Knepley   Level: developer
4835e5e52638SMatthew G. Knepley 
4836b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
4837e5e52638SMatthew G. Knepley @*/
4838e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4839e5e52638SMatthew G. Knepley {
4840e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
4841e5e52638SMatthew G. Knepley   PetscInt       s;
4842e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4843e5e52638SMatthew G. Knepley 
4844e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4845e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4846e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
4847e5e52638SMatthew G. Knepley   *prob = NULL;
4848e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4849e5e52638SMatthew G. Knepley     PetscInt val;
4850e5e52638SMatthew G. Knepley 
4851e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
4852e5e52638SMatthew G. Knepley     else {
4853e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
4854e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
4855e5e52638SMatthew G. Knepley     }
4856e5e52638SMatthew G. Knepley   }
4857e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
4858e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4859e5e52638SMatthew G. Knepley }
4860e5e52638SMatthew G. Knepley 
4861e5e52638SMatthew G. Knepley /*@
4862e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
4863e5e52638SMatthew G. Knepley 
4864e5e52638SMatthew G. Knepley   Not collective
4865e5e52638SMatthew G. Knepley 
4866e5e52638SMatthew G. Knepley   Input Parameters:
4867e5e52638SMatthew G. Knepley + dm    - The DM
4868e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
4869e5e52638SMatthew G. Knepley 
4870b3cf3223SMatthew G. Knepley   Output Parameters:
4871b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
4872b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
4873e5e52638SMatthew G. Knepley 
4874e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
4875e5e52638SMatthew G. Knepley 
4876e5e52638SMatthew G. Knepley   Level: advanced
4877e5e52638SMatthew G. Knepley 
4878e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4879e5e52638SMatthew G. Knepley @*/
4880b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
4881e5e52638SMatthew G. Knepley {
4882e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
4883e5e52638SMatthew G. Knepley 
4884e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4885e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4886e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4887b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
4888b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
4889e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4890b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
4891b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
4892b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
4893b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
4894b3cf3223SMatthew G. Knepley     }
4895e5e52638SMatthew G. Knepley   }
48962df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
4897e5e52638SMatthew G. Knepley }
4898e5e52638SMatthew G. Knepley 
4899e5e52638SMatthew G. Knepley /*@
4900e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
4901e5e52638SMatthew G. Knepley 
4902e5e52638SMatthew G. Knepley   Not collective
4903e5e52638SMatthew G. Knepley 
4904e5e52638SMatthew G. Knepley   Input Parameters:
4905e5e52638SMatthew G. Knepley + dm  - The DM
4906e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
4907e5e52638SMatthew G. Knepley 
4908e5e52638SMatthew G. Knepley   Output Parameters:
4909b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
4910b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
4911b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
4912e5e52638SMatthew G. Knepley 
4913e5e52638SMatthew G. Knepley   Level: advanced
4914e5e52638SMatthew G. Knepley 
4915e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4916e5e52638SMatthew G. Knepley @*/
4917b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
4918e5e52638SMatthew G. Knepley {
4919e5e52638SMatthew G. Knepley   PetscInt       Nds;
4920e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4921e5e52638SMatthew G. Knepley 
4922e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4923e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4924e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4925e5e52638SMatthew 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);
4926e5e52638SMatthew G. Knepley   if (label) {
4927e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
4928e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
4929e5e52638SMatthew G. Knepley   }
4930b3cf3223SMatthew G. Knepley   if (fields) {
4931b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
4932b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
4933b3cf3223SMatthew G. Knepley   }
4934e5e52638SMatthew G. Knepley   if (ds) {
4935b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
4936e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
4937e5e52638SMatthew G. Knepley   }
4938e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4939e5e52638SMatthew G. Knepley }
4940e5e52638SMatthew G. Knepley 
4941e5e52638SMatthew G. Knepley /*@
4942e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
4943e5e52638SMatthew G. Knepley 
4944d083f849SBarry Smith   Collective on dm
4945e5e52638SMatthew G. Knepley 
4946e5e52638SMatthew G. Knepley   Input Parameters:
4947e5e52638SMatthew G. Knepley + dm     - The DM
4948e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
4949b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
4950e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
4951e5e52638SMatthew G. Knepley 
4952b3cf3223SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM. If DS is replaced,
4953b3cf3223SMatthew G. Knepley   the fields argument is ignored.
4954e5e52638SMatthew G. Knepley 
4955e5e52638SMatthew G. Knepley   Level: advanced
4956e5e52638SMatthew G. Knepley 
4957e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
4958e5e52638SMatthew G. Knepley @*/
4959b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
4960e5e52638SMatthew G. Knepley {
4961e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4962e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4963e5e52638SMatthew G. Knepley 
4964e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4965e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4966e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4967e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
4968e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4969e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
4970b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4971e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
4972e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
4973e5e52638SMatthew G. Knepley     }
4974e5e52638SMatthew G. Knepley   }
49751b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
4976e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4977b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
4978e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
4979e5e52638SMatthew G. Knepley   if (!label) {
4980e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
4981e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
4982e5e52638SMatthew G. Knepley     Nds = 0;
4983e5e52638SMatthew G. Knepley   }
4984e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
4985b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
4986e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
4987e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4988e5e52638SMatthew G. Knepley }
4989e5e52638SMatthew G. Knepley 
4990e5e52638SMatthew G. Knepley /*@
4991e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
4992e5e52638SMatthew G. Knepley 
4993d083f849SBarry Smith   Collective on dm
4994e5e52638SMatthew G. Knepley 
4995e5e52638SMatthew G. Knepley   Input Parameter:
4996e5e52638SMatthew G. Knepley . dm - The DM
4997e5e52638SMatthew G. Knepley 
4998e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
4999e5e52638SMatthew G. Knepley 
5000e5e52638SMatthew G. Knepley   Level: intermediate
5001e5e52638SMatthew G. Knepley 
5002e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5003e5e52638SMatthew G. Knepley @*/
5004e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5005e5e52638SMatthew G. Knepley {
5006e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5007e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5008b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5009e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5010e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5011e5e52638SMatthew G. Knepley 
5012e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5013e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5014e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5015e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5016e5e52638SMatthew G. Knepley    1) NULL
5017e5e52638SMatthew G. Knepley    2) Hybrid cells
5018e5e52638SMatthew G. Knepley   */
5019e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5020e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5021e5e52638SMatthew G. Knepley   /* Create default DS */
5022b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
50232df9ee95SMatthew G. Knepley   if (!prob) {
5024b3cf3223SMatthew G. Knepley     IS        fields;
5025b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5026b3cf3223SMatthew G. Knepley 
5027b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5028b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5029b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
503088f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
503188f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
503288f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
503388f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
503488f0c812SMatthew G. Knepley 
50352df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5036b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
50372df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5038b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5039b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
50402df9ee95SMatthew G. Knepley   }
5041e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5042e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5043b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5044e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5045e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5046e5e52638SMatthew G. Knepley 
5047e5e52638SMatthew G. Knepley     if (label) {
50480122748bSMatthew G. Knepley       DM        plex;
5049a2d47024SMatthew G. Knepley       IS        fields;
5050a2d47024SMatthew G. Knepley       PetscInt *fld;
50510122748bSMatthew G. Knepley       PetscInt  depth, pMax[4];
50520122748bSMatthew G. Knepley 
50530122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
50540122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
50550122748bSMatthew 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);
50560122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
50570122748bSMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5059e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5060e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5061a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5062a2d47024SMatthew G. Knepley       fld[0] = f;
5063a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5064a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5065a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5066a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5067a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5068a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5069e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5070e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5071e5e52638SMatthew G. Knepley       break;
5072e5e52638SMatthew G. Knepley     }
5073e5e52638SMatthew G. Knepley   }
5074e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5075b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5076e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5077e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5078e5e52638SMatthew G. Knepley 
5079e5e52638SMatthew G. Knepley     if (!label) {
5080e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5081e5e52638SMatthew G. Knepley       if (probh) {
5082e5e52638SMatthew G. Knepley         PetscFE subfe;
5083e5e52638SMatthew G. Knepley 
5084e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5085e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5086e5e52638SMatthew G. Knepley       }
5087e5e52638SMatthew G. Knepley     } else {
5088e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5089e5e52638SMatthew G. Knepley     }
5090e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5091e5e52638SMatthew G. Knepley     {
5092e5e52638SMatthew G. Knepley       PetscClassId id;
5093e5e52638SMatthew G. Knepley 
5094e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5095e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5096e5e52638SMatthew G. Knepley     }
5097e5e52638SMatthew G. Knepley   }
5098e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5099e5e52638SMatthew G. Knepley   /* Setup DSes */
5100e5e52638SMatthew G. Knepley   if (doSetup) {
5101e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5102e5e52638SMatthew G. Knepley   }
5103e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5104e5e52638SMatthew G. Knepley }
5105e5e52638SMatthew G. Knepley 
5106e5e52638SMatthew G. Knepley /*@
5107e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5108e5e52638SMatthew G. Knepley 
5109d083f849SBarry Smith   Collective on dm
5110e5e52638SMatthew G. Knepley 
5111e5e52638SMatthew G. Knepley   Input Parameter:
5112e5e52638SMatthew G. Knepley . dm - The DM
5113e5e52638SMatthew G. Knepley 
5114e5e52638SMatthew G. Knepley   Output Parameter:
5115e5e52638SMatthew G. Knepley . newdm - The DM
5116e5e52638SMatthew G. Knepley 
5117e5e52638SMatthew G. Knepley   Level: advanced
5118e5e52638SMatthew G. Knepley 
5119e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5120e5e52638SMatthew G. Knepley @*/
5121e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5122e5e52638SMatthew G. Knepley {
5123e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5124e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5125e5e52638SMatthew G. Knepley 
5126e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5127e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5128e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5129e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5130e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5131e5e52638SMatthew G. Knepley     DMLabel label;
5132b3cf3223SMatthew G. Knepley     IS      fields;
5133e5e52638SMatthew G. Knepley     PetscDS ds;
5134e5e52638SMatthew G. Knepley 
5135b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5136b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5137e5e52638SMatthew G. Knepley   }
5138e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5139e5e52638SMatthew G. Knepley }
5140e5e52638SMatthew G. Knepley 
5141e5e52638SMatthew G. Knepley /*@
5142e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5143e5e52638SMatthew G. Knepley 
5144d083f849SBarry Smith   Collective on dm
5145e5e52638SMatthew G. Knepley 
5146e5e52638SMatthew G. Knepley   Input Parameter:
5147e5e52638SMatthew G. Knepley . dm - The DM
5148e5e52638SMatthew G. Knepley 
5149e5e52638SMatthew G. Knepley   Output Parameter:
5150e5e52638SMatthew G. Knepley . newdm - The DM
5151e5e52638SMatthew G. Knepley 
5152e5e52638SMatthew G. Knepley   Level: advanced
5153e5e52638SMatthew G. Knepley 
5154e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5155e5e52638SMatthew G. Knepley @*/
5156e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5157e5e52638SMatthew G. Knepley {
5158e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5159e5e52638SMatthew G. Knepley 
5160e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5161e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5162e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5163e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5164e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5165e5e52638SMatthew G. Knepley }
5166e5e52638SMatthew G. Knepley 
5167b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5168b64e0483SPeter Brune {
5169b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5170b64e0483SPeter Brune   PetscErrorCode ierr;
5171b64e0483SPeter Brune   Vec coords,ccoords;
51726dbf9973SLawrence Mitchell   Mat inject;
5173b64e0483SPeter Brune   PetscFunctionBegin;
5174b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5175b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5176b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5177b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5178b64e0483SPeter Brune   if (coords && !ccoords) {
5179b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
51806668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
51816dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
51822adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
51836dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5184b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5185b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5186b64e0483SPeter Brune   }
5187b64e0483SPeter Brune   PetscFunctionReturn(0);
5188b64e0483SPeter Brune }
5189b64e0483SPeter Brune 
519003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
519103dadc2fSPeter Brune {
519203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
519303dadc2fSPeter Brune   PetscErrorCode ierr;
519403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
519503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
519603dadc2fSPeter Brune   PetscFunctionBegin;
519703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
519803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
519903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
520003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
520103dadc2fSPeter Brune   if (coords && !ccoords) {
520203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
52036668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
520403dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
520524640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
520603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
520703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
520803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
520903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
521003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
521103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
521203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
521303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
521403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
521503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
521603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
521703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
521803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
521903dadc2fSPeter Brune   }
522003dadc2fSPeter Brune   PetscFunctionReturn(0);
522103dadc2fSPeter Brune }
522203dadc2fSPeter Brune 
5223c73cfb54SMatthew G. Knepley /*@
5224c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5225c73cfb54SMatthew G. Knepley 
5226c73cfb54SMatthew G. Knepley   Not collective
5227c73cfb54SMatthew G. Knepley 
5228c73cfb54SMatthew G. Knepley   Input Parameter:
5229c73cfb54SMatthew G. Knepley . dm - The DM
5230c73cfb54SMatthew G. Knepley 
5231c73cfb54SMatthew G. Knepley   Output Parameter:
5232c73cfb54SMatthew G. Knepley . dim - The topological dimension
5233c73cfb54SMatthew G. Knepley 
5234c73cfb54SMatthew G. Knepley   Level: beginner
5235c73cfb54SMatthew G. Knepley 
5236c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5237c73cfb54SMatthew G. Knepley @*/
5238c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5239c73cfb54SMatthew G. Knepley {
5240c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5241c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5242c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
5243c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5244c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5245c73cfb54SMatthew G. Knepley }
5246c73cfb54SMatthew G. Knepley 
5247c73cfb54SMatthew G. Knepley /*@
5248c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5249c73cfb54SMatthew G. Knepley 
5250c73cfb54SMatthew G. Knepley   Collective on dm
5251c73cfb54SMatthew G. Knepley 
5252c73cfb54SMatthew G. Knepley   Input Parameters:
5253c73cfb54SMatthew G. Knepley + dm - The DM
5254c73cfb54SMatthew G. Knepley - dim - The topological dimension
5255c73cfb54SMatthew G. Knepley 
5256c73cfb54SMatthew G. Knepley   Level: beginner
5257c73cfb54SMatthew G. Knepley 
5258c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5259c73cfb54SMatthew G. Knepley @*/
5260c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5261c73cfb54SMatthew G. Knepley {
5262e5e52638SMatthew G. Knepley   PetscDS        ds;
5263f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5264f17e8794SMatthew G. Knepley 
5265c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5266c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5267c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5268c73cfb54SMatthew G. Knepley   dm->dim = dim;
5269e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5270e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5271c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5272c73cfb54SMatthew G. Knepley }
5273c73cfb54SMatthew G. Knepley 
5274793f3fe5SMatthew G. Knepley /*@
5275793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5276793f3fe5SMatthew G. Knepley 
5277d083f849SBarry Smith   Collective on dm
5278793f3fe5SMatthew G. Knepley 
5279793f3fe5SMatthew G. Knepley   Input Parameters:
5280793f3fe5SMatthew G. Knepley + dm - the DM
5281793f3fe5SMatthew G. Knepley - dim - the dimension
5282793f3fe5SMatthew G. Knepley 
5283793f3fe5SMatthew G. Knepley   Output Parameters:
5284793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5285aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5286793f3fe5SMatthew G. Knepley 
5287793f3fe5SMatthew G. Knepley   Note:
5288793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5289a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5290793f3fe5SMatthew G. Knepley   then the interval is empty.
5291793f3fe5SMatthew G. Knepley 
5292793f3fe5SMatthew G. Knepley   Level: intermediate
5293793f3fe5SMatthew G. Knepley 
5294793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5295793f3fe5SMatthew G. Knepley @*/
5296793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5297793f3fe5SMatthew G. Knepley {
5298793f3fe5SMatthew G. Knepley   PetscInt       d;
5299793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5300793f3fe5SMatthew G. Knepley 
5301793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5302793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5303793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5304793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5305793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5306793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5307793f3fe5SMatthew G. Knepley }
5308793f3fe5SMatthew G. Knepley 
53096636e97aSMatthew G Knepley /*@
53106636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
53116636e97aSMatthew G Knepley 
5312d083f849SBarry Smith   Collective on dm
53136636e97aSMatthew G Knepley 
53146636e97aSMatthew G Knepley   Input Parameters:
53156636e97aSMatthew G Knepley + dm - the DM
53166636e97aSMatthew G Knepley - c - coordinate vector
53176636e97aSMatthew G Knepley 
53182dd40e9bSPatrick Sanan   Notes:
53192dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
53202dd40e9bSPatrick Sanan 
53212dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
53226636e97aSMatthew G Knepley 
53236636e97aSMatthew G Knepley   Level: intermediate
53246636e97aSMatthew G Knepley 
53252dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
53266636e97aSMatthew G Knepley @*/
53276636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
53286636e97aSMatthew G Knepley {
53296636e97aSMatthew G Knepley   PetscErrorCode ierr;
53306636e97aSMatthew G Knepley 
53316636e97aSMatthew G Knepley   PetscFunctionBegin;
53326636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
53346636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
53356636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
53366636e97aSMatthew G Knepley   dm->coordinates = c;
53376636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5338b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
533903dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
53406636e97aSMatthew G Knepley   PetscFunctionReturn(0);
53416636e97aSMatthew G Knepley }
53426636e97aSMatthew G Knepley 
53436636e97aSMatthew G Knepley /*@
53446636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
53456636e97aSMatthew G Knepley 
53467058e716SVaclav Hapla   Not collective
53476636e97aSMatthew G Knepley 
53486636e97aSMatthew G Knepley    Input Parameters:
53496636e97aSMatthew G Knepley +  dm - the DM
53506636e97aSMatthew G Knepley -  c - coordinate vector
53516636e97aSMatthew G Knepley 
53522dd40e9bSPatrick Sanan   Notes:
53536636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
53546636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
53556636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
53566636e97aSMatthew G Knepley 
53572dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
53582dd40e9bSPatrick Sanan 
53596636e97aSMatthew G Knepley   Level: intermediate
53606636e97aSMatthew G Knepley 
53616636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
53626636e97aSMatthew G Knepley @*/
53636636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
53646636e97aSMatthew G Knepley {
53656636e97aSMatthew G Knepley   PetscErrorCode ierr;
53666636e97aSMatthew G Knepley 
53676636e97aSMatthew G Knepley   PetscFunctionBegin;
53686636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53696636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
53706636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
53716636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
53728865f1eaSKarl Rupp 
53736636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
53748865f1eaSKarl Rupp 
53756636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
53766636e97aSMatthew G Knepley   PetscFunctionReturn(0);
53776636e97aSMatthew G Knepley }
53786636e97aSMatthew G Knepley 
53796636e97aSMatthew G Knepley /*@
53806636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
53816636e97aSMatthew G Knepley 
5382d083f849SBarry Smith   Collective on dm
53836636e97aSMatthew G Knepley 
53846636e97aSMatthew G Knepley   Input Parameter:
53856636e97aSMatthew G Knepley . dm - the DM
53866636e97aSMatthew G Knepley 
53876636e97aSMatthew G Knepley   Output Parameter:
53886636e97aSMatthew G Knepley . c - global coordinate vector
53896636e97aSMatthew G Knepley 
53906636e97aSMatthew G Knepley   Note:
53916636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
53926636e97aSMatthew G Knepley 
53936636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
53946636e97aSMatthew G Knepley 
53956636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
53966636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
53976636e97aSMatthew G Knepley 
53986636e97aSMatthew G Knepley   Level: intermediate
53996636e97aSMatthew G Knepley 
54006636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
54016636e97aSMatthew G Knepley @*/
54026636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
54036636e97aSMatthew G Knepley {
54046636e97aSMatthew G Knepley   PetscErrorCode ierr;
54056636e97aSMatthew G Knepley 
54066636e97aSMatthew G Knepley   PetscFunctionBegin;
54076636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54086636e97aSMatthew G Knepley   PetscValidPointer(c,2);
54091f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
54100298fd71SBarry Smith     DM        cdm = NULL;
54111970a576SMatthew G. Knepley     PetscBool localized;
54126636e97aSMatthew G Knepley 
54136636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
54146636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
54151970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
54161970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
54171970a576SMatthew G. Knepley     if (localized) {
54181970a576SMatthew G. Knepley       PetscInt cdim;
54191970a576SMatthew G. Knepley 
54201970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
54211970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
54221970a576SMatthew G. Knepley     }
54236636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
54246636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
54256636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
54266636e97aSMatthew G Knepley   }
54276636e97aSMatthew G Knepley   *c = dm->coordinates;
54286636e97aSMatthew G Knepley   PetscFunctionReturn(0);
54296636e97aSMatthew G Knepley }
54306636e97aSMatthew G Knepley 
54316636e97aSMatthew G Knepley /*@
543281e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
543381e9a530SVaclav Hapla 
5434d083f849SBarry Smith   Collective on dm
543581e9a530SVaclav Hapla 
543681e9a530SVaclav Hapla   Input Parameter:
543781e9a530SVaclav Hapla . dm - the DM
543881e9a530SVaclav Hapla 
543981e9a530SVaclav Hapla   Level: advanced
544081e9a530SVaclav Hapla 
544181e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
544281e9a530SVaclav Hapla @*/
544381e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
544481e9a530SVaclav Hapla {
544581e9a530SVaclav Hapla   PetscErrorCode ierr;
544681e9a530SVaclav Hapla 
544781e9a530SVaclav Hapla   PetscFunctionBegin;
544881e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
544981e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
54501970a576SMatthew G. Knepley     DM        cdm = NULL;
54511970a576SMatthew G. Knepley     PetscBool localized;
54521970a576SMatthew G. Knepley 
545381e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
545481e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
54551970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
54561970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
54571970a576SMatthew G. Knepley     if (localized) {
54581970a576SMatthew G. Knepley       PetscInt cdim;
54591970a576SMatthew G. Knepley 
54601970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
54611970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
54621970a576SMatthew G. Knepley     }
546381e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
546481e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
546581e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
546681e9a530SVaclav Hapla   }
546781e9a530SVaclav Hapla   PetscFunctionReturn(0);
546881e9a530SVaclav Hapla }
546981e9a530SVaclav Hapla 
547081e9a530SVaclav Hapla /*@
54716636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
54726636e97aSMatthew G Knepley 
5473d083f849SBarry Smith   Collective on dm
54746636e97aSMatthew G Knepley 
54756636e97aSMatthew G Knepley   Input Parameter:
54766636e97aSMatthew G Knepley . dm - the DM
54776636e97aSMatthew G Knepley 
54786636e97aSMatthew G Knepley   Output Parameter:
54796636e97aSMatthew G Knepley . c - coordinate vector
54806636e97aSMatthew G Knepley 
54816636e97aSMatthew G Knepley   Note:
54826636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
54836636e97aSMatthew G Knepley 
54846636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
54856636e97aSMatthew G Knepley 
54866636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
54876636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
54886636e97aSMatthew G Knepley 
54896636e97aSMatthew G Knepley   Level: intermediate
54906636e97aSMatthew G Knepley 
549181e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
54926636e97aSMatthew G Knepley @*/
54936636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
54946636e97aSMatthew G Knepley {
54956636e97aSMatthew G Knepley   PetscErrorCode ierr;
54966636e97aSMatthew G Knepley 
54976636e97aSMatthew G Knepley   PetscFunctionBegin;
54986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54996636e97aSMatthew G Knepley   PetscValidPointer(c,2);
550081e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
55016636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
55026636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55036636e97aSMatthew G Knepley }
55046636e97aSMatthew G Knepley 
550581e9a530SVaclav Hapla /*@
550681e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
550781e9a530SVaclav Hapla 
550881e9a530SVaclav Hapla   Not collective
550981e9a530SVaclav Hapla 
551081e9a530SVaclav Hapla   Input Parameter:
551181e9a530SVaclav Hapla . dm - the DM
551281e9a530SVaclav Hapla 
551381e9a530SVaclav Hapla   Output Parameter:
551481e9a530SVaclav Hapla . c - coordinate vector
551581e9a530SVaclav Hapla 
551681e9a530SVaclav Hapla   Level: advanced
551781e9a530SVaclav Hapla 
551881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
551981e9a530SVaclav Hapla @*/
552081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
552181e9a530SVaclav Hapla {
552281e9a530SVaclav Hapla   PetscFunctionBegin;
552381e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
552481e9a530SVaclav Hapla   PetscValidPointer(c,2);
552581e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
55266636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
55276636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55286636e97aSMatthew G Knepley }
55296636e97aSMatthew G Knepley 
55302db98f8dSVaclav Hapla /*@
55312db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
55322db98f8dSVaclav Hapla 
55332db98f8dSVaclav Hapla   Not collective
55342db98f8dSVaclav Hapla 
55352db98f8dSVaclav Hapla   Input Parameter:
55362db98f8dSVaclav Hapla + dm - the DM
55372db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
55382db98f8dSVaclav Hapla 
55392db98f8dSVaclav Hapla   Output Parameter:
55402db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
55412db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
55422db98f8dSVaclav Hapla 
55432db98f8dSVaclav Hapla   Note:
55442db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
55452db98f8dSVaclav Hapla 
55462db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
55472db98f8dSVaclav Hapla 
55482db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
55492db98f8dSVaclav Hapla 
55502db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
55512db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
55522db98f8dSVaclav Hapla 
55532db98f8dSVaclav Hapla   Level: advanced
55542db98f8dSVaclav Hapla 
55552db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
55562db98f8dSVaclav Hapla @*/
55572db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
55582db98f8dSVaclav Hapla {
55592db98f8dSVaclav Hapla   PetscSection        cs, newcs;
55602db98f8dSVaclav Hapla   Vec                 coords;
55612db98f8dSVaclav Hapla   const PetscScalar   *arr;
55622db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
55632db98f8dSVaclav Hapla   PetscInt            n;
55642db98f8dSVaclav Hapla   PetscErrorCode      ierr;
55652db98f8dSVaclav Hapla 
55662db98f8dSVaclav Hapla   PetscFunctionBegin;
55672db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55682db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
55692db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
55702db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
55712db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
55722db98f8dSVaclav Hapla   if (!dm->coordinateDM || !dm->coordinateDM->defaultSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
55732db98f8dSVaclav Hapla   cs = dm->coordinateDM->defaultSection;
55742db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
55752db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
55762db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
55772db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
55782db98f8dSVaclav Hapla   if (pCoord) {
55792db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
55802db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
55812db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
55822db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5583ad9ac99dSVaclav Hapla   } else {
5584ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
55852db98f8dSVaclav Hapla   }
5586ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5587ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
55882db98f8dSVaclav Hapla   PetscFunctionReturn(0);
55892db98f8dSVaclav Hapla }
55902db98f8dSVaclav Hapla 
5591f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5592f19dbd58SToby Isaac {
5593f19dbd58SToby Isaac   PetscErrorCode ierr;
5594f19dbd58SToby Isaac 
5595f19dbd58SToby Isaac   PetscFunctionBegin;
5596f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5597f19dbd58SToby Isaac   PetscValidPointer(field,2);
5598f19dbd58SToby Isaac   if (!dm->coordinateField) {
5599f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5600f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5601f19dbd58SToby Isaac     }
5602f19dbd58SToby Isaac   }
5603f19dbd58SToby Isaac   *field = dm->coordinateField;
5604f19dbd58SToby Isaac   PetscFunctionReturn(0);
5605f19dbd58SToby Isaac }
5606f19dbd58SToby Isaac 
5607f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5608f19dbd58SToby Isaac {
5609f19dbd58SToby Isaac   PetscErrorCode ierr;
5610f19dbd58SToby Isaac 
5611f19dbd58SToby Isaac   PetscFunctionBegin;
5612f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5613f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5614c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5615f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5616f19dbd58SToby Isaac   dm->coordinateField = field;
5617f19dbd58SToby Isaac   PetscFunctionReturn(0);
5618f19dbd58SToby Isaac }
5619f19dbd58SToby Isaac 
56206636e97aSMatthew G Knepley /*@
56211cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
56226636e97aSMatthew G Knepley 
5623d083f849SBarry Smith   Collective on dm
56246636e97aSMatthew G Knepley 
56256636e97aSMatthew G Knepley   Input Parameter:
56266636e97aSMatthew G Knepley . dm - the DM
56276636e97aSMatthew G Knepley 
56286636e97aSMatthew G Knepley   Output Parameter:
56296636e97aSMatthew G Knepley . cdm - coordinate DM
56306636e97aSMatthew G Knepley 
56316636e97aSMatthew G Knepley   Level: intermediate
56326636e97aSMatthew G Knepley 
56331cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
56346636e97aSMatthew G Knepley @*/
56356636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
56366636e97aSMatthew G Knepley {
56376636e97aSMatthew G Knepley   PetscErrorCode ierr;
56386636e97aSMatthew G Knepley 
56396636e97aSMatthew G Knepley   PetscFunctionBegin;
56406636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56416636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
56426636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5643308f8a94SToby Isaac     DM cdm;
5644308f8a94SToby Isaac 
564582f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5646308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5647308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5648308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5649308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5650308f8a94SToby Isaac     dm->coordinateDM = cdm;
56516636e97aSMatthew G Knepley   }
56526636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
56536636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56546636e97aSMatthew G Knepley }
5655e87bb0d3SMatthew G Knepley 
56561cfe2091SMatthew G. Knepley /*@
56571cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
56581cfe2091SMatthew G. Knepley 
5659d083f849SBarry Smith   Logically Collective on dm
56601cfe2091SMatthew G. Knepley 
56611cfe2091SMatthew G. Knepley   Input Parameters:
56621cfe2091SMatthew G. Knepley + dm - the DM
56631cfe2091SMatthew G. Knepley - cdm - coordinate DM
56641cfe2091SMatthew G. Knepley 
56651cfe2091SMatthew G. Knepley   Level: intermediate
56661cfe2091SMatthew G. Knepley 
56671cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
56681cfe2091SMatthew G. Knepley @*/
56691cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
56701cfe2091SMatthew G. Knepley {
56711cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
56721cfe2091SMatthew G. Knepley 
56731cfe2091SMatthew G. Knepley   PetscFunctionBegin;
56741cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56751cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5676f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
56771cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
56781cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
56791cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
56801cfe2091SMatthew G. Knepley }
56811cfe2091SMatthew G. Knepley 
568246e270d4SMatthew G. Knepley /*@
568346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
568446e270d4SMatthew G. Knepley 
568546e270d4SMatthew G. Knepley   Not Collective
568646e270d4SMatthew G. Knepley 
568746e270d4SMatthew G. Knepley   Input Parameter:
568846e270d4SMatthew G. Knepley . dm - The DM object
568946e270d4SMatthew G. Knepley 
569046e270d4SMatthew G. Knepley   Output Parameter:
569146e270d4SMatthew G. Knepley . dim - The embedding dimension
569246e270d4SMatthew G. Knepley 
569346e270d4SMatthew G. Knepley   Level: intermediate
569446e270d4SMatthew G. Knepley 
5695e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
569646e270d4SMatthew G. Knepley @*/
569746e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
569846e270d4SMatthew G. Knepley {
569946e270d4SMatthew G. Knepley   PetscFunctionBegin;
570046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
570146e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
57029a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
57039a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
57049a9a41abSToby Isaac   }
570546e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
570646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
570746e270d4SMatthew G. Knepley }
570846e270d4SMatthew G. Knepley 
570946e270d4SMatthew G. Knepley /*@
571046e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
571146e270d4SMatthew G. Knepley 
571246e270d4SMatthew G. Knepley   Not Collective
571346e270d4SMatthew G. Knepley 
571446e270d4SMatthew G. Knepley   Input Parameters:
571546e270d4SMatthew G. Knepley + dm  - The DM object
571646e270d4SMatthew G. Knepley - dim - The embedding dimension
571746e270d4SMatthew G. Knepley 
571846e270d4SMatthew G. Knepley   Level: intermediate
571946e270d4SMatthew G. Knepley 
5720e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
572146e270d4SMatthew G. Knepley @*/
572246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
572346e270d4SMatthew G. Knepley {
5724e5e52638SMatthew G. Knepley   PetscDS        ds;
5725f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5726f17e8794SMatthew G. Knepley 
572746e270d4SMatthew G. Knepley   PetscFunctionBegin;
572846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
572946e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5730e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5731e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
573246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
573346e270d4SMatthew G. Knepley }
573446e270d4SMatthew G. Knepley 
5735e8abe2deSMatthew G. Knepley /*@
5736e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5737e8abe2deSMatthew G. Knepley 
5738d083f849SBarry Smith   Collective on dm
5739e8abe2deSMatthew G. Knepley 
5740e8abe2deSMatthew G. Knepley   Input Parameter:
5741e8abe2deSMatthew G. Knepley . dm - The DM object
5742e8abe2deSMatthew G. Knepley 
5743e8abe2deSMatthew G. Knepley   Output Parameter:
5744e8abe2deSMatthew G. Knepley . section - The PetscSection object
5745e8abe2deSMatthew G. Knepley 
5746e8abe2deSMatthew G. Knepley   Level: intermediate
5747e8abe2deSMatthew G. Knepley 
5748e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
5749e8abe2deSMatthew G. Knepley @*/
5750e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5751e8abe2deSMatthew G. Knepley {
5752e8abe2deSMatthew G. Knepley   DM             cdm;
5753e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5754e8abe2deSMatthew G. Knepley 
5755e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5756e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5757e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5758e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5759e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
5760e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5761e8abe2deSMatthew G. Knepley }
5762e8abe2deSMatthew G. Knepley 
5763e8abe2deSMatthew G. Knepley /*@
5764e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5765e8abe2deSMatthew G. Knepley 
5766e8abe2deSMatthew G. Knepley   Not Collective
5767e8abe2deSMatthew G. Knepley 
5768e8abe2deSMatthew G. Knepley   Input Parameters:
5769e8abe2deSMatthew G. Knepley + dm      - The DM object
577046e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5771e8abe2deSMatthew G. Knepley - section - The PetscSection object
5772e8abe2deSMatthew G. Knepley 
5773e8abe2deSMatthew G. Knepley   Level: intermediate
5774e8abe2deSMatthew G. Knepley 
5775e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
5776e8abe2deSMatthew G. Knepley @*/
577746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5778e8abe2deSMatthew G. Knepley {
5779e8abe2deSMatthew G. Knepley   DM             cdm;
5780e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5781e8abe2deSMatthew G. Knepley 
5782e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5783e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
578446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5785e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5786e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
578746e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
57884c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
578946e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
579046e270d4SMatthew G. Knepley 
579146e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
579246e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
579346e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
579446e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
579546e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
579646e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
579746e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
579846e270d4SMatthew G. Knepley     }
5799ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
580046e270d4SMatthew G. Knepley   }
5801e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5802e8abe2deSMatthew G. Knepley }
5803e8abe2deSMatthew G. Knepley 
58045dc8c3f7SMatthew G. Knepley /*@C
580590b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
58065dc8c3f7SMatthew G. Knepley 
58075dc8c3f7SMatthew G. Knepley   Input Parameters:
580890b157c4SStefano Zampini . dm      - The DM object
580990b157c4SStefano Zampini 
581090b157c4SStefano Zampini   Output Parameters:
581190b157c4SStefano Zampini + per     - Whether the DM is periodic or not
58125dc8c3f7SMatthew 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
58135dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
58145dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
58155dc8c3f7SMatthew G. Knepley 
58165dc8c3f7SMatthew G. Knepley   Level: developer
58175dc8c3f7SMatthew G. Knepley 
58185dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
58195dc8c3f7SMatthew G. Knepley @*/
582090b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5821c6b900c6SMatthew G. Knepley {
5822c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5823c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
582490b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5825c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5826c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
58275dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5828c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5829c6b900c6SMatthew G. Knepley }
5830c6b900c6SMatthew G. Knepley 
58315dc8c3f7SMatthew G. Knepley /*@C
58325dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
58335dc8c3f7SMatthew G. Knepley 
58345dc8c3f7SMatthew G. Knepley   Input Parameters:
58355dc8c3f7SMatthew G. Knepley + dm      - The DM object
583690b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
58375dc8c3f7SMatthew 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
58385dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
58395dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
58405dc8c3f7SMatthew G. Knepley 
58415dc8c3f7SMatthew G. Knepley   Level: developer
58425dc8c3f7SMatthew G. Knepley 
58435dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
58445dc8c3f7SMatthew G. Knepley @*/
584590b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5846c6b900c6SMatthew G. Knepley {
5847c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
5848c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
5849c6b900c6SMatthew G. Knepley 
5850c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5851c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
585290b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
585390b157c4SStefano Zampini   if (maxCell) {
585490b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
585590b157c4SStefano Zampini     PetscValidPointer(L,4);
585690b157c4SStefano Zampini     PetscValidPointer(bd,5);
585790b157c4SStefano Zampini   }
58585dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
58595dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
586090b157c4SStefano Zampini   if (maxCell) {
58615dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
58625dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
586390b157c4SStefano Zampini   }
5864072d7d67SStefano Zampini   dm->periodic = per;
5865c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5866c6b900c6SMatthew G. Knepley }
5867c6b900c6SMatthew G. Knepley 
58682e17dfb7SMatthew G. Knepley /*@
58692e17dfb7SMatthew 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.
58702e17dfb7SMatthew G. Knepley 
58712e17dfb7SMatthew G. Knepley   Input Parameters:
58722e17dfb7SMatthew G. Knepley + dm     - The DM
587365da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
587465da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
58752e17dfb7SMatthew G. Knepley 
58762e17dfb7SMatthew G. Knepley   Output Parameter:
58772e17dfb7SMatthew G. Knepley . out - The localized coordinate point
58782e17dfb7SMatthew G. Knepley 
58792e17dfb7SMatthew G. Knepley   Level: developer
58802e17dfb7SMatthew G. Knepley 
58812e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
58822e17dfb7SMatthew G. Knepley @*/
588365da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
58842e17dfb7SMatthew G. Knepley {
58852e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
58862e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
58872e17dfb7SMatthew G. Knepley 
58882e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
58892e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
58902e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
58912e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
58922e17dfb7SMatthew G. Knepley   } else {
589365da65dcSMatthew G. Knepley     if (endpoint) {
589465da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
5895da3333bfSMatthew 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)) {
5896da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
589765da65dcSMatthew G. Knepley         } else {
5898da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
589965da65dcSMatthew G. Knepley         }
590065da65dcSMatthew G. Knepley       }
590165da65dcSMatthew G. Knepley     } else {
59022e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
59031118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
59042e17dfb7SMatthew G. Knepley       }
59052e17dfb7SMatthew G. Knepley     }
590665da65dcSMatthew G. Knepley   }
59072e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
59082e17dfb7SMatthew G. Knepley }
59092e17dfb7SMatthew G. Knepley 
59102e17dfb7SMatthew G. Knepley /*
59112e17dfb7SMatthew 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.
59122e17dfb7SMatthew G. Knepley 
59132e17dfb7SMatthew G. Knepley   Input Parameters:
59142e17dfb7SMatthew G. Knepley + dm     - The DM
59152e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
59162e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
59172e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
59182e17dfb7SMatthew G. Knepley 
59192e17dfb7SMatthew G. Knepley   Output Parameter:
59202e17dfb7SMatthew G. Knepley . out - The localized coordinate point
59212e17dfb7SMatthew G. Knepley 
59222e17dfb7SMatthew G. Knepley   Level: developer
59232e17dfb7SMatthew G. Knepley 
59242e17dfb7SMatthew 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
59252e17dfb7SMatthew G. Knepley 
59262e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
59272e17dfb7SMatthew G. Knepley */
59282e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
59292e17dfb7SMatthew G. Knepley {
59302e17dfb7SMatthew G. Knepley   PetscInt d;
59312e17dfb7SMatthew G. Knepley 
59322e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
59332e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
59342e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
59352e17dfb7SMatthew G. Knepley   } else {
59362e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5937908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
59382e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
59392e17dfb7SMatthew G. Knepley       } else {
59402e17dfb7SMatthew G. Knepley         out[d] = in[d];
59412e17dfb7SMatthew G. Knepley       }
59422e17dfb7SMatthew G. Knepley     }
59432e17dfb7SMatthew G. Knepley   }
59442e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
59452e17dfb7SMatthew G. Knepley }
59462e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
59472e17dfb7SMatthew G. Knepley {
59482e17dfb7SMatthew G. Knepley   PetscInt d;
59492e17dfb7SMatthew G. Knepley 
59502e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
59512e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
59522e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
59532e17dfb7SMatthew G. Knepley   } else {
59542e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5955908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
59562e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
59572e17dfb7SMatthew G. Knepley       } else {
59582e17dfb7SMatthew G. Knepley         out[d] = in[d];
59592e17dfb7SMatthew G. Knepley       }
59602e17dfb7SMatthew G. Knepley     }
59612e17dfb7SMatthew G. Knepley   }
59622e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
59632e17dfb7SMatthew G. Knepley }
59642e17dfb7SMatthew G. Knepley 
59652e17dfb7SMatthew G. Knepley /*
59662e17dfb7SMatthew 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.
59672e17dfb7SMatthew G. Knepley 
59682e17dfb7SMatthew G. Knepley   Input Parameters:
59692e17dfb7SMatthew G. Knepley + dm     - The DM
59702e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
59712e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
59722e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
59732e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
59742e17dfb7SMatthew G. Knepley 
59752e17dfb7SMatthew G. Knepley   Output Parameter:
59762e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
59772e17dfb7SMatthew G. Knepley 
59782e17dfb7SMatthew G. Knepley   Level: developer
59792e17dfb7SMatthew G. Knepley 
59802e17dfb7SMatthew 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
59812e17dfb7SMatthew G. Knepley 
59822e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
59832e17dfb7SMatthew G. Knepley */
59842e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
59852e17dfb7SMatthew G. Knepley {
59862e17dfb7SMatthew G. Knepley   PetscInt d;
59872e17dfb7SMatthew G. Knepley 
59882e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
59892e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
59902e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
59912e17dfb7SMatthew G. Knepley   } else {
59922e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5993908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
59942e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
59952e17dfb7SMatthew G. Knepley       } else {
59962e17dfb7SMatthew G. Knepley         out[d] += in[d];
59972e17dfb7SMatthew G. Knepley       }
59982e17dfb7SMatthew G. Knepley     }
59992e17dfb7SMatthew G. Knepley   }
60002e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60012e17dfb7SMatthew G. Knepley }
60022e17dfb7SMatthew G. Knepley 
600336447a5eSToby Isaac /*@
60048f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
60058f700142SStefano Zampini 
60068f700142SStefano Zampini   Not collective
600736447a5eSToby Isaac 
600836447a5eSToby Isaac   Input Parameter:
600936447a5eSToby Isaac . dm - The DM
601036447a5eSToby Isaac 
601136447a5eSToby Isaac   Output Parameter:
601236447a5eSToby Isaac   areLocalized - True if localized
601336447a5eSToby Isaac 
601436447a5eSToby Isaac   Level: developer
601536447a5eSToby Isaac 
60168f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
601736447a5eSToby Isaac @*/
60188f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
601936447a5eSToby Isaac {
602036447a5eSToby Isaac   DM             cdm;
602136447a5eSToby Isaac   PetscSection   coordSection;
602246a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
602346a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
602436447a5eSToby Isaac   PetscErrorCode ierr;
602536447a5eSToby Isaac 
602636447a5eSToby Isaac   PetscFunctionBegin;
602736447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
602846a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
60298b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
603046a3a80fSLisandro Dalcin 
603136447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
603236447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
603346a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
60349f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
603546a3a80fSLisandro Dalcin 
60369f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
603746a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
603836447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
603936447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
604046a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
604146a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
604236447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
604346a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
604436447a5eSToby Isaac   }
60458f700142SStefano Zampini   *areLocalized = alreadyLocalized;
604636447a5eSToby Isaac   PetscFunctionReturn(0);
604736447a5eSToby Isaac }
604836447a5eSToby Isaac 
60498f700142SStefano Zampini /*@
60508f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
60518f700142SStefano Zampini 
60528f700142SStefano Zampini   Collective on dm
60538f700142SStefano Zampini 
60548f700142SStefano Zampini   Input Parameter:
60558f700142SStefano Zampini . dm - The DM
60568f700142SStefano Zampini 
60578f700142SStefano Zampini   Output Parameter:
60588f700142SStefano Zampini   areLocalized - True if localized
60598f700142SStefano Zampini 
60608f700142SStefano Zampini   Level: developer
60618f700142SStefano Zampini 
60628f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
60638f700142SStefano Zampini @*/
60648f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
60658f700142SStefano Zampini {
60668f700142SStefano Zampini   PetscBool      localized;
60678f700142SStefano Zampini   PetscErrorCode ierr;
60688f700142SStefano Zampini 
60698f700142SStefano Zampini   PetscFunctionBegin;
60708f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60718f700142SStefano Zampini   PetscValidPointer(areLocalized, 2);
60728f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
60738f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
60748f700142SStefano Zampini   PetscFunctionReturn(0);
60758f700142SStefano Zampini }
607636447a5eSToby Isaac 
60772e17dfb7SMatthew G. Knepley /*@
6078492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
60792e17dfb7SMatthew G. Knepley 
60808f700142SStefano Zampini   Collective on dm
60818f700142SStefano Zampini 
60822e17dfb7SMatthew G. Knepley   Input Parameter:
60832e17dfb7SMatthew G. Knepley . dm - The DM
60842e17dfb7SMatthew G. Knepley 
60852e17dfb7SMatthew G. Knepley   Level: developer
60862e17dfb7SMatthew G. Knepley 
60878f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
60882e17dfb7SMatthew G. Knepley @*/
60892e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
60902e17dfb7SMatthew G. Knepley {
60912e17dfb7SMatthew G. Knepley   DM             cdm;
60922e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
60932e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
60943e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
60953e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6096e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
60973e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
60983e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
60992e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
61002e17dfb7SMatthew G. Knepley 
61012e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61022e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
610392c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6104f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6105f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6106f7cbd40bSStefano Zampini 
61072e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
61082e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
61092e17dfb7SMatthew G. Knepley   {
61102e17dfb7SMatthew G. Knepley     PetscBool isplex;
61112e17dfb7SMatthew G. Knepley 
61122e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
61132e17dfb7SMatthew G. Knepley     if (isplex) {
61142e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
61153e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
611669291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
61173e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
61183e922f36SToby Isaac       newStart = vStart;
61193e922f36SToby Isaac       newEnd   = vEnd;
61203e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
61213e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
61223e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
61233e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
61243e922f36SToby Isaac       }
61252e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
61262e17dfb7SMatthew G. Knepley   }
61272e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
612843eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
61292e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
61303e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6131e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
61323e922f36SToby Isaac 
61332e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
61342e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
61352e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
61362e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
61373e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
61383e922f36SToby Isaac 
613969291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
61403e922f36SToby Isaac   localized = &anchor[bs];
61413e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
61423e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
61433e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
61443e922f36SToby Isaac 
61453e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
61463e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
61473e922f36SToby Isaac       PetscInt     b;
61483e922f36SToby Isaac 
61493e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
61503e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
61513e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
61523e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
61533e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
61543e922f36SToby Isaac         for (b = 0; b < bs; b++) {
61553e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
61563e922f36SToby Isaac         }
61573e922f36SToby Isaac         if (b < bs) break;
61583e922f36SToby Isaac       }
61593e922f36SToby Isaac       if (d < dof/bs) {
61603e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
61613e922f36SToby Isaac           PetscInt cdof;
61623e922f36SToby Isaac 
61633e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
61643e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
61653e922f36SToby Isaac         }
61663e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
61673e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
61683e922f36SToby Isaac       }
61693e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
61703e922f36SToby Isaac     }
61713e922f36SToby Isaac   }
61723e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
61733e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
617469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
61753e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
617669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
61773e922f36SToby Isaac     PetscFunctionReturn(0);
61783e922f36SToby Isaac   }
61792e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
61802e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
61812e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
61822e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
61832e17dfb7SMatthew G. Knepley   }
61842e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
61852e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6186c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
61872e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
61882e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
61892e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
61902e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6191c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
61922e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
61932e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
61942e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
61952e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
61962e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
61972e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
61982e17dfb7SMatthew G. Knepley   }
61993e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
62003e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
62013e922f36SToby Isaac 
62022e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
62032e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
62043e922f36SToby Isaac       PetscInt     b, cdof;
62052e17dfb7SMatthew G. Knepley 
62063e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
62073e922f36SToby Isaac       if (!cdof) continue;
62082e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62092e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
62102e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
62112e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
62122e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62132e17dfb7SMatthew G. Knepley     }
62143e922f36SToby Isaac   }
621569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
621669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6217c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
62182e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
62192e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
62202e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
62212e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
62222e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
62232e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
62242e17dfb7SMatthew G. Knepley }
62252e17dfb7SMatthew G. Knepley 
6226e87bb0d3SMatthew G Knepley /*@
62273a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6228e87bb0d3SMatthew G Knepley 
6229d083f849SBarry Smith   Collective on v (see explanation below)
6230e87bb0d3SMatthew G Knepley 
6231e87bb0d3SMatthew G Knepley   Input Parameters:
6232e87bb0d3SMatthew G Knepley + dm - The DM
62333a93e3b7SToby Isaac . v - The Vec of points
623462a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
62353a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6236e87bb0d3SMatthew G Knepley 
623761e3bb9bSMatthew G Knepley   Output Parameter:
623862a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
623962a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
62403a93e3b7SToby Isaac 
6241e87bb0d3SMatthew G Knepley 
6242e87bb0d3SMatthew G Knepley   Level: developer
624361e3bb9bSMatthew G Knepley 
624462a38674SMatthew G. Knepley   Notes:
62453a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
624662a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
62473a93e3b7SToby Isaac 
62483a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
624962a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
62503a93e3b7SToby Isaac 
62513a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
62523a93e3b7SToby Isaac 
625362a38674SMatthew G. Knepley $    const PetscSFNode *cells;
625462a38674SMatthew G. Knepley $    PetscInt           nFound;
6255a6216909SToby Isaac $    const PetscInt    *found;
625662a38674SMatthew G. Knepley $
6257a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
62583a93e3b7SToby Isaac 
62593a93e3b7SToby 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
62603a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
62613a93e3b7SToby Isaac 
626262a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
626361e3bb9bSMatthew G Knepley @*/
626462a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6265e87bb0d3SMatthew G Knepley {
6266735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6267735aa83eSMatthew G Knepley 
6268e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6269e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6270e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6271e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
62723a93e3b7SToby Isaac   if (*cellSF) {
62733a93e3b7SToby Isaac     PetscMPIInt result;
62743a93e3b7SToby Isaac 
6275e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6276a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
62773a93e3b7SToby 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");
6278e0fc9d1bSMatthew G. Knepley   } else {
62793a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
62803a93e3b7SToby Isaac   }
628147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6282735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
628362a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
628482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
628547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6286e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6287e87bb0d3SMatthew G Knepley }
628814f150ffSMatthew G. Knepley 
6289f4d763aaSMatthew G. Knepley /*@
6290f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6291f4d763aaSMatthew G. Knepley 
62928f700142SStefano Zampini   Collective on dm
62938f700142SStefano Zampini 
6294f4d763aaSMatthew G. Knepley   Input Parameter:
6295f4d763aaSMatthew G. Knepley . dm - The original DM
6296f4d763aaSMatthew G. Knepley 
6297f4d763aaSMatthew G. Knepley   Output Parameter:
6298f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6299f4d763aaSMatthew G. Knepley 
6300f4d763aaSMatthew G. Knepley   Level: intermediate
6301f4d763aaSMatthew G. Knepley 
6302e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6303f4d763aaSMatthew G. Knepley @*/
630414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
630514f150ffSMatthew G. Knepley {
6306c26acbdeSMatthew G. Knepley   PetscSection   section;
63072d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
630814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
630914f150ffSMatthew G. Knepley 
631014f150ffSMatthew G. Knepley   PetscFunctionBegin;
631114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
631214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
6313e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
6314c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6315127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
63162d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6317c26acbdeSMatthew G. Knepley     *odm = dm;
6318c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6319c26acbdeSMatthew G. Knepley   }
632014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6321c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
632214f150ffSMatthew G. Knepley     PetscSF      sf;
632314f150ffSMatthew G. Knepley 
632414f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6325e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
632614f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
6327e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
632814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
632914f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
633015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6331e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
633214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
633314f150ffSMatthew G. Knepley   }
633414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
633514f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
633614f150ffSMatthew G. Knepley }
6337f4d763aaSMatthew G. Knepley 
6338f4d763aaSMatthew G. Knepley /*@
6339cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6340f4d763aaSMatthew G. Knepley 
6341f4d763aaSMatthew G. Knepley   Input Parameter:
6342f4d763aaSMatthew G. Knepley . dm - The original DM
6343f4d763aaSMatthew G. Knepley 
6344cdb7a50dSMatthew G. Knepley   Output Parameters:
6345cdb7a50dSMatthew G. Knepley + num - The output sequence number
6346cdb7a50dSMatthew G. Knepley - val - The output sequence value
6347f4d763aaSMatthew G. Knepley 
6348f4d763aaSMatthew G. Knepley   Level: intermediate
6349f4d763aaSMatthew G. Knepley 
6350f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6351f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6352f4d763aaSMatthew G. Knepley 
6353f4d763aaSMatthew G. Knepley .seealso: VecView()
6354f4d763aaSMatthew G. Knepley @*/
6355cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6356f4d763aaSMatthew G. Knepley {
6357f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6358f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6359cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
6360cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
6361f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6362f4d763aaSMatthew G. Knepley }
6363f4d763aaSMatthew G. Knepley 
6364f4d763aaSMatthew G. Knepley /*@
6365cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6366f4d763aaSMatthew G. Knepley 
6367f4d763aaSMatthew G. Knepley   Input Parameters:
6368f4d763aaSMatthew G. Knepley + dm - The original DM
6369cdb7a50dSMatthew G. Knepley . num - The output sequence number
6370cdb7a50dSMatthew G. Knepley - val - The output sequence value
6371f4d763aaSMatthew G. Knepley 
6372f4d763aaSMatthew G. Knepley   Level: intermediate
6373f4d763aaSMatthew G. Knepley 
6374f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6375f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6376f4d763aaSMatthew G. Knepley 
6377f4d763aaSMatthew G. Knepley .seealso: VecView()
6378f4d763aaSMatthew G. Knepley @*/
6379cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6380f4d763aaSMatthew G. Knepley {
6381f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6382f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6383f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6384cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6385cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6386cdb7a50dSMatthew G. Knepley }
6387cdb7a50dSMatthew G. Knepley 
6388cdb7a50dSMatthew G. Knepley /*@C
6389cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6390cdb7a50dSMatthew G. Knepley 
6391cdb7a50dSMatthew G. Knepley   Input Parameters:
6392cdb7a50dSMatthew G. Knepley + dm   - The original DM
6393cdb7a50dSMatthew G. Knepley . name - The sequence name
6394cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6395cdb7a50dSMatthew G. Knepley 
6396cdb7a50dSMatthew G. Knepley   Output Parameter:
6397cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6398cdb7a50dSMatthew G. Knepley 
6399cdb7a50dSMatthew G. Knepley   Level: intermediate
6400cdb7a50dSMatthew G. Knepley 
6401cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6402cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6403cdb7a50dSMatthew G. Knepley 
6404cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6405cdb7a50dSMatthew G. Knepley @*/
6406cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6407cdb7a50dSMatthew G. Knepley {
6408cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6409cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6410cdb7a50dSMatthew G. Knepley 
6411cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6412cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6413cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6414cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
6415cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6416cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6417cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6418cdb7a50dSMatthew G. Knepley     PetscScalar value;
6419cdb7a50dSMatthew G. Knepley 
642039d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
64214aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6422cdb7a50dSMatthew G. Knepley #endif
6423cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6424f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6425f4d763aaSMatthew G. Knepley }
64268e4ac7eaSMatthew G. Knepley 
64278e4ac7eaSMatthew G. Knepley /*@
64288e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
64298e4ac7eaSMatthew G. Knepley 
64308e4ac7eaSMatthew G. Knepley   Not collective
64318e4ac7eaSMatthew G. Knepley 
64328e4ac7eaSMatthew G. Knepley   Input Parameter:
64338e4ac7eaSMatthew G. Knepley . dm - The DM
64348e4ac7eaSMatthew G. Knepley 
64358e4ac7eaSMatthew G. Knepley   Output Parameter:
64368e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
64378e4ac7eaSMatthew G. Knepley 
64388e4ac7eaSMatthew G. Knepley   Level: beginner
64398e4ac7eaSMatthew G. Knepley 
64408e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
64418e4ac7eaSMatthew G. Knepley @*/
64428e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
64438e4ac7eaSMatthew G. Knepley {
64448e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64458e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64468e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
64478e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64488e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64498e4ac7eaSMatthew G. Knepley }
64508e4ac7eaSMatthew G. Knepley 
64518e4ac7eaSMatthew G. Knepley /*@
64525d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
64538e4ac7eaSMatthew G. Knepley 
64548e4ac7eaSMatthew G. Knepley   Collective on dm
64558e4ac7eaSMatthew G. Knepley 
64568e4ac7eaSMatthew G. Knepley   Input Parameters:
64578e4ac7eaSMatthew G. Knepley + dm - The DM
64588e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
64598e4ac7eaSMatthew G. Knepley 
64605d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
64615d3b26e6SMatthew G. Knepley 
64628e4ac7eaSMatthew G. Knepley   Level: beginner
64638e4ac7eaSMatthew G. Knepley 
64645d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
64658e4ac7eaSMatthew G. Knepley @*/
64668e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
64678e4ac7eaSMatthew G. Knepley {
64688e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64698e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64708833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
64718e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
64728e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64738e4ac7eaSMatthew G. Knepley }
6474c58f1c22SToby Isaac 
6475c58f1c22SToby Isaac 
6476c58f1c22SToby Isaac /*@C
6477c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6478c58f1c22SToby Isaac 
6479c58f1c22SToby Isaac   Not Collective
6480c58f1c22SToby Isaac 
6481c58f1c22SToby Isaac   Input Parameters:
6482c58f1c22SToby Isaac + dm   - The DM object
6483c58f1c22SToby Isaac - name - The label name
6484c58f1c22SToby Isaac 
6485c58f1c22SToby Isaac   Level: intermediate
6486c58f1c22SToby Isaac 
6487c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6488c58f1c22SToby Isaac @*/
6489c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6490c58f1c22SToby Isaac {
6491c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
6492c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
6493d67d17b1SMatthew G. Knepley   const char    *lname;
6494c58f1c22SToby Isaac   PetscErrorCode ierr;
6495c58f1c22SToby Isaac 
6496c58f1c22SToby Isaac   PetscFunctionBegin;
6497c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6498c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6499c58f1c22SToby Isaac   while (next) {
6500d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6501d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6502c58f1c22SToby Isaac     if (flg) break;
6503c58f1c22SToby Isaac     next = next->next;
6504c58f1c22SToby Isaac   }
6505c58f1c22SToby Isaac   if (!flg) {
6506c58f1c22SToby Isaac     DMLabelLink tmpLabel;
6507c58f1c22SToby Isaac 
6508c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6509d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
6510c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
6511c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
6512c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
6513c58f1c22SToby Isaac   }
6514c58f1c22SToby Isaac   PetscFunctionReturn(0);
6515c58f1c22SToby Isaac }
6516c58f1c22SToby Isaac 
6517c58f1c22SToby Isaac /*@C
6518c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6519c58f1c22SToby Isaac 
6520c58f1c22SToby Isaac   Not Collective
6521c58f1c22SToby Isaac 
6522c58f1c22SToby Isaac   Input Parameters:
6523c58f1c22SToby Isaac + dm   - The DM object
6524c58f1c22SToby Isaac . name - The label name
6525c58f1c22SToby Isaac - point - The mesh point
6526c58f1c22SToby Isaac 
6527c58f1c22SToby Isaac   Output Parameter:
6528c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6529c58f1c22SToby Isaac 
6530c58f1c22SToby Isaac   Level: beginner
6531c58f1c22SToby Isaac 
6532c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6533c58f1c22SToby Isaac @*/
6534c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6535c58f1c22SToby Isaac {
6536c58f1c22SToby Isaac   DMLabel        label;
6537c58f1c22SToby Isaac   PetscErrorCode ierr;
6538c58f1c22SToby Isaac 
6539c58f1c22SToby Isaac   PetscFunctionBegin;
6540c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6541c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6542c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
654313903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6544c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6545c58f1c22SToby Isaac   PetscFunctionReturn(0);
6546c58f1c22SToby Isaac }
6547c58f1c22SToby Isaac 
6548c58f1c22SToby Isaac /*@C
6549c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6550c58f1c22SToby Isaac 
6551c58f1c22SToby Isaac   Not Collective
6552c58f1c22SToby Isaac 
6553c58f1c22SToby Isaac   Input Parameters:
6554c58f1c22SToby Isaac + dm   - The DM object
6555c58f1c22SToby Isaac . name - The label name
6556c58f1c22SToby Isaac . point - The mesh point
6557c58f1c22SToby Isaac - value - The label value for this point
6558c58f1c22SToby Isaac 
6559c58f1c22SToby Isaac   Output Parameter:
6560c58f1c22SToby Isaac 
6561c58f1c22SToby Isaac   Level: beginner
6562c58f1c22SToby Isaac 
6563c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6564c58f1c22SToby Isaac @*/
6565c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6566c58f1c22SToby Isaac {
6567c58f1c22SToby Isaac   DMLabel        label;
6568c58f1c22SToby Isaac   PetscErrorCode ierr;
6569c58f1c22SToby Isaac 
6570c58f1c22SToby Isaac   PetscFunctionBegin;
6571c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6572c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6573c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6574c58f1c22SToby Isaac   if (!label) {
6575c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6576c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6577c58f1c22SToby Isaac   }
6578c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6579c58f1c22SToby Isaac   PetscFunctionReturn(0);
6580c58f1c22SToby Isaac }
6581c58f1c22SToby Isaac 
6582c58f1c22SToby Isaac /*@C
6583c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6584c58f1c22SToby Isaac 
6585c58f1c22SToby Isaac   Not Collective
6586c58f1c22SToby Isaac 
6587c58f1c22SToby Isaac   Input Parameters:
6588c58f1c22SToby Isaac + dm   - The DM object
6589c58f1c22SToby Isaac . name - The label name
6590c58f1c22SToby Isaac . point - The mesh point
6591c58f1c22SToby Isaac - value - The label value for this point
6592c58f1c22SToby Isaac 
6593c58f1c22SToby Isaac   Output Parameter:
6594c58f1c22SToby Isaac 
6595c58f1c22SToby Isaac   Level: beginner
6596c58f1c22SToby Isaac 
6597c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6598c58f1c22SToby Isaac @*/
6599c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6600c58f1c22SToby Isaac {
6601c58f1c22SToby Isaac   DMLabel        label;
6602c58f1c22SToby Isaac   PetscErrorCode ierr;
6603c58f1c22SToby Isaac 
6604c58f1c22SToby Isaac   PetscFunctionBegin;
6605c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6606c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6607c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6608c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6609c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6610c58f1c22SToby Isaac   PetscFunctionReturn(0);
6611c58f1c22SToby Isaac }
6612c58f1c22SToby Isaac 
6613c58f1c22SToby Isaac /*@C
6614c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6615c58f1c22SToby Isaac 
6616c58f1c22SToby Isaac   Not Collective
6617c58f1c22SToby Isaac 
6618c58f1c22SToby Isaac   Input Parameters:
6619c58f1c22SToby Isaac + dm   - The DM object
6620c58f1c22SToby Isaac - name - The label name
6621c58f1c22SToby Isaac 
6622c58f1c22SToby Isaac   Output Parameter:
6623c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6624c58f1c22SToby Isaac 
6625c58f1c22SToby Isaac   Level: beginner
6626c58f1c22SToby Isaac 
6627df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6628c58f1c22SToby Isaac @*/
6629c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6630c58f1c22SToby Isaac {
6631c58f1c22SToby Isaac   DMLabel        label;
6632c58f1c22SToby Isaac   PetscErrorCode ierr;
6633c58f1c22SToby Isaac 
6634c58f1c22SToby Isaac   PetscFunctionBegin;
6635c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6636c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6637c58f1c22SToby Isaac   PetscValidPointer(size, 3);
6638c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6639c58f1c22SToby Isaac   *size = 0;
6640c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6641c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6642c58f1c22SToby Isaac   PetscFunctionReturn(0);
6643c58f1c22SToby Isaac }
6644c58f1c22SToby Isaac 
6645c58f1c22SToby Isaac /*@C
6646c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6647c58f1c22SToby Isaac 
6648c58f1c22SToby Isaac   Not Collective
6649c58f1c22SToby Isaac 
6650c58f1c22SToby Isaac   Input Parameters:
6651c58f1c22SToby Isaac + mesh - The DM object
6652c58f1c22SToby Isaac - name - The label name
6653c58f1c22SToby Isaac 
6654c58f1c22SToby Isaac   Output Parameter:
6655c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6656c58f1c22SToby Isaac 
6657c58f1c22SToby Isaac   Level: beginner
6658c58f1c22SToby Isaac 
6659c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6660c58f1c22SToby Isaac @*/
6661c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6662c58f1c22SToby Isaac {
6663c58f1c22SToby Isaac   DMLabel        label;
6664c58f1c22SToby Isaac   PetscErrorCode ierr;
6665c58f1c22SToby Isaac 
6666c58f1c22SToby Isaac   PetscFunctionBegin;
6667c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6668c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6669c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6670c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6671c58f1c22SToby Isaac   *ids = NULL;
6672dab2e251SBlaise Bourdin  if (label) {
6673c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6674dab2e251SBlaise Bourdin   } else {
6675dab2e251SBlaise Bourdin     /* returning an empty IS */
6676dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6677dab2e251SBlaise Bourdin   }
6678c58f1c22SToby Isaac   PetscFunctionReturn(0);
6679c58f1c22SToby Isaac }
6680c58f1c22SToby Isaac 
6681c58f1c22SToby Isaac /*@C
6682c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6683c58f1c22SToby Isaac 
6684c58f1c22SToby Isaac   Not Collective
6685c58f1c22SToby Isaac 
6686c58f1c22SToby Isaac   Input Parameters:
6687c58f1c22SToby Isaac + dm - The DM object
6688c58f1c22SToby Isaac . name - The label name
6689c58f1c22SToby Isaac - value - The stratum value
6690c58f1c22SToby Isaac 
6691c58f1c22SToby Isaac   Output Parameter:
6692c58f1c22SToby Isaac . size - The stratum size
6693c58f1c22SToby Isaac 
6694c58f1c22SToby Isaac   Level: beginner
6695c58f1c22SToby Isaac 
6696c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6697c58f1c22SToby Isaac @*/
6698c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6699c58f1c22SToby Isaac {
6700c58f1c22SToby Isaac   DMLabel        label;
6701c58f1c22SToby Isaac   PetscErrorCode ierr;
6702c58f1c22SToby Isaac 
6703c58f1c22SToby Isaac   PetscFunctionBegin;
6704c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6705c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6706c58f1c22SToby Isaac   PetscValidPointer(size, 4);
6707c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6708c58f1c22SToby Isaac   *size = 0;
6709c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6710c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6711c58f1c22SToby Isaac   PetscFunctionReturn(0);
6712c58f1c22SToby Isaac }
6713c58f1c22SToby Isaac 
6714c58f1c22SToby Isaac /*@C
6715c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6716c58f1c22SToby Isaac 
6717c58f1c22SToby Isaac   Not Collective
6718c58f1c22SToby Isaac 
6719c58f1c22SToby Isaac   Input Parameters:
6720c58f1c22SToby Isaac + dm - The DM object
6721c58f1c22SToby Isaac . name - The label name
6722c58f1c22SToby Isaac - value - The stratum value
6723c58f1c22SToby Isaac 
6724c58f1c22SToby Isaac   Output Parameter:
6725c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6726c58f1c22SToby Isaac 
6727c58f1c22SToby Isaac   Level: beginner
6728c58f1c22SToby Isaac 
6729c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6730c58f1c22SToby Isaac @*/
6731c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6732c58f1c22SToby Isaac {
6733c58f1c22SToby Isaac   DMLabel        label;
6734c58f1c22SToby Isaac   PetscErrorCode ierr;
6735c58f1c22SToby Isaac 
6736c58f1c22SToby Isaac   PetscFunctionBegin;
6737c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6738c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6739c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6740c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6741c58f1c22SToby Isaac   *points = NULL;
6742c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6743c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6744c58f1c22SToby Isaac   PetscFunctionReturn(0);
6745c58f1c22SToby Isaac }
6746c58f1c22SToby Isaac 
67474de306b1SToby Isaac /*@C
67489044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
67494de306b1SToby Isaac 
67504de306b1SToby Isaac   Not Collective
67514de306b1SToby Isaac 
67524de306b1SToby Isaac   Input Parameters:
67534de306b1SToby Isaac + dm - The DM object
67544de306b1SToby Isaac . name - The label name
67554de306b1SToby Isaac . value - The stratum value
67564de306b1SToby Isaac - points - The stratum points
67574de306b1SToby Isaac 
67584de306b1SToby Isaac   Level: beginner
67594de306b1SToby Isaac 
67604de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
67614de306b1SToby Isaac @*/
67624de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
67634de306b1SToby Isaac {
67644de306b1SToby Isaac   DMLabel        label;
67654de306b1SToby Isaac   PetscErrorCode ierr;
67664de306b1SToby Isaac 
67674de306b1SToby Isaac   PetscFunctionBegin;
67684de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67694de306b1SToby Isaac   PetscValidCharPointer(name, 2);
67704de306b1SToby Isaac   PetscValidPointer(points, 4);
67714de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
67724de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
67734de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
67744de306b1SToby Isaac   PetscFunctionReturn(0);
67754de306b1SToby Isaac }
67764de306b1SToby Isaac 
6777c58f1c22SToby Isaac /*@C
6778c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6779c58f1c22SToby Isaac 
6780c58f1c22SToby Isaac   Not Collective
6781c58f1c22SToby Isaac 
6782c58f1c22SToby Isaac   Input Parameters:
6783c58f1c22SToby Isaac + dm   - The DM object
6784c58f1c22SToby Isaac . name - The label name
6785c58f1c22SToby Isaac - value - The label value for this point
6786c58f1c22SToby Isaac 
6787c58f1c22SToby Isaac   Output Parameter:
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac   Level: beginner
6790c58f1c22SToby Isaac 
6791c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6792c58f1c22SToby Isaac @*/
6793c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6794c58f1c22SToby Isaac {
6795c58f1c22SToby Isaac   DMLabel        label;
6796c58f1c22SToby Isaac   PetscErrorCode ierr;
6797c58f1c22SToby Isaac 
6798c58f1c22SToby Isaac   PetscFunctionBegin;
6799c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6800c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6801c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6802c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6803c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6804c58f1c22SToby Isaac   PetscFunctionReturn(0);
6805c58f1c22SToby Isaac }
6806c58f1c22SToby Isaac 
6807c58f1c22SToby Isaac /*@
6808c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6809c58f1c22SToby Isaac 
6810c58f1c22SToby Isaac   Not Collective
6811c58f1c22SToby Isaac 
6812c58f1c22SToby Isaac   Input Parameter:
6813c58f1c22SToby Isaac . dm   - The DM object
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac   Output Parameter:
6816c58f1c22SToby Isaac . numLabels - the number of Labels
6817c58f1c22SToby Isaac 
6818c58f1c22SToby Isaac   Level: intermediate
6819c58f1c22SToby Isaac 
6820c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6821c58f1c22SToby Isaac @*/
6822c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6823c58f1c22SToby Isaac {
6824c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6825c58f1c22SToby Isaac   PetscInt  n    = 0;
6826c58f1c22SToby Isaac 
6827c58f1c22SToby Isaac   PetscFunctionBegin;
6828c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6829c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
6830c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6831c58f1c22SToby Isaac   *numLabels = n;
6832c58f1c22SToby Isaac   PetscFunctionReturn(0);
6833c58f1c22SToby Isaac }
6834c58f1c22SToby Isaac 
6835c58f1c22SToby Isaac /*@C
6836c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6837c58f1c22SToby Isaac 
6838c58f1c22SToby Isaac   Not Collective
6839c58f1c22SToby Isaac 
6840c58f1c22SToby Isaac   Input Parameters:
6841c58f1c22SToby Isaac + dm - The DM object
6842c58f1c22SToby Isaac - n  - the label number
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Output Parameter:
6845c58f1c22SToby Isaac . name - the label name
6846c58f1c22SToby Isaac 
6847c58f1c22SToby Isaac   Level: intermediate
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6850c58f1c22SToby Isaac @*/
6851c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6852c58f1c22SToby Isaac {
6853c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6854c58f1c22SToby Isaac   PetscInt       l    = 0;
6855d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac   PetscFunctionBegin;
6858c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6859c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6860c58f1c22SToby Isaac   while (next) {
6861c58f1c22SToby Isaac     if (l == n) {
6862d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6863c58f1c22SToby Isaac       PetscFunctionReturn(0);
6864c58f1c22SToby Isaac     }
6865c58f1c22SToby Isaac     ++l;
6866c58f1c22SToby Isaac     next = next->next;
6867c58f1c22SToby Isaac   }
6868c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6869c58f1c22SToby Isaac }
6870c58f1c22SToby Isaac 
6871c58f1c22SToby Isaac /*@C
6872c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac   Not Collective
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac   Input Parameters:
6877c58f1c22SToby Isaac + dm   - The DM object
6878c58f1c22SToby Isaac - name - The label name
6879c58f1c22SToby Isaac 
6880c58f1c22SToby Isaac   Output Parameter:
6881c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6882c58f1c22SToby Isaac 
6883c58f1c22SToby Isaac   Level: intermediate
6884c58f1c22SToby Isaac 
6885c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6886c58f1c22SToby Isaac @*/
6887c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6888c58f1c22SToby Isaac {
6889c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6890d67d17b1SMatthew G. Knepley   const char    *lname;
6891c58f1c22SToby Isaac   PetscErrorCode ierr;
6892c58f1c22SToby Isaac 
6893c58f1c22SToby Isaac   PetscFunctionBegin;
6894c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6895c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6896c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
6897c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6898c58f1c22SToby Isaac   while (next) {
6899d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6900d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
6901c58f1c22SToby Isaac     if (*hasLabel) break;
6902c58f1c22SToby Isaac     next = next->next;
6903c58f1c22SToby Isaac   }
6904c58f1c22SToby Isaac   PetscFunctionReturn(0);
6905c58f1c22SToby Isaac }
6906c58f1c22SToby Isaac 
6907c58f1c22SToby Isaac /*@C
6908c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6909c58f1c22SToby Isaac 
6910c58f1c22SToby Isaac   Not Collective
6911c58f1c22SToby Isaac 
6912c58f1c22SToby Isaac   Input Parameters:
6913c58f1c22SToby Isaac + dm   - The DM object
6914c58f1c22SToby Isaac - name - The label name
6915c58f1c22SToby Isaac 
6916c58f1c22SToby Isaac   Output Parameter:
6917c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6918c58f1c22SToby Isaac 
6919c58f1c22SToby Isaac   Level: intermediate
6920c58f1c22SToby Isaac 
6921c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6922c58f1c22SToby Isaac @*/
6923c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6924c58f1c22SToby Isaac {
6925c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6926c58f1c22SToby Isaac   PetscBool      hasLabel;
6927d67d17b1SMatthew G. Knepley   const char    *lname;
6928c58f1c22SToby Isaac   PetscErrorCode ierr;
6929c58f1c22SToby Isaac 
6930c58f1c22SToby Isaac   PetscFunctionBegin;
6931c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6932c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6933c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6934c58f1c22SToby Isaac   *label = NULL;
6935c58f1c22SToby Isaac   while (next) {
6936d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6937d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6938c58f1c22SToby Isaac     if (hasLabel) {
6939c58f1c22SToby Isaac       *label = next->label;
6940c58f1c22SToby Isaac       break;
6941c58f1c22SToby Isaac     }
6942c58f1c22SToby Isaac     next = next->next;
6943c58f1c22SToby Isaac   }
6944c58f1c22SToby Isaac   PetscFunctionReturn(0);
6945c58f1c22SToby Isaac }
6946c58f1c22SToby Isaac 
6947c58f1c22SToby Isaac /*@C
6948c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6949c58f1c22SToby Isaac 
6950c58f1c22SToby Isaac   Not Collective
6951c58f1c22SToby Isaac 
6952c58f1c22SToby Isaac   Input Parameters:
6953c58f1c22SToby Isaac + dm - The DM object
6954c58f1c22SToby Isaac - n  - the label number
6955c58f1c22SToby Isaac 
6956c58f1c22SToby Isaac   Output Parameter:
6957c58f1c22SToby Isaac . label - the label
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Level: intermediate
6960c58f1c22SToby Isaac 
6961c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6962c58f1c22SToby Isaac @*/
6963c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6964c58f1c22SToby Isaac {
6965c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6966c58f1c22SToby Isaac   PetscInt    l    = 0;
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   PetscFunctionBegin;
6969c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6970c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6971c58f1c22SToby Isaac   while (next) {
6972c58f1c22SToby Isaac     if (l == n) {
6973c58f1c22SToby Isaac       *label = next->label;
6974c58f1c22SToby Isaac       PetscFunctionReturn(0);
6975c58f1c22SToby Isaac     }
6976c58f1c22SToby Isaac     ++l;
6977c58f1c22SToby Isaac     next = next->next;
6978c58f1c22SToby Isaac   }
6979c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6980c58f1c22SToby Isaac }
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac /*@C
6983c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   Not Collective
6986c58f1c22SToby Isaac 
6987c58f1c22SToby Isaac   Input Parameters:
6988c58f1c22SToby Isaac + dm   - The DM object
6989c58f1c22SToby Isaac - label - The DMLabel
6990c58f1c22SToby Isaac 
6991c58f1c22SToby Isaac   Level: developer
6992c58f1c22SToby Isaac 
6993c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6994c58f1c22SToby Isaac @*/
6995c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6996c58f1c22SToby Isaac {
6997c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
6998c58f1c22SToby Isaac   PetscBool      hasLabel;
6999d67d17b1SMatthew G. Knepley   const char    *lname;
7000c58f1c22SToby Isaac   PetscErrorCode ierr;
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac   PetscFunctionBegin;
7003c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7004d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7005d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7006d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7007c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7008c58f1c22SToby Isaac   tmpLabel->label  = label;
7009c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
7010c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
7011c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
7012c58f1c22SToby Isaac   PetscFunctionReturn(0);
7013c58f1c22SToby Isaac }
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac /*@C
7016c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac   Not Collective
7019c58f1c22SToby Isaac 
7020c58f1c22SToby Isaac   Input Parameters:
7021c58f1c22SToby Isaac + dm   - The DM object
7022c58f1c22SToby Isaac - name - The label name
7023c58f1c22SToby Isaac 
7024c58f1c22SToby Isaac   Output Parameter:
7025c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7026c58f1c22SToby Isaac 
7027c58f1c22SToby Isaac   Level: developer
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7030c58f1c22SToby Isaac @*/
7031c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7032c58f1c22SToby Isaac {
7033c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7034c58f1c22SToby Isaac   DMLabelLink    last = NULL;
7035c58f1c22SToby Isaac   PetscBool      hasLabel;
7036d67d17b1SMatthew G. Knepley   const char    *lname;
7037c58f1c22SToby Isaac   PetscErrorCode ierr;
7038c58f1c22SToby Isaac 
7039c58f1c22SToby Isaac   PetscFunctionBegin;
7040c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7041c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
7042c58f1c22SToby Isaac   *label = NULL;
7043c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
7044c58f1c22SToby Isaac   while (next) {
7045d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7046d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7047c58f1c22SToby Isaac     if (hasLabel) {
7048c58f1c22SToby Isaac       if (last) last->next       = next->next;
7049c58f1c22SToby Isaac       else      dm->labels->next = next->next;
7050c58f1c22SToby Isaac       next->next = NULL;
7051c58f1c22SToby Isaac       *label     = next->label;
7052c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
7053c58f1c22SToby Isaac       if (hasLabel) {
7054c58f1c22SToby Isaac         dm->depthLabel = NULL;
7055c58f1c22SToby Isaac       }
7056c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
7057c58f1c22SToby Isaac       break;
7058c58f1c22SToby Isaac     }
7059c58f1c22SToby Isaac     last = next;
7060c58f1c22SToby Isaac     next = next->next;
7061c58f1c22SToby Isaac   }
7062c58f1c22SToby Isaac   PetscFunctionReturn(0);
7063c58f1c22SToby Isaac }
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac /*@C
7066c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7067c58f1c22SToby Isaac 
7068c58f1c22SToby Isaac   Not Collective
7069c58f1c22SToby Isaac 
7070c58f1c22SToby Isaac   Input Parameters:
7071c58f1c22SToby Isaac + dm   - The DM object
7072c58f1c22SToby Isaac - name - The label name
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac   Output Parameter:
7075c58f1c22SToby Isaac . output - The flag for output
7076c58f1c22SToby Isaac 
7077c58f1c22SToby Isaac   Level: developer
7078c58f1c22SToby Isaac 
7079c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7080c58f1c22SToby Isaac @*/
7081c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7082c58f1c22SToby Isaac {
7083c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7084d67d17b1SMatthew G. Knepley   const char    *lname;
7085c58f1c22SToby Isaac   PetscErrorCode ierr;
7086c58f1c22SToby Isaac 
7087c58f1c22SToby Isaac   PetscFunctionBegin;
7088c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7089c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7090c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7091c58f1c22SToby Isaac   while (next) {
7092c58f1c22SToby Isaac     PetscBool flg;
7093c58f1c22SToby Isaac 
7094d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7095d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7096c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7097c58f1c22SToby Isaac     next = next->next;
7098c58f1c22SToby Isaac   }
7099c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7100c58f1c22SToby Isaac }
7101c58f1c22SToby Isaac 
7102c58f1c22SToby Isaac /*@C
7103c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7104c58f1c22SToby Isaac 
7105c58f1c22SToby Isaac   Not Collective
7106c58f1c22SToby Isaac 
7107c58f1c22SToby Isaac   Input Parameters:
7108c58f1c22SToby Isaac + dm     - The DM object
7109c58f1c22SToby Isaac . name   - The label name
7110c58f1c22SToby Isaac - output - The flag for output
7111c58f1c22SToby Isaac 
7112c58f1c22SToby Isaac   Level: developer
7113c58f1c22SToby Isaac 
7114c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7115c58f1c22SToby Isaac @*/
7116c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7117c58f1c22SToby Isaac {
7118c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7119d67d17b1SMatthew G. Knepley   const char    *lname;
7120c58f1c22SToby Isaac   PetscErrorCode ierr;
7121c58f1c22SToby Isaac 
7122c58f1c22SToby Isaac   PetscFunctionBegin;
7123c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7124c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7125c58f1c22SToby Isaac   while (next) {
7126c58f1c22SToby Isaac     PetscBool flg;
7127c58f1c22SToby Isaac 
7128d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7129d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7130c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7131c58f1c22SToby Isaac     next = next->next;
7132c58f1c22SToby Isaac   }
7133c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7134c58f1c22SToby Isaac }
7135c58f1c22SToby Isaac 
7136c58f1c22SToby Isaac 
7137c58f1c22SToby Isaac /*@
7138c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7139c58f1c22SToby Isaac 
7140d083f849SBarry Smith   Collective on dmA
7141c58f1c22SToby Isaac 
7142c58f1c22SToby Isaac   Input Parameter:
71432e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
7144c58f1c22SToby Isaac 
7145c58f1c22SToby Isaac   Output Parameter:
71462e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
7147c58f1c22SToby Isaac 
7148c58f1c22SToby Isaac   Level: intermediate
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7151c58f1c22SToby Isaac 
7152367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
7153c58f1c22SToby Isaac @*/
7154c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
7155c58f1c22SToby Isaac {
7156c58f1c22SToby Isaac   PetscInt       numLabels, l;
7157c58f1c22SToby Isaac   PetscErrorCode ierr;
7158c58f1c22SToby Isaac 
7159c58f1c22SToby Isaac   PetscFunctionBegin;
7160c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
7161c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
7162c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
7163c58f1c22SToby Isaac     DMLabel     label, labelNew;
7164c58f1c22SToby Isaac     const char *name;
7165c58f1c22SToby Isaac     PetscBool   flg;
7166c58f1c22SToby Isaac 
7167c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
7168c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7169c58f1c22SToby Isaac     if (flg) continue;
71707d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
71717d5acc75SStefano Zampini     if (flg) continue;
7172c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
7173c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
7174c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
7175c58f1c22SToby Isaac   }
7176c58f1c22SToby Isaac   PetscFunctionReturn(0);
7177c58f1c22SToby Isaac }
7178a8fb8f29SToby Isaac 
7179a8fb8f29SToby Isaac /*@
7180a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7181a8fb8f29SToby Isaac 
7182a8fb8f29SToby Isaac   Input Parameter:
7183a8fb8f29SToby Isaac . dm - The DM object
7184a8fb8f29SToby Isaac 
7185a8fb8f29SToby Isaac   Output Parameter:
7186a8fb8f29SToby Isaac . cdm - The coarse DM
7187a8fb8f29SToby Isaac 
7188a8fb8f29SToby Isaac   Level: intermediate
7189a8fb8f29SToby Isaac 
7190a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7191a8fb8f29SToby Isaac @*/
7192a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7193a8fb8f29SToby Isaac {
7194a8fb8f29SToby Isaac   PetscFunctionBegin;
7195a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7196a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7197a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7198a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7199a8fb8f29SToby Isaac }
7200a8fb8f29SToby Isaac 
7201a8fb8f29SToby Isaac /*@
7202a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7203a8fb8f29SToby Isaac 
7204a8fb8f29SToby Isaac   Input Parameters:
7205a8fb8f29SToby Isaac + dm - The DM object
7206a8fb8f29SToby Isaac - cdm - The coarse DM
7207a8fb8f29SToby Isaac 
7208a8fb8f29SToby Isaac   Level: intermediate
7209a8fb8f29SToby Isaac 
7210a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7211a8fb8f29SToby Isaac @*/
7212a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7213a8fb8f29SToby Isaac {
7214a8fb8f29SToby Isaac   PetscErrorCode ierr;
7215a8fb8f29SToby Isaac 
7216a8fb8f29SToby Isaac   PetscFunctionBegin;
7217a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7218a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7219a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7220a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7221a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7222a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7223a8fb8f29SToby Isaac }
7224a8fb8f29SToby Isaac 
722588bdff64SToby Isaac /*@
722688bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
722788bdff64SToby Isaac 
722888bdff64SToby Isaac   Input Parameter:
722988bdff64SToby Isaac . dm - The DM object
723088bdff64SToby Isaac 
723188bdff64SToby Isaac   Output Parameter:
723288bdff64SToby Isaac . fdm - The fine DM
723388bdff64SToby Isaac 
723488bdff64SToby Isaac   Level: intermediate
723588bdff64SToby Isaac 
723688bdff64SToby Isaac .seealso: DMSetFineDM()
723788bdff64SToby Isaac @*/
723888bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
723988bdff64SToby Isaac {
724088bdff64SToby Isaac   PetscFunctionBegin;
724188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
724288bdff64SToby Isaac   PetscValidPointer(fdm, 2);
724388bdff64SToby Isaac   *fdm = dm->fineMesh;
724488bdff64SToby Isaac   PetscFunctionReturn(0);
724588bdff64SToby Isaac }
724688bdff64SToby Isaac 
724788bdff64SToby Isaac /*@
724888bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
724988bdff64SToby Isaac 
725088bdff64SToby Isaac   Input Parameters:
725188bdff64SToby Isaac + dm - The DM object
725288bdff64SToby Isaac - fdm - The fine DM
725388bdff64SToby Isaac 
725488bdff64SToby Isaac   Level: intermediate
725588bdff64SToby Isaac 
725688bdff64SToby Isaac .seealso: DMGetFineDM()
725788bdff64SToby Isaac @*/
725888bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
725988bdff64SToby Isaac {
726088bdff64SToby Isaac   PetscErrorCode ierr;
726188bdff64SToby Isaac 
726288bdff64SToby Isaac   PetscFunctionBegin;
726388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
726488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
726588bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
726688bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
726788bdff64SToby Isaac   dm->fineMesh = fdm;
726888bdff64SToby Isaac   PetscFunctionReturn(0);
726988bdff64SToby Isaac }
727088bdff64SToby Isaac 
7271a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7272a6ba4734SToby Isaac 
7273a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7274a6ba4734SToby Isaac {
7275e5e52638SMatthew G. Knepley   PetscInt       d;
7276a6ba4734SToby Isaac   PetscErrorCode ierr;
7277a6ba4734SToby Isaac 
7278a6ba4734SToby Isaac   PetscFunctionBegin;
7279e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7280e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7281e5e52638SMatthew G. Knepley   }
7282a6ba4734SToby Isaac   PetscFunctionReturn(0);
7283a6ba4734SToby Isaac }
7284a6ba4734SToby Isaac 
7285a6ba4734SToby Isaac /*@C
7286a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7287a6ba4734SToby Isaac 
7288a6ba4734SToby Isaac   Input Parameters:
72894c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7290f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7291a6ba4734SToby Isaac . name        - The BC name
7292a6ba4734SToby Isaac . labelname   - The label defining constrained points
7293a6ba4734SToby Isaac . field       - The field to constrain
7294e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7295a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7296a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7297a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7298a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7299a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7300a6ba4734SToby Isaac 
7301a6ba4734SToby Isaac   Options Database Keys:
7302a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7303a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7304a6ba4734SToby Isaac 
7305a6ba4734SToby Isaac   Level: developer
7306a6ba4734SToby Isaac 
7307a6ba4734SToby Isaac .seealso: DMGetBoundary()
7308a6ba4734SToby Isaac @*/
7309a30ec4eaSSatish 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)
7310a6ba4734SToby Isaac {
7311e5e52638SMatthew G. Knepley   PetscDS        ds;
7312a6ba4734SToby Isaac   PetscErrorCode ierr;
7313a6ba4734SToby Isaac 
7314a6ba4734SToby Isaac   PetscFunctionBegin;
7315a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7316e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7317e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7318a6ba4734SToby Isaac   PetscFunctionReturn(0);
7319a6ba4734SToby Isaac }
7320a6ba4734SToby Isaac 
7321a6ba4734SToby Isaac /*@
7322a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7323a6ba4734SToby Isaac 
7324a6ba4734SToby Isaac   Input Parameters:
7325a6ba4734SToby Isaac . dm - The mesh object
7326a6ba4734SToby Isaac 
7327a6ba4734SToby Isaac   Output Parameters:
7328a6ba4734SToby Isaac . numBd - The number of BC
7329a6ba4734SToby Isaac 
7330a6ba4734SToby Isaac   Level: intermediate
7331a6ba4734SToby Isaac 
7332a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7333a6ba4734SToby Isaac @*/
7334a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7335a6ba4734SToby Isaac {
7336e5e52638SMatthew G. Knepley   PetscDS        ds;
733758ebd649SToby Isaac   PetscErrorCode ierr;
7338a6ba4734SToby Isaac 
7339a6ba4734SToby Isaac   PetscFunctionBegin;
7340a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7341e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7342e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7343a6ba4734SToby Isaac   PetscFunctionReturn(0);
7344a6ba4734SToby Isaac }
7345a6ba4734SToby Isaac 
7346a6ba4734SToby Isaac /*@C
73471c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7348a6ba4734SToby Isaac 
7349a6ba4734SToby Isaac   Input Parameters:
7350a6ba4734SToby Isaac + dm          - The mesh object
7351a6ba4734SToby Isaac - bd          - The BC number
7352a6ba4734SToby Isaac 
7353a6ba4734SToby Isaac   Output Parameters:
7354f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7355a6ba4734SToby Isaac . name        - The BC name
7356a6ba4734SToby Isaac . labelname   - The label defining constrained points
7357a6ba4734SToby Isaac . field       - The field to constrain
7358a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7359a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7360a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7361a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7362a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7363a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7364a6ba4734SToby Isaac 
7365a6ba4734SToby Isaac   Options Database Keys:
7366a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7367a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7368a6ba4734SToby Isaac 
7369a6ba4734SToby Isaac   Level: developer
7370a6ba4734SToby Isaac 
7371a6ba4734SToby Isaac .seealso: DMAddBoundary()
7372a6ba4734SToby Isaac @*/
7373a30ec4eaSSatish 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)
7374a6ba4734SToby Isaac {
7375e5e52638SMatthew G. Knepley   PetscDS        ds;
737658ebd649SToby Isaac   PetscErrorCode ierr;
7377a6ba4734SToby Isaac 
7378a6ba4734SToby Isaac   PetscFunctionBegin;
7379a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7380e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7381e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7382a6ba4734SToby Isaac   PetscFunctionReturn(0);
7383a6ba4734SToby Isaac }
7384a6ba4734SToby Isaac 
7385e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7386e6f8dbb6SToby Isaac {
7387e5e52638SMatthew G. Knepley   PetscDS        ds;
7388dff059c6SToby Isaac   DMBoundary    *lastnext;
7389e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7390e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7391e6f8dbb6SToby Isaac 
7392e6f8dbb6SToby Isaac   PetscFunctionBegin;
7393e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7394e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
739547a1f5adSToby Isaac   if (dm->boundary) {
739647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
739747a1f5adSToby Isaac 
739847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
739947a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
740047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
740147a1f5adSToby Isaac     while (next) {
740247a1f5adSToby Isaac       DMBoundary b = next;
740347a1f5adSToby Isaac 
740447a1f5adSToby Isaac       next = b->next;
740547a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7406a6ba4734SToby Isaac     }
740747a1f5adSToby Isaac     dm->boundary = NULL;
7408a6ba4734SToby Isaac   }
740947a1f5adSToby Isaac 
7410dff059c6SToby Isaac   lastnext = &(dm->boundary);
7411e6f8dbb6SToby Isaac   while (dsbound) {
7412e6f8dbb6SToby Isaac     DMBoundary dmbound;
7413e6f8dbb6SToby Isaac 
7414e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7415e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7416e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7417994fe344SLisandro 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);}
741847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7419dff059c6SToby Isaac     *lastnext = dmbound;
7420dff059c6SToby Isaac     lastnext = &(dmbound->next);
7421dff059c6SToby Isaac     dsbound = dsbound->next;
7422a6ba4734SToby Isaac   }
7423a6ba4734SToby Isaac   PetscFunctionReturn(0);
7424a6ba4734SToby Isaac }
7425a6ba4734SToby Isaac 
7426a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7427a6ba4734SToby Isaac {
7428b95f2879SToby Isaac   DMBoundary     b;
7429a6ba4734SToby Isaac   PetscErrorCode ierr;
7430a6ba4734SToby Isaac 
7431a6ba4734SToby Isaac   PetscFunctionBegin;
7432a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7433a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
7434a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7435e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7436b95f2879SToby Isaac   b = dm->boundary;
7437a6ba4734SToby Isaac   while (b && !(*isBd)) {
7438e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7439e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
74403424c85cSToby Isaac 
74413424c85cSToby Isaac     if (label) {
7442a6ba4734SToby Isaac       PetscInt i;
7443a6ba4734SToby Isaac 
7444e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7445e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7446a6ba4734SToby Isaac       }
7447a6ba4734SToby Isaac     }
7448a6ba4734SToby Isaac     b = b->next;
7449a6ba4734SToby Isaac   }
7450a6ba4734SToby Isaac   PetscFunctionReturn(0);
7451a6ba4734SToby Isaac }
74524d6f44ffSToby Isaac 
74534d6f44ffSToby Isaac /*@C
74544d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
74554d6f44ffSToby Isaac 
74564d6f44ffSToby Isaac   Input Parameters:
74574d6f44ffSToby Isaac + dm      - The DM
74580709b2feSToby Isaac . time    - The time
74594d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
74604d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
74614d6f44ffSToby Isaac - mode    - The insertion mode for values
74624d6f44ffSToby Isaac 
74634d6f44ffSToby Isaac   Output Parameter:
74644d6f44ffSToby Isaac . X - vector
74654d6f44ffSToby Isaac 
74664d6f44ffSToby Isaac    Calling sequence of func:
74670709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
74684d6f44ffSToby Isaac 
74694d6f44ffSToby Isaac +  dim - The spatial dimension
74704d6f44ffSToby Isaac .  x   - The coordinates
74714d6f44ffSToby Isaac .  Nf  - The number of fields
74724d6f44ffSToby Isaac .  u   - The output field values
74734d6f44ffSToby Isaac -  ctx - optional user-defined function context
74744d6f44ffSToby Isaac 
74754d6f44ffSToby Isaac   Level: developer
74764d6f44ffSToby Isaac 
74772716604bSToby Isaac .seealso: DMComputeL2Diff()
74784d6f44ffSToby Isaac @*/
74790709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
74804d6f44ffSToby Isaac {
74814d6f44ffSToby Isaac   Vec            localX;
74824d6f44ffSToby Isaac   PetscErrorCode ierr;
74834d6f44ffSToby Isaac 
74844d6f44ffSToby Isaac   PetscFunctionBegin;
74854d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74864d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
74870709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
74884d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
74894d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
74904d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
74914d6f44ffSToby Isaac   PetscFunctionReturn(0);
74924d6f44ffSToby Isaac }
74934d6f44ffSToby Isaac 
74940709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
74954d6f44ffSToby Isaac {
74964d6f44ffSToby Isaac   PetscErrorCode ierr;
74974d6f44ffSToby Isaac 
74984d6f44ffSToby Isaac   PetscFunctionBegin;
74994d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75004d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
75010918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
75020709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75034d6f44ffSToby Isaac   PetscFunctionReturn(0);
75044d6f44ffSToby Isaac }
75054d6f44ffSToby Isaac 
75062c53366bSMatthew 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)
75072c53366bSMatthew G. Knepley {
75082c53366bSMatthew G. Knepley   Vec            localX;
75092c53366bSMatthew G. Knepley   PetscErrorCode ierr;
75102c53366bSMatthew G. Knepley 
75112c53366bSMatthew G. Knepley   PetscFunctionBegin;
75122c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75132c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
75142c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75152c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
75162c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
75172c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
75182c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
75192c53366bSMatthew G. Knepley }
75202c53366bSMatthew G. Knepley 
75211c531cf8SMatthew 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)
75224d6f44ffSToby Isaac {
75234d6f44ffSToby Isaac   PetscErrorCode ierr;
75244d6f44ffSToby Isaac 
75254d6f44ffSToby Isaac   PetscFunctionBegin;
75264d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75274d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
75280918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
75291c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75304d6f44ffSToby Isaac   PetscFunctionReturn(0);
75314d6f44ffSToby Isaac }
75322716604bSToby Isaac 
75338c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
75348c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
75358c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
75368c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7537191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
75388c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
75398c6c5593SMatthew G. Knepley {
75408c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
75418c6c5593SMatthew G. Knepley 
75428c6c5593SMatthew G. Knepley   PetscFunctionBegin;
75438c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75448c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
75458c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
75460918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
75478c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
75488c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
75498c6c5593SMatthew G. Knepley }
75508c6c5593SMatthew G. Knepley 
75511c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
75528c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
75538c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
75548c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7555191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
75568c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
75578c6c5593SMatthew G. Knepley {
75588c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
75598c6c5593SMatthew G. Knepley 
75608c6c5593SMatthew G. Knepley   PetscFunctionBegin;
75618c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75628c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
75638c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
75640918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
75651c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
75668c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
75678c6c5593SMatthew G. Knepley }
75688c6c5593SMatthew G. Knepley 
75692716604bSToby Isaac /*@C
75702716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
75712716604bSToby Isaac 
75722716604bSToby Isaac   Input Parameters:
75732716604bSToby Isaac + dm    - The DM
75740709b2feSToby Isaac . time  - The time
75752716604bSToby Isaac . funcs - The functions to evaluate for each field component
75762716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7577574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
75782716604bSToby Isaac 
75792716604bSToby Isaac   Output Parameter:
75802716604bSToby Isaac . diff - The diff ||u - u_h||_2
75812716604bSToby Isaac 
75822716604bSToby Isaac   Level: developer
75832716604bSToby Isaac 
75841189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
75852716604bSToby Isaac @*/
75860709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
75872716604bSToby Isaac {
75882716604bSToby Isaac   PetscErrorCode ierr;
75892716604bSToby Isaac 
75902716604bSToby Isaac   PetscFunctionBegin;
75912716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7592b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
75930918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
75940709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
75952716604bSToby Isaac   PetscFunctionReturn(0);
75962716604bSToby Isaac }
7597b698f381SToby Isaac 
7598b698f381SToby Isaac /*@C
7599b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7600b698f381SToby Isaac 
7601d083f849SBarry Smith   Collective on dm
7602d083f849SBarry Smith 
7603b698f381SToby Isaac   Input Parameters:
7604b698f381SToby Isaac + dm    - The DM
7605b698f381SToby Isaac , time  - The time
7606b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
7607b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7608574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
7609b698f381SToby Isaac - n     - The vector to project along
7610b698f381SToby Isaac 
7611b698f381SToby Isaac   Output Parameter:
7612b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
7613b698f381SToby Isaac 
7614b698f381SToby Isaac   Level: developer
7615b698f381SToby Isaac 
7616b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
7617b698f381SToby Isaac @*/
7618b698f381SToby 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)
7619b698f381SToby Isaac {
7620b698f381SToby Isaac   PetscErrorCode ierr;
7621b698f381SToby Isaac 
7622b698f381SToby Isaac   PetscFunctionBegin;
7623b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7624b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7625b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7626b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7627b698f381SToby Isaac   PetscFunctionReturn(0);
7628b698f381SToby Isaac }
7629b698f381SToby Isaac 
76302a16baeaSToby Isaac /*@C
76312a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
76322a16baeaSToby Isaac 
7633d083f849SBarry Smith   Collective on dm
7634d083f849SBarry Smith 
76352a16baeaSToby Isaac   Input Parameters:
76362a16baeaSToby Isaac + dm    - The DM
76372a16baeaSToby Isaac . time  - The time
76382a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
76392a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7640574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
76412a16baeaSToby Isaac 
76422a16baeaSToby Isaac   Output Parameter:
76432a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
76442a16baeaSToby Isaac 
76452a16baeaSToby Isaac   Level: developer
76462a16baeaSToby Isaac 
76471189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
76482a16baeaSToby Isaac @*/
76491189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
76502a16baeaSToby Isaac {
76512a16baeaSToby Isaac   PetscErrorCode ierr;
76522a16baeaSToby Isaac 
76532a16baeaSToby Isaac   PetscFunctionBegin;
76542a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76552a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
76560918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
76572a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
76582a16baeaSToby Isaac   PetscFunctionReturn(0);
76592a16baeaSToby Isaac }
76602a16baeaSToby Isaac 
7661df0b854cSToby Isaac /*@C
7662df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7663cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7664df0b854cSToby Isaac 
7665df0b854cSToby Isaac   Collective on dm
7666df0b854cSToby Isaac 
7667df0b854cSToby Isaac   Input parameters:
7668df0b854cSToby Isaac + dm - the pre-adaptation DM object
7669a1b0c543SToby Isaac - label - label with the flags
7670df0b854cSToby Isaac 
7671df0b854cSToby Isaac   Output parameters:
76720d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7673df0b854cSToby Isaac 
7674df0b854cSToby Isaac   Level: intermediate
76750d1cd5e0SMatthew G. Knepley 
76760d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7677df0b854cSToby Isaac @*/
76780d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7679df0b854cSToby Isaac {
7680df0b854cSToby Isaac   PetscErrorCode ierr;
7681df0b854cSToby Isaac 
7682df0b854cSToby Isaac   PetscFunctionBegin;
7683df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7684a1b0c543SToby Isaac   PetscValidPointer(label,2);
76850d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
76860d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
76876f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
76880d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
76890d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
76900d1cd5e0SMatthew G. Knepley }
76910d1cd5e0SMatthew G. Knepley 
76920d1cd5e0SMatthew G. Knepley /*@C
76930d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
76940d1cd5e0SMatthew G. Knepley 
76950d1cd5e0SMatthew G. Knepley   Input Parameters:
76960d1cd5e0SMatthew G. Knepley + dm - The DM object
76970d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
76986f25b0d8SLisandro 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_".
76990d1cd5e0SMatthew G. Knepley 
77000d1cd5e0SMatthew G. Knepley   Output Parameter:
77010d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
77020d1cd5e0SMatthew G. Knepley 
77030d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
77040d1cd5e0SMatthew G. Knepley 
77050d1cd5e0SMatthew G. Knepley   Level: advanced
77060d1cd5e0SMatthew G. Knepley 
77070d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
77080d1cd5e0SMatthew G. Knepley @*/
77090d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
77100d1cd5e0SMatthew G. Knepley {
77110d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
77120d1cd5e0SMatthew G. Knepley 
77130d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
77140d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77150d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
77160d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
77170d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
77186f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
77196f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
77200d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7721df0b854cSToby Isaac   PetscFunctionReturn(0);
7722df0b854cSToby Isaac }
7723c4088d22SMatthew G. Knepley 
7724502a2867SDave May /*@C
7725502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7726502a2867SDave May 
7727502a2867SDave May  Not Collective
7728502a2867SDave May 
7729502a2867SDave May  Input Parameter:
7730502a2867SDave May  . dm    - The DM
7731502a2867SDave May 
7732502a2867SDave May  Output Parameter:
7733502a2867SDave May  . nranks - the number of neighbours
7734502a2867SDave May  . ranks - the neighbors ranks
7735502a2867SDave May 
7736502a2867SDave May  Notes:
7737502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
7738502a2867SDave May 
7739502a2867SDave May  Level: beginner
7740502a2867SDave May 
7741dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
7742502a2867SDave May @*/
7743502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7744502a2867SDave May {
7745502a2867SDave May   PetscErrorCode ierr;
7746502a2867SDave May 
7747502a2867SDave May   PetscFunctionBegin;
7748502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77490918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7750502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7751502a2867SDave May   PetscFunctionReturn(0);
7752502a2867SDave May }
7753502a2867SDave May 
7754531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7755531c7667SBarry Smith 
7756531c7667SBarry Smith /*
7757531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7758531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
7759531c7667SBarry Smith */
7760531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7761531c7667SBarry Smith {
7762531c7667SBarry Smith   PetscErrorCode ierr;
7763531c7667SBarry Smith 
7764531c7667SBarry Smith   PetscFunctionBegin;
7765531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7766531c7667SBarry Smith     Vec x1local;
7767531c7667SBarry Smith     DM  dm;
7768531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7769531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7770531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7771531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7772531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7773531c7667SBarry Smith     x1   = x1local;
7774531c7667SBarry Smith   }
7775531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7776531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7777531c7667SBarry Smith     DM  dm;
7778531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7779531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7780531c7667SBarry Smith   }
7781531c7667SBarry Smith   PetscFunctionReturn(0);
7782531c7667SBarry Smith }
7783531c7667SBarry Smith 
7784531c7667SBarry Smith /*@
7785531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7786531c7667SBarry Smith 
7787531c7667SBarry Smith     Input Parameter:
7788531c7667SBarry Smith .    coloring - the MatFDColoring object
7789531c7667SBarry Smith 
779095452b02SPatrick Sanan     Developer Notes:
779195452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
7792531c7667SBarry Smith 
77931b266c99SBarry Smith     Level: advanced
77941b266c99SBarry Smith 
7795531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7796531c7667SBarry Smith @*/
7797531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7798531c7667SBarry Smith {
7799531c7667SBarry Smith   PetscFunctionBegin;
7800531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7801531c7667SBarry Smith   PetscFunctionReturn(0);
7802531c7667SBarry Smith }
78038320bc6fSPatrick Sanan 
78048320bc6fSPatrick Sanan /*@
78058320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
78068320bc6fSPatrick Sanan 
78078320bc6fSPatrick Sanan     Collective
78088320bc6fSPatrick Sanan 
78098320bc6fSPatrick Sanan     Input Parameters:
78108320bc6fSPatrick Sanan +    dm - the first DM
78118320bc6fSPatrick Sanan -    dm2 - the second DM
78128320bc6fSPatrick Sanan 
78138320bc6fSPatrick Sanan     Output Parameters:
78148320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
78158320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
78168320bc6fSPatrick Sanan 
78178320bc6fSPatrick Sanan     Notes:
78188320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
78193d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
78208320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
78213d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
78223d862458SPatrick Sanan     decomposition, but hold different data.
78238320bc6fSPatrick Sanan 
78248320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
78258320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
78268320bc6fSPatrick Sanan 
78278320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
78288320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
78298320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
78308320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
78318320bc6fSPatrick Sanan 
78328320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
78338320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
78348320bc6fSPatrick Sanan .vb
78358320bc6fSPatrick Sanan   ...
78368320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
78378320bc6fSPatrick Sanan   if (set && compatible)  {
78388320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
78398320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
78403d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
78418320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
78428320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
78438320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
78448320bc6fSPatrick Sanan       }
78458320bc6fSPatrick Sanan     }
78468320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
78478320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
78488320bc6fSPatrick Sanan   } else {
78498320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
78508320bc6fSPatrick Sanan   }
78518320bc6fSPatrick Sanan   ...
78528320bc6fSPatrick Sanan .ve
78538320bc6fSPatrick Sanan 
78548320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
78558320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
78568320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
78578320bc6fSPatrick Sanan     always check the "set" output parameter.
78588320bc6fSPatrick Sanan 
78598320bc6fSPatrick Sanan     A DM is always compatible with itself.
78608320bc6fSPatrick Sanan 
78618320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
78628320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
78638320bc6fSPatrick Sanan     incompatible.
78648320bc6fSPatrick Sanan 
78658320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
78668320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
78678320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
78688320bc6fSPatrick Sanan 
78698320bc6fSPatrick Sanan     Developer Notes:
78703d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
78713d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
78728320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
78738320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
78748320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
78753d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
78763d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
78778320bc6fSPatrick Sanan 
78788320bc6fSPatrick Sanan     Level: advanced
78798320bc6fSPatrick Sanan 
78803d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
78818320bc6fSPatrick Sanan @*/
78828320bc6fSPatrick Sanan 
78838320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
78848320bc6fSPatrick Sanan {
78858320bc6fSPatrick Sanan   PetscErrorCode ierr;
78868320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
78878320bc6fSPatrick Sanan   DMType         type,type2;
78888320bc6fSPatrick Sanan   PetscBool      sameType;
78898320bc6fSPatrick Sanan 
78908320bc6fSPatrick Sanan   PetscFunctionBegin;
78918320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78928320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
78938320bc6fSPatrick Sanan 
78948320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
78958320bc6fSPatrick Sanan   if (dm == dm2) {
78968320bc6fSPatrick Sanan     *set = PETSC_TRUE;
78978320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
78988320bc6fSPatrick Sanan     PetscFunctionReturn(0);
78998320bc6fSPatrick Sanan   }
79008320bc6fSPatrick Sanan 
79018320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
79028320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
79038320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
79048320bc6fSPatrick Sanan      determined by the implementation-specific logic */
79058320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
79068320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
79078320bc6fSPatrick Sanan     *set = PETSC_TRUE;
79088320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
79098320bc6fSPatrick Sanan     PetscFunctionReturn(0);
79108320bc6fSPatrick Sanan   }
79118320bc6fSPatrick Sanan 
79128320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
79138320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
79148320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
79158320bc6fSPatrick Sanan     if (*set) {
79168320bc6fSPatrick Sanan       PetscFunctionReturn(0);
79178320bc6fSPatrick Sanan     }
79188320bc6fSPatrick Sanan   }
79198320bc6fSPatrick Sanan 
79208320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
79218320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
79228320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
79238320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
79248320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
79258320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
79268320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
79278320bc6fSPatrick Sanan   } else {
79288320bc6fSPatrick Sanan     *set = PETSC_FALSE;
79298320bc6fSPatrick Sanan   }
79308320bc6fSPatrick Sanan   PetscFunctionReturn(0);
79318320bc6fSPatrick Sanan }
7932