xref: /petsc/src/dm/interface/dm.c (revision b3cf3223eed54830957291c4a77c2bf4e49647a5)
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);
84*b3cf3223SMatthew 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);
7836636e97aSMatthew G Knepley 
784e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
78514f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
786e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
787e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
788732e2eb9SMatthew G Knepley 
789ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7906bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
791ed3c66a1SDave May   }
792435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
793732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
79447c6ae99SBarry Smith   PetscFunctionReturn(0);
79547c6ae99SBarry Smith }
79647c6ae99SBarry Smith 
797d7bf68aeSBarry Smith /*@
798d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
799d7bf68aeSBarry Smith 
800d7bf68aeSBarry Smith     Collective on DM
801d7bf68aeSBarry Smith 
802d7bf68aeSBarry Smith     Input Parameter:
803d7bf68aeSBarry Smith .   dm - the DM object to setup
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith     Level: developer
806d7bf68aeSBarry Smith 
807e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
808d7bf68aeSBarry Smith 
809d7bf68aeSBarry Smith @*/
8107087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
811d7bf68aeSBarry Smith {
812d7bf68aeSBarry Smith   PetscErrorCode ierr;
813d7bf68aeSBarry Smith 
814d7bf68aeSBarry Smith   PetscFunctionBegin;
815171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8168387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
817d7bf68aeSBarry Smith   if (dm->ops->setup) {
818d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
819d7bf68aeSBarry Smith   }
8208387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
821d7bf68aeSBarry Smith   PetscFunctionReturn(0);
822d7bf68aeSBarry Smith }
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith /*@
825d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
826d7bf68aeSBarry Smith 
827d7bf68aeSBarry Smith     Collective on DM
828d7bf68aeSBarry Smith 
829d7bf68aeSBarry Smith     Input Parameter:
830d7bf68aeSBarry Smith .   dm - the DM object to set options for
831d7bf68aeSBarry Smith 
832732e2eb9SMatthew G Knepley     Options Database:
8334164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8344164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8354164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8368f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
837732e2eb9SMatthew G Knepley 
838d7bf68aeSBarry Smith     Level: developer
839d7bf68aeSBarry Smith 
840e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
841d7bf68aeSBarry Smith 
842d7bf68aeSBarry Smith @*/
8437087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
844d7bf68aeSBarry Smith {
8457781c08eSBarry Smith   char           typeName[256];
846ca266f36SBarry Smith   PetscBool      flg;
847d7bf68aeSBarry Smith   PetscErrorCode ierr;
848d7bf68aeSBarry Smith 
849d7bf68aeSBarry Smith   PetscFunctionBegin;
850171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85149be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
85249be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
85349be4549SMatthew G. Knepley   if (dm->defaultSF) {ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);}
8543194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8550298fd71SBarry 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);
856a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
857f9ba7244SBarry Smith   if (flg) {
858f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
859f9ba7244SBarry Smith   }
860a264d7a6SBarry 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);
861073dac72SJed Brown   if (flg) {
862521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
863073dac72SJed Brown   }
8648f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
865f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
866e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
867f9ba7244SBarry Smith   }
868f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8690633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
87082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
871d7bf68aeSBarry Smith   PetscFunctionReturn(0);
872d7bf68aeSBarry Smith }
873d7bf68aeSBarry Smith 
874fc9bc008SSatish Balay /*@C
875224748a4SBarry Smith     DMView - Views a DM
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith     Collective on DM
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith     Input Parameter:
88047c6ae99SBarry Smith +   dm - the DM object to view
88147c6ae99SBarry Smith -   v - the viewer
88247c6ae99SBarry Smith 
883224748a4SBarry Smith     Level: beginner
88447c6ae99SBarry Smith 
885e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith @*/
8887087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
88947c6ae99SBarry Smith {
89047c6ae99SBarry Smith   PetscErrorCode    ierr;
89132c0f0efSBarry Smith   PetscBool         isbinary;
89276a8abe0SBarry Smith   PetscMPIInt       size;
89376a8abe0SBarry Smith   PetscViewerFormat format;
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith   PetscFunctionBegin;
896171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8973014e516SBarry Smith   if (!v) {
898ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8993014e516SBarry Smith   }
9008bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
90176a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
90276a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
90376a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
90498c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
90532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
90632c0f0efSBarry Smith   if (isbinary) {
90755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
90832c0f0efSBarry Smith     char     type[256];
90932c0f0efSBarry Smith 
91032c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
91132c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
91232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
91332c0f0efSBarry Smith   }
9140c010503SBarry Smith   if (dm->ops->view) {
9150c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
91647c6ae99SBarry Smith   }
91747c6ae99SBarry Smith   PetscFunctionReturn(0);
91847c6ae99SBarry Smith }
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith /*@
9218472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
92247c6ae99SBarry Smith 
92347c6ae99SBarry Smith     Collective on DM
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith     Input Parameter:
92647c6ae99SBarry Smith .   dm - the DM object
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith     Output Parameter:
92947c6ae99SBarry Smith .   vec - the global vector
93047c6ae99SBarry Smith 
931073dac72SJed Brown     Level: beginner
93247c6ae99SBarry Smith 
933e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith @*/
9367087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
93747c6ae99SBarry Smith {
93847c6ae99SBarry Smith   PetscErrorCode ierr;
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith   PetscFunctionBegin;
941171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
94247c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
94347c6ae99SBarry Smith   PetscFunctionReturn(0);
94447c6ae99SBarry Smith }
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith /*@
9478472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith     Not Collective
95047c6ae99SBarry Smith 
95147c6ae99SBarry Smith     Input Parameter:
95247c6ae99SBarry Smith .   dm - the DM object
95347c6ae99SBarry Smith 
95447c6ae99SBarry Smith     Output Parameter:
95547c6ae99SBarry Smith .   vec - the local vector
95647c6ae99SBarry Smith 
957073dac72SJed Brown     Level: beginner
95847c6ae99SBarry Smith 
959e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith @*/
9627087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
96347c6ae99SBarry Smith {
96447c6ae99SBarry Smith   PetscErrorCode ierr;
96547c6ae99SBarry Smith 
96647c6ae99SBarry Smith   PetscFunctionBegin;
967171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
96847c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
96947c6ae99SBarry Smith   PetscFunctionReturn(0);
97047c6ae99SBarry Smith }
97147c6ae99SBarry Smith 
9721411c6eeSJed Brown /*@
9731411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9741411c6eeSJed Brown 
9751411c6eeSJed Brown    Collective on DM
9761411c6eeSJed Brown 
9771411c6eeSJed Brown    Input Parameter:
9781411c6eeSJed Brown .  dm - the DM that provides the mapping
9791411c6eeSJed Brown 
9801411c6eeSJed Brown    Output Parameter:
9811411c6eeSJed Brown .  ltog - the mapping
9821411c6eeSJed Brown 
9831411c6eeSJed Brown    Level: intermediate
9841411c6eeSJed Brown 
9851411c6eeSJed Brown    Notes:
9861411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9871411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9881411c6eeSJed Brown 
989fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9901411c6eeSJed Brown @*/
9917087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9921411c6eeSJed Brown {
9930be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9941411c6eeSJed Brown   PetscErrorCode ierr;
9951411c6eeSJed Brown 
9961411c6eeSJed Brown   PetscFunctionBegin;
9971411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9981411c6eeSJed Brown   PetscValidPointer(ltog,2);
9991411c6eeSJed Brown   if (!dm->ltogmap) {
100037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
100137d0c07bSMatthew G Knepley 
1002e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
100337d0c07bSMatthew G Knepley     if (section) {
1004a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
100537d0c07bSMatthew G Knepley       PetscInt       *ltog;
1006ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
100737d0c07bSMatthew G Knepley 
1008e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
100937d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1010ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1011ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
101237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1013a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
101437d0c07bSMatthew G Knepley 
101537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
101637d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1017a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1018a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
101937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10201a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10211a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10221a7dc684SMatthew G. Knepley         if (dof) {
1023b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1024b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10251a7dc684SMatthew G. Knepley         }
102637d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1027a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1028a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
102937d0c07bSMatthew G Knepley         }
103037d0c07bSMatthew G Knepley       }
1031bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10320be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10330be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10340be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10350be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10367591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10377591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1038ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1039ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1040ccf3bd66SMatthew G. Knepley         n /= bs;
1041ccf3bd66SMatthew G. Knepley       }
1042ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10433bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
104437d0c07bSMatthew G Knepley     } else {
1045184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1046184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10471411c6eeSJed Brown     }
104837d0c07bSMatthew G Knepley   }
10491411c6eeSJed Brown   *ltog = dm->ltogmap;
10501411c6eeSJed Brown   PetscFunctionReturn(0);
10511411c6eeSJed Brown }
10521411c6eeSJed Brown 
10531411c6eeSJed Brown /*@
10541411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10551411c6eeSJed Brown 
10561411c6eeSJed Brown    Not Collective
10571411c6eeSJed Brown 
10581411c6eeSJed Brown    Input Parameter:
10591411c6eeSJed Brown .  dm - the DM with block structure
10601411c6eeSJed Brown 
10611411c6eeSJed Brown    Output Parameter:
10621411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10631411c6eeSJed Brown 
10641411c6eeSJed Brown    Level: intermediate
10651411c6eeSJed Brown 
1066fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10671411c6eeSJed Brown @*/
10687087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10691411c6eeSJed Brown {
10701411c6eeSJed Brown   PetscFunctionBegin;
10711411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10721411c6eeSJed Brown   PetscValidPointer(bs,2);
10731411c6eeSJed 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");
10741411c6eeSJed Brown   *bs = dm->bs;
10751411c6eeSJed Brown   PetscFunctionReturn(0);
10761411c6eeSJed Brown }
10771411c6eeSJed Brown 
107847c6ae99SBarry Smith /*@
10798472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
108047c6ae99SBarry Smith 
108147c6ae99SBarry Smith     Collective on DM
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith     Input Parameter:
108447c6ae99SBarry Smith +   dm1 - the DM object
108547c6ae99SBarry Smith -   dm2 - the second, finer DM object
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith     Output Parameter:
108847c6ae99SBarry Smith +  mat - the interpolation
108947c6ae99SBarry Smith -  vec - the scaling (optional)
109047c6ae99SBarry Smith 
109147c6ae99SBarry Smith     Level: developer
109247c6ae99SBarry Smith 
109395452b02SPatrick Sanan     Notes:
109495452b02SPatrick 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
109585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1096d52bd9f3SBarry Smith 
10971f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1098d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
109985afcc9aSBarry Smith 
110085afcc9aSBarry Smith 
11013ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
110247c6ae99SBarry Smith 
110347c6ae99SBarry Smith @*/
1104e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
110547c6ae99SBarry Smith {
110647c6ae99SBarry Smith   PetscErrorCode ierr;
110747c6ae99SBarry Smith 
110847c6ae99SBarry Smith   PetscFunctionBegin;
1109171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1110171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1111c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1112c7d20fa0SStefano Zampini   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInterpolation not implemented for type %s",((PetscObject)dm1)->type_name);
1113cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111425296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1115cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111647c6ae99SBarry Smith   PetscFunctionReturn(0);
111747c6ae99SBarry Smith }
111847c6ae99SBarry Smith 
11193ad4599aSBarry Smith /*@
11203ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11213ad4599aSBarry Smith 
11223ad4599aSBarry Smith     Collective on DM
11233ad4599aSBarry Smith 
11243ad4599aSBarry Smith     Input Parameter:
11253ad4599aSBarry Smith +   dm1 - the DM object
11263ad4599aSBarry Smith -   dm2 - the second, finer DM object
11273ad4599aSBarry Smith 
11283ad4599aSBarry Smith     Output Parameter:
11293ad4599aSBarry Smith .  mat - the restriction
11303ad4599aSBarry Smith 
11313ad4599aSBarry Smith 
11323ad4599aSBarry Smith     Level: developer
11333ad4599aSBarry Smith 
113495452b02SPatrick Sanan     Notes:
113595452b02SPatrick 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
11363ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11373ad4599aSBarry Smith 
11383ad4599aSBarry Smith 
11393ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11403ad4599aSBarry Smith 
11413ad4599aSBarry Smith @*/
11423ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11433ad4599aSBarry Smith {
11443ad4599aSBarry Smith   PetscErrorCode ierr;
11453ad4599aSBarry Smith 
11463ad4599aSBarry Smith   PetscFunctionBegin;
11473ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11483ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11493ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11507294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11513ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11523ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11533ad4599aSBarry Smith   PetscFunctionReturn(0);
11543ad4599aSBarry Smith }
11553ad4599aSBarry Smith 
115647c6ae99SBarry Smith /*@
11578472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
115847c6ae99SBarry Smith 
115947c6ae99SBarry Smith     Collective on DM
116047c6ae99SBarry Smith 
116147c6ae99SBarry Smith     Input Parameter:
116247c6ae99SBarry Smith +   dm1 - the DM object
116347c6ae99SBarry Smith -   dm2 - the second, finer DM object
116447c6ae99SBarry Smith 
116547c6ae99SBarry Smith     Output Parameter:
11666dbf9973SLawrence Mitchell .   mat - the injection
116747c6ae99SBarry Smith 
116847c6ae99SBarry Smith     Level: developer
116947c6ae99SBarry Smith 
117095452b02SPatrick Sanan    Notes:
117195452b02SPatrick 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
117285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
117385afcc9aSBarry Smith 
1174e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
117547c6ae99SBarry Smith 
117647c6ae99SBarry Smith @*/
11776dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
117847c6ae99SBarry Smith {
117947c6ae99SBarry Smith   PetscErrorCode ierr;
118047c6ae99SBarry Smith 
118147c6ae99SBarry Smith   PetscFunctionBegin;
1182171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1183171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11840c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11856dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
118647c6ae99SBarry Smith   PetscFunctionReturn(0);
118747c6ae99SBarry Smith }
118847c6ae99SBarry Smith 
1189b412c318SBarry Smith /*@
1190bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1191bd041c0cSMatthew G. Knepley 
1192bd041c0cSMatthew G. Knepley   Collective on DM
1193bd041c0cSMatthew G. Knepley 
1194bd041c0cSMatthew G. Knepley   Input Parameter:
1195bd041c0cSMatthew G. Knepley + dm1 - the DM object
1196bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1197bd041c0cSMatthew G. Knepley 
1198bd041c0cSMatthew G. Knepley   Output Parameter:
1199bd041c0cSMatthew G. Knepley . mat - the interpolation
1200bd041c0cSMatthew G. Knepley 
1201bd041c0cSMatthew G. Knepley   Level: developer
1202bd041c0cSMatthew G. Knepley 
1203bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1204bd041c0cSMatthew G. Knepley @*/
1205bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1206bd041c0cSMatthew G. Knepley {
1207bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1208bd041c0cSMatthew G. Knepley 
1209bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1210bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1211bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1212bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1213bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1214bd041c0cSMatthew G. Knepley }
1215bd041c0cSMatthew G. Knepley 
1216bd041c0cSMatthew G. Knepley /*@
1217b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
121847c6ae99SBarry Smith 
121947c6ae99SBarry Smith     Collective on DM
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith     Input Parameter:
122247c6ae99SBarry Smith +   dm - the DM object
12235bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith     Output Parameter:
122647c6ae99SBarry Smith .   coloring - the coloring
122747c6ae99SBarry Smith 
122847c6ae99SBarry Smith     Level: developer
122947c6ae99SBarry Smith 
1230b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
123147c6ae99SBarry Smith 
1232aab9d709SJed Brown @*/
1233b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
123447c6ae99SBarry Smith {
123547c6ae99SBarry Smith   PetscErrorCode ierr;
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith   PetscFunctionBegin;
1238171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1239ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1240b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
124147c6ae99SBarry Smith   PetscFunctionReturn(0);
124247c6ae99SBarry Smith }
124347c6ae99SBarry Smith 
1244b412c318SBarry Smith /*@
12458472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
124647c6ae99SBarry Smith 
124747c6ae99SBarry Smith     Collective on DM
124847c6ae99SBarry Smith 
124947c6ae99SBarry Smith     Input Parameter:
1250b412c318SBarry Smith .   dm - the DM object
125147c6ae99SBarry Smith 
125247c6ae99SBarry Smith     Output Parameter:
125347c6ae99SBarry Smith .   mat - the empty Jacobian
125447c6ae99SBarry Smith 
1255073dac72SJed Brown     Level: beginner
125647c6ae99SBarry Smith 
125795452b02SPatrick Sanan     Notes:
125895452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
125994013140SBarry Smith        do not need to do it yourself.
126094013140SBarry Smith 
126194013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12627889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
126394013140SBarry Smith 
126494013140SBarry 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
126594013140SBarry Smith        internally by PETSc.
126694013140SBarry Smith 
126794013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1268aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
126994013140SBarry Smith 
1270b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
127147c6ae99SBarry Smith 
1272aab9d709SJed Brown @*/
1273b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
127447c6ae99SBarry Smith {
127547c6ae99SBarry Smith   PetscErrorCode ierr;
127647c6ae99SBarry Smith 
127747c6ae99SBarry Smith   PetscFunctionBegin;
1278171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1279607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1280c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1281c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1282b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1283e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1284e5e52638SMatthew G. Knepley   if (dm->Nf) {
1285e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1286e571a35bSMatthew G. Knepley     PetscInt     Nf;
1287e571a35bSMatthew G. Knepley 
1288e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1289e571a35bSMatthew G. Knepley     if (Nf == 1) {
1290e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1291e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1292e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1293e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1294e571a35bSMatthew G. Knepley       }
1295e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1296e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1297e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1298e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1299e571a35bSMatthew G. Knepley       }
1300e571a35bSMatthew G. Knepley     }
1301e571a35bSMatthew G. Knepley   }
130247c6ae99SBarry Smith   PetscFunctionReturn(0);
130347c6ae99SBarry Smith }
130447c6ae99SBarry Smith 
1305732e2eb9SMatthew G Knepley /*@
1306950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1307732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1308732e2eb9SMatthew G Knepley 
13098472ad0fSDave May   Logically Collective on DM
1310732e2eb9SMatthew G Knepley 
1311732e2eb9SMatthew G Knepley   Input Parameter:
1312732e2eb9SMatthew G Knepley + dm - the DM
1313732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1314732e2eb9SMatthew G Knepley 
1315732e2eb9SMatthew G Knepley   Level: developer
1316b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1317732e2eb9SMatthew G Knepley @*/
1318732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1319732e2eb9SMatthew G Knepley {
1320732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1321732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1322732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1323732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1324732e2eb9SMatthew G Knepley }
1325732e2eb9SMatthew G Knepley 
1326b06ff27eSHong Zhang /*@
1327b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1328b06ff27eSHong Zhang     but the array for values will not be allocated.
1329b06ff27eSHong Zhang 
1330b06ff27eSHong Zhang   Logically Collective on DM
1331b06ff27eSHong Zhang 
1332b06ff27eSHong Zhang   Input Parameter:
1333b06ff27eSHong Zhang + dm - the DM
1334b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1335b06ff27eSHong Zhang 
1336b06ff27eSHong Zhang   Level: developer
1337b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1338b06ff27eSHong Zhang @*/
1339b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1340b06ff27eSHong Zhang {
1341b06ff27eSHong Zhang   PetscFunctionBegin;
1342b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1343b06ff27eSHong Zhang   dm->structure_only = only;
1344b06ff27eSHong Zhang   PetscFunctionReturn(0);
1345b06ff27eSHong Zhang }
1346b06ff27eSHong Zhang 
1347a89ea682SMatthew G Knepley /*@C
1348aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1349a89ea682SMatthew G Knepley 
1350a89ea682SMatthew G Knepley   Not Collective
1351a89ea682SMatthew G Knepley 
1352a89ea682SMatthew G Knepley   Input Parameters:
1353a89ea682SMatthew G Knepley + dm - the DM object
1354aa1993deSMatthew G Knepley . count - The minium size
135569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1356a89ea682SMatthew G Knepley 
1357a89ea682SMatthew G Knepley   Output Parameter:
1358a89ea682SMatthew G Knepley . array - the work array
1359a89ea682SMatthew G Knepley 
1360a89ea682SMatthew G Knepley   Level: developer
1361a89ea682SMatthew G Knepley 
1362a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1363a89ea682SMatthew G Knepley @*/
136469291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1365a89ea682SMatthew G Knepley {
1366a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1367aa1993deSMatthew G Knepley   DMWorkLink     link;
136869291d52SBarry Smith   PetscMPIInt    dsize;
1369a89ea682SMatthew G Knepley 
1370a89ea682SMatthew G Knepley   PetscFunctionBegin;
1371a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1372aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1373aa1993deSMatthew G Knepley   if (dm->workin) {
1374aa1993deSMatthew G Knepley     link       = dm->workin;
1375aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1376aa1993deSMatthew G Knepley   } else {
1377b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1378a89ea682SMatthew G Knepley   }
137969291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13805056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1381aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1382854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1383854ce69bSBarry Smith     link->bytes = dsize*count;
1384aa1993deSMatthew G Knepley   }
1385aa1993deSMatthew G Knepley   link->next   = dm->workout;
1386aa1993deSMatthew G Knepley   dm->workout  = link;
1387aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1388a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1389a89ea682SMatthew G Knepley }
1390a89ea682SMatthew G Knepley 
1391aa1993deSMatthew G Knepley /*@C
1392aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1393aa1993deSMatthew G Knepley 
1394aa1993deSMatthew G Knepley   Not Collective
1395aa1993deSMatthew G Knepley 
1396aa1993deSMatthew G Knepley   Input Parameters:
1397aa1993deSMatthew G Knepley + dm - the DM object
1398aa1993deSMatthew G Knepley . count - The minium size
139969291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1400aa1993deSMatthew G Knepley 
1401aa1993deSMatthew G Knepley   Output Parameter:
1402aa1993deSMatthew G Knepley . array - the work array
1403aa1993deSMatthew G Knepley 
1404aa1993deSMatthew G Knepley   Level: developer
1405aa1993deSMatthew G Knepley 
140695452b02SPatrick Sanan   Developer Notes:
140795452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1408aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1409aa1993deSMatthew G Knepley @*/
141069291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1411aa1993deSMatthew G Knepley {
1412aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1413aa1993deSMatthew G Knepley 
1414aa1993deSMatthew G Knepley   PetscFunctionBegin;
1415aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1416aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1417aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1418aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1419aa1993deSMatthew G Knepley       *p           = link->next;
1420aa1993deSMatthew G Knepley       link->next   = dm->workin;
1421aa1993deSMatthew G Knepley       dm->workin   = link;
14220298fd71SBarry Smith       *(void**)mem = NULL;
1423aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1424aa1993deSMatthew G Knepley     }
1425aa1993deSMatthew G Knepley   }
1426aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1427aa1993deSMatthew G Knepley }
1428e7c4fc90SDmitry Karpeev 
1429435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1430435a35e8SMatthew G Knepley {
1431435a35e8SMatthew G Knepley   PetscFunctionBegin;
1432435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
143382f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1434435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1435435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1436435a35e8SMatthew G Knepley }
1437435a35e8SMatthew G Knepley 
14380a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14390a50eb56SMatthew G. Knepley {
14400a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14410a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1442f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14430a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14440a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14450a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14460a50eb56SMatthew G. Knepley }
14470a50eb56SMatthew G. Knepley 
1448f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1449f9d4088aSMatthew G. Knepley {
1450f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1451f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1452f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1453f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1454f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1455f9d4088aSMatthew G. Knepley }
1456f9d4088aSMatthew G. Knepley 
1457f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1458f9d4088aSMatthew G. Knepley {
1459f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1460f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1461f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1462f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1463f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1464f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1465f9d4088aSMatthew G. Knepley }
1466f9d4088aSMatthew G. Knepley 
14674f3b5142SJed Brown /*@C
14684d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14694d343eeaSMatthew G Knepley 
14704d343eeaSMatthew G Knepley   Not collective
14714d343eeaSMatthew G Knepley 
14724d343eeaSMatthew G Knepley   Input Parameter:
14734d343eeaSMatthew G Knepley . dm - the DM object
14744d343eeaSMatthew G Knepley 
14754d343eeaSMatthew G Knepley   Output Parameters:
14760298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14770298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14780298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14794d343eeaSMatthew G Knepley 
14804d343eeaSMatthew G Knepley   Level: intermediate
14814d343eeaSMatthew G Knepley 
148221c9b008SJed Brown   Notes:
148321c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
148421c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
148521c9b008SJed Brown   PetscFree().
148621c9b008SJed Brown 
14874d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14884d343eeaSMatthew G Knepley @*/
148937d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14904d343eeaSMatthew G Knepley {
149137d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14924d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14934d343eeaSMatthew G Knepley 
14944d343eeaSMatthew G Knepley   PetscFunctionBegin;
14954d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
149669ca1f37SDmitry Karpeev   if (numFields) {
149769ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
149869ca1f37SDmitry Karpeev     *numFields = 0;
149969ca1f37SDmitry Karpeev   }
150037d0c07bSMatthew G Knepley   if (fieldNames) {
150137d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15020298fd71SBarry Smith     *fieldNames = NULL;
150369ca1f37SDmitry Karpeev   }
150469ca1f37SDmitry Karpeev   if (fields) {
150569ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15060298fd71SBarry Smith     *fields = NULL;
150769ca1f37SDmitry Karpeev   }
1508e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
150937d0c07bSMatthew G Knepley   if (section) {
15103a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
151137d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
151237d0c07bSMatthew G Knepley 
1513e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
151437d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15153a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
151637d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
151737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
151837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15193a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
152037d0c07bSMatthew G Knepley     }
152137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
152237d0c07bSMatthew G Knepley       PetscInt gdof;
152337d0c07bSMatthew G Knepley 
152437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
152537d0c07bSMatthew G Knepley       if (gdof > 0) {
152637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15273a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
152837d0c07bSMatthew G Knepley 
152937d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
153037d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15313a544194SStefano Zampini           fpdof = fdof-fcdof;
15323a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15333a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15343a544194SStefano Zampini             fieldNc[f] = 1;
15353a544194SStefano Zampini           }
15363a544194SStefano Zampini           fieldSizes[f] += fpdof;
153737d0c07bSMatthew G Knepley         }
153837d0c07bSMatthew G Knepley       }
153937d0c07bSMatthew G Knepley     }
154037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1541785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
154237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
154337d0c07bSMatthew G Knepley     }
154437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
154537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
154637d0c07bSMatthew G Knepley 
154737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
154837d0c07bSMatthew G Knepley       if (gdof > 0) {
154937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
155037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
155137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
155237d0c07bSMatthew G Knepley 
155337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
155437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
155537d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
155637d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
155737d0c07bSMatthew G Knepley           }
155837d0c07bSMatthew G Knepley         }
155937d0c07bSMatthew G Knepley       }
156037d0c07bSMatthew G Knepley     }
15618865f1eaSKarl Rupp     if (numFields) *numFields = nF;
156237d0c07bSMatthew G Knepley     if (fieldNames) {
1563785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
156437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
156537d0c07bSMatthew G Knepley         const char *fieldName;
156637d0c07bSMatthew G Knepley 
156737d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
156837d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
156937d0c07bSMatthew G Knepley       }
157037d0c07bSMatthew G Knepley     }
157137d0c07bSMatthew G Knepley     if (fields) {
1572785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
157337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15743a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15753a544194SStefano Zampini 
157682f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15773a544194SStefano Zampini         in[0] = -fieldNc[f];
15783a544194SStefano Zampini         in[1] = fieldNc[f];
15793a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15803a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15813a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
158237d0c07bSMatthew G Knepley       }
158337d0c07bSMatthew G Knepley     }
15843a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15858865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15868865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
158769ca1f37SDmitry Karpeev   }
15884d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15894d343eeaSMatthew G Knepley }
15904d343eeaSMatthew G Knepley 
159116621825SDmitry Karpeev 
159216621825SDmitry Karpeev /*@C
159316621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
159416621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
159516621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1596e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1597e7c4fc90SDmitry Karpeev 
1598e7c4fc90SDmitry Karpeev   Not collective
1599e7c4fc90SDmitry Karpeev 
1600e7c4fc90SDmitry Karpeev   Input Parameter:
1601e7c4fc90SDmitry Karpeev . dm - the DM object
1602e7c4fc90SDmitry Karpeev 
1603e7c4fc90SDmitry Karpeev   Output Parameters:
16040298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16050298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16060298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16070298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1608e7c4fc90SDmitry Karpeev 
1609e7c4fc90SDmitry Karpeev   Level: intermediate
1610e7c4fc90SDmitry Karpeev 
1611e7c4fc90SDmitry Karpeev   Notes:
1612e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1613e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1614e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1615e7c4fc90SDmitry Karpeev 
1616e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1617e7c4fc90SDmitry Karpeev @*/
161816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1619e7c4fc90SDmitry Karpeev {
1620e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1621e7c4fc90SDmitry Karpeev 
1622e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1623e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16248865f1eaSKarl Rupp   if (len) {
16258865f1eaSKarl Rupp     PetscValidPointer(len,2);
16268865f1eaSKarl Rupp     *len = 0;
16278865f1eaSKarl Rupp   }
16288865f1eaSKarl Rupp   if (namelist) {
16298865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16308865f1eaSKarl Rupp     *namelist = 0;
16318865f1eaSKarl Rupp   }
16328865f1eaSKarl Rupp   if (islist) {
16338865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16348865f1eaSKarl Rupp     *islist = 0;
16358865f1eaSKarl Rupp   }
16368865f1eaSKarl Rupp   if (dmlist) {
16378865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16388865f1eaSKarl Rupp     *dmlist = 0;
16398865f1eaSKarl Rupp   }
1640f3f0edfdSDmitry Karpeev   /*
1641f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1642f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1643f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1644f3f0edfdSDmitry Karpeev    */
1645ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
164616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1647435a35e8SMatthew G Knepley     PetscSection section;
1648435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1649435a35e8SMatthew G Knepley 
1650e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1651435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1652435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1653f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
165403dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
165503dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
165603dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1657435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1658435a35e8SMatthew G Knepley         const char *fieldName;
1659435a35e8SMatthew G Knepley 
166003dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
166103dc3394SMatthew G. Knepley         if (namelist) {
1662435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1663435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1664435a35e8SMatthew G Knepley         }
166503dc3394SMatthew G. Knepley       }
1666435a35e8SMatthew G Knepley     } else {
166769ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1668e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16690298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1670e7c4fc90SDmitry Karpeev     }
16718865f1eaSKarl Rupp   } else {
167216621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
167316621825SDmitry Karpeev   }
167416621825SDmitry Karpeev   PetscFunctionReturn(0);
167516621825SDmitry Karpeev }
167616621825SDmitry Karpeev 
1677564cec59SMatthew G. Knepley /*@
1678435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1679435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1680435a35e8SMatthew G Knepley 
1681435a35e8SMatthew G Knepley   Not collective
1682435a35e8SMatthew G Knepley 
1683435a35e8SMatthew G Knepley   Input Parameters:
16842adcc780SMatthew G. Knepley + dm        - The DM object
16852adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16862adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1687435a35e8SMatthew G Knepley 
1688435a35e8SMatthew G Knepley   Output Parameters:
16892adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16902adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1691435a35e8SMatthew G Knepley 
16925d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16935d3b26e6SMatthew G. Knepley 
1694435a35e8SMatthew G Knepley   Level: intermediate
1695435a35e8SMatthew G Knepley 
16965d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1697435a35e8SMatthew G Knepley @*/
169837bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1699435a35e8SMatthew G Knepley {
1700435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1701435a35e8SMatthew G Knepley 
1702435a35e8SMatthew G Knepley   PetscFunctionBegin;
1703435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1704435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17058865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17068865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1707435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1708435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
170982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1710435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1711435a35e8SMatthew G Knepley }
1712435a35e8SMatthew G Knepley 
17132adcc780SMatthew G. Knepley /*@C
17142adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17152adcc780SMatthew G. Knepley 
17162adcc780SMatthew G. Knepley   Not collective
17172adcc780SMatthew G. Knepley 
17182adcc780SMatthew G. Knepley   Input Parameter:
17192adcc780SMatthew G. Knepley + dms - The DM objects
17202adcc780SMatthew G. Knepley - len - The number of DMs
17212adcc780SMatthew G. Knepley 
17222adcc780SMatthew G. Knepley   Output Parameters:
1723a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17242adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17252adcc780SMatthew G. Knepley 
17265d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17275d3b26e6SMatthew G. Knepley 
17282adcc780SMatthew G. Knepley   Level: intermediate
17292adcc780SMatthew G. Knepley 
17305d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17312adcc780SMatthew G. Knepley @*/
17322adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17332adcc780SMatthew G. Knepley {
17342adcc780SMatthew G. Knepley   PetscInt       i;
17352adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17362adcc780SMatthew G. Knepley 
17372adcc780SMatthew G. Knepley   PetscFunctionBegin;
17382adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17392adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17402adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1741a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17422adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17432adcc780SMatthew G. Knepley   if (len) {
1744a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1745a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17462adcc780SMatthew G. Knepley   }
17472adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17482adcc780SMatthew G. Knepley }
17492adcc780SMatthew G. Knepley 
175016621825SDmitry Karpeev 
175116621825SDmitry Karpeev /*@C
17528d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17538d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17548d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17558d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17568d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
175716621825SDmitry Karpeev 
175816621825SDmitry Karpeev   Not collective
175916621825SDmitry Karpeev 
176016621825SDmitry Karpeev   Input Parameter:
176116621825SDmitry Karpeev . dm - the DM object
176216621825SDmitry Karpeev 
176316621825SDmitry Karpeev   Output Parameters:
17640298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17650298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17660298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17670298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17680298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
176916621825SDmitry Karpeev 
177016621825SDmitry Karpeev   Level: intermediate
177116621825SDmitry Karpeev 
177216621825SDmitry Karpeev   Notes:
177316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
177416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
177516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
177616621825SDmitry Karpeev 
17778d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
177816621825SDmitry Karpeev @*/
17798d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
178016621825SDmitry Karpeev {
178116621825SDmitry Karpeev   PetscErrorCode      ierr;
1782be081cd6SPeter Brune   DMSubDomainHookLink link;
1783be081cd6SPeter Brune   PetscInt            i,l;
178416621825SDmitry Karpeev 
178516621825SDmitry Karpeev   PetscFunctionBegin;
178616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
178714a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17880298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17890298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17900298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17910298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1792f3f0edfdSDmitry Karpeev   /*
1793f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1794f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1795f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1796f3f0edfdSDmitry Karpeev    */
1797ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
179816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1799be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
180014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1801f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1802be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1803be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1804be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1805be081cd6SPeter Brune         }
1806648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1807e7c4fc90SDmitry Karpeev       }
180814a18fd3SPeter Brune     }
180914a18fd3SPeter Brune     if (len) *len = l;
181014a18fd3SPeter Brune   }
1811e30e807fSPeter Brune   PetscFunctionReturn(0);
1812e30e807fSPeter Brune }
1813e30e807fSPeter Brune 
1814e30e807fSPeter Brune 
1815e30e807fSPeter Brune /*@C
1816e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1817e30e807fSPeter Brune 
1818e30e807fSPeter Brune   Not collective
1819e30e807fSPeter Brune 
1820e30e807fSPeter Brune   Input Parameters:
1821e30e807fSPeter Brune + dm - the DM object
1822e30e807fSPeter Brune . n  - the number of subdomain scatters
1823e30e807fSPeter Brune - subdms - the local subdomains
1824e30e807fSPeter Brune 
1825e30e807fSPeter Brune   Output Parameters:
1826e30e807fSPeter Brune + n     - the number of scatters returned
1827e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1828e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1829e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1830e30e807fSPeter Brune 
183195452b02SPatrick Sanan   Notes:
183295452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1833e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1834e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1835e30e807fSPeter Brune   solution and residual data.
1836e30e807fSPeter Brune 
1837e30e807fSPeter Brune   Level: developer
1838e30e807fSPeter Brune 
1839e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1840e30e807fSPeter Brune @*/
1841e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1842e30e807fSPeter Brune {
1843e30e807fSPeter Brune   PetscErrorCode ierr;
1844e30e807fSPeter Brune 
1845e30e807fSPeter Brune   PetscFunctionBegin;
1846e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1847e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1848e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1849e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18506668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1851e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1852e7c4fc90SDmitry Karpeev }
1853e7c4fc90SDmitry Karpeev 
185447c6ae99SBarry Smith /*@
185547c6ae99SBarry Smith   DMRefine - Refines a DM object
185647c6ae99SBarry Smith 
185747c6ae99SBarry Smith   Collective on DM
185847c6ae99SBarry Smith 
185947c6ae99SBarry Smith   Input Parameter:
186047c6ae99SBarry Smith + dm   - the DM object
186191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
186247c6ae99SBarry Smith 
186347c6ae99SBarry Smith   Output Parameter:
18640298fd71SBarry Smith . dmf - the refined DM, or NULL
1865ae0a1c52SMatthew G Knepley 
18660298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
186747c6ae99SBarry Smith 
186847c6ae99SBarry Smith   Level: developer
186947c6ae99SBarry Smith 
1870e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
187147c6ae99SBarry Smith @*/
18727087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
187347c6ae99SBarry Smith {
187447c6ae99SBarry Smith   PetscErrorCode   ierr;
1875c833c3b5SJed Brown   DMRefineHookLink link;
187647c6ae99SBarry Smith 
187747c6ae99SBarry Smith   PetscFunctionBegin;
1878732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18791ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1880fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
188147c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18824057135bSMatthew G Knepley   if (*dmf) {
188343842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18848865f1eaSKarl Rupp 
18858cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18868865f1eaSKarl Rupp 
1887644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18880598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1889656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18908865f1eaSKarl Rupp 
1891e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1892c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18938865f1eaSKarl Rupp       if (link->refinehook) {
18948865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18958865f1eaSKarl Rupp       }
1896c833c3b5SJed Brown     }
1897c833c3b5SJed Brown   }
18981ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1899c833c3b5SJed Brown   PetscFunctionReturn(0);
1900c833c3b5SJed Brown }
1901c833c3b5SJed Brown 
1902bb9467b5SJed Brown /*@C
1903c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1904c833c3b5SJed Brown 
1905c833c3b5SJed Brown    Logically Collective
1906c833c3b5SJed Brown 
1907c833c3b5SJed Brown    Input Arguments:
1908c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1909c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1910c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19110298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1912c833c3b5SJed Brown 
1913c833c3b5SJed Brown    Calling sequence of refinehook:
1914c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1915c833c3b5SJed Brown 
1916c833c3b5SJed Brown +  coarse - coarse level DM
1917c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1918c833c3b5SJed Brown -  ctx - optional user-defined function context
1919c833c3b5SJed Brown 
1920c833c3b5SJed Brown    Calling sequence for interphook:
1921c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1922c833c3b5SJed Brown 
1923c833c3b5SJed Brown +  coarse - coarse level DM
1924c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1925c833c3b5SJed Brown .  fine - fine level DM to update
1926c833c3b5SJed Brown -  ctx - optional user-defined function context
1927c833c3b5SJed Brown 
1928c833c3b5SJed Brown    Level: advanced
1929c833c3b5SJed Brown 
1930c833c3b5SJed Brown    Notes:
1931c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1932c833c3b5SJed Brown 
1933c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1934c833c3b5SJed Brown 
1935bb9467b5SJed Brown    This function is currently not available from Fortran.
1936bb9467b5SJed Brown 
1937c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1938c833c3b5SJed Brown @*/
1939c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1940c833c3b5SJed Brown {
1941c833c3b5SJed Brown   PetscErrorCode   ierr;
1942c833c3b5SJed Brown   DMRefineHookLink link,*p;
1943c833c3b5SJed Brown 
1944c833c3b5SJed Brown   PetscFunctionBegin;
1945c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19463d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19473d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19483d8e3701SJed Brown   }
194995dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1950c833c3b5SJed Brown   link->refinehook = refinehook;
1951c833c3b5SJed Brown   link->interphook = interphook;
1952c833c3b5SJed Brown   link->ctx        = ctx;
19530298fd71SBarry Smith   link->next       = NULL;
1954c833c3b5SJed Brown   *p               = link;
1955c833c3b5SJed Brown   PetscFunctionReturn(0);
1956c833c3b5SJed Brown }
1957c833c3b5SJed Brown 
19583d8e3701SJed Brown /*@C
19593d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19603d8e3701SJed Brown 
19613d8e3701SJed Brown    Logically Collective
19623d8e3701SJed Brown 
19633d8e3701SJed Brown    Input Arguments:
19643d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19653d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19663d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19673d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19683d8e3701SJed Brown 
19693d8e3701SJed Brown    Level: advanced
19703d8e3701SJed Brown 
19713d8e3701SJed Brown    Notes:
19723d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19733d8e3701SJed Brown 
19743d8e3701SJed Brown    This function is currently not available from Fortran.
19753d8e3701SJed Brown 
19763d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19773d8e3701SJed Brown @*/
19783d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19793d8e3701SJed Brown {
19803d8e3701SJed Brown   PetscErrorCode   ierr;
19813d8e3701SJed Brown   DMRefineHookLink link,*p;
19823d8e3701SJed Brown 
19833d8e3701SJed Brown   PetscFunctionBegin;
19843d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19853d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19863d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19873d8e3701SJed Brown       link = *p;
19883d8e3701SJed Brown       *p = link->next;
19893d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19903d8e3701SJed Brown       break;
19913d8e3701SJed Brown     }
19923d8e3701SJed Brown   }
19933d8e3701SJed Brown   PetscFunctionReturn(0);
19943d8e3701SJed Brown }
19953d8e3701SJed Brown 
1996c833c3b5SJed Brown /*@
1997c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1998c833c3b5SJed Brown 
1999c833c3b5SJed Brown    Collective if any hooks are
2000c833c3b5SJed Brown 
2001c833c3b5SJed Brown    Input Arguments:
2002c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2003e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2004c833c3b5SJed Brown -  fine - finer DM to update
2005c833c3b5SJed Brown 
2006c833c3b5SJed Brown    Level: developer
2007c833c3b5SJed Brown 
2008c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2009c833c3b5SJed Brown @*/
2010c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2011c833c3b5SJed Brown {
2012c833c3b5SJed Brown   PetscErrorCode   ierr;
2013c833c3b5SJed Brown   DMRefineHookLink link;
2014c833c3b5SJed Brown 
2015c833c3b5SJed Brown   PetscFunctionBegin;
2016c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20178865f1eaSKarl Rupp     if (link->interphook) {
20188865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20198865f1eaSKarl Rupp     }
20204057135bSMatthew G Knepley   }
202147c6ae99SBarry Smith   PetscFunctionReturn(0);
202247c6ae99SBarry Smith }
202347c6ae99SBarry Smith 
2024eb3f98d2SBarry Smith /*@
2025eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2026eb3f98d2SBarry Smith 
2027eb3f98d2SBarry Smith     Not Collective
2028eb3f98d2SBarry Smith 
2029eb3f98d2SBarry Smith     Input Parameter:
2030eb3f98d2SBarry Smith .   dm - the DM object
2031eb3f98d2SBarry Smith 
2032eb3f98d2SBarry Smith     Output Parameter:
2033eb3f98d2SBarry Smith .   level - number of refinements
2034eb3f98d2SBarry Smith 
2035eb3f98d2SBarry Smith     Level: developer
2036eb3f98d2SBarry Smith 
20376a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2038eb3f98d2SBarry Smith 
2039eb3f98d2SBarry Smith @*/
2040eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2041eb3f98d2SBarry Smith {
2042eb3f98d2SBarry Smith   PetscFunctionBegin;
2043eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2044eb3f98d2SBarry Smith   *level = dm->levelup;
2045eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2046eb3f98d2SBarry Smith }
2047eb3f98d2SBarry Smith 
2048fef3a512SBarry Smith /*@
2049fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2050fef3a512SBarry Smith 
2051fef3a512SBarry Smith     Not Collective
2052fef3a512SBarry Smith 
2053fef3a512SBarry Smith     Input Parameter:
2054fef3a512SBarry Smith +   dm - the DM object
2055fef3a512SBarry Smith -   level - number of refinements
2056fef3a512SBarry Smith 
2057fef3a512SBarry Smith     Level: advanced
2058fef3a512SBarry Smith 
205995452b02SPatrick Sanan     Notes:
206095452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2061fef3a512SBarry Smith 
2062fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2063fef3a512SBarry Smith 
2064fef3a512SBarry Smith @*/
2065fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2066fef3a512SBarry Smith {
2067fef3a512SBarry Smith   PetscFunctionBegin;
2068fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2069fef3a512SBarry Smith   dm->levelup = level;
2070fef3a512SBarry Smith   PetscFunctionReturn(0);
2071fef3a512SBarry Smith }
2072fef3a512SBarry Smith 
2073bb9467b5SJed Brown /*@C
2074baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2075baf369e7SPeter Brune 
2076baf369e7SPeter Brune    Logically Collective
2077baf369e7SPeter Brune 
2078baf369e7SPeter Brune    Input Arguments:
2079baf369e7SPeter Brune +  dm - the DM
2080baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2081baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20820298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2083baf369e7SPeter Brune 
2084baf369e7SPeter Brune    Calling sequence for beginhook:
2085baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2086baf369e7SPeter Brune 
2087baf369e7SPeter Brune +  dm - global DM
2088baf369e7SPeter Brune .  g - global vector
2089baf369e7SPeter Brune .  mode - mode
2090baf369e7SPeter Brune .  l - local vector
2091baf369e7SPeter Brune -  ctx - optional user-defined function context
2092baf369e7SPeter Brune 
2093baf369e7SPeter Brune 
2094baf369e7SPeter Brune    Calling sequence for endhook:
2095ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2096baf369e7SPeter Brune 
2097baf369e7SPeter Brune +  global - global DM
2098baf369e7SPeter Brune -  ctx - optional user-defined function context
2099baf369e7SPeter Brune 
2100baf369e7SPeter Brune    Level: advanced
2101baf369e7SPeter Brune 
2102baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2103baf369e7SPeter Brune @*/
2104baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2105baf369e7SPeter Brune {
2106baf369e7SPeter Brune   PetscErrorCode          ierr;
2107baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2108baf369e7SPeter Brune 
2109baf369e7SPeter Brune   PetscFunctionBegin;
2110baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2111baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
211295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2113baf369e7SPeter Brune   link->beginhook = beginhook;
2114baf369e7SPeter Brune   link->endhook   = endhook;
2115baf369e7SPeter Brune   link->ctx       = ctx;
21160298fd71SBarry Smith   link->next      = NULL;
2117baf369e7SPeter Brune   *p              = link;
2118baf369e7SPeter Brune   PetscFunctionReturn(0);
2119baf369e7SPeter Brune }
2120baf369e7SPeter Brune 
21214c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
21224c274da1SToby Isaac {
21234c274da1SToby Isaac   Mat cMat;
21244c274da1SToby Isaac   Vec cVec;
21254c274da1SToby Isaac   PetscSection section, cSec;
21264c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21274c274da1SToby Isaac   PetscErrorCode ierr;
21284c274da1SToby Isaac 
21294c274da1SToby Isaac   PetscFunctionBegin;
21304c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21314c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21324c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
21335db9a05bSToby Isaac     PetscInt nRows;
21345db9a05bSToby Isaac 
21355db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21365db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2137e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
21387711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21394c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
21404c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21414c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21424c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21434c274da1SToby Isaac       if (dof) {
21444c274da1SToby Isaac         PetscScalar *vals;
21454c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
21464c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
21474c274da1SToby Isaac       }
21484c274da1SToby Isaac     }
21494c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21504c274da1SToby Isaac   }
21514c274da1SToby Isaac   PetscFunctionReturn(0);
21524c274da1SToby Isaac }
21534c274da1SToby Isaac 
215447c6ae99SBarry Smith /*@
215501729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
215601729b5cSPatrick Sanan 
215701729b5cSPatrick Sanan     Neighbor-wise Collective on DM
215801729b5cSPatrick Sanan 
215901729b5cSPatrick Sanan     Input Parameters:
216001729b5cSPatrick Sanan +   dm - the DM object
216101729b5cSPatrick Sanan .   g - the global vector
216201729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
216301729b5cSPatrick Sanan -   l - the local vector
216401729b5cSPatrick Sanan 
216501729b5cSPatrick Sanan     Notes:
216601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
216701729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
216801729b5cSPatrick Sanan 
216901729b5cSPatrick Sanan     Level: beginner
217001729b5cSPatrick Sanan 
217101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
217201729b5cSPatrick Sanan 
217301729b5cSPatrick Sanan @*/
217401729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
217501729b5cSPatrick Sanan {
217601729b5cSPatrick Sanan   PetscErrorCode ierr;
217701729b5cSPatrick Sanan 
217801729b5cSPatrick Sanan   PetscFunctionBegin;
217901729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
218001729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
218101729b5cSPatrick Sanan   PetscFunctionReturn(0);
218201729b5cSPatrick Sanan }
218301729b5cSPatrick Sanan 
218401729b5cSPatrick Sanan /*@
218547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
218647c6ae99SBarry Smith 
218747c6ae99SBarry Smith     Neighbor-wise Collective on DM
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith     Input Parameters:
219047c6ae99SBarry Smith +   dm - the DM object
219147c6ae99SBarry Smith .   g - the global vector
219247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
219347c6ae99SBarry Smith -   l - the local vector
219447c6ae99SBarry Smith 
219501729b5cSPatrick Sanan     Level: intermediate
219647c6ae99SBarry Smith 
219701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
219847c6ae99SBarry Smith 
219947c6ae99SBarry Smith @*/
22007087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
220147c6ae99SBarry Smith {
22027128ae9fSMatthew G Knepley   PetscSF                 sf;
220347c6ae99SBarry Smith   PetscErrorCode          ierr;
2204baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
220547c6ae99SBarry Smith 
220647c6ae99SBarry Smith   PetscFunctionBegin;
2207171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2208baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
22098865f1eaSKarl Rupp     if (link->beginhook) {
22108865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
22118865f1eaSKarl Rupp     }
2212baf369e7SPeter Brune   }
22137128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22147128ae9fSMatthew G Knepley   if (sf) {
2215ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2216ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
22177128ae9fSMatthew G Knepley 
221882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22197128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2220ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22217128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22227128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2223ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22247128ae9fSMatthew G Knepley   } else {
222533907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2226843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22277128ae9fSMatthew G Knepley   }
222847c6ae99SBarry Smith   PetscFunctionReturn(0);
222947c6ae99SBarry Smith }
223047c6ae99SBarry Smith 
223147c6ae99SBarry Smith /*@
223247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
223347c6ae99SBarry Smith 
223447c6ae99SBarry Smith     Neighbor-wise Collective on DM
223547c6ae99SBarry Smith 
223647c6ae99SBarry Smith     Input Parameters:
223747c6ae99SBarry Smith +   dm - the DM object
223847c6ae99SBarry Smith .   g - the global vector
223947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
224047c6ae99SBarry Smith -   l - the local vector
224147c6ae99SBarry Smith 
224201729b5cSPatrick Sanan     Level: intermediate
224347c6ae99SBarry Smith 
224401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
224547c6ae99SBarry Smith 
224647c6ae99SBarry Smith @*/
22477087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
224847c6ae99SBarry Smith {
22497128ae9fSMatthew G Knepley   PetscSF                 sf;
225047c6ae99SBarry Smith   PetscErrorCode          ierr;
2251ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2252ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2253baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
225447c6ae99SBarry Smith 
225547c6ae99SBarry Smith   PetscFunctionBegin;
2256171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22577128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22587128ae9fSMatthew G Knepley   if (sf) {
225982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22607128ae9fSMatthew G Knepley 
22617128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2262ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22637128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22647128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2265ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22667128ae9fSMatthew G Knepley   } else {
226733907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2268843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22697128ae9fSMatthew G Knepley   }
22704c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2271baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2272baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2273baf369e7SPeter Brune   }
227447c6ae99SBarry Smith   PetscFunctionReturn(0);
227547c6ae99SBarry Smith }
227647c6ae99SBarry Smith 
2277d4d07f1eSToby Isaac /*@C
2278d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2279d4d07f1eSToby Isaac 
2280d4d07f1eSToby Isaac    Logically Collective
2281d4d07f1eSToby Isaac 
2282d4d07f1eSToby Isaac    Input Arguments:
2283d4d07f1eSToby Isaac +  dm - the DM
2284d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2285d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2286d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2287d4d07f1eSToby Isaac 
2288d4d07f1eSToby Isaac    Calling sequence for beginhook:
2289d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2290d4d07f1eSToby Isaac 
2291d4d07f1eSToby Isaac +  dm - global DM
2292d4d07f1eSToby Isaac .  l - local vector
2293d4d07f1eSToby Isaac .  mode - mode
2294d4d07f1eSToby Isaac .  g - global vector
2295d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2296d4d07f1eSToby Isaac 
2297d4d07f1eSToby Isaac 
2298d4d07f1eSToby Isaac    Calling sequence for endhook:
2299d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2300d4d07f1eSToby Isaac 
2301d4d07f1eSToby Isaac +  global - global DM
2302d4d07f1eSToby Isaac .  l - local vector
2303d4d07f1eSToby Isaac .  mode - mode
2304d4d07f1eSToby Isaac .  g - global vector
2305d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2306d4d07f1eSToby Isaac 
2307d4d07f1eSToby Isaac    Level: advanced
2308d4d07f1eSToby Isaac 
2309d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2310d4d07f1eSToby Isaac @*/
2311d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2312d4d07f1eSToby Isaac {
2313d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2314d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2315d4d07f1eSToby Isaac 
2316d4d07f1eSToby Isaac   PetscFunctionBegin;
2317d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2318d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
231995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2320d4d07f1eSToby Isaac   link->beginhook = beginhook;
2321d4d07f1eSToby Isaac   link->endhook   = endhook;
2322d4d07f1eSToby Isaac   link->ctx       = ctx;
2323d4d07f1eSToby Isaac   link->next      = NULL;
2324d4d07f1eSToby Isaac   *p              = link;
2325d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2326d4d07f1eSToby Isaac }
2327d4d07f1eSToby Isaac 
23284c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
23294c274da1SToby Isaac {
23304c274da1SToby Isaac   Mat cMat;
23314c274da1SToby Isaac   Vec cVec;
23324c274da1SToby Isaac   PetscSection section, cSec;
23334c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23344c274da1SToby Isaac   PetscErrorCode ierr;
23354c274da1SToby Isaac 
23364c274da1SToby Isaac   PetscFunctionBegin;
23374c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23384c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23394c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
23405db9a05bSToby Isaac     PetscInt nRows;
23415db9a05bSToby Isaac 
23425db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23435db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2344e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
23457711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23464c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23474c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23484c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23494c274da1SToby Isaac       if (dof) {
23504c274da1SToby Isaac         PetscInt d;
23514c274da1SToby Isaac         PetscScalar *vals;
23524c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
23534c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
23544c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
23554c274da1SToby Isaac          * we just extracted */
23564c274da1SToby Isaac         for (d = 0; d < dof; d++) {
23574c274da1SToby Isaac           vals[d] = 0.;
23584c274da1SToby Isaac         }
23594c274da1SToby Isaac       }
23604c274da1SToby Isaac     }
23614c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
23624c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23634c274da1SToby Isaac   }
23644c274da1SToby Isaac   PetscFunctionReturn(0);
23654c274da1SToby Isaac }
236601729b5cSPatrick Sanan /*@
236701729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
236801729b5cSPatrick Sanan 
236901729b5cSPatrick Sanan     Neighbor-wise Collective on DM
237001729b5cSPatrick Sanan 
237101729b5cSPatrick Sanan     Input Parameters:
237201729b5cSPatrick Sanan +   dm - the DM object
237301729b5cSPatrick Sanan .   l - the local vector
237401729b5cSPatrick 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.
237501729b5cSPatrick Sanan -   g - the global vector
237601729b5cSPatrick Sanan 
237701729b5cSPatrick Sanan     Notes:
237801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
237901729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
238001729b5cSPatrick Sanan 
238101729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
238201729b5cSPatrick 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.
238301729b5cSPatrick Sanan 
238401729b5cSPatrick Sanan     Level: beginner
238501729b5cSPatrick Sanan 
238601729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
238701729b5cSPatrick Sanan 
238801729b5cSPatrick Sanan @*/
238901729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
239001729b5cSPatrick Sanan {
239101729b5cSPatrick Sanan   PetscErrorCode ierr;
239201729b5cSPatrick Sanan 
239301729b5cSPatrick Sanan   PetscFunctionBegin;
239401729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
239501729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
239601729b5cSPatrick Sanan   PetscFunctionReturn(0);
239701729b5cSPatrick Sanan }
23984c274da1SToby Isaac 
239947c6ae99SBarry Smith /*@
240001729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
24019a42bb27SBarry Smith 
24029a42bb27SBarry Smith     Neighbor-wise Collective on DM
24039a42bb27SBarry Smith 
24049a42bb27SBarry Smith     Input Parameters:
24059a42bb27SBarry Smith +   dm - the DM object
2406f6813fd5SJed Brown .   l - the local vector
24071eb28f2eSBarry 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.
24081eb28f2eSBarry Smith -   g - the global vector
24099a42bb27SBarry Smith 
241095452b02SPatrick Sanan     Notes:
241195452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
241284330215SMatthew 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.
24139a42bb27SBarry Smith 
241401729b5cSPatrick Sanan     Level: intermediate
24159a42bb27SBarry Smith 
241601729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
24179a42bb27SBarry Smith 
24189a42bb27SBarry Smith @*/
24197087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
24209a42bb27SBarry Smith {
24217128ae9fSMatthew G Knepley   PetscSF                 sf;
242284330215SMatthew G. Knepley   PetscSection            s, gs;
2423d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2424ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2425ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
242684330215SMatthew G. Knepley   PetscBool               isInsert;
242784330215SMatthew G. Knepley   PetscErrorCode          ierr;
24289a42bb27SBarry Smith 
24299a42bb27SBarry Smith   PetscFunctionBegin;
2430171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2431d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2432d4d07f1eSToby Isaac     if (link->beginhook) {
2433d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2434d4d07f1eSToby Isaac     }
2435d4d07f1eSToby Isaac   }
24364c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
24377128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2438e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24397128ae9fSMatthew G Knepley   switch (mode) {
24407128ae9fSMatthew G Knepley   case INSERT_VALUES:
24417128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2442304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
244384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24447128ae9fSMatthew G Knepley   case ADD_VALUES:
24457128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2446304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
244784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
24487128ae9fSMatthew G Knepley   default:
244982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24507128ae9fSMatthew G Knepley   }
245184330215SMatthew G. Knepley   if (sf && !isInsert) {
2452ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24537128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2454a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2455ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
245684330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
245784330215SMatthew G. Knepley   } else if (s && isInsert) {
245884330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
245984330215SMatthew G. Knepley 
2460e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
246184330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
246284330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2463ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
246484330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
246584330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2466b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
246784330215SMatthew G. Knepley 
246884330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
246903442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
247084330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2471b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
247284330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
247384330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2474b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
247503442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2476b3b16f48SMatthew 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);
2477b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2478b3b16f48SMatthew G. Knepley       if (!gcdof) {
247984330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2480b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2481b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
248284330215SMatthew G. Knepley         const PetscInt *cdofs;
248384330215SMatthew G. Knepley         PetscInt        cind = 0;
248484330215SMatthew G. Knepley 
248584330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2486b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
248784330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2488b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
248984330215SMatthew G. Knepley         }
2490b3b16f48SMatthew 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);
249184330215SMatthew G. Knepley     }
2492ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24937128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
24947128ae9fSMatthew G Knepley   } else {
2495843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24967128ae9fSMatthew G Knepley   }
24979a42bb27SBarry Smith   PetscFunctionReturn(0);
24989a42bb27SBarry Smith }
24999a42bb27SBarry Smith 
25009a42bb27SBarry Smith /*@
25019a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
250247c6ae99SBarry Smith 
250347c6ae99SBarry Smith     Neighbor-wise Collective on DM
250447c6ae99SBarry Smith 
250547c6ae99SBarry Smith     Input Parameters:
250647c6ae99SBarry Smith +   dm - the DM object
2507f6813fd5SJed Brown .   l - the local vector
250847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2509f6813fd5SJed Brown -   g - the global vector
251047c6ae99SBarry Smith 
251101729b5cSPatrick Sanan     Level: intermediate
251247c6ae99SBarry Smith 
2513e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
251447c6ae99SBarry Smith 
251547c6ae99SBarry Smith @*/
25167087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
251747c6ae99SBarry Smith {
25187128ae9fSMatthew G Knepley   PetscSF                 sf;
251984330215SMatthew G. Knepley   PetscSection            s;
2520d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
252184330215SMatthew G. Knepley   PetscBool               isInsert;
252284330215SMatthew G. Knepley   PetscErrorCode          ierr;
252347c6ae99SBarry Smith 
252447c6ae99SBarry Smith   PetscFunctionBegin;
2525171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25267128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2527e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
25287128ae9fSMatthew G Knepley   switch (mode) {
25297128ae9fSMatthew G Knepley   case INSERT_VALUES:
25307128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
253184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25327128ae9fSMatthew G Knepley   case ADD_VALUES:
25337128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
253484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25357128ae9fSMatthew G Knepley   default:
253682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25377128ae9fSMatthew G Knepley   }
253884330215SMatthew G. Knepley   if (sf && !isInsert) {
2539ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2540ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
254184330215SMatthew G. Knepley 
2542ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
25437128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2544a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2545ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
25467128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
254784330215SMatthew G. Knepley   } else if (s && isInsert) {
25487128ae9fSMatthew G Knepley   } else {
2549843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
25507128ae9fSMatthew G Knepley   }
2551d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2552d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2553d4d07f1eSToby Isaac   }
255447c6ae99SBarry Smith   PetscFunctionReturn(0);
255547c6ae99SBarry Smith }
255647c6ae99SBarry Smith 
2557f089877aSRichard Tran Mills /*@
2558bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2559bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2560d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2561f089877aSRichard Tran Mills 
2562bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2563f089877aSRichard Tran Mills 
2564f089877aSRichard Tran Mills    Input Parameters:
2565f089877aSRichard Tran Mills +  dm - the DM object
2566bc0a1609SRichard Tran Mills .  g - the original local vector
2567bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2568f089877aSRichard Tran Mills 
2569bc0a1609SRichard Tran Mills    Output Parameter:
2570bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2571f089877aSRichard Tran Mills 
2572f089877aSRichard Tran Mills    Level: intermediate
2573f089877aSRichard Tran Mills 
2574bc0a1609SRichard Tran Mills    Notes:
2575bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2576bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2577bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2578bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2579bc0a1609SRichard Tran Mills 
2580bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2581bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2582f089877aSRichard Tran Mills 
2583f089877aSRichard Tran Mills @*/
2584f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2585f089877aSRichard Tran Mills {
2586f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2587f089877aSRichard Tran Mills 
2588f089877aSRichard Tran Mills   PetscFunctionBegin;
2589f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2590bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2591f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2592f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2593f089877aSRichard Tran Mills }
2594f089877aSRichard Tran Mills 
2595f089877aSRichard Tran Mills /*@
2596bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2597bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2598d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2599f089877aSRichard Tran Mills 
2600bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2601f089877aSRichard Tran Mills 
2602f089877aSRichard Tran Mills    Input Parameters:
2603bc0a1609SRichard Tran Mills +  da - the DM object
2604bc0a1609SRichard Tran Mills .  g - the original local vector
2605bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2606f089877aSRichard Tran Mills 
2607bc0a1609SRichard Tran Mills    Output Parameter:
2608bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2609f089877aSRichard Tran Mills 
2610f089877aSRichard Tran Mills    Level: intermediate
2611f089877aSRichard Tran Mills 
2612bc0a1609SRichard Tran Mills    Notes:
2613bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2614bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2615bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2616bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2617bc0a1609SRichard Tran Mills 
2618bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2619bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2620f089877aSRichard Tran Mills 
2621f089877aSRichard Tran Mills @*/
2622f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2623f089877aSRichard Tran Mills {
2624f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2625f089877aSRichard Tran Mills 
2626f089877aSRichard Tran Mills   PetscFunctionBegin;
2627f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2628bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2629f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2630f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2631f089877aSRichard Tran Mills }
2632f089877aSRichard Tran Mills 
2633f089877aSRichard Tran Mills 
263447c6ae99SBarry Smith /*@
263547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
263647c6ae99SBarry Smith 
263747c6ae99SBarry Smith     Collective on DM
263847c6ae99SBarry Smith 
263947c6ae99SBarry Smith     Input Parameter:
264047c6ae99SBarry Smith +   dm - the DM object
264191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
264247c6ae99SBarry Smith 
264347c6ae99SBarry Smith     Output Parameter:
264447c6ae99SBarry Smith .   dmc - the coarsened DM
264547c6ae99SBarry Smith 
264647c6ae99SBarry Smith     Level: developer
264747c6ae99SBarry Smith 
2648e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
264947c6ae99SBarry Smith 
265047c6ae99SBarry Smith @*/
26517087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
265247c6ae99SBarry Smith {
265347c6ae99SBarry Smith   PetscErrorCode    ierr;
2654b17ce1afSJed Brown   DMCoarsenHookLink link;
265547c6ae99SBarry Smith 
265647c6ae99SBarry Smith   PetscFunctionBegin;
2657171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2658fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
265947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
266047c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
26618892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2662a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
266343842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
26648cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2665644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
26660598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2667656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2668e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2669b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2670b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2671b17ce1afSJed Brown   }
267247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2673b17ce1afSJed Brown   PetscFunctionReturn(0);
2674b17ce1afSJed Brown }
2675b17ce1afSJed Brown 
2676bb9467b5SJed Brown /*@C
2677b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2678b17ce1afSJed Brown 
2679b17ce1afSJed Brown    Logically Collective
2680b17ce1afSJed Brown 
2681b17ce1afSJed Brown    Input Arguments:
2682b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2683b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2684b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
26850298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2686b17ce1afSJed Brown 
2687b17ce1afSJed Brown    Calling sequence of coarsenhook:
2688b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2689b17ce1afSJed Brown 
2690b17ce1afSJed Brown +  fine - fine level DM
2691b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2692b17ce1afSJed Brown -  ctx - optional user-defined function context
2693b17ce1afSJed Brown 
2694b17ce1afSJed Brown    Calling sequence for restricthook:
2695c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2696b17ce1afSJed Brown 
2697b17ce1afSJed Brown +  fine - fine level DM
2698b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2699c833c3b5SJed Brown .  rscale - scaling vector for restriction
2700c833c3b5SJed Brown .  inject - matrix restricting by injection
2701b17ce1afSJed Brown .  coarse - coarse level DM to update
2702b17ce1afSJed Brown -  ctx - optional user-defined function context
2703b17ce1afSJed Brown 
2704b17ce1afSJed Brown    Level: advanced
2705b17ce1afSJed Brown 
2706b17ce1afSJed Brown    Notes:
2707b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2708b17ce1afSJed Brown 
2709b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2710b17ce1afSJed Brown 
2711b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2712b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2713b17ce1afSJed Brown 
2714bb9467b5SJed Brown    This function is currently not available from Fortran.
2715bb9467b5SJed Brown 
2716dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2717b17ce1afSJed Brown @*/
2718b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2719b17ce1afSJed Brown {
2720b17ce1afSJed Brown   PetscErrorCode    ierr;
2721b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2722b17ce1afSJed Brown 
2723b17ce1afSJed Brown   PetscFunctionBegin;
2724b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
27251e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
27261e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
27271e3d8eccSJed Brown   }
272895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2729b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2730b17ce1afSJed Brown   link->restricthook = restricthook;
2731b17ce1afSJed Brown   link->ctx          = ctx;
27320298fd71SBarry Smith   link->next         = NULL;
2733b17ce1afSJed Brown   *p                 = link;
2734b17ce1afSJed Brown   PetscFunctionReturn(0);
2735b17ce1afSJed Brown }
2736b17ce1afSJed Brown 
2737dc822a44SJed Brown /*@C
2738dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2739dc822a44SJed Brown 
2740dc822a44SJed Brown    Logically Collective
2741dc822a44SJed Brown 
2742dc822a44SJed Brown    Input Arguments:
2743dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2744dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2745dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2746dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2747dc822a44SJed Brown 
2748dc822a44SJed Brown    Level: advanced
2749dc822a44SJed Brown 
2750dc822a44SJed Brown    Notes:
2751dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2752dc822a44SJed Brown 
2753dc822a44SJed Brown    This function is currently not available from Fortran.
2754dc822a44SJed Brown 
2755dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2756dc822a44SJed Brown @*/
2757dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2758dc822a44SJed Brown {
2759dc822a44SJed Brown   PetscErrorCode    ierr;
2760dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2761dc822a44SJed Brown 
2762dc822a44SJed Brown   PetscFunctionBegin;
2763dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2764dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2765dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2766dc822a44SJed Brown       link = *p;
2767dc822a44SJed Brown       *p = link->next;
2768dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2769dc822a44SJed Brown       break;
2770dc822a44SJed Brown     }
2771dc822a44SJed Brown   }
2772dc822a44SJed Brown   PetscFunctionReturn(0);
2773dc822a44SJed Brown }
2774dc822a44SJed Brown 
2775dc822a44SJed Brown 
2776b17ce1afSJed Brown /*@
2777b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2778b17ce1afSJed Brown 
2779b17ce1afSJed Brown    Collective if any hooks are
2780b17ce1afSJed Brown 
2781b17ce1afSJed Brown    Input Arguments:
2782b17ce1afSJed Brown +  fine - finer DM to use as a base
2783b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2784e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2785b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2786e91eccc2SStefano Zampini -  coarse - coarser DM to update
2787b17ce1afSJed Brown 
2788b17ce1afSJed Brown    Level: developer
2789b17ce1afSJed Brown 
2790b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2791b17ce1afSJed Brown @*/
2792b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2793b17ce1afSJed Brown {
2794b17ce1afSJed Brown   PetscErrorCode    ierr;
2795b17ce1afSJed Brown   DMCoarsenHookLink link;
2796b17ce1afSJed Brown 
2797b17ce1afSJed Brown   PetscFunctionBegin;
2798b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
27998865f1eaSKarl Rupp     if (link->restricthook) {
28008865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
28018865f1eaSKarl Rupp     }
2802b17ce1afSJed Brown   }
280347c6ae99SBarry Smith   PetscFunctionReturn(0);
280447c6ae99SBarry Smith }
280547c6ae99SBarry Smith 
2806bb9467b5SJed Brown /*@C
2807be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
28085dbd56e3SPeter Brune 
28095dbd56e3SPeter Brune    Logically Collective
28105dbd56e3SPeter Brune 
28115dbd56e3SPeter Brune    Input Arguments:
28125dbd56e3SPeter Brune +  global - global DM
2813ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
28145dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
28150298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
28165dbd56e3SPeter Brune 
2817ec4806b8SPeter Brune 
2818ec4806b8SPeter Brune    Calling sequence for ddhook:
2819ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2820ec4806b8SPeter Brune 
2821ec4806b8SPeter Brune +  global - global DM
2822ec4806b8SPeter Brune .  block  - block DM
2823ec4806b8SPeter Brune -  ctx - optional user-defined function context
2824ec4806b8SPeter Brune 
28255dbd56e3SPeter Brune    Calling sequence for restricthook:
2826ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
28275dbd56e3SPeter Brune 
28285dbd56e3SPeter Brune +  global - global DM
28295dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
28305dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2831ec4806b8SPeter Brune .  block  - block DM
28325dbd56e3SPeter Brune -  ctx - optional user-defined function context
28335dbd56e3SPeter Brune 
28345dbd56e3SPeter Brune    Level: advanced
28355dbd56e3SPeter Brune 
28365dbd56e3SPeter Brune    Notes:
2837ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
28385dbd56e3SPeter Brune 
28395dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
28405dbd56e3SPeter Brune 
28415dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2842ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
28435dbd56e3SPeter Brune 
2844bb9467b5SJed Brown    This function is currently not available from Fortran.
2845bb9467b5SJed Brown 
28465dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
28475dbd56e3SPeter Brune @*/
2848be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
28495dbd56e3SPeter Brune {
28505dbd56e3SPeter Brune   PetscErrorCode      ierr;
2851be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
28525dbd56e3SPeter Brune 
28535dbd56e3SPeter Brune   PetscFunctionBegin;
28545dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2855b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2856b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2857b3a6b972SJed Brown   }
285895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
28595dbd56e3SPeter Brune   link->restricthook = restricthook;
2860be081cd6SPeter Brune   link->ddhook       = ddhook;
28615dbd56e3SPeter Brune   link->ctx          = ctx;
28620298fd71SBarry Smith   link->next         = NULL;
28635dbd56e3SPeter Brune   *p                 = link;
28645dbd56e3SPeter Brune   PetscFunctionReturn(0);
28655dbd56e3SPeter Brune }
28665dbd56e3SPeter Brune 
2867b3a6b972SJed Brown /*@C
2868b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2869b3a6b972SJed Brown 
2870b3a6b972SJed Brown    Logically Collective
2871b3a6b972SJed Brown 
2872b3a6b972SJed Brown    Input Arguments:
2873b3a6b972SJed Brown +  global - global DM
2874b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2875b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2876b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2877b3a6b972SJed Brown 
2878b3a6b972SJed Brown    Level: advanced
2879b3a6b972SJed Brown 
2880b3a6b972SJed Brown    Notes:
2881b3a6b972SJed Brown 
2882b3a6b972SJed Brown    This function is currently not available from Fortran.
2883b3a6b972SJed Brown 
2884b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2885b3a6b972SJed Brown @*/
2886b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2887b3a6b972SJed Brown {
2888b3a6b972SJed Brown   PetscErrorCode      ierr;
2889b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2890b3a6b972SJed Brown 
2891b3a6b972SJed Brown   PetscFunctionBegin;
2892b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2893b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2894b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2895b3a6b972SJed Brown       link = *p;
2896b3a6b972SJed Brown       *p = link->next;
2897b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2898b3a6b972SJed Brown       break;
2899b3a6b972SJed Brown     }
2900b3a6b972SJed Brown   }
2901b3a6b972SJed Brown   PetscFunctionReturn(0);
2902b3a6b972SJed Brown }
2903b3a6b972SJed Brown 
29045dbd56e3SPeter Brune /*@
2905be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
29065dbd56e3SPeter Brune 
29075dbd56e3SPeter Brune    Collective if any hooks are
29085dbd56e3SPeter Brune 
29095dbd56e3SPeter Brune    Input Arguments:
29105dbd56e3SPeter Brune +  fine - finer DM to use as a base
2911be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2912be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
29135dbd56e3SPeter Brune -  coarse - coarer DM to update
29145dbd56e3SPeter Brune 
29155dbd56e3SPeter Brune    Level: developer
29165dbd56e3SPeter Brune 
29175dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
29185dbd56e3SPeter Brune @*/
2919be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
29205dbd56e3SPeter Brune {
29215dbd56e3SPeter Brune   PetscErrorCode      ierr;
2922be081cd6SPeter Brune   DMSubDomainHookLink link;
29235dbd56e3SPeter Brune 
29245dbd56e3SPeter Brune   PetscFunctionBegin;
2925be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
29268865f1eaSKarl Rupp     if (link->restricthook) {
29278865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
29288865f1eaSKarl Rupp     }
29295dbd56e3SPeter Brune   }
29305dbd56e3SPeter Brune   PetscFunctionReturn(0);
29315dbd56e3SPeter Brune }
29325dbd56e3SPeter Brune 
29335fe1f584SPeter Brune /*@
29346a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
29355fe1f584SPeter Brune 
29365fe1f584SPeter Brune     Not Collective
29375fe1f584SPeter Brune 
29385fe1f584SPeter Brune     Input Parameter:
29395fe1f584SPeter Brune .   dm - the DM object
29405fe1f584SPeter Brune 
29415fe1f584SPeter Brune     Output Parameter:
29426a7d9d85SPeter Brune .   level - number of coarsenings
29435fe1f584SPeter Brune 
29445fe1f584SPeter Brune     Level: developer
29455fe1f584SPeter Brune 
29466a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
29475fe1f584SPeter Brune 
29485fe1f584SPeter Brune @*/
29495fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
29505fe1f584SPeter Brune {
29515fe1f584SPeter Brune   PetscFunctionBegin;
29525fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29535fe1f584SPeter Brune   *level = dm->leveldown;
29545fe1f584SPeter Brune   PetscFunctionReturn(0);
29555fe1f584SPeter Brune }
29565fe1f584SPeter Brune 
29579a64c4a8SMatthew G. Knepley /*@
29589a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
29599a64c4a8SMatthew G. Knepley 
29609a64c4a8SMatthew G. Knepley     Not Collective
29619a64c4a8SMatthew G. Knepley 
29629a64c4a8SMatthew G. Knepley     Input Parameters:
29639a64c4a8SMatthew G. Knepley +   dm - the DM object
29649a64c4a8SMatthew G. Knepley -   level - number of coarsenings
29659a64c4a8SMatthew G. Knepley 
29669a64c4a8SMatthew G. Knepley     Level: developer
29679a64c4a8SMatthew G. Knepley 
29689a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
29699a64c4a8SMatthew G. Knepley @*/
29709a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
29719a64c4a8SMatthew G. Knepley {
29729a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
29739a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29749a64c4a8SMatthew G. Knepley   dm->leveldown = level;
29759a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
29769a64c4a8SMatthew G. Knepley }
29779a64c4a8SMatthew G. Knepley 
29785fe1f584SPeter Brune 
29795fe1f584SPeter Brune 
298047c6ae99SBarry Smith /*@C
298147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
298247c6ae99SBarry Smith 
298347c6ae99SBarry Smith     Collective on DM
298447c6ae99SBarry Smith 
298547c6ae99SBarry Smith     Input Parameter:
298647c6ae99SBarry Smith +   dm - the DM object
298747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
298847c6ae99SBarry Smith 
298947c6ae99SBarry Smith     Output Parameter:
299047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
299147c6ae99SBarry Smith 
299247c6ae99SBarry Smith     Level: developer
299347c6ae99SBarry Smith 
2994e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
299547c6ae99SBarry Smith 
299647c6ae99SBarry Smith @*/
29977087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
299847c6ae99SBarry Smith {
299947c6ae99SBarry Smith   PetscErrorCode ierr;
300047c6ae99SBarry Smith 
300147c6ae99SBarry Smith   PetscFunctionBegin;
3002171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3003ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
300447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
300547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
300647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
300747c6ae99SBarry Smith   } else if (dm->ops->refine) {
300847c6ae99SBarry Smith     PetscInt i;
300947c6ae99SBarry Smith 
3010ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
301147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3012ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
301347c6ae99SBarry Smith     }
3014ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
301547c6ae99SBarry Smith   PetscFunctionReturn(0);
301647c6ae99SBarry Smith }
301747c6ae99SBarry Smith 
301847c6ae99SBarry Smith /*@C
301947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
302047c6ae99SBarry Smith 
302147c6ae99SBarry Smith     Collective on DM
302247c6ae99SBarry Smith 
302347c6ae99SBarry Smith     Input Parameter:
302447c6ae99SBarry Smith +   dm - the DM object
302547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
302647c6ae99SBarry Smith 
302747c6ae99SBarry Smith     Output Parameter:
302847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
302947c6ae99SBarry Smith 
303047c6ae99SBarry Smith     Level: developer
303147c6ae99SBarry Smith 
3032e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
303347c6ae99SBarry Smith 
303447c6ae99SBarry Smith @*/
30357087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
303647c6ae99SBarry Smith {
303747c6ae99SBarry Smith   PetscErrorCode ierr;
303847c6ae99SBarry Smith 
303947c6ae99SBarry Smith   PetscFunctionBegin;
3040171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3041ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
304247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
304347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
304447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
304547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
304647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
304747c6ae99SBarry Smith     PetscInt i;
304847c6ae99SBarry Smith 
3049ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
305047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3051ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
305247c6ae99SBarry Smith     }
3053ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
305447c6ae99SBarry Smith   PetscFunctionReturn(0);
305547c6ae99SBarry Smith }
305647c6ae99SBarry Smith 
305747c6ae99SBarry Smith /*@
3058e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
305947c6ae99SBarry Smith    grids associated with two DMs.
306047c6ae99SBarry Smith 
306147c6ae99SBarry Smith    Collective on DM
306247c6ae99SBarry Smith 
306347c6ae99SBarry Smith    Input Parameters:
306447c6ae99SBarry Smith +  dmc - the coarse grid DM
306547c6ae99SBarry Smith -  dmf - the fine grid DM
306647c6ae99SBarry Smith 
306747c6ae99SBarry Smith    Output Parameters:
306847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
306947c6ae99SBarry Smith 
307047c6ae99SBarry Smith    Level: intermediate
307147c6ae99SBarry Smith 
307247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
307347c6ae99SBarry Smith 
3074e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
307547c6ae99SBarry Smith @*/
3076e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
307747c6ae99SBarry Smith {
307847c6ae99SBarry Smith   PetscErrorCode ierr;
307947c6ae99SBarry Smith 
308047c6ae99SBarry Smith   PetscFunctionBegin;
3081171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3082171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
308347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
308447c6ae99SBarry Smith   PetscFunctionReturn(0);
308547c6ae99SBarry Smith }
308647c6ae99SBarry Smith 
30871a266240SBarry Smith /*@C
30881a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
30891a266240SBarry Smith 
30901a266240SBarry Smith     Not Collective
30911a266240SBarry Smith 
30921a266240SBarry Smith     Input Parameters:
30931a266240SBarry Smith +   dm - the DM object
30941a266240SBarry Smith -   destroy - the destroy function
30951a266240SBarry Smith 
30961a266240SBarry Smith     Level: intermediate
30971a266240SBarry Smith 
3098e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
30991a266240SBarry Smith 
3100f07f9ceaSJed Brown @*/
31011a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
31021a266240SBarry Smith {
31031a266240SBarry Smith   PetscFunctionBegin;
3104171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31051a266240SBarry Smith   dm->ctxdestroy = destroy;
31061a266240SBarry Smith   PetscFunctionReturn(0);
31071a266240SBarry Smith }
31081a266240SBarry Smith 
3109b07ff414SBarry Smith /*@
31101b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
311147c6ae99SBarry Smith 
311247c6ae99SBarry Smith     Not Collective
311347c6ae99SBarry Smith 
311447c6ae99SBarry Smith     Input Parameters:
311547c6ae99SBarry Smith +   dm - the DM object
311647c6ae99SBarry Smith -   ctx - the user context
311747c6ae99SBarry Smith 
311847c6ae99SBarry Smith     Level: intermediate
311947c6ae99SBarry Smith 
3120e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
312147c6ae99SBarry Smith 
312247c6ae99SBarry Smith @*/
31231b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
312447c6ae99SBarry Smith {
312547c6ae99SBarry Smith   PetscFunctionBegin;
3126171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
312747c6ae99SBarry Smith   dm->ctx = ctx;
312847c6ae99SBarry Smith   PetscFunctionReturn(0);
312947c6ae99SBarry Smith }
313047c6ae99SBarry Smith 
313147c6ae99SBarry Smith /*@
31321b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
313347c6ae99SBarry Smith 
313447c6ae99SBarry Smith     Not Collective
313547c6ae99SBarry Smith 
313647c6ae99SBarry Smith     Input Parameter:
313747c6ae99SBarry Smith .   dm - the DM object
313847c6ae99SBarry Smith 
313947c6ae99SBarry Smith     Output Parameter:
314047c6ae99SBarry Smith .   ctx - the user context
314147c6ae99SBarry Smith 
314247c6ae99SBarry Smith     Level: intermediate
314347c6ae99SBarry Smith 
3144e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
314547c6ae99SBarry Smith 
314647c6ae99SBarry Smith @*/
31471b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
314847c6ae99SBarry Smith {
314947c6ae99SBarry Smith   PetscFunctionBegin;
3150171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31511b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
315247c6ae99SBarry Smith   PetscFunctionReturn(0);
315347c6ae99SBarry Smith }
315447c6ae99SBarry Smith 
315508da532bSDmitry Karpeev /*@C
3156df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
315708da532bSDmitry Karpeev 
315808da532bSDmitry Karpeev     Logically Collective on DM
315908da532bSDmitry Karpeev 
316008da532bSDmitry Karpeev     Input Parameter:
316108da532bSDmitry Karpeev +   dm - the DM object
31620298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
316308da532bSDmitry Karpeev 
316408da532bSDmitry Karpeev     Level: intermediate
316508da532bSDmitry Karpeev 
3166835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
316708da532bSDmitry Karpeev          DMSetJacobian()
316808da532bSDmitry Karpeev 
316908da532bSDmitry Karpeev @*/
317008da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
317108da532bSDmitry Karpeev {
317208da532bSDmitry Karpeev   PetscFunctionBegin;
317308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
317408da532bSDmitry Karpeev   PetscFunctionReturn(0);
317508da532bSDmitry Karpeev }
317608da532bSDmitry Karpeev 
317708da532bSDmitry Karpeev /*@
317808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
317908da532bSDmitry Karpeev 
318008da532bSDmitry Karpeev     Not Collective
318108da532bSDmitry Karpeev 
318208da532bSDmitry Karpeev     Input Parameter:
318308da532bSDmitry Karpeev .   dm - the DM object to destroy
318408da532bSDmitry Karpeev 
318508da532bSDmitry Karpeev     Output Parameter:
318608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
318708da532bSDmitry Karpeev 
318808da532bSDmitry Karpeev     Level: developer
318908da532bSDmitry Karpeev 
319074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
319108da532bSDmitry Karpeev 
319208da532bSDmitry Karpeev @*/
319308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
319408da532bSDmitry Karpeev {
319508da532bSDmitry Karpeev   PetscFunctionBegin;
319608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
319708da532bSDmitry Karpeev   PetscFunctionReturn(0);
319808da532bSDmitry Karpeev }
319908da532bSDmitry Karpeev 
320008da532bSDmitry Karpeev /*@C
320108da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
320208da532bSDmitry Karpeev 
320308da532bSDmitry Karpeev     Logically Collective on DM
320408da532bSDmitry Karpeev 
320508da532bSDmitry Karpeev     Input Parameters:
3206907376e6SBarry Smith .   dm - the DM object
320708da532bSDmitry Karpeev 
320808da532bSDmitry Karpeev     Output parameters:
320908da532bSDmitry Karpeev +   xl - lower bound
321008da532bSDmitry Karpeev -   xu - upper bound
321108da532bSDmitry Karpeev 
3212907376e6SBarry Smith     Level: advanced
3213907376e6SBarry Smith 
321495452b02SPatrick Sanan     Notes:
321595452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
321608da532bSDmitry Karpeev 
321774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
321808da532bSDmitry Karpeev 
321908da532bSDmitry Karpeev @*/
322008da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
322108da532bSDmitry Karpeev {
322208da532bSDmitry Karpeev   PetscErrorCode ierr;
32235fd66863SKarl Rupp 
322408da532bSDmitry Karpeev   PetscFunctionBegin;
322508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
322608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
322708da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
322808da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
32298865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
323008da532bSDmitry Karpeev   PetscFunctionReturn(0);
323108da532bSDmitry Karpeev }
323208da532bSDmitry Karpeev 
3233b0ae01b7SPeter Brune /*@
3234b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3235b0ae01b7SPeter Brune 
3236b0ae01b7SPeter Brune     Not Collective
3237b0ae01b7SPeter Brune 
3238b0ae01b7SPeter Brune     Input Parameter:
3239b0ae01b7SPeter Brune .   dm - the DM object
3240b0ae01b7SPeter Brune 
3241b0ae01b7SPeter Brune     Output Parameter:
3242b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3243b0ae01b7SPeter Brune 
3244b0ae01b7SPeter Brune     Level: developer
3245b0ae01b7SPeter Brune 
3246b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3247b0ae01b7SPeter Brune 
3248b0ae01b7SPeter Brune @*/
3249b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3250b0ae01b7SPeter Brune {
3251b0ae01b7SPeter Brune   PetscFunctionBegin;
3252b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3253b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3254b0ae01b7SPeter Brune }
3255b0ae01b7SPeter Brune 
32563ad4599aSBarry Smith /*@
32573ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
32583ad4599aSBarry Smith 
32593ad4599aSBarry Smith     Not Collective
32603ad4599aSBarry Smith 
32613ad4599aSBarry Smith     Input Parameter:
32623ad4599aSBarry Smith .   dm - the DM object
32633ad4599aSBarry Smith 
32643ad4599aSBarry Smith     Output Parameter:
32653ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
32663ad4599aSBarry Smith 
32673ad4599aSBarry Smith     Level: developer
32683ad4599aSBarry Smith 
32693ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
32703ad4599aSBarry Smith 
32713ad4599aSBarry Smith @*/
32723ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
32733ad4599aSBarry Smith {
32743ad4599aSBarry Smith   PetscFunctionBegin;
32753ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
32763ad4599aSBarry Smith   PetscFunctionReturn(0);
32773ad4599aSBarry Smith }
32783ad4599aSBarry Smith 
3279a7058e45SLawrence Mitchell 
3280a7058e45SLawrence Mitchell /*@
3281a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3282a7058e45SLawrence Mitchell 
3283a7058e45SLawrence Mitchell     Not Collective
3284a7058e45SLawrence Mitchell 
3285a7058e45SLawrence Mitchell     Input Parameter:
3286a7058e45SLawrence Mitchell .   dm - the DM object
3287a7058e45SLawrence Mitchell 
3288a7058e45SLawrence Mitchell     Output Parameter:
3289a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3290a7058e45SLawrence Mitchell 
3291a7058e45SLawrence Mitchell     Level: developer
3292a7058e45SLawrence Mitchell 
3293a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3294a7058e45SLawrence Mitchell 
3295a7058e45SLawrence Mitchell @*/
3296a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3297a7058e45SLawrence Mitchell {
32984a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3299a7058e45SLawrence Mitchell   PetscFunctionBegin;
33004a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
33014a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3302a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3303a7058e45SLawrence Mitchell }
3304a7058e45SLawrence Mitchell 
3305748fac09SDmitry Karpeev /*@C
330608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
330708da532bSDmitry Karpeev 
330808da532bSDmitry Karpeev     Collective on DM
330908da532bSDmitry Karpeev 
331008da532bSDmitry Karpeev     Input Parameter:
331108da532bSDmitry Karpeev +   dm - the DM object
33120298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
331308da532bSDmitry Karpeev 
331408da532bSDmitry Karpeev     Level: developer
331508da532bSDmitry Karpeev 
331674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
331708da532bSDmitry Karpeev 
331808da532bSDmitry Karpeev @*/
331908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
332008da532bSDmitry Karpeev {
332108da532bSDmitry Karpeev   PetscErrorCode ierr;
33225fd66863SKarl Rupp 
332308da532bSDmitry Karpeev   PetscFunctionBegin;
332408da532bSDmitry Karpeev   if (x) {
332508da532bSDmitry Karpeev     if (!dm->x) {
332608da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
332708da532bSDmitry Karpeev     }
332808da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
33298865f1eaSKarl Rupp   } else if (dm->x) {
333008da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
333108da532bSDmitry Karpeev   }
333208da532bSDmitry Karpeev   PetscFunctionReturn(0);
333308da532bSDmitry Karpeev }
333408da532bSDmitry Karpeev 
33350298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3336264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3337264ace61SBarry Smith 
3338264ace61SBarry Smith /*@C
3339264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3340264ace61SBarry Smith 
3341264ace61SBarry Smith   Collective on DM
3342264ace61SBarry Smith 
3343264ace61SBarry Smith   Input Parameters:
3344264ace61SBarry Smith + dm     - The DM object
3345264ace61SBarry Smith - method - The name of the DM type
3346264ace61SBarry Smith 
3347264ace61SBarry Smith   Options Database Key:
3348264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3349264ace61SBarry Smith 
3350264ace61SBarry Smith   Notes:
3351e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3352264ace61SBarry Smith 
3353264ace61SBarry Smith   Level: intermediate
3354264ace61SBarry Smith 
3355264ace61SBarry Smith .keywords: DM, set, type
3356264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3357264ace61SBarry Smith @*/
335819fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3359264ace61SBarry Smith {
3360264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3361264ace61SBarry Smith   PetscBool      match;
3362264ace61SBarry Smith   PetscErrorCode ierr;
3363264ace61SBarry Smith 
3364264ace61SBarry Smith   PetscFunctionBegin;
3365264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3366251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3367264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3368264ace61SBarry Smith 
33690f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
33701c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3371ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3372264ace61SBarry Smith 
3373264ace61SBarry Smith   if (dm->ops->destroy) {
3374264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
33750298fd71SBarry Smith     dm->ops->destroy = NULL;
3376264ace61SBarry Smith   }
3377264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3378264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3379264ace61SBarry Smith   PetscFunctionReturn(0);
3380264ace61SBarry Smith }
3381264ace61SBarry Smith 
3382264ace61SBarry Smith /*@C
3383264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3384264ace61SBarry Smith 
3385264ace61SBarry Smith   Not Collective
3386264ace61SBarry Smith 
3387264ace61SBarry Smith   Input Parameter:
3388264ace61SBarry Smith . dm  - The DM
3389264ace61SBarry Smith 
3390264ace61SBarry Smith   Output Parameter:
3391264ace61SBarry Smith . type - The DM type name
3392264ace61SBarry Smith 
3393264ace61SBarry Smith   Level: intermediate
3394264ace61SBarry Smith 
3395264ace61SBarry Smith .keywords: DM, get, type, name
3396264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3397264ace61SBarry Smith @*/
339819fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3399264ace61SBarry Smith {
3400264ace61SBarry Smith   PetscErrorCode ierr;
3401264ace61SBarry Smith 
3402264ace61SBarry Smith   PetscFunctionBegin;
3403264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3404c959eef4SJed Brown   PetscValidPointer(type,2);
3405607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3406264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3407264ace61SBarry Smith   PetscFunctionReturn(0);
3408264ace61SBarry Smith }
3409264ace61SBarry Smith 
341067a56275SMatthew G Knepley /*@C
341167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
341267a56275SMatthew G Knepley 
341367a56275SMatthew G Knepley   Collective on DM
341467a56275SMatthew G Knepley 
341567a56275SMatthew G Knepley   Input Parameters:
341667a56275SMatthew G Knepley + dm - the DM
341767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
341867a56275SMatthew G Knepley 
341967a56275SMatthew G Knepley   Output Parameter:
342067a56275SMatthew G Knepley . M - pointer to new DM
342167a56275SMatthew G Knepley 
342267a56275SMatthew G Knepley   Notes:
342367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
342467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
342567a56275SMatthew G Knepley   of the input DM.
342667a56275SMatthew G Knepley 
342767a56275SMatthew G Knepley   Level: intermediate
342867a56275SMatthew G Knepley 
342967a56275SMatthew G Knepley .seealso: DMCreate()
343067a56275SMatthew G Knepley @*/
343119fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
343267a56275SMatthew G Knepley {
343367a56275SMatthew G Knepley   DM             B;
343467a56275SMatthew G Knepley   char           convname[256];
3435c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
343667a56275SMatthew G Knepley   PetscErrorCode ierr;
343767a56275SMatthew G Knepley 
343867a56275SMatthew G Knepley   PetscFunctionBegin;
343967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
344067a56275SMatthew G Knepley   PetscValidType(dm,1);
344167a56275SMatthew G Knepley   PetscValidPointer(M,3);
3442251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3443c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3444c067b6caSMatthew G. Knepley   if (sametype) {
3445c067b6caSMatthew G. Knepley     *M   = dm;
3446c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3447c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3448c067b6caSMatthew G. Knepley   } else {
34490298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
345067a56275SMatthew G Knepley 
345167a56275SMatthew G Knepley     /*
345267a56275SMatthew G Knepley        Order of precedence:
345367a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
345467a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
345567a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
345667a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
345767a56275SMatthew G Knepley        5) Use a really basic converter.
345867a56275SMatthew G Knepley     */
345967a56275SMatthew G Knepley 
346067a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3461a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3462a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3463a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3464a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3465a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34660005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
346767a56275SMatthew G Knepley     if (conv) goto foundconv;
346867a56275SMatthew G Knepley 
346967a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
347082f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
347167a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3472a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3473a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3474a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3475a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3476a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34770005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
347867a56275SMatthew G Knepley     if (conv) {
3479fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
348067a56275SMatthew G Knepley       goto foundconv;
348167a56275SMatthew G Knepley     }
348267a56275SMatthew G Knepley 
348367a56275SMatthew G Knepley #if 0
348467a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
348567a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3486fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
348767a56275SMatthew G Knepley     if (conv) goto foundconv;
348867a56275SMatthew G Knepley 
348967a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
349067a56275SMatthew G Knepley     if (dm->ops->convert) {
349167a56275SMatthew G Knepley       conv = dm->ops->convert;
349267a56275SMatthew G Knepley     }
349367a56275SMatthew G Knepley     if (conv) goto foundconv;
349467a56275SMatthew G Knepley #endif
349567a56275SMatthew G Knepley 
349667a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
349782f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
349867a56275SMatthew G Knepley 
349967a56275SMatthew G Knepley foundconv:
350067a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
350167a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
350212fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
350390b157c4SStefano Zampini     {
350490b157c4SStefano Zampini       PetscBool             isper;
350512fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
350612fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
350790b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
350890b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
350912fa691eSMatthew G. Knepley     }
351067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
351167a56275SMatthew G Knepley   }
351267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
351367a56275SMatthew G Knepley   PetscFunctionReturn(0);
351467a56275SMatthew G Knepley }
3515264ace61SBarry Smith 
3516264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3517264ace61SBarry Smith 
3518264ace61SBarry Smith /*@C
35191c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
35201c84c290SBarry Smith 
35211c84c290SBarry Smith   Not Collective
35221c84c290SBarry Smith 
35231c84c290SBarry Smith   Input Parameters:
35241c84c290SBarry Smith + name        - The name of a new user-defined creation routine
35251c84c290SBarry Smith - create_func - The creation routine itself
35261c84c290SBarry Smith 
35271c84c290SBarry Smith   Notes:
35281c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
35291c84c290SBarry Smith 
35301c84c290SBarry Smith 
35311c84c290SBarry Smith   Sample usage:
35321c84c290SBarry Smith .vb
3533bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
35341c84c290SBarry Smith .ve
35351c84c290SBarry Smith 
35361c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
35371c84c290SBarry Smith .vb
35381c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
35391c84c290SBarry Smith     DMSetType(DM,"my_da");
35401c84c290SBarry Smith .ve
35411c84c290SBarry Smith    or at runtime via the option
35421c84c290SBarry Smith .vb
35431c84c290SBarry Smith     -da_type my_da
35441c84c290SBarry Smith .ve
3545264ace61SBarry Smith 
3546264ace61SBarry Smith   Level: advanced
35471c84c290SBarry Smith 
35481c84c290SBarry Smith .keywords: DM, register
3549bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
35501c84c290SBarry Smith 
3551264ace61SBarry Smith @*/
3552bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3553264ace61SBarry Smith {
3554264ace61SBarry Smith   PetscErrorCode ierr;
3555264ace61SBarry Smith 
3556264ace61SBarry Smith   PetscFunctionBegin;
35571d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3558a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3559264ace61SBarry Smith   PetscFunctionReturn(0);
3560264ace61SBarry Smith }
3561264ace61SBarry Smith 
3562b859378eSBarry Smith /*@C
356355849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3564b859378eSBarry Smith 
3565b859378eSBarry Smith   Collective on PetscViewer
3566b859378eSBarry Smith 
3567b859378eSBarry Smith   Input Parameters:
3568b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3569b859378eSBarry Smith            some related function before a call to DMLoad().
3570b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3571b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3572b859378eSBarry Smith 
3573b859378eSBarry Smith    Level: intermediate
3574b859378eSBarry Smith 
3575b859378eSBarry Smith   Notes:
357655849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3577b859378eSBarry Smith 
3578b859378eSBarry Smith   Notes for advanced users:
3579b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3580b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3581b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3582b859378eSBarry Smith   format is
3583b859378eSBarry Smith .vb
3584b859378eSBarry Smith      has not yet been determined
3585b859378eSBarry Smith .ve
3586b859378eSBarry Smith 
3587b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3588b859378eSBarry Smith @*/
3589b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3590b859378eSBarry Smith {
35919331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3592b859378eSBarry Smith   PetscErrorCode ierr;
3593b859378eSBarry Smith 
3594b859378eSBarry Smith   PetscFunctionBegin;
3595b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3596b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3597fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
359832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
35999331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
36009331c7a4SMatthew G. Knepley   if (isbinary) {
36019331c7a4SMatthew G. Knepley     PetscInt classid;
36029331c7a4SMatthew G. Knepley     char     type[256];
3603b859378eSBarry Smith 
3604060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
36059200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3606060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
360732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
36089331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
36099331c7a4SMatthew G. Knepley   } else if (ishdf5) {
36109331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
36119331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3612b859378eSBarry Smith   PetscFunctionReturn(0);
3613b859378eSBarry Smith }
3614b859378eSBarry Smith 
36157da65231SMatthew G Knepley /******************************** FEM Support **********************************/
36167da65231SMatthew G Knepley 
3617a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3618a6dfd86eSKarl Rupp {
36191d47ebbbSSatish Balay   PetscInt       f;
36201b30c384SMatthew G Knepley   PetscErrorCode ierr;
36211b30c384SMatthew G Knepley 
36227da65231SMatthew G Knepley   PetscFunctionBegin;
362374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
36241d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
362557622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
36267da65231SMatthew G Knepley   }
36277da65231SMatthew G Knepley   PetscFunctionReturn(0);
36287da65231SMatthew G Knepley }
36297da65231SMatthew G Knepley 
3630a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3631a6dfd86eSKarl Rupp {
36321b30c384SMatthew G Knepley   PetscInt       f, g;
36337da65231SMatthew G Knepley   PetscErrorCode ierr;
36347da65231SMatthew G Knepley 
36357da65231SMatthew G Knepley   PetscFunctionBegin;
363674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
36371d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
363874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
36391d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3640e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
36417da65231SMatthew G Knepley     }
364274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
36437da65231SMatthew G Knepley   }
36447da65231SMatthew G Knepley   PetscFunctionReturn(0);
36457da65231SMatthew G Knepley }
3646e7c4fc90SDmitry Karpeev 
36476113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3648e759306cSMatthew G. Knepley {
36490c5b8624SToby Isaac   PetscInt          localSize, bs;
36500c5b8624SToby Isaac   PetscMPIInt       size;
36510c5b8624SToby Isaac   Vec               x, xglob;
36520c5b8624SToby Isaac   const PetscScalar *xarray;
3653e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3654e759306cSMatthew G. Knepley 
3655e759306cSMatthew G. Knepley   PetscFunctionBegin;
36569852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3657e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3658e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
36596113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
36600c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
36610c5b8624SToby Isaac   if (size > 1) {
36620c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
36630c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
36640c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
36650c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
36660c5b8624SToby Isaac   } else {
36670c5b8624SToby Isaac     xglob = x;
36680c5b8624SToby Isaac   }
36690c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
36700c5b8624SToby Isaac   if (size > 1) {
36710c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
36720c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
36730c5b8624SToby Isaac   }
3674e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3675e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3676e759306cSMatthew G. Knepley }
3677e759306cSMatthew G. Knepley 
367888ed4aceSMatthew G Knepley /*@
3679e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
368088ed4aceSMatthew G Knepley 
368188ed4aceSMatthew G Knepley   Input Parameter:
368288ed4aceSMatthew G Knepley . dm - The DM
368388ed4aceSMatthew G Knepley 
368488ed4aceSMatthew G Knepley   Output Parameter:
368588ed4aceSMatthew G Knepley . section - The PetscSection
368688ed4aceSMatthew G Knepley 
3687e5893cccSMatthew G. Knepley   Options Database Keys:
3688e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3689e5893cccSMatthew G. Knepley 
369088ed4aceSMatthew G Knepley   Level: intermediate
369188ed4aceSMatthew G Knepley 
369288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
369388ed4aceSMatthew G Knepley 
3694e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
369588ed4aceSMatthew G Knepley @*/
3696e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
36970adebc6cSBarry Smith {
3698fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3699fd59a867SMatthew G. Knepley 
370088ed4aceSMatthew G Knepley   PetscFunctionBegin;
370188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
370288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
37032f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
3704e5e52638SMatthew G. Knepley     PetscInt d;
3705e5e52638SMatthew G. Knepley 
3706e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
37072f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3708ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
37092f0f8703SMatthew G. Knepley   }
371088ed4aceSMatthew G Knepley   *section = dm->defaultSection;
371188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
371288ed4aceSMatthew G Knepley }
371388ed4aceSMatthew G Knepley 
371488ed4aceSMatthew G Knepley /*@
3715e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
371688ed4aceSMatthew G Knepley 
371788ed4aceSMatthew G Knepley   Input Parameters:
371888ed4aceSMatthew G Knepley + dm - The DM
371988ed4aceSMatthew G Knepley - section - The PetscSection
372088ed4aceSMatthew G Knepley 
372188ed4aceSMatthew G Knepley   Level: intermediate
372288ed4aceSMatthew G Knepley 
372388ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
372488ed4aceSMatthew G Knepley 
3725e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
372688ed4aceSMatthew G Knepley @*/
3727e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
37280adebc6cSBarry Smith {
3729c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3730af122d2aSMatthew G Knepley   PetscInt       f;
373188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
373288ed4aceSMatthew G Knepley 
373388ed4aceSMatthew G Knepley   PetscFunctionBegin;
373488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3735c473ab19SMatthew G. Knepley   if (section) {
37361d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
37371d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3738c473ab19SMatthew G. Knepley   }
373988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
374088ed4aceSMatthew G Knepley   dm->defaultSection = section;
3741c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3742af122d2aSMatthew G Knepley   if (numFields) {
3743af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3744af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
37450f21e855SMatthew G. Knepley       PetscObject disc;
3746af122d2aSMatthew G Knepley       const char *name;
3747af122d2aSMatthew G Knepley 
3748af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
374944a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
37500f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3751af122d2aSMatthew G Knepley     }
3752af122d2aSMatthew G Knepley   }
3753e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
37541d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
375588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
375688ed4aceSMatthew G Knepley }
375788ed4aceSMatthew G Knepley 
37589435951eSToby Isaac /*@
37599435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
37609435951eSToby Isaac 
3761e228b242SToby Isaac   not collective
3762e228b242SToby Isaac 
37639435951eSToby Isaac   Input Parameter:
37649435951eSToby Isaac . dm - The DM
37659435951eSToby Isaac 
37669435951eSToby Isaac   Output Parameter:
37679435951eSToby 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.
37689435951eSToby 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.
37699435951eSToby Isaac 
37709435951eSToby Isaac   Level: advanced
37719435951eSToby Isaac 
37729435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
37739435951eSToby Isaac 
37749435951eSToby Isaac .seealso: DMSetDefaultConstraints()
37759435951eSToby Isaac @*/
37769435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
37779435951eSToby Isaac {
37789435951eSToby Isaac   PetscErrorCode ierr;
37799435951eSToby Isaac 
37809435951eSToby Isaac   PetscFunctionBegin;
37819435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37829435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
378345a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
378445a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
37859435951eSToby Isaac   PetscFunctionReturn(0);
37869435951eSToby Isaac }
37879435951eSToby Isaac 
37889435951eSToby Isaac /*@
37899435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
37909435951eSToby Isaac 
37919435951eSToby 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().
37929435951eSToby Isaac 
37939435951eSToby 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.
37949435951eSToby Isaac 
3795e228b242SToby Isaac   collective on dm
3796e228b242SToby Isaac 
37979435951eSToby Isaac   Input Parameters:
37989435951eSToby Isaac + dm - The DM
3799e228b242SToby 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).
3800e228b242SToby 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).
38019435951eSToby Isaac 
38029435951eSToby Isaac   Level: advanced
38039435951eSToby Isaac 
38049435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
38059435951eSToby Isaac 
38069435951eSToby Isaac .seealso: DMGetDefaultConstraints()
38079435951eSToby Isaac @*/
38089435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
38099435951eSToby Isaac {
3810e228b242SToby Isaac   PetscMPIInt result;
38119435951eSToby Isaac   PetscErrorCode ierr;
38129435951eSToby Isaac 
38139435951eSToby Isaac   PetscFunctionBegin;
38149435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3815e228b242SToby Isaac   if (section) {
3816e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3817e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3818f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3819e228b242SToby Isaac   }
3820e228b242SToby Isaac   if (mat) {
3821e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3822e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3823f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3824e228b242SToby Isaac   }
38259435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
38269435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
38279435951eSToby Isaac   dm->defaultConstraintSection = section;
38289435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
38299435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
38309435951eSToby Isaac   dm->defaultConstraintMat = mat;
38319435951eSToby Isaac   PetscFunctionReturn(0);
38329435951eSToby Isaac }
38339435951eSToby Isaac 
3834497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3835507e4973SMatthew G. Knepley /*
3836507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3837507e4973SMatthew G. Knepley 
3838507e4973SMatthew G. Knepley   Input Parameters:
3839507e4973SMatthew G. Knepley + dm - The DM
3840507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3841507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3842507e4973SMatthew G. Knepley 
3843507e4973SMatthew G. Knepley   Level: intermediate
3844507e4973SMatthew G. Knepley 
3845507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3846507e4973SMatthew G. Knepley */
3847f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3848507e4973SMatthew G. Knepley {
3849507e4973SMatthew G. Knepley   MPI_Comm        comm;
3850507e4973SMatthew G. Knepley   PetscLayout     layout;
3851507e4973SMatthew G. Knepley   const PetscInt *ranges;
3852507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3853507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3854507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3855507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3856507e4973SMatthew G. Knepley 
3857507e4973SMatthew G. Knepley   PetscFunctionBegin;
3858507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3859507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3860507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3861507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3862507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3863507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3864507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3865507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3866507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3867507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3868507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3869507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3870f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3871507e4973SMatthew G. Knepley 
3872507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3873507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3874507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3875507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3876507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3877507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3878507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3879507e4973SMatthew 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;}
3880507e4973SMatthew 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;}
3881507e4973SMatthew G. Knepley     if (gdof < 0) {
3882507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3883507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3884507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3885507e4973SMatthew G. Knepley 
3886507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3887507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3888507e4973SMatthew 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;}
3889507e4973SMatthew G. Knepley       }
3890507e4973SMatthew G. Knepley     }
3891507e4973SMatthew G. Knepley   }
3892507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3893507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3894b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3895507e4973SMatthew G. Knepley   if (!gvalid) {
3896507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3897507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3898507e4973SMatthew G. Knepley   }
3899507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3900507e4973SMatthew G. Knepley }
3901f741bcd2SMatthew G. Knepley #endif
3902507e4973SMatthew G. Knepley 
390388ed4aceSMatthew G Knepley /*@
3904e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
390588ed4aceSMatthew G Knepley 
39068b1ab98fSJed Brown   Collective on DM
39078b1ab98fSJed Brown 
390888ed4aceSMatthew G Knepley   Input Parameter:
390988ed4aceSMatthew G Knepley . dm - The DM
391088ed4aceSMatthew G Knepley 
391188ed4aceSMatthew G Knepley   Output Parameter:
391288ed4aceSMatthew G Knepley . section - The PetscSection
391388ed4aceSMatthew G Knepley 
391488ed4aceSMatthew G Knepley   Level: intermediate
391588ed4aceSMatthew G Knepley 
391688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
391788ed4aceSMatthew G Knepley 
3918e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
391988ed4aceSMatthew G Knepley @*/
3920e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
39210adebc6cSBarry Smith {
392288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
392388ed4aceSMatthew G Knepley 
392488ed4aceSMatthew G Knepley   PetscFunctionBegin;
392588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
392688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
392788ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3928fd59a867SMatthew G. Knepley     PetscSection s;
3929fd59a867SMatthew G. Knepley 
3930e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3931fd59a867SMatthew 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");
393233907cc2SStefano 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");
393315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3934cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3935ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3936685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
393788ed4aceSMatthew G Knepley   }
393888ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
393988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
394088ed4aceSMatthew G Knepley }
394188ed4aceSMatthew G Knepley 
3942b21d0597SMatthew G Knepley /*@
3943e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3944b21d0597SMatthew G Knepley 
3945b21d0597SMatthew G Knepley   Input Parameters:
3946b21d0597SMatthew G Knepley + dm - The DM
39475080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3948b21d0597SMatthew G Knepley 
3949b21d0597SMatthew G Knepley   Level: intermediate
3950b21d0597SMatthew G Knepley 
3951b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3952b21d0597SMatthew G Knepley 
3953e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3954b21d0597SMatthew G Knepley @*/
3955e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
39560adebc6cSBarry Smith {
3957b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3958b21d0597SMatthew G Knepley 
3959b21d0597SMatthew G Knepley   PetscFunctionBegin;
3960b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39615080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
39621d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3963b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3964b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3965497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3966f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3967507e4973SMatthew G. Knepley #endif
3968b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3969b21d0597SMatthew G Knepley }
3970b21d0597SMatthew G Knepley 
397188ed4aceSMatthew G Knepley /*@
397288ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
397388ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
397488ed4aceSMatthew G Knepley 
397588ed4aceSMatthew G Knepley   Input Parameter:
397688ed4aceSMatthew G Knepley . dm - The DM
397788ed4aceSMatthew G Knepley 
397888ed4aceSMatthew G Knepley   Output Parameter:
397988ed4aceSMatthew G Knepley . sf - The PetscSF
398088ed4aceSMatthew G Knepley 
398188ed4aceSMatthew G Knepley   Level: intermediate
398288ed4aceSMatthew G Knepley 
398388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
398488ed4aceSMatthew G Knepley 
398588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
398688ed4aceSMatthew G Knepley @*/
39870adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
39880adebc6cSBarry Smith {
398988ed4aceSMatthew G Knepley   PetscInt       nroots;
399088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
399188ed4aceSMatthew G Knepley 
399288ed4aceSMatthew G Knepley   PetscFunctionBegin;
399388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
399488ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
399533907cc2SStefano Zampini   if (!dm->defaultSF) {
399633907cc2SStefano Zampini     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->defaultSF);CHKERRQ(ierr);
399733907cc2SStefano Zampini   }
39980298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
399988ed4aceSMatthew G Knepley   if (nroots < 0) {
400088ed4aceSMatthew G Knepley     PetscSection section, gSection;
400188ed4aceSMatthew G Knepley 
4002e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
400331ea6d37SMatthew G Knepley     if (section) {
4004e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
400588ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
400631ea6d37SMatthew G Knepley     } else {
40070298fd71SBarry Smith       *sf = NULL;
400831ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
400931ea6d37SMatthew G Knepley     }
401088ed4aceSMatthew G Knepley   }
401188ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
401288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
401388ed4aceSMatthew G Knepley }
401488ed4aceSMatthew G Knepley 
401588ed4aceSMatthew G Knepley /*@
401688ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
401788ed4aceSMatthew G Knepley 
401888ed4aceSMatthew G Knepley   Input Parameters:
401988ed4aceSMatthew G Knepley + dm - The DM
402088ed4aceSMatthew G Knepley - sf - The PetscSF
402188ed4aceSMatthew G Knepley 
402288ed4aceSMatthew G Knepley   Level: intermediate
402388ed4aceSMatthew G Knepley 
402488ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
402588ed4aceSMatthew G Knepley 
402688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
402788ed4aceSMatthew G Knepley @*/
40280adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
40290adebc6cSBarry Smith {
403088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
403188ed4aceSMatthew G Knepley 
403288ed4aceSMatthew G Knepley   PetscFunctionBegin;
403388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
403433907cc2SStefano Zampini   if (sf) {
403588ed4aceSMatthew G Knepley     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
403633907cc2SStefano Zampini     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
403733907cc2SStefano Zampini   }
403888ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
403988ed4aceSMatthew G Knepley   dm->defaultSF = sf;
404088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
404188ed4aceSMatthew G Knepley }
404288ed4aceSMatthew G Knepley 
404388ed4aceSMatthew G Knepley /*@C
404488ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
404588ed4aceSMatthew G Knepley   describing the data layout.
404688ed4aceSMatthew G Knepley 
404788ed4aceSMatthew G Knepley   Input Parameters:
404888ed4aceSMatthew G Knepley + dm - The DM
404988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
405088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
405188ed4aceSMatthew G Knepley 
405288ed4aceSMatthew G Knepley   Level: intermediate
405388ed4aceSMatthew G Knepley 
405488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
405588ed4aceSMatthew G Knepley @*/
405688ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
405788ed4aceSMatthew G Knepley {
405882f516ccSBarry Smith   MPI_Comm       comm;
405988ed4aceSMatthew G Knepley   PetscLayout    layout;
406088ed4aceSMatthew G Knepley   const PetscInt *ranges;
406188ed4aceSMatthew G Knepley   PetscInt       *local;
406288ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4063ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
406488ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
406588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
406688ed4aceSMatthew G Knepley 
406788ed4aceSMatthew G Knepley   PetscFunctionBegin;
406888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4069367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
407088ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
407188ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
407288ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
407388ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
407488ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
407588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
407688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
407788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
407888ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4079ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
40806636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
408188ed4aceSMatthew G Knepley 
40826636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40836636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4084235fbf56SMatthew 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));
40856636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
408688ed4aceSMatthew G Knepley   }
4087785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4088785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
408988ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
40901f588964SMatthew G Knepley     const PetscInt *cind;
40916636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
409288ed4aceSMatthew G Knepley 
409388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
409488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
409588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
409688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
409788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40986636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
409988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
41006636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
41016636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
41026636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4103057b4bcdSMatthew 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);
41046636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
41056636e97aSMatthew G Knepley     }
410688ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
410788ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
410888ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
410988ed4aceSMatthew G Knepley     }
411088ed4aceSMatthew G Knepley     if (gdof < 0) {
41116636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
411288ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
411388ed4aceSMatthew G Knepley 
411405376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
411531d3f06eSJed Brown         if (r < 0) r = -(r+2);
411605376888SMatthew 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);
411788ed4aceSMatthew G Knepley         remote[l].rank  = r;
411888ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
411988ed4aceSMatthew G Knepley       }
412088ed4aceSMatthew G Knepley     } else {
41216636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
412288ed4aceSMatthew G Knepley         remote[l].rank  = rank;
412388ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
412488ed4aceSMatthew G Knepley       }
412588ed4aceSMatthew G Knepley     }
412688ed4aceSMatthew G Knepley   }
41276636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
412888ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
412988ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
413088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
413188ed4aceSMatthew G Knepley }
4132af122d2aSMatthew G Knepley 
4133b21d0597SMatthew G Knepley /*@
4134b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4135b21d0597SMatthew G Knepley 
4136b21d0597SMatthew G Knepley   Input Parameter:
4137b21d0597SMatthew G Knepley . dm - The DM
4138b21d0597SMatthew G Knepley 
4139b21d0597SMatthew G Knepley   Output Parameter:
4140b21d0597SMatthew G Knepley . sf - The PetscSF
4141b21d0597SMatthew G Knepley 
4142b21d0597SMatthew G Knepley   Level: intermediate
4143b21d0597SMatthew G Knepley 
4144b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4145b21d0597SMatthew G Knepley 
4146057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4147b21d0597SMatthew G Knepley @*/
41480adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
41490adebc6cSBarry Smith {
4150b21d0597SMatthew G Knepley   PetscFunctionBegin;
4151b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4152b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4153b21d0597SMatthew G Knepley   *sf = dm->sf;
4154b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4155b21d0597SMatthew G Knepley }
4156b21d0597SMatthew G Knepley 
4157057b4bcdSMatthew G Knepley /*@
4158057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4159057b4bcdSMatthew G Knepley 
4160057b4bcdSMatthew G Knepley   Input Parameters:
4161057b4bcdSMatthew G Knepley + dm - The DM
4162057b4bcdSMatthew G Knepley - sf - The PetscSF
4163057b4bcdSMatthew G Knepley 
4164057b4bcdSMatthew G Knepley   Level: intermediate
4165057b4bcdSMatthew G Knepley 
4166057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4167057b4bcdSMatthew G Knepley @*/
41680adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
41690adebc6cSBarry Smith {
4170057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4171057b4bcdSMatthew G Knepley 
4172057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4173057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
417433907cc2SStefano Zampini   if (sf) {
417533907cc2SStefano Zampini     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4176057b4bcdSMatthew G Knepley     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
417733907cc2SStefano Zampini   }
417833907cc2SStefano Zampini   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4179057b4bcdSMatthew G Knepley   dm->sf = sf;
4180057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4181057b4bcdSMatthew G Knepley }
4182057b4bcdSMatthew G Knepley 
418334aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
418434aa8a36SMatthew G. Knepley {
418534aa8a36SMatthew G. Knepley   PetscClassId   id;
418634aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
418734aa8a36SMatthew G. Knepley 
418834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
418934aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
419034aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
419134aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
419234aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
419334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
419417c1d62eSMatthew G. Knepley   } else {
419517c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
419634aa8a36SMatthew G. Knepley   }
419734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
419834aa8a36SMatthew G. Knepley }
419934aa8a36SMatthew G. Knepley 
420044a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
420144a7f3ddSMatthew G. Knepley {
420244a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
420344a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
420444a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
420544a7f3ddSMatthew G. Knepley 
420644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
420744a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
420844a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
420944a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
421044a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
421144a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
421244a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
421344a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
421444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
421544a7f3ddSMatthew G. Knepley }
421644a7f3ddSMatthew G. Knepley 
421744a7f3ddSMatthew G. Knepley /*@
421844a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
421944a7f3ddSMatthew G. Knepley 
422044a7f3ddSMatthew G. Knepley   Logically collective on DM
422144a7f3ddSMatthew G. Knepley 
422244a7f3ddSMatthew G. Knepley   Input Parameter:
422344a7f3ddSMatthew G. Knepley . dm - The DM
422444a7f3ddSMatthew G. Knepley 
422544a7f3ddSMatthew G. Knepley   Level: intermediate
422644a7f3ddSMatthew G. Knepley 
422744a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
422844a7f3ddSMatthew G. Knepley @*/
422944a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
423044a7f3ddSMatthew G. Knepley {
423144a7f3ddSMatthew G. Knepley   PetscInt       f;
423244a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
423344a7f3ddSMatthew G. Knepley 
423444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
423544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
423644a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
423744a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
423844a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
423944a7f3ddSMatthew G. Knepley   }
424044a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
424144a7f3ddSMatthew G. Knepley   dm->fields = NULL;
424244a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
424344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
424444a7f3ddSMatthew G. Knepley }
424544a7f3ddSMatthew G. Knepley 
4246689b5837SMatthew G. Knepley /*@
4247689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4248689b5837SMatthew G. Knepley 
4249689b5837SMatthew G. Knepley   Not collective
4250689b5837SMatthew G. Knepley 
4251689b5837SMatthew G. Knepley   Input Parameter:
4252689b5837SMatthew G. Knepley . dm - The DM
4253689b5837SMatthew G. Knepley 
4254689b5837SMatthew G. Knepley   Output Parameter:
4255689b5837SMatthew G. Knepley . Nf - The number of fields
4256689b5837SMatthew G. Knepley 
4257689b5837SMatthew G. Knepley   Level: intermediate
4258689b5837SMatthew G. Knepley 
4259689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4260689b5837SMatthew G. Knepley @*/
42610f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
42620f21e855SMatthew G. Knepley {
42630f21e855SMatthew G. Knepley   PetscFunctionBegin;
42640f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
426544a7f3ddSMatthew G. Knepley   PetscValidPointer(numFields, 2);
426644a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4267af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4268af122d2aSMatthew G Knepley }
4269af122d2aSMatthew G Knepley 
4270689b5837SMatthew G. Knepley /*@
4271689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4272689b5837SMatthew G. Knepley 
4273689b5837SMatthew G. Knepley   Logically collective on DM
4274689b5837SMatthew G. Knepley 
4275689b5837SMatthew G. Knepley   Input Parameters:
4276689b5837SMatthew G. Knepley + dm - The DM
4277689b5837SMatthew G. Knepley - Nf - The number of fields
4278689b5837SMatthew G. Knepley 
4279689b5837SMatthew G. Knepley   Level: intermediate
4280689b5837SMatthew G. Knepley 
4281689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4282689b5837SMatthew G. Knepley @*/
4283af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4284af122d2aSMatthew G Knepley {
42850f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4286af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4287af122d2aSMatthew G Knepley 
4288af122d2aSMatthew G Knepley   PetscFunctionBegin;
4289af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429044a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
42910f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
42920f21e855SMatthew G. Knepley     PetscContainer obj;
42930f21e855SMatthew G. Knepley 
42940f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
429544a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
42960f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4297af122d2aSMatthew G Knepley   }
4298af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4299af122d2aSMatthew G Knepley }
4300af122d2aSMatthew G Knepley 
4301c1929be8SMatthew G. Knepley /*@
4302c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4303c1929be8SMatthew G. Knepley 
4304c1929be8SMatthew G. Knepley   Not collective
4305c1929be8SMatthew G. Knepley 
4306c1929be8SMatthew G. Knepley   Input Parameters:
4307c1929be8SMatthew G. Knepley + dm - The DM
4308c1929be8SMatthew G. Knepley - f  - The field number
4309c1929be8SMatthew G. Knepley 
431044a7f3ddSMatthew G. Knepley   Output Parameters:
431144a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
431244a7f3ddSMatthew G. Knepley - field - The discretization object
4313c1929be8SMatthew G. Knepley 
431444a7f3ddSMatthew G. Knepley   Level: intermediate
4315c1929be8SMatthew G. Knepley 
431644a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4317c1929be8SMatthew G. Knepley @*/
431844a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4319af122d2aSMatthew G Knepley {
4320af122d2aSMatthew G Knepley   PetscFunctionBegin;
4321af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
432244a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
432344a7f3ddSMatthew 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);
432444a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
432544a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4326decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4327decb47aaSMatthew G. Knepley }
4328decb47aaSMatthew G. Knepley 
4329c1929be8SMatthew G. Knepley /*@
4330c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4331c1929be8SMatthew G. Knepley 
4332c1929be8SMatthew G. Knepley   Logically collective on DM
4333c1929be8SMatthew G. Knepley 
4334c1929be8SMatthew G. Knepley   Input Parameters:
4335c1929be8SMatthew G. Knepley + dm    - The DM
4336c1929be8SMatthew G. Knepley . f     - The field number
433744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4338c1929be8SMatthew G. Knepley - field - The discretization object
4339c1929be8SMatthew G. Knepley 
434044a7f3ddSMatthew G. Knepley   Level: intermediate
4341c1929be8SMatthew G. Knepley 
434244a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4343c1929be8SMatthew G. Knepley @*/
434444a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4345decb47aaSMatthew G. Knepley {
4346decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4347decb47aaSMatthew G. Knepley 
4348decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4349decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4350e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
435144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4352e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
435344a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
435444a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
435544a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
435644a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
435744a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
435844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
435944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
436034aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
43612df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
436244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
436344a7f3ddSMatthew G. Knepley }
436444a7f3ddSMatthew G. Knepley 
436544a7f3ddSMatthew G. Knepley /*@
436644a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
436744a7f3ddSMatthew G. Knepley 
436844a7f3ddSMatthew G. Knepley   Logically collective on DM
436944a7f3ddSMatthew G. Knepley 
437044a7f3ddSMatthew G. Knepley   Input Parameters:
437144a7f3ddSMatthew G. Knepley + dm    - The DM
437244a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
437344a7f3ddSMatthew G. Knepley - field - The discretization object
437444a7f3ddSMatthew G. Knepley 
437544a7f3ddSMatthew G. Knepley   Level: intermediate
437644a7f3ddSMatthew G. Knepley 
437744a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
437844a7f3ddSMatthew G. Knepley @*/
437944a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
438044a7f3ddSMatthew G. Knepley {
438144a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
438244a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
438344a7f3ddSMatthew G. Knepley 
438444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
438544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
438644a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
438744a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
438844a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
438944a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
439044a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
439144a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
439244a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
439334aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
43942df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4395af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4396af122d2aSMatthew G Knepley }
43976636e97aSMatthew G Knepley 
4398e5e52638SMatthew G. Knepley /*@
4399e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4400e5e52638SMatthew G. Knepley 
4401e5e52638SMatthew G. Knepley   Collective on DM
4402e5e52638SMatthew G. Knepley 
4403e5e52638SMatthew G. Knepley   Input Parameter:
4404e5e52638SMatthew G. Knepley . dm - The DM
4405e5e52638SMatthew G. Knepley 
4406e5e52638SMatthew G. Knepley   Output Parameter:
4407e5e52638SMatthew G. Knepley . newdm - The DM
4408e5e52638SMatthew G. Knepley 
4409e5e52638SMatthew G. Knepley   Level: advanced
4410e5e52638SMatthew G. Knepley 
4411e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4412e5e52638SMatthew G. Knepley @*/
4413e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4414e5e52638SMatthew G. Knepley {
4415e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4416e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4417e5e52638SMatthew G. Knepley 
4418e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4419e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4420e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4421e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4422e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4423e5e52638SMatthew G. Knepley     DMLabel     label;
4424e5e52638SMatthew G. Knepley     PetscObject field;
442534aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4426e5e52638SMatthew G. Knepley 
4427e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4428e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
442934aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
443034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
443134aa8a36SMatthew G. Knepley   }
443234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
443334aa8a36SMatthew G. Knepley }
443434aa8a36SMatthew G. Knepley 
443534aa8a36SMatthew G. Knepley /*@
443634aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
443734aa8a36SMatthew G. Knepley 
443834aa8a36SMatthew G. Knepley   Not collective
443934aa8a36SMatthew G. Knepley 
444034aa8a36SMatthew G. Knepley   Input Parameters:
444134aa8a36SMatthew G. Knepley + dm - The DM object
444234aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
444334aa8a36SMatthew G. Knepley 
444434aa8a36SMatthew G. Knepley   Output Parameter:
444534aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
444634aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
444734aa8a36SMatthew G. Knepley 
444834aa8a36SMatthew G. Knepley   Notes:
444934aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
445034aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
445134aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4452979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
445334aa8a36SMatthew G. Knepley 
445434aa8a36SMatthew G. Knepley   Level: developer
445534aa8a36SMatthew G. Knepley 
445634aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
445734aa8a36SMatthew G. Knepley @*/
445834aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
445934aa8a36SMatthew G. Knepley {
446034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
446134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
446234aa8a36SMatthew G. Knepley   if (useCone) PetscValidPointer(useCone, 3);
446334aa8a36SMatthew G. Knepley   if (useClosure) PetscValidPointer(useClosure, 4);
446434aa8a36SMatthew G. Knepley   if (f < 0) {
446534aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
446634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
446734aa8a36SMatthew G. Knepley   } else {
446834aa8a36SMatthew G. Knepley     PetscInt       Nf;
446934aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
447034aa8a36SMatthew G. Knepley 
447134aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
447234aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
447334aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
447434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
447534aa8a36SMatthew G. Knepley   }
447634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
447734aa8a36SMatthew G. Knepley }
447834aa8a36SMatthew G. Knepley 
447934aa8a36SMatthew G. Knepley /*@
448034aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
448134aa8a36SMatthew G. Knepley 
448234aa8a36SMatthew G. Knepley   Not collective
448334aa8a36SMatthew G. Knepley 
448434aa8a36SMatthew G. Knepley   Input Parameters:
448534aa8a36SMatthew G. Knepley + dm         - The DM object
448634aa8a36SMatthew G. Knepley . f          - The field number
448734aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
448834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
448934aa8a36SMatthew G. Knepley 
449034aa8a36SMatthew G. Knepley   Notes:
449134aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
449234aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
449334aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4494979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
449534aa8a36SMatthew G. Knepley 
449634aa8a36SMatthew G. Knepley   Level: developer
449734aa8a36SMatthew G. Knepley 
449834aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
449934aa8a36SMatthew G. Knepley @*/
450034aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
450134aa8a36SMatthew G. Knepley {
450234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
450334aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
450434aa8a36SMatthew G. Knepley   if (f < 0) {
450534aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
450634aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
450734aa8a36SMatthew G. Knepley   } else {
450834aa8a36SMatthew G. Knepley     PetscInt       Nf;
450934aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
451034aa8a36SMatthew G. Knepley 
451134aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
451234aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
451334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
451434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4515e5e52638SMatthew G. Knepley   }
4516e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4517e5e52638SMatthew G. Knepley }
4518e5e52638SMatthew G. Knepley 
4519b0441da4SMatthew G. Knepley /*@
4520b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4521b0441da4SMatthew G. Knepley 
4522b0441da4SMatthew G. Knepley   Not collective
4523b0441da4SMatthew G. Knepley 
4524b0441da4SMatthew G. Knepley   Input Parameters:
4525b0441da4SMatthew G. Knepley . dm - The DM object
4526b0441da4SMatthew G. Knepley 
4527b0441da4SMatthew G. Knepley   Output Parameter:
4528b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4529b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4530b0441da4SMatthew G. Knepley 
4531b0441da4SMatthew G. Knepley   Notes:
4532b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4533b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4534b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4535b0441da4SMatthew G. Knepley 
4536b0441da4SMatthew G. Knepley   Level: developer
4537b0441da4SMatthew G. Knepley 
4538b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4539b0441da4SMatthew G. Knepley @*/
4540b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4541b0441da4SMatthew G. Knepley {
4542b0441da4SMatthew G. Knepley   PetscInt       Nf;
4543b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4544b0441da4SMatthew G. Knepley 
4545b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4546b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4547b0441da4SMatthew G. Knepley   if (useCone) PetscValidPointer(useCone, 3);
4548b0441da4SMatthew G. Knepley   if (useClosure) PetscValidPointer(useClosure, 4);
4549b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4550b0441da4SMatthew G. Knepley   if (!Nf) {
4551b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4552b0441da4SMatthew G. Knepley   } else {
4553b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4554b0441da4SMatthew G. Knepley   }
4555b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4556b0441da4SMatthew G. Knepley }
4557b0441da4SMatthew G. Knepley 
4558b0441da4SMatthew G. Knepley /*@
4559b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4560b0441da4SMatthew G. Knepley 
4561b0441da4SMatthew G. Knepley   Not collective
4562b0441da4SMatthew G. Knepley 
4563b0441da4SMatthew G. Knepley   Input Parameters:
4564b0441da4SMatthew G. Knepley + dm         - The DM object
4565b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4566b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4567b0441da4SMatthew G. Knepley 
4568b0441da4SMatthew G. Knepley   Notes:
4569b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4570b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4571b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4572b0441da4SMatthew G. Knepley 
4573b0441da4SMatthew G. Knepley   Level: developer
4574b0441da4SMatthew G. Knepley 
4575b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4576b0441da4SMatthew G. Knepley @*/
4577b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4578b0441da4SMatthew G. Knepley {
4579b0441da4SMatthew G. Knepley   PetscInt       Nf;
4580b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4581b0441da4SMatthew G. Knepley 
4582b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4583b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4584b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4585b0441da4SMatthew G. Knepley   if (!Nf) {
4586b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4587b0441da4SMatthew G. Knepley   } else {
4588b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4589e5e52638SMatthew G. Knepley   }
4590e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4591e5e52638SMatthew G. Knepley }
4592e5e52638SMatthew G. Knepley 
4593e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4594e5e52638SMatthew G. Knepley {
4595e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4596e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4597e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4598e5e52638SMatthew G. Knepley 
4599e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4600e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4601e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4602e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4603*b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4604e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4605e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4606e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4607e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4608e5e52638SMatthew G. Knepley }
4609e5e52638SMatthew G. Knepley 
4610e5e52638SMatthew G. Knepley /*@
4611e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4612e5e52638SMatthew G. Knepley 
4613e5e52638SMatthew G. Knepley   Not collective
4614e5e52638SMatthew G. Knepley 
4615e5e52638SMatthew G. Knepley   Input Parameter:
4616e5e52638SMatthew G. Knepley . dm - The DM
4617e5e52638SMatthew G. Knepley 
4618e5e52638SMatthew G. Knepley   Output Parameter:
4619e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4620e5e52638SMatthew G. Knepley 
4621e5e52638SMatthew G. Knepley   Level: intermediate
4622e5e52638SMatthew G. Knepley 
4623e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4624e5e52638SMatthew G. Knepley @*/
4625e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4626e5e52638SMatthew G. Knepley {
4627e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4628e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4629e5e52638SMatthew G. Knepley   PetscValidPointer(Nds, 2);
4630e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4631e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4632e5e52638SMatthew G. Knepley }
4633e5e52638SMatthew G. Knepley 
4634e5e52638SMatthew G. Knepley /*@
4635e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4636e5e52638SMatthew G. Knepley 
4637e5e52638SMatthew G. Knepley   Logically collective on DM
4638e5e52638SMatthew G. Knepley 
4639e5e52638SMatthew G. Knepley   Input Parameter:
4640e5e52638SMatthew G. Knepley . dm - The DM
4641e5e52638SMatthew G. Knepley 
4642e5e52638SMatthew G. Knepley   Level: intermediate
4643e5e52638SMatthew G. Knepley 
4644e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4645e5e52638SMatthew G. Knepley @*/
4646e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4647e5e52638SMatthew G. Knepley {
4648e5e52638SMatthew G. Knepley   PetscInt       s;
4649e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4650e5e52638SMatthew G. Knepley 
4651e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4652e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4653e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4654e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4655e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4656*b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4657e5e52638SMatthew G. Knepley   }
4658e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4659e5e52638SMatthew G. Knepley   dm->probs = NULL;
4660e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4661e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4662e5e52638SMatthew G. Knepley }
4663e5e52638SMatthew G. Knepley 
4664e5e52638SMatthew G. Knepley /*@
4665e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4666e5e52638SMatthew G. Knepley 
4667e5e52638SMatthew G. Knepley   Not collective
4668e5e52638SMatthew G. Knepley 
4669e5e52638SMatthew G. Knepley   Input Parameter:
4670e5e52638SMatthew G. Knepley . dm    - The DM
4671e5e52638SMatthew G. Knepley 
4672e5e52638SMatthew G. Knepley   Output Parameter:
4673e5e52638SMatthew G. Knepley . prob - The default PetscDS
4674e5e52638SMatthew G. Knepley 
4675e5e52638SMatthew G. Knepley   Level: intermediate
4676e5e52638SMatthew G. Knepley 
4677e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4678e5e52638SMatthew G. Knepley @*/
4679e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4680e5e52638SMatthew G. Knepley {
4681b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4682b0143b4dSMatthew G. Knepley 
4683e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4684e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4685e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4686b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4687b0143b4dSMatthew G. Knepley     PetscDS ds;
4688b0143b4dSMatthew G. Knepley 
4689b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4690*b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4691b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4692b0143b4dSMatthew G. Knepley   }
4693b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4694e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4695e5e52638SMatthew G. Knepley }
4696e5e52638SMatthew G. Knepley 
4697e5e52638SMatthew G. Knepley /*@
4698e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4699e5e52638SMatthew G. Knepley 
4700e5e52638SMatthew G. Knepley   Not collective
4701e5e52638SMatthew G. Knepley 
4702e5e52638SMatthew G. Knepley   Input Parameters:
4703e5e52638SMatthew G. Knepley + dm    - The DM
4704e5e52638SMatthew G. Knepley - point - Cell for the DS
4705e5e52638SMatthew G. Knepley 
4706e5e52638SMatthew G. Knepley   Output Parameter:
4707e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4708e5e52638SMatthew G. Knepley 
4709e5e52638SMatthew G. Knepley   Level: developer
4710e5e52638SMatthew G. Knepley 
4711b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
4712e5e52638SMatthew G. Knepley @*/
4713e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4714e5e52638SMatthew G. Knepley {
4715e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
4716e5e52638SMatthew G. Knepley   PetscInt       s;
4717e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4718e5e52638SMatthew G. Knepley 
4719e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4720e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4721e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
4722e5e52638SMatthew G. Knepley   *prob = NULL;
4723e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4724e5e52638SMatthew G. Knepley     PetscInt val;
4725e5e52638SMatthew G. Knepley 
4726e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
4727e5e52638SMatthew G. Knepley     else {
4728e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
4729e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
4730e5e52638SMatthew G. Knepley     }
4731e5e52638SMatthew G. Knepley   }
4732e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
4733e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4734e5e52638SMatthew G. Knepley }
4735e5e52638SMatthew G. Knepley 
4736e5e52638SMatthew G. Knepley /*@
4737e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
4738e5e52638SMatthew G. Knepley 
4739e5e52638SMatthew G. Knepley   Not collective
4740e5e52638SMatthew G. Knepley 
4741e5e52638SMatthew G. Knepley   Input Parameters:
4742e5e52638SMatthew G. Knepley + dm    - The DM
4743e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
4744e5e52638SMatthew G. Knepley 
4745*b3cf3223SMatthew G. Knepley   Output Parameters:
4746*b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
4747*b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
4748e5e52638SMatthew G. Knepley 
4749e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
4750e5e52638SMatthew G. Knepley 
4751e5e52638SMatthew G. Knepley   Level: advanced
4752e5e52638SMatthew G. Knepley 
4753e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4754e5e52638SMatthew G. Knepley @*/
4755*b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
4756e5e52638SMatthew G. Knepley {
4757e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
4758e5e52638SMatthew G. Knepley 
4759e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4760e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4761e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4762*b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
4763*b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
4764e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4765*b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
4766*b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
4767*b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
4768*b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
4769*b3cf3223SMatthew G. Knepley     }
4770e5e52638SMatthew G. Knepley   }
47712df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
4772e5e52638SMatthew G. Knepley }
4773e5e52638SMatthew G. Knepley 
4774e5e52638SMatthew G. Knepley /*@
4775e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
4776e5e52638SMatthew G. Knepley 
4777e5e52638SMatthew G. Knepley   Not collective
4778e5e52638SMatthew G. Knepley 
4779e5e52638SMatthew G. Knepley   Input Parameters:
4780e5e52638SMatthew G. Knepley + dm  - The DM
4781e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
4782e5e52638SMatthew G. Knepley 
4783e5e52638SMatthew G. Knepley   Output Parameters:
4784*b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
4785*b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
4786*b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
4787e5e52638SMatthew G. Knepley 
4788e5e52638SMatthew G. Knepley   Level: advanced
4789e5e52638SMatthew G. Knepley 
4790e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4791e5e52638SMatthew G. Knepley @*/
4792*b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
4793e5e52638SMatthew G. Knepley {
4794e5e52638SMatthew G. Knepley   PetscInt       Nds;
4795e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4796e5e52638SMatthew G. Knepley 
4797e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4798e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4799e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4800e5e52638SMatthew 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);
4801e5e52638SMatthew G. Knepley   if (label) {
4802e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
4803e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
4804e5e52638SMatthew G. Knepley   }
4805*b3cf3223SMatthew G. Knepley   if (fields) {
4806*b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
4807*b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
4808*b3cf3223SMatthew G. Knepley   }
4809e5e52638SMatthew G. Knepley   if (ds) {
4810*b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
4811e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
4812e5e52638SMatthew G. Knepley   }
4813e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4814e5e52638SMatthew G. Knepley }
4815e5e52638SMatthew G. Knepley 
4816e5e52638SMatthew G. Knepley /*@
4817e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
4818e5e52638SMatthew G. Knepley 
4819e5e52638SMatthew G. Knepley   Collective on DM
4820e5e52638SMatthew G. Knepley 
4821e5e52638SMatthew G. Knepley   Input Parameters:
4822e5e52638SMatthew G. Knepley + dm     - The DM
4823e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
4824*b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
4825e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
4826e5e52638SMatthew G. Knepley 
4827*b3cf3223SMatthew 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,
4828*b3cf3223SMatthew G. Knepley   the fields argument is ignored.
4829e5e52638SMatthew G. Knepley 
4830e5e52638SMatthew G. Knepley   Level: advanced
4831e5e52638SMatthew G. Knepley 
4832e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
4833e5e52638SMatthew G. Knepley @*/
4834*b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
4835e5e52638SMatthew G. Knepley {
4836e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4837e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4838e5e52638SMatthew G. Knepley 
4839e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4840e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4841e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4842e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
4843e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4844e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
4845*b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4846e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
4847e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
4848e5e52638SMatthew G. Knepley     }
4849e5e52638SMatthew G. Knepley   }
48501b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
4851e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4852*b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
4853e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
4854e5e52638SMatthew G. Knepley   if (!label) {
4855e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
4856e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
4857e5e52638SMatthew G. Knepley     Nds = 0;
4858e5e52638SMatthew G. Knepley   }
4859e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
4860*b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
4861e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
4862e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4863e5e52638SMatthew G. Knepley }
4864e5e52638SMatthew G. Knepley 
4865e5e52638SMatthew G. Knepley /*@
4866e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
4867e5e52638SMatthew G. Knepley 
4868e5e52638SMatthew G. Knepley   Collective on DM
4869e5e52638SMatthew G. Knepley 
4870e5e52638SMatthew G. Knepley   Input Parameter:
4871e5e52638SMatthew G. Knepley . dm - The DM
4872e5e52638SMatthew G. Knepley 
4873e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
4874e5e52638SMatthew G. Knepley 
4875e5e52638SMatthew G. Knepley   Level: intermediate
4876e5e52638SMatthew G. Knepley 
4877e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
4878e5e52638SMatthew G. Knepley @*/
4879e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
4880e5e52638SMatthew G. Knepley {
4881e5e52638SMatthew G. Knepley   MPI_Comm       comm;
4882e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
4883*b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
4884e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
4885e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4886e5e52638SMatthew G. Knepley 
4887e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4888e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4889e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
4890e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
4891e5e52638SMatthew G. Knepley    1) NULL
4892e5e52638SMatthew G. Knepley    2) Hybrid cells
4893e5e52638SMatthew G. Knepley   */
4894e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
4895e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4896e5e52638SMatthew G. Knepley   /* Create default DS */
4897*b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
48982df9ee95SMatthew G. Knepley   if (!prob) {
4899*b3cf3223SMatthew G. Knepley     IS        fields;
4900*b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
4901*b3cf3223SMatthew G. Knepley 
4902*b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
4903*b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
4904*b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
4905*b3cf3223SMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, nf, fld, PETSC_OWN_POINTER, &fields);CHKERRQ(ierr);
49062df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
4907*b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
49082df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
4909*b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
4910*b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
49112df9ee95SMatthew G. Knepley   }
4912e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
4913e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
4914*b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4915e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
4916e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
4917e5e52638SMatthew G. Knepley 
4918e5e52638SMatthew G. Knepley     if (label) {
49190122748bSMatthew G. Knepley       DM       plex;
49200122748bSMatthew G. Knepley       PetscInt depth, pMax[4];
49210122748bSMatthew G. Knepley 
49220122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
49230122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
49240122748bSMatthew 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);
49250122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
49260122748bSMatthew G. Knepley 
4927e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
4928e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
4929e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
4930*b3cf3223SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, NULL, probh);CHKERRQ(ierr);
4931e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
4932e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
4933e5e52638SMatthew G. Knepley       break;
4934e5e52638SMatthew G. Knepley     }
4935e5e52638SMatthew G. Knepley   }
4936e5e52638SMatthew G. Knepley   /* Set fields in DSes */
4937*b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4938e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
4939e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
4940e5e52638SMatthew G. Knepley 
4941e5e52638SMatthew G. Knepley     if (!label) {
4942e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
4943e5e52638SMatthew G. Knepley       if (probh) {
4944e5e52638SMatthew G. Knepley         PetscFE subfe;
4945e5e52638SMatthew G. Knepley 
4946e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
4947e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
4948e5e52638SMatthew G. Knepley       }
4949e5e52638SMatthew G. Knepley     } else {
4950e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
4951e5e52638SMatthew G. Knepley     }
4952e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
4953e5e52638SMatthew G. Knepley     {
4954e5e52638SMatthew G. Knepley       PetscClassId id;
4955e5e52638SMatthew G. Knepley 
4956e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
4957e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
4958e5e52638SMatthew G. Knepley     }
4959e5e52638SMatthew G. Knepley   }
4960e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
4961e5e52638SMatthew G. Knepley   /* Setup DSes */
4962e5e52638SMatthew G. Knepley   if (doSetup) {
4963e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
4964e5e52638SMatthew G. Knepley   }
4965e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4966e5e52638SMatthew G. Knepley }
4967e5e52638SMatthew G. Knepley 
4968e5e52638SMatthew G. Knepley /*@
4969e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
4970e5e52638SMatthew G. Knepley 
4971e5e52638SMatthew G. Knepley   Collective on DM
4972e5e52638SMatthew G. Knepley 
4973e5e52638SMatthew G. Knepley   Input Parameter:
4974e5e52638SMatthew G. Knepley . dm - The DM
4975e5e52638SMatthew G. Knepley 
4976e5e52638SMatthew G. Knepley   Output Parameter:
4977e5e52638SMatthew G. Knepley . newdm - The DM
4978e5e52638SMatthew G. Knepley 
4979e5e52638SMatthew G. Knepley   Level: advanced
4980e5e52638SMatthew G. Knepley 
4981e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
4982e5e52638SMatthew G. Knepley @*/
4983e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
4984e5e52638SMatthew G. Knepley {
4985e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
4986e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4987e5e52638SMatthew G. Knepley 
4988e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4989e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4990e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4991e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
4992e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4993e5e52638SMatthew G. Knepley     DMLabel label;
4994*b3cf3223SMatthew G. Knepley     IS      fields;
4995e5e52638SMatthew G. Knepley     PetscDS ds;
4996e5e52638SMatthew G. Knepley 
4997*b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
4998*b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
4999e5e52638SMatthew G. Knepley   }
5000e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5001e5e52638SMatthew G. Knepley }
5002e5e52638SMatthew G. Knepley 
5003e5e52638SMatthew G. Knepley /*@
5004e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another 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   Output Parameter:
5012e5e52638SMatthew G. Knepley . newdm - The DM
5013e5e52638SMatthew G. Knepley 
5014e5e52638SMatthew G. Knepley   Level: advanced
5015e5e52638SMatthew G. Knepley 
5016e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5017e5e52638SMatthew G. Knepley @*/
5018e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5019e5e52638SMatthew G. Knepley {
5020e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5021e5e52638SMatthew G. Knepley 
5022e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5023e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5024e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5025e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5026e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5027e5e52638SMatthew G. Knepley }
5028e5e52638SMatthew G. Knepley 
5029b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5030b64e0483SPeter Brune {
5031b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5032b64e0483SPeter Brune   PetscErrorCode ierr;
5033b64e0483SPeter Brune   Vec coords,ccoords;
50346dbf9973SLawrence Mitchell   Mat inject;
5035b64e0483SPeter Brune   PetscFunctionBegin;
5036b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5037b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5038b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5039b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5040b64e0483SPeter Brune   if (coords && !ccoords) {
5041b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
50426668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
50436dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
50442adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
50456dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5046b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5047b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5048b64e0483SPeter Brune   }
5049b64e0483SPeter Brune   PetscFunctionReturn(0);
5050b64e0483SPeter Brune }
5051b64e0483SPeter Brune 
505203dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
505303dadc2fSPeter Brune {
505403dadc2fSPeter Brune   DM dm_coord,subdm_coord;
505503dadc2fSPeter Brune   PetscErrorCode ierr;
505603dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
505703dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
505803dadc2fSPeter Brune   PetscFunctionBegin;
505903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
506003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
506103dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
506203dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
506303dadc2fSPeter Brune   if (coords && !ccoords) {
506403dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
50656668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
506603dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
506724640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
506803dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
506903dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
507003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
507103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
507203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
507303dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
507403dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
507503dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
507603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
507703dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
507803dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
507903dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
508003dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
508103dadc2fSPeter Brune   }
508203dadc2fSPeter Brune   PetscFunctionReturn(0);
508303dadc2fSPeter Brune }
508403dadc2fSPeter Brune 
5085c73cfb54SMatthew G. Knepley /*@
5086c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5087c73cfb54SMatthew G. Knepley 
5088c73cfb54SMatthew G. Knepley   Not collective
5089c73cfb54SMatthew G. Knepley 
5090c73cfb54SMatthew G. Knepley   Input Parameter:
5091c73cfb54SMatthew G. Knepley . dm - The DM
5092c73cfb54SMatthew G. Knepley 
5093c73cfb54SMatthew G. Knepley   Output Parameter:
5094c73cfb54SMatthew G. Knepley . dim - The topological dimension
5095c73cfb54SMatthew G. Knepley 
5096c73cfb54SMatthew G. Knepley   Level: beginner
5097c73cfb54SMatthew G. Knepley 
5098c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5099c73cfb54SMatthew G. Knepley @*/
5100c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5101c73cfb54SMatthew G. Knepley {
5102c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5103c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5104c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
5105c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5106c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5107c73cfb54SMatthew G. Knepley }
5108c73cfb54SMatthew G. Knepley 
5109c73cfb54SMatthew G. Knepley /*@
5110c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5111c73cfb54SMatthew G. Knepley 
5112c73cfb54SMatthew G. Knepley   Collective on dm
5113c73cfb54SMatthew G. Knepley 
5114c73cfb54SMatthew G. Knepley   Input Parameters:
5115c73cfb54SMatthew G. Knepley + dm - The DM
5116c73cfb54SMatthew G. Knepley - dim - The topological dimension
5117c73cfb54SMatthew G. Knepley 
5118c73cfb54SMatthew G. Knepley   Level: beginner
5119c73cfb54SMatthew G. Knepley 
5120c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5121c73cfb54SMatthew G. Knepley @*/
5122c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5123c73cfb54SMatthew G. Knepley {
5124e5e52638SMatthew G. Knepley   PetscDS        ds;
5125f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5126f17e8794SMatthew G. Knepley 
5127c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5128c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5129c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5130c73cfb54SMatthew G. Knepley   dm->dim = dim;
5131e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5132e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5133c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5134c73cfb54SMatthew G. Knepley }
5135c73cfb54SMatthew G. Knepley 
5136793f3fe5SMatthew G. Knepley /*@
5137793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5138793f3fe5SMatthew G. Knepley 
5139793f3fe5SMatthew G. Knepley   Collective on DM
5140793f3fe5SMatthew G. Knepley 
5141793f3fe5SMatthew G. Knepley   Input Parameters:
5142793f3fe5SMatthew G. Knepley + dm - the DM
5143793f3fe5SMatthew G. Knepley - dim - the dimension
5144793f3fe5SMatthew G. Knepley 
5145793f3fe5SMatthew G. Knepley   Output Parameters:
5146793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5147793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
5148793f3fe5SMatthew G. Knepley 
5149793f3fe5SMatthew G. Knepley   Note:
5150793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5151ebfe4b0dSMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5152793f3fe5SMatthew G. Knepley   then the interval is empty.
5153793f3fe5SMatthew G. Knepley 
5154793f3fe5SMatthew G. Knepley   Level: intermediate
5155793f3fe5SMatthew G. Knepley 
5156793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
5157793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5158793f3fe5SMatthew G. Knepley @*/
5159793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5160793f3fe5SMatthew G. Knepley {
5161793f3fe5SMatthew G. Knepley   PetscInt       d;
5162793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5163793f3fe5SMatthew G. Knepley 
5164793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5165793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5166793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5167793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5168793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5169793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5170793f3fe5SMatthew G. Knepley }
5171793f3fe5SMatthew G. Knepley 
51726636e97aSMatthew G Knepley /*@
51736636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
51746636e97aSMatthew G Knepley 
51756636e97aSMatthew G Knepley   Collective on DM
51766636e97aSMatthew G Knepley 
51776636e97aSMatthew G Knepley   Input Parameters:
51786636e97aSMatthew G Knepley + dm - the DM
51796636e97aSMatthew G Knepley - c - coordinate vector
51806636e97aSMatthew G Knepley 
51812dd40e9bSPatrick Sanan   Notes:
51822dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
51832dd40e9bSPatrick Sanan 
51842dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
51856636e97aSMatthew G Knepley 
51866636e97aSMatthew G Knepley   Level: intermediate
51876636e97aSMatthew G Knepley 
51886636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
51892dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
51906636e97aSMatthew G Knepley @*/
51916636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
51926636e97aSMatthew G Knepley {
51936636e97aSMatthew G Knepley   PetscErrorCode ierr;
51946636e97aSMatthew G Knepley 
51956636e97aSMatthew G Knepley   PetscFunctionBegin;
51966636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51976636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
51986636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
51996636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
52006636e97aSMatthew G Knepley   dm->coordinates = c;
52016636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5202b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
520303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
52046636e97aSMatthew G Knepley   PetscFunctionReturn(0);
52056636e97aSMatthew G Knepley }
52066636e97aSMatthew G Knepley 
52076636e97aSMatthew G Knepley /*@
52086636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
52096636e97aSMatthew G Knepley 
52107058e716SVaclav Hapla   Not collective
52116636e97aSMatthew G Knepley 
52126636e97aSMatthew G Knepley    Input Parameters:
52136636e97aSMatthew G Knepley +  dm - the DM
52146636e97aSMatthew G Knepley -  c - coordinate vector
52156636e97aSMatthew G Knepley 
52162dd40e9bSPatrick Sanan   Notes:
52176636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
52186636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
52196636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
52206636e97aSMatthew G Knepley 
52212dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
52222dd40e9bSPatrick Sanan 
52236636e97aSMatthew G Knepley   Level: intermediate
52246636e97aSMatthew G Knepley 
52256636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
52266636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
52276636e97aSMatthew G Knepley @*/
52286636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
52296636e97aSMatthew G Knepley {
52306636e97aSMatthew G Knepley   PetscErrorCode ierr;
52316636e97aSMatthew G Knepley 
52326636e97aSMatthew G Knepley   PetscFunctionBegin;
52336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52346636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
52356636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
52366636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
52378865f1eaSKarl Rupp 
52386636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
52398865f1eaSKarl Rupp 
52406636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
52416636e97aSMatthew G Knepley   PetscFunctionReturn(0);
52426636e97aSMatthew G Knepley }
52436636e97aSMatthew G Knepley 
52446636e97aSMatthew G Knepley /*@
52456636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
52466636e97aSMatthew G Knepley 
5247c4a54dccSVaclav Hapla   Collective on DM
52486636e97aSMatthew G Knepley 
52496636e97aSMatthew G Knepley   Input Parameter:
52506636e97aSMatthew G Knepley . dm - the DM
52516636e97aSMatthew G Knepley 
52526636e97aSMatthew G Knepley   Output Parameter:
52536636e97aSMatthew G Knepley . c - global coordinate vector
52546636e97aSMatthew G Knepley 
52556636e97aSMatthew G Knepley   Note:
52566636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
52576636e97aSMatthew G Knepley 
52586636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
52596636e97aSMatthew G Knepley 
52606636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
52616636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
52626636e97aSMatthew G Knepley 
52636636e97aSMatthew G Knepley   Level: intermediate
52646636e97aSMatthew G Knepley 
52656636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
52666636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
52676636e97aSMatthew G Knepley @*/
52686636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
52696636e97aSMatthew G Knepley {
52706636e97aSMatthew G Knepley   PetscErrorCode ierr;
52716636e97aSMatthew G Knepley 
52726636e97aSMatthew G Knepley   PetscFunctionBegin;
52736636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52746636e97aSMatthew G Knepley   PetscValidPointer(c,2);
52751f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
52760298fd71SBarry Smith     DM        cdm = NULL;
52771970a576SMatthew G. Knepley     PetscBool localized;
52786636e97aSMatthew G Knepley 
52796636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
52806636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
52811970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
52821970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
52831970a576SMatthew G. Knepley     if (localized) {
52841970a576SMatthew G. Knepley       PetscInt cdim;
52851970a576SMatthew G. Knepley 
52861970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
52871970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
52881970a576SMatthew G. Knepley     }
52896636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
52906636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
52916636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
52926636e97aSMatthew G Knepley   }
52936636e97aSMatthew G Knepley   *c = dm->coordinates;
52946636e97aSMatthew G Knepley   PetscFunctionReturn(0);
52956636e97aSMatthew G Knepley }
52966636e97aSMatthew G Knepley 
52976636e97aSMatthew G Knepley /*@
529881e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
529981e9a530SVaclav Hapla 
530081e9a530SVaclav Hapla   Collective on DM
530181e9a530SVaclav Hapla 
530281e9a530SVaclav Hapla   Input Parameter:
530381e9a530SVaclav Hapla . dm - the DM
530481e9a530SVaclav Hapla 
530581e9a530SVaclav Hapla   Level: advanced
530681e9a530SVaclav Hapla 
530781e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
530881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
530981e9a530SVaclav Hapla @*/
531081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
531181e9a530SVaclav Hapla {
531281e9a530SVaclav Hapla   PetscErrorCode ierr;
531381e9a530SVaclav Hapla 
531481e9a530SVaclav Hapla   PetscFunctionBegin;
531581e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
531681e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
53171970a576SMatthew G. Knepley     DM        cdm = NULL;
53181970a576SMatthew G. Knepley     PetscBool localized;
53191970a576SMatthew G. Knepley 
532081e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
532181e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
53221970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
53231970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
53241970a576SMatthew G. Knepley     if (localized) {
53251970a576SMatthew G. Knepley       PetscInt cdim;
53261970a576SMatthew G. Knepley 
53271970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
53281970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
53291970a576SMatthew G. Knepley     }
533081e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
533181e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
533281e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
533381e9a530SVaclav Hapla   }
533481e9a530SVaclav Hapla   PetscFunctionReturn(0);
533581e9a530SVaclav Hapla }
533681e9a530SVaclav Hapla 
533781e9a530SVaclav Hapla /*@
53386636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
53396636e97aSMatthew G Knepley 
53406636e97aSMatthew G Knepley   Collective on DM
53416636e97aSMatthew G Knepley 
53426636e97aSMatthew G Knepley   Input Parameter:
53436636e97aSMatthew G Knepley . dm - the DM
53446636e97aSMatthew G Knepley 
53456636e97aSMatthew G Knepley   Output Parameter:
53466636e97aSMatthew G Knepley . c - coordinate vector
53476636e97aSMatthew G Knepley 
53486636e97aSMatthew G Knepley   Note:
53496636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
53506636e97aSMatthew G Knepley 
53516636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
53526636e97aSMatthew G Knepley 
53536636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
53546636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
53556636e97aSMatthew G Knepley 
53566636e97aSMatthew G Knepley   Level: intermediate
53576636e97aSMatthew G Knepley 
53586636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
535981e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
53606636e97aSMatthew G Knepley @*/
53616636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
53626636e97aSMatthew G Knepley {
53636636e97aSMatthew G Knepley   PetscErrorCode ierr;
53646636e97aSMatthew G Knepley 
53656636e97aSMatthew G Knepley   PetscFunctionBegin;
53666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53676636e97aSMatthew G Knepley   PetscValidPointer(c,2);
536881e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
53696636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
53706636e97aSMatthew G Knepley   PetscFunctionReturn(0);
53716636e97aSMatthew G Knepley }
53726636e97aSMatthew G Knepley 
537381e9a530SVaclav Hapla /*@
537481e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
537581e9a530SVaclav Hapla 
537681e9a530SVaclav Hapla   Not collective
537781e9a530SVaclav Hapla 
537881e9a530SVaclav Hapla   Input Parameter:
537981e9a530SVaclav Hapla . dm - the DM
538081e9a530SVaclav Hapla 
538181e9a530SVaclav Hapla   Output Parameter:
538281e9a530SVaclav Hapla . c - coordinate vector
538381e9a530SVaclav Hapla 
538481e9a530SVaclav Hapla   Level: advanced
538581e9a530SVaclav Hapla 
538681e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
538781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
538881e9a530SVaclav Hapla @*/
538981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
539081e9a530SVaclav Hapla {
539181e9a530SVaclav Hapla   PetscFunctionBegin;
539281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
539381e9a530SVaclav Hapla   PetscValidPointer(c,2);
539481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
53956636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
53966636e97aSMatthew G Knepley   PetscFunctionReturn(0);
53976636e97aSMatthew G Knepley }
53986636e97aSMatthew G Knepley 
53992db98f8dSVaclav Hapla /*@
54002db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
54012db98f8dSVaclav Hapla 
54022db98f8dSVaclav Hapla   Not collective
54032db98f8dSVaclav Hapla 
54042db98f8dSVaclav Hapla   Input Parameter:
54052db98f8dSVaclav Hapla + dm - the DM
54062db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
54072db98f8dSVaclav Hapla 
54082db98f8dSVaclav Hapla   Output Parameter:
54092db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
54102db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
54112db98f8dSVaclav Hapla 
54122db98f8dSVaclav Hapla   Note:
54132db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
54142db98f8dSVaclav Hapla 
54152db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
54162db98f8dSVaclav Hapla 
54172db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
54182db98f8dSVaclav Hapla 
54192db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
54202db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
54212db98f8dSVaclav Hapla 
54222db98f8dSVaclav Hapla   Level: advanced
54232db98f8dSVaclav Hapla 
54242db98f8dSVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
54252db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
54262db98f8dSVaclav Hapla @*/
54272db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
54282db98f8dSVaclav Hapla {
54292db98f8dSVaclav Hapla   PetscSection        cs, newcs;
54302db98f8dSVaclav Hapla   Vec                 coords;
54312db98f8dSVaclav Hapla   const PetscScalar   *arr;
54322db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
54332db98f8dSVaclav Hapla   PetscInt            n;
54342db98f8dSVaclav Hapla   PetscErrorCode      ierr;
54352db98f8dSVaclav Hapla 
54362db98f8dSVaclav Hapla   PetscFunctionBegin;
54372db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54382db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
54392db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
54402db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
54412db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
54422db98f8dSVaclav Hapla   if (!dm->coordinateDM || !dm->coordinateDM->defaultSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
54432db98f8dSVaclav Hapla   cs = dm->coordinateDM->defaultSection;
54442db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
54452db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
54462db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
54472db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
54482db98f8dSVaclav Hapla   if (pCoord) {
54492db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
54502db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
54512db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
54522db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5453ad9ac99dSVaclav Hapla   } else {
5454ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
54552db98f8dSVaclav Hapla   }
5456ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5457ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
54582db98f8dSVaclav Hapla   PetscFunctionReturn(0);
54592db98f8dSVaclav Hapla }
54602db98f8dSVaclav Hapla 
5461f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5462f19dbd58SToby Isaac {
5463f19dbd58SToby Isaac   PetscErrorCode ierr;
5464f19dbd58SToby Isaac 
5465f19dbd58SToby Isaac   PetscFunctionBegin;
5466f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5467f19dbd58SToby Isaac   PetscValidPointer(field,2);
5468f19dbd58SToby Isaac   if (!dm->coordinateField) {
5469f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5470f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5471f19dbd58SToby Isaac     }
5472f19dbd58SToby Isaac   }
5473f19dbd58SToby Isaac   *field = dm->coordinateField;
5474f19dbd58SToby Isaac   PetscFunctionReturn(0);
5475f19dbd58SToby Isaac }
5476f19dbd58SToby Isaac 
5477f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5478f19dbd58SToby Isaac {
5479f19dbd58SToby Isaac   PetscErrorCode ierr;
5480f19dbd58SToby Isaac 
5481f19dbd58SToby Isaac   PetscFunctionBegin;
5482f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5483f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5484c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5485f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5486f19dbd58SToby Isaac   dm->coordinateField = field;
5487f19dbd58SToby Isaac   PetscFunctionReturn(0);
5488f19dbd58SToby Isaac }
5489f19dbd58SToby Isaac 
54906636e97aSMatthew G Knepley /*@
54911cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
54926636e97aSMatthew G Knepley 
54936636e97aSMatthew G Knepley   Collective on DM
54946636e97aSMatthew G Knepley 
54956636e97aSMatthew G Knepley   Input Parameter:
54966636e97aSMatthew G Knepley . dm - the DM
54976636e97aSMatthew G Knepley 
54986636e97aSMatthew G Knepley   Output Parameter:
54996636e97aSMatthew G Knepley . cdm - coordinate DM
55006636e97aSMatthew G Knepley 
55016636e97aSMatthew G Knepley   Level: intermediate
55026636e97aSMatthew G Knepley 
55036636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
55041cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
55056636e97aSMatthew G Knepley @*/
55066636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
55076636e97aSMatthew G Knepley {
55086636e97aSMatthew G Knepley   PetscErrorCode ierr;
55096636e97aSMatthew G Knepley 
55106636e97aSMatthew G Knepley   PetscFunctionBegin;
55116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55126636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
55136636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5514308f8a94SToby Isaac     DM cdm;
5515308f8a94SToby Isaac 
551682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5517308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5518308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5519308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5520308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5521308f8a94SToby Isaac     dm->coordinateDM = cdm;
55226636e97aSMatthew G Knepley   }
55236636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
55246636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55256636e97aSMatthew G Knepley }
5526e87bb0d3SMatthew G Knepley 
55271cfe2091SMatthew G. Knepley /*@
55281cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
55291cfe2091SMatthew G. Knepley 
55301cfe2091SMatthew G. Knepley   Logically Collective on DM
55311cfe2091SMatthew G. Knepley 
55321cfe2091SMatthew G. Knepley   Input Parameters:
55331cfe2091SMatthew G. Knepley + dm - the DM
55341cfe2091SMatthew G. Knepley - cdm - coordinate DM
55351cfe2091SMatthew G. Knepley 
55361cfe2091SMatthew G. Knepley   Level: intermediate
55371cfe2091SMatthew G. Knepley 
55381cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
55391cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
55401cfe2091SMatthew G. Knepley @*/
55411cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
55421cfe2091SMatthew G. Knepley {
55431cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
55441cfe2091SMatthew G. Knepley 
55451cfe2091SMatthew G. Knepley   PetscFunctionBegin;
55461cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55471cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5548f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
55491cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
55501cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
55511cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
55521cfe2091SMatthew G. Knepley }
55531cfe2091SMatthew G. Knepley 
555446e270d4SMatthew G. Knepley /*@
555546e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
555646e270d4SMatthew G. Knepley 
555746e270d4SMatthew G. Knepley   Not Collective
555846e270d4SMatthew G. Knepley 
555946e270d4SMatthew G. Knepley   Input Parameter:
556046e270d4SMatthew G. Knepley . dm - The DM object
556146e270d4SMatthew G. Knepley 
556246e270d4SMatthew G. Knepley   Output Parameter:
556346e270d4SMatthew G. Knepley . dim - The embedding dimension
556446e270d4SMatthew G. Knepley 
556546e270d4SMatthew G. Knepley   Level: intermediate
556646e270d4SMatthew G. Knepley 
556746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
5568e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
556946e270d4SMatthew G. Knepley @*/
557046e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
557146e270d4SMatthew G. Knepley {
557246e270d4SMatthew G. Knepley   PetscFunctionBegin;
557346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
557446e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
55759a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
55769a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
55779a9a41abSToby Isaac   }
557846e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
557946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
558046e270d4SMatthew G. Knepley }
558146e270d4SMatthew G. Knepley 
558246e270d4SMatthew G. Knepley /*@
558346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
558446e270d4SMatthew G. Knepley 
558546e270d4SMatthew G. Knepley   Not Collective
558646e270d4SMatthew G. Knepley 
558746e270d4SMatthew G. Knepley   Input Parameters:
558846e270d4SMatthew G. Knepley + dm  - The DM object
558946e270d4SMatthew G. Knepley - dim - The embedding dimension
559046e270d4SMatthew G. Knepley 
559146e270d4SMatthew G. Knepley   Level: intermediate
559246e270d4SMatthew G. Knepley 
559346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
5594e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
559546e270d4SMatthew G. Knepley @*/
559646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
559746e270d4SMatthew G. Knepley {
5598e5e52638SMatthew G. Knepley   PetscDS        ds;
5599f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5600f17e8794SMatthew G. Knepley 
560146e270d4SMatthew G. Knepley   PetscFunctionBegin;
560246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
560346e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5604e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5605e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
560646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
560746e270d4SMatthew G. Knepley }
560846e270d4SMatthew G. Knepley 
5609e8abe2deSMatthew G. Knepley /*@
5610e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5611e8abe2deSMatthew G. Knepley 
5612116501dfSVaclav Hapla   Collective on DM
5613e8abe2deSMatthew G. Knepley 
5614e8abe2deSMatthew G. Knepley   Input Parameter:
5615e8abe2deSMatthew G. Knepley . dm - The DM object
5616e8abe2deSMatthew G. Knepley 
5617e8abe2deSMatthew G. Knepley   Output Parameter:
5618e8abe2deSMatthew G. Knepley . section - The PetscSection object
5619e8abe2deSMatthew G. Knepley 
5620e8abe2deSMatthew G. Knepley   Level: intermediate
5621e8abe2deSMatthew G. Knepley 
5622e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
5623e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
5624e8abe2deSMatthew G. Knepley @*/
5625e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5626e8abe2deSMatthew G. Knepley {
5627e8abe2deSMatthew G. Knepley   DM             cdm;
5628e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5629e8abe2deSMatthew G. Knepley 
5630e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5631e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5632e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5633e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5634e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
5635e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5636e8abe2deSMatthew G. Knepley }
5637e8abe2deSMatthew G. Knepley 
5638e8abe2deSMatthew G. Knepley /*@
5639e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5640e8abe2deSMatthew G. Knepley 
5641e8abe2deSMatthew G. Knepley   Not Collective
5642e8abe2deSMatthew G. Knepley 
5643e8abe2deSMatthew G. Knepley   Input Parameters:
5644e8abe2deSMatthew G. Knepley + dm      - The DM object
564546e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5646e8abe2deSMatthew G. Knepley - section - The PetscSection object
5647e8abe2deSMatthew G. Knepley 
5648e8abe2deSMatthew G. Knepley   Level: intermediate
5649e8abe2deSMatthew G. Knepley 
5650e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
5651e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
5652e8abe2deSMatthew G. Knepley @*/
565346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5654e8abe2deSMatthew G. Knepley {
5655e8abe2deSMatthew G. Knepley   DM             cdm;
5656e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5657e8abe2deSMatthew G. Knepley 
5658e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5659e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
566046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5661e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5662e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
566346e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
56644c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
566546e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
566646e270d4SMatthew G. Knepley 
566746e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
566846e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
566946e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
567046e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
567146e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
567246e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
567346e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
567446e270d4SMatthew G. Knepley     }
5675ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
567646e270d4SMatthew G. Knepley   }
5677e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5678e8abe2deSMatthew G. Knepley }
5679e8abe2deSMatthew G. Knepley 
56805dc8c3f7SMatthew G. Knepley /*@C
568190b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
56825dc8c3f7SMatthew G. Knepley 
56835dc8c3f7SMatthew G. Knepley   Input Parameters:
568490b157c4SStefano Zampini . dm      - The DM object
568590b157c4SStefano Zampini 
568690b157c4SStefano Zampini   Output Parameters:
568790b157c4SStefano Zampini + per     - Whether the DM is periodic or not
56885dc8c3f7SMatthew 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
56895dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
56905dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
56915dc8c3f7SMatthew G. Knepley 
56925dc8c3f7SMatthew G. Knepley   Level: developer
56935dc8c3f7SMatthew G. Knepley 
56945dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
56955dc8c3f7SMatthew G. Knepley @*/
569690b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5697c6b900c6SMatthew G. Knepley {
5698c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5699c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
570090b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5701c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5702c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
57035dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5704c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5705c6b900c6SMatthew G. Knepley }
5706c6b900c6SMatthew G. Knepley 
57075dc8c3f7SMatthew G. Knepley /*@C
57085dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
57095dc8c3f7SMatthew G. Knepley 
57105dc8c3f7SMatthew G. Knepley   Input Parameters:
57115dc8c3f7SMatthew G. Knepley + dm      - The DM object
571290b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
57135dc8c3f7SMatthew 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
57145dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
57155dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
57165dc8c3f7SMatthew G. Knepley 
57175dc8c3f7SMatthew G. Knepley   Level: developer
57185dc8c3f7SMatthew G. Knepley 
57195dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
57205dc8c3f7SMatthew G. Knepley @*/
572190b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5722c6b900c6SMatthew G. Knepley {
5723c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
5724c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
5725c6b900c6SMatthew G. Knepley 
5726c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5727c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
572890b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
572990b157c4SStefano Zampini   if (maxCell) {
573090b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
573190b157c4SStefano Zampini     PetscValidPointer(L,4);
573290b157c4SStefano Zampini     PetscValidPointer(bd,5);
573390b157c4SStefano Zampini   }
57345dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
57355dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
573690b157c4SStefano Zampini   if (maxCell) {
57375dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
57385dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
573990b157c4SStefano Zampini   }
5740072d7d67SStefano Zampini   dm->periodic = per;
5741c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5742c6b900c6SMatthew G. Knepley }
5743c6b900c6SMatthew G. Knepley 
57442e17dfb7SMatthew G. Knepley /*@
57452e17dfb7SMatthew 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.
57462e17dfb7SMatthew G. Knepley 
57472e17dfb7SMatthew G. Knepley   Input Parameters:
57482e17dfb7SMatthew G. Knepley + dm     - The DM
574965da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
575065da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
57512e17dfb7SMatthew G. Knepley 
57522e17dfb7SMatthew G. Knepley   Output Parameter:
57532e17dfb7SMatthew G. Knepley . out - The localized coordinate point
57542e17dfb7SMatthew G. Knepley 
57552e17dfb7SMatthew G. Knepley   Level: developer
57562e17dfb7SMatthew G. Knepley 
57572e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
57582e17dfb7SMatthew G. Knepley @*/
575965da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
57602e17dfb7SMatthew G. Knepley {
57612e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
57622e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
57632e17dfb7SMatthew G. Knepley 
57642e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
57652e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
57662e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
57672e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
57682e17dfb7SMatthew G. Knepley   } else {
576965da65dcSMatthew G. Knepley     if (endpoint) {
577065da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
5771da3333bfSMatthew 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)) {
5772da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
577365da65dcSMatthew G. Knepley         } else {
5774da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
577565da65dcSMatthew G. Knepley         }
577665da65dcSMatthew G. Knepley       }
577765da65dcSMatthew G. Knepley     } else {
57782e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
57791118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
57802e17dfb7SMatthew G. Knepley       }
57812e17dfb7SMatthew G. Knepley     }
578265da65dcSMatthew G. Knepley   }
57832e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
57842e17dfb7SMatthew G. Knepley }
57852e17dfb7SMatthew G. Knepley 
57862e17dfb7SMatthew G. Knepley /*
57872e17dfb7SMatthew 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.
57882e17dfb7SMatthew G. Knepley 
57892e17dfb7SMatthew G. Knepley   Input Parameters:
57902e17dfb7SMatthew G. Knepley + dm     - The DM
57912e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
57922e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
57932e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
57942e17dfb7SMatthew G. Knepley 
57952e17dfb7SMatthew G. Knepley   Output Parameter:
57962e17dfb7SMatthew G. Knepley . out - The localized coordinate point
57972e17dfb7SMatthew G. Knepley 
57982e17dfb7SMatthew G. Knepley   Level: developer
57992e17dfb7SMatthew G. Knepley 
58002e17dfb7SMatthew 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
58012e17dfb7SMatthew G. Knepley 
58022e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
58032e17dfb7SMatthew G. Knepley */
58042e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
58052e17dfb7SMatthew G. Knepley {
58062e17dfb7SMatthew G. Knepley   PetscInt d;
58072e17dfb7SMatthew G. Knepley 
58082e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
58092e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
58102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
58112e17dfb7SMatthew G. Knepley   } else {
58122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5813908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
58142e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
58152e17dfb7SMatthew G. Knepley       } else {
58162e17dfb7SMatthew G. Knepley         out[d] = in[d];
58172e17dfb7SMatthew G. Knepley       }
58182e17dfb7SMatthew G. Knepley     }
58192e17dfb7SMatthew G. Knepley   }
58202e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
58212e17dfb7SMatthew G. Knepley }
58222e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
58232e17dfb7SMatthew G. Knepley {
58242e17dfb7SMatthew G. Knepley   PetscInt d;
58252e17dfb7SMatthew G. Knepley 
58262e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
58272e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
58282e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
58292e17dfb7SMatthew G. Knepley   } else {
58302e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5831908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
58322e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
58332e17dfb7SMatthew G. Knepley       } else {
58342e17dfb7SMatthew G. Knepley         out[d] = in[d];
58352e17dfb7SMatthew G. Knepley       }
58362e17dfb7SMatthew G. Knepley     }
58372e17dfb7SMatthew G. Knepley   }
58382e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
58392e17dfb7SMatthew G. Knepley }
58402e17dfb7SMatthew G. Knepley 
58412e17dfb7SMatthew G. Knepley /*
58422e17dfb7SMatthew 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.
58432e17dfb7SMatthew G. Knepley 
58442e17dfb7SMatthew G. Knepley   Input Parameters:
58452e17dfb7SMatthew G. Knepley + dm     - The DM
58462e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
58472e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
58482e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
58492e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
58502e17dfb7SMatthew G. Knepley 
58512e17dfb7SMatthew G. Knepley   Output Parameter:
58522e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
58532e17dfb7SMatthew G. Knepley 
58542e17dfb7SMatthew G. Knepley   Level: developer
58552e17dfb7SMatthew G. Knepley 
58562e17dfb7SMatthew 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
58572e17dfb7SMatthew G. Knepley 
58582e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
58592e17dfb7SMatthew G. Knepley */
58602e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
58612e17dfb7SMatthew G. Knepley {
58622e17dfb7SMatthew G. Knepley   PetscInt d;
58632e17dfb7SMatthew G. Knepley 
58642e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
58652e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
58662e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
58672e17dfb7SMatthew G. Knepley   } else {
58682e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5869908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
58702e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
58712e17dfb7SMatthew G. Knepley       } else {
58722e17dfb7SMatthew G. Knepley         out[d] += in[d];
58732e17dfb7SMatthew G. Knepley       }
58742e17dfb7SMatthew G. Knepley     }
58752e17dfb7SMatthew G. Knepley   }
58762e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
58772e17dfb7SMatthew G. Knepley }
58782e17dfb7SMatthew G. Knepley 
587936447a5eSToby Isaac /*@
58808f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
58818f700142SStefano Zampini 
58828f700142SStefano Zampini   Not collective
588336447a5eSToby Isaac 
588436447a5eSToby Isaac   Input Parameter:
588536447a5eSToby Isaac . dm - The DM
588636447a5eSToby Isaac 
588736447a5eSToby Isaac   Output Parameter:
588836447a5eSToby Isaac   areLocalized - True if localized
588936447a5eSToby Isaac 
589036447a5eSToby Isaac   Level: developer
589136447a5eSToby Isaac 
58928f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
589336447a5eSToby Isaac @*/
58948f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
589536447a5eSToby Isaac {
589636447a5eSToby Isaac   DM             cdm;
589736447a5eSToby Isaac   PetscSection   coordSection;
589846a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
589946a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
590036447a5eSToby Isaac   PetscErrorCode ierr;
590136447a5eSToby Isaac 
590236447a5eSToby Isaac   PetscFunctionBegin;
590336447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
590446a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
59058b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
590646a3a80fSLisandro Dalcin 
590736447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
590836447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
590946a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
59109f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
591146a3a80fSLisandro Dalcin 
59129f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
591346a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
591436447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
591536447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
591646a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
591746a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
591836447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
591946a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
592036447a5eSToby Isaac   }
59218f700142SStefano Zampini   *areLocalized = alreadyLocalized;
592236447a5eSToby Isaac   PetscFunctionReturn(0);
592336447a5eSToby Isaac }
592436447a5eSToby Isaac 
59258f700142SStefano Zampini /*@
59268f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
59278f700142SStefano Zampini 
59288f700142SStefano Zampini   Collective on dm
59298f700142SStefano Zampini 
59308f700142SStefano Zampini   Input Parameter:
59318f700142SStefano Zampini . dm - The DM
59328f700142SStefano Zampini 
59338f700142SStefano Zampini   Output Parameter:
59348f700142SStefano Zampini   areLocalized - True if localized
59358f700142SStefano Zampini 
59368f700142SStefano Zampini   Level: developer
59378f700142SStefano Zampini 
59388f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
59398f700142SStefano Zampini @*/
59408f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
59418f700142SStefano Zampini {
59428f700142SStefano Zampini   PetscBool      localized;
59438f700142SStefano Zampini   PetscErrorCode ierr;
59448f700142SStefano Zampini 
59458f700142SStefano Zampini   PetscFunctionBegin;
59468f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59478f700142SStefano Zampini   PetscValidPointer(areLocalized, 2);
59488f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
59498f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
59508f700142SStefano Zampini   PetscFunctionReturn(0);
59518f700142SStefano Zampini }
595236447a5eSToby Isaac 
59532e17dfb7SMatthew G. Knepley /*@
5954492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
59552e17dfb7SMatthew G. Knepley 
59568f700142SStefano Zampini   Collective on dm
59578f700142SStefano Zampini 
59582e17dfb7SMatthew G. Knepley   Input Parameter:
59592e17dfb7SMatthew G. Knepley . dm - The DM
59602e17dfb7SMatthew G. Knepley 
59612e17dfb7SMatthew G. Knepley   Level: developer
59622e17dfb7SMatthew G. Knepley 
59638f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
59642e17dfb7SMatthew G. Knepley @*/
59652e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
59662e17dfb7SMatthew G. Knepley {
59672e17dfb7SMatthew G. Knepley   DM             cdm;
59682e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
59692e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
59703e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
59713e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
5972e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
59733e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
59743e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
59752e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
59762e17dfb7SMatthew G. Knepley 
59772e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
59782e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
597992c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
5980f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
5981f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
5982f7cbd40bSStefano Zampini 
59832e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
59842e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
59852e17dfb7SMatthew G. Knepley   {
59862e17dfb7SMatthew G. Knepley     PetscBool isplex;
59872e17dfb7SMatthew G. Knepley 
59882e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
59892e17dfb7SMatthew G. Knepley     if (isplex) {
59902e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
59913e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
599269291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
59933e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
59943e922f36SToby Isaac       newStart = vStart;
59953e922f36SToby Isaac       newEnd   = vEnd;
59963e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
59973e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
59983e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
59993e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
60003e922f36SToby Isaac       }
60012e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
60022e17dfb7SMatthew G. Knepley   }
60032e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
600443eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
60052e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
60063e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6007e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
60083e922f36SToby Isaac 
60092e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
60102e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
60112e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
60122e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
60133e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
60143e922f36SToby Isaac 
601569291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
60163e922f36SToby Isaac   localized = &anchor[bs];
60173e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
60183e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
60193e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
60203e922f36SToby Isaac 
60213e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
60223e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
60233e922f36SToby Isaac       PetscInt     b;
60243e922f36SToby Isaac 
60253e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
60263e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
60273e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
60283e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
60293e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
60303e922f36SToby Isaac         for (b = 0; b < bs; b++) {
60313e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
60323e922f36SToby Isaac         }
60333e922f36SToby Isaac         if (b < bs) break;
60343e922f36SToby Isaac       }
60353e922f36SToby Isaac       if (d < dof/bs) {
60363e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
60373e922f36SToby Isaac           PetscInt cdof;
60383e922f36SToby Isaac 
60393e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
60403e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
60413e922f36SToby Isaac         }
60423e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
60433e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
60443e922f36SToby Isaac       }
60453e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
60463e922f36SToby Isaac     }
60473e922f36SToby Isaac   }
60483e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
60493e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
605069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
60513e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
605269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
60533e922f36SToby Isaac     PetscFunctionReturn(0);
60543e922f36SToby Isaac   }
60552e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
60562e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
60572e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
60582e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
60592e17dfb7SMatthew G. Knepley   }
60602e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
60612e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6062c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
60632e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
60642e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
60652e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
60662e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6067c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
60682e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
60692e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
60702e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
60712e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
60722e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
60732e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
60742e17dfb7SMatthew G. Knepley   }
60753e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
60763e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
60773e922f36SToby Isaac 
60782e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
60792e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
60803e922f36SToby Isaac       PetscInt     b, cdof;
60812e17dfb7SMatthew G. Knepley 
60823e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
60833e922f36SToby Isaac       if (!cdof) continue;
60842e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
60852e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
60862e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
60872e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
60882e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
60892e17dfb7SMatthew G. Knepley     }
60903e922f36SToby Isaac   }
609169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
609269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6093c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
60942e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
60952e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
60962e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
60972e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
60982e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
60992e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61002e17dfb7SMatthew G. Knepley }
61012e17dfb7SMatthew G. Knepley 
6102e87bb0d3SMatthew G Knepley /*@
61033a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6104e87bb0d3SMatthew G Knepley 
61053a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
6106e87bb0d3SMatthew G Knepley 
6107e87bb0d3SMatthew G Knepley   Input Parameters:
6108e87bb0d3SMatthew G Knepley + dm - The DM
61093a93e3b7SToby Isaac . v - The Vec of points
611062a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
61113a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6112e87bb0d3SMatthew G Knepley 
611361e3bb9bSMatthew G Knepley   Output Parameter:
611462a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
611562a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
61163a93e3b7SToby Isaac 
6117e87bb0d3SMatthew G Knepley 
6118e87bb0d3SMatthew G Knepley   Level: developer
611961e3bb9bSMatthew G Knepley 
612062a38674SMatthew G. Knepley   Notes:
61213a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
612262a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
61233a93e3b7SToby Isaac 
61243a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
612562a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
61263a93e3b7SToby Isaac 
61273a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
61283a93e3b7SToby Isaac 
612962a38674SMatthew G. Knepley $    const PetscSFNode *cells;
613062a38674SMatthew G. Knepley $    PetscInt           nFound;
6131a6216909SToby Isaac $    const PetscInt    *found;
613262a38674SMatthew G. Knepley $
6133a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
61343a93e3b7SToby Isaac 
61353a93e3b7SToby 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
61363a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
61373a93e3b7SToby Isaac 
613861e3bb9bSMatthew G Knepley .keywords: point location, mesh
613962a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
614061e3bb9bSMatthew G Knepley @*/
614162a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6142e87bb0d3SMatthew G Knepley {
6143735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6144735aa83eSMatthew G Knepley 
6145e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6146e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6147e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6148e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
61493a93e3b7SToby Isaac   if (*cellSF) {
61503a93e3b7SToby Isaac     PetscMPIInt result;
61513a93e3b7SToby Isaac 
6152e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6153a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
61543a93e3b7SToby 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");
6155e0fc9d1bSMatthew G. Knepley   } else {
61563a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
61573a93e3b7SToby Isaac   }
615847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6159735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
616062a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
616182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
616247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6163e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6164e87bb0d3SMatthew G Knepley }
616514f150ffSMatthew G. Knepley 
6166f4d763aaSMatthew G. Knepley /*@
6167f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6168f4d763aaSMatthew G. Knepley 
61698f700142SStefano Zampini   Collective on dm
61708f700142SStefano Zampini 
6171f4d763aaSMatthew G. Knepley   Input Parameter:
6172f4d763aaSMatthew G. Knepley . dm - The original DM
6173f4d763aaSMatthew G. Knepley 
6174f4d763aaSMatthew G. Knepley   Output Parameter:
6175f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6176f4d763aaSMatthew G. Knepley 
6177f4d763aaSMatthew G. Knepley   Level: intermediate
6178f4d763aaSMatthew G. Knepley 
6179e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6180f4d763aaSMatthew G. Knepley @*/
618114f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
618214f150ffSMatthew G. Knepley {
6183c26acbdeSMatthew G. Knepley   PetscSection   section;
61842d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
618514f150ffSMatthew G. Knepley   PetscErrorCode ierr;
618614f150ffSMatthew G. Knepley 
618714f150ffSMatthew G. Knepley   PetscFunctionBegin;
618814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
618914f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
6190e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
6191c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6192127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
61932d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6194c26acbdeSMatthew G. Knepley     *odm = dm;
6195c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6196c26acbdeSMatthew G. Knepley   }
619714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6198c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
619914f150ffSMatthew G. Knepley     PetscSF      sf;
620014f150ffSMatthew G. Knepley 
620114f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6202e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
620314f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
6204e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
620514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
620614f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
620715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6208e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
620914f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
621014f150ffSMatthew G. Knepley   }
621114f150ffSMatthew G. Knepley   *odm = dm->dmBC;
621214f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
621314f150ffSMatthew G. Knepley }
6214f4d763aaSMatthew G. Knepley 
6215f4d763aaSMatthew G. Knepley /*@
6216cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6217f4d763aaSMatthew G. Knepley 
6218f4d763aaSMatthew G. Knepley   Input Parameter:
6219f4d763aaSMatthew G. Knepley . dm - The original DM
6220f4d763aaSMatthew G. Knepley 
6221cdb7a50dSMatthew G. Knepley   Output Parameters:
6222cdb7a50dSMatthew G. Knepley + num - The output sequence number
6223cdb7a50dSMatthew G. Knepley - val - The output sequence value
6224f4d763aaSMatthew G. Knepley 
6225f4d763aaSMatthew G. Knepley   Level: intermediate
6226f4d763aaSMatthew G. Knepley 
6227f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6228f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6229f4d763aaSMatthew G. Knepley 
6230f4d763aaSMatthew G. Knepley .seealso: VecView()
6231f4d763aaSMatthew G. Knepley @*/
6232cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6233f4d763aaSMatthew G. Knepley {
6234f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6235f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6236cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
6237cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
6238f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6239f4d763aaSMatthew G. Knepley }
6240f4d763aaSMatthew G. Knepley 
6241f4d763aaSMatthew G. Knepley /*@
6242cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6243f4d763aaSMatthew G. Knepley 
6244f4d763aaSMatthew G. Knepley   Input Parameters:
6245f4d763aaSMatthew G. Knepley + dm - The original DM
6246cdb7a50dSMatthew G. Knepley . num - The output sequence number
6247cdb7a50dSMatthew G. Knepley - val - The output sequence value
6248f4d763aaSMatthew G. Knepley 
6249f4d763aaSMatthew G. Knepley   Level: intermediate
6250f4d763aaSMatthew G. Knepley 
6251f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6252f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6253f4d763aaSMatthew G. Knepley 
6254f4d763aaSMatthew G. Knepley .seealso: VecView()
6255f4d763aaSMatthew G. Knepley @*/
6256cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6257f4d763aaSMatthew G. Knepley {
6258f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6259f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6260f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6261cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6262cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6263cdb7a50dSMatthew G. Knepley }
6264cdb7a50dSMatthew G. Knepley 
6265cdb7a50dSMatthew G. Knepley /*@C
6266cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6267cdb7a50dSMatthew G. Knepley 
6268cdb7a50dSMatthew G. Knepley   Input Parameters:
6269cdb7a50dSMatthew G. Knepley + dm   - The original DM
6270cdb7a50dSMatthew G. Knepley . name - The sequence name
6271cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6272cdb7a50dSMatthew G. Knepley 
6273cdb7a50dSMatthew G. Knepley   Output Parameter:
6274cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6275cdb7a50dSMatthew G. Knepley 
6276cdb7a50dSMatthew G. Knepley   Level: intermediate
6277cdb7a50dSMatthew G. Knepley 
6278cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6279cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6280cdb7a50dSMatthew G. Knepley 
6281cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6282cdb7a50dSMatthew G. Knepley @*/
6283cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6284cdb7a50dSMatthew G. Knepley {
6285cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6286cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6287cdb7a50dSMatthew G. Knepley 
6288cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6289cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6290cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6291cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
6292cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6293cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6294cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6295cdb7a50dSMatthew G. Knepley     PetscScalar value;
6296cdb7a50dSMatthew G. Knepley 
629739d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
62984aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6299cdb7a50dSMatthew G. Knepley #endif
6300cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6301f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6302f4d763aaSMatthew G. Knepley }
63038e4ac7eaSMatthew G. Knepley 
63048e4ac7eaSMatthew G. Knepley /*@
63058e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
63068e4ac7eaSMatthew G. Knepley 
63078e4ac7eaSMatthew G. Knepley   Not collective
63088e4ac7eaSMatthew G. Knepley 
63098e4ac7eaSMatthew G. Knepley   Input Parameter:
63108e4ac7eaSMatthew G. Knepley . dm - The DM
63118e4ac7eaSMatthew G. Knepley 
63128e4ac7eaSMatthew G. Knepley   Output Parameter:
63138e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
63148e4ac7eaSMatthew G. Knepley 
63158e4ac7eaSMatthew G. Knepley   Level: beginner
63168e4ac7eaSMatthew G. Knepley 
63178e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
63188e4ac7eaSMatthew G. Knepley @*/
63198e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
63208e4ac7eaSMatthew G. Knepley {
63218e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
63228e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63238e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
63248e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
63258e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
63268e4ac7eaSMatthew G. Knepley }
63278e4ac7eaSMatthew G. Knepley 
63288e4ac7eaSMatthew G. Knepley /*@
63295d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
63308e4ac7eaSMatthew G. Knepley 
63318e4ac7eaSMatthew G. Knepley   Collective on dm
63328e4ac7eaSMatthew G. Knepley 
63338e4ac7eaSMatthew G. Knepley   Input Parameters:
63348e4ac7eaSMatthew G. Knepley + dm - The DM
63358e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
63368e4ac7eaSMatthew G. Knepley 
63375d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
63385d3b26e6SMatthew G. Knepley 
63398e4ac7eaSMatthew G. Knepley   Level: beginner
63408e4ac7eaSMatthew G. Knepley 
63415d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
63428e4ac7eaSMatthew G. Knepley @*/
63438e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
63448e4ac7eaSMatthew G. Knepley {
63458e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
63468e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63478833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
63488e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
63498e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
63508e4ac7eaSMatthew G. Knepley }
6351c58f1c22SToby Isaac 
6352c58f1c22SToby Isaac 
6353c58f1c22SToby Isaac /*@C
6354c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6355c58f1c22SToby Isaac 
6356c58f1c22SToby Isaac   Not Collective
6357c58f1c22SToby Isaac 
6358c58f1c22SToby Isaac   Input Parameters:
6359c58f1c22SToby Isaac + dm   - The DM object
6360c58f1c22SToby Isaac - name - The label name
6361c58f1c22SToby Isaac 
6362c58f1c22SToby Isaac   Level: intermediate
6363c58f1c22SToby Isaac 
6364c58f1c22SToby Isaac .keywords: mesh
6365c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6366c58f1c22SToby Isaac @*/
6367c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6368c58f1c22SToby Isaac {
6369c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
6370c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
6371d67d17b1SMatthew G. Knepley   const char    *lname;
6372c58f1c22SToby Isaac   PetscErrorCode ierr;
6373c58f1c22SToby Isaac 
6374c58f1c22SToby Isaac   PetscFunctionBegin;
6375c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6376c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6377c58f1c22SToby Isaac   while (next) {
6378d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6379d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6380c58f1c22SToby Isaac     if (flg) break;
6381c58f1c22SToby Isaac     next = next->next;
6382c58f1c22SToby Isaac   }
6383c58f1c22SToby Isaac   if (!flg) {
6384c58f1c22SToby Isaac     DMLabelLink tmpLabel;
6385c58f1c22SToby Isaac 
6386c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6387d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
6388c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
6389c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
6390c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
6391c58f1c22SToby Isaac   }
6392c58f1c22SToby Isaac   PetscFunctionReturn(0);
6393c58f1c22SToby Isaac }
6394c58f1c22SToby Isaac 
6395c58f1c22SToby Isaac /*@C
6396c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6397c58f1c22SToby Isaac 
6398c58f1c22SToby Isaac   Not Collective
6399c58f1c22SToby Isaac 
6400c58f1c22SToby Isaac   Input Parameters:
6401c58f1c22SToby Isaac + dm   - The DM object
6402c58f1c22SToby Isaac . name - The label name
6403c58f1c22SToby Isaac - point - The mesh point
6404c58f1c22SToby Isaac 
6405c58f1c22SToby Isaac   Output Parameter:
6406c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6407c58f1c22SToby Isaac 
6408c58f1c22SToby Isaac   Level: beginner
6409c58f1c22SToby Isaac 
6410c58f1c22SToby Isaac .keywords: mesh
6411c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6412c58f1c22SToby Isaac @*/
6413c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6414c58f1c22SToby Isaac {
6415c58f1c22SToby Isaac   DMLabel        label;
6416c58f1c22SToby Isaac   PetscErrorCode ierr;
6417c58f1c22SToby Isaac 
6418c58f1c22SToby Isaac   PetscFunctionBegin;
6419c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6420c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6421c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
642213903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6423c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6424c58f1c22SToby Isaac   PetscFunctionReturn(0);
6425c58f1c22SToby Isaac }
6426c58f1c22SToby Isaac 
6427c58f1c22SToby Isaac /*@C
6428c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6429c58f1c22SToby Isaac 
6430c58f1c22SToby Isaac   Not Collective
6431c58f1c22SToby Isaac 
6432c58f1c22SToby Isaac   Input Parameters:
6433c58f1c22SToby Isaac + dm   - The DM object
6434c58f1c22SToby Isaac . name - The label name
6435c58f1c22SToby Isaac . point - The mesh point
6436c58f1c22SToby Isaac - value - The label value for this point
6437c58f1c22SToby Isaac 
6438c58f1c22SToby Isaac   Output Parameter:
6439c58f1c22SToby Isaac 
6440c58f1c22SToby Isaac   Level: beginner
6441c58f1c22SToby Isaac 
6442c58f1c22SToby Isaac .keywords: mesh
6443c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6444c58f1c22SToby Isaac @*/
6445c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6446c58f1c22SToby Isaac {
6447c58f1c22SToby Isaac   DMLabel        label;
6448c58f1c22SToby Isaac   PetscErrorCode ierr;
6449c58f1c22SToby Isaac 
6450c58f1c22SToby Isaac   PetscFunctionBegin;
6451c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6452c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6453c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6454c58f1c22SToby Isaac   if (!label) {
6455c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6456c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6457c58f1c22SToby Isaac   }
6458c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6459c58f1c22SToby Isaac   PetscFunctionReturn(0);
6460c58f1c22SToby Isaac }
6461c58f1c22SToby Isaac 
6462c58f1c22SToby Isaac /*@C
6463c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6464c58f1c22SToby Isaac 
6465c58f1c22SToby Isaac   Not Collective
6466c58f1c22SToby Isaac 
6467c58f1c22SToby Isaac   Input Parameters:
6468c58f1c22SToby Isaac + dm   - The DM object
6469c58f1c22SToby Isaac . name - The label name
6470c58f1c22SToby Isaac . point - The mesh point
6471c58f1c22SToby Isaac - value - The label value for this point
6472c58f1c22SToby Isaac 
6473c58f1c22SToby Isaac   Output Parameter:
6474c58f1c22SToby Isaac 
6475c58f1c22SToby Isaac   Level: beginner
6476c58f1c22SToby Isaac 
6477c58f1c22SToby Isaac .keywords: mesh
6478c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6479c58f1c22SToby Isaac @*/
6480c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6481c58f1c22SToby Isaac {
6482c58f1c22SToby Isaac   DMLabel        label;
6483c58f1c22SToby Isaac   PetscErrorCode ierr;
6484c58f1c22SToby Isaac 
6485c58f1c22SToby Isaac   PetscFunctionBegin;
6486c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6487c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6488c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6489c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6490c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6491c58f1c22SToby Isaac   PetscFunctionReturn(0);
6492c58f1c22SToby Isaac }
6493c58f1c22SToby Isaac 
6494c58f1c22SToby Isaac /*@C
6495c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6496c58f1c22SToby Isaac 
6497c58f1c22SToby Isaac   Not Collective
6498c58f1c22SToby Isaac 
6499c58f1c22SToby Isaac   Input Parameters:
6500c58f1c22SToby Isaac + dm   - The DM object
6501c58f1c22SToby Isaac - name - The label name
6502c58f1c22SToby Isaac 
6503c58f1c22SToby Isaac   Output Parameter:
6504c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6505c58f1c22SToby Isaac 
6506c58f1c22SToby Isaac   Level: beginner
6507c58f1c22SToby Isaac 
6508c58f1c22SToby Isaac .keywords: mesh
6509df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6510c58f1c22SToby Isaac @*/
6511c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6512c58f1c22SToby Isaac {
6513c58f1c22SToby Isaac   DMLabel        label;
6514c58f1c22SToby Isaac   PetscErrorCode ierr;
6515c58f1c22SToby Isaac 
6516c58f1c22SToby Isaac   PetscFunctionBegin;
6517c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6518c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6519c58f1c22SToby Isaac   PetscValidPointer(size, 3);
6520c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6521c58f1c22SToby Isaac   *size = 0;
6522c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6523c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6524c58f1c22SToby Isaac   PetscFunctionReturn(0);
6525c58f1c22SToby Isaac }
6526c58f1c22SToby Isaac 
6527c58f1c22SToby Isaac /*@C
6528c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6529c58f1c22SToby Isaac 
6530c58f1c22SToby Isaac   Not Collective
6531c58f1c22SToby Isaac 
6532c58f1c22SToby Isaac   Input Parameters:
6533c58f1c22SToby Isaac + mesh - The DM object
6534c58f1c22SToby Isaac - name - The label name
6535c58f1c22SToby Isaac 
6536c58f1c22SToby Isaac   Output Parameter:
6537c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6538c58f1c22SToby Isaac 
6539c58f1c22SToby Isaac   Level: beginner
6540c58f1c22SToby Isaac 
6541c58f1c22SToby Isaac .keywords: mesh
6542c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6543c58f1c22SToby Isaac @*/
6544c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6545c58f1c22SToby Isaac {
6546c58f1c22SToby Isaac   DMLabel        label;
6547c58f1c22SToby Isaac   PetscErrorCode ierr;
6548c58f1c22SToby Isaac 
6549c58f1c22SToby Isaac   PetscFunctionBegin;
6550c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6551c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6552c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6553c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6554c58f1c22SToby Isaac   *ids = NULL;
6555dab2e251SBlaise Bourdin  if (label) {
6556c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6557dab2e251SBlaise Bourdin   } else {
6558dab2e251SBlaise Bourdin     /* returning an empty IS */
6559dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6560dab2e251SBlaise Bourdin   }
6561c58f1c22SToby Isaac   PetscFunctionReturn(0);
6562c58f1c22SToby Isaac }
6563c58f1c22SToby Isaac 
6564c58f1c22SToby Isaac /*@C
6565c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6566c58f1c22SToby Isaac 
6567c58f1c22SToby Isaac   Not Collective
6568c58f1c22SToby Isaac 
6569c58f1c22SToby Isaac   Input Parameters:
6570c58f1c22SToby Isaac + dm - The DM object
6571c58f1c22SToby Isaac . name - The label name
6572c58f1c22SToby Isaac - value - The stratum value
6573c58f1c22SToby Isaac 
6574c58f1c22SToby Isaac   Output Parameter:
6575c58f1c22SToby Isaac . size - The stratum size
6576c58f1c22SToby Isaac 
6577c58f1c22SToby Isaac   Level: beginner
6578c58f1c22SToby Isaac 
6579c58f1c22SToby Isaac .keywords: mesh
6580c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6581c58f1c22SToby Isaac @*/
6582c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6583c58f1c22SToby Isaac {
6584c58f1c22SToby Isaac   DMLabel        label;
6585c58f1c22SToby Isaac   PetscErrorCode ierr;
6586c58f1c22SToby Isaac 
6587c58f1c22SToby Isaac   PetscFunctionBegin;
6588c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6589c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6590c58f1c22SToby Isaac   PetscValidPointer(size, 4);
6591c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6592c58f1c22SToby Isaac   *size = 0;
6593c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6594c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6595c58f1c22SToby Isaac   PetscFunctionReturn(0);
6596c58f1c22SToby Isaac }
6597c58f1c22SToby Isaac 
6598c58f1c22SToby Isaac /*@C
6599c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6600c58f1c22SToby Isaac 
6601c58f1c22SToby Isaac   Not Collective
6602c58f1c22SToby Isaac 
6603c58f1c22SToby Isaac   Input Parameters:
6604c58f1c22SToby Isaac + dm - The DM object
6605c58f1c22SToby Isaac . name - The label name
6606c58f1c22SToby Isaac - value - The stratum value
6607c58f1c22SToby Isaac 
6608c58f1c22SToby Isaac   Output Parameter:
6609c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6610c58f1c22SToby Isaac 
6611c58f1c22SToby Isaac   Level: beginner
6612c58f1c22SToby Isaac 
6613c58f1c22SToby Isaac .keywords: mesh
6614c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6615c58f1c22SToby Isaac @*/
6616c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6617c58f1c22SToby Isaac {
6618c58f1c22SToby Isaac   DMLabel        label;
6619c58f1c22SToby Isaac   PetscErrorCode ierr;
6620c58f1c22SToby Isaac 
6621c58f1c22SToby Isaac   PetscFunctionBegin;
6622c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6623c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6624c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6625c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6626c58f1c22SToby Isaac   *points = NULL;
6627c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6628c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6629c58f1c22SToby Isaac   PetscFunctionReturn(0);
6630c58f1c22SToby Isaac }
6631c58f1c22SToby Isaac 
66324de306b1SToby Isaac /*@C
66339044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
66344de306b1SToby Isaac 
66354de306b1SToby Isaac   Not Collective
66364de306b1SToby Isaac 
66374de306b1SToby Isaac   Input Parameters:
66384de306b1SToby Isaac + dm - The DM object
66394de306b1SToby Isaac . name - The label name
66404de306b1SToby Isaac . value - The stratum value
66414de306b1SToby Isaac - points - The stratum points
66424de306b1SToby Isaac 
66434de306b1SToby Isaac   Level: beginner
66444de306b1SToby Isaac 
66454de306b1SToby Isaac .keywords: mesh
66464de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
66474de306b1SToby Isaac @*/
66484de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
66494de306b1SToby Isaac {
66504de306b1SToby Isaac   DMLabel        label;
66514de306b1SToby Isaac   PetscErrorCode ierr;
66524de306b1SToby Isaac 
66534de306b1SToby Isaac   PetscFunctionBegin;
66544de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66554de306b1SToby Isaac   PetscValidCharPointer(name, 2);
66564de306b1SToby Isaac   PetscValidPointer(points, 4);
66574de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
66584de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
66594de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
66604de306b1SToby Isaac   PetscFunctionReturn(0);
66614de306b1SToby Isaac }
66624de306b1SToby Isaac 
6663c58f1c22SToby Isaac /*@C
6664c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6665c58f1c22SToby Isaac 
6666c58f1c22SToby Isaac   Not Collective
6667c58f1c22SToby Isaac 
6668c58f1c22SToby Isaac   Input Parameters:
6669c58f1c22SToby Isaac + dm   - The DM object
6670c58f1c22SToby Isaac . name - The label name
6671c58f1c22SToby Isaac - value - The label value for this point
6672c58f1c22SToby Isaac 
6673c58f1c22SToby Isaac   Output Parameter:
6674c58f1c22SToby Isaac 
6675c58f1c22SToby Isaac   Level: beginner
6676c58f1c22SToby Isaac 
6677c58f1c22SToby Isaac .keywords: mesh
6678c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6679c58f1c22SToby Isaac @*/
6680c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6681c58f1c22SToby Isaac {
6682c58f1c22SToby Isaac   DMLabel        label;
6683c58f1c22SToby Isaac   PetscErrorCode ierr;
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac   PetscFunctionBegin;
6686c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6687c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6688c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6689c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6690c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6691c58f1c22SToby Isaac   PetscFunctionReturn(0);
6692c58f1c22SToby Isaac }
6693c58f1c22SToby Isaac 
6694c58f1c22SToby Isaac /*@
6695c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6696c58f1c22SToby Isaac 
6697c58f1c22SToby Isaac   Not Collective
6698c58f1c22SToby Isaac 
6699c58f1c22SToby Isaac   Input Parameter:
6700c58f1c22SToby Isaac . dm   - The DM object
6701c58f1c22SToby Isaac 
6702c58f1c22SToby Isaac   Output Parameter:
6703c58f1c22SToby Isaac . numLabels - the number of Labels
6704c58f1c22SToby Isaac 
6705c58f1c22SToby Isaac   Level: intermediate
6706c58f1c22SToby Isaac 
6707c58f1c22SToby Isaac .keywords: mesh
6708c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6709c58f1c22SToby Isaac @*/
6710c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6711c58f1c22SToby Isaac {
6712c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6713c58f1c22SToby Isaac   PetscInt  n    = 0;
6714c58f1c22SToby Isaac 
6715c58f1c22SToby Isaac   PetscFunctionBegin;
6716c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6717c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
6718c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6719c58f1c22SToby Isaac   *numLabels = n;
6720c58f1c22SToby Isaac   PetscFunctionReturn(0);
6721c58f1c22SToby Isaac }
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac /*@C
6724c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6725c58f1c22SToby Isaac 
6726c58f1c22SToby Isaac   Not Collective
6727c58f1c22SToby Isaac 
6728c58f1c22SToby Isaac   Input Parameters:
6729c58f1c22SToby Isaac + dm - The DM object
6730c58f1c22SToby Isaac - n  - the label number
6731c58f1c22SToby Isaac 
6732c58f1c22SToby Isaac   Output Parameter:
6733c58f1c22SToby Isaac . name - the label name
6734c58f1c22SToby Isaac 
6735c58f1c22SToby Isaac   Level: intermediate
6736c58f1c22SToby Isaac 
6737c58f1c22SToby Isaac .keywords: mesh
6738c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6739c58f1c22SToby Isaac @*/
6740c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6741c58f1c22SToby Isaac {
6742c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6743c58f1c22SToby Isaac   PetscInt       l    = 0;
6744d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac   PetscFunctionBegin;
6747c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6748c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6749c58f1c22SToby Isaac   while (next) {
6750c58f1c22SToby Isaac     if (l == n) {
6751d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6752c58f1c22SToby Isaac       PetscFunctionReturn(0);
6753c58f1c22SToby Isaac     }
6754c58f1c22SToby Isaac     ++l;
6755c58f1c22SToby Isaac     next = next->next;
6756c58f1c22SToby Isaac   }
6757c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6758c58f1c22SToby Isaac }
6759c58f1c22SToby Isaac 
6760c58f1c22SToby Isaac /*@C
6761c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6762c58f1c22SToby Isaac 
6763c58f1c22SToby Isaac   Not Collective
6764c58f1c22SToby Isaac 
6765c58f1c22SToby Isaac   Input Parameters:
6766c58f1c22SToby Isaac + dm   - The DM object
6767c58f1c22SToby Isaac - name - The label name
6768c58f1c22SToby Isaac 
6769c58f1c22SToby Isaac   Output Parameter:
6770c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6771c58f1c22SToby Isaac 
6772c58f1c22SToby Isaac   Level: intermediate
6773c58f1c22SToby Isaac 
6774c58f1c22SToby Isaac .keywords: mesh
6775c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6776c58f1c22SToby Isaac @*/
6777c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6778c58f1c22SToby Isaac {
6779c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6780d67d17b1SMatthew G. Knepley   const char    *lname;
6781c58f1c22SToby Isaac   PetscErrorCode ierr;
6782c58f1c22SToby Isaac 
6783c58f1c22SToby Isaac   PetscFunctionBegin;
6784c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6785c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6786c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
6787c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6788c58f1c22SToby Isaac   while (next) {
6789d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6790d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
6791c58f1c22SToby Isaac     if (*hasLabel) break;
6792c58f1c22SToby Isaac     next = next->next;
6793c58f1c22SToby Isaac   }
6794c58f1c22SToby Isaac   PetscFunctionReturn(0);
6795c58f1c22SToby Isaac }
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac /*@C
6798c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac   Not Collective
6801c58f1c22SToby Isaac 
6802c58f1c22SToby Isaac   Input Parameters:
6803c58f1c22SToby Isaac + dm   - The DM object
6804c58f1c22SToby Isaac - name - The label name
6805c58f1c22SToby Isaac 
6806c58f1c22SToby Isaac   Output Parameter:
6807c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6808c58f1c22SToby Isaac 
6809c58f1c22SToby Isaac   Level: intermediate
6810c58f1c22SToby Isaac 
6811c58f1c22SToby Isaac .keywords: mesh
6812c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6813c58f1c22SToby Isaac @*/
6814c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6815c58f1c22SToby Isaac {
6816c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6817c58f1c22SToby Isaac   PetscBool      hasLabel;
6818d67d17b1SMatthew G. Knepley   const char    *lname;
6819c58f1c22SToby Isaac   PetscErrorCode ierr;
6820c58f1c22SToby Isaac 
6821c58f1c22SToby Isaac   PetscFunctionBegin;
6822c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6823c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6824c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6825c58f1c22SToby Isaac   *label = NULL;
6826c58f1c22SToby Isaac   while (next) {
6827d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6828d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6829c58f1c22SToby Isaac     if (hasLabel) {
6830c58f1c22SToby Isaac       *label = next->label;
6831c58f1c22SToby Isaac       break;
6832c58f1c22SToby Isaac     }
6833c58f1c22SToby Isaac     next = next->next;
6834c58f1c22SToby Isaac   }
6835c58f1c22SToby Isaac   PetscFunctionReturn(0);
6836c58f1c22SToby Isaac }
6837c58f1c22SToby Isaac 
6838c58f1c22SToby Isaac /*@C
6839c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6840c58f1c22SToby Isaac 
6841c58f1c22SToby Isaac   Not Collective
6842c58f1c22SToby Isaac 
6843c58f1c22SToby Isaac   Input Parameters:
6844c58f1c22SToby Isaac + dm - The DM object
6845c58f1c22SToby Isaac - n  - the label number
6846c58f1c22SToby Isaac 
6847c58f1c22SToby Isaac   Output Parameter:
6848c58f1c22SToby Isaac . label - the label
6849c58f1c22SToby Isaac 
6850c58f1c22SToby Isaac   Level: intermediate
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac .keywords: mesh
6853c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6854c58f1c22SToby Isaac @*/
6855c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6856c58f1c22SToby Isaac {
6857c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6858c58f1c22SToby Isaac   PetscInt    l    = 0;
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac   PetscFunctionBegin;
6861c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6862c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6863c58f1c22SToby Isaac   while (next) {
6864c58f1c22SToby Isaac     if (l == n) {
6865c58f1c22SToby Isaac       *label = next->label;
6866c58f1c22SToby Isaac       PetscFunctionReturn(0);
6867c58f1c22SToby Isaac     }
6868c58f1c22SToby Isaac     ++l;
6869c58f1c22SToby Isaac     next = next->next;
6870c58f1c22SToby Isaac   }
6871c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6872c58f1c22SToby Isaac }
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac /*@C
6875c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Not Collective
6878c58f1c22SToby Isaac 
6879c58f1c22SToby Isaac   Input Parameters:
6880c58f1c22SToby Isaac + dm   - The DM object
6881c58f1c22SToby Isaac - label - The DMLabel
6882c58f1c22SToby Isaac 
6883c58f1c22SToby Isaac   Level: developer
6884c58f1c22SToby Isaac 
6885c58f1c22SToby Isaac .keywords: mesh
6886c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6887c58f1c22SToby Isaac @*/
6888c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6889c58f1c22SToby Isaac {
6890c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
6891c58f1c22SToby Isaac   PetscBool      hasLabel;
6892d67d17b1SMatthew G. Knepley   const char    *lname;
6893c58f1c22SToby Isaac   PetscErrorCode ierr;
6894c58f1c22SToby Isaac 
6895c58f1c22SToby Isaac   PetscFunctionBegin;
6896c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6897d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
6898d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
6899d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
6900c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6901c58f1c22SToby Isaac   tmpLabel->label  = label;
6902c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
6903c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
6904c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
6905c58f1c22SToby Isaac   PetscFunctionReturn(0);
6906c58f1c22SToby Isaac }
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac /*@C
6909c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
6910c58f1c22SToby Isaac 
6911c58f1c22SToby Isaac   Not Collective
6912c58f1c22SToby Isaac 
6913c58f1c22SToby Isaac   Input Parameters:
6914c58f1c22SToby Isaac + dm   - The DM object
6915c58f1c22SToby Isaac - name - The label name
6916c58f1c22SToby Isaac 
6917c58f1c22SToby Isaac   Output Parameter:
6918c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6919c58f1c22SToby Isaac 
6920c58f1c22SToby Isaac   Level: developer
6921c58f1c22SToby Isaac 
6922c58f1c22SToby Isaac .keywords: mesh
6923c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6924c58f1c22SToby Isaac @*/
6925c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
6926c58f1c22SToby Isaac {
6927c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6928c58f1c22SToby Isaac   DMLabelLink    last = NULL;
6929c58f1c22SToby Isaac   PetscBool      hasLabel;
6930d67d17b1SMatthew G. Knepley   const char    *lname;
6931c58f1c22SToby Isaac   PetscErrorCode ierr;
6932c58f1c22SToby Isaac 
6933c58f1c22SToby Isaac   PetscFunctionBegin;
6934c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6935c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
6936c58f1c22SToby Isaac   *label = NULL;
6937c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
6938c58f1c22SToby Isaac   while (next) {
6939d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6940d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6941c58f1c22SToby Isaac     if (hasLabel) {
6942c58f1c22SToby Isaac       if (last) last->next       = next->next;
6943c58f1c22SToby Isaac       else      dm->labels->next = next->next;
6944c58f1c22SToby Isaac       next->next = NULL;
6945c58f1c22SToby Isaac       *label     = next->label;
6946c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
6947c58f1c22SToby Isaac       if (hasLabel) {
6948c58f1c22SToby Isaac         dm->depthLabel = NULL;
6949c58f1c22SToby Isaac       }
6950c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
6951c58f1c22SToby Isaac       break;
6952c58f1c22SToby Isaac     }
6953c58f1c22SToby Isaac     last = next;
6954c58f1c22SToby Isaac     next = next->next;
6955c58f1c22SToby Isaac   }
6956c58f1c22SToby Isaac   PetscFunctionReturn(0);
6957c58f1c22SToby Isaac }
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac /*@C
6960c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6961c58f1c22SToby Isaac 
6962c58f1c22SToby Isaac   Not Collective
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac   Input Parameters:
6965c58f1c22SToby Isaac + dm   - The DM object
6966c58f1c22SToby Isaac - name - The label name
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   Output Parameter:
6969c58f1c22SToby Isaac . output - The flag for output
6970c58f1c22SToby Isaac 
6971c58f1c22SToby Isaac   Level: developer
6972c58f1c22SToby Isaac 
6973c58f1c22SToby Isaac .keywords: mesh
6974c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6975c58f1c22SToby Isaac @*/
6976c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6977c58f1c22SToby Isaac {
6978c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6979d67d17b1SMatthew G. Knepley   const char    *lname;
6980c58f1c22SToby Isaac   PetscErrorCode ierr;
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac   PetscFunctionBegin;
6983c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6984c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6985c58f1c22SToby Isaac   PetscValidPointer(output, 3);
6986c58f1c22SToby Isaac   while (next) {
6987c58f1c22SToby Isaac     PetscBool flg;
6988c58f1c22SToby Isaac 
6989d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6990d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6991c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6992c58f1c22SToby Isaac     next = next->next;
6993c58f1c22SToby Isaac   }
6994c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6995c58f1c22SToby Isaac }
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac /*@C
6998c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
6999c58f1c22SToby Isaac 
7000c58f1c22SToby Isaac   Not Collective
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac   Input Parameters:
7003c58f1c22SToby Isaac + dm     - The DM object
7004c58f1c22SToby Isaac . name   - The label name
7005c58f1c22SToby Isaac - output - The flag for output
7006c58f1c22SToby Isaac 
7007c58f1c22SToby Isaac   Level: developer
7008c58f1c22SToby Isaac 
7009c58f1c22SToby Isaac .keywords: mesh
7010c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7011c58f1c22SToby Isaac @*/
7012c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7013c58f1c22SToby Isaac {
7014c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7015d67d17b1SMatthew G. Knepley   const char    *lname;
7016c58f1c22SToby Isaac   PetscErrorCode ierr;
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac   PetscFunctionBegin;
7019c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7020c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7021c58f1c22SToby Isaac   while (next) {
7022c58f1c22SToby Isaac     PetscBool flg;
7023c58f1c22SToby Isaac 
7024d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7025d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7026c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7027c58f1c22SToby Isaac     next = next->next;
7028c58f1c22SToby Isaac   }
7029c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7030c58f1c22SToby Isaac }
7031c58f1c22SToby Isaac 
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac /*@
7034c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7035c58f1c22SToby Isaac 
7036c58f1c22SToby Isaac   Collective on DM
7037c58f1c22SToby Isaac 
7038c58f1c22SToby Isaac   Input Parameter:
70392e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
7040c58f1c22SToby Isaac 
7041c58f1c22SToby Isaac   Output Parameter:
70422e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
7043c58f1c22SToby Isaac 
7044c58f1c22SToby Isaac   Level: intermediate
7045c58f1c22SToby Isaac 
7046c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7047c58f1c22SToby Isaac 
7048c58f1c22SToby Isaac .keywords: mesh
7049367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
7050c58f1c22SToby Isaac @*/
7051c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
7052c58f1c22SToby Isaac {
7053c58f1c22SToby Isaac   PetscInt       numLabels, l;
7054c58f1c22SToby Isaac   PetscErrorCode ierr;
7055c58f1c22SToby Isaac 
7056c58f1c22SToby Isaac   PetscFunctionBegin;
7057c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
7058c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
7059c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
7060c58f1c22SToby Isaac     DMLabel     label, labelNew;
7061c58f1c22SToby Isaac     const char *name;
7062c58f1c22SToby Isaac     PetscBool   flg;
7063c58f1c22SToby Isaac 
7064c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
7065c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7066c58f1c22SToby Isaac     if (flg) continue;
70677d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
70687d5acc75SStefano Zampini     if (flg) continue;
7069c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
7070c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
7071c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
7072c58f1c22SToby Isaac   }
7073c58f1c22SToby Isaac   PetscFunctionReturn(0);
7074c58f1c22SToby Isaac }
7075a8fb8f29SToby Isaac 
7076a8fb8f29SToby Isaac /*@
7077a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7078a8fb8f29SToby Isaac 
7079a8fb8f29SToby Isaac   Input Parameter:
7080a8fb8f29SToby Isaac . dm - The DM object
7081a8fb8f29SToby Isaac 
7082a8fb8f29SToby Isaac   Output Parameter:
7083a8fb8f29SToby Isaac . cdm - The coarse DM
7084a8fb8f29SToby Isaac 
7085a8fb8f29SToby Isaac   Level: intermediate
7086a8fb8f29SToby Isaac 
7087a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7088a8fb8f29SToby Isaac @*/
7089a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7090a8fb8f29SToby Isaac {
7091a8fb8f29SToby Isaac   PetscFunctionBegin;
7092a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7093a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7094a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7095a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7096a8fb8f29SToby Isaac }
7097a8fb8f29SToby Isaac 
7098a8fb8f29SToby Isaac /*@
7099a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7100a8fb8f29SToby Isaac 
7101a8fb8f29SToby Isaac   Input Parameters:
7102a8fb8f29SToby Isaac + dm - The DM object
7103a8fb8f29SToby Isaac - cdm - The coarse DM
7104a8fb8f29SToby Isaac 
7105a8fb8f29SToby Isaac   Level: intermediate
7106a8fb8f29SToby Isaac 
7107a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7108a8fb8f29SToby Isaac @*/
7109a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7110a8fb8f29SToby Isaac {
7111a8fb8f29SToby Isaac   PetscErrorCode ierr;
7112a8fb8f29SToby Isaac 
7113a8fb8f29SToby Isaac   PetscFunctionBegin;
7114a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7115a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7116a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7117a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7118a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7119a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7120a8fb8f29SToby Isaac }
7121a8fb8f29SToby Isaac 
712288bdff64SToby Isaac /*@
712388bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
712488bdff64SToby Isaac 
712588bdff64SToby Isaac   Input Parameter:
712688bdff64SToby Isaac . dm - The DM object
712788bdff64SToby Isaac 
712888bdff64SToby Isaac   Output Parameter:
712988bdff64SToby Isaac . fdm - The fine DM
713088bdff64SToby Isaac 
713188bdff64SToby Isaac   Level: intermediate
713288bdff64SToby Isaac 
713388bdff64SToby Isaac .seealso: DMSetFineDM()
713488bdff64SToby Isaac @*/
713588bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
713688bdff64SToby Isaac {
713788bdff64SToby Isaac   PetscFunctionBegin;
713888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
713988bdff64SToby Isaac   PetscValidPointer(fdm, 2);
714088bdff64SToby Isaac   *fdm = dm->fineMesh;
714188bdff64SToby Isaac   PetscFunctionReturn(0);
714288bdff64SToby Isaac }
714388bdff64SToby Isaac 
714488bdff64SToby Isaac /*@
714588bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
714688bdff64SToby Isaac 
714788bdff64SToby Isaac   Input Parameters:
714888bdff64SToby Isaac + dm - The DM object
714988bdff64SToby Isaac - fdm - The fine DM
715088bdff64SToby Isaac 
715188bdff64SToby Isaac   Level: intermediate
715288bdff64SToby Isaac 
715388bdff64SToby Isaac .seealso: DMGetFineDM()
715488bdff64SToby Isaac @*/
715588bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
715688bdff64SToby Isaac {
715788bdff64SToby Isaac   PetscErrorCode ierr;
715888bdff64SToby Isaac 
715988bdff64SToby Isaac   PetscFunctionBegin;
716088bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
716188bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
716288bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
716388bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
716488bdff64SToby Isaac   dm->fineMesh = fdm;
716588bdff64SToby Isaac   PetscFunctionReturn(0);
716688bdff64SToby Isaac }
716788bdff64SToby Isaac 
7168a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7169a6ba4734SToby Isaac 
7170a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7171a6ba4734SToby Isaac {
7172e5e52638SMatthew G. Knepley   PetscInt       d;
7173a6ba4734SToby Isaac   PetscErrorCode ierr;
7174a6ba4734SToby Isaac 
7175a6ba4734SToby Isaac   PetscFunctionBegin;
7176e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7177e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7178e5e52638SMatthew G. Knepley   }
7179a6ba4734SToby Isaac   PetscFunctionReturn(0);
7180a6ba4734SToby Isaac }
7181a6ba4734SToby Isaac 
7182a6ba4734SToby Isaac /*@C
7183a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7184a6ba4734SToby Isaac 
7185a6ba4734SToby Isaac   Input Parameters:
71864c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7187f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7188a6ba4734SToby Isaac . name        - The BC name
7189a6ba4734SToby Isaac . labelname   - The label defining constrained points
7190a6ba4734SToby Isaac . field       - The field to constrain
7191e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7192a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7193a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7194a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7195a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7196a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7197a6ba4734SToby Isaac 
7198a6ba4734SToby Isaac   Options Database Keys:
7199a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7200a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7201a6ba4734SToby Isaac 
7202a6ba4734SToby Isaac   Level: developer
7203a6ba4734SToby Isaac 
7204a6ba4734SToby Isaac .seealso: DMGetBoundary()
7205a6ba4734SToby Isaac @*/
7206a30ec4eaSSatish 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)
7207a6ba4734SToby Isaac {
7208e5e52638SMatthew G. Knepley   PetscDS        ds;
7209a6ba4734SToby Isaac   PetscErrorCode ierr;
7210a6ba4734SToby Isaac 
7211a6ba4734SToby Isaac   PetscFunctionBegin;
7212a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7213e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7214e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7215a6ba4734SToby Isaac   PetscFunctionReturn(0);
7216a6ba4734SToby Isaac }
7217a6ba4734SToby Isaac 
7218a6ba4734SToby Isaac /*@
7219a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7220a6ba4734SToby Isaac 
7221a6ba4734SToby Isaac   Input Parameters:
7222a6ba4734SToby Isaac . dm - The mesh object
7223a6ba4734SToby Isaac 
7224a6ba4734SToby Isaac   Output Parameters:
7225a6ba4734SToby Isaac . numBd - The number of BC
7226a6ba4734SToby Isaac 
7227a6ba4734SToby Isaac   Level: intermediate
7228a6ba4734SToby Isaac 
7229a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7230a6ba4734SToby Isaac @*/
7231a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7232a6ba4734SToby Isaac {
7233e5e52638SMatthew G. Knepley   PetscDS        ds;
723458ebd649SToby Isaac   PetscErrorCode ierr;
7235a6ba4734SToby Isaac 
7236a6ba4734SToby Isaac   PetscFunctionBegin;
7237a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7238e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7239e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7240a6ba4734SToby Isaac   PetscFunctionReturn(0);
7241a6ba4734SToby Isaac }
7242a6ba4734SToby Isaac 
7243a6ba4734SToby Isaac /*@C
72441c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7245a6ba4734SToby Isaac 
7246a6ba4734SToby Isaac   Input Parameters:
7247a6ba4734SToby Isaac + dm          - The mesh object
7248a6ba4734SToby Isaac - bd          - The BC number
7249a6ba4734SToby Isaac 
7250a6ba4734SToby Isaac   Output Parameters:
7251f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7252a6ba4734SToby Isaac . name        - The BC name
7253a6ba4734SToby Isaac . labelname   - The label defining constrained points
7254a6ba4734SToby Isaac . field       - The field to constrain
7255a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7256a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7257a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7258a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7259a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7260a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7261a6ba4734SToby Isaac 
7262a6ba4734SToby Isaac   Options Database Keys:
7263a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7264a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7265a6ba4734SToby Isaac 
7266a6ba4734SToby Isaac   Level: developer
7267a6ba4734SToby Isaac 
7268a6ba4734SToby Isaac .seealso: DMAddBoundary()
7269a6ba4734SToby Isaac @*/
7270a30ec4eaSSatish 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)
7271a6ba4734SToby Isaac {
7272e5e52638SMatthew G. Knepley   PetscDS        ds;
727358ebd649SToby Isaac   PetscErrorCode ierr;
7274a6ba4734SToby Isaac 
7275a6ba4734SToby Isaac   PetscFunctionBegin;
7276a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7277e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7278e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7279a6ba4734SToby Isaac   PetscFunctionReturn(0);
7280a6ba4734SToby Isaac }
7281a6ba4734SToby Isaac 
7282e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7283e6f8dbb6SToby Isaac {
7284e5e52638SMatthew G. Knepley   PetscDS        ds;
7285dff059c6SToby Isaac   DMBoundary    *lastnext;
7286e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7287e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7288e6f8dbb6SToby Isaac 
7289e6f8dbb6SToby Isaac   PetscFunctionBegin;
7290e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7291e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
729247a1f5adSToby Isaac   if (dm->boundary) {
729347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
729447a1f5adSToby Isaac 
729547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
729647a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
729747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
729847a1f5adSToby Isaac     while (next) {
729947a1f5adSToby Isaac       DMBoundary b = next;
730047a1f5adSToby Isaac 
730147a1f5adSToby Isaac       next = b->next;
730247a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7303a6ba4734SToby Isaac     }
730447a1f5adSToby Isaac     dm->boundary = NULL;
7305a6ba4734SToby Isaac   }
730647a1f5adSToby Isaac 
7307dff059c6SToby Isaac   lastnext = &(dm->boundary);
7308e6f8dbb6SToby Isaac   while (dsbound) {
7309e6f8dbb6SToby Isaac     DMBoundary dmbound;
7310e6f8dbb6SToby Isaac 
7311e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7312e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7313e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7314994fe344SLisandro 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);}
731547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7316dff059c6SToby Isaac     *lastnext = dmbound;
7317dff059c6SToby Isaac     lastnext = &(dmbound->next);
7318dff059c6SToby Isaac     dsbound = dsbound->next;
7319a6ba4734SToby Isaac   }
7320a6ba4734SToby Isaac   PetscFunctionReturn(0);
7321a6ba4734SToby Isaac }
7322a6ba4734SToby Isaac 
7323a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7324a6ba4734SToby Isaac {
7325b95f2879SToby Isaac   DMBoundary     b;
7326a6ba4734SToby Isaac   PetscErrorCode ierr;
7327a6ba4734SToby Isaac 
7328a6ba4734SToby Isaac   PetscFunctionBegin;
7329a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7330a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
7331a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7332e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7333b95f2879SToby Isaac   b = dm->boundary;
7334a6ba4734SToby Isaac   while (b && !(*isBd)) {
7335e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7336e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
73373424c85cSToby Isaac 
73383424c85cSToby Isaac     if (label) {
7339a6ba4734SToby Isaac       PetscInt i;
7340a6ba4734SToby Isaac 
7341e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7342e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7343a6ba4734SToby Isaac       }
7344a6ba4734SToby Isaac     }
7345a6ba4734SToby Isaac     b = b->next;
7346a6ba4734SToby Isaac   }
7347a6ba4734SToby Isaac   PetscFunctionReturn(0);
7348a6ba4734SToby Isaac }
73494d6f44ffSToby Isaac 
73504d6f44ffSToby Isaac /*@C
73514d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
73524d6f44ffSToby Isaac 
73534d6f44ffSToby Isaac   Input Parameters:
73544d6f44ffSToby Isaac + dm      - The DM
73550709b2feSToby Isaac . time    - The time
73564d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
73574d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
73584d6f44ffSToby Isaac - mode    - The insertion mode for values
73594d6f44ffSToby Isaac 
73604d6f44ffSToby Isaac   Output Parameter:
73614d6f44ffSToby Isaac . X - vector
73624d6f44ffSToby Isaac 
73634d6f44ffSToby Isaac    Calling sequence of func:
73640709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
73654d6f44ffSToby Isaac 
73664d6f44ffSToby Isaac +  dim - The spatial dimension
73674d6f44ffSToby Isaac .  x   - The coordinates
73684d6f44ffSToby Isaac .  Nf  - The number of fields
73694d6f44ffSToby Isaac .  u   - The output field values
73704d6f44ffSToby Isaac -  ctx - optional user-defined function context
73714d6f44ffSToby Isaac 
73724d6f44ffSToby Isaac   Level: developer
73734d6f44ffSToby Isaac 
73742716604bSToby Isaac .seealso: DMComputeL2Diff()
73754d6f44ffSToby Isaac @*/
73760709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
73774d6f44ffSToby Isaac {
73784d6f44ffSToby Isaac   Vec            localX;
73794d6f44ffSToby Isaac   PetscErrorCode ierr;
73804d6f44ffSToby Isaac 
73814d6f44ffSToby Isaac   PetscFunctionBegin;
73824d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73834d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
73840709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
73854d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
73864d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
73874d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
73884d6f44ffSToby Isaac   PetscFunctionReturn(0);
73894d6f44ffSToby Isaac }
73904d6f44ffSToby Isaac 
73910709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
73924d6f44ffSToby Isaac {
73934d6f44ffSToby Isaac   PetscErrorCode ierr;
73944d6f44ffSToby Isaac 
73954d6f44ffSToby Isaac   PetscFunctionBegin;
73964d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
73974d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
73980918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
73990709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
74004d6f44ffSToby Isaac   PetscFunctionReturn(0);
74014d6f44ffSToby Isaac }
74024d6f44ffSToby Isaac 
74032c53366bSMatthew 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)
74042c53366bSMatthew G. Knepley {
74052c53366bSMatthew G. Knepley   Vec            localX;
74062c53366bSMatthew G. Knepley   PetscErrorCode ierr;
74072c53366bSMatthew G. Knepley 
74082c53366bSMatthew G. Knepley   PetscFunctionBegin;
74092c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74102c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
74112c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
74122c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
74132c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
74142c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
74152c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
74162c53366bSMatthew G. Knepley }
74172c53366bSMatthew G. Knepley 
74181c531cf8SMatthew 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)
74194d6f44ffSToby Isaac {
74204d6f44ffSToby Isaac   PetscErrorCode ierr;
74214d6f44ffSToby Isaac 
74224d6f44ffSToby Isaac   PetscFunctionBegin;
74234d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
74244d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
74250918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
74261c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
74274d6f44ffSToby Isaac   PetscFunctionReturn(0);
74284d6f44ffSToby Isaac }
74292716604bSToby Isaac 
74308c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
74318c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
74328c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
74338c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7434191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
74358c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
74368c6c5593SMatthew G. Knepley {
74378c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
74388c6c5593SMatthew G. Knepley 
74398c6c5593SMatthew G. Knepley   PetscFunctionBegin;
74408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
74418c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
74428c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
74430918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
74448c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
74458c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
74468c6c5593SMatthew G. Knepley }
74478c6c5593SMatthew G. Knepley 
74481c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
74498c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
74508c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
74518c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7452191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
74538c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
74548c6c5593SMatthew G. Knepley {
74558c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
74568c6c5593SMatthew G. Knepley 
74578c6c5593SMatthew G. Knepley   PetscFunctionBegin;
74588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
74598c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
74608c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
74610918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
74621c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
74638c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
74648c6c5593SMatthew G. Knepley }
74658c6c5593SMatthew G. Knepley 
74662716604bSToby Isaac /*@C
74672716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
74682716604bSToby Isaac 
74692716604bSToby Isaac   Input Parameters:
74702716604bSToby Isaac + dm    - The DM
74710709b2feSToby Isaac . time  - The time
74722716604bSToby Isaac . funcs - The functions to evaluate for each field component
74732716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7474574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
74752716604bSToby Isaac 
74762716604bSToby Isaac   Output Parameter:
74772716604bSToby Isaac . diff - The diff ||u - u_h||_2
74782716604bSToby Isaac 
74792716604bSToby Isaac   Level: developer
74802716604bSToby Isaac 
74811189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
74822716604bSToby Isaac @*/
74830709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
74842716604bSToby Isaac {
74852716604bSToby Isaac   PetscErrorCode ierr;
74862716604bSToby Isaac 
74872716604bSToby Isaac   PetscFunctionBegin;
74882716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7489b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
74900918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
74910709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
74922716604bSToby Isaac   PetscFunctionReturn(0);
74932716604bSToby Isaac }
7494b698f381SToby Isaac 
7495b698f381SToby Isaac /*@C
7496b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7497b698f381SToby Isaac 
7498b698f381SToby Isaac   Input Parameters:
7499b698f381SToby Isaac + dm    - The DM
7500b698f381SToby Isaac , time  - The time
7501b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
7502b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7503574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
7504b698f381SToby Isaac - n     - The vector to project along
7505b698f381SToby Isaac 
7506b698f381SToby Isaac   Output Parameter:
7507b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
7508b698f381SToby Isaac 
7509b698f381SToby Isaac   Level: developer
7510b698f381SToby Isaac 
7511b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
7512b698f381SToby Isaac @*/
7513b698f381SToby 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)
7514b698f381SToby Isaac {
7515b698f381SToby Isaac   PetscErrorCode ierr;
7516b698f381SToby Isaac 
7517b698f381SToby Isaac   PetscFunctionBegin;
7518b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7519b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7520b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7521b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7522b698f381SToby Isaac   PetscFunctionReturn(0);
7523b698f381SToby Isaac }
7524b698f381SToby Isaac 
75252a16baeaSToby Isaac /*@C
75262a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
75272a16baeaSToby Isaac 
75282a16baeaSToby Isaac   Input Parameters:
75292a16baeaSToby Isaac + dm    - The DM
75302a16baeaSToby Isaac . time  - The time
75312a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
75322a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7533574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
75342a16baeaSToby Isaac 
75352a16baeaSToby Isaac   Output Parameter:
75362a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
75372a16baeaSToby Isaac 
75382a16baeaSToby Isaac   Level: developer
75392a16baeaSToby Isaac 
75401189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
75412a16baeaSToby Isaac @*/
75421189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
75432a16baeaSToby Isaac {
75442a16baeaSToby Isaac   PetscErrorCode ierr;
75452a16baeaSToby Isaac 
75462a16baeaSToby Isaac   PetscFunctionBegin;
75472a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75482a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
75490918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
75502a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
75512a16baeaSToby Isaac   PetscFunctionReturn(0);
75522a16baeaSToby Isaac }
75532a16baeaSToby Isaac 
7554df0b854cSToby Isaac /*@C
7555df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7556cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7557df0b854cSToby Isaac 
7558df0b854cSToby Isaac   Collective on dm
7559df0b854cSToby Isaac 
7560df0b854cSToby Isaac   Input parameters:
7561df0b854cSToby Isaac + dm - the pre-adaptation DM object
7562a1b0c543SToby Isaac - label - label with the flags
7563df0b854cSToby Isaac 
7564df0b854cSToby Isaac   Output parameters:
75650d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7566df0b854cSToby Isaac 
7567df0b854cSToby Isaac   Level: intermediate
75680d1cd5e0SMatthew G. Knepley 
75690d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7570df0b854cSToby Isaac @*/
75710d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7572df0b854cSToby Isaac {
7573df0b854cSToby Isaac   PetscErrorCode ierr;
7574df0b854cSToby Isaac 
7575df0b854cSToby Isaac   PetscFunctionBegin;
7576df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7577a1b0c543SToby Isaac   PetscValidPointer(label,2);
75780d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
75790d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
75806f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
75810d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
75820d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
75830d1cd5e0SMatthew G. Knepley }
75840d1cd5e0SMatthew G. Knepley 
75850d1cd5e0SMatthew G. Knepley /*@C
75860d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
75870d1cd5e0SMatthew G. Knepley 
75880d1cd5e0SMatthew G. Knepley   Input Parameters:
75890d1cd5e0SMatthew G. Knepley + dm - The DM object
75900d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
75916f25b0d8SLisandro 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_".
75920d1cd5e0SMatthew G. Knepley 
75930d1cd5e0SMatthew G. Knepley   Output Parameter:
75940d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
75950d1cd5e0SMatthew G. Knepley 
75960d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
75970d1cd5e0SMatthew G. Knepley 
75980d1cd5e0SMatthew G. Knepley   Level: advanced
75990d1cd5e0SMatthew G. Knepley 
76000d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
76010d1cd5e0SMatthew G. Knepley @*/
76020d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
76030d1cd5e0SMatthew G. Knepley {
76040d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
76050d1cd5e0SMatthew G. Knepley 
76060d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
76070d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76080d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
76090d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
76100d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
76116f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
76126f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
76130d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7614df0b854cSToby Isaac   PetscFunctionReturn(0);
7615df0b854cSToby Isaac }
7616c4088d22SMatthew G. Knepley 
7617502a2867SDave May /*@C
7618502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7619502a2867SDave May 
7620502a2867SDave May  Not Collective
7621502a2867SDave May 
7622502a2867SDave May  Input Parameter:
7623502a2867SDave May  . dm    - The DM
7624502a2867SDave May 
7625502a2867SDave May  Output Parameter:
7626502a2867SDave May  . nranks - the number of neighbours
7627502a2867SDave May  . ranks - the neighbors ranks
7628502a2867SDave May 
7629502a2867SDave May  Notes:
7630502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
7631502a2867SDave May 
7632502a2867SDave May  Level: beginner
7633502a2867SDave May 
7634dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
7635502a2867SDave May @*/
7636502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7637502a2867SDave May {
7638502a2867SDave May   PetscErrorCode ierr;
7639502a2867SDave May 
7640502a2867SDave May   PetscFunctionBegin;
7641502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76420918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7643502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7644502a2867SDave May   PetscFunctionReturn(0);
7645502a2867SDave May }
7646502a2867SDave May 
7647531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7648531c7667SBarry Smith 
7649531c7667SBarry Smith /*
7650531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7651531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
7652531c7667SBarry Smith */
7653531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7654531c7667SBarry Smith {
7655531c7667SBarry Smith   PetscErrorCode ierr;
7656531c7667SBarry Smith 
7657531c7667SBarry Smith   PetscFunctionBegin;
7658531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7659531c7667SBarry Smith     Vec x1local;
7660531c7667SBarry Smith     DM  dm;
7661531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7662531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7663531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7664531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7665531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7666531c7667SBarry Smith     x1   = x1local;
7667531c7667SBarry Smith   }
7668531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7669531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7670531c7667SBarry Smith     DM  dm;
7671531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7672531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7673531c7667SBarry Smith   }
7674531c7667SBarry Smith   PetscFunctionReturn(0);
7675531c7667SBarry Smith }
7676531c7667SBarry Smith 
7677531c7667SBarry Smith /*@
7678531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7679531c7667SBarry Smith 
7680531c7667SBarry Smith     Input Parameter:
7681531c7667SBarry Smith .    coloring - the MatFDColoring object
7682531c7667SBarry Smith 
768395452b02SPatrick Sanan     Developer Notes:
768495452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
7685531c7667SBarry Smith 
76861b266c99SBarry Smith     Level: advanced
76871b266c99SBarry Smith 
7688531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7689531c7667SBarry Smith @*/
7690531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7691531c7667SBarry Smith {
7692531c7667SBarry Smith   PetscFunctionBegin;
7693531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7694531c7667SBarry Smith   PetscFunctionReturn(0);
7695531c7667SBarry Smith }
76968320bc6fSPatrick Sanan 
76978320bc6fSPatrick Sanan /*@
76988320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
76998320bc6fSPatrick Sanan 
77008320bc6fSPatrick Sanan     Collective
77018320bc6fSPatrick Sanan 
77028320bc6fSPatrick Sanan     Input Parameters:
77038320bc6fSPatrick Sanan +    dm - the first DM
77048320bc6fSPatrick Sanan -    dm2 - the second DM
77058320bc6fSPatrick Sanan 
77068320bc6fSPatrick Sanan     Output Parameters:
77078320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
77088320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
77098320bc6fSPatrick Sanan 
77108320bc6fSPatrick Sanan     Notes:
77118320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
77123d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
77138320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
77143d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
77153d862458SPatrick Sanan     decomposition, but hold different data.
77168320bc6fSPatrick Sanan 
77178320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
77188320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
77198320bc6fSPatrick Sanan 
77208320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
77218320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
77228320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
77238320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
77248320bc6fSPatrick Sanan 
77258320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
77268320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
77278320bc6fSPatrick Sanan .vb
77288320bc6fSPatrick Sanan   ...
77298320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
77308320bc6fSPatrick Sanan   if (set && compatible)  {
77318320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
77328320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
77333d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
77348320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
77358320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
77368320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
77378320bc6fSPatrick Sanan       }
77388320bc6fSPatrick Sanan     }
77398320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
77408320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
77418320bc6fSPatrick Sanan   } else {
77428320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
77438320bc6fSPatrick Sanan   }
77448320bc6fSPatrick Sanan   ...
77458320bc6fSPatrick Sanan .ve
77468320bc6fSPatrick Sanan 
77478320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
77488320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
77498320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
77508320bc6fSPatrick Sanan     always check the "set" output parameter.
77518320bc6fSPatrick Sanan 
77528320bc6fSPatrick Sanan     A DM is always compatible with itself.
77538320bc6fSPatrick Sanan 
77548320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
77558320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
77568320bc6fSPatrick Sanan     incompatible.
77578320bc6fSPatrick Sanan 
77588320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
77598320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
77608320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
77618320bc6fSPatrick Sanan 
77628320bc6fSPatrick Sanan     Developer Notes:
77633d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
77643d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
77658320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
77668320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
77678320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
77683d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
77693d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
77708320bc6fSPatrick Sanan 
77718320bc6fSPatrick Sanan     Level: advanced
77728320bc6fSPatrick Sanan 
77733d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
77748320bc6fSPatrick Sanan @*/
77758320bc6fSPatrick Sanan 
77768320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
77778320bc6fSPatrick Sanan {
77788320bc6fSPatrick Sanan   PetscErrorCode ierr;
77798320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
77808320bc6fSPatrick Sanan   DMType         type,type2;
77818320bc6fSPatrick Sanan   PetscBool      sameType;
77828320bc6fSPatrick Sanan 
77838320bc6fSPatrick Sanan   PetscFunctionBegin;
77848320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77858320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
77868320bc6fSPatrick Sanan 
77878320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
77888320bc6fSPatrick Sanan   if (dm == dm2) {
77898320bc6fSPatrick Sanan     *set = PETSC_TRUE;
77908320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
77918320bc6fSPatrick Sanan     PetscFunctionReturn(0);
77928320bc6fSPatrick Sanan   }
77938320bc6fSPatrick Sanan 
77948320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
77958320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
77968320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
77978320bc6fSPatrick Sanan      determined by the implementation-specific logic */
77988320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
77998320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
78008320bc6fSPatrick Sanan     *set = PETSC_TRUE;
78018320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
78028320bc6fSPatrick Sanan     PetscFunctionReturn(0);
78038320bc6fSPatrick Sanan   }
78048320bc6fSPatrick Sanan 
78058320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
78068320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
78078320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
78088320bc6fSPatrick Sanan     if (*set) {
78098320bc6fSPatrick Sanan       PetscFunctionReturn(0);
78108320bc6fSPatrick Sanan     }
78118320bc6fSPatrick Sanan   }
78128320bc6fSPatrick Sanan 
78138320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
78148320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
78158320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
78168320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
78178320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
78188320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
78198320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
78208320bc6fSPatrick Sanan   } else {
78218320bc6fSPatrick Sanan     *set = PETSC_FALSE;
78228320bc6fSPatrick Sanan   }
78238320bc6fSPatrick Sanan   PetscFunctionReturn(0);
78248320bc6fSPatrick Sanan }
7825