xref: /petsc/src/dm/interface/dm.c (revision ca3d3a14de63d3f97e9c05ca58d776b1c45e4686)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
10d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
111ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1267a56275SMatthew G Knepley 
13e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
1440967b3bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_",0};
15bff4a2f0SMatthew G. Knepley 
164a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
174a7a4c06SLawrence Mitchell {
184a7a4c06SLawrence Mitchell   PetscFunctionBegin;
194a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
204a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
214a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
224a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
234a7a4c06SLawrence Mitchell }
244a7a4c06SLawrence Mitchell 
25a4121054SBarry Smith /*@
26de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
27a4121054SBarry Smith 
28a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
29a4121054SBarry Smith    error when you try to use the vector.
30a4121054SBarry Smith 
31a4121054SBarry Smith   Collective on MPI_Comm
32a4121054SBarry Smith 
33a4121054SBarry Smith   Input Parameter:
34a4121054SBarry Smith . comm - The communicator for the DM object
35a4121054SBarry Smith 
36a4121054SBarry Smith   Output Parameter:
37a4121054SBarry Smith . dm - The DM object
38a4121054SBarry Smith 
39a4121054SBarry Smith   Level: beginner
40a4121054SBarry Smith 
418472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
42a4121054SBarry Smith @*/
437087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
44a4121054SBarry Smith {
45a4121054SBarry Smith   DM             v;
46e5e52638SMatthew G. Knepley   PetscDS        ds;
47a4121054SBarry Smith   PetscErrorCode ierr;
48a4121054SBarry Smith 
49a4121054SBarry Smith   PetscFunctionBegin;
501411c6eeSJed Brown   PetscValidPointer(dm,2);
510298fd71SBarry Smith   *dm = NULL;
52607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
53a4121054SBarry Smith 
5473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
55e7c4fc90SDmitry Karpeev 
5649be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5749be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
580298fd71SBarry Smith   v->ltogmap                  = NULL;
591411c6eeSJed Brown   v->bs                       = 1;
60171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
6188ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
6288ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
63c58f1c22SToby Isaac   v->labels                   = NULL;
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 
10438221697SMatthew G. Knepley   Collective on MPI_Comm
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 .keywords: DM, topology, create
11538221697SMatthew G. Knepley @*/
11638221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11738221697SMatthew G. Knepley {
11838221697SMatthew G. Knepley   PetscSF        sf;
11938221697SMatthew G. Knepley   Vec            coords;
12038221697SMatthew G. Knepley   void          *ctx;
121a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12238221697SMatthew G. Knepley   PetscErrorCode ierr;
12338221697SMatthew G. Knepley 
12438221697SMatthew G. Knepley   PetscFunctionBegin;
12538221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12638221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12738221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
128c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
129c58f1c22SToby Isaac   dm->labels->refct++;
130c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
131c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
132ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
133ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1341de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1351de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13638221697SMatthew G. Knepley   if (dm->ops->clone) {
13738221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13838221697SMatthew G. Knepley   }
1393f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
14038221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
14238221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14338221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
144be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
145be4c1c3eSMatthew G. Knepley     DM           ncdm;
146be4c1c3eSMatthew G. Knepley     PetscSection cs;
1475a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
148be4c1c3eSMatthew G. Knepley 
149e87a4003SBarry Smith     ierr = DMGetSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
150be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1515a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1525a0206caSToby Isaac     if (pEndMax >= 0) {
153be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15483bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
155e87a4003SBarry Smith       ierr = DMSetSection(ncdm, cs);CHKERRQ(ierr);
156a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
157be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
158be4c1c3eSMatthew G. Knepley     }
159be4c1c3eSMatthew G. Knepley   }
160a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
161a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
16238221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16338221697SMatthew G. Knepley   if (coords) {
16438221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16538221697SMatthew G. Knepley   } else {
16638221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16738221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16838221697SMatthew G. Knepley   }
16990b157c4SStefano Zampini   {
17090b157c4SStefano Zampini     PetscBool             isper;
171c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1725dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17390b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17490b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
175c6b900c6SMatthew G. Knepley   }
17634aa8a36SMatthew G. Knepley   {
17734aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17834aa8a36SMatthew G. Knepley 
17934aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
18034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
18134aa8a36SMatthew G. Knepley   }
18238221697SMatthew G. Knepley   PetscFunctionReturn(0);
18338221697SMatthew G. Knepley }
18438221697SMatthew G. Knepley 
1859a42bb27SBarry Smith /*@C
186564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1879a42bb27SBarry Smith 
1888472ad0fSDave May    Logically Collective on DM
1899a42bb27SBarry Smith 
1909a42bb27SBarry Smith    Input Parameter:
1919a42bb27SBarry Smith +  da - initial distributed array
192e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1939a42bb27SBarry Smith 
1949a42bb27SBarry Smith    Options Database:
195dd85299cSBarry Smith .   -dm_vec_type ctype
1969a42bb27SBarry Smith 
1979a42bb27SBarry Smith    Level: intermediate
1989a42bb27SBarry Smith 
199a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2009a42bb27SBarry Smith @*/
20119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2029a42bb27SBarry Smith {
2039a42bb27SBarry Smith   PetscErrorCode ierr;
2049a42bb27SBarry Smith 
2059a42bb27SBarry Smith   PetscFunctionBegin;
2069a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2079a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2099a42bb27SBarry Smith   PetscFunctionReturn(0);
2109a42bb27SBarry Smith }
2119a42bb27SBarry Smith 
212c0dedaeaSBarry Smith /*@C
213c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
214c0dedaeaSBarry Smith 
2158472ad0fSDave May    Logically Collective on DM
216c0dedaeaSBarry Smith 
217c0dedaeaSBarry Smith    Input Parameter:
218c0dedaeaSBarry Smith .  da - initial distributed array
219c0dedaeaSBarry Smith 
220c0dedaeaSBarry Smith    Output Parameter:
221c0dedaeaSBarry Smith .  ctype - the vector type
222c0dedaeaSBarry Smith 
223c0dedaeaSBarry Smith    Level: intermediate
224c0dedaeaSBarry Smith 
225a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
226c0dedaeaSBarry Smith @*/
227c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
228c0dedaeaSBarry Smith {
229c0dedaeaSBarry Smith   PetscFunctionBegin;
230c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
231c0dedaeaSBarry Smith   *ctype = da->vectype;
232c0dedaeaSBarry Smith   PetscFunctionReturn(0);
233c0dedaeaSBarry Smith }
234c0dedaeaSBarry Smith 
2355f1ad066SMatthew G Knepley /*@
23634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2375f1ad066SMatthew G Knepley 
2385f1ad066SMatthew G Knepley   Not collective
2395f1ad066SMatthew G Knepley 
2405f1ad066SMatthew G Knepley   Input Parameter:
2415f1ad066SMatthew G Knepley . v - The Vec
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   Output Parameter:
2445f1ad066SMatthew G Knepley . dm - The DM
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   Level: intermediate
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2495f1ad066SMatthew G Knepley @*/
2505f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2515f1ad066SMatthew G Knepley {
2525f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley   PetscFunctionBegin;
2555f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2565f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2575f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2585f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2595f1ad066SMatthew G Knepley }
2605f1ad066SMatthew G Knepley 
2615f1ad066SMatthew G Knepley /*@
262d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   Not collective
2655f1ad066SMatthew G Knepley 
2665f1ad066SMatthew G Knepley   Input Parameters:
2675f1ad066SMatthew G Knepley + v - The Vec
2685f1ad066SMatthew G Knepley - dm - The DM
2695f1ad066SMatthew G Knepley 
270d9805387SMatthew 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.
271d9805387SMatthew G. Knepley 
2725f1ad066SMatthew G Knepley   Level: intermediate
2735f1ad066SMatthew G Knepley 
2745f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2755f1ad066SMatthew G Knepley @*/
2765f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2775f1ad066SMatthew G Knepley {
2785f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2795f1ad066SMatthew G Knepley 
2805f1ad066SMatthew G Knepley   PetscFunctionBegin;
2815f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
282d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2835f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2845f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2855f1ad066SMatthew G Knepley }
2865f1ad066SMatthew G Knepley 
287521d9a4cSLisandro Dalcin /*@C
2888f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2898f1509bcSBarry Smith 
2908f1509bcSBarry Smith    Logically Collective on DM
2918f1509bcSBarry Smith 
2928f1509bcSBarry Smith    Input Parameters:
2938f1509bcSBarry Smith +  dm - the DM context
2948f1509bcSBarry Smith -  ctype - the matrix type
2958f1509bcSBarry Smith 
2968f1509bcSBarry Smith    Options Database:
2978f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2988f1509bcSBarry Smith 
2998f1509bcSBarry Smith    Level: intermediate
3008f1509bcSBarry Smith 
3018f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3028f1509bcSBarry Smith           DMGetISColoringType()
3038f1509bcSBarry Smith @*/
3048f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3058f1509bcSBarry Smith {
3068f1509bcSBarry Smith   PetscFunctionBegin;
3078f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3088f1509bcSBarry Smith   dm->coloringtype = ctype;
3098f1509bcSBarry Smith   PetscFunctionReturn(0);
3108f1509bcSBarry Smith }
3118f1509bcSBarry Smith 
3128f1509bcSBarry Smith /*@C
3138f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
314521d9a4cSLisandro Dalcin 
315521d9a4cSLisandro Dalcin    Logically Collective on DM
316521d9a4cSLisandro Dalcin 
317521d9a4cSLisandro Dalcin    Input Parameter:
3188f1509bcSBarry Smith .  dm - the DM context
3198f1509bcSBarry Smith 
3208f1509bcSBarry Smith    Output Parameter:
3218f1509bcSBarry Smith .  ctype - the matrix type
3228f1509bcSBarry Smith 
3238f1509bcSBarry Smith    Options Database:
3248f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3258f1509bcSBarry Smith 
3268f1509bcSBarry Smith    Level: intermediate
3278f1509bcSBarry Smith 
3288f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3298f1509bcSBarry Smith           DMGetISColoringType()
3308f1509bcSBarry Smith @*/
3318f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3328f1509bcSBarry Smith {
3338f1509bcSBarry Smith   PetscFunctionBegin;
3348f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3358f1509bcSBarry Smith   *ctype = dm->coloringtype;
3368f1509bcSBarry Smith   PetscFunctionReturn(0);
3378f1509bcSBarry Smith }
3388f1509bcSBarry Smith 
3398f1509bcSBarry Smith /*@C
3408f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3418f1509bcSBarry Smith 
3428f1509bcSBarry Smith    Logically Collective on DM
3438f1509bcSBarry Smith 
3448f1509bcSBarry Smith    Input Parameters:
345521d9a4cSLisandro Dalcin +  dm - the DM context
346a2b5a043SBarry Smith -  ctype - the matrix type
347521d9a4cSLisandro Dalcin 
348521d9a4cSLisandro Dalcin    Options Database:
349521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
350521d9a4cSLisandro Dalcin 
351521d9a4cSLisandro Dalcin    Level: intermediate
352521d9a4cSLisandro Dalcin 
353a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
354521d9a4cSLisandro Dalcin @*/
35519fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
356521d9a4cSLisandro Dalcin {
357521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35888f0584fSBarry Smith 
359521d9a4cSLisandro Dalcin   PetscFunctionBegin;
360521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
361521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
36219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
363521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
364521d9a4cSLisandro Dalcin }
365521d9a4cSLisandro Dalcin 
366c0dedaeaSBarry Smith /*@C
367c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
368c0dedaeaSBarry Smith 
369c0dedaeaSBarry Smith    Logically Collective on DM
370c0dedaeaSBarry Smith 
371c0dedaeaSBarry Smith    Input Parameter:
372c0dedaeaSBarry Smith .  dm - the DM context
373c0dedaeaSBarry Smith 
374c0dedaeaSBarry Smith    Output Parameter:
375c0dedaeaSBarry Smith .  ctype - the matrix type
376c0dedaeaSBarry Smith 
377c0dedaeaSBarry Smith    Options Database:
378c0dedaeaSBarry Smith .   -dm_mat_type ctype
379c0dedaeaSBarry Smith 
380c0dedaeaSBarry Smith    Level: intermediate
381c0dedaeaSBarry Smith 
382a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
383c0dedaeaSBarry Smith @*/
384c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
385c0dedaeaSBarry Smith {
386c0dedaeaSBarry Smith   PetscFunctionBegin;
387c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
388c0dedaeaSBarry Smith   *ctype = dm->mattype;
389c0dedaeaSBarry Smith   PetscFunctionReturn(0);
390c0dedaeaSBarry Smith }
391c0dedaeaSBarry Smith 
392c688c046SMatthew G Knepley /*@
39334f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
394c688c046SMatthew G Knepley 
395c688c046SMatthew G Knepley   Not collective
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   Input Parameter:
398c688c046SMatthew G Knepley . A - The Mat
399c688c046SMatthew G Knepley 
400c688c046SMatthew G Knepley   Output Parameter:
401c688c046SMatthew G Knepley . dm - The DM
402c688c046SMatthew G Knepley 
403c688c046SMatthew G Knepley   Level: intermediate
404c688c046SMatthew G Knepley 
4058f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4068f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4078f1509bcSBarry Smith 
408c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
409c688c046SMatthew G Knepley @*/
410c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
411c688c046SMatthew G Knepley {
412c688c046SMatthew G Knepley   PetscErrorCode ierr;
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley   PetscFunctionBegin;
415c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
416c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
417c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
418c688c046SMatthew G Knepley   PetscFunctionReturn(0);
419c688c046SMatthew G Knepley }
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley /*@
422c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Not collective
425c688c046SMatthew G Knepley 
426c688c046SMatthew G Knepley   Input Parameters:
427c688c046SMatthew G Knepley + A - The Mat
428c688c046SMatthew G Knepley - dm - The DM
429c688c046SMatthew G Knepley 
430c688c046SMatthew G Knepley   Level: intermediate
431c688c046SMatthew G Knepley 
4328f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4338f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4348f1509bcSBarry Smith 
4358f1509bcSBarry Smith 
436c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
437c688c046SMatthew G Knepley @*/
438c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
439c688c046SMatthew G Knepley {
440c688c046SMatthew G Knepley   PetscErrorCode ierr;
441c688c046SMatthew G Knepley 
442c688c046SMatthew G Knepley   PetscFunctionBegin;
443c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4448865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
445c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
446c688c046SMatthew G Knepley   PetscFunctionReturn(0);
447c688c046SMatthew G Knepley }
448c688c046SMatthew G Knepley 
4499a42bb27SBarry Smith /*@C
4509a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4516757b960SDave May    DM options in the database.
4529a42bb27SBarry Smith 
4538353ddbbSDave May    Logically Collective on DM
4549a42bb27SBarry Smith 
4559a42bb27SBarry Smith    Input Parameter:
4568353ddbbSDave May +  da - the DM context
4579a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4589a42bb27SBarry Smith 
4599a42bb27SBarry Smith    Notes:
4609a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4619a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4629a42bb27SBarry Smith 
4639a42bb27SBarry Smith    Level: advanced
4649a42bb27SBarry Smith 
4658353ddbbSDave May .keywords: DM, set, options, prefix, database
4669a42bb27SBarry Smith 
4679a42bb27SBarry Smith .seealso: DMSetFromOptions()
4689a42bb27SBarry Smith @*/
4697087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4709a42bb27SBarry Smith {
4719a42bb27SBarry Smith   PetscErrorCode ierr;
4729a42bb27SBarry Smith 
4739a42bb27SBarry Smith   PetscFunctionBegin;
4749a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4759a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
476691be533SLawrence Mitchell   if (dm->sf) {
477691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
478691be533SLawrence Mitchell   }
479691be533SLawrence Mitchell   if (dm->defaultSF) {
480691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
481691be533SLawrence Mitchell   }
4829a42bb27SBarry Smith   PetscFunctionReturn(0);
4839a42bb27SBarry Smith }
4849a42bb27SBarry Smith 
48531697293SDave May /*@C
48631697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48731697293SDave May    DM options in the database.
48831697293SDave May 
48931697293SDave May    Logically Collective on DM
49031697293SDave May 
49131697293SDave May    Input Parameters:
49231697293SDave May +  dm - the DM context
49331697293SDave May -  prefix - the prefix string to prepend to all DM option requests
49431697293SDave May 
49531697293SDave May    Notes:
49631697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49731697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49831697293SDave May 
49931697293SDave May    Level: advanced
50031697293SDave May 
50131697293SDave May .keywords: DM, append, options, prefix, database
50231697293SDave May 
50331697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
50431697293SDave May @*/
50531697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
50631697293SDave May {
50731697293SDave May   PetscErrorCode ierr;
50831697293SDave May 
50931697293SDave May   PetscFunctionBegin;
51031697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51131697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
51231697293SDave May   PetscFunctionReturn(0);
51331697293SDave May }
51431697293SDave May 
51531697293SDave May /*@C
51631697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51731697293SDave May    DM options in the database.
51831697293SDave May 
51931697293SDave May    Not Collective
52031697293SDave May 
52131697293SDave May    Input Parameters:
52231697293SDave May .  dm - the DM context
52331697293SDave May 
52431697293SDave May    Output Parameters:
52531697293SDave May .  prefix - pointer to the prefix string used is returned
52631697293SDave May 
52795452b02SPatrick Sanan    Notes:
52895452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52931697293SDave May    sufficient length to hold the prefix.
53031697293SDave May 
53131697293SDave May    Level: advanced
53231697293SDave May 
53331697293SDave May .keywords: DM, set, options, prefix, database
53431697293SDave May 
53531697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
53631697293SDave May @*/
53731697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
53831697293SDave May {
53931697293SDave May   PetscErrorCode ierr;
54031697293SDave May 
54131697293SDave May   PetscFunctionBegin;
54231697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54331697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
54431697293SDave May   PetscFunctionReturn(0);
54531697293SDave May }
54631697293SDave May 
54788bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
54888bdff64SToby Isaac {
54988bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
55088bdff64SToby Isaac   DMNamedVecLink nlink;
55188bdff64SToby Isaac   PetscErrorCode ierr;
55288bdff64SToby Isaac 
55388bdff64SToby Isaac   PetscFunctionBegin;
554aab5bcd8SJed Brown   *ncrefct = 0;
55588bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
55688bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
55788bdff64SToby Isaac     if (dm->localin[i])  refct--;
55888bdff64SToby Isaac     if (dm->globalin[i]) refct--;
55988bdff64SToby Isaac   }
56088bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
56188bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
56288bdff64SToby Isaac   if (dm->x) {
56388bdff64SToby Isaac     DM obj;
56488bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
56588bdff64SToby Isaac     if (obj == dm) refct--;
56688bdff64SToby Isaac   }
56788bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56888bdff64SToby Isaac     refct--;
56988bdff64SToby Isaac     if (recurseCoarse) {
57088bdff64SToby Isaac       PetscInt coarseCount;
57188bdff64SToby Isaac 
57288bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
57388bdff64SToby Isaac       refct += coarseCount;
57488bdff64SToby Isaac     }
57588bdff64SToby Isaac   }
57688bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
57788bdff64SToby Isaac     refct--;
57888bdff64SToby Isaac     if (recurseFine) {
57988bdff64SToby Isaac       PetscInt fineCount;
58088bdff64SToby Isaac 
58188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
58288bdff64SToby Isaac       refct += fineCount;
58388bdff64SToby Isaac     }
58488bdff64SToby Isaac   }
58588bdff64SToby Isaac   *ncrefct = refct;
58688bdff64SToby Isaac   PetscFunctionReturn(0);
58788bdff64SToby Isaac }
58888bdff64SToby Isaac 
589354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
590354557abSToby Isaac {
591354557abSToby Isaac   PetscErrorCode ierr;
592354557abSToby Isaac 
593354557abSToby Isaac   PetscFunctionBegin;
594354557abSToby Isaac   if (!--(dm->labels->refct)) {
595354557abSToby Isaac     DMLabelLink next = dm->labels->next;
596354557abSToby Isaac 
597354557abSToby Isaac     /* destroy the labels */
598354557abSToby Isaac     while (next) {
599354557abSToby Isaac       DMLabelLink tmp = next->next;
600354557abSToby Isaac 
601354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
602354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
603354557abSToby Isaac       next = tmp;
604354557abSToby Isaac     }
605354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
606354557abSToby Isaac   }
607354557abSToby Isaac   PetscFunctionReturn(0);
608354557abSToby Isaac }
609354557abSToby Isaac 
61047c6ae99SBarry Smith /*@
6118472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
61247c6ae99SBarry Smith 
61347c6ae99SBarry Smith     Collective on DM
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith     Input Parameter:
61647c6ae99SBarry Smith .   dm - the DM object to destroy
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith     Level: developer
61947c6ae99SBarry Smith 
620e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
62147c6ae99SBarry Smith 
62247c6ae99SBarry Smith @*/
623fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
62447c6ae99SBarry Smith {
62588bdff64SToby Isaac   PetscInt       i, cnt;
626dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
62747c6ae99SBarry Smith   PetscErrorCode ierr;
62847c6ae99SBarry Smith 
62947c6ae99SBarry Smith   PetscFunctionBegin;
6306bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6316bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
63287e657c6SBarry Smith 
63388bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
63488bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
63588bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
63688bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
637732e2eb9SMatthew G Knepley   /*
638732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
639732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
640732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
641732e2eb9SMatthew G Knepley   */
6426bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6436bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
644732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6456bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6466bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
647732e2eb9SMatthew G Knepley   }
648f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6490298fd71SBarry Smith   (*dm)->namedglobal = NULL;
650f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6512348bcf4SPeter Brune     nnext = nlink->next;
6522348bcf4SPeter 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);
6532348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6542348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6552348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6562348bcf4SPeter Brune   }
657f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6580298fd71SBarry Smith   (*dm)->namedlocal = NULL;
659f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
660f490541aSPeter Brune     nnext = nlink->next;
661f490541aSPeter 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);
662f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
663f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
664f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
665f490541aSPeter Brune   }
6662348bcf4SPeter Brune 
667b17ce1afSJed Brown   /* Destroy the list of hooks */
668c833c3b5SJed Brown   {
669c833c3b5SJed Brown     DMCoarsenHookLink link,next;
670b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
671b17ce1afSJed Brown       next = link->next;
672b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
673b17ce1afSJed Brown     }
6740298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
675c833c3b5SJed Brown   }
676c833c3b5SJed Brown   {
677c833c3b5SJed Brown     DMRefineHookLink link,next;
678c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
679c833c3b5SJed Brown       next = link->next;
680c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
681c833c3b5SJed Brown     }
6820298fd71SBarry Smith     (*dm)->refinehook = NULL;
683c833c3b5SJed Brown   }
684be081cd6SPeter Brune   {
685be081cd6SPeter Brune     DMSubDomainHookLink link,next;
686be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
687be081cd6SPeter Brune       next = link->next;
688be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
689be081cd6SPeter Brune     }
6900298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
691be081cd6SPeter Brune   }
692baf369e7SPeter Brune   {
693baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
694baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
695baf369e7SPeter Brune       next = link->next;
696baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
697baf369e7SPeter Brune     }
6980298fd71SBarry Smith     (*dm)->gtolhook = NULL;
699baf369e7SPeter Brune   }
700d4d07f1eSToby Isaac   {
701d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
702d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
703d4d07f1eSToby Isaac       next = link->next;
704d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
705d4d07f1eSToby Isaac     }
706d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
707d4d07f1eSToby Isaac   }
708aa1993deSMatthew G Knepley   /* Destroy the work arrays */
709aa1993deSMatthew G Knepley   {
710aa1993deSMatthew G Knepley     DMWorkLink link,next;
711aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
712aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
713aa1993deSMatthew G Knepley       next = link->next;
714aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
715aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
716aa1993deSMatthew G Knepley     }
7170298fd71SBarry Smith     (*dm)->workin = NULL;
718aa1993deSMatthew G Knepley   }
719c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
720c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
721c58f1c22SToby Isaac 
722c58f1c22SToby Isaac     /* destroy the labels */
723c58f1c22SToby Isaac     while (next) {
724c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
725c58f1c22SToby Isaac 
726c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
727c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
728c58f1c22SToby Isaac       next = tmp;
729c58f1c22SToby Isaac     }
730c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
731c58f1c22SToby Isaac   }
732e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
733e6f8dbb6SToby Isaac   {
734e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
735e6f8dbb6SToby Isaac     while (next) {
736e6f8dbb6SToby Isaac       DMBoundary b = next;
737e6f8dbb6SToby Isaac 
738e6f8dbb6SToby Isaac       next = b->next;
739e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
740e6f8dbb6SToby Isaac     }
741e6f8dbb6SToby Isaac   }
742b17ce1afSJed Brown 
74352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
74452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
74552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
74652536dc3SBarry Smith 
7471a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7481a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7491a266240SBarry Smith   }
75087e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
75171cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7524dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7536bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7546bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
755073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
75688ed4aceSMatthew G Knepley 
75788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
75888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
7598b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
760fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
761fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
76288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
76388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
764736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
765736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7668e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
767736995cdSBlaise Bourdin     }
768736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
769736995cdSBlaise Bourdin   }
77088bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
77188bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
77288bdff64SToby Isaac   }
773a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
77488bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
77588bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
77688bdff64SToby Isaac   }
77788bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
778f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7796636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7806636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7816636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7825dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
783*ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
784*ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
785*ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7866636e97aSMatthew G Knepley 
787e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
78814f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
789e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
790e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
791732e2eb9SMatthew G Knepley 
792ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7936bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
794ed3c66a1SDave May   }
795435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
796732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
79747c6ae99SBarry Smith   PetscFunctionReturn(0);
79847c6ae99SBarry Smith }
79947c6ae99SBarry Smith 
800d7bf68aeSBarry Smith /*@
801d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
802d7bf68aeSBarry Smith 
803d7bf68aeSBarry Smith     Collective on DM
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith     Input Parameter:
806d7bf68aeSBarry Smith .   dm - the DM object to setup
807d7bf68aeSBarry Smith 
808d7bf68aeSBarry Smith     Level: developer
809d7bf68aeSBarry Smith 
810e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
811d7bf68aeSBarry Smith 
812d7bf68aeSBarry Smith @*/
8137087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
814d7bf68aeSBarry Smith {
815d7bf68aeSBarry Smith   PetscErrorCode ierr;
816d7bf68aeSBarry Smith 
817d7bf68aeSBarry Smith   PetscFunctionBegin;
818171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8198387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
820d7bf68aeSBarry Smith   if (dm->ops->setup) {
821d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
822d7bf68aeSBarry Smith   }
8238387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
824d7bf68aeSBarry Smith   PetscFunctionReturn(0);
825d7bf68aeSBarry Smith }
826d7bf68aeSBarry Smith 
827d7bf68aeSBarry Smith /*@
828d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
829d7bf68aeSBarry Smith 
830d7bf68aeSBarry Smith     Collective on DM
831d7bf68aeSBarry Smith 
832d7bf68aeSBarry Smith     Input Parameter:
833d7bf68aeSBarry Smith .   dm - the DM object to set options for
834d7bf68aeSBarry Smith 
835732e2eb9SMatthew G Knepley     Options Database:
8364164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8374164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8384164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8398f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
840732e2eb9SMatthew G Knepley 
841d7bf68aeSBarry Smith     Level: developer
842d7bf68aeSBarry Smith 
843e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
844d7bf68aeSBarry Smith 
845d7bf68aeSBarry Smith @*/
8467087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
847d7bf68aeSBarry Smith {
8487781c08eSBarry Smith   char           typeName[256];
849ca266f36SBarry Smith   PetscBool      flg;
850d7bf68aeSBarry Smith   PetscErrorCode ierr;
851d7bf68aeSBarry Smith 
852d7bf68aeSBarry Smith   PetscFunctionBegin;
853171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85449be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
85549be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
85649be4549SMatthew G. Knepley   if (dm->defaultSF) {ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);}
8573194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8580298fd71SBarry 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);
859a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
860f9ba7244SBarry Smith   if (flg) {
861f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
862f9ba7244SBarry Smith   }
863a264d7a6SBarry 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);
864073dac72SJed Brown   if (flg) {
865521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
866073dac72SJed Brown   }
8678f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
868f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
869e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
870f9ba7244SBarry Smith   }
871f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8720633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
87382fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
874d7bf68aeSBarry Smith   PetscFunctionReturn(0);
875d7bf68aeSBarry Smith }
876d7bf68aeSBarry Smith 
877fc9bc008SSatish Balay /*@C
878224748a4SBarry Smith     DMView - Views a DM
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Collective on DM
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Input Parameter:
88347c6ae99SBarry Smith +   dm - the DM object to view
88447c6ae99SBarry Smith -   v - the viewer
88547c6ae99SBarry Smith 
886224748a4SBarry Smith     Level: beginner
88747c6ae99SBarry Smith 
888e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith @*/
8917087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
89247c6ae99SBarry Smith {
89347c6ae99SBarry Smith   PetscErrorCode    ierr;
89432c0f0efSBarry Smith   PetscBool         isbinary;
89576a8abe0SBarry Smith   PetscMPIInt       size;
89676a8abe0SBarry Smith   PetscViewerFormat format;
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith   PetscFunctionBegin;
899171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9003014e516SBarry Smith   if (!v) {
901ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9023014e516SBarry Smith   }
9038bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
90476a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
90576a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
90676a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
90798c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
90832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
90932c0f0efSBarry Smith   if (isbinary) {
91055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
91132c0f0efSBarry Smith     char     type[256];
91232c0f0efSBarry Smith 
91332c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
91432c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
91532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
91632c0f0efSBarry Smith   }
9170c010503SBarry Smith   if (dm->ops->view) {
9180c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
91947c6ae99SBarry Smith   }
92047c6ae99SBarry Smith   PetscFunctionReturn(0);
92147c6ae99SBarry Smith }
92247c6ae99SBarry Smith 
92347c6ae99SBarry Smith /*@
9248472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
92547c6ae99SBarry Smith 
92647c6ae99SBarry Smith     Collective on DM
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith     Input Parameter:
92947c6ae99SBarry Smith .   dm - the DM object
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith     Output Parameter:
93247c6ae99SBarry Smith .   vec - the global vector
93347c6ae99SBarry Smith 
934073dac72SJed Brown     Level: beginner
93547c6ae99SBarry Smith 
936e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith @*/
9397087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94047c6ae99SBarry Smith {
94147c6ae99SBarry Smith   PetscErrorCode ierr;
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith   PetscFunctionBegin;
944171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
94547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
94647c6ae99SBarry Smith   PetscFunctionReturn(0);
94747c6ae99SBarry Smith }
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith /*@
9508472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith     Not Collective
95347c6ae99SBarry Smith 
95447c6ae99SBarry Smith     Input Parameter:
95547c6ae99SBarry Smith .   dm - the DM object
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith     Output Parameter:
95847c6ae99SBarry Smith .   vec - the local vector
95947c6ae99SBarry Smith 
960073dac72SJed Brown     Level: beginner
96147c6ae99SBarry Smith 
962e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith @*/
9657087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
96647c6ae99SBarry Smith {
96747c6ae99SBarry Smith   PetscErrorCode ierr;
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith   PetscFunctionBegin;
970171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
97147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
97247c6ae99SBarry Smith   PetscFunctionReturn(0);
97347c6ae99SBarry Smith }
97447c6ae99SBarry Smith 
9751411c6eeSJed Brown /*@
9761411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9771411c6eeSJed Brown 
9781411c6eeSJed Brown    Collective on DM
9791411c6eeSJed Brown 
9801411c6eeSJed Brown    Input Parameter:
9811411c6eeSJed Brown .  dm - the DM that provides the mapping
9821411c6eeSJed Brown 
9831411c6eeSJed Brown    Output Parameter:
9841411c6eeSJed Brown .  ltog - the mapping
9851411c6eeSJed Brown 
9861411c6eeSJed Brown    Level: intermediate
9871411c6eeSJed Brown 
9881411c6eeSJed Brown    Notes:
9891411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9901411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9911411c6eeSJed Brown 
992fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9931411c6eeSJed Brown @*/
9947087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9951411c6eeSJed Brown {
9960be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9971411c6eeSJed Brown   PetscErrorCode ierr;
9981411c6eeSJed Brown 
9991411c6eeSJed Brown   PetscFunctionBegin;
10001411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10011411c6eeSJed Brown   PetscValidPointer(ltog,2);
10021411c6eeSJed Brown   if (!dm->ltogmap) {
100337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
100437d0c07bSMatthew G Knepley 
1005e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
100637d0c07bSMatthew G Knepley     if (section) {
1007a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
100837d0c07bSMatthew G Knepley       PetscInt       *ltog;
1009ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
101037d0c07bSMatthew G Knepley 
1011e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
101237d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1013ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1014ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
101537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1016a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
101737d0c07bSMatthew G Knepley 
101837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
101937d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1020a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1021a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
102237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10231a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10241a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10251a7dc684SMatthew G. Knepley         if (dof) {
1026b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1027b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10281a7dc684SMatthew G. Knepley         }
102937d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1030a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1031a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
103237d0c07bSMatthew G Knepley         }
103337d0c07bSMatthew G Knepley       }
1034bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10350be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10360be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10370be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10380be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10397591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10407591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1041ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1042ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1043ccf3bd66SMatthew G. Knepley         n /= bs;
1044ccf3bd66SMatthew G. Knepley       }
1045ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10463bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
104737d0c07bSMatthew G Knepley     } else {
1048184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1049184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10501411c6eeSJed Brown     }
105137d0c07bSMatthew G Knepley   }
10521411c6eeSJed Brown   *ltog = dm->ltogmap;
10531411c6eeSJed Brown   PetscFunctionReturn(0);
10541411c6eeSJed Brown }
10551411c6eeSJed Brown 
10561411c6eeSJed Brown /*@
10571411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10581411c6eeSJed Brown 
10591411c6eeSJed Brown    Not Collective
10601411c6eeSJed Brown 
10611411c6eeSJed Brown    Input Parameter:
10621411c6eeSJed Brown .  dm - the DM with block structure
10631411c6eeSJed Brown 
10641411c6eeSJed Brown    Output Parameter:
10651411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10661411c6eeSJed Brown 
10671411c6eeSJed Brown    Level: intermediate
10681411c6eeSJed Brown 
1069fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10701411c6eeSJed Brown @*/
10717087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10721411c6eeSJed Brown {
10731411c6eeSJed Brown   PetscFunctionBegin;
10741411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10751411c6eeSJed Brown   PetscValidPointer(bs,2);
10761411c6eeSJed 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");
10771411c6eeSJed Brown   *bs = dm->bs;
10781411c6eeSJed Brown   PetscFunctionReturn(0);
10791411c6eeSJed Brown }
10801411c6eeSJed Brown 
108147c6ae99SBarry Smith /*@
10828472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
108347c6ae99SBarry Smith 
108447c6ae99SBarry Smith     Collective on DM
108547c6ae99SBarry Smith 
108647c6ae99SBarry Smith     Input Parameter:
108747c6ae99SBarry Smith +   dm1 - the DM object
108847c6ae99SBarry Smith -   dm2 - the second, finer DM object
108947c6ae99SBarry Smith 
109047c6ae99SBarry Smith     Output Parameter:
109147c6ae99SBarry Smith +  mat - the interpolation
109247c6ae99SBarry Smith -  vec - the scaling (optional)
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith     Level: developer
109547c6ae99SBarry Smith 
109695452b02SPatrick Sanan     Notes:
109795452b02SPatrick 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
109885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1099d52bd9f3SBarry Smith 
11001f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1101d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
110285afcc9aSBarry Smith 
110385afcc9aSBarry Smith 
11043ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
110547c6ae99SBarry Smith 
110647c6ae99SBarry Smith @*/
1107e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
110847c6ae99SBarry Smith {
110947c6ae99SBarry Smith   PetscErrorCode ierr;
111047c6ae99SBarry Smith 
111147c6ae99SBarry Smith   PetscFunctionBegin;
1112171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1113171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1114c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1115c7d20fa0SStefano Zampini   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInterpolation not implemented for type %s",((PetscObject)dm1)->type_name);
1116cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111725296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1118cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111947c6ae99SBarry Smith   PetscFunctionReturn(0);
112047c6ae99SBarry Smith }
112147c6ae99SBarry Smith 
11223ad4599aSBarry Smith /*@
11233ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11243ad4599aSBarry Smith 
11253ad4599aSBarry Smith     Collective on DM
11263ad4599aSBarry Smith 
11273ad4599aSBarry Smith     Input Parameter:
11283ad4599aSBarry Smith +   dm1 - the DM object
11293ad4599aSBarry Smith -   dm2 - the second, finer DM object
11303ad4599aSBarry Smith 
11313ad4599aSBarry Smith     Output Parameter:
11323ad4599aSBarry Smith .  mat - the restriction
11333ad4599aSBarry Smith 
11343ad4599aSBarry Smith 
11353ad4599aSBarry Smith     Level: developer
11363ad4599aSBarry Smith 
113795452b02SPatrick Sanan     Notes:
113895452b02SPatrick 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
11393ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11403ad4599aSBarry Smith 
11413ad4599aSBarry Smith 
11423ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11433ad4599aSBarry Smith 
11443ad4599aSBarry Smith @*/
11453ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11463ad4599aSBarry Smith {
11473ad4599aSBarry Smith   PetscErrorCode ierr;
11483ad4599aSBarry Smith 
11493ad4599aSBarry Smith   PetscFunctionBegin;
11503ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11513ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11523ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11537294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11543ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11553ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11563ad4599aSBarry Smith   PetscFunctionReturn(0);
11573ad4599aSBarry Smith }
11583ad4599aSBarry Smith 
115947c6ae99SBarry Smith /*@
11608472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
116147c6ae99SBarry Smith 
116247c6ae99SBarry Smith     Collective on DM
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith     Input Parameter:
116547c6ae99SBarry Smith +   dm1 - the DM object
116647c6ae99SBarry Smith -   dm2 - the second, finer DM object
116747c6ae99SBarry Smith 
116847c6ae99SBarry Smith     Output Parameter:
11696dbf9973SLawrence Mitchell .   mat - the injection
117047c6ae99SBarry Smith 
117147c6ae99SBarry Smith     Level: developer
117247c6ae99SBarry Smith 
117395452b02SPatrick Sanan    Notes:
117495452b02SPatrick 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
117585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
117685afcc9aSBarry Smith 
1177e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith @*/
11806dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
118147c6ae99SBarry Smith {
118247c6ae99SBarry Smith   PetscErrorCode ierr;
118347c6ae99SBarry Smith 
118447c6ae99SBarry Smith   PetscFunctionBegin;
1185171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1186171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11870c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11886dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
118947c6ae99SBarry Smith   PetscFunctionReturn(0);
119047c6ae99SBarry Smith }
119147c6ae99SBarry Smith 
1192b412c318SBarry Smith /*@
1193bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1194bd041c0cSMatthew G. Knepley 
1195bd041c0cSMatthew G. Knepley   Collective on DM
1196bd041c0cSMatthew G. Knepley 
1197bd041c0cSMatthew G. Knepley   Input Parameter:
1198bd041c0cSMatthew G. Knepley + dm1 - the DM object
1199bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1200bd041c0cSMatthew G. Knepley 
1201bd041c0cSMatthew G. Knepley   Output Parameter:
1202bd041c0cSMatthew G. Knepley . mat - the interpolation
1203bd041c0cSMatthew G. Knepley 
1204bd041c0cSMatthew G. Knepley   Level: developer
1205bd041c0cSMatthew G. Knepley 
1206bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1207bd041c0cSMatthew G. Knepley @*/
1208bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1209bd041c0cSMatthew G. Knepley {
1210bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1211bd041c0cSMatthew G. Knepley 
1212bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1213bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1214bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1215bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1216bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1217bd041c0cSMatthew G. Knepley }
1218bd041c0cSMatthew G. Knepley 
1219bd041c0cSMatthew G. Knepley /*@
1220b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
122147c6ae99SBarry Smith 
122247c6ae99SBarry Smith     Collective on DM
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith     Input Parameter:
122547c6ae99SBarry Smith +   dm - the DM object
12265bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
122747c6ae99SBarry Smith 
122847c6ae99SBarry Smith     Output Parameter:
122947c6ae99SBarry Smith .   coloring - the coloring
123047c6ae99SBarry Smith 
123147c6ae99SBarry Smith     Level: developer
123247c6ae99SBarry Smith 
1233b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
123447c6ae99SBarry Smith 
1235aab9d709SJed Brown @*/
1236b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
123747c6ae99SBarry Smith {
123847c6ae99SBarry Smith   PetscErrorCode ierr;
123947c6ae99SBarry Smith 
124047c6ae99SBarry Smith   PetscFunctionBegin;
1241171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1242ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1243b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
124447c6ae99SBarry Smith   PetscFunctionReturn(0);
124547c6ae99SBarry Smith }
124647c6ae99SBarry Smith 
1247b412c318SBarry Smith /*@
12488472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
124947c6ae99SBarry Smith 
125047c6ae99SBarry Smith     Collective on DM
125147c6ae99SBarry Smith 
125247c6ae99SBarry Smith     Input Parameter:
1253b412c318SBarry Smith .   dm - the DM object
125447c6ae99SBarry Smith 
125547c6ae99SBarry Smith     Output Parameter:
125647c6ae99SBarry Smith .   mat - the empty Jacobian
125747c6ae99SBarry Smith 
1258073dac72SJed Brown     Level: beginner
125947c6ae99SBarry Smith 
126095452b02SPatrick Sanan     Notes:
126195452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
126294013140SBarry Smith        do not need to do it yourself.
126394013140SBarry Smith 
126494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12657889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
126694013140SBarry Smith 
126794013140SBarry 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
126894013140SBarry Smith        internally by PETSc.
126994013140SBarry Smith 
127094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1271aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
127294013140SBarry Smith 
1273b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
127447c6ae99SBarry Smith 
1275aab9d709SJed Brown @*/
1276b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
127747c6ae99SBarry Smith {
127847c6ae99SBarry Smith   PetscErrorCode ierr;
127947c6ae99SBarry Smith 
128047c6ae99SBarry Smith   PetscFunctionBegin;
1281171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1282607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1283c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1284c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1285b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1286e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1287e5e52638SMatthew G. Knepley   if (dm->Nf) {
1288e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1289e571a35bSMatthew G. Knepley     PetscInt     Nf;
1290e571a35bSMatthew G. Knepley 
1291e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1292e571a35bSMatthew G. Knepley     if (Nf == 1) {
1293e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1294e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1295e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1296e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1297e571a35bSMatthew G. Knepley       }
1298e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1299e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1300e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1301e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1302e571a35bSMatthew G. Knepley       }
1303e571a35bSMatthew G. Knepley     }
1304e571a35bSMatthew G. Knepley   }
130547c6ae99SBarry Smith   PetscFunctionReturn(0);
130647c6ae99SBarry Smith }
130747c6ae99SBarry Smith 
1308732e2eb9SMatthew G Knepley /*@
1309950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1310732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1311732e2eb9SMatthew G Knepley 
13128472ad0fSDave May   Logically Collective on DM
1313732e2eb9SMatthew G Knepley 
1314732e2eb9SMatthew G Knepley   Input Parameter:
1315732e2eb9SMatthew G Knepley + dm - the DM
1316732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1317732e2eb9SMatthew G Knepley 
1318732e2eb9SMatthew G Knepley   Level: developer
1319b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1320732e2eb9SMatthew G Knepley @*/
1321732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1322732e2eb9SMatthew G Knepley {
1323732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1324732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1325732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1326732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1327732e2eb9SMatthew G Knepley }
1328732e2eb9SMatthew G Knepley 
1329b06ff27eSHong Zhang /*@
1330b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1331b06ff27eSHong Zhang     but the array for values will not be allocated.
1332b06ff27eSHong Zhang 
1333b06ff27eSHong Zhang   Logically Collective on DM
1334b06ff27eSHong Zhang 
1335b06ff27eSHong Zhang   Input Parameter:
1336b06ff27eSHong Zhang + dm - the DM
1337b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1338b06ff27eSHong Zhang 
1339b06ff27eSHong Zhang   Level: developer
1340b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1341b06ff27eSHong Zhang @*/
1342b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1343b06ff27eSHong Zhang {
1344b06ff27eSHong Zhang   PetscFunctionBegin;
1345b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1346b06ff27eSHong Zhang   dm->structure_only = only;
1347b06ff27eSHong Zhang   PetscFunctionReturn(0);
1348b06ff27eSHong Zhang }
1349b06ff27eSHong Zhang 
1350a89ea682SMatthew G Knepley /*@C
1351aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1352a89ea682SMatthew G Knepley 
1353a89ea682SMatthew G Knepley   Not Collective
1354a89ea682SMatthew G Knepley 
1355a89ea682SMatthew G Knepley   Input Parameters:
1356a89ea682SMatthew G Knepley + dm - the DM object
1357aa1993deSMatthew G Knepley . count - The minium size
135869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1359a89ea682SMatthew G Knepley 
1360a89ea682SMatthew G Knepley   Output Parameter:
1361a89ea682SMatthew G Knepley . array - the work array
1362a89ea682SMatthew G Knepley 
1363a89ea682SMatthew G Knepley   Level: developer
1364a89ea682SMatthew G Knepley 
1365a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1366a89ea682SMatthew G Knepley @*/
136769291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1368a89ea682SMatthew G Knepley {
1369a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1370aa1993deSMatthew G Knepley   DMWorkLink     link;
137169291d52SBarry Smith   PetscMPIInt    dsize;
1372a89ea682SMatthew G Knepley 
1373a89ea682SMatthew G Knepley   PetscFunctionBegin;
1374a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1375aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1376aa1993deSMatthew G Knepley   if (dm->workin) {
1377aa1993deSMatthew G Knepley     link       = dm->workin;
1378aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1379aa1993deSMatthew G Knepley   } else {
1380b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1381a89ea682SMatthew G Knepley   }
138269291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13835056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1384aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1385854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1386854ce69bSBarry Smith     link->bytes = dsize*count;
1387aa1993deSMatthew G Knepley   }
1388aa1993deSMatthew G Knepley   link->next   = dm->workout;
1389aa1993deSMatthew G Knepley   dm->workout  = link;
1390aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1391a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1392a89ea682SMatthew G Knepley }
1393a89ea682SMatthew G Knepley 
1394aa1993deSMatthew G Knepley /*@C
1395aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1396aa1993deSMatthew G Knepley 
1397aa1993deSMatthew G Knepley   Not Collective
1398aa1993deSMatthew G Knepley 
1399aa1993deSMatthew G Knepley   Input Parameters:
1400aa1993deSMatthew G Knepley + dm - the DM object
1401aa1993deSMatthew G Knepley . count - The minium size
140269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1403aa1993deSMatthew G Knepley 
1404aa1993deSMatthew G Knepley   Output Parameter:
1405aa1993deSMatthew G Knepley . array - the work array
1406aa1993deSMatthew G Knepley 
1407aa1993deSMatthew G Knepley   Level: developer
1408aa1993deSMatthew G Knepley 
140995452b02SPatrick Sanan   Developer Notes:
141095452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1411aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1412aa1993deSMatthew G Knepley @*/
141369291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1414aa1993deSMatthew G Knepley {
1415aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1416aa1993deSMatthew G Knepley 
1417aa1993deSMatthew G Knepley   PetscFunctionBegin;
1418aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1419aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1420aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1421aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1422aa1993deSMatthew G Knepley       *p           = link->next;
1423aa1993deSMatthew G Knepley       link->next   = dm->workin;
1424aa1993deSMatthew G Knepley       dm->workin   = link;
14250298fd71SBarry Smith       *(void**)mem = NULL;
1426aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1427aa1993deSMatthew G Knepley     }
1428aa1993deSMatthew G Knepley   }
1429aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1430aa1993deSMatthew G Knepley }
1431e7c4fc90SDmitry Karpeev 
1432435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1433435a35e8SMatthew G Knepley {
1434435a35e8SMatthew G Knepley   PetscFunctionBegin;
1435435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
143682f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1437435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1438435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1439435a35e8SMatthew G Knepley }
1440435a35e8SMatthew G Knepley 
14410a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14420a50eb56SMatthew G. Knepley {
14430a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14440a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1445f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14460a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14470a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14480a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14490a50eb56SMatthew G. Knepley }
14500a50eb56SMatthew G. Knepley 
1451f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1452f9d4088aSMatthew G. Knepley {
1453f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1454f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1455f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1456f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1457f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1458f9d4088aSMatthew G. Knepley }
1459f9d4088aSMatthew G. Knepley 
1460f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1461f9d4088aSMatthew G. Knepley {
1462f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1463f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1464f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1465f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1466f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1467f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1468f9d4088aSMatthew G. Knepley }
1469f9d4088aSMatthew G. Knepley 
14704f3b5142SJed Brown /*@C
14714d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14724d343eeaSMatthew G Knepley 
14734d343eeaSMatthew G Knepley   Not collective
14744d343eeaSMatthew G Knepley 
14754d343eeaSMatthew G Knepley   Input Parameter:
14764d343eeaSMatthew G Knepley . dm - the DM object
14774d343eeaSMatthew G Knepley 
14784d343eeaSMatthew G Knepley   Output Parameters:
14790298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14800298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14810298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14824d343eeaSMatthew G Knepley 
14834d343eeaSMatthew G Knepley   Level: intermediate
14844d343eeaSMatthew G Knepley 
148521c9b008SJed Brown   Notes:
148621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
148721c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
148821c9b008SJed Brown   PetscFree().
148921c9b008SJed Brown 
14904d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14914d343eeaSMatthew G Knepley @*/
149237d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14934d343eeaSMatthew G Knepley {
149437d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14954d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14964d343eeaSMatthew G Knepley 
14974d343eeaSMatthew G Knepley   PetscFunctionBegin;
14984d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
149969ca1f37SDmitry Karpeev   if (numFields) {
150069ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
150169ca1f37SDmitry Karpeev     *numFields = 0;
150269ca1f37SDmitry Karpeev   }
150337d0c07bSMatthew G Knepley   if (fieldNames) {
150437d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15050298fd71SBarry Smith     *fieldNames = NULL;
150669ca1f37SDmitry Karpeev   }
150769ca1f37SDmitry Karpeev   if (fields) {
150869ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15090298fd71SBarry Smith     *fields = NULL;
151069ca1f37SDmitry Karpeev   }
1511e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
151237d0c07bSMatthew G Knepley   if (section) {
15133a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
151437d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
151537d0c07bSMatthew G Knepley 
1516e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
151737d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15183a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
151937d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
152037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
152137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15223a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
152337d0c07bSMatthew G Knepley     }
152437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
152537d0c07bSMatthew G Knepley       PetscInt gdof;
152637d0c07bSMatthew G Knepley 
152737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
152837d0c07bSMatthew G Knepley       if (gdof > 0) {
152937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15303a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
153137d0c07bSMatthew G Knepley 
153237d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
153337d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15343a544194SStefano Zampini           fpdof = fdof-fcdof;
15353a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15363a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15373a544194SStefano Zampini             fieldNc[f] = 1;
15383a544194SStefano Zampini           }
15393a544194SStefano Zampini           fieldSizes[f] += fpdof;
154037d0c07bSMatthew G Knepley         }
154137d0c07bSMatthew G Knepley       }
154237d0c07bSMatthew G Knepley     }
154337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1544785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
154537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
154637d0c07bSMatthew G Knepley     }
154737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
154837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
154937d0c07bSMatthew G Knepley 
155037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
155137d0c07bSMatthew G Knepley       if (gdof > 0) {
155237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
155337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
155437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
155537d0c07bSMatthew G Knepley 
155637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
155737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
155837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
155937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
156037d0c07bSMatthew G Knepley           }
156137d0c07bSMatthew G Knepley         }
156237d0c07bSMatthew G Knepley       }
156337d0c07bSMatthew G Knepley     }
15648865f1eaSKarl Rupp     if (numFields) *numFields = nF;
156537d0c07bSMatthew G Knepley     if (fieldNames) {
1566785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
156737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
156837d0c07bSMatthew G Knepley         const char *fieldName;
156937d0c07bSMatthew G Knepley 
157037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
157137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
157237d0c07bSMatthew G Knepley       }
157337d0c07bSMatthew G Knepley     }
157437d0c07bSMatthew G Knepley     if (fields) {
1575785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
157637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15773a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15783a544194SStefano Zampini 
157982f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15803a544194SStefano Zampini         in[0] = -fieldNc[f];
15813a544194SStefano Zampini         in[1] = fieldNc[f];
15823a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15833a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15843a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
158537d0c07bSMatthew G Knepley       }
158637d0c07bSMatthew G Knepley     }
15873a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15888865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15898865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
159069ca1f37SDmitry Karpeev   }
15914d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15924d343eeaSMatthew G Knepley }
15934d343eeaSMatthew G Knepley 
159416621825SDmitry Karpeev 
159516621825SDmitry Karpeev /*@C
159616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
159716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
159816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1599e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1600e7c4fc90SDmitry Karpeev 
1601e7c4fc90SDmitry Karpeev   Not collective
1602e7c4fc90SDmitry Karpeev 
1603e7c4fc90SDmitry Karpeev   Input Parameter:
1604e7c4fc90SDmitry Karpeev . dm - the DM object
1605e7c4fc90SDmitry Karpeev 
1606e7c4fc90SDmitry Karpeev   Output Parameters:
16070298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16080298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16090298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16100298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1611e7c4fc90SDmitry Karpeev 
1612e7c4fc90SDmitry Karpeev   Level: intermediate
1613e7c4fc90SDmitry Karpeev 
1614e7c4fc90SDmitry Karpeev   Notes:
1615e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1616e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1617e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1618e7c4fc90SDmitry Karpeev 
1619e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1620e7c4fc90SDmitry Karpeev @*/
162116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1622e7c4fc90SDmitry Karpeev {
1623e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1624e7c4fc90SDmitry Karpeev 
1625e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1626e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16278865f1eaSKarl Rupp   if (len) {
16288865f1eaSKarl Rupp     PetscValidPointer(len,2);
16298865f1eaSKarl Rupp     *len = 0;
16308865f1eaSKarl Rupp   }
16318865f1eaSKarl Rupp   if (namelist) {
16328865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16338865f1eaSKarl Rupp     *namelist = 0;
16348865f1eaSKarl Rupp   }
16358865f1eaSKarl Rupp   if (islist) {
16368865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16378865f1eaSKarl Rupp     *islist = 0;
16388865f1eaSKarl Rupp   }
16398865f1eaSKarl Rupp   if (dmlist) {
16408865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16418865f1eaSKarl Rupp     *dmlist = 0;
16428865f1eaSKarl Rupp   }
1643f3f0edfdSDmitry Karpeev   /*
1644f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1645f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1646f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1647f3f0edfdSDmitry Karpeev    */
1648ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
164916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1650435a35e8SMatthew G Knepley     PetscSection section;
1651435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1652435a35e8SMatthew G Knepley 
1653e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1654435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1655435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1656f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
165703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
165803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
165903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1660435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1661435a35e8SMatthew G Knepley         const char *fieldName;
1662435a35e8SMatthew G Knepley 
166303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
166403dc3394SMatthew G. Knepley         if (namelist) {
1665435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1666435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1667435a35e8SMatthew G Knepley         }
166803dc3394SMatthew G. Knepley       }
1669435a35e8SMatthew G Knepley     } else {
167069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1671e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16720298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1673e7c4fc90SDmitry Karpeev     }
16748865f1eaSKarl Rupp   } else {
167516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
167616621825SDmitry Karpeev   }
167716621825SDmitry Karpeev   PetscFunctionReturn(0);
167816621825SDmitry Karpeev }
167916621825SDmitry Karpeev 
1680564cec59SMatthew G. Knepley /*@
1681435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1682435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1683435a35e8SMatthew G Knepley 
1684435a35e8SMatthew G Knepley   Not collective
1685435a35e8SMatthew G Knepley 
1686435a35e8SMatthew G Knepley   Input Parameters:
16872adcc780SMatthew G. Knepley + dm        - The DM object
16882adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16892adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1690435a35e8SMatthew G Knepley 
1691435a35e8SMatthew G Knepley   Output Parameters:
16922adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16932adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1694435a35e8SMatthew G Knepley 
16955d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16965d3b26e6SMatthew G. Knepley 
1697435a35e8SMatthew G Knepley   Level: intermediate
1698435a35e8SMatthew G Knepley 
16995d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1700435a35e8SMatthew G Knepley @*/
170137bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1702435a35e8SMatthew G Knepley {
1703435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1704435a35e8SMatthew G Knepley 
1705435a35e8SMatthew G Knepley   PetscFunctionBegin;
1706435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1707435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17088865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17098865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1710435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1711435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
171282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1713435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1714435a35e8SMatthew G Knepley }
1715435a35e8SMatthew G Knepley 
17162adcc780SMatthew G. Knepley /*@C
17172adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17182adcc780SMatthew G. Knepley 
17192adcc780SMatthew G. Knepley   Not collective
17202adcc780SMatthew G. Knepley 
17212adcc780SMatthew G. Knepley   Input Parameter:
17222adcc780SMatthew G. Knepley + dms - The DM objects
17232adcc780SMatthew G. Knepley - len - The number of DMs
17242adcc780SMatthew G. Knepley 
17252adcc780SMatthew G. Knepley   Output Parameters:
1726a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17272adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17282adcc780SMatthew G. Knepley 
17295d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17305d3b26e6SMatthew G. Knepley 
17312adcc780SMatthew G. Knepley   Level: intermediate
17322adcc780SMatthew G. Knepley 
17335d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17342adcc780SMatthew G. Knepley @*/
17352adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17362adcc780SMatthew G. Knepley {
17372adcc780SMatthew G. Knepley   PetscInt       i;
17382adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17392adcc780SMatthew G. Knepley 
17402adcc780SMatthew G. Knepley   PetscFunctionBegin;
17412adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17422adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17432adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1744a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17452adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17462adcc780SMatthew G. Knepley   if (len) {
1747a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1748a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17492adcc780SMatthew G. Knepley   }
17502adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17512adcc780SMatthew G. Knepley }
17522adcc780SMatthew G. Knepley 
175316621825SDmitry Karpeev 
175416621825SDmitry Karpeev /*@C
17558d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17568d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17578d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17588d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17598d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
176016621825SDmitry Karpeev 
176116621825SDmitry Karpeev   Not collective
176216621825SDmitry Karpeev 
176316621825SDmitry Karpeev   Input Parameter:
176416621825SDmitry Karpeev . dm - the DM object
176516621825SDmitry Karpeev 
176616621825SDmitry Karpeev   Output Parameters:
17670298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17680298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17690298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17700298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17710298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
177216621825SDmitry Karpeev 
177316621825SDmitry Karpeev   Level: intermediate
177416621825SDmitry Karpeev 
177516621825SDmitry Karpeev   Notes:
177616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
177716621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
177816621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
177916621825SDmitry Karpeev 
17808d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
178116621825SDmitry Karpeev @*/
17828d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
178316621825SDmitry Karpeev {
178416621825SDmitry Karpeev   PetscErrorCode      ierr;
1785be081cd6SPeter Brune   DMSubDomainHookLink link;
1786be081cd6SPeter Brune   PetscInt            i,l;
178716621825SDmitry Karpeev 
178816621825SDmitry Karpeev   PetscFunctionBegin;
178916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
179014a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17910298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17920298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17930298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17940298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1795f3f0edfdSDmitry Karpeev   /*
1796f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1797f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1798f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1799f3f0edfdSDmitry Karpeev    */
1800ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
180116621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1802be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
180314a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1804f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1805be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1806be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1807be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1808be081cd6SPeter Brune         }
1809648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1810e7c4fc90SDmitry Karpeev       }
181114a18fd3SPeter Brune     }
181214a18fd3SPeter Brune     if (len) *len = l;
181314a18fd3SPeter Brune   }
1814e30e807fSPeter Brune   PetscFunctionReturn(0);
1815e30e807fSPeter Brune }
1816e30e807fSPeter Brune 
1817e30e807fSPeter Brune 
1818e30e807fSPeter Brune /*@C
1819e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1820e30e807fSPeter Brune 
1821e30e807fSPeter Brune   Not collective
1822e30e807fSPeter Brune 
1823e30e807fSPeter Brune   Input Parameters:
1824e30e807fSPeter Brune + dm - the DM object
1825e30e807fSPeter Brune . n  - the number of subdomain scatters
1826e30e807fSPeter Brune - subdms - the local subdomains
1827e30e807fSPeter Brune 
1828e30e807fSPeter Brune   Output Parameters:
1829e30e807fSPeter Brune + n     - the number of scatters returned
1830e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1831e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1832e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1833e30e807fSPeter Brune 
183495452b02SPatrick Sanan   Notes:
183595452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1836e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1837e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1838e30e807fSPeter Brune   solution and residual data.
1839e30e807fSPeter Brune 
1840e30e807fSPeter Brune   Level: developer
1841e30e807fSPeter Brune 
1842e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1843e30e807fSPeter Brune @*/
1844e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1845e30e807fSPeter Brune {
1846e30e807fSPeter Brune   PetscErrorCode ierr;
1847e30e807fSPeter Brune 
1848e30e807fSPeter Brune   PetscFunctionBegin;
1849e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1850e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1851e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1852e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18536668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1854e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1855e7c4fc90SDmitry Karpeev }
1856e7c4fc90SDmitry Karpeev 
185747c6ae99SBarry Smith /*@
185847c6ae99SBarry Smith   DMRefine - Refines a DM object
185947c6ae99SBarry Smith 
186047c6ae99SBarry Smith   Collective on DM
186147c6ae99SBarry Smith 
186247c6ae99SBarry Smith   Input Parameter:
186347c6ae99SBarry Smith + dm   - the DM object
186491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
186547c6ae99SBarry Smith 
186647c6ae99SBarry Smith   Output Parameter:
18670298fd71SBarry Smith . dmf - the refined DM, or NULL
1868ae0a1c52SMatthew G Knepley 
18690298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
187047c6ae99SBarry Smith 
187147c6ae99SBarry Smith   Level: developer
187247c6ae99SBarry Smith 
1873e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
187447c6ae99SBarry Smith @*/
18757087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
187647c6ae99SBarry Smith {
187747c6ae99SBarry Smith   PetscErrorCode   ierr;
1878c833c3b5SJed Brown   DMRefineHookLink link;
187947c6ae99SBarry Smith 
188047c6ae99SBarry Smith   PetscFunctionBegin;
1881732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18821ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1883fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
188447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18854057135bSMatthew G Knepley   if (*dmf) {
188643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18878865f1eaSKarl Rupp 
18888cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18898865f1eaSKarl Rupp 
1890644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18910598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1892656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18938865f1eaSKarl Rupp 
1894e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1895c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18968865f1eaSKarl Rupp       if (link->refinehook) {
18978865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18988865f1eaSKarl Rupp       }
1899c833c3b5SJed Brown     }
1900c833c3b5SJed Brown   }
19011ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1902c833c3b5SJed Brown   PetscFunctionReturn(0);
1903c833c3b5SJed Brown }
1904c833c3b5SJed Brown 
1905bb9467b5SJed Brown /*@C
1906c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1907c833c3b5SJed Brown 
1908c833c3b5SJed Brown    Logically Collective
1909c833c3b5SJed Brown 
1910c833c3b5SJed Brown    Input Arguments:
1911c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1912c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1913c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19140298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1915c833c3b5SJed Brown 
1916c833c3b5SJed Brown    Calling sequence of refinehook:
1917c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1918c833c3b5SJed Brown 
1919c833c3b5SJed Brown +  coarse - coarse level DM
1920c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1921c833c3b5SJed Brown -  ctx - optional user-defined function context
1922c833c3b5SJed Brown 
1923c833c3b5SJed Brown    Calling sequence for interphook:
1924c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1925c833c3b5SJed Brown 
1926c833c3b5SJed Brown +  coarse - coarse level DM
1927c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1928c833c3b5SJed Brown .  fine - fine level DM to update
1929c833c3b5SJed Brown -  ctx - optional user-defined function context
1930c833c3b5SJed Brown 
1931c833c3b5SJed Brown    Level: advanced
1932c833c3b5SJed Brown 
1933c833c3b5SJed Brown    Notes:
1934c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1935c833c3b5SJed Brown 
1936c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1937c833c3b5SJed Brown 
1938bb9467b5SJed Brown    This function is currently not available from Fortran.
1939bb9467b5SJed Brown 
1940c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1941c833c3b5SJed Brown @*/
1942c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1943c833c3b5SJed Brown {
1944c833c3b5SJed Brown   PetscErrorCode   ierr;
1945c833c3b5SJed Brown   DMRefineHookLink link,*p;
1946c833c3b5SJed Brown 
1947c833c3b5SJed Brown   PetscFunctionBegin;
1948c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19493d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19503d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19513d8e3701SJed Brown   }
195295dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1953c833c3b5SJed Brown   link->refinehook = refinehook;
1954c833c3b5SJed Brown   link->interphook = interphook;
1955c833c3b5SJed Brown   link->ctx        = ctx;
19560298fd71SBarry Smith   link->next       = NULL;
1957c833c3b5SJed Brown   *p               = link;
1958c833c3b5SJed Brown   PetscFunctionReturn(0);
1959c833c3b5SJed Brown }
1960c833c3b5SJed Brown 
19613d8e3701SJed Brown /*@C
19623d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19633d8e3701SJed Brown 
19643d8e3701SJed Brown    Logically Collective
19653d8e3701SJed Brown 
19663d8e3701SJed Brown    Input Arguments:
19673d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19683d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19693d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19703d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19713d8e3701SJed Brown 
19723d8e3701SJed Brown    Level: advanced
19733d8e3701SJed Brown 
19743d8e3701SJed Brown    Notes:
19753d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19763d8e3701SJed Brown 
19773d8e3701SJed Brown    This function is currently not available from Fortran.
19783d8e3701SJed Brown 
19793d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19803d8e3701SJed Brown @*/
19813d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19823d8e3701SJed Brown {
19833d8e3701SJed Brown   PetscErrorCode   ierr;
19843d8e3701SJed Brown   DMRefineHookLink link,*p;
19853d8e3701SJed Brown 
19863d8e3701SJed Brown   PetscFunctionBegin;
19873d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19883d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19893d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19903d8e3701SJed Brown       link = *p;
19913d8e3701SJed Brown       *p = link->next;
19923d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19933d8e3701SJed Brown       break;
19943d8e3701SJed Brown     }
19953d8e3701SJed Brown   }
19963d8e3701SJed Brown   PetscFunctionReturn(0);
19973d8e3701SJed Brown }
19983d8e3701SJed Brown 
1999c833c3b5SJed Brown /*@
2000c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2001c833c3b5SJed Brown 
2002c833c3b5SJed Brown    Collective if any hooks are
2003c833c3b5SJed Brown 
2004c833c3b5SJed Brown    Input Arguments:
2005c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2006e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2007c833c3b5SJed Brown -  fine - finer DM to update
2008c833c3b5SJed Brown 
2009c833c3b5SJed Brown    Level: developer
2010c833c3b5SJed Brown 
2011c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2012c833c3b5SJed Brown @*/
2013c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2014c833c3b5SJed Brown {
2015c833c3b5SJed Brown   PetscErrorCode   ierr;
2016c833c3b5SJed Brown   DMRefineHookLink link;
2017c833c3b5SJed Brown 
2018c833c3b5SJed Brown   PetscFunctionBegin;
2019c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20208865f1eaSKarl Rupp     if (link->interphook) {
20218865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20228865f1eaSKarl Rupp     }
20234057135bSMatthew G Knepley   }
202447c6ae99SBarry Smith   PetscFunctionReturn(0);
202547c6ae99SBarry Smith }
202647c6ae99SBarry Smith 
2027eb3f98d2SBarry Smith /*@
2028eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2029eb3f98d2SBarry Smith 
2030eb3f98d2SBarry Smith     Not Collective
2031eb3f98d2SBarry Smith 
2032eb3f98d2SBarry Smith     Input Parameter:
2033eb3f98d2SBarry Smith .   dm - the DM object
2034eb3f98d2SBarry Smith 
2035eb3f98d2SBarry Smith     Output Parameter:
2036eb3f98d2SBarry Smith .   level - number of refinements
2037eb3f98d2SBarry Smith 
2038eb3f98d2SBarry Smith     Level: developer
2039eb3f98d2SBarry Smith 
20406a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2041eb3f98d2SBarry Smith 
2042eb3f98d2SBarry Smith @*/
2043eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2044eb3f98d2SBarry Smith {
2045eb3f98d2SBarry Smith   PetscFunctionBegin;
2046eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2047eb3f98d2SBarry Smith   *level = dm->levelup;
2048eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2049eb3f98d2SBarry Smith }
2050eb3f98d2SBarry Smith 
2051fef3a512SBarry Smith /*@
2052fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2053fef3a512SBarry Smith 
2054fef3a512SBarry Smith     Not Collective
2055fef3a512SBarry Smith 
2056fef3a512SBarry Smith     Input Parameter:
2057fef3a512SBarry Smith +   dm - the DM object
2058fef3a512SBarry Smith -   level - number of refinements
2059fef3a512SBarry Smith 
2060fef3a512SBarry Smith     Level: advanced
2061fef3a512SBarry Smith 
206295452b02SPatrick Sanan     Notes:
206395452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2064fef3a512SBarry Smith 
2065fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2066fef3a512SBarry Smith 
2067fef3a512SBarry Smith @*/
2068fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2069fef3a512SBarry Smith {
2070fef3a512SBarry Smith   PetscFunctionBegin;
2071fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2072fef3a512SBarry Smith   dm->levelup = level;
2073fef3a512SBarry Smith   PetscFunctionReturn(0);
2074fef3a512SBarry Smith }
2075fef3a512SBarry Smith 
2076*ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2077*ca3d3a14SMatthew G. Knepley {
2078*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2079*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2080*ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2081*ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2082*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2083*ca3d3a14SMatthew G. Knepley }
2084*ca3d3a14SMatthew G. Knepley 
2085*ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2086*ca3d3a14SMatthew G. Knepley {
2087*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2088*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2089*ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2090*ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2091*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2092*ca3d3a14SMatthew G. Knepley }
2093*ca3d3a14SMatthew G. Knepley 
2094*ca3d3a14SMatthew G. Knepley /*@
2095*ca3d3a14SMatthew G. Knepley   DMHasBasisTransform - Employ a basis transformation from functions in global vectors to functions in local vectors
2096*ca3d3a14SMatthew G. Knepley 
2097*ca3d3a14SMatthew G. Knepley   Input Parameter:
2098*ca3d3a14SMatthew G. Knepley . dm - The DM
2099*ca3d3a14SMatthew G. Knepley 
2100*ca3d3a14SMatthew G. Knepley   Output Parameter:
2101*ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2102*ca3d3a14SMatthew G. Knepley 
2103*ca3d3a14SMatthew G. Knepley   Level: developer
2104*ca3d3a14SMatthew G. Knepley 
2105*ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2106*ca3d3a14SMatthew G. Knepley @*/
2107*ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2108*ca3d3a14SMatthew G. Knepley {
2109*ca3d3a14SMatthew G. Knepley   Vec            tv;
2110*ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2111*ca3d3a14SMatthew G. Knepley 
2112*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2113*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2114*ca3d3a14SMatthew G. Knepley   PetscValidPointer(flg, 2);
2115*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2116*ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2117*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2118*ca3d3a14SMatthew G. Knepley }
2119*ca3d3a14SMatthew G. Knepley 
2120*ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2121*ca3d3a14SMatthew G. Knepley {
2122*ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2123*ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2124*ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2125*ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2126*ca3d3a14SMatthew G. Knepley 
2127*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2128*ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
2129*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
2130*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2131*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2132*ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
2133*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(dm->transformDM, &ts);CHKERRQ(ierr);
2134*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2135*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2136*ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2137*ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2138*ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2139*ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2140*ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2141*ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2142*ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2143*ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2144*ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2145*ca3d3a14SMatthew G. Knepley     }
2146*ca3d3a14SMatthew G. Knepley   }
2147*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2148*ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2149*ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2150*ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2151*ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2152*ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2153*ca3d3a14SMatthew G. Knepley       if (dof) {
2154*ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2155*ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2156*ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2157*ca3d3a14SMatthew G. Knepley 
2158*ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2159*ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2160*ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2161*ca3d3a14SMatthew G. Knepley         ierr = PetscMemcpy(tva, A, PetscSqr(cdim) * sizeof(PetscScalar));
2162*ca3d3a14SMatthew G. Knepley       }
2163*ca3d3a14SMatthew G. Knepley     }
2164*ca3d3a14SMatthew G. Knepley   }
2165*ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2166*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2167*ca3d3a14SMatthew G. Knepley }
2168*ca3d3a14SMatthew G. Knepley 
2169*ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2170*ca3d3a14SMatthew G. Knepley {
2171*ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2172*ca3d3a14SMatthew G. Knepley 
2173*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2174*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2175*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2176*ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2177*ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2178*ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2179*ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2180*ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2181*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2182*ca3d3a14SMatthew G. Knepley }
2183*ca3d3a14SMatthew G. Knepley 
2184bb9467b5SJed Brown /*@C
2185baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2186baf369e7SPeter Brune 
2187baf369e7SPeter Brune    Logically Collective
2188baf369e7SPeter Brune 
2189baf369e7SPeter Brune    Input Arguments:
2190baf369e7SPeter Brune +  dm - the DM
2191baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2192baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
21930298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2194baf369e7SPeter Brune 
2195baf369e7SPeter Brune    Calling sequence for beginhook:
2196baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2197baf369e7SPeter Brune 
2198baf369e7SPeter Brune +  dm - global DM
2199baf369e7SPeter Brune .  g - global vector
2200baf369e7SPeter Brune .  mode - mode
2201baf369e7SPeter Brune .  l - local vector
2202baf369e7SPeter Brune -  ctx - optional user-defined function context
2203baf369e7SPeter Brune 
2204baf369e7SPeter Brune 
2205baf369e7SPeter Brune    Calling sequence for endhook:
2206ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2207baf369e7SPeter Brune 
2208baf369e7SPeter Brune +  global - global DM
2209baf369e7SPeter Brune -  ctx - optional user-defined function context
2210baf369e7SPeter Brune 
2211baf369e7SPeter Brune    Level: advanced
2212baf369e7SPeter Brune 
2213baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2214baf369e7SPeter Brune @*/
2215baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2216baf369e7SPeter Brune {
2217baf369e7SPeter Brune   PetscErrorCode          ierr;
2218baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2219baf369e7SPeter Brune 
2220baf369e7SPeter Brune   PetscFunctionBegin;
2221baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2222baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
222395dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2224baf369e7SPeter Brune   link->beginhook = beginhook;
2225baf369e7SPeter Brune   link->endhook   = endhook;
2226baf369e7SPeter Brune   link->ctx       = ctx;
22270298fd71SBarry Smith   link->next      = NULL;
2228baf369e7SPeter Brune   *p              = link;
2229baf369e7SPeter Brune   PetscFunctionReturn(0);
2230baf369e7SPeter Brune }
2231baf369e7SPeter Brune 
22324c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
22334c274da1SToby Isaac {
22344c274da1SToby Isaac   Mat cMat;
22354c274da1SToby Isaac   Vec cVec;
22364c274da1SToby Isaac   PetscSection section, cSec;
22374c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22384c274da1SToby Isaac   PetscErrorCode ierr;
22394c274da1SToby Isaac 
22404c274da1SToby Isaac   PetscFunctionBegin;
22414c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22424c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22434c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
22445db9a05bSToby Isaac     PetscInt nRows;
22455db9a05bSToby Isaac 
22465db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22475db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2248e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
22497711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22504c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
22514c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22524c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22534c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22544c274da1SToby Isaac       if (dof) {
22554c274da1SToby Isaac         PetscScalar *vals;
22564c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
22574c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
22584c274da1SToby Isaac       }
22594c274da1SToby Isaac     }
22604c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22614c274da1SToby Isaac   }
22624c274da1SToby Isaac   PetscFunctionReturn(0);
22634c274da1SToby Isaac }
22644c274da1SToby Isaac 
226547c6ae99SBarry Smith /*@
226601729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
226701729b5cSPatrick Sanan 
226801729b5cSPatrick Sanan     Neighbor-wise Collective on DM
226901729b5cSPatrick Sanan 
227001729b5cSPatrick Sanan     Input Parameters:
227101729b5cSPatrick Sanan +   dm - the DM object
227201729b5cSPatrick Sanan .   g - the global vector
227301729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
227401729b5cSPatrick Sanan -   l - the local vector
227501729b5cSPatrick Sanan 
227601729b5cSPatrick Sanan     Notes:
227701729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
227801729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
227901729b5cSPatrick Sanan 
228001729b5cSPatrick Sanan     Level: beginner
228101729b5cSPatrick Sanan 
228201729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
228301729b5cSPatrick Sanan 
228401729b5cSPatrick Sanan @*/
228501729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
228601729b5cSPatrick Sanan {
228701729b5cSPatrick Sanan   PetscErrorCode ierr;
228801729b5cSPatrick Sanan 
228901729b5cSPatrick Sanan   PetscFunctionBegin;
229001729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
229101729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
229201729b5cSPatrick Sanan   PetscFunctionReturn(0);
229301729b5cSPatrick Sanan }
229401729b5cSPatrick Sanan 
229501729b5cSPatrick Sanan /*@
229647c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
229747c6ae99SBarry Smith 
229847c6ae99SBarry Smith     Neighbor-wise Collective on DM
229947c6ae99SBarry Smith 
230047c6ae99SBarry Smith     Input Parameters:
230147c6ae99SBarry Smith +   dm - the DM object
230247c6ae99SBarry Smith .   g - the global vector
230347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
230447c6ae99SBarry Smith -   l - the local vector
230547c6ae99SBarry Smith 
230601729b5cSPatrick Sanan     Level: intermediate
230747c6ae99SBarry Smith 
230801729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
230947c6ae99SBarry Smith 
231047c6ae99SBarry Smith @*/
23117087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
231247c6ae99SBarry Smith {
23137128ae9fSMatthew G Knepley   PetscSF                 sf;
231447c6ae99SBarry Smith   PetscErrorCode          ierr;
2315baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
231647c6ae99SBarry Smith 
231747c6ae99SBarry Smith   PetscFunctionBegin;
2318171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2319baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23208865f1eaSKarl Rupp     if (link->beginhook) {
23218865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23228865f1eaSKarl Rupp     }
2323baf369e7SPeter Brune   }
23247128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
23257128ae9fSMatthew G Knepley   if (sf) {
2326ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2327ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23287128ae9fSMatthew G Knepley 
232982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23307128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2331ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23327128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23337128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2334ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
23357128ae9fSMatthew G Knepley   } else {
233633907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2337843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23387128ae9fSMatthew G Knepley   }
233947c6ae99SBarry Smith   PetscFunctionReturn(0);
234047c6ae99SBarry Smith }
234147c6ae99SBarry Smith 
234247c6ae99SBarry Smith /*@
234347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
234447c6ae99SBarry Smith 
234547c6ae99SBarry Smith     Neighbor-wise Collective on DM
234647c6ae99SBarry Smith 
234747c6ae99SBarry Smith     Input Parameters:
234847c6ae99SBarry Smith +   dm - the DM object
234947c6ae99SBarry Smith .   g - the global vector
235047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
235147c6ae99SBarry Smith -   l - the local vector
235247c6ae99SBarry Smith 
235301729b5cSPatrick Sanan     Level: intermediate
235447c6ae99SBarry Smith 
235501729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
235647c6ae99SBarry Smith 
235747c6ae99SBarry Smith @*/
23587087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
235947c6ae99SBarry Smith {
23607128ae9fSMatthew G Knepley   PetscSF                 sf;
236147c6ae99SBarry Smith   PetscErrorCode          ierr;
2362ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2363ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2364*ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2365baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
236647c6ae99SBarry Smith 
236747c6ae99SBarry Smith   PetscFunctionBegin;
2368171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23697128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2370*ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
23717128ae9fSMatthew G Knepley   if (sf) {
237282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23737128ae9fSMatthew G Knepley 
23747128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2375ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23767128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23777128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2378ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2379*ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
23807128ae9fSMatthew G Knepley   } else {
238133907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2382843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23837128ae9fSMatthew G Knepley   }
23844c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2385baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2386baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2387baf369e7SPeter Brune   }
238847c6ae99SBarry Smith   PetscFunctionReturn(0);
238947c6ae99SBarry Smith }
239047c6ae99SBarry Smith 
2391d4d07f1eSToby Isaac /*@C
2392d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2393d4d07f1eSToby Isaac 
2394d4d07f1eSToby Isaac    Logically Collective
2395d4d07f1eSToby Isaac 
2396d4d07f1eSToby Isaac    Input Arguments:
2397d4d07f1eSToby Isaac +  dm - the DM
2398d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2399d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2400d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2401d4d07f1eSToby Isaac 
2402d4d07f1eSToby Isaac    Calling sequence for beginhook:
2403d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2404d4d07f1eSToby Isaac 
2405d4d07f1eSToby Isaac +  dm - global DM
2406d4d07f1eSToby Isaac .  l - local vector
2407d4d07f1eSToby Isaac .  mode - mode
2408d4d07f1eSToby Isaac .  g - global vector
2409d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2410d4d07f1eSToby Isaac 
2411d4d07f1eSToby Isaac 
2412d4d07f1eSToby Isaac    Calling sequence for endhook:
2413d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2414d4d07f1eSToby Isaac 
2415d4d07f1eSToby Isaac +  global - global DM
2416d4d07f1eSToby Isaac .  l - local vector
2417d4d07f1eSToby Isaac .  mode - mode
2418d4d07f1eSToby Isaac .  g - global vector
2419d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2420d4d07f1eSToby Isaac 
2421d4d07f1eSToby Isaac    Level: advanced
2422d4d07f1eSToby Isaac 
2423d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2424d4d07f1eSToby Isaac @*/
2425d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2426d4d07f1eSToby Isaac {
2427d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2428d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2429d4d07f1eSToby Isaac 
2430d4d07f1eSToby Isaac   PetscFunctionBegin;
2431d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2432d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
243395dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2434d4d07f1eSToby Isaac   link->beginhook = beginhook;
2435d4d07f1eSToby Isaac   link->endhook   = endhook;
2436d4d07f1eSToby Isaac   link->ctx       = ctx;
2437d4d07f1eSToby Isaac   link->next      = NULL;
2438d4d07f1eSToby Isaac   *p              = link;
2439d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2440d4d07f1eSToby Isaac }
2441d4d07f1eSToby Isaac 
24424c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
24434c274da1SToby Isaac {
24444c274da1SToby Isaac   Mat cMat;
24454c274da1SToby Isaac   Vec cVec;
24464c274da1SToby Isaac   PetscSection section, cSec;
24474c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24484c274da1SToby Isaac   PetscErrorCode ierr;
24494c274da1SToby Isaac 
24504c274da1SToby Isaac   PetscFunctionBegin;
24514c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24524c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24534c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
24545db9a05bSToby Isaac     PetscInt nRows;
24555db9a05bSToby Isaac 
24565db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24575db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2458e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
24597711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24604c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24614c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24624c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24634c274da1SToby Isaac       if (dof) {
24644c274da1SToby Isaac         PetscInt d;
24654c274da1SToby Isaac         PetscScalar *vals;
24664c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
24674c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
24684c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
24694c274da1SToby Isaac          * we just extracted */
24704c274da1SToby Isaac         for (d = 0; d < dof; d++) {
24714c274da1SToby Isaac           vals[d] = 0.;
24724c274da1SToby Isaac         }
24734c274da1SToby Isaac       }
24744c274da1SToby Isaac     }
24754c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
24764c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24774c274da1SToby Isaac   }
24784c274da1SToby Isaac   PetscFunctionReturn(0);
24794c274da1SToby Isaac }
248001729b5cSPatrick Sanan /*@
248101729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
248201729b5cSPatrick Sanan 
248301729b5cSPatrick Sanan     Neighbor-wise Collective on DM
248401729b5cSPatrick Sanan 
248501729b5cSPatrick Sanan     Input Parameters:
248601729b5cSPatrick Sanan +   dm - the DM object
248701729b5cSPatrick Sanan .   l - the local vector
248801729b5cSPatrick 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.
248901729b5cSPatrick Sanan -   g - the global vector
249001729b5cSPatrick Sanan 
249101729b5cSPatrick Sanan     Notes:
249201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
249301729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
249401729b5cSPatrick Sanan 
249501729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
249601729b5cSPatrick 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.
249701729b5cSPatrick Sanan 
249801729b5cSPatrick Sanan     Level: beginner
249901729b5cSPatrick Sanan 
250001729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
250101729b5cSPatrick Sanan 
250201729b5cSPatrick Sanan @*/
250301729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
250401729b5cSPatrick Sanan {
250501729b5cSPatrick Sanan   PetscErrorCode ierr;
250601729b5cSPatrick Sanan 
250701729b5cSPatrick Sanan   PetscFunctionBegin;
250801729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
250901729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
251001729b5cSPatrick Sanan   PetscFunctionReturn(0);
251101729b5cSPatrick Sanan }
25124c274da1SToby Isaac 
251347c6ae99SBarry Smith /*@
251401729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25159a42bb27SBarry Smith 
25169a42bb27SBarry Smith     Neighbor-wise Collective on DM
25179a42bb27SBarry Smith 
25189a42bb27SBarry Smith     Input Parameters:
25199a42bb27SBarry Smith +   dm - the DM object
2520f6813fd5SJed Brown .   l - the local vector
25211eb28f2eSBarry 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.
25221eb28f2eSBarry Smith -   g - the global vector
25239a42bb27SBarry Smith 
252495452b02SPatrick Sanan     Notes:
252595452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
252684330215SMatthew 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.
25279a42bb27SBarry Smith 
252801729b5cSPatrick Sanan     Level: intermediate
25299a42bb27SBarry Smith 
253001729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
25319a42bb27SBarry Smith 
25329a42bb27SBarry Smith @*/
25337087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
25349a42bb27SBarry Smith {
25357128ae9fSMatthew G Knepley   PetscSF                 sf;
253684330215SMatthew G. Knepley   PetscSection            s, gs;
2537d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2538*ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2539ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2540ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2541*ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
254284330215SMatthew G. Knepley   PetscErrorCode          ierr;
25439a42bb27SBarry Smith 
25449a42bb27SBarry Smith   PetscFunctionBegin;
2545171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2546d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2547d4d07f1eSToby Isaac     if (link->beginhook) {
2548d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2549d4d07f1eSToby Isaac     }
2550d4d07f1eSToby Isaac   }
25514c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
25527128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2553e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
25547128ae9fSMatthew G Knepley   switch (mode) {
25557128ae9fSMatthew G Knepley   case INSERT_VALUES:
25567128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2557304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
255884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25597128ae9fSMatthew G Knepley   case ADD_VALUES:
25607128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2561304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
256284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25637128ae9fSMatthew G Knepley   default:
256482f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25657128ae9fSMatthew G Knepley   }
2566*ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2567*ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2568*ca3d3a14SMatthew G. Knepley     if (transform) {
2569*ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2570*ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2571*ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2572*ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2573*ca3d3a14SMatthew G. Knepley     } else {
2574ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2575*ca3d3a14SMatthew G. Knepley     }
25767128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2577*ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2578a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
257984330215SMatthew G. Knepley     } else if (s && isInsert) {
258084330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
258184330215SMatthew G. Knepley 
2582e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
258384330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
258484330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
258584330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2586b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
258784330215SMatthew G. Knepley 
258884330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
258903442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
259084330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2591b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
259284330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
259384330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2594b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
259503442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2596b3b16f48SMatthew 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);
2597b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2598b3b16f48SMatthew G. Knepley         if (!gcdof) {
259984330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2600b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2601b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
260284330215SMatthew G. Knepley           const PetscInt *cdofs;
260384330215SMatthew G. Knepley           PetscInt        cind = 0;
260484330215SMatthew G. Knepley 
260584330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2606b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
260784330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2608b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
260984330215SMatthew G. Knepley           }
2610b3b16f48SMatthew 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);
261184330215SMatthew G. Knepley       }
2612*ca3d3a14SMatthew G. Knepley     }
26137128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2614*ca3d3a14SMatthew G. Knepley     if (transform) {
2615*ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2616*ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2617*ca3d3a14SMatthew G. Knepley     } else {
2618*ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2619*ca3d3a14SMatthew G. Knepley     }
26207128ae9fSMatthew G Knepley   } else {
2621843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26227128ae9fSMatthew G Knepley   }
26239a42bb27SBarry Smith   PetscFunctionReturn(0);
26249a42bb27SBarry Smith }
26259a42bb27SBarry Smith 
26269a42bb27SBarry Smith /*@
26279a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
262847c6ae99SBarry Smith 
262947c6ae99SBarry Smith     Neighbor-wise Collective on DM
263047c6ae99SBarry Smith 
263147c6ae99SBarry Smith     Input Parameters:
263247c6ae99SBarry Smith +   dm - the DM object
2633f6813fd5SJed Brown .   l - the local vector
263447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2635f6813fd5SJed Brown -   g - the global vector
263647c6ae99SBarry Smith 
263701729b5cSPatrick Sanan     Level: intermediate
263847c6ae99SBarry Smith 
2639e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
264047c6ae99SBarry Smith 
264147c6ae99SBarry Smith @*/
26427087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
264347c6ae99SBarry Smith {
26447128ae9fSMatthew G Knepley   PetscSF                 sf;
264584330215SMatthew G. Knepley   PetscSection            s;
2646d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2647*ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
264884330215SMatthew G. Knepley   PetscErrorCode          ierr;
264947c6ae99SBarry Smith 
265047c6ae99SBarry Smith   PetscFunctionBegin;
2651171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26527128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2653e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
26547128ae9fSMatthew G Knepley   switch (mode) {
26557128ae9fSMatthew G Knepley   case INSERT_VALUES:
26567128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
265784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26587128ae9fSMatthew G Knepley   case ADD_VALUES:
26597128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
266084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26617128ae9fSMatthew G Knepley   default:
266282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26637128ae9fSMatthew G Knepley   }
266484330215SMatthew G. Knepley   if (sf && !isInsert) {
2665ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2666ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2667*ca3d3a14SMatthew G. Knepley     Vec                tmpl;
266884330215SMatthew G. Knepley 
2669*ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2670*ca3d3a14SMatthew G. Knepley     if (transform) {
2671*ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2672*ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2673*ca3d3a14SMatthew G. Knepley     } else {
2674ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2675*ca3d3a14SMatthew G. Knepley     }
26767128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2677a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2678*ca3d3a14SMatthew G. Knepley     if (transform) {
2679*ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2680*ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2681*ca3d3a14SMatthew G. Knepley     } else {
2682ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2683*ca3d3a14SMatthew G. Knepley     }
26847128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
268584330215SMatthew G. Knepley   } else if (s && isInsert) {
26867128ae9fSMatthew G Knepley   } else {
2687843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26887128ae9fSMatthew G Knepley   }
2689d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2690d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2691d4d07f1eSToby Isaac   }
269247c6ae99SBarry Smith   PetscFunctionReturn(0);
269347c6ae99SBarry Smith }
269447c6ae99SBarry Smith 
2695f089877aSRichard Tran Mills /*@
2696bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2697bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2698d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2699f089877aSRichard Tran Mills 
2700bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2701f089877aSRichard Tran Mills 
2702f089877aSRichard Tran Mills    Input Parameters:
2703f089877aSRichard Tran Mills +  dm - the DM object
2704bc0a1609SRichard Tran Mills .  g - the original local vector
2705bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2706f089877aSRichard Tran Mills 
2707bc0a1609SRichard Tran Mills    Output Parameter:
2708bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2709f089877aSRichard Tran Mills 
2710f089877aSRichard Tran Mills    Level: intermediate
2711f089877aSRichard Tran Mills 
2712bc0a1609SRichard Tran Mills    Notes:
2713bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2714bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2715bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2716bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2717bc0a1609SRichard Tran Mills 
2718bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2719bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2720f089877aSRichard Tran Mills 
2721f089877aSRichard Tran Mills @*/
2722f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2723f089877aSRichard Tran Mills {
2724f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2725f089877aSRichard Tran Mills 
2726f089877aSRichard Tran Mills   PetscFunctionBegin;
2727f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2728bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2729f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2730f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2731f089877aSRichard Tran Mills }
2732f089877aSRichard Tran Mills 
2733f089877aSRichard Tran Mills /*@
2734bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2735bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2736d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2737f089877aSRichard Tran Mills 
2738bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2739f089877aSRichard Tran Mills 
2740f089877aSRichard Tran Mills    Input Parameters:
2741bc0a1609SRichard Tran Mills +  da - the DM object
2742bc0a1609SRichard Tran Mills .  g - the original local vector
2743bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2744f089877aSRichard Tran Mills 
2745bc0a1609SRichard Tran Mills    Output Parameter:
2746bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2747f089877aSRichard Tran Mills 
2748f089877aSRichard Tran Mills    Level: intermediate
2749f089877aSRichard Tran Mills 
2750bc0a1609SRichard Tran Mills    Notes:
2751bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2752bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2753bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2754bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2755bc0a1609SRichard Tran Mills 
2756bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2757bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2758f089877aSRichard Tran Mills 
2759f089877aSRichard Tran Mills @*/
2760f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2761f089877aSRichard Tran Mills {
2762f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2763f089877aSRichard Tran Mills 
2764f089877aSRichard Tran Mills   PetscFunctionBegin;
2765f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2766bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2767f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2768f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2769f089877aSRichard Tran Mills }
2770f089877aSRichard Tran Mills 
2771f089877aSRichard Tran Mills 
277247c6ae99SBarry Smith /*@
277347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
277447c6ae99SBarry Smith 
277547c6ae99SBarry Smith     Collective on DM
277647c6ae99SBarry Smith 
277747c6ae99SBarry Smith     Input Parameter:
277847c6ae99SBarry Smith +   dm - the DM object
277991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
278047c6ae99SBarry Smith 
278147c6ae99SBarry Smith     Output Parameter:
278247c6ae99SBarry Smith .   dmc - the coarsened DM
278347c6ae99SBarry Smith 
278447c6ae99SBarry Smith     Level: developer
278547c6ae99SBarry Smith 
2786e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
278747c6ae99SBarry Smith 
278847c6ae99SBarry Smith @*/
27897087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
279047c6ae99SBarry Smith {
279147c6ae99SBarry Smith   PetscErrorCode    ierr;
2792b17ce1afSJed Brown   DMCoarsenHookLink link;
279347c6ae99SBarry Smith 
279447c6ae99SBarry Smith   PetscFunctionBegin;
2795171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2796fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
279747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
279847c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
27998892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2800a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
280143842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28028cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2803644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
28040598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2805656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2806e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2807b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2808b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2809b17ce1afSJed Brown   }
281047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2811b17ce1afSJed Brown   PetscFunctionReturn(0);
2812b17ce1afSJed Brown }
2813b17ce1afSJed Brown 
2814bb9467b5SJed Brown /*@C
2815b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2816b17ce1afSJed Brown 
2817b17ce1afSJed Brown    Logically Collective
2818b17ce1afSJed Brown 
2819b17ce1afSJed Brown    Input Arguments:
2820b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2821b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2822b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28230298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2824b17ce1afSJed Brown 
2825b17ce1afSJed Brown    Calling sequence of coarsenhook:
2826b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2827b17ce1afSJed Brown 
2828b17ce1afSJed Brown +  fine - fine level DM
2829b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2830b17ce1afSJed Brown -  ctx - optional user-defined function context
2831b17ce1afSJed Brown 
2832b17ce1afSJed Brown    Calling sequence for restricthook:
2833c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2834b17ce1afSJed Brown 
2835b17ce1afSJed Brown +  fine - fine level DM
2836b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2837c833c3b5SJed Brown .  rscale - scaling vector for restriction
2838c833c3b5SJed Brown .  inject - matrix restricting by injection
2839b17ce1afSJed Brown .  coarse - coarse level DM to update
2840b17ce1afSJed Brown -  ctx - optional user-defined function context
2841b17ce1afSJed Brown 
2842b17ce1afSJed Brown    Level: advanced
2843b17ce1afSJed Brown 
2844b17ce1afSJed Brown    Notes:
2845b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2846b17ce1afSJed Brown 
2847b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2848b17ce1afSJed Brown 
2849b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2850b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2851b17ce1afSJed Brown 
2852bb9467b5SJed Brown    This function is currently not available from Fortran.
2853bb9467b5SJed Brown 
2854dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2855b17ce1afSJed Brown @*/
2856b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2857b17ce1afSJed Brown {
2858b17ce1afSJed Brown   PetscErrorCode    ierr;
2859b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2860b17ce1afSJed Brown 
2861b17ce1afSJed Brown   PetscFunctionBegin;
2862b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
28631e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
28641e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
28651e3d8eccSJed Brown   }
286695dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2867b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2868b17ce1afSJed Brown   link->restricthook = restricthook;
2869b17ce1afSJed Brown   link->ctx          = ctx;
28700298fd71SBarry Smith   link->next         = NULL;
2871b17ce1afSJed Brown   *p                 = link;
2872b17ce1afSJed Brown   PetscFunctionReturn(0);
2873b17ce1afSJed Brown }
2874b17ce1afSJed Brown 
2875dc822a44SJed Brown /*@C
2876dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2877dc822a44SJed Brown 
2878dc822a44SJed Brown    Logically Collective
2879dc822a44SJed Brown 
2880dc822a44SJed Brown    Input Arguments:
2881dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2882dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2883dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2884dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2885dc822a44SJed Brown 
2886dc822a44SJed Brown    Level: advanced
2887dc822a44SJed Brown 
2888dc822a44SJed Brown    Notes:
2889dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2890dc822a44SJed Brown 
2891dc822a44SJed Brown    This function is currently not available from Fortran.
2892dc822a44SJed Brown 
2893dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2894dc822a44SJed Brown @*/
2895dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2896dc822a44SJed Brown {
2897dc822a44SJed Brown   PetscErrorCode    ierr;
2898dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2899dc822a44SJed Brown 
2900dc822a44SJed Brown   PetscFunctionBegin;
2901dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2902dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2903dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2904dc822a44SJed Brown       link = *p;
2905dc822a44SJed Brown       *p = link->next;
2906dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2907dc822a44SJed Brown       break;
2908dc822a44SJed Brown     }
2909dc822a44SJed Brown   }
2910dc822a44SJed Brown   PetscFunctionReturn(0);
2911dc822a44SJed Brown }
2912dc822a44SJed Brown 
2913dc822a44SJed Brown 
2914b17ce1afSJed Brown /*@
2915b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2916b17ce1afSJed Brown 
2917b17ce1afSJed Brown    Collective if any hooks are
2918b17ce1afSJed Brown 
2919b17ce1afSJed Brown    Input Arguments:
2920b17ce1afSJed Brown +  fine - finer DM to use as a base
2921b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2922e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2923b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2924e91eccc2SStefano Zampini -  coarse - coarser DM to update
2925b17ce1afSJed Brown 
2926b17ce1afSJed Brown    Level: developer
2927b17ce1afSJed Brown 
2928b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2929b17ce1afSJed Brown @*/
2930b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2931b17ce1afSJed Brown {
2932b17ce1afSJed Brown   PetscErrorCode    ierr;
2933b17ce1afSJed Brown   DMCoarsenHookLink link;
2934b17ce1afSJed Brown 
2935b17ce1afSJed Brown   PetscFunctionBegin;
2936b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
29378865f1eaSKarl Rupp     if (link->restricthook) {
29388865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
29398865f1eaSKarl Rupp     }
2940b17ce1afSJed Brown   }
294147c6ae99SBarry Smith   PetscFunctionReturn(0);
294247c6ae99SBarry Smith }
294347c6ae99SBarry Smith 
2944bb9467b5SJed Brown /*@C
2945be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
29465dbd56e3SPeter Brune 
29475dbd56e3SPeter Brune    Logically Collective
29485dbd56e3SPeter Brune 
29495dbd56e3SPeter Brune    Input Arguments:
29505dbd56e3SPeter Brune +  global - global DM
2951ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
29525dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
29530298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
29545dbd56e3SPeter Brune 
2955ec4806b8SPeter Brune 
2956ec4806b8SPeter Brune    Calling sequence for ddhook:
2957ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2958ec4806b8SPeter Brune 
2959ec4806b8SPeter Brune +  global - global DM
2960ec4806b8SPeter Brune .  block  - block DM
2961ec4806b8SPeter Brune -  ctx - optional user-defined function context
2962ec4806b8SPeter Brune 
29635dbd56e3SPeter Brune    Calling sequence for restricthook:
2964ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
29655dbd56e3SPeter Brune 
29665dbd56e3SPeter Brune +  global - global DM
29675dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
29685dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2969ec4806b8SPeter Brune .  block  - block DM
29705dbd56e3SPeter Brune -  ctx - optional user-defined function context
29715dbd56e3SPeter Brune 
29725dbd56e3SPeter Brune    Level: advanced
29735dbd56e3SPeter Brune 
29745dbd56e3SPeter Brune    Notes:
2975ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
29765dbd56e3SPeter Brune 
29775dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
29785dbd56e3SPeter Brune 
29795dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2980ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
29815dbd56e3SPeter Brune 
2982bb9467b5SJed Brown    This function is currently not available from Fortran.
2983bb9467b5SJed Brown 
29845dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
29855dbd56e3SPeter Brune @*/
2986be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
29875dbd56e3SPeter Brune {
29885dbd56e3SPeter Brune   PetscErrorCode      ierr;
2989be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
29905dbd56e3SPeter Brune 
29915dbd56e3SPeter Brune   PetscFunctionBegin;
29925dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2993b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2994b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2995b3a6b972SJed Brown   }
299695dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
29975dbd56e3SPeter Brune   link->restricthook = restricthook;
2998be081cd6SPeter Brune   link->ddhook       = ddhook;
29995dbd56e3SPeter Brune   link->ctx          = ctx;
30000298fd71SBarry Smith   link->next         = NULL;
30015dbd56e3SPeter Brune   *p                 = link;
30025dbd56e3SPeter Brune   PetscFunctionReturn(0);
30035dbd56e3SPeter Brune }
30045dbd56e3SPeter Brune 
3005b3a6b972SJed Brown /*@C
3006b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3007b3a6b972SJed Brown 
3008b3a6b972SJed Brown    Logically Collective
3009b3a6b972SJed Brown 
3010b3a6b972SJed Brown    Input Arguments:
3011b3a6b972SJed Brown +  global - global DM
3012b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3013b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3014b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3015b3a6b972SJed Brown 
3016b3a6b972SJed Brown    Level: advanced
3017b3a6b972SJed Brown 
3018b3a6b972SJed Brown    Notes:
3019b3a6b972SJed Brown 
3020b3a6b972SJed Brown    This function is currently not available from Fortran.
3021b3a6b972SJed Brown 
3022b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3023b3a6b972SJed Brown @*/
3024b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3025b3a6b972SJed Brown {
3026b3a6b972SJed Brown   PetscErrorCode      ierr;
3027b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3028b3a6b972SJed Brown 
3029b3a6b972SJed Brown   PetscFunctionBegin;
3030b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3031b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3032b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3033b3a6b972SJed Brown       link = *p;
3034b3a6b972SJed Brown       *p = link->next;
3035b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3036b3a6b972SJed Brown       break;
3037b3a6b972SJed Brown     }
3038b3a6b972SJed Brown   }
3039b3a6b972SJed Brown   PetscFunctionReturn(0);
3040b3a6b972SJed Brown }
3041b3a6b972SJed Brown 
30425dbd56e3SPeter Brune /*@
3043be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
30445dbd56e3SPeter Brune 
30455dbd56e3SPeter Brune    Collective if any hooks are
30465dbd56e3SPeter Brune 
30475dbd56e3SPeter Brune    Input Arguments:
30485dbd56e3SPeter Brune +  fine - finer DM to use as a base
3049be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3050be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
30515dbd56e3SPeter Brune -  coarse - coarer DM to update
30525dbd56e3SPeter Brune 
30535dbd56e3SPeter Brune    Level: developer
30545dbd56e3SPeter Brune 
30555dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
30565dbd56e3SPeter Brune @*/
3057be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
30585dbd56e3SPeter Brune {
30595dbd56e3SPeter Brune   PetscErrorCode      ierr;
3060be081cd6SPeter Brune   DMSubDomainHookLink link;
30615dbd56e3SPeter Brune 
30625dbd56e3SPeter Brune   PetscFunctionBegin;
3063be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
30648865f1eaSKarl Rupp     if (link->restricthook) {
30658865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
30668865f1eaSKarl Rupp     }
30675dbd56e3SPeter Brune   }
30685dbd56e3SPeter Brune   PetscFunctionReturn(0);
30695dbd56e3SPeter Brune }
30705dbd56e3SPeter Brune 
30715fe1f584SPeter Brune /*@
30726a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
30735fe1f584SPeter Brune 
30745fe1f584SPeter Brune     Not Collective
30755fe1f584SPeter Brune 
30765fe1f584SPeter Brune     Input Parameter:
30775fe1f584SPeter Brune .   dm - the DM object
30785fe1f584SPeter Brune 
30795fe1f584SPeter Brune     Output Parameter:
30806a7d9d85SPeter Brune .   level - number of coarsenings
30815fe1f584SPeter Brune 
30825fe1f584SPeter Brune     Level: developer
30835fe1f584SPeter Brune 
30846a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
30855fe1f584SPeter Brune 
30865fe1f584SPeter Brune @*/
30875fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
30885fe1f584SPeter Brune {
30895fe1f584SPeter Brune   PetscFunctionBegin;
30905fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30915fe1f584SPeter Brune   *level = dm->leveldown;
30925fe1f584SPeter Brune   PetscFunctionReturn(0);
30935fe1f584SPeter Brune }
30945fe1f584SPeter Brune 
30959a64c4a8SMatthew G. Knepley /*@
30969a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
30979a64c4a8SMatthew G. Knepley 
30989a64c4a8SMatthew G. Knepley     Not Collective
30999a64c4a8SMatthew G. Knepley 
31009a64c4a8SMatthew G. Knepley     Input Parameters:
31019a64c4a8SMatthew G. Knepley +   dm - the DM object
31029a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31039a64c4a8SMatthew G. Knepley 
31049a64c4a8SMatthew G. Knepley     Level: developer
31059a64c4a8SMatthew G. Knepley 
31069a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31079a64c4a8SMatthew G. Knepley @*/
31089a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31099a64c4a8SMatthew G. Knepley {
31109a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31119a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31129a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31139a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31149a64c4a8SMatthew G. Knepley }
31159a64c4a8SMatthew G. Knepley 
31165fe1f584SPeter Brune 
31175fe1f584SPeter Brune 
311847c6ae99SBarry Smith /*@C
311947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
312047c6ae99SBarry Smith 
312147c6ae99SBarry Smith     Collective on DM
312247c6ae99SBarry Smith 
312347c6ae99SBarry Smith     Input Parameter:
312447c6ae99SBarry Smith +   dm - the DM object
312547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
312647c6ae99SBarry Smith 
312747c6ae99SBarry Smith     Output Parameter:
312847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
312947c6ae99SBarry Smith 
313047c6ae99SBarry Smith     Level: developer
313147c6ae99SBarry Smith 
3132e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
313347c6ae99SBarry Smith 
313447c6ae99SBarry Smith @*/
31357087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
313647c6ae99SBarry Smith {
313747c6ae99SBarry Smith   PetscErrorCode ierr;
313847c6ae99SBarry Smith 
313947c6ae99SBarry Smith   PetscFunctionBegin;
3140171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3141ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
314247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
314347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
314447c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
314547c6ae99SBarry Smith   } else if (dm->ops->refine) {
314647c6ae99SBarry Smith     PetscInt i;
314747c6ae99SBarry Smith 
3148ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
314947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3150ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
315147c6ae99SBarry Smith     }
3152ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
315347c6ae99SBarry Smith   PetscFunctionReturn(0);
315447c6ae99SBarry Smith }
315547c6ae99SBarry Smith 
315647c6ae99SBarry Smith /*@C
315747c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
315847c6ae99SBarry Smith 
315947c6ae99SBarry Smith     Collective on DM
316047c6ae99SBarry Smith 
316147c6ae99SBarry Smith     Input Parameter:
316247c6ae99SBarry Smith +   dm - the DM object
316347c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
316447c6ae99SBarry Smith 
316547c6ae99SBarry Smith     Output Parameter:
316647c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
316747c6ae99SBarry Smith 
316847c6ae99SBarry Smith     Level: developer
316947c6ae99SBarry Smith 
3170e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
317147c6ae99SBarry Smith 
317247c6ae99SBarry Smith @*/
31737087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
317447c6ae99SBarry Smith {
317547c6ae99SBarry Smith   PetscErrorCode ierr;
317647c6ae99SBarry Smith 
317747c6ae99SBarry Smith   PetscFunctionBegin;
3178171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3179ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
318047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
318147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
318247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
318347c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
318447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
318547c6ae99SBarry Smith     PetscInt i;
318647c6ae99SBarry Smith 
3187ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
318847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3189ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
319047c6ae99SBarry Smith     }
3191ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
319247c6ae99SBarry Smith   PetscFunctionReturn(0);
319347c6ae99SBarry Smith }
319447c6ae99SBarry Smith 
319547c6ae99SBarry Smith /*@
3196e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
319747c6ae99SBarry Smith    grids associated with two DMs.
319847c6ae99SBarry Smith 
319947c6ae99SBarry Smith    Collective on DM
320047c6ae99SBarry Smith 
320147c6ae99SBarry Smith    Input Parameters:
320247c6ae99SBarry Smith +  dmc - the coarse grid DM
320347c6ae99SBarry Smith -  dmf - the fine grid DM
320447c6ae99SBarry Smith 
320547c6ae99SBarry Smith    Output Parameters:
320647c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
320747c6ae99SBarry Smith 
320847c6ae99SBarry Smith    Level: intermediate
320947c6ae99SBarry Smith 
321047c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
321147c6ae99SBarry Smith 
3212e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
321347c6ae99SBarry Smith @*/
3214e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
321547c6ae99SBarry Smith {
321647c6ae99SBarry Smith   PetscErrorCode ierr;
321747c6ae99SBarry Smith 
321847c6ae99SBarry Smith   PetscFunctionBegin;
3219171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3220171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
322147c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
322247c6ae99SBarry Smith   PetscFunctionReturn(0);
322347c6ae99SBarry Smith }
322447c6ae99SBarry Smith 
32251a266240SBarry Smith /*@C
32261a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32271a266240SBarry Smith 
32281a266240SBarry Smith     Not Collective
32291a266240SBarry Smith 
32301a266240SBarry Smith     Input Parameters:
32311a266240SBarry Smith +   dm - the DM object
32321a266240SBarry Smith -   destroy - the destroy function
32331a266240SBarry Smith 
32341a266240SBarry Smith     Level: intermediate
32351a266240SBarry Smith 
3236e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32371a266240SBarry Smith 
3238f07f9ceaSJed Brown @*/
32391a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32401a266240SBarry Smith {
32411a266240SBarry Smith   PetscFunctionBegin;
3242171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32431a266240SBarry Smith   dm->ctxdestroy = destroy;
32441a266240SBarry Smith   PetscFunctionReturn(0);
32451a266240SBarry Smith }
32461a266240SBarry Smith 
3247b07ff414SBarry Smith /*@
32481b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
324947c6ae99SBarry Smith 
325047c6ae99SBarry Smith     Not Collective
325147c6ae99SBarry Smith 
325247c6ae99SBarry Smith     Input Parameters:
325347c6ae99SBarry Smith +   dm - the DM object
325447c6ae99SBarry Smith -   ctx - the user context
325547c6ae99SBarry Smith 
325647c6ae99SBarry Smith     Level: intermediate
325747c6ae99SBarry Smith 
3258e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
325947c6ae99SBarry Smith 
326047c6ae99SBarry Smith @*/
32611b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
326247c6ae99SBarry Smith {
326347c6ae99SBarry Smith   PetscFunctionBegin;
3264171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
326547c6ae99SBarry Smith   dm->ctx = ctx;
326647c6ae99SBarry Smith   PetscFunctionReturn(0);
326747c6ae99SBarry Smith }
326847c6ae99SBarry Smith 
326947c6ae99SBarry Smith /*@
32701b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
327147c6ae99SBarry Smith 
327247c6ae99SBarry Smith     Not Collective
327347c6ae99SBarry Smith 
327447c6ae99SBarry Smith     Input Parameter:
327547c6ae99SBarry Smith .   dm - the DM object
327647c6ae99SBarry Smith 
327747c6ae99SBarry Smith     Output Parameter:
327847c6ae99SBarry Smith .   ctx - the user context
327947c6ae99SBarry Smith 
328047c6ae99SBarry Smith     Level: intermediate
328147c6ae99SBarry Smith 
3282e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
328347c6ae99SBarry Smith 
328447c6ae99SBarry Smith @*/
32851b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
328647c6ae99SBarry Smith {
328747c6ae99SBarry Smith   PetscFunctionBegin;
3288171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32891b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
329047c6ae99SBarry Smith   PetscFunctionReturn(0);
329147c6ae99SBarry Smith }
329247c6ae99SBarry Smith 
329308da532bSDmitry Karpeev /*@C
3294df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
329508da532bSDmitry Karpeev 
329608da532bSDmitry Karpeev     Logically Collective on DM
329708da532bSDmitry Karpeev 
329808da532bSDmitry Karpeev     Input Parameter:
329908da532bSDmitry Karpeev +   dm - the DM object
33000298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
330108da532bSDmitry Karpeev 
330208da532bSDmitry Karpeev     Level: intermediate
330308da532bSDmitry Karpeev 
3304835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
330508da532bSDmitry Karpeev          DMSetJacobian()
330608da532bSDmitry Karpeev 
330708da532bSDmitry Karpeev @*/
330808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
330908da532bSDmitry Karpeev {
331008da532bSDmitry Karpeev   PetscFunctionBegin;
331108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
331208da532bSDmitry Karpeev   PetscFunctionReturn(0);
331308da532bSDmitry Karpeev }
331408da532bSDmitry Karpeev 
331508da532bSDmitry Karpeev /*@
331608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
331708da532bSDmitry Karpeev 
331808da532bSDmitry Karpeev     Not Collective
331908da532bSDmitry Karpeev 
332008da532bSDmitry Karpeev     Input Parameter:
332108da532bSDmitry Karpeev .   dm - the DM object to destroy
332208da532bSDmitry Karpeev 
332308da532bSDmitry Karpeev     Output Parameter:
332408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
332508da532bSDmitry Karpeev 
332608da532bSDmitry Karpeev     Level: developer
332708da532bSDmitry Karpeev 
332874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
332908da532bSDmitry Karpeev 
333008da532bSDmitry Karpeev @*/
333108da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
333208da532bSDmitry Karpeev {
333308da532bSDmitry Karpeev   PetscFunctionBegin;
333408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
333508da532bSDmitry Karpeev   PetscFunctionReturn(0);
333608da532bSDmitry Karpeev }
333708da532bSDmitry Karpeev 
333808da532bSDmitry Karpeev /*@C
333908da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
334008da532bSDmitry Karpeev 
334108da532bSDmitry Karpeev     Logically Collective on DM
334208da532bSDmitry Karpeev 
334308da532bSDmitry Karpeev     Input Parameters:
3344907376e6SBarry Smith .   dm - the DM object
334508da532bSDmitry Karpeev 
334608da532bSDmitry Karpeev     Output parameters:
334708da532bSDmitry Karpeev +   xl - lower bound
334808da532bSDmitry Karpeev -   xu - upper bound
334908da532bSDmitry Karpeev 
3350907376e6SBarry Smith     Level: advanced
3351907376e6SBarry Smith 
335295452b02SPatrick Sanan     Notes:
335395452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
335408da532bSDmitry Karpeev 
335574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
335608da532bSDmitry Karpeev 
335708da532bSDmitry Karpeev @*/
335808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
335908da532bSDmitry Karpeev {
336008da532bSDmitry Karpeev   PetscErrorCode ierr;
33615fd66863SKarl Rupp 
336208da532bSDmitry Karpeev   PetscFunctionBegin;
336308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
336408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
336508da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
336608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
33678865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
336808da532bSDmitry Karpeev   PetscFunctionReturn(0);
336908da532bSDmitry Karpeev }
337008da532bSDmitry Karpeev 
3371b0ae01b7SPeter Brune /*@
3372b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3373b0ae01b7SPeter Brune 
3374b0ae01b7SPeter Brune     Not Collective
3375b0ae01b7SPeter Brune 
3376b0ae01b7SPeter Brune     Input Parameter:
3377b0ae01b7SPeter Brune .   dm - the DM object
3378b0ae01b7SPeter Brune 
3379b0ae01b7SPeter Brune     Output Parameter:
3380b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3381b0ae01b7SPeter Brune 
3382b0ae01b7SPeter Brune     Level: developer
3383b0ae01b7SPeter Brune 
3384b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3385b0ae01b7SPeter Brune 
3386b0ae01b7SPeter Brune @*/
3387b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3388b0ae01b7SPeter Brune {
3389b0ae01b7SPeter Brune   PetscFunctionBegin;
3390b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3391b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3392b0ae01b7SPeter Brune }
3393b0ae01b7SPeter Brune 
33943ad4599aSBarry Smith /*@
33953ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
33963ad4599aSBarry Smith 
33973ad4599aSBarry Smith     Not Collective
33983ad4599aSBarry Smith 
33993ad4599aSBarry Smith     Input Parameter:
34003ad4599aSBarry Smith .   dm - the DM object
34013ad4599aSBarry Smith 
34023ad4599aSBarry Smith     Output Parameter:
34033ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34043ad4599aSBarry Smith 
34053ad4599aSBarry Smith     Level: developer
34063ad4599aSBarry Smith 
34073ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
34083ad4599aSBarry Smith 
34093ad4599aSBarry Smith @*/
34103ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
34113ad4599aSBarry Smith {
34123ad4599aSBarry Smith   PetscFunctionBegin;
34133ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34143ad4599aSBarry Smith   PetscFunctionReturn(0);
34153ad4599aSBarry Smith }
34163ad4599aSBarry Smith 
3417a7058e45SLawrence Mitchell 
3418a7058e45SLawrence Mitchell /*@
3419a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3420a7058e45SLawrence Mitchell 
3421a7058e45SLawrence Mitchell     Not Collective
3422a7058e45SLawrence Mitchell 
3423a7058e45SLawrence Mitchell     Input Parameter:
3424a7058e45SLawrence Mitchell .   dm - the DM object
3425a7058e45SLawrence Mitchell 
3426a7058e45SLawrence Mitchell     Output Parameter:
3427a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3428a7058e45SLawrence Mitchell 
3429a7058e45SLawrence Mitchell     Level: developer
3430a7058e45SLawrence Mitchell 
3431a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3432a7058e45SLawrence Mitchell 
3433a7058e45SLawrence Mitchell @*/
3434a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3435a7058e45SLawrence Mitchell {
34364a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3437a7058e45SLawrence Mitchell   PetscFunctionBegin;
34384a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
34394a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3440a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3441a7058e45SLawrence Mitchell }
3442a7058e45SLawrence Mitchell 
3443748fac09SDmitry Karpeev /*@C
344408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
344508da532bSDmitry Karpeev 
344608da532bSDmitry Karpeev     Collective on DM
344708da532bSDmitry Karpeev 
344808da532bSDmitry Karpeev     Input Parameter:
344908da532bSDmitry Karpeev +   dm - the DM object
34500298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
345108da532bSDmitry Karpeev 
345208da532bSDmitry Karpeev     Level: developer
345308da532bSDmitry Karpeev 
345474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
345508da532bSDmitry Karpeev 
345608da532bSDmitry Karpeev @*/
345708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
345808da532bSDmitry Karpeev {
345908da532bSDmitry Karpeev   PetscErrorCode ierr;
34605fd66863SKarl Rupp 
346108da532bSDmitry Karpeev   PetscFunctionBegin;
346208da532bSDmitry Karpeev   if (x) {
346308da532bSDmitry Karpeev     if (!dm->x) {
346408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
346508da532bSDmitry Karpeev     }
346608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
34678865f1eaSKarl Rupp   } else if (dm->x) {
346808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
346908da532bSDmitry Karpeev   }
347008da532bSDmitry Karpeev   PetscFunctionReturn(0);
347108da532bSDmitry Karpeev }
347208da532bSDmitry Karpeev 
34730298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3474264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3475264ace61SBarry Smith 
3476264ace61SBarry Smith /*@C
3477264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3478264ace61SBarry Smith 
3479264ace61SBarry Smith   Collective on DM
3480264ace61SBarry Smith 
3481264ace61SBarry Smith   Input Parameters:
3482264ace61SBarry Smith + dm     - The DM object
3483264ace61SBarry Smith - method - The name of the DM type
3484264ace61SBarry Smith 
3485264ace61SBarry Smith   Options Database Key:
3486264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3487264ace61SBarry Smith 
3488264ace61SBarry Smith   Notes:
3489e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3490264ace61SBarry Smith 
3491264ace61SBarry Smith   Level: intermediate
3492264ace61SBarry Smith 
3493264ace61SBarry Smith .keywords: DM, set, type
3494264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3495264ace61SBarry Smith @*/
349619fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3497264ace61SBarry Smith {
3498264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3499264ace61SBarry Smith   PetscBool      match;
3500264ace61SBarry Smith   PetscErrorCode ierr;
3501264ace61SBarry Smith 
3502264ace61SBarry Smith   PetscFunctionBegin;
3503264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3504251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3505264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3506264ace61SBarry Smith 
35070f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35081c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3509ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3510264ace61SBarry Smith 
3511264ace61SBarry Smith   if (dm->ops->destroy) {
3512264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
35130298fd71SBarry Smith     dm->ops->destroy = NULL;
3514264ace61SBarry Smith   }
3515264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3516264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3517264ace61SBarry Smith   PetscFunctionReturn(0);
3518264ace61SBarry Smith }
3519264ace61SBarry Smith 
3520264ace61SBarry Smith /*@C
3521264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3522264ace61SBarry Smith 
3523264ace61SBarry Smith   Not Collective
3524264ace61SBarry Smith 
3525264ace61SBarry Smith   Input Parameter:
3526264ace61SBarry Smith . dm  - The DM
3527264ace61SBarry Smith 
3528264ace61SBarry Smith   Output Parameter:
3529264ace61SBarry Smith . type - The DM type name
3530264ace61SBarry Smith 
3531264ace61SBarry Smith   Level: intermediate
3532264ace61SBarry Smith 
3533264ace61SBarry Smith .keywords: DM, get, type, name
3534264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3535264ace61SBarry Smith @*/
353619fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3537264ace61SBarry Smith {
3538264ace61SBarry Smith   PetscErrorCode ierr;
3539264ace61SBarry Smith 
3540264ace61SBarry Smith   PetscFunctionBegin;
3541264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3542c959eef4SJed Brown   PetscValidPointer(type,2);
3543607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3544264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3545264ace61SBarry Smith   PetscFunctionReturn(0);
3546264ace61SBarry Smith }
3547264ace61SBarry Smith 
354867a56275SMatthew G Knepley /*@C
354967a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
355067a56275SMatthew G Knepley 
355167a56275SMatthew G Knepley   Collective on DM
355267a56275SMatthew G Knepley 
355367a56275SMatthew G Knepley   Input Parameters:
355467a56275SMatthew G Knepley + dm - the DM
355567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
355667a56275SMatthew G Knepley 
355767a56275SMatthew G Knepley   Output Parameter:
355867a56275SMatthew G Knepley . M - pointer to new DM
355967a56275SMatthew G Knepley 
356067a56275SMatthew G Knepley   Notes:
356167a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
356267a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
356367a56275SMatthew G Knepley   of the input DM.
356467a56275SMatthew G Knepley 
356567a56275SMatthew G Knepley   Level: intermediate
356667a56275SMatthew G Knepley 
356767a56275SMatthew G Knepley .seealso: DMCreate()
356867a56275SMatthew G Knepley @*/
356919fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
357067a56275SMatthew G Knepley {
357167a56275SMatthew G Knepley   DM             B;
357267a56275SMatthew G Knepley   char           convname[256];
3573c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
357467a56275SMatthew G Knepley   PetscErrorCode ierr;
357567a56275SMatthew G Knepley 
357667a56275SMatthew G Knepley   PetscFunctionBegin;
357767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
357867a56275SMatthew G Knepley   PetscValidType(dm,1);
357967a56275SMatthew G Knepley   PetscValidPointer(M,3);
3580251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3581c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3582c067b6caSMatthew G. Knepley   if (sametype) {
3583c067b6caSMatthew G. Knepley     *M   = dm;
3584c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3585c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3586c067b6caSMatthew G. Knepley   } else {
35870298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
358867a56275SMatthew G Knepley 
358967a56275SMatthew G Knepley     /*
359067a56275SMatthew G Knepley        Order of precedence:
359167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
359267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
359367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
359467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
359567a56275SMatthew G Knepley        5) Use a really basic converter.
359667a56275SMatthew G Knepley     */
359767a56275SMatthew G Knepley 
359867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3599a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3600a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3601a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3602a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3603a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36040005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
360567a56275SMatthew G Knepley     if (conv) goto foundconv;
360667a56275SMatthew G Knepley 
360767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
360882f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
360967a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3610a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3611a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3612a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3613a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3614a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36150005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
361667a56275SMatthew G Knepley     if (conv) {
3617fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
361867a56275SMatthew G Knepley       goto foundconv;
361967a56275SMatthew G Knepley     }
362067a56275SMatthew G Knepley 
362167a56275SMatthew G Knepley #if 0
362267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
362367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3624fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
362567a56275SMatthew G Knepley     if (conv) goto foundconv;
362667a56275SMatthew G Knepley 
362767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
362867a56275SMatthew G Knepley     if (dm->ops->convert) {
362967a56275SMatthew G Knepley       conv = dm->ops->convert;
363067a56275SMatthew G Knepley     }
363167a56275SMatthew G Knepley     if (conv) goto foundconv;
363267a56275SMatthew G Knepley #endif
363367a56275SMatthew G Knepley 
363467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
363582f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
363667a56275SMatthew G Knepley 
363767a56275SMatthew G Knepley foundconv:
363867a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
363967a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
364012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
364190b157c4SStefano Zampini     {
364290b157c4SStefano Zampini       PetscBool             isper;
364312fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
364412fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
364590b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
364690b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
364712fa691eSMatthew G. Knepley     }
364867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
364967a56275SMatthew G Knepley   }
365067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
365167a56275SMatthew G Knepley   PetscFunctionReturn(0);
365267a56275SMatthew G Knepley }
3653264ace61SBarry Smith 
3654264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3655264ace61SBarry Smith 
3656264ace61SBarry Smith /*@C
36571c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
36581c84c290SBarry Smith 
36591c84c290SBarry Smith   Not Collective
36601c84c290SBarry Smith 
36611c84c290SBarry Smith   Input Parameters:
36621c84c290SBarry Smith + name        - The name of a new user-defined creation routine
36631c84c290SBarry Smith - create_func - The creation routine itself
36641c84c290SBarry Smith 
36651c84c290SBarry Smith   Notes:
36661c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
36671c84c290SBarry Smith 
36681c84c290SBarry Smith 
36691c84c290SBarry Smith   Sample usage:
36701c84c290SBarry Smith .vb
3671bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
36721c84c290SBarry Smith .ve
36731c84c290SBarry Smith 
36741c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
36751c84c290SBarry Smith .vb
36761c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
36771c84c290SBarry Smith     DMSetType(DM,"my_da");
36781c84c290SBarry Smith .ve
36791c84c290SBarry Smith    or at runtime via the option
36801c84c290SBarry Smith .vb
36811c84c290SBarry Smith     -da_type my_da
36821c84c290SBarry Smith .ve
3683264ace61SBarry Smith 
3684264ace61SBarry Smith   Level: advanced
36851c84c290SBarry Smith 
36861c84c290SBarry Smith .keywords: DM, register
3687bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
36881c84c290SBarry Smith 
3689264ace61SBarry Smith @*/
3690bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3691264ace61SBarry Smith {
3692264ace61SBarry Smith   PetscErrorCode ierr;
3693264ace61SBarry Smith 
3694264ace61SBarry Smith   PetscFunctionBegin;
36951d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3696a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3697264ace61SBarry Smith   PetscFunctionReturn(0);
3698264ace61SBarry Smith }
3699264ace61SBarry Smith 
3700b859378eSBarry Smith /*@C
370155849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3702b859378eSBarry Smith 
3703b859378eSBarry Smith   Collective on PetscViewer
3704b859378eSBarry Smith 
3705b859378eSBarry Smith   Input Parameters:
3706b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3707b859378eSBarry Smith            some related function before a call to DMLoad().
3708b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3709b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3710b859378eSBarry Smith 
3711b859378eSBarry Smith    Level: intermediate
3712b859378eSBarry Smith 
3713b859378eSBarry Smith   Notes:
371455849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3715b859378eSBarry Smith 
3716b859378eSBarry Smith   Notes for advanced users:
3717b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3718b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3719b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3720b859378eSBarry Smith   format is
3721b859378eSBarry Smith .vb
3722b859378eSBarry Smith      has not yet been determined
3723b859378eSBarry Smith .ve
3724b859378eSBarry Smith 
3725b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3726b859378eSBarry Smith @*/
3727b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3728b859378eSBarry Smith {
37299331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3730b859378eSBarry Smith   PetscErrorCode ierr;
3731b859378eSBarry Smith 
3732b859378eSBarry Smith   PetscFunctionBegin;
3733b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3734b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3735fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
373632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37379331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
37389331c7a4SMatthew G. Knepley   if (isbinary) {
37399331c7a4SMatthew G. Knepley     PetscInt classid;
37409331c7a4SMatthew G. Knepley     char     type[256];
3741b859378eSBarry Smith 
3742060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37439200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3744060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
374532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37469331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37479331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37489331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37499331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3750b859378eSBarry Smith   PetscFunctionReturn(0);
3751b859378eSBarry Smith }
3752b859378eSBarry Smith 
37537da65231SMatthew G Knepley /******************************** FEM Support **********************************/
37547da65231SMatthew G Knepley 
3755a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3756a6dfd86eSKarl Rupp {
37571d47ebbbSSatish Balay   PetscInt       f;
37581b30c384SMatthew G Knepley   PetscErrorCode ierr;
37591b30c384SMatthew 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 < len; ++f) {
376357622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
37647da65231SMatthew G Knepley   }
37657da65231SMatthew G Knepley   PetscFunctionReturn(0);
37667da65231SMatthew G Knepley }
37677da65231SMatthew G Knepley 
3768a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3769a6dfd86eSKarl Rupp {
37701b30c384SMatthew G Knepley   PetscInt       f, g;
37717da65231SMatthew G Knepley   PetscErrorCode ierr;
37727da65231SMatthew G Knepley 
37737da65231SMatthew G Knepley   PetscFunctionBegin;
377474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
37751d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
377674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
37771d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3778e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
37797da65231SMatthew G Knepley     }
378074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
37817da65231SMatthew G Knepley   }
37827da65231SMatthew G Knepley   PetscFunctionReturn(0);
37837da65231SMatthew G Knepley }
3784e7c4fc90SDmitry Karpeev 
37856113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3786e759306cSMatthew G. Knepley {
37870c5b8624SToby Isaac   PetscInt          localSize, bs;
37880c5b8624SToby Isaac   PetscMPIInt       size;
37890c5b8624SToby Isaac   Vec               x, xglob;
37900c5b8624SToby Isaac   const PetscScalar *xarray;
3791e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3792e759306cSMatthew G. Knepley 
3793e759306cSMatthew G. Knepley   PetscFunctionBegin;
37949852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3795e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3796e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
37976113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
37980c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
37990c5b8624SToby Isaac   if (size > 1) {
38000c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
38010c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
38020c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
38030c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
38040c5b8624SToby Isaac   } else {
38050c5b8624SToby Isaac     xglob = x;
38060c5b8624SToby Isaac   }
38070c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
38080c5b8624SToby Isaac   if (size > 1) {
38090c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
38100c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
38110c5b8624SToby Isaac   }
3812e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3813e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3814e759306cSMatthew G. Knepley }
3815e759306cSMatthew G. Knepley 
381688ed4aceSMatthew G Knepley /*@
3817e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
381888ed4aceSMatthew G Knepley 
381988ed4aceSMatthew G Knepley   Input Parameter:
382088ed4aceSMatthew G Knepley . dm - The DM
382188ed4aceSMatthew G Knepley 
382288ed4aceSMatthew G Knepley   Output Parameter:
382388ed4aceSMatthew G Knepley . section - The PetscSection
382488ed4aceSMatthew G Knepley 
3825e5893cccSMatthew G. Knepley   Options Database Keys:
3826e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3827e5893cccSMatthew G. Knepley 
382888ed4aceSMatthew G Knepley   Level: intermediate
382988ed4aceSMatthew G Knepley 
383088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
383188ed4aceSMatthew G Knepley 
3832e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
383388ed4aceSMatthew G Knepley @*/
3834e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
38350adebc6cSBarry Smith {
3836fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3837fd59a867SMatthew G. Knepley 
383888ed4aceSMatthew G Knepley   PetscFunctionBegin;
383988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
384088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
38412f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
3842e5e52638SMatthew G. Knepley     PetscInt d;
3843e5e52638SMatthew G. Knepley 
3844e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
38452f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3846ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
38472f0f8703SMatthew G. Knepley   }
384888ed4aceSMatthew G Knepley   *section = dm->defaultSection;
384988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
385088ed4aceSMatthew G Knepley }
385188ed4aceSMatthew G Knepley 
385288ed4aceSMatthew G Knepley /*@
3853e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
385488ed4aceSMatthew G Knepley 
385588ed4aceSMatthew G Knepley   Input Parameters:
385688ed4aceSMatthew G Knepley + dm - The DM
385788ed4aceSMatthew G Knepley - section - The PetscSection
385888ed4aceSMatthew G Knepley 
385988ed4aceSMatthew G Knepley   Level: intermediate
386088ed4aceSMatthew G Knepley 
386188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
386288ed4aceSMatthew G Knepley 
3863e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
386488ed4aceSMatthew G Knepley @*/
3865e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
38660adebc6cSBarry Smith {
3867c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3868af122d2aSMatthew G Knepley   PetscInt       f;
386988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
387088ed4aceSMatthew G Knepley 
387188ed4aceSMatthew G Knepley   PetscFunctionBegin;
387288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3873c473ab19SMatthew G. Knepley   if (section) {
38741d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
38751d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3876c473ab19SMatthew G. Knepley   }
387788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
387888ed4aceSMatthew G Knepley   dm->defaultSection = section;
3879c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3880af122d2aSMatthew G Knepley   if (numFields) {
3881af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3882af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
38830f21e855SMatthew G. Knepley       PetscObject disc;
3884af122d2aSMatthew G Knepley       const char *name;
3885af122d2aSMatthew G Knepley 
3886af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
388744a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
38880f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3889af122d2aSMatthew G Knepley     }
3890af122d2aSMatthew G Knepley   }
3891e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
38921d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
389388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
389488ed4aceSMatthew G Knepley }
389588ed4aceSMatthew G Knepley 
38969435951eSToby Isaac /*@
38979435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
38989435951eSToby Isaac 
3899e228b242SToby Isaac   not collective
3900e228b242SToby Isaac 
39019435951eSToby Isaac   Input Parameter:
39029435951eSToby Isaac . dm - The DM
39039435951eSToby Isaac 
39049435951eSToby Isaac   Output Parameter:
39059435951eSToby 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.
39069435951eSToby 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.
39079435951eSToby Isaac 
39089435951eSToby Isaac   Level: advanced
39099435951eSToby Isaac 
39109435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
39119435951eSToby Isaac 
39129435951eSToby Isaac .seealso: DMSetDefaultConstraints()
39139435951eSToby Isaac @*/
39149435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
39159435951eSToby Isaac {
39169435951eSToby Isaac   PetscErrorCode ierr;
39179435951eSToby Isaac 
39189435951eSToby Isaac   PetscFunctionBegin;
39199435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39209435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
392145a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
392245a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
39239435951eSToby Isaac   PetscFunctionReturn(0);
39249435951eSToby Isaac }
39259435951eSToby Isaac 
39269435951eSToby Isaac /*@
39279435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
39289435951eSToby Isaac 
39299435951eSToby 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().
39309435951eSToby Isaac 
39319435951eSToby 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.
39329435951eSToby Isaac 
3933e228b242SToby Isaac   collective on dm
3934e228b242SToby Isaac 
39359435951eSToby Isaac   Input Parameters:
39369435951eSToby Isaac + dm - The DM
3937e228b242SToby 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).
3938e228b242SToby 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).
39399435951eSToby Isaac 
39409435951eSToby Isaac   Level: advanced
39419435951eSToby Isaac 
39429435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
39439435951eSToby Isaac 
39449435951eSToby Isaac .seealso: DMGetDefaultConstraints()
39459435951eSToby Isaac @*/
39469435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
39479435951eSToby Isaac {
3948e228b242SToby Isaac   PetscMPIInt result;
39499435951eSToby Isaac   PetscErrorCode ierr;
39509435951eSToby Isaac 
39519435951eSToby Isaac   PetscFunctionBegin;
39529435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3953e228b242SToby Isaac   if (section) {
3954e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3955e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3956f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3957e228b242SToby Isaac   }
3958e228b242SToby Isaac   if (mat) {
3959e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3960e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3961f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3962e228b242SToby Isaac   }
39639435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
39649435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
39659435951eSToby Isaac   dm->defaultConstraintSection = section;
39669435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
39679435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
39689435951eSToby Isaac   dm->defaultConstraintMat = mat;
39699435951eSToby Isaac   PetscFunctionReturn(0);
39709435951eSToby Isaac }
39719435951eSToby Isaac 
3972497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3973507e4973SMatthew G. Knepley /*
3974507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3975507e4973SMatthew G. Knepley 
3976507e4973SMatthew G. Knepley   Input Parameters:
3977507e4973SMatthew G. Knepley + dm - The DM
3978507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3979507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3980507e4973SMatthew G. Knepley 
3981507e4973SMatthew G. Knepley   Level: intermediate
3982507e4973SMatthew G. Knepley 
3983507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3984507e4973SMatthew G. Knepley */
3985f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3986507e4973SMatthew G. Knepley {
3987507e4973SMatthew G. Knepley   MPI_Comm        comm;
3988507e4973SMatthew G. Knepley   PetscLayout     layout;
3989507e4973SMatthew G. Knepley   const PetscInt *ranges;
3990507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3991507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3992507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3993507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3994507e4973SMatthew G. Knepley 
3995507e4973SMatthew G. Knepley   PetscFunctionBegin;
3996507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3997507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3998507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3999507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4000507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4001507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4002507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4003507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4004507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4005507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4006507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4007507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4008f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4009507e4973SMatthew G. Knepley 
4010507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4011507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4012507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4013507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4014507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4015507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4016507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4017507e4973SMatthew 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;}
4018507e4973SMatthew 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;}
4019507e4973SMatthew G. Knepley     if (gdof < 0) {
4020507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4021507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4022507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4023507e4973SMatthew G. Knepley 
4024507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4025507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4026507e4973SMatthew 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;}
4027507e4973SMatthew G. Knepley       }
4028507e4973SMatthew G. Knepley     }
4029507e4973SMatthew G. Knepley   }
4030507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4031507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4032b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4033507e4973SMatthew G. Knepley   if (!gvalid) {
4034507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4035507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4036507e4973SMatthew G. Knepley   }
4037507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4038507e4973SMatthew G. Knepley }
4039f741bcd2SMatthew G. Knepley #endif
4040507e4973SMatthew G. Knepley 
404188ed4aceSMatthew G Knepley /*@
4042e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
404388ed4aceSMatthew G Knepley 
40448b1ab98fSJed Brown   Collective on DM
40458b1ab98fSJed Brown 
404688ed4aceSMatthew G Knepley   Input Parameter:
404788ed4aceSMatthew G Knepley . dm - The DM
404888ed4aceSMatthew G Knepley 
404988ed4aceSMatthew G Knepley   Output Parameter:
405088ed4aceSMatthew G Knepley . section - The PetscSection
405188ed4aceSMatthew G Knepley 
405288ed4aceSMatthew G Knepley   Level: intermediate
405388ed4aceSMatthew G Knepley 
405488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
405588ed4aceSMatthew G Knepley 
4056e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
405788ed4aceSMatthew G Knepley @*/
4058e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
40590adebc6cSBarry Smith {
406088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
406188ed4aceSMatthew G Knepley 
406288ed4aceSMatthew G Knepley   PetscFunctionBegin;
406388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
406488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
406588ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
4066fd59a867SMatthew G. Knepley     PetscSection s;
4067fd59a867SMatthew G. Knepley 
4068e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
4069fd59a867SMatthew 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");
407033907cc2SStefano 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");
407115b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
4072cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
4073ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
4074685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
407588ed4aceSMatthew G Knepley   }
407688ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
407788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
407888ed4aceSMatthew G Knepley }
407988ed4aceSMatthew G Knepley 
4080b21d0597SMatthew G Knepley /*@
4081e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4082b21d0597SMatthew G Knepley 
4083b21d0597SMatthew G Knepley   Input Parameters:
4084b21d0597SMatthew G Knepley + dm - The DM
40855080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4086b21d0597SMatthew G Knepley 
4087b21d0597SMatthew G Knepley   Level: intermediate
4088b21d0597SMatthew G Knepley 
4089b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4090b21d0597SMatthew G Knepley 
4091e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
4092b21d0597SMatthew G Knepley @*/
4093e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
40940adebc6cSBarry Smith {
4095b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4096b21d0597SMatthew G Knepley 
4097b21d0597SMatthew G Knepley   PetscFunctionBegin;
4098b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40995080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
41001d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
4101b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
4102b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
4103497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4104f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
4105507e4973SMatthew G. Knepley #endif
4106b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4107b21d0597SMatthew G Knepley }
4108b21d0597SMatthew G Knepley 
410988ed4aceSMatthew G Knepley /*@
411088ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
411188ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
411288ed4aceSMatthew G Knepley 
411388ed4aceSMatthew G Knepley   Input Parameter:
411488ed4aceSMatthew G Knepley . dm - The DM
411588ed4aceSMatthew G Knepley 
411688ed4aceSMatthew G Knepley   Output Parameter:
411788ed4aceSMatthew G Knepley . sf - The PetscSF
411888ed4aceSMatthew G Knepley 
411988ed4aceSMatthew G Knepley   Level: intermediate
412088ed4aceSMatthew G Knepley 
412188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
412288ed4aceSMatthew G Knepley 
412388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
412488ed4aceSMatthew G Knepley @*/
41250adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
41260adebc6cSBarry Smith {
412788ed4aceSMatthew G Knepley   PetscInt       nroots;
412888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
412988ed4aceSMatthew G Knepley 
413088ed4aceSMatthew G Knepley   PetscFunctionBegin;
413188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413288ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
413333907cc2SStefano Zampini   if (!dm->defaultSF) {
413433907cc2SStefano Zampini     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->defaultSF);CHKERRQ(ierr);
413533907cc2SStefano Zampini   }
41360298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
413788ed4aceSMatthew G Knepley   if (nroots < 0) {
413888ed4aceSMatthew G Knepley     PetscSection section, gSection;
413988ed4aceSMatthew G Knepley 
4140e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
414131ea6d37SMatthew G Knepley     if (section) {
4142e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
414388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
414431ea6d37SMatthew G Knepley     } else {
41450298fd71SBarry Smith       *sf = NULL;
414631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
414731ea6d37SMatthew G Knepley     }
414888ed4aceSMatthew G Knepley   }
414988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
415088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
415188ed4aceSMatthew G Knepley }
415288ed4aceSMatthew G Knepley 
415388ed4aceSMatthew G Knepley /*@
415488ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
415588ed4aceSMatthew G Knepley 
415688ed4aceSMatthew G Knepley   Input Parameters:
415788ed4aceSMatthew G Knepley + dm - The DM
415888ed4aceSMatthew G Knepley - sf - The PetscSF
415988ed4aceSMatthew G Knepley 
416088ed4aceSMatthew G Knepley   Level: intermediate
416188ed4aceSMatthew G Knepley 
416288ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
416388ed4aceSMatthew G Knepley 
416488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
416588ed4aceSMatthew G Knepley @*/
41660adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
41670adebc6cSBarry Smith {
416888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
416988ed4aceSMatthew G Knepley 
417088ed4aceSMatthew G Knepley   PetscFunctionBegin;
417188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
417233907cc2SStefano Zampini   if (sf) {
417388ed4aceSMatthew G Knepley     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
417433907cc2SStefano Zampini     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
417533907cc2SStefano Zampini   }
417688ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
417788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
417888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
417988ed4aceSMatthew G Knepley }
418088ed4aceSMatthew G Knepley 
418188ed4aceSMatthew G Knepley /*@C
418288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
418388ed4aceSMatthew G Knepley   describing the data layout.
418488ed4aceSMatthew G Knepley 
418588ed4aceSMatthew G Knepley   Input Parameters:
418688ed4aceSMatthew G Knepley + dm - The DM
418788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
418888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
418988ed4aceSMatthew G Knepley 
419088ed4aceSMatthew G Knepley   Level: intermediate
419188ed4aceSMatthew G Knepley 
419288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
419388ed4aceSMatthew G Knepley @*/
419488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
419588ed4aceSMatthew G Knepley {
419682f516ccSBarry Smith   MPI_Comm       comm;
419788ed4aceSMatthew G Knepley   PetscLayout    layout;
419888ed4aceSMatthew G Knepley   const PetscInt *ranges;
419988ed4aceSMatthew G Knepley   PetscInt       *local;
420088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4201ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
420288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
420388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
420488ed4aceSMatthew G Knepley 
420588ed4aceSMatthew G Knepley   PetscFunctionBegin;
420688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4207367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
420888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
420988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
421088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
421188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
421288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
421388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
421488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
421588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
421688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4217ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
42186636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
421988ed4aceSMatthew G Knepley 
42206636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
42216636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4222235fbf56SMatthew 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));
42236636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
422488ed4aceSMatthew G Knepley   }
4225785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4226785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
422788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
42281f588964SMatthew G Knepley     const PetscInt *cind;
42296636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
423088ed4aceSMatthew G Knepley 
423188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
423288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
423388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
423488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
423588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
42366636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
423788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
42386636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
42396636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
42406636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4241057b4bcdSMatthew 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);
42426636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
42436636e97aSMatthew G Knepley     }
424488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
424588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
424688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
424788ed4aceSMatthew G Knepley     }
424888ed4aceSMatthew G Knepley     if (gdof < 0) {
42496636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
425088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
425188ed4aceSMatthew G Knepley 
425205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
425331d3f06eSJed Brown         if (r < 0) r = -(r+2);
425405376888SMatthew 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);
425588ed4aceSMatthew G Knepley         remote[l].rank  = r;
425688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
425788ed4aceSMatthew G Knepley       }
425888ed4aceSMatthew G Knepley     } else {
42596636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
426088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
426188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
426288ed4aceSMatthew G Knepley       }
426388ed4aceSMatthew G Knepley     }
426488ed4aceSMatthew G Knepley   }
42656636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
426688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
426788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
426888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
426988ed4aceSMatthew G Knepley }
4270af122d2aSMatthew G Knepley 
4271b21d0597SMatthew G Knepley /*@
4272b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4273b21d0597SMatthew G Knepley 
4274b21d0597SMatthew G Knepley   Input Parameter:
4275b21d0597SMatthew G Knepley . dm - The DM
4276b21d0597SMatthew G Knepley 
4277b21d0597SMatthew G Knepley   Output Parameter:
4278b21d0597SMatthew G Knepley . sf - The PetscSF
4279b21d0597SMatthew G Knepley 
4280b21d0597SMatthew G Knepley   Level: intermediate
4281b21d0597SMatthew G Knepley 
4282b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4283b21d0597SMatthew G Knepley 
4284057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4285b21d0597SMatthew G Knepley @*/
42860adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
42870adebc6cSBarry Smith {
4288b21d0597SMatthew G Knepley   PetscFunctionBegin;
4289b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4290b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4291b21d0597SMatthew G Knepley   *sf = dm->sf;
4292b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4293b21d0597SMatthew G Knepley }
4294b21d0597SMatthew G Knepley 
4295057b4bcdSMatthew G Knepley /*@
4296057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4297057b4bcdSMatthew G Knepley 
4298057b4bcdSMatthew G Knepley   Input Parameters:
4299057b4bcdSMatthew G Knepley + dm - The DM
4300057b4bcdSMatthew G Knepley - sf - The PetscSF
4301057b4bcdSMatthew G Knepley 
4302057b4bcdSMatthew G Knepley   Level: intermediate
4303057b4bcdSMatthew G Knepley 
4304057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4305057b4bcdSMatthew G Knepley @*/
43060adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
43070adebc6cSBarry Smith {
4308057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4309057b4bcdSMatthew G Knepley 
4310057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4311057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
431233907cc2SStefano Zampini   if (sf) {
431333907cc2SStefano Zampini     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4314057b4bcdSMatthew G Knepley     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
431533907cc2SStefano Zampini   }
431633907cc2SStefano Zampini   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4317057b4bcdSMatthew G Knepley   dm->sf = sf;
4318057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4319057b4bcdSMatthew G Knepley }
4320057b4bcdSMatthew G Knepley 
432134aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
432234aa8a36SMatthew G. Knepley {
432334aa8a36SMatthew G. Knepley   PetscClassId   id;
432434aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
432534aa8a36SMatthew G. Knepley 
432634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
432734aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
432834aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
432934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
433034aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
433134aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
433217c1d62eSMatthew G. Knepley   } else {
433317c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
433434aa8a36SMatthew G. Knepley   }
433534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
433634aa8a36SMatthew G. Knepley }
433734aa8a36SMatthew G. Knepley 
433844a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
433944a7f3ddSMatthew G. Knepley {
434044a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
434144a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
434244a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
434344a7f3ddSMatthew G. Knepley 
434444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
434544a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
434644a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
434744a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
434844a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
434944a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
435044a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
435144a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
435244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
435344a7f3ddSMatthew G. Knepley }
435444a7f3ddSMatthew G. Knepley 
435544a7f3ddSMatthew G. Knepley /*@
435644a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
435744a7f3ddSMatthew G. Knepley 
435844a7f3ddSMatthew G. Knepley   Logically collective on DM
435944a7f3ddSMatthew G. Knepley 
436044a7f3ddSMatthew G. Knepley   Input Parameter:
436144a7f3ddSMatthew G. Knepley . dm - The DM
436244a7f3ddSMatthew G. Knepley 
436344a7f3ddSMatthew G. Knepley   Level: intermediate
436444a7f3ddSMatthew G. Knepley 
436544a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
436644a7f3ddSMatthew G. Knepley @*/
436744a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
436844a7f3ddSMatthew G. Knepley {
436944a7f3ddSMatthew G. Knepley   PetscInt       f;
437044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
437144a7f3ddSMatthew G. Knepley 
437244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
437344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
437444a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
437544a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
437644a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
437744a7f3ddSMatthew G. Knepley   }
437844a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
437944a7f3ddSMatthew G. Knepley   dm->fields = NULL;
438044a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
438144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
438244a7f3ddSMatthew G. Knepley }
438344a7f3ddSMatthew G. Knepley 
4384689b5837SMatthew G. Knepley /*@
4385689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4386689b5837SMatthew G. Knepley 
4387689b5837SMatthew G. Knepley   Not collective
4388689b5837SMatthew G. Knepley 
4389689b5837SMatthew G. Knepley   Input Parameter:
4390689b5837SMatthew G. Knepley . dm - The DM
4391689b5837SMatthew G. Knepley 
4392689b5837SMatthew G. Knepley   Output Parameter:
4393689b5837SMatthew G. Knepley . Nf - The number of fields
4394689b5837SMatthew G. Knepley 
4395689b5837SMatthew G. Knepley   Level: intermediate
4396689b5837SMatthew G. Knepley 
4397689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4398689b5837SMatthew G. Knepley @*/
43990f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
44000f21e855SMatthew G. Knepley {
44010f21e855SMatthew G. Knepley   PetscFunctionBegin;
44020f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
440344a7f3ddSMatthew G. Knepley   PetscValidPointer(numFields, 2);
440444a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4405af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4406af122d2aSMatthew G Knepley }
4407af122d2aSMatthew G Knepley 
4408689b5837SMatthew G. Knepley /*@
4409689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4410689b5837SMatthew G. Knepley 
4411689b5837SMatthew G. Knepley   Logically collective on DM
4412689b5837SMatthew G. Knepley 
4413689b5837SMatthew G. Knepley   Input Parameters:
4414689b5837SMatthew G. Knepley + dm - The DM
4415689b5837SMatthew G. Knepley - Nf - The number of fields
4416689b5837SMatthew G. Knepley 
4417689b5837SMatthew G. Knepley   Level: intermediate
4418689b5837SMatthew G. Knepley 
4419689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4420689b5837SMatthew G. Knepley @*/
4421af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4422af122d2aSMatthew G Knepley {
44230f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4424af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4425af122d2aSMatthew G Knepley 
4426af122d2aSMatthew G Knepley   PetscFunctionBegin;
4427af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442844a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
44290f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
44300f21e855SMatthew G. Knepley     PetscContainer obj;
44310f21e855SMatthew G. Knepley 
44320f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
443344a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
44340f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4435af122d2aSMatthew G Knepley   }
4436af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4437af122d2aSMatthew G Knepley }
4438af122d2aSMatthew G Knepley 
4439c1929be8SMatthew G. Knepley /*@
4440c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4441c1929be8SMatthew G. Knepley 
4442c1929be8SMatthew G. Knepley   Not collective
4443c1929be8SMatthew G. Knepley 
4444c1929be8SMatthew G. Knepley   Input Parameters:
4445c1929be8SMatthew G. Knepley + dm - The DM
4446c1929be8SMatthew G. Knepley - f  - The field number
4447c1929be8SMatthew G. Knepley 
444844a7f3ddSMatthew G. Knepley   Output Parameters:
444944a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
445044a7f3ddSMatthew G. Knepley - field - The discretization object
4451c1929be8SMatthew G. Knepley 
445244a7f3ddSMatthew G. Knepley   Level: intermediate
4453c1929be8SMatthew G. Knepley 
445444a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4455c1929be8SMatthew G. Knepley @*/
445644a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4457af122d2aSMatthew G Knepley {
4458af122d2aSMatthew G Knepley   PetscFunctionBegin;
4459af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
446044a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
446144a7f3ddSMatthew 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);
446244a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
446344a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4464decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4465decb47aaSMatthew G. Knepley }
4466decb47aaSMatthew G. Knepley 
4467c1929be8SMatthew G. Knepley /*@
4468c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4469c1929be8SMatthew G. Knepley 
4470c1929be8SMatthew G. Knepley   Logically collective on DM
4471c1929be8SMatthew G. Knepley 
4472c1929be8SMatthew G. Knepley   Input Parameters:
4473c1929be8SMatthew G. Knepley + dm    - The DM
4474c1929be8SMatthew G. Knepley . f     - The field number
447544a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4476c1929be8SMatthew G. Knepley - field - The discretization object
4477c1929be8SMatthew G. Knepley 
447844a7f3ddSMatthew G. Knepley   Level: intermediate
4479c1929be8SMatthew G. Knepley 
448044a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4481c1929be8SMatthew G. Knepley @*/
448244a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4483decb47aaSMatthew G. Knepley {
4484decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4485decb47aaSMatthew G. Knepley 
4486decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4487decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4488e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
448944a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4490e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
449144a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
449244a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
449344a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
449444a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
449544a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
449644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
449744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
449834aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
44992df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
450044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
450144a7f3ddSMatthew G. Knepley }
450244a7f3ddSMatthew G. Knepley 
450344a7f3ddSMatthew G. Knepley /*@
450444a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
450544a7f3ddSMatthew G. Knepley 
450644a7f3ddSMatthew G. Knepley   Logically collective on DM
450744a7f3ddSMatthew G. Knepley 
450844a7f3ddSMatthew G. Knepley   Input Parameters:
450944a7f3ddSMatthew G. Knepley + dm    - The DM
451044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
451144a7f3ddSMatthew G. Knepley - field - The discretization object
451244a7f3ddSMatthew G. Knepley 
451344a7f3ddSMatthew G. Knepley   Level: intermediate
451444a7f3ddSMatthew G. Knepley 
451544a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
451644a7f3ddSMatthew G. Knepley @*/
451744a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
451844a7f3ddSMatthew G. Knepley {
451944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
452044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
452144a7f3ddSMatthew G. Knepley 
452244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
452344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
452444a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
452544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
452644a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
452744a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
452844a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
452944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
453044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
453134aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
45322df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4533af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4534af122d2aSMatthew G Knepley }
45356636e97aSMatthew G Knepley 
4536e5e52638SMatthew G. Knepley /*@
4537e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4538e5e52638SMatthew G. Knepley 
4539e5e52638SMatthew G. Knepley   Collective on DM
4540e5e52638SMatthew G. Knepley 
4541e5e52638SMatthew G. Knepley   Input Parameter:
4542e5e52638SMatthew G. Knepley . dm - The DM
4543e5e52638SMatthew G. Knepley 
4544e5e52638SMatthew G. Knepley   Output Parameter:
4545e5e52638SMatthew G. Knepley . newdm - The DM
4546e5e52638SMatthew G. Knepley 
4547e5e52638SMatthew G. Knepley   Level: advanced
4548e5e52638SMatthew G. Knepley 
4549e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4550e5e52638SMatthew G. Knepley @*/
4551e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4552e5e52638SMatthew G. Knepley {
4553e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4554e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4555e5e52638SMatthew G. Knepley 
4556e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4557e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4558e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4559e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4560e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4561e5e52638SMatthew G. Knepley     DMLabel     label;
4562e5e52638SMatthew G. Knepley     PetscObject field;
456334aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4564e5e52638SMatthew G. Knepley 
4565e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4566e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
456734aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
456834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
456934aa8a36SMatthew G. Knepley   }
457034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
457134aa8a36SMatthew G. Knepley }
457234aa8a36SMatthew G. Knepley 
457334aa8a36SMatthew G. Knepley /*@
457434aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
457534aa8a36SMatthew G. Knepley 
457634aa8a36SMatthew G. Knepley   Not collective
457734aa8a36SMatthew G. Knepley 
457834aa8a36SMatthew G. Knepley   Input Parameters:
457934aa8a36SMatthew G. Knepley + dm - The DM object
458034aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
458134aa8a36SMatthew G. Knepley 
458234aa8a36SMatthew G. Knepley   Output Parameter:
458334aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
458434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
458534aa8a36SMatthew G. Knepley 
458634aa8a36SMatthew G. Knepley   Notes:
458734aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
458834aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
458934aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4590979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
459134aa8a36SMatthew G. Knepley 
459234aa8a36SMatthew G. Knepley   Level: developer
459334aa8a36SMatthew G. Knepley 
459434aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
459534aa8a36SMatthew G. Knepley @*/
459634aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
459734aa8a36SMatthew G. Knepley {
459834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
459934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
460034aa8a36SMatthew G. Knepley   if (useCone) PetscValidPointer(useCone, 3);
460134aa8a36SMatthew G. Knepley   if (useClosure) PetscValidPointer(useClosure, 4);
460234aa8a36SMatthew G. Knepley   if (f < 0) {
460334aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
460434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
460534aa8a36SMatthew G. Knepley   } else {
460634aa8a36SMatthew G. Knepley     PetscInt       Nf;
460734aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
460834aa8a36SMatthew G. Knepley 
460934aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
461034aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
461134aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
461234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
461334aa8a36SMatthew G. Knepley   }
461434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
461534aa8a36SMatthew G. Knepley }
461634aa8a36SMatthew G. Knepley 
461734aa8a36SMatthew G. Knepley /*@
461834aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
461934aa8a36SMatthew G. Knepley 
462034aa8a36SMatthew G. Knepley   Not collective
462134aa8a36SMatthew G. Knepley 
462234aa8a36SMatthew G. Knepley   Input Parameters:
462334aa8a36SMatthew G. Knepley + dm         - The DM object
462434aa8a36SMatthew G. Knepley . f          - The field number
462534aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
462634aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
462734aa8a36SMatthew G. Knepley 
462834aa8a36SMatthew G. Knepley   Notes:
462934aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
463034aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
463134aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4632979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
463334aa8a36SMatthew G. Knepley 
463434aa8a36SMatthew G. Knepley   Level: developer
463534aa8a36SMatthew G. Knepley 
463634aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
463734aa8a36SMatthew G. Knepley @*/
463834aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
463934aa8a36SMatthew G. Knepley {
464034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
464134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
464234aa8a36SMatthew G. Knepley   if (f < 0) {
464334aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
464434aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
464534aa8a36SMatthew G. Knepley   } else {
464634aa8a36SMatthew G. Knepley     PetscInt       Nf;
464734aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
464834aa8a36SMatthew G. Knepley 
464934aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
465034aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
465134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
465234aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4653e5e52638SMatthew G. Knepley   }
4654e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4655e5e52638SMatthew G. Knepley }
4656e5e52638SMatthew G. Knepley 
4657b0441da4SMatthew G. Knepley /*@
4658b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4659b0441da4SMatthew G. Knepley 
4660b0441da4SMatthew G. Knepley   Not collective
4661b0441da4SMatthew G. Knepley 
4662b0441da4SMatthew G. Knepley   Input Parameters:
4663b0441da4SMatthew G. Knepley . dm - The DM object
4664b0441da4SMatthew G. Knepley 
4665b0441da4SMatthew G. Knepley   Output Parameter:
4666b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4667b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4668b0441da4SMatthew G. Knepley 
4669b0441da4SMatthew G. Knepley   Notes:
4670b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4671b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4672b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4673b0441da4SMatthew G. Knepley 
4674b0441da4SMatthew G. Knepley   Level: developer
4675b0441da4SMatthew G. Knepley 
4676b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4677b0441da4SMatthew G. Knepley @*/
4678b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4679b0441da4SMatthew G. Knepley {
4680b0441da4SMatthew G. Knepley   PetscInt       Nf;
4681b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4682b0441da4SMatthew G. Knepley 
4683b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4684b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4685b0441da4SMatthew G. Knepley   if (useCone) PetscValidPointer(useCone, 3);
4686b0441da4SMatthew G. Knepley   if (useClosure) PetscValidPointer(useClosure, 4);
4687b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4688b0441da4SMatthew G. Knepley   if (!Nf) {
4689b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4690b0441da4SMatthew G. Knepley   } else {
4691b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4692b0441da4SMatthew G. Knepley   }
4693b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4694b0441da4SMatthew G. Knepley }
4695b0441da4SMatthew G. Knepley 
4696b0441da4SMatthew G. Knepley /*@
4697b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4698b0441da4SMatthew G. Knepley 
4699b0441da4SMatthew G. Knepley   Not collective
4700b0441da4SMatthew G. Knepley 
4701b0441da4SMatthew G. Knepley   Input Parameters:
4702b0441da4SMatthew G. Knepley + dm         - The DM object
4703b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4704b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4705b0441da4SMatthew G. Knepley 
4706b0441da4SMatthew G. Knepley   Notes:
4707b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4708b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4709b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4710b0441da4SMatthew G. Knepley 
4711b0441da4SMatthew G. Knepley   Level: developer
4712b0441da4SMatthew G. Knepley 
4713b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4714b0441da4SMatthew G. Knepley @*/
4715b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4716b0441da4SMatthew G. Knepley {
4717b0441da4SMatthew G. Knepley   PetscInt       Nf;
4718b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4719b0441da4SMatthew G. Knepley 
4720b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4721b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4722b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4723b0441da4SMatthew G. Knepley   if (!Nf) {
4724b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4725b0441da4SMatthew G. Knepley   } else {
4726b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4727e5e52638SMatthew G. Knepley   }
4728e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4729e5e52638SMatthew G. Knepley }
4730e5e52638SMatthew G. Knepley 
4731e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4732e5e52638SMatthew G. Knepley {
4733e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4734e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4735e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4736e5e52638SMatthew G. Knepley 
4737e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4738e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4739e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4740e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4741b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4742e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4743e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4744e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4745e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4746e5e52638SMatthew G. Knepley }
4747e5e52638SMatthew G. Knepley 
4748e5e52638SMatthew G. Knepley /*@
4749e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4750e5e52638SMatthew G. Knepley 
4751e5e52638SMatthew G. Knepley   Not collective
4752e5e52638SMatthew G. Knepley 
4753e5e52638SMatthew G. Knepley   Input Parameter:
4754e5e52638SMatthew G. Knepley . dm - The DM
4755e5e52638SMatthew G. Knepley 
4756e5e52638SMatthew G. Knepley   Output Parameter:
4757e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4758e5e52638SMatthew G. Knepley 
4759e5e52638SMatthew G. Knepley   Level: intermediate
4760e5e52638SMatthew G. Knepley 
4761e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4762e5e52638SMatthew G. Knepley @*/
4763e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4764e5e52638SMatthew G. Knepley {
4765e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4766e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4767e5e52638SMatthew G. Knepley   PetscValidPointer(Nds, 2);
4768e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4769e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4770e5e52638SMatthew G. Knepley }
4771e5e52638SMatthew G. Knepley 
4772e5e52638SMatthew G. Knepley /*@
4773e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4774e5e52638SMatthew G. Knepley 
4775e5e52638SMatthew G. Knepley   Logically collective on DM
4776e5e52638SMatthew G. Knepley 
4777e5e52638SMatthew G. Knepley   Input Parameter:
4778e5e52638SMatthew G. Knepley . dm - The DM
4779e5e52638SMatthew G. Knepley 
4780e5e52638SMatthew G. Knepley   Level: intermediate
4781e5e52638SMatthew G. Knepley 
4782e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4783e5e52638SMatthew G. Knepley @*/
4784e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4785e5e52638SMatthew G. Knepley {
4786e5e52638SMatthew G. Knepley   PetscInt       s;
4787e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4788e5e52638SMatthew G. Knepley 
4789e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4790e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4791e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4792e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4793e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4794b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4795e5e52638SMatthew G. Knepley   }
4796e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4797e5e52638SMatthew G. Knepley   dm->probs = NULL;
4798e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4799e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4800e5e52638SMatthew G. Knepley }
4801e5e52638SMatthew G. Knepley 
4802e5e52638SMatthew G. Knepley /*@
4803e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4804e5e52638SMatthew G. Knepley 
4805e5e52638SMatthew G. Knepley   Not collective
4806e5e52638SMatthew G. Knepley 
4807e5e52638SMatthew G. Knepley   Input Parameter:
4808e5e52638SMatthew G. Knepley . dm    - The DM
4809e5e52638SMatthew G. Knepley 
4810e5e52638SMatthew G. Knepley   Output Parameter:
4811e5e52638SMatthew G. Knepley . prob - The default PetscDS
4812e5e52638SMatthew G. Knepley 
4813e5e52638SMatthew G. Knepley   Level: intermediate
4814e5e52638SMatthew G. Knepley 
4815e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4816e5e52638SMatthew G. Knepley @*/
4817e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4818e5e52638SMatthew G. Knepley {
4819b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4820b0143b4dSMatthew G. Knepley 
4821e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4822e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4823e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4824b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4825b0143b4dSMatthew G. Knepley     PetscDS ds;
4826b0143b4dSMatthew G. Knepley 
4827b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4828b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4829b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4830b0143b4dSMatthew G. Knepley   }
4831b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4832e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4833e5e52638SMatthew G. Knepley }
4834e5e52638SMatthew G. Knepley 
4835e5e52638SMatthew G. Knepley /*@
4836e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4837e5e52638SMatthew G. Knepley 
4838e5e52638SMatthew G. Knepley   Not collective
4839e5e52638SMatthew G. Knepley 
4840e5e52638SMatthew G. Knepley   Input Parameters:
4841e5e52638SMatthew G. Knepley + dm    - The DM
4842e5e52638SMatthew G. Knepley - point - Cell for the DS
4843e5e52638SMatthew G. Knepley 
4844e5e52638SMatthew G. Knepley   Output Parameter:
4845e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4846e5e52638SMatthew G. Knepley 
4847e5e52638SMatthew G. Knepley   Level: developer
4848e5e52638SMatthew G. Knepley 
4849b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
4850e5e52638SMatthew G. Knepley @*/
4851e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4852e5e52638SMatthew G. Knepley {
4853e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
4854e5e52638SMatthew G. Knepley   PetscInt       s;
4855e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4856e5e52638SMatthew G. Knepley 
4857e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4858e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4859e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
4860e5e52638SMatthew G. Knepley   *prob = NULL;
4861e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4862e5e52638SMatthew G. Knepley     PetscInt val;
4863e5e52638SMatthew G. Knepley 
4864e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
4865e5e52638SMatthew G. Knepley     else {
4866e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
4867e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
4868e5e52638SMatthew G. Knepley     }
4869e5e52638SMatthew G. Knepley   }
4870e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
4871e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4872e5e52638SMatthew G. Knepley }
4873e5e52638SMatthew G. Knepley 
4874e5e52638SMatthew G. Knepley /*@
4875e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
4876e5e52638SMatthew G. Knepley 
4877e5e52638SMatthew G. Knepley   Not collective
4878e5e52638SMatthew G. Knepley 
4879e5e52638SMatthew G. Knepley   Input Parameters:
4880e5e52638SMatthew G. Knepley + dm    - The DM
4881e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
4882e5e52638SMatthew G. Knepley 
4883b3cf3223SMatthew G. Knepley   Output Parameters:
4884b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
4885b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
4886e5e52638SMatthew G. Knepley 
4887e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
4888e5e52638SMatthew G. Knepley 
4889e5e52638SMatthew G. Knepley   Level: advanced
4890e5e52638SMatthew G. Knepley 
4891e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4892e5e52638SMatthew G. Knepley @*/
4893b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
4894e5e52638SMatthew G. Knepley {
4895e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
4896e5e52638SMatthew G. Knepley 
4897e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4898e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4899e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4900b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
4901b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
4902e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4903b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
4904b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
4905b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
4906b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
4907b3cf3223SMatthew G. Knepley     }
4908e5e52638SMatthew G. Knepley   }
49092df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
4910e5e52638SMatthew G. Knepley }
4911e5e52638SMatthew G. Knepley 
4912e5e52638SMatthew G. Knepley /*@
4913e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
4914e5e52638SMatthew G. Knepley 
4915e5e52638SMatthew G. Knepley   Not collective
4916e5e52638SMatthew G. Knepley 
4917e5e52638SMatthew G. Knepley   Input Parameters:
4918e5e52638SMatthew G. Knepley + dm  - The DM
4919e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
4920e5e52638SMatthew G. Knepley 
4921e5e52638SMatthew G. Knepley   Output Parameters:
4922b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
4923b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
4924b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
4925e5e52638SMatthew G. Knepley 
4926e5e52638SMatthew G. Knepley   Level: advanced
4927e5e52638SMatthew G. Knepley 
4928e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4929e5e52638SMatthew G. Knepley @*/
4930b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
4931e5e52638SMatthew G. Knepley {
4932e5e52638SMatthew G. Knepley   PetscInt       Nds;
4933e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4934e5e52638SMatthew G. Knepley 
4935e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4936e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4937e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4938e5e52638SMatthew 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);
4939e5e52638SMatthew G. Knepley   if (label) {
4940e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
4941e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
4942e5e52638SMatthew G. Knepley   }
4943b3cf3223SMatthew G. Knepley   if (fields) {
4944b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
4945b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
4946b3cf3223SMatthew G. Knepley   }
4947e5e52638SMatthew G. Knepley   if (ds) {
4948b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
4949e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
4950e5e52638SMatthew G. Knepley   }
4951e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4952e5e52638SMatthew G. Knepley }
4953e5e52638SMatthew G. Knepley 
4954e5e52638SMatthew G. Knepley /*@
4955e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
4956e5e52638SMatthew G. Knepley 
4957e5e52638SMatthew G. Knepley   Collective on DM
4958e5e52638SMatthew G. Knepley 
4959e5e52638SMatthew G. Knepley   Input Parameters:
4960e5e52638SMatthew G. Knepley + dm     - The DM
4961e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
4962b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
4963e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
4964e5e52638SMatthew G. Knepley 
4965b3cf3223SMatthew 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,
4966b3cf3223SMatthew G. Knepley   the fields argument is ignored.
4967e5e52638SMatthew G. Knepley 
4968e5e52638SMatthew G. Knepley   Level: advanced
4969e5e52638SMatthew G. Knepley 
4970e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
4971e5e52638SMatthew G. Knepley @*/
4972b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
4973e5e52638SMatthew G. Knepley {
4974e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4975e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4976e5e52638SMatthew G. Knepley 
4977e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4978e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4979e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4980e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
4981e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4982e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
4983b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4984e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
4985e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
4986e5e52638SMatthew G. Knepley     }
4987e5e52638SMatthew G. Knepley   }
49881b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
4989e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4990b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
4991e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
4992e5e52638SMatthew G. Knepley   if (!label) {
4993e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
4994e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
4995e5e52638SMatthew G. Knepley     Nds = 0;
4996e5e52638SMatthew G. Knepley   }
4997e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
4998b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
4999e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5000e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5001e5e52638SMatthew G. Knepley }
5002e5e52638SMatthew G. Knepley 
5003e5e52638SMatthew G. Knepley /*@
5004e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5005e5e52638SMatthew G. Knepley 
5006e5e52638SMatthew G. Knepley   Collective on DM
5007e5e52638SMatthew G. Knepley 
5008e5e52638SMatthew G. Knepley   Input Parameter:
5009e5e52638SMatthew G. Knepley . dm - The DM
5010e5e52638SMatthew G. Knepley 
5011e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5012e5e52638SMatthew G. Knepley 
5013e5e52638SMatthew G. Knepley   Level: intermediate
5014e5e52638SMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5016e5e52638SMatthew G. Knepley @*/
5017e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5018e5e52638SMatthew G. Knepley {
5019e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5020e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5021b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5022e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5023e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5024e5e52638SMatthew G. Knepley 
5025e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5026e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5027e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5028e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5029e5e52638SMatthew G. Knepley    1) NULL
5030e5e52638SMatthew G. Knepley    2) Hybrid cells
5031e5e52638SMatthew G. Knepley   */
5032e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5033e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5034e5e52638SMatthew G. Knepley   /* Create default DS */
5035b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
50362df9ee95SMatthew G. Knepley   if (!prob) {
5037b3cf3223SMatthew G. Knepley     IS        fields;
5038b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5039b3cf3223SMatthew G. Knepley 
5040b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5041b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5042b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
504388f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
504488f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
504588f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
504688f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
504788f0c812SMatthew G. Knepley 
50482df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5049b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
50502df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5051b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5052b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
50532df9ee95SMatthew G. Knepley   }
5054e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5055e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5056b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5057e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5058e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5059e5e52638SMatthew G. Knepley 
5060e5e52638SMatthew G. Knepley     if (label) {
50610122748bSMatthew G. Knepley       DM       plex;
50620122748bSMatthew G. Knepley       PetscInt depth, pMax[4];
50630122748bSMatthew G. Knepley 
50640122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
50650122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
50660122748bSMatthew 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);
50670122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
50680122748bSMatthew G. Knepley 
5069e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5070e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5071e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5072b3cf3223SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, NULL, probh);CHKERRQ(ierr);
5073e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5074e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5075e5e52638SMatthew G. Knepley       break;
5076e5e52638SMatthew G. Knepley     }
5077e5e52638SMatthew G. Knepley   }
5078e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5079b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5080e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5081e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5082e5e52638SMatthew G. Knepley 
5083e5e52638SMatthew G. Knepley     if (!label) {
5084e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5085e5e52638SMatthew G. Knepley       if (probh) {
5086e5e52638SMatthew G. Knepley         PetscFE subfe;
5087e5e52638SMatthew G. Knepley 
5088e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5089e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5090e5e52638SMatthew G. Knepley       }
5091e5e52638SMatthew G. Knepley     } else {
5092e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5093e5e52638SMatthew G. Knepley     }
5094e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5095e5e52638SMatthew G. Knepley     {
5096e5e52638SMatthew G. Knepley       PetscClassId id;
5097e5e52638SMatthew G. Knepley 
5098e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5099e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5100e5e52638SMatthew G. Knepley     }
5101e5e52638SMatthew G. Knepley   }
5102e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5103e5e52638SMatthew G. Knepley   /* Setup DSes */
5104e5e52638SMatthew G. Knepley   if (doSetup) {
5105e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5106e5e52638SMatthew G. Knepley   }
5107e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5108e5e52638SMatthew G. Knepley }
5109e5e52638SMatthew G. Knepley 
5110e5e52638SMatthew G. Knepley /*@
5111e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5112e5e52638SMatthew G. Knepley 
5113e5e52638SMatthew G. Knepley   Collective on DM
5114e5e52638SMatthew G. Knepley 
5115e5e52638SMatthew G. Knepley   Input Parameter:
5116e5e52638SMatthew G. Knepley . dm - The DM
5117e5e52638SMatthew G. Knepley 
5118e5e52638SMatthew G. Knepley   Output Parameter:
5119e5e52638SMatthew G. Knepley . newdm - The DM
5120e5e52638SMatthew G. Knepley 
5121e5e52638SMatthew G. Knepley   Level: advanced
5122e5e52638SMatthew G. Knepley 
5123e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5124e5e52638SMatthew G. Knepley @*/
5125e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5126e5e52638SMatthew G. Knepley {
5127e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5128e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5129e5e52638SMatthew G. Knepley 
5130e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5131e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5132e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5133e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5134e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5135e5e52638SMatthew G. Knepley     DMLabel label;
5136b3cf3223SMatthew G. Knepley     IS      fields;
5137e5e52638SMatthew G. Knepley     PetscDS ds;
5138e5e52638SMatthew G. Knepley 
5139b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5140b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5141e5e52638SMatthew G. Knepley   }
5142e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5143e5e52638SMatthew G. Knepley }
5144e5e52638SMatthew G. Knepley 
5145e5e52638SMatthew G. Knepley /*@
5146e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5147e5e52638SMatthew G. Knepley 
5148e5e52638SMatthew G. Knepley   Collective on DM
5149e5e52638SMatthew G. Knepley 
5150e5e52638SMatthew G. Knepley   Input Parameter:
5151e5e52638SMatthew G. Knepley . dm - The DM
5152e5e52638SMatthew G. Knepley 
5153e5e52638SMatthew G. Knepley   Output Parameter:
5154e5e52638SMatthew G. Knepley . newdm - The DM
5155e5e52638SMatthew G. Knepley 
5156e5e52638SMatthew G. Knepley   Level: advanced
5157e5e52638SMatthew G. Knepley 
5158e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5159e5e52638SMatthew G. Knepley @*/
5160e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5161e5e52638SMatthew G. Knepley {
5162e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5163e5e52638SMatthew G. Knepley 
5164e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5165e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5166e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5167e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5168e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5169e5e52638SMatthew G. Knepley }
5170e5e52638SMatthew G. Knepley 
5171b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5172b64e0483SPeter Brune {
5173b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5174b64e0483SPeter Brune   PetscErrorCode ierr;
5175b64e0483SPeter Brune   Vec coords,ccoords;
51766dbf9973SLawrence Mitchell   Mat inject;
5177b64e0483SPeter Brune   PetscFunctionBegin;
5178b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5179b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5180b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5181b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5182b64e0483SPeter Brune   if (coords && !ccoords) {
5183b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
51846668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
51856dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
51862adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
51876dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5188b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5189b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5190b64e0483SPeter Brune   }
5191b64e0483SPeter Brune   PetscFunctionReturn(0);
5192b64e0483SPeter Brune }
5193b64e0483SPeter Brune 
519403dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
519503dadc2fSPeter Brune {
519603dadc2fSPeter Brune   DM dm_coord,subdm_coord;
519703dadc2fSPeter Brune   PetscErrorCode ierr;
519803dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
519903dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
520003dadc2fSPeter Brune   PetscFunctionBegin;
520103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
520203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
520303dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
520403dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
520503dadc2fSPeter Brune   if (coords && !ccoords) {
520603dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
52076668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
520803dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
520924640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
521003dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
521103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
521203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
521303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
521403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
521503dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
521603dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
521703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
521803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
521903dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
522003dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
522103dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
522203dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
522303dadc2fSPeter Brune   }
522403dadc2fSPeter Brune   PetscFunctionReturn(0);
522503dadc2fSPeter Brune }
522603dadc2fSPeter Brune 
5227c73cfb54SMatthew G. Knepley /*@
5228c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5229c73cfb54SMatthew G. Knepley 
5230c73cfb54SMatthew G. Knepley   Not collective
5231c73cfb54SMatthew G. Knepley 
5232c73cfb54SMatthew G. Knepley   Input Parameter:
5233c73cfb54SMatthew G. Knepley . dm - The DM
5234c73cfb54SMatthew G. Knepley 
5235c73cfb54SMatthew G. Knepley   Output Parameter:
5236c73cfb54SMatthew G. Knepley . dim - The topological dimension
5237c73cfb54SMatthew G. Knepley 
5238c73cfb54SMatthew G. Knepley   Level: beginner
5239c73cfb54SMatthew G. Knepley 
5240c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5241c73cfb54SMatthew G. Knepley @*/
5242c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5243c73cfb54SMatthew G. Knepley {
5244c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5245c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5246c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
5247c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5248c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5249c73cfb54SMatthew G. Knepley }
5250c73cfb54SMatthew G. Knepley 
5251c73cfb54SMatthew G. Knepley /*@
5252c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5253c73cfb54SMatthew G. Knepley 
5254c73cfb54SMatthew G. Knepley   Collective on dm
5255c73cfb54SMatthew G. Knepley 
5256c73cfb54SMatthew G. Knepley   Input Parameters:
5257c73cfb54SMatthew G. Knepley + dm - The DM
5258c73cfb54SMatthew G. Knepley - dim - The topological dimension
5259c73cfb54SMatthew G. Knepley 
5260c73cfb54SMatthew G. Knepley   Level: beginner
5261c73cfb54SMatthew G. Knepley 
5262c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5263c73cfb54SMatthew G. Knepley @*/
5264c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5265c73cfb54SMatthew G. Knepley {
5266e5e52638SMatthew G. Knepley   PetscDS        ds;
5267f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5268f17e8794SMatthew G. Knepley 
5269c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5270c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5271c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5272c73cfb54SMatthew G. Knepley   dm->dim = dim;
5273e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5274e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5275c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5276c73cfb54SMatthew G. Knepley }
5277c73cfb54SMatthew G. Knepley 
5278793f3fe5SMatthew G. Knepley /*@
5279793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5280793f3fe5SMatthew G. Knepley 
5281793f3fe5SMatthew G. Knepley   Collective on DM
5282793f3fe5SMatthew G. Knepley 
5283793f3fe5SMatthew G. Knepley   Input Parameters:
5284793f3fe5SMatthew G. Knepley + dm - the DM
5285793f3fe5SMatthew G. Knepley - dim - the dimension
5286793f3fe5SMatthew G. Knepley 
5287793f3fe5SMatthew G. Knepley   Output Parameters:
5288793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5289793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
5290793f3fe5SMatthew G. Knepley 
5291793f3fe5SMatthew G. Knepley   Note:
5292793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5293ebfe4b0dSMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5294793f3fe5SMatthew G. Knepley   then the interval is empty.
5295793f3fe5SMatthew G. Knepley 
5296793f3fe5SMatthew G. Knepley   Level: intermediate
5297793f3fe5SMatthew G. Knepley 
5298793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
5299793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5300793f3fe5SMatthew G. Knepley @*/
5301793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5302793f3fe5SMatthew G. Knepley {
5303793f3fe5SMatthew G. Knepley   PetscInt       d;
5304793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5305793f3fe5SMatthew G. Knepley 
5306793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5307793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5308793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5309793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5310793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5311793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5312793f3fe5SMatthew G. Knepley }
5313793f3fe5SMatthew G. Knepley 
53146636e97aSMatthew G Knepley /*@
53156636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
53166636e97aSMatthew G Knepley 
53176636e97aSMatthew G Knepley   Collective on DM
53186636e97aSMatthew G Knepley 
53196636e97aSMatthew G Knepley   Input Parameters:
53206636e97aSMatthew G Knepley + dm - the DM
53216636e97aSMatthew G Knepley - c - coordinate vector
53226636e97aSMatthew G Knepley 
53232dd40e9bSPatrick Sanan   Notes:
53242dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
53252dd40e9bSPatrick Sanan 
53262dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
53276636e97aSMatthew G Knepley 
53286636e97aSMatthew G Knepley   Level: intermediate
53296636e97aSMatthew G Knepley 
53306636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
53312dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
53326636e97aSMatthew G Knepley @*/
53336636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
53346636e97aSMatthew G Knepley {
53356636e97aSMatthew G Knepley   PetscErrorCode ierr;
53366636e97aSMatthew G Knepley 
53376636e97aSMatthew G Knepley   PetscFunctionBegin;
53386636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53396636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
53406636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
53416636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
53426636e97aSMatthew G Knepley   dm->coordinates = c;
53436636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5344b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
534503dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
53466636e97aSMatthew G Knepley   PetscFunctionReturn(0);
53476636e97aSMatthew G Knepley }
53486636e97aSMatthew G Knepley 
53496636e97aSMatthew G Knepley /*@
53506636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
53516636e97aSMatthew G Knepley 
53527058e716SVaclav Hapla   Not collective
53536636e97aSMatthew G Knepley 
53546636e97aSMatthew G Knepley    Input Parameters:
53556636e97aSMatthew G Knepley +  dm - the DM
53566636e97aSMatthew G Knepley -  c - coordinate vector
53576636e97aSMatthew G Knepley 
53582dd40e9bSPatrick Sanan   Notes:
53596636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
53606636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
53616636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
53626636e97aSMatthew G Knepley 
53632dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
53642dd40e9bSPatrick Sanan 
53656636e97aSMatthew G Knepley   Level: intermediate
53666636e97aSMatthew G Knepley 
53676636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
53686636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
53696636e97aSMatthew G Knepley @*/
53706636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
53716636e97aSMatthew G Knepley {
53726636e97aSMatthew G Knepley   PetscErrorCode ierr;
53736636e97aSMatthew G Knepley 
53746636e97aSMatthew G Knepley   PetscFunctionBegin;
53756636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53766636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
53776636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
53786636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
53798865f1eaSKarl Rupp 
53806636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
53818865f1eaSKarl Rupp 
53826636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
53836636e97aSMatthew G Knepley   PetscFunctionReturn(0);
53846636e97aSMatthew G Knepley }
53856636e97aSMatthew G Knepley 
53866636e97aSMatthew G Knepley /*@
53876636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
53886636e97aSMatthew G Knepley 
5389c4a54dccSVaclav Hapla   Collective on DM
53906636e97aSMatthew G Knepley 
53916636e97aSMatthew G Knepley   Input Parameter:
53926636e97aSMatthew G Knepley . dm - the DM
53936636e97aSMatthew G Knepley 
53946636e97aSMatthew G Knepley   Output Parameter:
53956636e97aSMatthew G Knepley . c - global coordinate vector
53966636e97aSMatthew G Knepley 
53976636e97aSMatthew G Knepley   Note:
53986636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
53996636e97aSMatthew G Knepley 
54006636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
54016636e97aSMatthew G Knepley 
54026636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
54036636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
54046636e97aSMatthew G Knepley 
54056636e97aSMatthew G Knepley   Level: intermediate
54066636e97aSMatthew G Knepley 
54076636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
54086636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
54096636e97aSMatthew G Knepley @*/
54106636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
54116636e97aSMatthew G Knepley {
54126636e97aSMatthew G Knepley   PetscErrorCode ierr;
54136636e97aSMatthew G Knepley 
54146636e97aSMatthew G Knepley   PetscFunctionBegin;
54156636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54166636e97aSMatthew G Knepley   PetscValidPointer(c,2);
54171f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
54180298fd71SBarry Smith     DM        cdm = NULL;
54191970a576SMatthew G. Knepley     PetscBool localized;
54206636e97aSMatthew G Knepley 
54216636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
54226636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
54231970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
54241970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
54251970a576SMatthew G. Knepley     if (localized) {
54261970a576SMatthew G. Knepley       PetscInt cdim;
54271970a576SMatthew G. Knepley 
54281970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
54291970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
54301970a576SMatthew G. Knepley     }
54316636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
54326636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
54336636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
54346636e97aSMatthew G Knepley   }
54356636e97aSMatthew G Knepley   *c = dm->coordinates;
54366636e97aSMatthew G Knepley   PetscFunctionReturn(0);
54376636e97aSMatthew G Knepley }
54386636e97aSMatthew G Knepley 
54396636e97aSMatthew G Knepley /*@
544081e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
544181e9a530SVaclav Hapla 
544281e9a530SVaclav Hapla   Collective on DM
544381e9a530SVaclav Hapla 
544481e9a530SVaclav Hapla   Input Parameter:
544581e9a530SVaclav Hapla . dm - the DM
544681e9a530SVaclav Hapla 
544781e9a530SVaclav Hapla   Level: advanced
544881e9a530SVaclav Hapla 
544981e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
545081e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
545181e9a530SVaclav Hapla @*/
545281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
545381e9a530SVaclav Hapla {
545481e9a530SVaclav Hapla   PetscErrorCode ierr;
545581e9a530SVaclav Hapla 
545681e9a530SVaclav Hapla   PetscFunctionBegin;
545781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
545881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
54591970a576SMatthew G. Knepley     DM        cdm = NULL;
54601970a576SMatthew G. Knepley     PetscBool localized;
54611970a576SMatthew G. Knepley 
546281e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
546381e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
54641970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
54651970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
54661970a576SMatthew G. Knepley     if (localized) {
54671970a576SMatthew G. Knepley       PetscInt cdim;
54681970a576SMatthew G. Knepley 
54691970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
54701970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
54711970a576SMatthew G. Knepley     }
547281e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
547381e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
547481e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
547581e9a530SVaclav Hapla   }
547681e9a530SVaclav Hapla   PetscFunctionReturn(0);
547781e9a530SVaclav Hapla }
547881e9a530SVaclav Hapla 
547981e9a530SVaclav Hapla /*@
54806636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
54816636e97aSMatthew G Knepley 
54826636e97aSMatthew G Knepley   Collective on DM
54836636e97aSMatthew G Knepley 
54846636e97aSMatthew G Knepley   Input Parameter:
54856636e97aSMatthew G Knepley . dm - the DM
54866636e97aSMatthew G Knepley 
54876636e97aSMatthew G Knepley   Output Parameter:
54886636e97aSMatthew G Knepley . c - coordinate vector
54896636e97aSMatthew G Knepley 
54906636e97aSMatthew G Knepley   Note:
54916636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
54926636e97aSMatthew G Knepley 
54936636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
54946636e97aSMatthew G Knepley 
54956636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
54966636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
54976636e97aSMatthew G Knepley 
54986636e97aSMatthew G Knepley   Level: intermediate
54996636e97aSMatthew G Knepley 
55006636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
550181e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
55026636e97aSMatthew G Knepley @*/
55036636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
55046636e97aSMatthew G Knepley {
55056636e97aSMatthew G Knepley   PetscErrorCode ierr;
55066636e97aSMatthew G Knepley 
55076636e97aSMatthew G Knepley   PetscFunctionBegin;
55086636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55096636e97aSMatthew G Knepley   PetscValidPointer(c,2);
551081e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
55116636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
55126636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55136636e97aSMatthew G Knepley }
55146636e97aSMatthew G Knepley 
551581e9a530SVaclav Hapla /*@
551681e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
551781e9a530SVaclav Hapla 
551881e9a530SVaclav Hapla   Not collective
551981e9a530SVaclav Hapla 
552081e9a530SVaclav Hapla   Input Parameter:
552181e9a530SVaclav Hapla . dm - the DM
552281e9a530SVaclav Hapla 
552381e9a530SVaclav Hapla   Output Parameter:
552481e9a530SVaclav Hapla . c - coordinate vector
552581e9a530SVaclav Hapla 
552681e9a530SVaclav Hapla   Level: advanced
552781e9a530SVaclav Hapla 
552881e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
552981e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
553081e9a530SVaclav Hapla @*/
553181e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
553281e9a530SVaclav Hapla {
553381e9a530SVaclav Hapla   PetscFunctionBegin;
553481e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
553581e9a530SVaclav Hapla   PetscValidPointer(c,2);
553681e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
55376636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
55386636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55396636e97aSMatthew G Knepley }
55406636e97aSMatthew G Knepley 
55412db98f8dSVaclav Hapla /*@
55422db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
55432db98f8dSVaclav Hapla 
55442db98f8dSVaclav Hapla   Not collective
55452db98f8dSVaclav Hapla 
55462db98f8dSVaclav Hapla   Input Parameter:
55472db98f8dSVaclav Hapla + dm - the DM
55482db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
55492db98f8dSVaclav Hapla 
55502db98f8dSVaclav Hapla   Output Parameter:
55512db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
55522db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
55532db98f8dSVaclav Hapla 
55542db98f8dSVaclav Hapla   Note:
55552db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
55562db98f8dSVaclav Hapla 
55572db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
55582db98f8dSVaclav Hapla 
55592db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
55602db98f8dSVaclav Hapla 
55612db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
55622db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
55632db98f8dSVaclav Hapla 
55642db98f8dSVaclav Hapla   Level: advanced
55652db98f8dSVaclav Hapla 
55662db98f8dSVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
55672db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
55682db98f8dSVaclav Hapla @*/
55692db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
55702db98f8dSVaclav Hapla {
55712db98f8dSVaclav Hapla   PetscSection        cs, newcs;
55722db98f8dSVaclav Hapla   Vec                 coords;
55732db98f8dSVaclav Hapla   const PetscScalar   *arr;
55742db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
55752db98f8dSVaclav Hapla   PetscInt            n;
55762db98f8dSVaclav Hapla   PetscErrorCode      ierr;
55772db98f8dSVaclav Hapla 
55782db98f8dSVaclav Hapla   PetscFunctionBegin;
55792db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55802db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
55812db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
55822db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
55832db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
55842db98f8dSVaclav Hapla   if (!dm->coordinateDM || !dm->coordinateDM->defaultSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
55852db98f8dSVaclav Hapla   cs = dm->coordinateDM->defaultSection;
55862db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
55872db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
55882db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
55892db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
55902db98f8dSVaclav Hapla   if (pCoord) {
55912db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
55922db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
55932db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
55942db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5595ad9ac99dSVaclav Hapla   } else {
5596ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
55972db98f8dSVaclav Hapla   }
5598ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5599ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
56002db98f8dSVaclav Hapla   PetscFunctionReturn(0);
56012db98f8dSVaclav Hapla }
56022db98f8dSVaclav Hapla 
5603f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5604f19dbd58SToby Isaac {
5605f19dbd58SToby Isaac   PetscErrorCode ierr;
5606f19dbd58SToby Isaac 
5607f19dbd58SToby Isaac   PetscFunctionBegin;
5608f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5609f19dbd58SToby Isaac   PetscValidPointer(field,2);
5610f19dbd58SToby Isaac   if (!dm->coordinateField) {
5611f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5612f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5613f19dbd58SToby Isaac     }
5614f19dbd58SToby Isaac   }
5615f19dbd58SToby Isaac   *field = dm->coordinateField;
5616f19dbd58SToby Isaac   PetscFunctionReturn(0);
5617f19dbd58SToby Isaac }
5618f19dbd58SToby Isaac 
5619f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5620f19dbd58SToby Isaac {
5621f19dbd58SToby Isaac   PetscErrorCode ierr;
5622f19dbd58SToby Isaac 
5623f19dbd58SToby Isaac   PetscFunctionBegin;
5624f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5625f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5626c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5627f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5628f19dbd58SToby Isaac   dm->coordinateField = field;
5629f19dbd58SToby Isaac   PetscFunctionReturn(0);
5630f19dbd58SToby Isaac }
5631f19dbd58SToby Isaac 
56326636e97aSMatthew G Knepley /*@
56331cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
56346636e97aSMatthew G Knepley 
56356636e97aSMatthew G Knepley   Collective on DM
56366636e97aSMatthew G Knepley 
56376636e97aSMatthew G Knepley   Input Parameter:
56386636e97aSMatthew G Knepley . dm - the DM
56396636e97aSMatthew G Knepley 
56406636e97aSMatthew G Knepley   Output Parameter:
56416636e97aSMatthew G Knepley . cdm - coordinate DM
56426636e97aSMatthew G Knepley 
56436636e97aSMatthew G Knepley   Level: intermediate
56446636e97aSMatthew G Knepley 
56456636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
56461cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
56476636e97aSMatthew G Knepley @*/
56486636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
56496636e97aSMatthew G Knepley {
56506636e97aSMatthew G Knepley   PetscErrorCode ierr;
56516636e97aSMatthew G Knepley 
56526636e97aSMatthew G Knepley   PetscFunctionBegin;
56536636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56546636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
56556636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5656308f8a94SToby Isaac     DM cdm;
5657308f8a94SToby Isaac 
565882f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5659308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5660308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5661308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5662308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5663308f8a94SToby Isaac     dm->coordinateDM = cdm;
56646636e97aSMatthew G Knepley   }
56656636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
56666636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56676636e97aSMatthew G Knepley }
5668e87bb0d3SMatthew G Knepley 
56691cfe2091SMatthew G. Knepley /*@
56701cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
56711cfe2091SMatthew G. Knepley 
56721cfe2091SMatthew G. Knepley   Logically Collective on DM
56731cfe2091SMatthew G. Knepley 
56741cfe2091SMatthew G. Knepley   Input Parameters:
56751cfe2091SMatthew G. Knepley + dm - the DM
56761cfe2091SMatthew G. Knepley - cdm - coordinate DM
56771cfe2091SMatthew G. Knepley 
56781cfe2091SMatthew G. Knepley   Level: intermediate
56791cfe2091SMatthew G. Knepley 
56801cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
56811cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
56821cfe2091SMatthew G. Knepley @*/
56831cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
56841cfe2091SMatthew G. Knepley {
56851cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
56861cfe2091SMatthew G. Knepley 
56871cfe2091SMatthew G. Knepley   PetscFunctionBegin;
56881cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56891cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5690f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
56911cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
56921cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
56931cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
56941cfe2091SMatthew G. Knepley }
56951cfe2091SMatthew G. Knepley 
569646e270d4SMatthew G. Knepley /*@
569746e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
569846e270d4SMatthew G. Knepley 
569946e270d4SMatthew G. Knepley   Not Collective
570046e270d4SMatthew G. Knepley 
570146e270d4SMatthew G. Knepley   Input Parameter:
570246e270d4SMatthew G. Knepley . dm - The DM object
570346e270d4SMatthew G. Knepley 
570446e270d4SMatthew G. Knepley   Output Parameter:
570546e270d4SMatthew G. Knepley . dim - The embedding dimension
570646e270d4SMatthew G. Knepley 
570746e270d4SMatthew G. Knepley   Level: intermediate
570846e270d4SMatthew G. Knepley 
570946e270d4SMatthew G. Knepley .keywords: mesh, coordinates
5710e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
571146e270d4SMatthew G. Knepley @*/
571246e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
571346e270d4SMatthew G. Knepley {
571446e270d4SMatthew G. Knepley   PetscFunctionBegin;
571546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
571646e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
57179a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
57189a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
57199a9a41abSToby Isaac   }
572046e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
572146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
572246e270d4SMatthew G. Knepley }
572346e270d4SMatthew G. Knepley 
572446e270d4SMatthew G. Knepley /*@
572546e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
572646e270d4SMatthew G. Knepley 
572746e270d4SMatthew G. Knepley   Not Collective
572846e270d4SMatthew G. Knepley 
572946e270d4SMatthew G. Knepley   Input Parameters:
573046e270d4SMatthew G. Knepley + dm  - The DM object
573146e270d4SMatthew G. Knepley - dim - The embedding dimension
573246e270d4SMatthew G. Knepley 
573346e270d4SMatthew G. Knepley   Level: intermediate
573446e270d4SMatthew G. Knepley 
573546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
5736e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
573746e270d4SMatthew G. Knepley @*/
573846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
573946e270d4SMatthew G. Knepley {
5740e5e52638SMatthew G. Knepley   PetscDS        ds;
5741f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5742f17e8794SMatthew G. Knepley 
574346e270d4SMatthew G. Knepley   PetscFunctionBegin;
574446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
574546e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5746e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5747e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
574846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
574946e270d4SMatthew G. Knepley }
575046e270d4SMatthew G. Knepley 
5751e8abe2deSMatthew G. Knepley /*@
5752e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5753e8abe2deSMatthew G. Knepley 
5754116501dfSVaclav Hapla   Collective on DM
5755e8abe2deSMatthew G. Knepley 
5756e8abe2deSMatthew G. Knepley   Input Parameter:
5757e8abe2deSMatthew G. Knepley . dm - The DM object
5758e8abe2deSMatthew G. Knepley 
5759e8abe2deSMatthew G. Knepley   Output Parameter:
5760e8abe2deSMatthew G. Knepley . section - The PetscSection object
5761e8abe2deSMatthew G. Knepley 
5762e8abe2deSMatthew G. Knepley   Level: intermediate
5763e8abe2deSMatthew G. Knepley 
5764e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
5765e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
5766e8abe2deSMatthew G. Knepley @*/
5767e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5768e8abe2deSMatthew G. Knepley {
5769e8abe2deSMatthew G. Knepley   DM             cdm;
5770e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5771e8abe2deSMatthew G. Knepley 
5772e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5773e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5774e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5775e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5776e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
5777e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5778e8abe2deSMatthew G. Knepley }
5779e8abe2deSMatthew G. Knepley 
5780e8abe2deSMatthew G. Knepley /*@
5781e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5782e8abe2deSMatthew G. Knepley 
5783e8abe2deSMatthew G. Knepley   Not Collective
5784e8abe2deSMatthew G. Knepley 
5785e8abe2deSMatthew G. Knepley   Input Parameters:
5786e8abe2deSMatthew G. Knepley + dm      - The DM object
578746e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5788e8abe2deSMatthew G. Knepley - section - The PetscSection object
5789e8abe2deSMatthew G. Knepley 
5790e8abe2deSMatthew G. Knepley   Level: intermediate
5791e8abe2deSMatthew G. Knepley 
5792e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
5793e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
5794e8abe2deSMatthew G. Knepley @*/
579546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5796e8abe2deSMatthew G. Knepley {
5797e8abe2deSMatthew G. Knepley   DM             cdm;
5798e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5799e8abe2deSMatthew G. Knepley 
5800e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5801e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
580246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5803e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5804e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
580546e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
58064c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
580746e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
580846e270d4SMatthew G. Knepley 
580946e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
581046e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
581146e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
581246e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
581346e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
581446e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
581546e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
581646e270d4SMatthew G. Knepley     }
5817ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
581846e270d4SMatthew G. Knepley   }
5819e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5820e8abe2deSMatthew G. Knepley }
5821e8abe2deSMatthew G. Knepley 
58225dc8c3f7SMatthew G. Knepley /*@C
582390b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
58245dc8c3f7SMatthew G. Knepley 
58255dc8c3f7SMatthew G. Knepley   Input Parameters:
582690b157c4SStefano Zampini . dm      - The DM object
582790b157c4SStefano Zampini 
582890b157c4SStefano Zampini   Output Parameters:
582990b157c4SStefano Zampini + per     - Whether the DM is periodic or not
58305dc8c3f7SMatthew 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
58315dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
58325dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
58335dc8c3f7SMatthew G. Knepley 
58345dc8c3f7SMatthew G. Knepley   Level: developer
58355dc8c3f7SMatthew G. Knepley 
58365dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
58375dc8c3f7SMatthew G. Knepley @*/
583890b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5839c6b900c6SMatthew G. Knepley {
5840c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5841c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
584290b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5843c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5844c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
58455dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5846c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5847c6b900c6SMatthew G. Knepley }
5848c6b900c6SMatthew G. Knepley 
58495dc8c3f7SMatthew G. Knepley /*@C
58505dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
58515dc8c3f7SMatthew G. Knepley 
58525dc8c3f7SMatthew G. Knepley   Input Parameters:
58535dc8c3f7SMatthew G. Knepley + dm      - The DM object
585490b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
58555dc8c3f7SMatthew 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
58565dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
58575dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
58585dc8c3f7SMatthew G. Knepley 
58595dc8c3f7SMatthew G. Knepley   Level: developer
58605dc8c3f7SMatthew G. Knepley 
58615dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
58625dc8c3f7SMatthew G. Knepley @*/
586390b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5864c6b900c6SMatthew G. Knepley {
5865c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
5866c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
5867c6b900c6SMatthew G. Knepley 
5868c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5869c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
587090b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
587190b157c4SStefano Zampini   if (maxCell) {
587290b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
587390b157c4SStefano Zampini     PetscValidPointer(L,4);
587490b157c4SStefano Zampini     PetscValidPointer(bd,5);
587590b157c4SStefano Zampini   }
58765dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
58775dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
587890b157c4SStefano Zampini   if (maxCell) {
58795dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
58805dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
588190b157c4SStefano Zampini   }
5882072d7d67SStefano Zampini   dm->periodic = per;
5883c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5884c6b900c6SMatthew G. Knepley }
5885c6b900c6SMatthew G. Knepley 
58862e17dfb7SMatthew G. Knepley /*@
58872e17dfb7SMatthew 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.
58882e17dfb7SMatthew G. Knepley 
58892e17dfb7SMatthew G. Knepley   Input Parameters:
58902e17dfb7SMatthew G. Knepley + dm     - The DM
589165da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
589265da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
58932e17dfb7SMatthew G. Knepley 
58942e17dfb7SMatthew G. Knepley   Output Parameter:
58952e17dfb7SMatthew G. Knepley . out - The localized coordinate point
58962e17dfb7SMatthew G. Knepley 
58972e17dfb7SMatthew G. Knepley   Level: developer
58982e17dfb7SMatthew G. Knepley 
58992e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
59002e17dfb7SMatthew G. Knepley @*/
590165da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
59022e17dfb7SMatthew G. Knepley {
59032e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
59042e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
59052e17dfb7SMatthew G. Knepley 
59062e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
59072e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
59082e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
59092e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
59102e17dfb7SMatthew G. Knepley   } else {
591165da65dcSMatthew G. Knepley     if (endpoint) {
591265da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
5913da3333bfSMatthew 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)) {
5914da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
591565da65dcSMatthew G. Knepley         } else {
5916da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
591765da65dcSMatthew G. Knepley         }
591865da65dcSMatthew G. Knepley       }
591965da65dcSMatthew G. Knepley     } else {
59202e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
59211118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
59222e17dfb7SMatthew G. Knepley       }
59232e17dfb7SMatthew G. Knepley     }
592465da65dcSMatthew G. Knepley   }
59252e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
59262e17dfb7SMatthew G. Knepley }
59272e17dfb7SMatthew G. Knepley 
59282e17dfb7SMatthew G. Knepley /*
59292e17dfb7SMatthew 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.
59302e17dfb7SMatthew G. Knepley 
59312e17dfb7SMatthew G. Knepley   Input Parameters:
59322e17dfb7SMatthew G. Knepley + dm     - The DM
59332e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
59342e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
59352e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
59362e17dfb7SMatthew G. Knepley 
59372e17dfb7SMatthew G. Knepley   Output Parameter:
59382e17dfb7SMatthew G. Knepley . out - The localized coordinate point
59392e17dfb7SMatthew G. Knepley 
59402e17dfb7SMatthew G. Knepley   Level: developer
59412e17dfb7SMatthew G. Knepley 
59422e17dfb7SMatthew 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
59432e17dfb7SMatthew G. Knepley 
59442e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
59452e17dfb7SMatthew G. Knepley */
59462e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar 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) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
59562e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(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 PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
59652e17dfb7SMatthew G. Knepley {
59662e17dfb7SMatthew G. Knepley   PetscInt d;
59672e17dfb7SMatthew G. Knepley 
59682e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
59692e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
59702e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
59712e17dfb7SMatthew G. Knepley   } else {
59722e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5973908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
59742e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
59752e17dfb7SMatthew G. Knepley       } else {
59762e17dfb7SMatthew G. Knepley         out[d] = in[d];
59772e17dfb7SMatthew G. Knepley       }
59782e17dfb7SMatthew G. Knepley     }
59792e17dfb7SMatthew G. Knepley   }
59802e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
59812e17dfb7SMatthew G. Knepley }
59822e17dfb7SMatthew G. Knepley 
59832e17dfb7SMatthew G. Knepley /*
59842e17dfb7SMatthew 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.
59852e17dfb7SMatthew G. Knepley 
59862e17dfb7SMatthew G. Knepley   Input Parameters:
59872e17dfb7SMatthew G. Knepley + dm     - The DM
59882e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
59892e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
59902e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
59912e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
59922e17dfb7SMatthew G. Knepley 
59932e17dfb7SMatthew G. Knepley   Output Parameter:
59942e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
59952e17dfb7SMatthew G. Knepley 
59962e17dfb7SMatthew G. Knepley   Level: developer
59972e17dfb7SMatthew G. Knepley 
59982e17dfb7SMatthew 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
59992e17dfb7SMatthew G. Knepley 
60002e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
60012e17dfb7SMatthew G. Knepley */
60022e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
60032e17dfb7SMatthew G. Knepley {
60042e17dfb7SMatthew G. Knepley   PetscInt d;
60052e17dfb7SMatthew G. Knepley 
60062e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60072e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60082e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
60092e17dfb7SMatthew G. Knepley   } else {
60102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6011908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
60122e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
60132e17dfb7SMatthew G. Knepley       } else {
60142e17dfb7SMatthew G. Knepley         out[d] += in[d];
60152e17dfb7SMatthew G. Knepley       }
60162e17dfb7SMatthew G. Knepley     }
60172e17dfb7SMatthew G. Knepley   }
60182e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60192e17dfb7SMatthew G. Knepley }
60202e17dfb7SMatthew G. Knepley 
602136447a5eSToby Isaac /*@
60228f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
60238f700142SStefano Zampini 
60248f700142SStefano Zampini   Not collective
602536447a5eSToby Isaac 
602636447a5eSToby Isaac   Input Parameter:
602736447a5eSToby Isaac . dm - The DM
602836447a5eSToby Isaac 
602936447a5eSToby Isaac   Output Parameter:
603036447a5eSToby Isaac   areLocalized - True if localized
603136447a5eSToby Isaac 
603236447a5eSToby Isaac   Level: developer
603336447a5eSToby Isaac 
60348f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
603536447a5eSToby Isaac @*/
60368f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
603736447a5eSToby Isaac {
603836447a5eSToby Isaac   DM             cdm;
603936447a5eSToby Isaac   PetscSection   coordSection;
604046a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
604146a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
604236447a5eSToby Isaac   PetscErrorCode ierr;
604336447a5eSToby Isaac 
604436447a5eSToby Isaac   PetscFunctionBegin;
604536447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
604646a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
60478b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
604846a3a80fSLisandro Dalcin 
604936447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
605036447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
605146a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
60529f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
605346a3a80fSLisandro Dalcin 
60549f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
605546a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
605636447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
605736447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
605846a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
605946a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
606036447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
606146a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
606236447a5eSToby Isaac   }
60638f700142SStefano Zampini   *areLocalized = alreadyLocalized;
606436447a5eSToby Isaac   PetscFunctionReturn(0);
606536447a5eSToby Isaac }
606636447a5eSToby Isaac 
60678f700142SStefano Zampini /*@
60688f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
60698f700142SStefano Zampini 
60708f700142SStefano Zampini   Collective on dm
60718f700142SStefano Zampini 
60728f700142SStefano Zampini   Input Parameter:
60738f700142SStefano Zampini . dm - The DM
60748f700142SStefano Zampini 
60758f700142SStefano Zampini   Output Parameter:
60768f700142SStefano Zampini   areLocalized - True if localized
60778f700142SStefano Zampini 
60788f700142SStefano Zampini   Level: developer
60798f700142SStefano Zampini 
60808f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
60818f700142SStefano Zampini @*/
60828f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
60838f700142SStefano Zampini {
60848f700142SStefano Zampini   PetscBool      localized;
60858f700142SStefano Zampini   PetscErrorCode ierr;
60868f700142SStefano Zampini 
60878f700142SStefano Zampini   PetscFunctionBegin;
60888f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60898f700142SStefano Zampini   PetscValidPointer(areLocalized, 2);
60908f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
60918f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
60928f700142SStefano Zampini   PetscFunctionReturn(0);
60938f700142SStefano Zampini }
609436447a5eSToby Isaac 
60952e17dfb7SMatthew G. Knepley /*@
6096492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
60972e17dfb7SMatthew G. Knepley 
60988f700142SStefano Zampini   Collective on dm
60998f700142SStefano Zampini 
61002e17dfb7SMatthew G. Knepley   Input Parameter:
61012e17dfb7SMatthew G. Knepley . dm - The DM
61022e17dfb7SMatthew G. Knepley 
61032e17dfb7SMatthew G. Knepley   Level: developer
61042e17dfb7SMatthew G. Knepley 
61058f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
61062e17dfb7SMatthew G. Knepley @*/
61072e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
61082e17dfb7SMatthew G. Knepley {
61092e17dfb7SMatthew G. Knepley   DM             cdm;
61102e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
61112e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
61123e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
61133e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6114e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
61153e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
61163e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
61172e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
61182e17dfb7SMatthew G. Knepley 
61192e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61202e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
612192c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6122f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6123f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6124f7cbd40bSStefano Zampini 
61252e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
61262e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
61272e17dfb7SMatthew G. Knepley   {
61282e17dfb7SMatthew G. Knepley     PetscBool isplex;
61292e17dfb7SMatthew G. Knepley 
61302e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
61312e17dfb7SMatthew G. Knepley     if (isplex) {
61322e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
61333e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
613469291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
61353e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
61363e922f36SToby Isaac       newStart = vStart;
61373e922f36SToby Isaac       newEnd   = vEnd;
61383e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
61393e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
61403e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
61413e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
61423e922f36SToby Isaac       }
61432e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
61442e17dfb7SMatthew G. Knepley   }
61452e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
614643eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
61472e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
61483e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6149e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
61503e922f36SToby Isaac 
61512e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
61522e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
61532e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
61542e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
61553e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
61563e922f36SToby Isaac 
615769291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
61583e922f36SToby Isaac   localized = &anchor[bs];
61593e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
61603e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
61613e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
61623e922f36SToby Isaac 
61633e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
61643e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
61653e922f36SToby Isaac       PetscInt     b;
61663e922f36SToby Isaac 
61673e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
61683e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
61693e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
61703e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
61713e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
61723e922f36SToby Isaac         for (b = 0; b < bs; b++) {
61733e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
61743e922f36SToby Isaac         }
61753e922f36SToby Isaac         if (b < bs) break;
61763e922f36SToby Isaac       }
61773e922f36SToby Isaac       if (d < dof/bs) {
61783e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
61793e922f36SToby Isaac           PetscInt cdof;
61803e922f36SToby Isaac 
61813e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
61823e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
61833e922f36SToby Isaac         }
61843e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
61853e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
61863e922f36SToby Isaac       }
61873e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
61883e922f36SToby Isaac     }
61893e922f36SToby Isaac   }
61903e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
61913e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
619269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
61933e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
619469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
61953e922f36SToby Isaac     PetscFunctionReturn(0);
61963e922f36SToby Isaac   }
61972e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
61982e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
61992e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
62002e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
62012e17dfb7SMatthew G. Knepley   }
62022e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
62032e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6204c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
62052e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
62062e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
62072e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
62082e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6209c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
62102e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
62112e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
62122e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
62132e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
62142e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
62152e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
62162e17dfb7SMatthew G. Knepley   }
62173e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
62183e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
62193e922f36SToby Isaac 
62202e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
62212e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
62223e922f36SToby Isaac       PetscInt     b, cdof;
62232e17dfb7SMatthew G. Knepley 
62243e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
62253e922f36SToby Isaac       if (!cdof) continue;
62262e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62272e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
62282e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
62292e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
62302e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62312e17dfb7SMatthew G. Knepley     }
62323e922f36SToby Isaac   }
623369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
623469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6235c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
62362e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
62372e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
62382e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
62392e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
62402e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
62412e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
62422e17dfb7SMatthew G. Knepley }
62432e17dfb7SMatthew G. Knepley 
6244e87bb0d3SMatthew G Knepley /*@
62453a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6246e87bb0d3SMatthew G Knepley 
62473a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
6248e87bb0d3SMatthew G Knepley 
6249e87bb0d3SMatthew G Knepley   Input Parameters:
6250e87bb0d3SMatthew G Knepley + dm - The DM
62513a93e3b7SToby Isaac . v - The Vec of points
625262a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
62533a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6254e87bb0d3SMatthew G Knepley 
625561e3bb9bSMatthew G Knepley   Output Parameter:
625662a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
625762a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
62583a93e3b7SToby Isaac 
6259e87bb0d3SMatthew G Knepley 
6260e87bb0d3SMatthew G Knepley   Level: developer
626161e3bb9bSMatthew G Knepley 
626262a38674SMatthew G. Knepley   Notes:
62633a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
626462a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
62653a93e3b7SToby Isaac 
62663a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
626762a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
62683a93e3b7SToby Isaac 
62693a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
62703a93e3b7SToby Isaac 
627162a38674SMatthew G. Knepley $    const PetscSFNode *cells;
627262a38674SMatthew G. Knepley $    PetscInt           nFound;
6273a6216909SToby Isaac $    const PetscInt    *found;
627462a38674SMatthew G. Knepley $
6275a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
62763a93e3b7SToby Isaac 
62773a93e3b7SToby 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
62783a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
62793a93e3b7SToby Isaac 
628061e3bb9bSMatthew G Knepley .keywords: point location, mesh
628162a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
628261e3bb9bSMatthew G Knepley @*/
628362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6284e87bb0d3SMatthew G Knepley {
6285735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6286735aa83eSMatthew G Knepley 
6287e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6288e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6289e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6290e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
62913a93e3b7SToby Isaac   if (*cellSF) {
62923a93e3b7SToby Isaac     PetscMPIInt result;
62933a93e3b7SToby Isaac 
6294e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6295a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
62963a93e3b7SToby 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");
6297e0fc9d1bSMatthew G. Knepley   } else {
62983a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
62993a93e3b7SToby Isaac   }
630047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6301735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
630262a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
630382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
630447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6305e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6306e87bb0d3SMatthew G Knepley }
630714f150ffSMatthew G. Knepley 
6308f4d763aaSMatthew G. Knepley /*@
6309f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6310f4d763aaSMatthew G. Knepley 
63118f700142SStefano Zampini   Collective on dm
63128f700142SStefano Zampini 
6313f4d763aaSMatthew G. Knepley   Input Parameter:
6314f4d763aaSMatthew G. Knepley . dm - The original DM
6315f4d763aaSMatthew G. Knepley 
6316f4d763aaSMatthew G. Knepley   Output Parameter:
6317f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6318f4d763aaSMatthew G. Knepley 
6319f4d763aaSMatthew G. Knepley   Level: intermediate
6320f4d763aaSMatthew G. Knepley 
6321e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6322f4d763aaSMatthew G. Knepley @*/
632314f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
632414f150ffSMatthew G. Knepley {
6325c26acbdeSMatthew G. Knepley   PetscSection   section;
63262d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
632714f150ffSMatthew G. Knepley   PetscErrorCode ierr;
632814f150ffSMatthew G. Knepley 
632914f150ffSMatthew G. Knepley   PetscFunctionBegin;
633014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
633114f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
6332e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
6333c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6334127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
63352d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6336c26acbdeSMatthew G. Knepley     *odm = dm;
6337c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6338c26acbdeSMatthew G. Knepley   }
633914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6340c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
634114f150ffSMatthew G. Knepley     PetscSF      sf;
634214f150ffSMatthew G. Knepley 
634314f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6344e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
634514f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
6346e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
634714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
634814f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
634915b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6350e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
635114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
635214f150ffSMatthew G. Knepley   }
635314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
635414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
635514f150ffSMatthew G. Knepley }
6356f4d763aaSMatthew G. Knepley 
6357f4d763aaSMatthew G. Knepley /*@
6358cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6359f4d763aaSMatthew G. Knepley 
6360f4d763aaSMatthew G. Knepley   Input Parameter:
6361f4d763aaSMatthew G. Knepley . dm - The original DM
6362f4d763aaSMatthew G. Knepley 
6363cdb7a50dSMatthew G. Knepley   Output Parameters:
6364cdb7a50dSMatthew G. Knepley + num - The output sequence number
6365cdb7a50dSMatthew G. Knepley - val - The output sequence value
6366f4d763aaSMatthew G. Knepley 
6367f4d763aaSMatthew G. Knepley   Level: intermediate
6368f4d763aaSMatthew G. Knepley 
6369f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6370f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6371f4d763aaSMatthew G. Knepley 
6372f4d763aaSMatthew G. Knepley .seealso: VecView()
6373f4d763aaSMatthew G. Knepley @*/
6374cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6375f4d763aaSMatthew G. Knepley {
6376f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6377f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6378cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
6379cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
6380f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6381f4d763aaSMatthew G. Knepley }
6382f4d763aaSMatthew G. Knepley 
6383f4d763aaSMatthew G. Knepley /*@
6384cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6385f4d763aaSMatthew G. Knepley 
6386f4d763aaSMatthew G. Knepley   Input Parameters:
6387f4d763aaSMatthew G. Knepley + dm - The original DM
6388cdb7a50dSMatthew G. Knepley . num - The output sequence number
6389cdb7a50dSMatthew G. Knepley - val - The output sequence value
6390f4d763aaSMatthew G. Knepley 
6391f4d763aaSMatthew G. Knepley   Level: intermediate
6392f4d763aaSMatthew G. Knepley 
6393f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6394f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6395f4d763aaSMatthew G. Knepley 
6396f4d763aaSMatthew G. Knepley .seealso: VecView()
6397f4d763aaSMatthew G. Knepley @*/
6398cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6399f4d763aaSMatthew G. Knepley {
6400f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6401f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6402f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6403cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6404cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6405cdb7a50dSMatthew G. Knepley }
6406cdb7a50dSMatthew G. Knepley 
6407cdb7a50dSMatthew G. Knepley /*@C
6408cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6409cdb7a50dSMatthew G. Knepley 
6410cdb7a50dSMatthew G. Knepley   Input Parameters:
6411cdb7a50dSMatthew G. Knepley + dm   - The original DM
6412cdb7a50dSMatthew G. Knepley . name - The sequence name
6413cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6414cdb7a50dSMatthew G. Knepley 
6415cdb7a50dSMatthew G. Knepley   Output Parameter:
6416cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6417cdb7a50dSMatthew G. Knepley 
6418cdb7a50dSMatthew G. Knepley   Level: intermediate
6419cdb7a50dSMatthew G. Knepley 
6420cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6421cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6422cdb7a50dSMatthew G. Knepley 
6423cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6424cdb7a50dSMatthew G. Knepley @*/
6425cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6426cdb7a50dSMatthew G. Knepley {
6427cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6428cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6429cdb7a50dSMatthew G. Knepley 
6430cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6431cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6432cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6433cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
6434cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6435cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6436cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6437cdb7a50dSMatthew G. Knepley     PetscScalar value;
6438cdb7a50dSMatthew G. Knepley 
643939d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
64404aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6441cdb7a50dSMatthew G. Knepley #endif
6442cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6443f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6444f4d763aaSMatthew G. Knepley }
64458e4ac7eaSMatthew G. Knepley 
64468e4ac7eaSMatthew G. Knepley /*@
64478e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
64488e4ac7eaSMatthew G. Knepley 
64498e4ac7eaSMatthew G. Knepley   Not collective
64508e4ac7eaSMatthew G. Knepley 
64518e4ac7eaSMatthew G. Knepley   Input Parameter:
64528e4ac7eaSMatthew G. Knepley . dm - The DM
64538e4ac7eaSMatthew G. Knepley 
64548e4ac7eaSMatthew G. Knepley   Output Parameter:
64558e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
64568e4ac7eaSMatthew G. Knepley 
64578e4ac7eaSMatthew G. Knepley   Level: beginner
64588e4ac7eaSMatthew G. Knepley 
64598e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
64608e4ac7eaSMatthew G. Knepley @*/
64618e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
64628e4ac7eaSMatthew G. Knepley {
64638e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64648e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64658e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
64668e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64678e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64688e4ac7eaSMatthew G. Knepley }
64698e4ac7eaSMatthew G. Knepley 
64708e4ac7eaSMatthew G. Knepley /*@
64715d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
64728e4ac7eaSMatthew G. Knepley 
64738e4ac7eaSMatthew G. Knepley   Collective on dm
64748e4ac7eaSMatthew G. Knepley 
64758e4ac7eaSMatthew G. Knepley   Input Parameters:
64768e4ac7eaSMatthew G. Knepley + dm - The DM
64778e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
64788e4ac7eaSMatthew G. Knepley 
64795d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
64805d3b26e6SMatthew G. Knepley 
64818e4ac7eaSMatthew G. Knepley   Level: beginner
64828e4ac7eaSMatthew G. Knepley 
64835d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
64848e4ac7eaSMatthew G. Knepley @*/
64858e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
64868e4ac7eaSMatthew G. Knepley {
64878e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64888e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64898833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
64908e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
64918e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64928e4ac7eaSMatthew G. Knepley }
6493c58f1c22SToby Isaac 
6494c58f1c22SToby Isaac 
6495c58f1c22SToby Isaac /*@C
6496c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6497c58f1c22SToby Isaac 
6498c58f1c22SToby Isaac   Not Collective
6499c58f1c22SToby Isaac 
6500c58f1c22SToby Isaac   Input Parameters:
6501c58f1c22SToby Isaac + dm   - The DM object
6502c58f1c22SToby Isaac - name - The label name
6503c58f1c22SToby Isaac 
6504c58f1c22SToby Isaac   Level: intermediate
6505c58f1c22SToby Isaac 
6506c58f1c22SToby Isaac .keywords: mesh
6507c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6508c58f1c22SToby Isaac @*/
6509c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6510c58f1c22SToby Isaac {
6511c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
6512c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
6513d67d17b1SMatthew G. Knepley   const char    *lname;
6514c58f1c22SToby Isaac   PetscErrorCode ierr;
6515c58f1c22SToby Isaac 
6516c58f1c22SToby Isaac   PetscFunctionBegin;
6517c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6518c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6519c58f1c22SToby Isaac   while (next) {
6520d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6521d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6522c58f1c22SToby Isaac     if (flg) break;
6523c58f1c22SToby Isaac     next = next->next;
6524c58f1c22SToby Isaac   }
6525c58f1c22SToby Isaac   if (!flg) {
6526c58f1c22SToby Isaac     DMLabelLink tmpLabel;
6527c58f1c22SToby Isaac 
6528c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6529d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
6530c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
6531c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
6532c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
6533c58f1c22SToby Isaac   }
6534c58f1c22SToby Isaac   PetscFunctionReturn(0);
6535c58f1c22SToby Isaac }
6536c58f1c22SToby Isaac 
6537c58f1c22SToby Isaac /*@C
6538c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6539c58f1c22SToby Isaac 
6540c58f1c22SToby Isaac   Not Collective
6541c58f1c22SToby Isaac 
6542c58f1c22SToby Isaac   Input Parameters:
6543c58f1c22SToby Isaac + dm   - The DM object
6544c58f1c22SToby Isaac . name - The label name
6545c58f1c22SToby Isaac - point - The mesh point
6546c58f1c22SToby Isaac 
6547c58f1c22SToby Isaac   Output Parameter:
6548c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6549c58f1c22SToby Isaac 
6550c58f1c22SToby Isaac   Level: beginner
6551c58f1c22SToby Isaac 
6552c58f1c22SToby Isaac .keywords: mesh
6553c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6554c58f1c22SToby Isaac @*/
6555c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6556c58f1c22SToby Isaac {
6557c58f1c22SToby Isaac   DMLabel        label;
6558c58f1c22SToby Isaac   PetscErrorCode ierr;
6559c58f1c22SToby Isaac 
6560c58f1c22SToby Isaac   PetscFunctionBegin;
6561c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6562c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6563c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
656413903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6565c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6566c58f1c22SToby Isaac   PetscFunctionReturn(0);
6567c58f1c22SToby Isaac }
6568c58f1c22SToby Isaac 
6569c58f1c22SToby Isaac /*@C
6570c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6571c58f1c22SToby Isaac 
6572c58f1c22SToby Isaac   Not Collective
6573c58f1c22SToby Isaac 
6574c58f1c22SToby Isaac   Input Parameters:
6575c58f1c22SToby Isaac + dm   - The DM object
6576c58f1c22SToby Isaac . name - The label name
6577c58f1c22SToby Isaac . point - The mesh point
6578c58f1c22SToby Isaac - value - The label value for this point
6579c58f1c22SToby Isaac 
6580c58f1c22SToby Isaac   Output Parameter:
6581c58f1c22SToby Isaac 
6582c58f1c22SToby Isaac   Level: beginner
6583c58f1c22SToby Isaac 
6584c58f1c22SToby Isaac .keywords: mesh
6585c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6586c58f1c22SToby Isaac @*/
6587c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6588c58f1c22SToby Isaac {
6589c58f1c22SToby Isaac   DMLabel        label;
6590c58f1c22SToby Isaac   PetscErrorCode ierr;
6591c58f1c22SToby Isaac 
6592c58f1c22SToby Isaac   PetscFunctionBegin;
6593c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6594c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6595c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6596c58f1c22SToby Isaac   if (!label) {
6597c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6598c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6599c58f1c22SToby Isaac   }
6600c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6601c58f1c22SToby Isaac   PetscFunctionReturn(0);
6602c58f1c22SToby Isaac }
6603c58f1c22SToby Isaac 
6604c58f1c22SToby Isaac /*@C
6605c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6606c58f1c22SToby Isaac 
6607c58f1c22SToby Isaac   Not Collective
6608c58f1c22SToby Isaac 
6609c58f1c22SToby Isaac   Input Parameters:
6610c58f1c22SToby Isaac + dm   - The DM object
6611c58f1c22SToby Isaac . name - The label name
6612c58f1c22SToby Isaac . point - The mesh point
6613c58f1c22SToby Isaac - value - The label value for this point
6614c58f1c22SToby Isaac 
6615c58f1c22SToby Isaac   Output Parameter:
6616c58f1c22SToby Isaac 
6617c58f1c22SToby Isaac   Level: beginner
6618c58f1c22SToby Isaac 
6619c58f1c22SToby Isaac .keywords: mesh
6620c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6621c58f1c22SToby Isaac @*/
6622c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6623c58f1c22SToby Isaac {
6624c58f1c22SToby Isaac   DMLabel        label;
6625c58f1c22SToby Isaac   PetscErrorCode ierr;
6626c58f1c22SToby Isaac 
6627c58f1c22SToby Isaac   PetscFunctionBegin;
6628c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6629c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6630c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6631c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6632c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6633c58f1c22SToby Isaac   PetscFunctionReturn(0);
6634c58f1c22SToby Isaac }
6635c58f1c22SToby Isaac 
6636c58f1c22SToby Isaac /*@C
6637c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6638c58f1c22SToby Isaac 
6639c58f1c22SToby Isaac   Not Collective
6640c58f1c22SToby Isaac 
6641c58f1c22SToby Isaac   Input Parameters:
6642c58f1c22SToby Isaac + dm   - The DM object
6643c58f1c22SToby Isaac - name - The label name
6644c58f1c22SToby Isaac 
6645c58f1c22SToby Isaac   Output Parameter:
6646c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6647c58f1c22SToby Isaac 
6648c58f1c22SToby Isaac   Level: beginner
6649c58f1c22SToby Isaac 
6650c58f1c22SToby Isaac .keywords: mesh
6651df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6652c58f1c22SToby Isaac @*/
6653c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6654c58f1c22SToby Isaac {
6655c58f1c22SToby Isaac   DMLabel        label;
6656c58f1c22SToby Isaac   PetscErrorCode ierr;
6657c58f1c22SToby Isaac 
6658c58f1c22SToby Isaac   PetscFunctionBegin;
6659c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6660c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6661c58f1c22SToby Isaac   PetscValidPointer(size, 3);
6662c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6663c58f1c22SToby Isaac   *size = 0;
6664c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6665c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6666c58f1c22SToby Isaac   PetscFunctionReturn(0);
6667c58f1c22SToby Isaac }
6668c58f1c22SToby Isaac 
6669c58f1c22SToby Isaac /*@C
6670c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6671c58f1c22SToby Isaac 
6672c58f1c22SToby Isaac   Not Collective
6673c58f1c22SToby Isaac 
6674c58f1c22SToby Isaac   Input Parameters:
6675c58f1c22SToby Isaac + mesh - The DM object
6676c58f1c22SToby Isaac - name - The label name
6677c58f1c22SToby Isaac 
6678c58f1c22SToby Isaac   Output Parameter:
6679c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6680c58f1c22SToby Isaac 
6681c58f1c22SToby Isaac   Level: beginner
6682c58f1c22SToby Isaac 
6683c58f1c22SToby Isaac .keywords: mesh
6684c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6685c58f1c22SToby Isaac @*/
6686c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6687c58f1c22SToby Isaac {
6688c58f1c22SToby Isaac   DMLabel        label;
6689c58f1c22SToby Isaac   PetscErrorCode ierr;
6690c58f1c22SToby Isaac 
6691c58f1c22SToby Isaac   PetscFunctionBegin;
6692c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6693c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6694c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6695c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6696c58f1c22SToby Isaac   *ids = NULL;
6697dab2e251SBlaise Bourdin  if (label) {
6698c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6699dab2e251SBlaise Bourdin   } else {
6700dab2e251SBlaise Bourdin     /* returning an empty IS */
6701dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6702dab2e251SBlaise Bourdin   }
6703c58f1c22SToby Isaac   PetscFunctionReturn(0);
6704c58f1c22SToby Isaac }
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac /*@C
6707c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6708c58f1c22SToby Isaac 
6709c58f1c22SToby Isaac   Not Collective
6710c58f1c22SToby Isaac 
6711c58f1c22SToby Isaac   Input Parameters:
6712c58f1c22SToby Isaac + dm - The DM object
6713c58f1c22SToby Isaac . name - The label name
6714c58f1c22SToby Isaac - value - The stratum value
6715c58f1c22SToby Isaac 
6716c58f1c22SToby Isaac   Output Parameter:
6717c58f1c22SToby Isaac . size - The stratum size
6718c58f1c22SToby Isaac 
6719c58f1c22SToby Isaac   Level: beginner
6720c58f1c22SToby Isaac 
6721c58f1c22SToby Isaac .keywords: mesh
6722c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6723c58f1c22SToby Isaac @*/
6724c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6725c58f1c22SToby Isaac {
6726c58f1c22SToby Isaac   DMLabel        label;
6727c58f1c22SToby Isaac   PetscErrorCode ierr;
6728c58f1c22SToby Isaac 
6729c58f1c22SToby Isaac   PetscFunctionBegin;
6730c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6731c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6732c58f1c22SToby Isaac   PetscValidPointer(size, 4);
6733c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6734c58f1c22SToby Isaac   *size = 0;
6735c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6736c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6737c58f1c22SToby Isaac   PetscFunctionReturn(0);
6738c58f1c22SToby Isaac }
6739c58f1c22SToby Isaac 
6740c58f1c22SToby Isaac /*@C
6741c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6742c58f1c22SToby Isaac 
6743c58f1c22SToby Isaac   Not Collective
6744c58f1c22SToby Isaac 
6745c58f1c22SToby Isaac   Input Parameters:
6746c58f1c22SToby Isaac + dm - The DM object
6747c58f1c22SToby Isaac . name - The label name
6748c58f1c22SToby Isaac - value - The stratum value
6749c58f1c22SToby Isaac 
6750c58f1c22SToby Isaac   Output Parameter:
6751c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6752c58f1c22SToby Isaac 
6753c58f1c22SToby Isaac   Level: beginner
6754c58f1c22SToby Isaac 
6755c58f1c22SToby Isaac .keywords: mesh
6756c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6757c58f1c22SToby Isaac @*/
6758c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6759c58f1c22SToby Isaac {
6760c58f1c22SToby Isaac   DMLabel        label;
6761c58f1c22SToby Isaac   PetscErrorCode ierr;
6762c58f1c22SToby Isaac 
6763c58f1c22SToby Isaac   PetscFunctionBegin;
6764c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6765c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6766c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6767c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6768c58f1c22SToby Isaac   *points = NULL;
6769c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6770c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6771c58f1c22SToby Isaac   PetscFunctionReturn(0);
6772c58f1c22SToby Isaac }
6773c58f1c22SToby Isaac 
67744de306b1SToby Isaac /*@C
67759044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
67764de306b1SToby Isaac 
67774de306b1SToby Isaac   Not Collective
67784de306b1SToby Isaac 
67794de306b1SToby Isaac   Input Parameters:
67804de306b1SToby Isaac + dm - The DM object
67814de306b1SToby Isaac . name - The label name
67824de306b1SToby Isaac . value - The stratum value
67834de306b1SToby Isaac - points - The stratum points
67844de306b1SToby Isaac 
67854de306b1SToby Isaac   Level: beginner
67864de306b1SToby Isaac 
67874de306b1SToby Isaac .keywords: mesh
67884de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
67894de306b1SToby Isaac @*/
67904de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
67914de306b1SToby Isaac {
67924de306b1SToby Isaac   DMLabel        label;
67934de306b1SToby Isaac   PetscErrorCode ierr;
67944de306b1SToby Isaac 
67954de306b1SToby Isaac   PetscFunctionBegin;
67964de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67974de306b1SToby Isaac   PetscValidCharPointer(name, 2);
67984de306b1SToby Isaac   PetscValidPointer(points, 4);
67994de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
68004de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
68014de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
68024de306b1SToby Isaac   PetscFunctionReturn(0);
68034de306b1SToby Isaac }
68044de306b1SToby Isaac 
6805c58f1c22SToby Isaac /*@C
6806c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6807c58f1c22SToby Isaac 
6808c58f1c22SToby Isaac   Not Collective
6809c58f1c22SToby Isaac 
6810c58f1c22SToby Isaac   Input Parameters:
6811c58f1c22SToby Isaac + dm   - The DM object
6812c58f1c22SToby Isaac . name - The label name
6813c58f1c22SToby Isaac - value - The label value for this point
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac   Output Parameter:
6816c58f1c22SToby Isaac 
6817c58f1c22SToby Isaac   Level: beginner
6818c58f1c22SToby Isaac 
6819c58f1c22SToby Isaac .keywords: mesh
6820c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6821c58f1c22SToby Isaac @*/
6822c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6823c58f1c22SToby Isaac {
6824c58f1c22SToby Isaac   DMLabel        label;
6825c58f1c22SToby Isaac   PetscErrorCode ierr;
6826c58f1c22SToby Isaac 
6827c58f1c22SToby Isaac   PetscFunctionBegin;
6828c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6829c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6830c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6831c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6832c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6833c58f1c22SToby Isaac   PetscFunctionReturn(0);
6834c58f1c22SToby Isaac }
6835c58f1c22SToby Isaac 
6836c58f1c22SToby Isaac /*@
6837c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6838c58f1c22SToby Isaac 
6839c58f1c22SToby Isaac   Not Collective
6840c58f1c22SToby Isaac 
6841c58f1c22SToby Isaac   Input Parameter:
6842c58f1c22SToby Isaac . dm   - The DM object
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Output Parameter:
6845c58f1c22SToby Isaac . numLabels - the number of Labels
6846c58f1c22SToby Isaac 
6847c58f1c22SToby Isaac   Level: intermediate
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac .keywords: mesh
6850c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6851c58f1c22SToby Isaac @*/
6852c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6853c58f1c22SToby Isaac {
6854c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6855c58f1c22SToby Isaac   PetscInt  n    = 0;
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac   PetscFunctionBegin;
6858c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6859c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
6860c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6861c58f1c22SToby Isaac   *numLabels = n;
6862c58f1c22SToby Isaac   PetscFunctionReturn(0);
6863c58f1c22SToby Isaac }
6864c58f1c22SToby Isaac 
6865c58f1c22SToby Isaac /*@C
6866c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Not Collective
6869c58f1c22SToby Isaac 
6870c58f1c22SToby Isaac   Input Parameters:
6871c58f1c22SToby Isaac + dm - The DM object
6872c58f1c22SToby Isaac - n  - the label number
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac   Output Parameter:
6875c58f1c22SToby Isaac . name - the label name
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Level: intermediate
6878c58f1c22SToby Isaac 
6879c58f1c22SToby Isaac .keywords: mesh
6880c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6881c58f1c22SToby Isaac @*/
6882c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6883c58f1c22SToby Isaac {
6884c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6885c58f1c22SToby Isaac   PetscInt       l    = 0;
6886d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
6887c58f1c22SToby Isaac 
6888c58f1c22SToby Isaac   PetscFunctionBegin;
6889c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6890c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6891c58f1c22SToby Isaac   while (next) {
6892c58f1c22SToby Isaac     if (l == n) {
6893d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6894c58f1c22SToby Isaac       PetscFunctionReturn(0);
6895c58f1c22SToby Isaac     }
6896c58f1c22SToby Isaac     ++l;
6897c58f1c22SToby Isaac     next = next->next;
6898c58f1c22SToby Isaac   }
6899c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6900c58f1c22SToby Isaac }
6901c58f1c22SToby Isaac 
6902c58f1c22SToby Isaac /*@C
6903c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6904c58f1c22SToby Isaac 
6905c58f1c22SToby Isaac   Not Collective
6906c58f1c22SToby Isaac 
6907c58f1c22SToby Isaac   Input Parameters:
6908c58f1c22SToby Isaac + dm   - The DM object
6909c58f1c22SToby Isaac - name - The label name
6910c58f1c22SToby Isaac 
6911c58f1c22SToby Isaac   Output Parameter:
6912c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6913c58f1c22SToby Isaac 
6914c58f1c22SToby Isaac   Level: intermediate
6915c58f1c22SToby Isaac 
6916c58f1c22SToby Isaac .keywords: mesh
6917c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6918c58f1c22SToby Isaac @*/
6919c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6920c58f1c22SToby Isaac {
6921c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6922d67d17b1SMatthew G. Knepley   const char    *lname;
6923c58f1c22SToby Isaac   PetscErrorCode ierr;
6924c58f1c22SToby Isaac 
6925c58f1c22SToby Isaac   PetscFunctionBegin;
6926c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6927c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6928c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
6929c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6930c58f1c22SToby Isaac   while (next) {
6931d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6932d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
6933c58f1c22SToby Isaac     if (*hasLabel) break;
6934c58f1c22SToby Isaac     next = next->next;
6935c58f1c22SToby Isaac   }
6936c58f1c22SToby Isaac   PetscFunctionReturn(0);
6937c58f1c22SToby Isaac }
6938c58f1c22SToby Isaac 
6939c58f1c22SToby Isaac /*@C
6940c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   Not Collective
6943c58f1c22SToby Isaac 
6944c58f1c22SToby Isaac   Input Parameters:
6945c58f1c22SToby Isaac + dm   - The DM object
6946c58f1c22SToby Isaac - name - The label name
6947c58f1c22SToby Isaac 
6948c58f1c22SToby Isaac   Output Parameter:
6949c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6950c58f1c22SToby Isaac 
6951c58f1c22SToby Isaac   Level: intermediate
6952c58f1c22SToby Isaac 
6953c58f1c22SToby Isaac .keywords: mesh
6954c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6955c58f1c22SToby Isaac @*/
6956c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6957c58f1c22SToby Isaac {
6958c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6959c58f1c22SToby Isaac   PetscBool      hasLabel;
6960d67d17b1SMatthew G. Knepley   const char    *lname;
6961c58f1c22SToby Isaac   PetscErrorCode ierr;
6962c58f1c22SToby Isaac 
6963c58f1c22SToby Isaac   PetscFunctionBegin;
6964c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6965c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6966c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6967c58f1c22SToby Isaac   *label = NULL;
6968c58f1c22SToby Isaac   while (next) {
6969d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6970d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6971c58f1c22SToby Isaac     if (hasLabel) {
6972c58f1c22SToby Isaac       *label = next->label;
6973c58f1c22SToby Isaac       break;
6974c58f1c22SToby Isaac     }
6975c58f1c22SToby Isaac     next = next->next;
6976c58f1c22SToby Isaac   }
6977c58f1c22SToby Isaac   PetscFunctionReturn(0);
6978c58f1c22SToby Isaac }
6979c58f1c22SToby Isaac 
6980c58f1c22SToby Isaac /*@C
6981c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac   Not Collective
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   Input Parameters:
6986c58f1c22SToby Isaac + dm - The DM object
6987c58f1c22SToby Isaac - n  - the label number
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   Output Parameter:
6990c58f1c22SToby Isaac . label - the label
6991c58f1c22SToby Isaac 
6992c58f1c22SToby Isaac   Level: intermediate
6993c58f1c22SToby Isaac 
6994c58f1c22SToby Isaac .keywords: mesh
6995c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6996c58f1c22SToby Isaac @*/
6997c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6998c58f1c22SToby Isaac {
6999c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
7000c58f1c22SToby Isaac   PetscInt    l    = 0;
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac   PetscFunctionBegin;
7003c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7004c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7005c58f1c22SToby Isaac   while (next) {
7006c58f1c22SToby Isaac     if (l == n) {
7007c58f1c22SToby Isaac       *label = next->label;
7008c58f1c22SToby Isaac       PetscFunctionReturn(0);
7009c58f1c22SToby Isaac     }
7010c58f1c22SToby Isaac     ++l;
7011c58f1c22SToby Isaac     next = next->next;
7012c58f1c22SToby Isaac   }
7013c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7014c58f1c22SToby Isaac }
7015c58f1c22SToby Isaac 
7016c58f1c22SToby Isaac /*@C
7017c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac   Not Collective
7020c58f1c22SToby Isaac 
7021c58f1c22SToby Isaac   Input Parameters:
7022c58f1c22SToby Isaac + dm   - The DM object
7023c58f1c22SToby Isaac - label - The DMLabel
7024c58f1c22SToby Isaac 
7025c58f1c22SToby Isaac   Level: developer
7026c58f1c22SToby Isaac 
7027c58f1c22SToby Isaac .keywords: mesh
7028c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7029c58f1c22SToby Isaac @*/
7030c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7031c58f1c22SToby Isaac {
7032c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
7033c58f1c22SToby Isaac   PetscBool      hasLabel;
7034d67d17b1SMatthew G. Knepley   const char    *lname;
7035c58f1c22SToby Isaac   PetscErrorCode ierr;
7036c58f1c22SToby Isaac 
7037c58f1c22SToby Isaac   PetscFunctionBegin;
7038c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7039d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7040d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7041d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7042c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7043c58f1c22SToby Isaac   tmpLabel->label  = label;
7044c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
7045c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
7046c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
7047c58f1c22SToby Isaac   PetscFunctionReturn(0);
7048c58f1c22SToby Isaac }
7049c58f1c22SToby Isaac 
7050c58f1c22SToby Isaac /*@C
7051c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
7052c58f1c22SToby Isaac 
7053c58f1c22SToby Isaac   Not Collective
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac   Input Parameters:
7056c58f1c22SToby Isaac + dm   - The DM object
7057c58f1c22SToby Isaac - name - The label name
7058c58f1c22SToby Isaac 
7059c58f1c22SToby Isaac   Output Parameter:
7060c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7061c58f1c22SToby Isaac 
7062c58f1c22SToby Isaac   Level: developer
7063c58f1c22SToby Isaac 
7064c58f1c22SToby Isaac .keywords: mesh
7065c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7066c58f1c22SToby Isaac @*/
7067c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7068c58f1c22SToby Isaac {
7069c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7070c58f1c22SToby Isaac   DMLabelLink    last = NULL;
7071c58f1c22SToby Isaac   PetscBool      hasLabel;
7072d67d17b1SMatthew G. Knepley   const char    *lname;
7073c58f1c22SToby Isaac   PetscErrorCode ierr;
7074c58f1c22SToby Isaac 
7075c58f1c22SToby Isaac   PetscFunctionBegin;
7076c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7077c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
7078c58f1c22SToby Isaac   *label = NULL;
7079c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
7080c58f1c22SToby Isaac   while (next) {
7081d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7082d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7083c58f1c22SToby Isaac     if (hasLabel) {
7084c58f1c22SToby Isaac       if (last) last->next       = next->next;
7085c58f1c22SToby Isaac       else      dm->labels->next = next->next;
7086c58f1c22SToby Isaac       next->next = NULL;
7087c58f1c22SToby Isaac       *label     = next->label;
7088c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
7089c58f1c22SToby Isaac       if (hasLabel) {
7090c58f1c22SToby Isaac         dm->depthLabel = NULL;
7091c58f1c22SToby Isaac       }
7092c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
7093c58f1c22SToby Isaac       break;
7094c58f1c22SToby Isaac     }
7095c58f1c22SToby Isaac     last = next;
7096c58f1c22SToby Isaac     next = next->next;
7097c58f1c22SToby Isaac   }
7098c58f1c22SToby Isaac   PetscFunctionReturn(0);
7099c58f1c22SToby Isaac }
7100c58f1c22SToby Isaac 
7101c58f1c22SToby Isaac /*@C
7102c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7103c58f1c22SToby Isaac 
7104c58f1c22SToby Isaac   Not Collective
7105c58f1c22SToby Isaac 
7106c58f1c22SToby Isaac   Input Parameters:
7107c58f1c22SToby Isaac + dm   - The DM object
7108c58f1c22SToby Isaac - name - The label name
7109c58f1c22SToby Isaac 
7110c58f1c22SToby Isaac   Output Parameter:
7111c58f1c22SToby Isaac . output - The flag for output
7112c58f1c22SToby Isaac 
7113c58f1c22SToby Isaac   Level: developer
7114c58f1c22SToby Isaac 
7115c58f1c22SToby Isaac .keywords: mesh
7116c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7117c58f1c22SToby Isaac @*/
7118c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7119c58f1c22SToby Isaac {
7120c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7121d67d17b1SMatthew G. Knepley   const char    *lname;
7122c58f1c22SToby Isaac   PetscErrorCode ierr;
7123c58f1c22SToby Isaac 
7124c58f1c22SToby Isaac   PetscFunctionBegin;
7125c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7126c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7127c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7128c58f1c22SToby Isaac   while (next) {
7129c58f1c22SToby Isaac     PetscBool flg;
7130c58f1c22SToby Isaac 
7131d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7132d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7133c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7134c58f1c22SToby Isaac     next = next->next;
7135c58f1c22SToby Isaac   }
7136c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7137c58f1c22SToby Isaac }
7138c58f1c22SToby Isaac 
7139c58f1c22SToby Isaac /*@C
7140c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7141c58f1c22SToby Isaac 
7142c58f1c22SToby Isaac   Not Collective
7143c58f1c22SToby Isaac 
7144c58f1c22SToby Isaac   Input Parameters:
7145c58f1c22SToby Isaac + dm     - The DM object
7146c58f1c22SToby Isaac . name   - The label name
7147c58f1c22SToby Isaac - output - The flag for output
7148c58f1c22SToby Isaac 
7149c58f1c22SToby Isaac   Level: developer
7150c58f1c22SToby Isaac 
7151c58f1c22SToby Isaac .keywords: mesh
7152c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7153c58f1c22SToby Isaac @*/
7154c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7155c58f1c22SToby Isaac {
7156c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7157d67d17b1SMatthew G. Knepley   const char    *lname;
7158c58f1c22SToby Isaac   PetscErrorCode ierr;
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac   PetscFunctionBegin;
7161c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7162c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7163c58f1c22SToby Isaac   while (next) {
7164c58f1c22SToby Isaac     PetscBool flg;
7165c58f1c22SToby Isaac 
7166d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7167d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7168c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7169c58f1c22SToby Isaac     next = next->next;
7170c58f1c22SToby Isaac   }
7171c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7172c58f1c22SToby Isaac }
7173c58f1c22SToby Isaac 
7174c58f1c22SToby Isaac 
7175c58f1c22SToby Isaac /*@
7176c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7177c58f1c22SToby Isaac 
7178c58f1c22SToby Isaac   Collective on DM
7179c58f1c22SToby Isaac 
7180c58f1c22SToby Isaac   Input Parameter:
71812e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
7182c58f1c22SToby Isaac 
7183c58f1c22SToby Isaac   Output Parameter:
71842e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
7185c58f1c22SToby Isaac 
7186c58f1c22SToby Isaac   Level: intermediate
7187c58f1c22SToby Isaac 
7188c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7189c58f1c22SToby Isaac 
7190c58f1c22SToby Isaac .keywords: mesh
7191367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
7192c58f1c22SToby Isaac @*/
7193c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
7194c58f1c22SToby Isaac {
7195c58f1c22SToby Isaac   PetscInt       numLabels, l;
7196c58f1c22SToby Isaac   PetscErrorCode ierr;
7197c58f1c22SToby Isaac 
7198c58f1c22SToby Isaac   PetscFunctionBegin;
7199c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
7200c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
7201c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
7202c58f1c22SToby Isaac     DMLabel     label, labelNew;
7203c58f1c22SToby Isaac     const char *name;
7204c58f1c22SToby Isaac     PetscBool   flg;
7205c58f1c22SToby Isaac 
7206c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
7207c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7208c58f1c22SToby Isaac     if (flg) continue;
72097d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
72107d5acc75SStefano Zampini     if (flg) continue;
7211c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
7212c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
7213c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
7214c58f1c22SToby Isaac   }
7215c58f1c22SToby Isaac   PetscFunctionReturn(0);
7216c58f1c22SToby Isaac }
7217a8fb8f29SToby Isaac 
7218a8fb8f29SToby Isaac /*@
7219a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7220a8fb8f29SToby Isaac 
7221a8fb8f29SToby Isaac   Input Parameter:
7222a8fb8f29SToby Isaac . dm - The DM object
7223a8fb8f29SToby Isaac 
7224a8fb8f29SToby Isaac   Output Parameter:
7225a8fb8f29SToby Isaac . cdm - The coarse DM
7226a8fb8f29SToby Isaac 
7227a8fb8f29SToby Isaac   Level: intermediate
7228a8fb8f29SToby Isaac 
7229a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7230a8fb8f29SToby Isaac @*/
7231a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7232a8fb8f29SToby Isaac {
7233a8fb8f29SToby Isaac   PetscFunctionBegin;
7234a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7235a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7236a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7237a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7238a8fb8f29SToby Isaac }
7239a8fb8f29SToby Isaac 
7240a8fb8f29SToby Isaac /*@
7241a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7242a8fb8f29SToby Isaac 
7243a8fb8f29SToby Isaac   Input Parameters:
7244a8fb8f29SToby Isaac + dm - The DM object
7245a8fb8f29SToby Isaac - cdm - The coarse DM
7246a8fb8f29SToby Isaac 
7247a8fb8f29SToby Isaac   Level: intermediate
7248a8fb8f29SToby Isaac 
7249a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7250a8fb8f29SToby Isaac @*/
7251a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7252a8fb8f29SToby Isaac {
7253a8fb8f29SToby Isaac   PetscErrorCode ierr;
7254a8fb8f29SToby Isaac 
7255a8fb8f29SToby Isaac   PetscFunctionBegin;
7256a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7257a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7258a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7259a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7260a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7261a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7262a8fb8f29SToby Isaac }
7263a8fb8f29SToby Isaac 
726488bdff64SToby Isaac /*@
726588bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
726688bdff64SToby Isaac 
726788bdff64SToby Isaac   Input Parameter:
726888bdff64SToby Isaac . dm - The DM object
726988bdff64SToby Isaac 
727088bdff64SToby Isaac   Output Parameter:
727188bdff64SToby Isaac . fdm - The fine DM
727288bdff64SToby Isaac 
727388bdff64SToby Isaac   Level: intermediate
727488bdff64SToby Isaac 
727588bdff64SToby Isaac .seealso: DMSetFineDM()
727688bdff64SToby Isaac @*/
727788bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
727888bdff64SToby Isaac {
727988bdff64SToby Isaac   PetscFunctionBegin;
728088bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
728188bdff64SToby Isaac   PetscValidPointer(fdm, 2);
728288bdff64SToby Isaac   *fdm = dm->fineMesh;
728388bdff64SToby Isaac   PetscFunctionReturn(0);
728488bdff64SToby Isaac }
728588bdff64SToby Isaac 
728688bdff64SToby Isaac /*@
728788bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
728888bdff64SToby Isaac 
728988bdff64SToby Isaac   Input Parameters:
729088bdff64SToby Isaac + dm - The DM object
729188bdff64SToby Isaac - fdm - The fine DM
729288bdff64SToby Isaac 
729388bdff64SToby Isaac   Level: intermediate
729488bdff64SToby Isaac 
729588bdff64SToby Isaac .seealso: DMGetFineDM()
729688bdff64SToby Isaac @*/
729788bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
729888bdff64SToby Isaac {
729988bdff64SToby Isaac   PetscErrorCode ierr;
730088bdff64SToby Isaac 
730188bdff64SToby Isaac   PetscFunctionBegin;
730288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
730388bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
730488bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
730588bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
730688bdff64SToby Isaac   dm->fineMesh = fdm;
730788bdff64SToby Isaac   PetscFunctionReturn(0);
730888bdff64SToby Isaac }
730988bdff64SToby Isaac 
7310a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7311a6ba4734SToby Isaac 
7312a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7313a6ba4734SToby Isaac {
7314e5e52638SMatthew G. Knepley   PetscInt       d;
7315a6ba4734SToby Isaac   PetscErrorCode ierr;
7316a6ba4734SToby Isaac 
7317a6ba4734SToby Isaac   PetscFunctionBegin;
7318e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7319e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7320e5e52638SMatthew G. Knepley   }
7321a6ba4734SToby Isaac   PetscFunctionReturn(0);
7322a6ba4734SToby Isaac }
7323a6ba4734SToby Isaac 
7324a6ba4734SToby Isaac /*@C
7325a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7326a6ba4734SToby Isaac 
7327a6ba4734SToby Isaac   Input Parameters:
73284c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7329f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7330a6ba4734SToby Isaac . name        - The BC name
7331a6ba4734SToby Isaac . labelname   - The label defining constrained points
7332a6ba4734SToby Isaac . field       - The field to constrain
7333e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7334a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7335a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7336a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7337a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7338a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7339a6ba4734SToby Isaac 
7340a6ba4734SToby Isaac   Options Database Keys:
7341a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7342a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7343a6ba4734SToby Isaac 
7344a6ba4734SToby Isaac   Level: developer
7345a6ba4734SToby Isaac 
7346a6ba4734SToby Isaac .seealso: DMGetBoundary()
7347a6ba4734SToby Isaac @*/
7348a30ec4eaSSatish 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)
7349a6ba4734SToby Isaac {
7350e5e52638SMatthew G. Knepley   PetscDS        ds;
7351a6ba4734SToby Isaac   PetscErrorCode ierr;
7352a6ba4734SToby Isaac 
7353a6ba4734SToby Isaac   PetscFunctionBegin;
7354a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7355e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7356e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7357a6ba4734SToby Isaac   PetscFunctionReturn(0);
7358a6ba4734SToby Isaac }
7359a6ba4734SToby Isaac 
7360a6ba4734SToby Isaac /*@
7361a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7362a6ba4734SToby Isaac 
7363a6ba4734SToby Isaac   Input Parameters:
7364a6ba4734SToby Isaac . dm - The mesh object
7365a6ba4734SToby Isaac 
7366a6ba4734SToby Isaac   Output Parameters:
7367a6ba4734SToby Isaac . numBd - The number of BC
7368a6ba4734SToby Isaac 
7369a6ba4734SToby Isaac   Level: intermediate
7370a6ba4734SToby Isaac 
7371a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7372a6ba4734SToby Isaac @*/
7373a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
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 = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7382a6ba4734SToby Isaac   PetscFunctionReturn(0);
7383a6ba4734SToby Isaac }
7384a6ba4734SToby Isaac 
7385a6ba4734SToby Isaac /*@C
73861c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7387a6ba4734SToby Isaac 
7388a6ba4734SToby Isaac   Input Parameters:
7389a6ba4734SToby Isaac + dm          - The mesh object
7390a6ba4734SToby Isaac - bd          - The BC number
7391a6ba4734SToby Isaac 
7392a6ba4734SToby Isaac   Output Parameters:
7393f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7394a6ba4734SToby Isaac . name        - The BC name
7395a6ba4734SToby Isaac . labelname   - The label defining constrained points
7396a6ba4734SToby Isaac . field       - The field to constrain
7397a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7398a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7399a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7400a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7401a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7402a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7403a6ba4734SToby Isaac 
7404a6ba4734SToby Isaac   Options Database Keys:
7405a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7406a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7407a6ba4734SToby Isaac 
7408a6ba4734SToby Isaac   Level: developer
7409a6ba4734SToby Isaac 
7410a6ba4734SToby Isaac .seealso: DMAddBoundary()
7411a6ba4734SToby Isaac @*/
7412a30ec4eaSSatish 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)
7413a6ba4734SToby Isaac {
7414e5e52638SMatthew G. Knepley   PetscDS        ds;
741558ebd649SToby Isaac   PetscErrorCode ierr;
7416a6ba4734SToby Isaac 
7417a6ba4734SToby Isaac   PetscFunctionBegin;
7418a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7419e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7420e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7421a6ba4734SToby Isaac   PetscFunctionReturn(0);
7422a6ba4734SToby Isaac }
7423a6ba4734SToby Isaac 
7424e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7425e6f8dbb6SToby Isaac {
7426e5e52638SMatthew G. Knepley   PetscDS        ds;
7427dff059c6SToby Isaac   DMBoundary    *lastnext;
7428e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7429e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7430e6f8dbb6SToby Isaac 
7431e6f8dbb6SToby Isaac   PetscFunctionBegin;
7432e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7433e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
743447a1f5adSToby Isaac   if (dm->boundary) {
743547a1f5adSToby Isaac     DMBoundary next = dm->boundary;
743647a1f5adSToby Isaac 
743747a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
743847a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
743947a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
744047a1f5adSToby Isaac     while (next) {
744147a1f5adSToby Isaac       DMBoundary b = next;
744247a1f5adSToby Isaac 
744347a1f5adSToby Isaac       next = b->next;
744447a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7445a6ba4734SToby Isaac     }
744647a1f5adSToby Isaac     dm->boundary = NULL;
7447a6ba4734SToby Isaac   }
744847a1f5adSToby Isaac 
7449dff059c6SToby Isaac   lastnext = &(dm->boundary);
7450e6f8dbb6SToby Isaac   while (dsbound) {
7451e6f8dbb6SToby Isaac     DMBoundary dmbound;
7452e6f8dbb6SToby Isaac 
7453e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7454e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7455e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7456994fe344SLisandro 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);}
745747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7458dff059c6SToby Isaac     *lastnext = dmbound;
7459dff059c6SToby Isaac     lastnext = &(dmbound->next);
7460dff059c6SToby Isaac     dsbound = dsbound->next;
7461a6ba4734SToby Isaac   }
7462a6ba4734SToby Isaac   PetscFunctionReturn(0);
7463a6ba4734SToby Isaac }
7464a6ba4734SToby Isaac 
7465a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7466a6ba4734SToby Isaac {
7467b95f2879SToby Isaac   DMBoundary     b;
7468a6ba4734SToby Isaac   PetscErrorCode ierr;
7469a6ba4734SToby Isaac 
7470a6ba4734SToby Isaac   PetscFunctionBegin;
7471a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7472a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
7473a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7474e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7475b95f2879SToby Isaac   b = dm->boundary;
7476a6ba4734SToby Isaac   while (b && !(*isBd)) {
7477e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7478e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
74793424c85cSToby Isaac 
74803424c85cSToby Isaac     if (label) {
7481a6ba4734SToby Isaac       PetscInt i;
7482a6ba4734SToby Isaac 
7483e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7484e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7485a6ba4734SToby Isaac       }
7486a6ba4734SToby Isaac     }
7487a6ba4734SToby Isaac     b = b->next;
7488a6ba4734SToby Isaac   }
7489a6ba4734SToby Isaac   PetscFunctionReturn(0);
7490a6ba4734SToby Isaac }
74914d6f44ffSToby Isaac 
74924d6f44ffSToby Isaac /*@C
74934d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
74944d6f44ffSToby Isaac 
74954d6f44ffSToby Isaac   Input Parameters:
74964d6f44ffSToby Isaac + dm      - The DM
74970709b2feSToby Isaac . time    - The time
74984d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
74994d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
75004d6f44ffSToby Isaac - mode    - The insertion mode for values
75014d6f44ffSToby Isaac 
75024d6f44ffSToby Isaac   Output Parameter:
75034d6f44ffSToby Isaac . X - vector
75044d6f44ffSToby Isaac 
75054d6f44ffSToby Isaac    Calling sequence of func:
75060709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
75074d6f44ffSToby Isaac 
75084d6f44ffSToby Isaac +  dim - The spatial dimension
75094d6f44ffSToby Isaac .  x   - The coordinates
75104d6f44ffSToby Isaac .  Nf  - The number of fields
75114d6f44ffSToby Isaac .  u   - The output field values
75124d6f44ffSToby Isaac -  ctx - optional user-defined function context
75134d6f44ffSToby Isaac 
75144d6f44ffSToby Isaac   Level: developer
75154d6f44ffSToby Isaac 
75162716604bSToby Isaac .seealso: DMComputeL2Diff()
75174d6f44ffSToby Isaac @*/
75180709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
75194d6f44ffSToby Isaac {
75204d6f44ffSToby Isaac   Vec            localX;
75214d6f44ffSToby Isaac   PetscErrorCode ierr;
75224d6f44ffSToby Isaac 
75234d6f44ffSToby Isaac   PetscFunctionBegin;
75244d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75254d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
75260709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75274d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
75284d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
75294d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
75304d6f44ffSToby Isaac   PetscFunctionReturn(0);
75314d6f44ffSToby Isaac }
75324d6f44ffSToby Isaac 
75330709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
75344d6f44ffSToby Isaac {
75354d6f44ffSToby Isaac   PetscErrorCode ierr;
75364d6f44ffSToby Isaac 
75374d6f44ffSToby Isaac   PetscFunctionBegin;
75384d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75394d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
75400918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
75410709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75424d6f44ffSToby Isaac   PetscFunctionReturn(0);
75434d6f44ffSToby Isaac }
75444d6f44ffSToby Isaac 
75452c53366bSMatthew 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)
75462c53366bSMatthew G. Knepley {
75472c53366bSMatthew G. Knepley   Vec            localX;
75482c53366bSMatthew G. Knepley   PetscErrorCode ierr;
75492c53366bSMatthew G. Knepley 
75502c53366bSMatthew G. Knepley   PetscFunctionBegin;
75512c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75522c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
75532c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75542c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
75552c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
75562c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
75572c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
75582c53366bSMatthew G. Knepley }
75592c53366bSMatthew G. Knepley 
75601c531cf8SMatthew 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)
75614d6f44ffSToby Isaac {
75624d6f44ffSToby Isaac   PetscErrorCode ierr;
75634d6f44ffSToby Isaac 
75644d6f44ffSToby Isaac   PetscFunctionBegin;
75654d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75664d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
75670918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
75681c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75694d6f44ffSToby Isaac   PetscFunctionReturn(0);
75704d6f44ffSToby Isaac }
75712716604bSToby Isaac 
75728c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
75738c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
75748c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
75758c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7576191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
75778c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
75788c6c5593SMatthew G. Knepley {
75798c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
75808c6c5593SMatthew G. Knepley 
75818c6c5593SMatthew G. Knepley   PetscFunctionBegin;
75828c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75838c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
75848c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
75850918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
75868c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
75878c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
75888c6c5593SMatthew G. Knepley }
75898c6c5593SMatthew G. Knepley 
75901c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
75918c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
75928c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
75938c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7594191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
75958c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
75968c6c5593SMatthew G. Knepley {
75978c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
75988c6c5593SMatthew G. Knepley 
75998c6c5593SMatthew G. Knepley   PetscFunctionBegin;
76008c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76018c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
76028c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
76030918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
76041c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
76058c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
76068c6c5593SMatthew G. Knepley }
76078c6c5593SMatthew G. Knepley 
76082716604bSToby Isaac /*@C
76092716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
76102716604bSToby Isaac 
76112716604bSToby Isaac   Input Parameters:
76122716604bSToby Isaac + dm    - The DM
76130709b2feSToby Isaac . time  - The time
76142716604bSToby Isaac . funcs - The functions to evaluate for each field component
76152716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7616574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
76172716604bSToby Isaac 
76182716604bSToby Isaac   Output Parameter:
76192716604bSToby Isaac . diff - The diff ||u - u_h||_2
76202716604bSToby Isaac 
76212716604bSToby Isaac   Level: developer
76222716604bSToby Isaac 
76231189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
76242716604bSToby Isaac @*/
76250709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
76262716604bSToby Isaac {
76272716604bSToby Isaac   PetscErrorCode ierr;
76282716604bSToby Isaac 
76292716604bSToby Isaac   PetscFunctionBegin;
76302716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7631b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
76320918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
76330709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
76342716604bSToby Isaac   PetscFunctionReturn(0);
76352716604bSToby Isaac }
7636b698f381SToby Isaac 
7637b698f381SToby Isaac /*@C
7638b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7639b698f381SToby Isaac 
7640b698f381SToby Isaac   Input Parameters:
7641b698f381SToby Isaac + dm    - The DM
7642b698f381SToby Isaac , time  - The time
7643b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
7644b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7645574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
7646b698f381SToby Isaac - n     - The vector to project along
7647b698f381SToby Isaac 
7648b698f381SToby Isaac   Output Parameter:
7649b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
7650b698f381SToby Isaac 
7651b698f381SToby Isaac   Level: developer
7652b698f381SToby Isaac 
7653b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
7654b698f381SToby Isaac @*/
7655b698f381SToby 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)
7656b698f381SToby Isaac {
7657b698f381SToby Isaac   PetscErrorCode ierr;
7658b698f381SToby Isaac 
7659b698f381SToby Isaac   PetscFunctionBegin;
7660b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7661b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7662b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7663b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7664b698f381SToby Isaac   PetscFunctionReturn(0);
7665b698f381SToby Isaac }
7666b698f381SToby Isaac 
76672a16baeaSToby Isaac /*@C
76682a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
76692a16baeaSToby Isaac 
76702a16baeaSToby Isaac   Input Parameters:
76712a16baeaSToby Isaac + dm    - The DM
76722a16baeaSToby Isaac . time  - The time
76732a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
76742a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7675574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
76762a16baeaSToby Isaac 
76772a16baeaSToby Isaac   Output Parameter:
76782a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
76792a16baeaSToby Isaac 
76802a16baeaSToby Isaac   Level: developer
76812a16baeaSToby Isaac 
76821189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
76832a16baeaSToby Isaac @*/
76841189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
76852a16baeaSToby Isaac {
76862a16baeaSToby Isaac   PetscErrorCode ierr;
76872a16baeaSToby Isaac 
76882a16baeaSToby Isaac   PetscFunctionBegin;
76892a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76902a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
76910918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
76922a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
76932a16baeaSToby Isaac   PetscFunctionReturn(0);
76942a16baeaSToby Isaac }
76952a16baeaSToby Isaac 
7696df0b854cSToby Isaac /*@C
7697df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7698cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7699df0b854cSToby Isaac 
7700df0b854cSToby Isaac   Collective on dm
7701df0b854cSToby Isaac 
7702df0b854cSToby Isaac   Input parameters:
7703df0b854cSToby Isaac + dm - the pre-adaptation DM object
7704a1b0c543SToby Isaac - label - label with the flags
7705df0b854cSToby Isaac 
7706df0b854cSToby Isaac   Output parameters:
77070d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7708df0b854cSToby Isaac 
7709df0b854cSToby Isaac   Level: intermediate
77100d1cd5e0SMatthew G. Knepley 
77110d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7712df0b854cSToby Isaac @*/
77130d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7714df0b854cSToby Isaac {
7715df0b854cSToby Isaac   PetscErrorCode ierr;
7716df0b854cSToby Isaac 
7717df0b854cSToby Isaac   PetscFunctionBegin;
7718df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7719a1b0c543SToby Isaac   PetscValidPointer(label,2);
77200d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
77210d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
77226f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
77230d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
77240d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
77250d1cd5e0SMatthew G. Knepley }
77260d1cd5e0SMatthew G. Knepley 
77270d1cd5e0SMatthew G. Knepley /*@C
77280d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
77290d1cd5e0SMatthew G. Knepley 
77300d1cd5e0SMatthew G. Knepley   Input Parameters:
77310d1cd5e0SMatthew G. Knepley + dm - The DM object
77320d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
77336f25b0d8SLisandro 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_".
77340d1cd5e0SMatthew G. Knepley 
77350d1cd5e0SMatthew G. Knepley   Output Parameter:
77360d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
77370d1cd5e0SMatthew G. Knepley 
77380d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
77390d1cd5e0SMatthew G. Knepley 
77400d1cd5e0SMatthew G. Knepley   Level: advanced
77410d1cd5e0SMatthew G. Knepley 
77420d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
77430d1cd5e0SMatthew G. Knepley @*/
77440d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
77450d1cd5e0SMatthew G. Knepley {
77460d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
77470d1cd5e0SMatthew G. Knepley 
77480d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
77490d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77500d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
77510d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
77520d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
77536f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
77546f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
77550d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7756df0b854cSToby Isaac   PetscFunctionReturn(0);
7757df0b854cSToby Isaac }
7758c4088d22SMatthew G. Knepley 
7759502a2867SDave May /*@C
7760502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7761502a2867SDave May 
7762502a2867SDave May  Not Collective
7763502a2867SDave May 
7764502a2867SDave May  Input Parameter:
7765502a2867SDave May  . dm    - The DM
7766502a2867SDave May 
7767502a2867SDave May  Output Parameter:
7768502a2867SDave May  . nranks - the number of neighbours
7769502a2867SDave May  . ranks - the neighbors ranks
7770502a2867SDave May 
7771502a2867SDave May  Notes:
7772502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
7773502a2867SDave May 
7774502a2867SDave May  Level: beginner
7775502a2867SDave May 
7776dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
7777502a2867SDave May @*/
7778502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7779502a2867SDave May {
7780502a2867SDave May   PetscErrorCode ierr;
7781502a2867SDave May 
7782502a2867SDave May   PetscFunctionBegin;
7783502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77840918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7785502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7786502a2867SDave May   PetscFunctionReturn(0);
7787502a2867SDave May }
7788502a2867SDave May 
7789531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7790531c7667SBarry Smith 
7791531c7667SBarry Smith /*
7792531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7793531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
7794531c7667SBarry Smith */
7795531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7796531c7667SBarry Smith {
7797531c7667SBarry Smith   PetscErrorCode ierr;
7798531c7667SBarry Smith 
7799531c7667SBarry Smith   PetscFunctionBegin;
7800531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7801531c7667SBarry Smith     Vec x1local;
7802531c7667SBarry Smith     DM  dm;
7803531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7804531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7805531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7806531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7807531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7808531c7667SBarry Smith     x1   = x1local;
7809531c7667SBarry Smith   }
7810531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7811531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7812531c7667SBarry Smith     DM  dm;
7813531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7814531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7815531c7667SBarry Smith   }
7816531c7667SBarry Smith   PetscFunctionReturn(0);
7817531c7667SBarry Smith }
7818531c7667SBarry Smith 
7819531c7667SBarry Smith /*@
7820531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7821531c7667SBarry Smith 
7822531c7667SBarry Smith     Input Parameter:
7823531c7667SBarry Smith .    coloring - the MatFDColoring object
7824531c7667SBarry Smith 
782595452b02SPatrick Sanan     Developer Notes:
782695452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
7827531c7667SBarry Smith 
78281b266c99SBarry Smith     Level: advanced
78291b266c99SBarry Smith 
7830531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7831531c7667SBarry Smith @*/
7832531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7833531c7667SBarry Smith {
7834531c7667SBarry Smith   PetscFunctionBegin;
7835531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7836531c7667SBarry Smith   PetscFunctionReturn(0);
7837531c7667SBarry Smith }
78388320bc6fSPatrick Sanan 
78398320bc6fSPatrick Sanan /*@
78408320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
78418320bc6fSPatrick Sanan 
78428320bc6fSPatrick Sanan     Collective
78438320bc6fSPatrick Sanan 
78448320bc6fSPatrick Sanan     Input Parameters:
78458320bc6fSPatrick Sanan +    dm - the first DM
78468320bc6fSPatrick Sanan -    dm2 - the second DM
78478320bc6fSPatrick Sanan 
78488320bc6fSPatrick Sanan     Output Parameters:
78498320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
78508320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
78518320bc6fSPatrick Sanan 
78528320bc6fSPatrick Sanan     Notes:
78538320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
78543d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
78558320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
78563d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
78573d862458SPatrick Sanan     decomposition, but hold different data.
78588320bc6fSPatrick Sanan 
78598320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
78608320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
78618320bc6fSPatrick Sanan 
78628320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
78638320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
78648320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
78658320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
78668320bc6fSPatrick Sanan 
78678320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
78688320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
78698320bc6fSPatrick Sanan .vb
78708320bc6fSPatrick Sanan   ...
78718320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
78728320bc6fSPatrick Sanan   if (set && compatible)  {
78738320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
78748320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
78753d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
78768320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
78778320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
78788320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
78798320bc6fSPatrick Sanan       }
78808320bc6fSPatrick Sanan     }
78818320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
78828320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
78838320bc6fSPatrick Sanan   } else {
78848320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
78858320bc6fSPatrick Sanan   }
78868320bc6fSPatrick Sanan   ...
78878320bc6fSPatrick Sanan .ve
78888320bc6fSPatrick Sanan 
78898320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
78908320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
78918320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
78928320bc6fSPatrick Sanan     always check the "set" output parameter.
78938320bc6fSPatrick Sanan 
78948320bc6fSPatrick Sanan     A DM is always compatible with itself.
78958320bc6fSPatrick Sanan 
78968320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
78978320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
78988320bc6fSPatrick Sanan     incompatible.
78998320bc6fSPatrick Sanan 
79008320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
79018320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
79028320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
79038320bc6fSPatrick Sanan 
79048320bc6fSPatrick Sanan     Developer Notes:
79053d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
79063d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
79078320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
79088320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
79098320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
79103d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
79113d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
79128320bc6fSPatrick Sanan 
79138320bc6fSPatrick Sanan     Level: advanced
79148320bc6fSPatrick Sanan 
79153d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
79168320bc6fSPatrick Sanan @*/
79178320bc6fSPatrick Sanan 
79188320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
79198320bc6fSPatrick Sanan {
79208320bc6fSPatrick Sanan   PetscErrorCode ierr;
79218320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
79228320bc6fSPatrick Sanan   DMType         type,type2;
79238320bc6fSPatrick Sanan   PetscBool      sameType;
79248320bc6fSPatrick Sanan 
79258320bc6fSPatrick Sanan   PetscFunctionBegin;
79268320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79278320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
79288320bc6fSPatrick Sanan 
79298320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
79308320bc6fSPatrick Sanan   if (dm == dm2) {
79318320bc6fSPatrick Sanan     *set = PETSC_TRUE;
79328320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
79338320bc6fSPatrick Sanan     PetscFunctionReturn(0);
79348320bc6fSPatrick Sanan   }
79358320bc6fSPatrick Sanan 
79368320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
79378320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
79388320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
79398320bc6fSPatrick Sanan      determined by the implementation-specific logic */
79408320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
79418320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
79428320bc6fSPatrick Sanan     *set = PETSC_TRUE;
79438320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
79448320bc6fSPatrick Sanan     PetscFunctionReturn(0);
79458320bc6fSPatrick Sanan   }
79468320bc6fSPatrick Sanan 
79478320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
79488320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
79498320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
79508320bc6fSPatrick Sanan     if (*set) {
79518320bc6fSPatrick Sanan       PetscFunctionReturn(0);
79528320bc6fSPatrick Sanan     }
79538320bc6fSPatrick Sanan   }
79548320bc6fSPatrick Sanan 
79558320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
79568320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
79578320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
79588320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
79598320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
79608320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
79618320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
79628320bc6fSPatrick Sanan   } else {
79638320bc6fSPatrick Sanan     *set = PETSC_FALSE;
79648320bc6fSPatrick Sanan   }
79658320bc6fSPatrick Sanan   PetscFunctionReturn(0);
79668320bc6fSPatrick Sanan }
7967