xref: /petsc/src/dm/interface/dm.c (revision 88bdff64cb7d92bd2b7e4dc3355b210b6b0de139)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
30c312b8eSJed Brown #include <petscsf.h>
42764a2aaSMatthew G. Knepley #include <petscds.h>
547c6ae99SBarry Smith 
6732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
7cea54e9dSPatrick Farrell PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_CreateInterpolation;
867a56275SMatthew G Knepley 
9bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
10bff4a2f0SMatthew G. Knepley 
1147c6ae99SBarry Smith #undef __FUNCT__
12a4121054SBarry Smith #define __FUNCT__ "DMCreate"
13a4121054SBarry Smith /*@
14de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
15a4121054SBarry Smith 
16a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
17a4121054SBarry Smith    error when you try to use the vector.
18a4121054SBarry Smith 
19a4121054SBarry Smith   Collective on MPI_Comm
20a4121054SBarry Smith 
21a4121054SBarry Smith   Input Parameter:
22a4121054SBarry Smith . comm - The communicator for the DM object
23a4121054SBarry Smith 
24a4121054SBarry Smith   Output Parameter:
25a4121054SBarry Smith . dm - The DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Level: beginner
28a4121054SBarry Smith 
298472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
30a4121054SBarry Smith @*/
317087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
32a4121054SBarry Smith {
33a4121054SBarry Smith   DM             v;
34a4121054SBarry Smith   PetscErrorCode ierr;
35a4121054SBarry Smith 
36a4121054SBarry Smith   PetscFunctionBegin;
371411c6eeSJed Brown   PetscValidPointer(dm,2);
380298fd71SBarry Smith   *dm = NULL;
398b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
42607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
43a4121054SBarry Smith 
4473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
45e7c4fc90SDmitry Karpeev 
460298fd71SBarry Smith   v->ltogmap                  = NULL;
471411c6eeSJed Brown   v->bs                       = 1;
48171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
4988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
51c58f1c22SToby Isaac   v->labels                   = NULL;
52c58f1c22SToby Isaac   v->depthLabel               = NULL;
530298fd71SBarry Smith   v->defaultSection           = NULL;
540298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
55fba222abSToby Isaac   v->defaultConstraintSection = NULL;
56fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
57c6b900c6SMatthew G. Knepley   v->L                        = NULL;
58c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
595dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
609a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
61435a35e8SMatthew G Knepley   {
62435a35e8SMatthew G Knepley     PetscInt i;
63435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
640298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
65435a35e8SMatthew G Knepley     }
66435a35e8SMatthew G Knepley   }
672764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6814f150ffSMatthew G. Knepley   v->dmBC = NULL;
69a8fb8f29SToby Isaac   v->coarseMesh = NULL;
70f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
71cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
72c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
73b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
74c58f1c22SToby Isaac   ierr = PetscCalloc1(1,&(v->labels));CHKERRQ(ierr);
75c58f1c22SToby Isaac   v->labels->refct = 1;
761411c6eeSJed Brown   *dm = v;
77a4121054SBarry Smith   PetscFunctionReturn(0);
78a4121054SBarry Smith }
79a4121054SBarry Smith 
80a4121054SBarry Smith #undef __FUNCT__
8138221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
8238221697SMatthew G. Knepley /*@
8338221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
8438221697SMatthew G. Knepley 
8538221697SMatthew G. Knepley   Collective on MPI_Comm
8638221697SMatthew G. Knepley 
8738221697SMatthew G. Knepley   Input Parameter:
8838221697SMatthew G. Knepley . dm - The original DM object
8938221697SMatthew G. Knepley 
9038221697SMatthew G. Knepley   Output Parameter:
9138221697SMatthew G. Knepley . newdm  - The new DM object
9238221697SMatthew G. Knepley 
9338221697SMatthew G. Knepley   Level: beginner
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley .keywords: DM, topology, create
9638221697SMatthew G. Knepley @*/
9738221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9838221697SMatthew G. Knepley {
9938221697SMatthew G. Knepley   PetscSF        sf;
10038221697SMatthew G. Knepley   Vec            coords;
10138221697SMatthew G. Knepley   void          *ctx;
1021de53e9aSMatthew G. Knepley   PetscInt       dim;
10338221697SMatthew G. Knepley   PetscErrorCode ierr;
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley   PetscFunctionBegin;
10638221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10738221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10838221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
109c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
110c58f1c22SToby Isaac   dm->labels->refct++;
111c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
112c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1131de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1141de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
11538221697SMatthew G. Knepley   if (dm->ops->clone) {
11638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
11738221697SMatthew G. Knepley   }
118cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
11938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
12038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
12138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
12238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
12338221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
12438221697SMatthew G. Knepley   if (coords) {
12538221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
12638221697SMatthew G. Knepley   } else {
12738221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
12838221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
12938221697SMatthew G. Knepley   }
130c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
131c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1325dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1335dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1345dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
135c6b900c6SMatthew G. Knepley   }
13638221697SMatthew G. Knepley   PetscFunctionReturn(0);
13738221697SMatthew G. Knepley }
13838221697SMatthew G. Knepley 
13938221697SMatthew G. Knepley #undef __FUNCT__
1409a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1419a42bb27SBarry Smith /*@C
142564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1439a42bb27SBarry Smith 
1448472ad0fSDave May    Logically Collective on DM
1459a42bb27SBarry Smith 
1469a42bb27SBarry Smith    Input Parameter:
1479a42bb27SBarry Smith +  da - initial distributed array
1488154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1499a42bb27SBarry Smith 
1509a42bb27SBarry Smith    Options Database:
151dd85299cSBarry Smith .   -dm_vec_type ctype
1529a42bb27SBarry Smith 
1539a42bb27SBarry Smith    Level: intermediate
1549a42bb27SBarry Smith 
1558472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1569a42bb27SBarry Smith @*/
15719fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1589a42bb27SBarry Smith {
1599a42bb27SBarry Smith   PetscErrorCode ierr;
1609a42bb27SBarry Smith 
1619a42bb27SBarry Smith   PetscFunctionBegin;
1629a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1639a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
16419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1659a42bb27SBarry Smith   PetscFunctionReturn(0);
1669a42bb27SBarry Smith }
1679a42bb27SBarry Smith 
1689a42bb27SBarry Smith #undef __FUNCT__
169c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
170c0dedaeaSBarry Smith /*@C
171c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
172c0dedaeaSBarry Smith 
1738472ad0fSDave May    Logically Collective on DM
174c0dedaeaSBarry Smith 
175c0dedaeaSBarry Smith    Input Parameter:
176c0dedaeaSBarry Smith .  da - initial distributed array
177c0dedaeaSBarry Smith 
178c0dedaeaSBarry Smith    Output Parameter:
179c0dedaeaSBarry Smith .  ctype - the vector type
180c0dedaeaSBarry Smith 
181c0dedaeaSBarry Smith    Level: intermediate
182c0dedaeaSBarry Smith 
1838472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
184c0dedaeaSBarry Smith @*/
185c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
186c0dedaeaSBarry Smith {
187c0dedaeaSBarry Smith   PetscFunctionBegin;
188c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
189c0dedaeaSBarry Smith   *ctype = da->vectype;
190c0dedaeaSBarry Smith   PetscFunctionReturn(0);
191c0dedaeaSBarry Smith }
192c0dedaeaSBarry Smith 
193c0dedaeaSBarry Smith #undef __FUNCT__
1945f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1955f1ad066SMatthew G Knepley /*@
19634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1975f1ad066SMatthew G Knepley 
1985f1ad066SMatthew G Knepley   Not collective
1995f1ad066SMatthew G Knepley 
2005f1ad066SMatthew G Knepley   Input Parameter:
2015f1ad066SMatthew G Knepley . v - The Vec
2025f1ad066SMatthew G Knepley 
2035f1ad066SMatthew G Knepley   Output Parameter:
2045f1ad066SMatthew G Knepley . dm - The DM
2055f1ad066SMatthew G Knepley 
2065f1ad066SMatthew G Knepley   Level: intermediate
2075f1ad066SMatthew G Knepley 
2085f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2095f1ad066SMatthew G Knepley @*/
2105f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2115f1ad066SMatthew G Knepley {
2125f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2135f1ad066SMatthew G Knepley 
2145f1ad066SMatthew G Knepley   PetscFunctionBegin;
2155f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2165f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2175f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2185f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2195f1ad066SMatthew G Knepley }
2205f1ad066SMatthew G Knepley 
2215f1ad066SMatthew G Knepley #undef __FUNCT__
2225f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2235f1ad066SMatthew G Knepley /*@
224d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2255f1ad066SMatthew G Knepley 
2265f1ad066SMatthew G Knepley   Not collective
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Input Parameters:
2295f1ad066SMatthew G Knepley + v - The Vec
2305f1ad066SMatthew G Knepley - dm - The DM
2315f1ad066SMatthew G Knepley 
232d9805387SMatthew 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.
233d9805387SMatthew G. Knepley 
2345f1ad066SMatthew G Knepley   Level: intermediate
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2375f1ad066SMatthew G Knepley @*/
2385f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2395f1ad066SMatthew G Knepley {
2405f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2415f1ad066SMatthew G Knepley 
2425f1ad066SMatthew G Knepley   PetscFunctionBegin;
2435f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
244d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2455f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2465f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2475f1ad066SMatthew G Knepley }
2485f1ad066SMatthew G Knepley 
2495f1ad066SMatthew G Knepley #undef __FUNCT__
250521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
251521d9a4cSLisandro Dalcin /*@C
252521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
253521d9a4cSLisandro Dalcin 
254521d9a4cSLisandro Dalcin    Logically Collective on DM
255521d9a4cSLisandro Dalcin 
256521d9a4cSLisandro Dalcin    Input Parameter:
257521d9a4cSLisandro Dalcin +  dm - the DM context
258521d9a4cSLisandro Dalcin .  ctype - the matrix type
259521d9a4cSLisandro Dalcin 
260521d9a4cSLisandro Dalcin    Options Database:
261521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
262521d9a4cSLisandro Dalcin 
263521d9a4cSLisandro Dalcin    Level: intermediate
264521d9a4cSLisandro Dalcin 
265c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
266521d9a4cSLisandro Dalcin @*/
26719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
268521d9a4cSLisandro Dalcin {
269521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27088f0584fSBarry Smith 
271521d9a4cSLisandro Dalcin   PetscFunctionBegin;
272521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
273521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
27419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
275521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
276521d9a4cSLisandro Dalcin }
277521d9a4cSLisandro Dalcin 
278521d9a4cSLisandro Dalcin #undef __FUNCT__
279c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
280c0dedaeaSBarry Smith /*@C
281c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
282c0dedaeaSBarry Smith 
283c0dedaeaSBarry Smith    Logically Collective on DM
284c0dedaeaSBarry Smith 
285c0dedaeaSBarry Smith    Input Parameter:
286c0dedaeaSBarry Smith .  dm - the DM context
287c0dedaeaSBarry Smith 
288c0dedaeaSBarry Smith    Output Parameter:
289c0dedaeaSBarry Smith .  ctype - the matrix type
290c0dedaeaSBarry Smith 
291c0dedaeaSBarry Smith    Options Database:
292c0dedaeaSBarry Smith .   -dm_mat_type ctype
293c0dedaeaSBarry Smith 
294c0dedaeaSBarry Smith    Level: intermediate
295c0dedaeaSBarry Smith 
296c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
297c0dedaeaSBarry Smith @*/
298c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
299c0dedaeaSBarry Smith {
300c0dedaeaSBarry Smith   PetscFunctionBegin;
301c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
302c0dedaeaSBarry Smith   *ctype = dm->mattype;
303c0dedaeaSBarry Smith   PetscFunctionReturn(0);
304c0dedaeaSBarry Smith }
305c0dedaeaSBarry Smith 
306c0dedaeaSBarry Smith #undef __FUNCT__
307c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
308c688c046SMatthew G Knepley /*@
30934f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
310c688c046SMatthew G Knepley 
311c688c046SMatthew G Knepley   Not collective
312c688c046SMatthew G Knepley 
313c688c046SMatthew G Knepley   Input Parameter:
314c688c046SMatthew G Knepley . A - The Mat
315c688c046SMatthew G Knepley 
316c688c046SMatthew G Knepley   Output Parameter:
317c688c046SMatthew G Knepley . dm - The DM
318c688c046SMatthew G Knepley 
319c688c046SMatthew G Knepley   Level: intermediate
320c688c046SMatthew G Knepley 
321c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
322c688c046SMatthew G Knepley @*/
323c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
324c688c046SMatthew G Knepley {
325c688c046SMatthew G Knepley   PetscErrorCode ierr;
326c688c046SMatthew G Knepley 
327c688c046SMatthew G Knepley   PetscFunctionBegin;
328c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
329c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
330c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
331c688c046SMatthew G Knepley   PetscFunctionReturn(0);
332c688c046SMatthew G Knepley }
333c688c046SMatthew G Knepley 
334c688c046SMatthew G Knepley #undef __FUNCT__
335c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
336c688c046SMatthew G Knepley /*@
337c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
338c688c046SMatthew G Knepley 
339c688c046SMatthew G Knepley   Not collective
340c688c046SMatthew G Knepley 
341c688c046SMatthew G Knepley   Input Parameters:
342c688c046SMatthew G Knepley + A - The Mat
343c688c046SMatthew G Knepley - dm - The DM
344c688c046SMatthew G Knepley 
345c688c046SMatthew G Knepley   Level: intermediate
346c688c046SMatthew G Knepley 
347c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
348c688c046SMatthew G Knepley @*/
349c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
350c688c046SMatthew G Knepley {
351c688c046SMatthew G Knepley   PetscErrorCode ierr;
352c688c046SMatthew G Knepley 
353c688c046SMatthew G Knepley   PetscFunctionBegin;
354c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3558865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
356c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
357c688c046SMatthew G Knepley   PetscFunctionReturn(0);
358c688c046SMatthew G Knepley }
359c688c046SMatthew G Knepley 
360c688c046SMatthew G Knepley #undef __FUNCT__
3619a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3629a42bb27SBarry Smith /*@C
3639a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3646757b960SDave May    DM options in the database.
3659a42bb27SBarry Smith 
3668353ddbbSDave May    Logically Collective on DM
3679a42bb27SBarry Smith 
3689a42bb27SBarry Smith    Input Parameter:
3698353ddbbSDave May +  da - the DM context
3709a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3719a42bb27SBarry Smith 
3729a42bb27SBarry Smith    Notes:
3739a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3749a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3759a42bb27SBarry Smith 
3769a42bb27SBarry Smith    Level: advanced
3779a42bb27SBarry Smith 
3788353ddbbSDave May .keywords: DM, set, options, prefix, database
3799a42bb27SBarry Smith 
3809a42bb27SBarry Smith .seealso: DMSetFromOptions()
3819a42bb27SBarry Smith @*/
3827087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3839a42bb27SBarry Smith {
3849a42bb27SBarry Smith   PetscErrorCode ierr;
3859a42bb27SBarry Smith 
3869a42bb27SBarry Smith   PetscFunctionBegin;
3879a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3889a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
389691be533SLawrence Mitchell   if (dm->sf) {
390691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
391691be533SLawrence Mitchell   }
392691be533SLawrence Mitchell   if (dm->defaultSF) {
393691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
394691be533SLawrence Mitchell   }
3959a42bb27SBarry Smith   PetscFunctionReturn(0);
3969a42bb27SBarry Smith }
3979a42bb27SBarry Smith 
3989a42bb27SBarry Smith #undef __FUNCT__
39931697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
40031697293SDave May /*@C
40131697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
40231697293SDave May    DM options in the database.
40331697293SDave May 
40431697293SDave May    Logically Collective on DM
40531697293SDave May 
40631697293SDave May    Input Parameters:
40731697293SDave May +  dm - the DM context
40831697293SDave May -  prefix - the prefix string to prepend to all DM option requests
40931697293SDave May 
41031697293SDave May    Notes:
41131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
41231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
41331697293SDave May 
41431697293SDave May    Level: advanced
41531697293SDave May 
41631697293SDave May .keywords: DM, append, options, prefix, database
41731697293SDave May 
41831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
41931697293SDave May @*/
42031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
42131697293SDave May {
42231697293SDave May   PetscErrorCode ierr;
42331697293SDave May 
42431697293SDave May   PetscFunctionBegin;
42531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
42731697293SDave May   PetscFunctionReturn(0);
42831697293SDave May }
42931697293SDave May 
43031697293SDave May #undef __FUNCT__
43131697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
43231697293SDave May /*@C
43331697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
43431697293SDave May    DM options in the database.
43531697293SDave May 
43631697293SDave May    Not Collective
43731697293SDave May 
43831697293SDave May    Input Parameters:
43931697293SDave May .  dm - the DM context
44031697293SDave May 
44131697293SDave May    Output Parameters:
44231697293SDave May .  prefix - pointer to the prefix string used is returned
44331697293SDave May 
44431697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
44531697293SDave May    sufficient length to hold the prefix.
44631697293SDave May 
44731697293SDave May    Level: advanced
44831697293SDave May 
44931697293SDave May .keywords: DM, set, options, prefix, database
45031697293SDave May 
45131697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
45231697293SDave May @*/
45331697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
45431697293SDave May {
45531697293SDave May   PetscErrorCode ierr;
45631697293SDave May 
45731697293SDave May   PetscFunctionBegin;
45831697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45931697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
46031697293SDave May   PetscFunctionReturn(0);
46131697293SDave May }
46231697293SDave May 
46331697293SDave May #undef __FUNCT__
464*88bdff64SToby Isaac #define __FUNCT__ "DMCountNonCyclicReferences"
465*88bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
466*88bdff64SToby Isaac {
467*88bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
468*88bdff64SToby Isaac   DMNamedVecLink nlink;
469*88bdff64SToby Isaac   PetscErrorCode ierr;
470*88bdff64SToby Isaac 
471*88bdff64SToby Isaac   PetscFunctionBegin;
472*88bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
473*88bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
474*88bdff64SToby Isaac     if (dm->localin[i])  refct--;
475*88bdff64SToby Isaac     if (dm->globalin[i]) refct--;
476*88bdff64SToby Isaac   }
477*88bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
478*88bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
479*88bdff64SToby Isaac   if (dm->x) {
480*88bdff64SToby Isaac     DM obj;
481*88bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
482*88bdff64SToby Isaac     if (obj == dm) refct--;
483*88bdff64SToby Isaac   }
484*88bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
485*88bdff64SToby Isaac     refct--;
486*88bdff64SToby Isaac     if (recurseCoarse) {
487*88bdff64SToby Isaac       PetscInt coarseCount;
488*88bdff64SToby Isaac 
489*88bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
490*88bdff64SToby Isaac       refct += coarseCount;
491*88bdff64SToby Isaac     }
492*88bdff64SToby Isaac   }
493*88bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
494*88bdff64SToby Isaac     refct--;
495*88bdff64SToby Isaac     if (recurseFine) {
496*88bdff64SToby Isaac       PetscInt fineCount;
497*88bdff64SToby Isaac 
498*88bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
499*88bdff64SToby Isaac       refct += fineCount;
500*88bdff64SToby Isaac     }
501*88bdff64SToby Isaac   }
502*88bdff64SToby Isaac   *ncrefct = refct;
503*88bdff64SToby Isaac   PetscFunctionReturn(0);
504*88bdff64SToby Isaac }
505*88bdff64SToby Isaac 
506*88bdff64SToby Isaac #undef __FUNCT__
50747c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
50847c6ae99SBarry Smith /*@
5098472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
51047c6ae99SBarry Smith 
51147c6ae99SBarry Smith     Collective on DM
51247c6ae99SBarry Smith 
51347c6ae99SBarry Smith     Input Parameter:
51447c6ae99SBarry Smith .   dm - the DM object to destroy
51547c6ae99SBarry Smith 
51647c6ae99SBarry Smith     Level: developer
51747c6ae99SBarry Smith 
518e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith @*/
521fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
52247c6ae99SBarry Smith {
523*88bdff64SToby Isaac   PetscInt       i, cnt;
524dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
52547c6ae99SBarry Smith   PetscErrorCode ierr;
52647c6ae99SBarry Smith 
52747c6ae99SBarry Smith   PetscFunctionBegin;
5286bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5296bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
53087e657c6SBarry Smith 
531*88bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
532*88bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
533*88bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
534*88bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
535732e2eb9SMatthew G Knepley   /*
536732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
537732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
538732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
539732e2eb9SMatthew G Knepley   */
5406bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5416bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
542732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5436bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5446bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
545732e2eb9SMatthew G Knepley   }
546f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5470298fd71SBarry Smith   (*dm)->namedglobal = NULL;
548f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5492348bcf4SPeter Brune     nnext = nlink->next;
5502348bcf4SPeter 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);
5512348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5522348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5532348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5542348bcf4SPeter Brune   }
555f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5560298fd71SBarry Smith   (*dm)->namedlocal = NULL;
557f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
558f490541aSPeter Brune     nnext = nlink->next;
559f490541aSPeter 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);
560f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
561f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
562f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
563f490541aSPeter Brune   }
5642348bcf4SPeter Brune 
565b17ce1afSJed Brown   /* Destroy the list of hooks */
566c833c3b5SJed Brown   {
567c833c3b5SJed Brown     DMCoarsenHookLink link,next;
568b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
569b17ce1afSJed Brown       next = link->next;
570b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
571b17ce1afSJed Brown     }
5720298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
573c833c3b5SJed Brown   }
574c833c3b5SJed Brown   {
575c833c3b5SJed Brown     DMRefineHookLink link,next;
576c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
577c833c3b5SJed Brown       next = link->next;
578c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
579c833c3b5SJed Brown     }
5800298fd71SBarry Smith     (*dm)->refinehook = NULL;
581c833c3b5SJed Brown   }
582be081cd6SPeter Brune   {
583be081cd6SPeter Brune     DMSubDomainHookLink link,next;
584be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
585be081cd6SPeter Brune       next = link->next;
586be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
587be081cd6SPeter Brune     }
5880298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
589be081cd6SPeter Brune   }
590baf369e7SPeter Brune   {
591baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
592baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
593baf369e7SPeter Brune       next = link->next;
594baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
595baf369e7SPeter Brune     }
5960298fd71SBarry Smith     (*dm)->gtolhook = NULL;
597baf369e7SPeter Brune   }
598d4d07f1eSToby Isaac   {
599d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
600d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
601d4d07f1eSToby Isaac       next = link->next;
602d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
603d4d07f1eSToby Isaac     }
604d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
605d4d07f1eSToby Isaac   }
606aa1993deSMatthew G Knepley   /* Destroy the work arrays */
607aa1993deSMatthew G Knepley   {
608aa1993deSMatthew G Knepley     DMWorkLink link,next;
609aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
610aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
611aa1993deSMatthew G Knepley       next = link->next;
612aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
613aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
614aa1993deSMatthew G Knepley     }
6150298fd71SBarry Smith     (*dm)->workin = NULL;
616aa1993deSMatthew G Knepley   }
617c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
618c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
619c58f1c22SToby Isaac 
620c58f1c22SToby Isaac     /* destroy the labels */
621c58f1c22SToby Isaac     while (next) {
622c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
623c58f1c22SToby Isaac 
624c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
625c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
626c58f1c22SToby Isaac       next = tmp;
627c58f1c22SToby Isaac     }
628c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
629c58f1c22SToby Isaac   }
630b17ce1afSJed Brown 
63152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
63252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
63352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
63452536dc3SBarry Smith 
6351a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6361a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6371a266240SBarry Smith   }
63887e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
63971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6404dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6416bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6426bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
643073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
64488ed4aceSMatthew G Knepley 
64588ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
64688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6478b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
648fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
649fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
65088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
65188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6528e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
653af122d2aSMatthew G Knepley 
654*88bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
655*88bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
656*88bdff64SToby Isaac   }
657a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
658*88bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
659*88bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
660*88bdff64SToby Isaac   }
661*88bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
6626636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6636636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6646636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6655dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6666636e97aSMatthew G Knepley 
6672764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
66814f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
669e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
670e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
671732e2eb9SMatthew G Knepley 
6726bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
673435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
674732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
67547c6ae99SBarry Smith   PetscFunctionReturn(0);
67647c6ae99SBarry Smith }
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith #undef __FUNCT__
679d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
680d7bf68aeSBarry Smith /*@
681d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
682d7bf68aeSBarry Smith 
683d7bf68aeSBarry Smith     Collective on DM
684d7bf68aeSBarry Smith 
685d7bf68aeSBarry Smith     Input Parameter:
686d7bf68aeSBarry Smith .   dm - the DM object to setup
687d7bf68aeSBarry Smith 
688d7bf68aeSBarry Smith     Level: developer
689d7bf68aeSBarry Smith 
690e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
691d7bf68aeSBarry Smith 
692d7bf68aeSBarry Smith @*/
6937087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
694d7bf68aeSBarry Smith {
695d7bf68aeSBarry Smith   PetscErrorCode ierr;
696d7bf68aeSBarry Smith 
697d7bf68aeSBarry Smith   PetscFunctionBegin;
698171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6998387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
700d7bf68aeSBarry Smith   if (dm->ops->setup) {
701d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
702d7bf68aeSBarry Smith   }
7038387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
704d7bf68aeSBarry Smith   PetscFunctionReturn(0);
705d7bf68aeSBarry Smith }
706d7bf68aeSBarry Smith 
707d7bf68aeSBarry Smith #undef __FUNCT__
708d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
709d7bf68aeSBarry Smith /*@
710d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
711d7bf68aeSBarry Smith 
712d7bf68aeSBarry Smith     Collective on DM
713d7bf68aeSBarry Smith 
714d7bf68aeSBarry Smith     Input Parameter:
715d7bf68aeSBarry Smith .   dm - the DM object to set options for
716d7bf68aeSBarry Smith 
717732e2eb9SMatthew G Knepley     Options Database:
7184164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7194164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7204164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7214164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
722732e2eb9SMatthew G Knepley 
723d7bf68aeSBarry Smith     Level: developer
724d7bf68aeSBarry Smith 
725e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
726d7bf68aeSBarry Smith 
727d7bf68aeSBarry Smith @*/
7287087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
729d7bf68aeSBarry Smith {
7307781c08eSBarry Smith   char           typeName[256];
731ca266f36SBarry Smith   PetscBool      flg;
732d7bf68aeSBarry Smith   PetscErrorCode ierr;
733d7bf68aeSBarry Smith 
734d7bf68aeSBarry Smith   PetscFunctionBegin;
735171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
736691be533SLawrence Mitchell   if (dm->sf) {
737691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
738691be533SLawrence Mitchell   }
739691be533SLawrence Mitchell   if (dm->defaultSF) {
740691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
741691be533SLawrence Mitchell   }
7423194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7430298fd71SBarry 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);
744a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
745f9ba7244SBarry Smith   if (flg) {
746f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
747f9ba7244SBarry Smith   }
748a264d7a6SBarry 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);
749073dac72SJed Brown   if (flg) {
750521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
751073dac72SJed Brown   }
7520298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
753f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
754e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
755f9ba7244SBarry Smith   }
756f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
757f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
75882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
759d7bf68aeSBarry Smith   PetscFunctionReturn(0);
760d7bf68aeSBarry Smith }
761d7bf68aeSBarry Smith 
762d7bf68aeSBarry Smith #undef __FUNCT__
76347c6ae99SBarry Smith #define __FUNCT__ "DMView"
764fc9bc008SSatish Balay /*@C
765224748a4SBarry Smith     DMView - Views a DM
76647c6ae99SBarry Smith 
76747c6ae99SBarry Smith     Collective on DM
76847c6ae99SBarry Smith 
76947c6ae99SBarry Smith     Input Parameter:
77047c6ae99SBarry Smith +   dm - the DM object to view
77147c6ae99SBarry Smith -   v - the viewer
77247c6ae99SBarry Smith 
773224748a4SBarry Smith     Level: beginner
77447c6ae99SBarry Smith 
775e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
77647c6ae99SBarry Smith 
77747c6ae99SBarry Smith @*/
7787087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
77947c6ae99SBarry Smith {
78047c6ae99SBarry Smith   PetscErrorCode ierr;
78132c0f0efSBarry Smith   PetscBool      isbinary;
78247c6ae99SBarry Smith 
78347c6ae99SBarry Smith   PetscFunctionBegin;
784171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7853014e516SBarry Smith   if (!v) {
786ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
7873014e516SBarry Smith   }
78898c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
78932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
79032c0f0efSBarry Smith   if (isbinary) {
79155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
79232c0f0efSBarry Smith     char     type[256];
79332c0f0efSBarry Smith 
79432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
79532c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
79632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
79732c0f0efSBarry Smith   }
7980c010503SBarry Smith   if (dm->ops->view) {
7990c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
80047c6ae99SBarry Smith   }
80147c6ae99SBarry Smith   PetscFunctionReturn(0);
80247c6ae99SBarry Smith }
80347c6ae99SBarry Smith 
80447c6ae99SBarry Smith #undef __FUNCT__
80547c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
80647c6ae99SBarry Smith /*@
8078472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith     Collective on DM
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith     Input Parameter:
81247c6ae99SBarry Smith .   dm - the DM object
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith     Output Parameter:
81547c6ae99SBarry Smith .   vec - the global vector
81647c6ae99SBarry Smith 
817073dac72SJed Brown     Level: beginner
81847c6ae99SBarry Smith 
819e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith @*/
8227087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
82347c6ae99SBarry Smith {
82447c6ae99SBarry Smith   PetscErrorCode ierr;
82547c6ae99SBarry Smith 
82647c6ae99SBarry Smith   PetscFunctionBegin;
827171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82847c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
82947c6ae99SBarry Smith   PetscFunctionReturn(0);
83047c6ae99SBarry Smith }
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith #undef __FUNCT__
83347c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
83447c6ae99SBarry Smith /*@
8358472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith     Not Collective
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith     Input Parameter:
84047c6ae99SBarry Smith .   dm - the DM object
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith     Output Parameter:
84347c6ae99SBarry Smith .   vec - the local vector
84447c6ae99SBarry Smith 
845073dac72SJed Brown     Level: beginner
84647c6ae99SBarry Smith 
847e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith @*/
8507087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
85147c6ae99SBarry Smith {
85247c6ae99SBarry Smith   PetscErrorCode ierr;
85347c6ae99SBarry Smith 
85447c6ae99SBarry Smith   PetscFunctionBegin;
855171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85647c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
85747c6ae99SBarry Smith   PetscFunctionReturn(0);
85847c6ae99SBarry Smith }
85947c6ae99SBarry Smith 
86047c6ae99SBarry Smith #undef __FUNCT__
8611411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
8621411c6eeSJed Brown /*@
8631411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8641411c6eeSJed Brown 
8651411c6eeSJed Brown    Collective on DM
8661411c6eeSJed Brown 
8671411c6eeSJed Brown    Input Parameter:
8681411c6eeSJed Brown .  dm - the DM that provides the mapping
8691411c6eeSJed Brown 
8701411c6eeSJed Brown    Output Parameter:
8711411c6eeSJed Brown .  ltog - the mapping
8721411c6eeSJed Brown 
8731411c6eeSJed Brown    Level: intermediate
8741411c6eeSJed Brown 
8751411c6eeSJed Brown    Notes:
8761411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8771411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8781411c6eeSJed Brown 
879fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8801411c6eeSJed Brown @*/
8817087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8821411c6eeSJed Brown {
8831411c6eeSJed Brown   PetscErrorCode ierr;
8841411c6eeSJed Brown 
8851411c6eeSJed Brown   PetscFunctionBegin;
8861411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8871411c6eeSJed Brown   PetscValidPointer(ltog,2);
8881411c6eeSJed Brown   if (!dm->ltogmap) {
88937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
89037d0c07bSMatthew G Knepley 
89137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
89237d0c07bSMatthew G Knepley     if (section) {
89337d0c07bSMatthew G Knepley       PetscInt *ltog;
89437d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
89537d0c07bSMatthew G Knepley 
89637d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
89737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
89837d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
899785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
90037d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
90137d0c07bSMatthew G Knepley         PetscInt dof, off, c;
90237d0c07bSMatthew G Knepley 
90337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
90437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
90537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
90637d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
90737d0c07bSMatthew G Knepley           ltog[l] = off+c;
90837d0c07bSMatthew G Knepley         }
90937d0c07bSMatthew G Knepley       }
910f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9113bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
91237d0c07bSMatthew G Knepley     } else {
913184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
914184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9151411c6eeSJed Brown     }
91637d0c07bSMatthew G Knepley   }
9171411c6eeSJed Brown   *ltog = dm->ltogmap;
9181411c6eeSJed Brown   PetscFunctionReturn(0);
9191411c6eeSJed Brown }
9201411c6eeSJed Brown 
9211411c6eeSJed Brown #undef __FUNCT__
9221411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
9231411c6eeSJed Brown /*@
9241411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9251411c6eeSJed Brown 
9261411c6eeSJed Brown    Not Collective
9271411c6eeSJed Brown 
9281411c6eeSJed Brown    Input Parameter:
9291411c6eeSJed Brown .  dm - the DM with block structure
9301411c6eeSJed Brown 
9311411c6eeSJed Brown    Output Parameter:
9321411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9331411c6eeSJed Brown 
9341411c6eeSJed Brown    Level: intermediate
9351411c6eeSJed Brown 
936fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9371411c6eeSJed Brown @*/
9387087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9391411c6eeSJed Brown {
9401411c6eeSJed Brown   PetscFunctionBegin;
9411411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9421411c6eeSJed Brown   PetscValidPointer(bs,2);
9431411c6eeSJed 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");
9441411c6eeSJed Brown   *bs = dm->bs;
9451411c6eeSJed Brown   PetscFunctionReturn(0);
9461411c6eeSJed Brown }
9471411c6eeSJed Brown 
9481411c6eeSJed Brown #undef __FUNCT__
949e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
95047c6ae99SBarry Smith /*@
9518472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith     Collective on DM
95447c6ae99SBarry Smith 
95547c6ae99SBarry Smith     Input Parameter:
95647c6ae99SBarry Smith +   dm1 - the DM object
95747c6ae99SBarry Smith -   dm2 - the second, finer DM object
95847c6ae99SBarry Smith 
95947c6ae99SBarry Smith     Output Parameter:
96047c6ae99SBarry Smith +  mat - the interpolation
96147c6ae99SBarry Smith -  vec - the scaling (optional)
96247c6ae99SBarry Smith 
96347c6ae99SBarry Smith     Level: developer
96447c6ae99SBarry Smith 
96585afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
96685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
967d52bd9f3SBarry Smith 
9681f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
969d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
97085afcc9aSBarry Smith 
97185afcc9aSBarry Smith 
972e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith @*/
975e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
97647c6ae99SBarry Smith {
97747c6ae99SBarry Smith   PetscErrorCode ierr;
97847c6ae99SBarry Smith 
97947c6ae99SBarry Smith   PetscFunctionBegin;
980171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
981171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
982cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
98325296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
984cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
98547c6ae99SBarry Smith   PetscFunctionReturn(0);
98647c6ae99SBarry Smith }
98747c6ae99SBarry Smith 
98847c6ae99SBarry Smith #undef __FUNCT__
989e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
99047c6ae99SBarry Smith /*@
9918472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
99247c6ae99SBarry Smith 
99347c6ae99SBarry Smith     Collective on DM
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Input Parameter:
99647c6ae99SBarry Smith +   dm1 - the DM object
99747c6ae99SBarry Smith -   dm2 - the second, finer DM object
99847c6ae99SBarry Smith 
99947c6ae99SBarry Smith     Output Parameter:
10006dbf9973SLawrence Mitchell .   mat - the injection
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith     Level: developer
100347c6ae99SBarry Smith 
100485afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
100585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
100685afcc9aSBarry Smith 
1007e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
100847c6ae99SBarry Smith 
100947c6ae99SBarry Smith @*/
10106dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
101147c6ae99SBarry Smith {
101247c6ae99SBarry Smith   PetscErrorCode ierr;
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith   PetscFunctionBegin;
1015171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1016171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10176dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
101847c6ae99SBarry Smith   PetscFunctionReturn(0);
101947c6ae99SBarry Smith }
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith #undef __FUNCT__
1022e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
1023b412c318SBarry Smith /*@
1024b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith     Collective on DM
102747c6ae99SBarry Smith 
102847c6ae99SBarry Smith     Input Parameter:
102947c6ae99SBarry Smith +   dm - the DM object
1030b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith     Output Parameter:
103347c6ae99SBarry Smith .   coloring - the coloring
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith     Level: developer
103647c6ae99SBarry Smith 
1037b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
103847c6ae99SBarry Smith 
1039aab9d709SJed Brown @*/
1040b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
104147c6ae99SBarry Smith {
104247c6ae99SBarry Smith   PetscErrorCode ierr;
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith   PetscFunctionBegin;
1045171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1046ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1047b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
104847c6ae99SBarry Smith   PetscFunctionReturn(0);
104947c6ae99SBarry Smith }
105047c6ae99SBarry Smith 
105147c6ae99SBarry Smith #undef __FUNCT__
1052950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1053b412c318SBarry Smith /*@
10548472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
105547c6ae99SBarry Smith 
105647c6ae99SBarry Smith     Collective on DM
105747c6ae99SBarry Smith 
105847c6ae99SBarry Smith     Input Parameter:
1059b412c318SBarry Smith .   dm - the DM object
106047c6ae99SBarry Smith 
106147c6ae99SBarry Smith     Output Parameter:
106247c6ae99SBarry Smith .   mat - the empty Jacobian
106347c6ae99SBarry Smith 
1064073dac72SJed Brown     Level: beginner
106547c6ae99SBarry Smith 
106694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
106794013140SBarry Smith        do not need to do it yourself.
106894013140SBarry Smith 
106994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1070aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
107194013140SBarry Smith 
107294013140SBarry 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
107394013140SBarry Smith        internally by PETSc.
107494013140SBarry Smith 
107594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1076aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
107794013140SBarry Smith 
1078b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
107947c6ae99SBarry Smith 
1080aab9d709SJed Brown @*/
1081b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
108247c6ae99SBarry Smith {
108347c6ae99SBarry Smith   PetscErrorCode ierr;
108447c6ae99SBarry Smith 
108547c6ae99SBarry Smith   PetscFunctionBegin;
1086171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1087607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1088c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1089c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1090b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
109147c6ae99SBarry Smith   PetscFunctionReturn(0);
109247c6ae99SBarry Smith }
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith #undef __FUNCT__
1095732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1096732e2eb9SMatthew G Knepley /*@
1097950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1098732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1099732e2eb9SMatthew G Knepley 
11008472ad0fSDave May   Logically Collective on DM
1101732e2eb9SMatthew G Knepley 
1102732e2eb9SMatthew G Knepley   Input Parameter:
1103732e2eb9SMatthew G Knepley + dm - the DM
1104732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1105732e2eb9SMatthew G Knepley 
1106732e2eb9SMatthew G Knepley   Level: developer
1107950540a4SJed Brown .seealso DMCreateMatrix()
1108732e2eb9SMatthew G Knepley @*/
1109732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1110732e2eb9SMatthew G Knepley {
1111732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1112732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1113732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1114732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1115732e2eb9SMatthew G Knepley }
1116732e2eb9SMatthew G Knepley 
1117732e2eb9SMatthew G Knepley #undef __FUNCT__
1118a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1119a89ea682SMatthew G Knepley /*@C
1120aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1121a89ea682SMatthew G Knepley 
1122a89ea682SMatthew G Knepley   Not Collective
1123a89ea682SMatthew G Knepley 
1124a89ea682SMatthew G Knepley   Input Parameters:
1125a89ea682SMatthew G Knepley + dm - the DM object
1126aa1993deSMatthew G Knepley . count - The minium size
1127aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1128a89ea682SMatthew G Knepley 
1129a89ea682SMatthew G Knepley   Output Parameter:
1130a89ea682SMatthew G Knepley . array - the work array
1131a89ea682SMatthew G Knepley 
1132a89ea682SMatthew G Knepley   Level: developer
1133a89ea682SMatthew G Knepley 
1134a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1135a89ea682SMatthew G Knepley @*/
1136aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1137a89ea682SMatthew G Knepley {
1138a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1139aa1993deSMatthew G Knepley   DMWorkLink     link;
1140854ce69bSBarry Smith   size_t         dsize;
1141a89ea682SMatthew G Knepley 
1142a89ea682SMatthew G Knepley   PetscFunctionBegin;
1143a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1144aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1145aa1993deSMatthew G Knepley   if (dm->workin) {
1146aa1993deSMatthew G Knepley     link       = dm->workin;
1147aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1148aa1993deSMatthew G Knepley   } else {
1149b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1150a89ea682SMatthew G Knepley   }
1151854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1152854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1153aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1154854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1155854ce69bSBarry Smith     link->bytes = dsize*count;
1156aa1993deSMatthew G Knepley   }
1157aa1993deSMatthew G Knepley   link->next   = dm->workout;
1158aa1993deSMatthew G Knepley   dm->workout  = link;
1159aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1160a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1161a89ea682SMatthew G Knepley }
1162a89ea682SMatthew G Knepley 
1163aa1993deSMatthew G Knepley #undef __FUNCT__
1164aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1165aa1993deSMatthew G Knepley /*@C
1166aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1167aa1993deSMatthew G Knepley 
1168aa1993deSMatthew G Knepley   Not Collective
1169aa1993deSMatthew G Knepley 
1170aa1993deSMatthew G Knepley   Input Parameters:
1171aa1993deSMatthew G Knepley + dm - the DM object
1172aa1993deSMatthew G Knepley . count - The minium size
1173aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1174aa1993deSMatthew G Knepley 
1175aa1993deSMatthew G Knepley   Output Parameter:
1176aa1993deSMatthew G Knepley . array - the work array
1177aa1993deSMatthew G Knepley 
1178aa1993deSMatthew G Knepley   Level: developer
1179aa1993deSMatthew G Knepley 
1180aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1181aa1993deSMatthew G Knepley @*/
1182aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1183aa1993deSMatthew G Knepley {
1184aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1185aa1993deSMatthew G Knepley 
1186aa1993deSMatthew G Knepley   PetscFunctionBegin;
1187aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1188aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1189aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1190aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1191aa1993deSMatthew G Knepley       *p           = link->next;
1192aa1993deSMatthew G Knepley       link->next   = dm->workin;
1193aa1993deSMatthew G Knepley       dm->workin   = link;
11940298fd71SBarry Smith       *(void**)mem = NULL;
1195aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1196aa1993deSMatthew G Knepley     }
1197aa1993deSMatthew G Knepley   }
1198aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1199aa1993deSMatthew G Knepley }
1200e7c4fc90SDmitry Karpeev 
1201e7c4fc90SDmitry Karpeev #undef __FUNCT__
1202435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1203435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1204435a35e8SMatthew G Knepley {
1205435a35e8SMatthew G Knepley   PetscFunctionBegin;
1206435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
120782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1208435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1209435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1210435a35e8SMatthew G Knepley }
1211435a35e8SMatthew G Knepley 
1212435a35e8SMatthew G Knepley #undef __FUNCT__
12134d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
12144f3b5142SJed Brown /*@C
12154d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12164d343eeaSMatthew G Knepley 
12174d343eeaSMatthew G Knepley   Not collective
12184d343eeaSMatthew G Knepley 
12194d343eeaSMatthew G Knepley   Input Parameter:
12204d343eeaSMatthew G Knepley . dm - the DM object
12214d343eeaSMatthew G Knepley 
12224d343eeaSMatthew G Knepley   Output Parameters:
12230298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
12240298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
12250298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
12264d343eeaSMatthew G Knepley 
12274d343eeaSMatthew G Knepley   Level: intermediate
12284d343eeaSMatthew G Knepley 
122921c9b008SJed Brown   Notes:
123021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
123121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
123221c9b008SJed Brown   PetscFree().
123321c9b008SJed Brown 
12344d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
12354d343eeaSMatthew G Knepley @*/
123637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
12374d343eeaSMatthew G Knepley {
123837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
12394d343eeaSMatthew G Knepley   PetscErrorCode ierr;
12404d343eeaSMatthew G Knepley 
12414d343eeaSMatthew G Knepley   PetscFunctionBegin;
12424d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
124369ca1f37SDmitry Karpeev   if (numFields) {
124469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
124569ca1f37SDmitry Karpeev     *numFields = 0;
124669ca1f37SDmitry Karpeev   }
124737d0c07bSMatthew G Knepley   if (fieldNames) {
124837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
12490298fd71SBarry Smith     *fieldNames = NULL;
125069ca1f37SDmitry Karpeev   }
125169ca1f37SDmitry Karpeev   if (fields) {
125269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
12530298fd71SBarry Smith     *fields = NULL;
125469ca1f37SDmitry Karpeev   }
125537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
125637d0c07bSMatthew G Knepley   if (section) {
125737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
125837d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
125937d0c07bSMatthew G Knepley 
126037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
126137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1262dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
126337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
126437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
126537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
126637d0c07bSMatthew G Knepley     }
126737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
126837d0c07bSMatthew G Knepley       PetscInt gdof;
126937d0c07bSMatthew G Knepley 
127037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
127137d0c07bSMatthew G Knepley       if (gdof > 0) {
127237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
127337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
127437d0c07bSMatthew G Knepley 
127537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
127637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
127737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
127837d0c07bSMatthew G Knepley         }
127937d0c07bSMatthew G Knepley       }
128037d0c07bSMatthew G Knepley     }
128137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1282785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
128337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
128437d0c07bSMatthew G Knepley     }
128537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
128637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
128737d0c07bSMatthew G Knepley 
128837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
128937d0c07bSMatthew G Knepley       if (gdof > 0) {
129037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
129137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
129237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
129337d0c07bSMatthew G Knepley 
129437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
129537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
129637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
129737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
129837d0c07bSMatthew G Knepley           }
129937d0c07bSMatthew G Knepley         }
130037d0c07bSMatthew G Knepley       }
130137d0c07bSMatthew G Knepley     }
13028865f1eaSKarl Rupp     if (numFields) *numFields = nF;
130337d0c07bSMatthew G Knepley     if (fieldNames) {
1304785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
130537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
130637d0c07bSMatthew G Knepley         const char *fieldName;
130737d0c07bSMatthew G Knepley 
130837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
130937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
131037d0c07bSMatthew G Knepley       }
131137d0c07bSMatthew G Knepley     }
131237d0c07bSMatthew G Knepley     if (fields) {
1313785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
131437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
131582f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
131637d0c07bSMatthew G Knepley       }
131737d0c07bSMatthew G Knepley     }
131837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13198865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
13208865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
132169ca1f37SDmitry Karpeev   }
13224d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
13234d343eeaSMatthew G Knepley }
13244d343eeaSMatthew G Knepley 
132516621825SDmitry Karpeev 
132616621825SDmitry Karpeev #undef __FUNCT__
132716621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
132816621825SDmitry Karpeev /*@C
132916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
133016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
133116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1332e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1333e7c4fc90SDmitry Karpeev 
1334e7c4fc90SDmitry Karpeev   Not collective
1335e7c4fc90SDmitry Karpeev 
1336e7c4fc90SDmitry Karpeev   Input Parameter:
1337e7c4fc90SDmitry Karpeev . dm - the DM object
1338e7c4fc90SDmitry Karpeev 
1339e7c4fc90SDmitry Karpeev   Output Parameters:
13400298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
13410298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
13420298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
13430298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1344e7c4fc90SDmitry Karpeev 
1345e7c4fc90SDmitry Karpeev   Level: intermediate
1346e7c4fc90SDmitry Karpeev 
1347e7c4fc90SDmitry Karpeev   Notes:
1348e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1349e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1350e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1351e7c4fc90SDmitry Karpeev 
1352e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1353e7c4fc90SDmitry Karpeev @*/
135416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1355e7c4fc90SDmitry Karpeev {
1356e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1357e7c4fc90SDmitry Karpeev 
1358e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1359e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13608865f1eaSKarl Rupp   if (len) {
13618865f1eaSKarl Rupp     PetscValidPointer(len,2);
13628865f1eaSKarl Rupp     *len = 0;
13638865f1eaSKarl Rupp   }
13648865f1eaSKarl Rupp   if (namelist) {
13658865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
13668865f1eaSKarl Rupp     *namelist = 0;
13678865f1eaSKarl Rupp   }
13688865f1eaSKarl Rupp   if (islist) {
13698865f1eaSKarl Rupp     PetscValidPointer(islist,4);
13708865f1eaSKarl Rupp     *islist = 0;
13718865f1eaSKarl Rupp   }
13728865f1eaSKarl Rupp   if (dmlist) {
13738865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
13748865f1eaSKarl Rupp     *dmlist = 0;
13758865f1eaSKarl Rupp   }
1376f3f0edfdSDmitry Karpeev   /*
1377f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1378f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1379f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1380f3f0edfdSDmitry Karpeev    */
1381ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
138216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1383435a35e8SMatthew G Knepley     PetscSection section;
1384435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1385435a35e8SMatthew G Knepley 
1386435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1387435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1388435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1389435a35e8SMatthew G Knepley       *len = numFields;
139003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
139103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
139203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1393435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1394435a35e8SMatthew G Knepley         const char *fieldName;
1395435a35e8SMatthew G Knepley 
139603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
139703dc3394SMatthew G. Knepley         if (namelist) {
1398435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1399435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1400435a35e8SMatthew G Knepley         }
140103dc3394SMatthew G. Knepley       }
1402435a35e8SMatthew G Knepley     } else {
140369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1404e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14050298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1406e7c4fc90SDmitry Karpeev     }
14078865f1eaSKarl Rupp   } else {
140816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
140916621825SDmitry Karpeev   }
141016621825SDmitry Karpeev   PetscFunctionReturn(0);
141116621825SDmitry Karpeev }
141216621825SDmitry Karpeev 
141316621825SDmitry Karpeev #undef __FUNCT__
1414435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1415564cec59SMatthew G. Knepley /*@
1416435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1417435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1418435a35e8SMatthew G Knepley 
1419435a35e8SMatthew G Knepley   Not collective
1420435a35e8SMatthew G Knepley 
1421435a35e8SMatthew G Knepley   Input Parameters:
1422435a35e8SMatthew G Knepley + dm - the DM object
1423435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
14240298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1425435a35e8SMatthew G Knepley 
1426435a35e8SMatthew G Knepley   Output Parameters:
1427435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1428435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1429435a35e8SMatthew G Knepley 
1430435a35e8SMatthew G Knepley   Level: intermediate
1431435a35e8SMatthew G Knepley 
1432435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1433435a35e8SMatthew G Knepley @*/
1434435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1435435a35e8SMatthew G Knepley {
1436435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1437435a35e8SMatthew G Knepley 
1438435a35e8SMatthew G Knepley   PetscFunctionBegin;
1439435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1440435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
14418865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
14428865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1443435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1444435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
144582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1446435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1447435a35e8SMatthew G Knepley }
1448435a35e8SMatthew G Knepley 
144916621825SDmitry Karpeev 
145016621825SDmitry Karpeev #undef __FUNCT__
145116621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
145216621825SDmitry Karpeev /*@C
14538d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
14548d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
14558d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
14568d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
14578d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
145816621825SDmitry Karpeev 
145916621825SDmitry Karpeev   Not collective
146016621825SDmitry Karpeev 
146116621825SDmitry Karpeev   Input Parameter:
146216621825SDmitry Karpeev . dm - the DM object
146316621825SDmitry Karpeev 
146416621825SDmitry Karpeev   Output Parameters:
14650298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
14660298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
14670298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
14680298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
14690298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
147016621825SDmitry Karpeev 
147116621825SDmitry Karpeev   Level: intermediate
147216621825SDmitry Karpeev 
147316621825SDmitry Karpeev   Notes:
147416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
147516621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
147616621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
147716621825SDmitry Karpeev 
14788d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
147916621825SDmitry Karpeev @*/
14808d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
148116621825SDmitry Karpeev {
148216621825SDmitry Karpeev   PetscErrorCode      ierr;
1483be081cd6SPeter Brune   DMSubDomainHookLink link;
1484be081cd6SPeter Brune   PetscInt            i,l;
148516621825SDmitry Karpeev 
148616621825SDmitry Karpeev   PetscFunctionBegin;
148716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
148814a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14890298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14900298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14910298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14920298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1493f3f0edfdSDmitry Karpeev   /*
1494f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1495f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1496f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1497f3f0edfdSDmitry Karpeev    */
1498ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
149916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1500be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
150114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
150214a18fd3SPeter Brune     if (dmlist) {
1503be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1504be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1505be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1506be081cd6SPeter Brune         }
1507d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1508e7c4fc90SDmitry Karpeev       }
150914a18fd3SPeter Brune     }
151014a18fd3SPeter Brune     if (len) *len = l;
151114a18fd3SPeter Brune   }
1512e30e807fSPeter Brune   PetscFunctionReturn(0);
1513e30e807fSPeter Brune }
1514e30e807fSPeter Brune 
1515e30e807fSPeter Brune 
1516e30e807fSPeter Brune #undef __FUNCT__
1517e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1518e30e807fSPeter Brune /*@C
1519e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1520e30e807fSPeter Brune 
1521e30e807fSPeter Brune   Not collective
1522e30e807fSPeter Brune 
1523e30e807fSPeter Brune   Input Parameters:
1524e30e807fSPeter Brune + dm - the DM object
1525e30e807fSPeter Brune . n  - the number of subdomain scatters
1526e30e807fSPeter Brune - subdms - the local subdomains
1527e30e807fSPeter Brune 
1528e30e807fSPeter Brune   Output Parameters:
1529e30e807fSPeter Brune + n     - the number of scatters returned
1530e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1531e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1532e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1533e30e807fSPeter Brune 
1534e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1535e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1536e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1537e30e807fSPeter Brune   solution and residual data.
1538e30e807fSPeter Brune 
1539e30e807fSPeter Brune   Level: developer
1540e30e807fSPeter Brune 
1541e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1542e30e807fSPeter Brune @*/
1543e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1544e30e807fSPeter Brune {
1545e30e807fSPeter Brune   PetscErrorCode ierr;
1546e30e807fSPeter Brune 
1547e30e807fSPeter Brune   PetscFunctionBegin;
1548e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1549e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1550e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1551e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
155282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1553e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1554e7c4fc90SDmitry Karpeev }
1555e7c4fc90SDmitry Karpeev 
1556731c8d9eSDmitry Karpeev #undef __FUNCT__
155747c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
155847c6ae99SBarry Smith /*@
155947c6ae99SBarry Smith   DMRefine - Refines a DM object
156047c6ae99SBarry Smith 
156147c6ae99SBarry Smith   Collective on DM
156247c6ae99SBarry Smith 
156347c6ae99SBarry Smith   Input Parameter:
156447c6ae99SBarry Smith + dm   - the DM object
156591d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
156647c6ae99SBarry Smith 
156747c6ae99SBarry Smith   Output Parameter:
15680298fd71SBarry Smith . dmf - the refined DM, or NULL
1569ae0a1c52SMatthew G Knepley 
15700298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
157147c6ae99SBarry Smith 
157247c6ae99SBarry Smith   Level: developer
157347c6ae99SBarry Smith 
1574e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
157547c6ae99SBarry Smith @*/
15767087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
157747c6ae99SBarry Smith {
157847c6ae99SBarry Smith   PetscErrorCode   ierr;
1579c833c3b5SJed Brown   DMRefineHookLink link;
158047c6ae99SBarry Smith 
158147c6ae99SBarry Smith   PetscFunctionBegin;
1582732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
158347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15844057135bSMatthew G Knepley   if (*dmf) {
158543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15868865f1eaSKarl Rupp 
15878cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15888865f1eaSKarl Rupp 
1589644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15900598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1591656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15928865f1eaSKarl Rupp 
1593e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1594c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
15958865f1eaSKarl Rupp       if (link->refinehook) {
15968865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
15978865f1eaSKarl Rupp       }
1598c833c3b5SJed Brown     }
1599c833c3b5SJed Brown   }
1600c833c3b5SJed Brown   PetscFunctionReturn(0);
1601c833c3b5SJed Brown }
1602c833c3b5SJed Brown 
1603c833c3b5SJed Brown #undef __FUNCT__
1604c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1605bb9467b5SJed Brown /*@C
1606c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1607c833c3b5SJed Brown 
1608c833c3b5SJed Brown    Logically Collective
1609c833c3b5SJed Brown 
1610c833c3b5SJed Brown    Input Arguments:
1611c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1612c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1613c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16140298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1615c833c3b5SJed Brown 
1616c833c3b5SJed Brown    Calling sequence of refinehook:
1617c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1618c833c3b5SJed Brown 
1619c833c3b5SJed Brown +  coarse - coarse level DM
1620c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1621c833c3b5SJed Brown -  ctx - optional user-defined function context
1622c833c3b5SJed Brown 
1623c833c3b5SJed Brown    Calling sequence for interphook:
1624c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1625c833c3b5SJed Brown 
1626c833c3b5SJed Brown +  coarse - coarse level DM
1627c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1628c833c3b5SJed Brown .  fine - fine level DM to update
1629c833c3b5SJed Brown -  ctx - optional user-defined function context
1630c833c3b5SJed Brown 
1631c833c3b5SJed Brown    Level: advanced
1632c833c3b5SJed Brown 
1633c833c3b5SJed Brown    Notes:
1634c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1635c833c3b5SJed Brown 
1636c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1637c833c3b5SJed Brown 
1638bb9467b5SJed Brown    This function is currently not available from Fortran.
1639bb9467b5SJed Brown 
1640c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1641c833c3b5SJed Brown @*/
1642c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1643c833c3b5SJed Brown {
1644c833c3b5SJed Brown   PetscErrorCode   ierr;
1645c833c3b5SJed Brown   DMRefineHookLink link,*p;
1646c833c3b5SJed Brown 
1647c833c3b5SJed Brown   PetscFunctionBegin;
1648c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1649c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1650c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1651c833c3b5SJed Brown   link->refinehook = refinehook;
1652c833c3b5SJed Brown   link->interphook = interphook;
1653c833c3b5SJed Brown   link->ctx        = ctx;
16540298fd71SBarry Smith   link->next       = NULL;
1655c833c3b5SJed Brown   *p               = link;
1656c833c3b5SJed Brown   PetscFunctionReturn(0);
1657c833c3b5SJed Brown }
1658c833c3b5SJed Brown 
1659c833c3b5SJed Brown #undef __FUNCT__
1660c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1661c833c3b5SJed Brown /*@
1662c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1663c833c3b5SJed Brown 
1664c833c3b5SJed Brown    Collective if any hooks are
1665c833c3b5SJed Brown 
1666c833c3b5SJed Brown    Input Arguments:
1667c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1668c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1669c833c3b5SJed Brown -  fine - finer DM to update
1670c833c3b5SJed Brown 
1671c833c3b5SJed Brown    Level: developer
1672c833c3b5SJed Brown 
1673c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1674c833c3b5SJed Brown @*/
1675c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1676c833c3b5SJed Brown {
1677c833c3b5SJed Brown   PetscErrorCode   ierr;
1678c833c3b5SJed Brown   DMRefineHookLink link;
1679c833c3b5SJed Brown 
1680c833c3b5SJed Brown   PetscFunctionBegin;
1681c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16828865f1eaSKarl Rupp     if (link->interphook) {
16838865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16848865f1eaSKarl Rupp     }
16854057135bSMatthew G Knepley   }
168647c6ae99SBarry Smith   PetscFunctionReturn(0);
168747c6ae99SBarry Smith }
168847c6ae99SBarry Smith 
168947c6ae99SBarry Smith #undef __FUNCT__
1690eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1691eb3f98d2SBarry Smith /*@
1692eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1693eb3f98d2SBarry Smith 
1694eb3f98d2SBarry Smith     Not Collective
1695eb3f98d2SBarry Smith 
1696eb3f98d2SBarry Smith     Input Parameter:
1697eb3f98d2SBarry Smith .   dm - the DM object
1698eb3f98d2SBarry Smith 
1699eb3f98d2SBarry Smith     Output Parameter:
1700eb3f98d2SBarry Smith .   level - number of refinements
1701eb3f98d2SBarry Smith 
1702eb3f98d2SBarry Smith     Level: developer
1703eb3f98d2SBarry Smith 
17046a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1705eb3f98d2SBarry Smith 
1706eb3f98d2SBarry Smith @*/
1707eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1708eb3f98d2SBarry Smith {
1709eb3f98d2SBarry Smith   PetscFunctionBegin;
1710eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1711eb3f98d2SBarry Smith   *level = dm->levelup;
1712eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1713eb3f98d2SBarry Smith }
1714eb3f98d2SBarry Smith 
1715eb3f98d2SBarry Smith #undef __FUNCT__
1716baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1717bb9467b5SJed Brown /*@C
1718baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1719baf369e7SPeter Brune 
1720baf369e7SPeter Brune    Logically Collective
1721baf369e7SPeter Brune 
1722baf369e7SPeter Brune    Input Arguments:
1723baf369e7SPeter Brune +  dm - the DM
1724baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1725baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
17260298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1727baf369e7SPeter Brune 
1728baf369e7SPeter Brune    Calling sequence for beginhook:
1729baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1730baf369e7SPeter Brune 
1731baf369e7SPeter Brune +  dm - global DM
1732baf369e7SPeter Brune .  g - global vector
1733baf369e7SPeter Brune .  mode - mode
1734baf369e7SPeter Brune .  l - local vector
1735baf369e7SPeter Brune -  ctx - optional user-defined function context
1736baf369e7SPeter Brune 
1737baf369e7SPeter Brune 
1738baf369e7SPeter Brune    Calling sequence for endhook:
1739ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1740baf369e7SPeter Brune 
1741baf369e7SPeter Brune +  global - global DM
1742baf369e7SPeter Brune -  ctx - optional user-defined function context
1743baf369e7SPeter Brune 
1744baf369e7SPeter Brune    Level: advanced
1745baf369e7SPeter Brune 
1746baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1747baf369e7SPeter Brune @*/
1748baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1749baf369e7SPeter Brune {
1750baf369e7SPeter Brune   PetscErrorCode          ierr;
1751baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1752baf369e7SPeter Brune 
1753baf369e7SPeter Brune   PetscFunctionBegin;
1754baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1755baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1756baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1757baf369e7SPeter Brune   link->beginhook = beginhook;
1758baf369e7SPeter Brune   link->endhook   = endhook;
1759baf369e7SPeter Brune   link->ctx       = ctx;
17600298fd71SBarry Smith   link->next      = NULL;
1761baf369e7SPeter Brune   *p              = link;
1762baf369e7SPeter Brune   PetscFunctionReturn(0);
1763baf369e7SPeter Brune }
1764baf369e7SPeter Brune 
1765baf369e7SPeter Brune #undef __FUNCT__
17664c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
17674c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
17684c274da1SToby Isaac {
17694c274da1SToby Isaac   Mat cMat;
17704c274da1SToby Isaac   Vec cVec;
17714c274da1SToby Isaac   PetscSection section, cSec;
17724c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
17734c274da1SToby Isaac   PetscErrorCode ierr;
17744c274da1SToby Isaac 
17754c274da1SToby Isaac   PetscFunctionBegin;
17764c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17774c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
17784c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
17794c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
17807711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
17814c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
17824c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
17834c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
17844c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
17854c274da1SToby Isaac       if (dof) {
17864c274da1SToby Isaac         PetscScalar *vals;
17874c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
17884c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
17894c274da1SToby Isaac       }
17904c274da1SToby Isaac     }
17914c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
17924c274da1SToby Isaac   }
17934c274da1SToby Isaac   PetscFunctionReturn(0);
17944c274da1SToby Isaac }
17954c274da1SToby Isaac 
17964c274da1SToby Isaac #undef __FUNCT__
179747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
179847c6ae99SBarry Smith /*@
179947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
180047c6ae99SBarry Smith 
180147c6ae99SBarry Smith     Neighbor-wise Collective on DM
180247c6ae99SBarry Smith 
180347c6ae99SBarry Smith     Input Parameters:
180447c6ae99SBarry Smith +   dm - the DM object
180547c6ae99SBarry Smith .   g - the global vector
180647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
180747c6ae99SBarry Smith -   l - the local vector
180847c6ae99SBarry Smith 
180947c6ae99SBarry Smith 
181047c6ae99SBarry Smith     Level: beginner
181147c6ae99SBarry Smith 
1812e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
181347c6ae99SBarry Smith 
181447c6ae99SBarry Smith @*/
18157087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
181647c6ae99SBarry Smith {
18177128ae9fSMatthew G Knepley   PetscSF                 sf;
181847c6ae99SBarry Smith   PetscErrorCode          ierr;
1819baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
182047c6ae99SBarry Smith 
182147c6ae99SBarry Smith   PetscFunctionBegin;
1822171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1823baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
18248865f1eaSKarl Rupp     if (link->beginhook) {
18258865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
18268865f1eaSKarl Rupp     }
1827baf369e7SPeter Brune   }
18287128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18297128ae9fSMatthew G Knepley   if (sf) {
1830ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1831ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
18327128ae9fSMatthew G Knepley 
183382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18347128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1835ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18367128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18377128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1838ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18397128ae9fSMatthew G Knepley   } else {
1840843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18417128ae9fSMatthew G Knepley   }
184247c6ae99SBarry Smith   PetscFunctionReturn(0);
184347c6ae99SBarry Smith }
184447c6ae99SBarry Smith 
184547c6ae99SBarry Smith #undef __FUNCT__
184647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
184747c6ae99SBarry Smith /*@
184847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
184947c6ae99SBarry Smith 
185047c6ae99SBarry Smith     Neighbor-wise Collective on DM
185147c6ae99SBarry Smith 
185247c6ae99SBarry Smith     Input Parameters:
185347c6ae99SBarry Smith +   dm - the DM object
185447c6ae99SBarry Smith .   g - the global vector
185547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
185647c6ae99SBarry Smith -   l - the local vector
185747c6ae99SBarry Smith 
185847c6ae99SBarry Smith 
185947c6ae99SBarry Smith     Level: beginner
186047c6ae99SBarry Smith 
1861e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
186247c6ae99SBarry Smith 
186347c6ae99SBarry Smith @*/
18647087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
186547c6ae99SBarry Smith {
18667128ae9fSMatthew G Knepley   PetscSF                 sf;
186747c6ae99SBarry Smith   PetscErrorCode          ierr;
1868ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1869ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1870baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
187147c6ae99SBarry Smith 
187247c6ae99SBarry Smith   PetscFunctionBegin;
1873171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18747128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18757128ae9fSMatthew G Knepley   if (sf) {
187682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18777128ae9fSMatthew G Knepley 
18787128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1879ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18807128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1882ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18837128ae9fSMatthew G Knepley   } else {
1884843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18857128ae9fSMatthew G Knepley   }
18864c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1887baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1888baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1889baf369e7SPeter Brune   }
189047c6ae99SBarry Smith   PetscFunctionReturn(0);
189147c6ae99SBarry Smith }
189247c6ae99SBarry Smith 
189347c6ae99SBarry Smith #undef __FUNCT__
1894d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1895d4d07f1eSToby Isaac /*@C
1896d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1897d4d07f1eSToby Isaac 
1898d4d07f1eSToby Isaac    Logically Collective
1899d4d07f1eSToby Isaac 
1900d4d07f1eSToby Isaac    Input Arguments:
1901d4d07f1eSToby Isaac +  dm - the DM
1902d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1903d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1904d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1905d4d07f1eSToby Isaac 
1906d4d07f1eSToby Isaac    Calling sequence for beginhook:
1907d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1908d4d07f1eSToby Isaac 
1909d4d07f1eSToby Isaac +  dm - global DM
1910d4d07f1eSToby Isaac .  l - local vector
1911d4d07f1eSToby Isaac .  mode - mode
1912d4d07f1eSToby Isaac .  g - global vector
1913d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1914d4d07f1eSToby Isaac 
1915d4d07f1eSToby Isaac 
1916d4d07f1eSToby Isaac    Calling sequence for endhook:
1917d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1918d4d07f1eSToby Isaac 
1919d4d07f1eSToby Isaac +  global - global DM
1920d4d07f1eSToby Isaac .  l - local vector
1921d4d07f1eSToby Isaac .  mode - mode
1922d4d07f1eSToby Isaac .  g - global vector
1923d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1924d4d07f1eSToby Isaac 
1925d4d07f1eSToby Isaac    Level: advanced
1926d4d07f1eSToby Isaac 
1927d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1928d4d07f1eSToby Isaac @*/
1929d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1930d4d07f1eSToby Isaac {
1931d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1932d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1933d4d07f1eSToby Isaac 
1934d4d07f1eSToby Isaac   PetscFunctionBegin;
1935d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1936d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1937d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1938d4d07f1eSToby Isaac   link->beginhook = beginhook;
1939d4d07f1eSToby Isaac   link->endhook   = endhook;
1940d4d07f1eSToby Isaac   link->ctx       = ctx;
1941d4d07f1eSToby Isaac   link->next      = NULL;
1942d4d07f1eSToby Isaac   *p              = link;
1943d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1944d4d07f1eSToby Isaac }
1945d4d07f1eSToby Isaac 
1946d4d07f1eSToby Isaac #undef __FUNCT__
19474c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
19484c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
19494c274da1SToby Isaac {
19504c274da1SToby Isaac   Mat cMat;
19514c274da1SToby Isaac   Vec cVec;
19524c274da1SToby Isaac   PetscSection section, cSec;
19534c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19544c274da1SToby Isaac   PetscErrorCode ierr;
19554c274da1SToby Isaac 
19564c274da1SToby Isaac   PetscFunctionBegin;
19574c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19584c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19594c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
19604c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19617711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19624c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19634c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19644c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19654c274da1SToby Isaac       if (dof) {
19664c274da1SToby Isaac         PetscInt d;
19674c274da1SToby Isaac         PetscScalar *vals;
19684c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
19694c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
19704c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
19714c274da1SToby Isaac          * we just extracted */
19724c274da1SToby Isaac         for (d = 0; d < dof; d++) {
19734c274da1SToby Isaac           vals[d] = 0.;
19744c274da1SToby Isaac         }
19754c274da1SToby Isaac       }
19764c274da1SToby Isaac     }
19774c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
19784c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19794c274da1SToby Isaac   }
19804c274da1SToby Isaac   PetscFunctionReturn(0);
19814c274da1SToby Isaac }
19824c274da1SToby Isaac 
19834c274da1SToby Isaac #undef __FUNCT__
19849a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
198547c6ae99SBarry Smith /*@
19869a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
19879a42bb27SBarry Smith 
19889a42bb27SBarry Smith     Neighbor-wise Collective on DM
19899a42bb27SBarry Smith 
19909a42bb27SBarry Smith     Input Parameters:
19919a42bb27SBarry Smith +   dm - the DM object
1992f6813fd5SJed Brown .   l - the local vector
19931eb28f2eSBarry 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.
19941eb28f2eSBarry Smith -   g - the global vector
19959a42bb27SBarry Smith 
1996d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
199784330215SMatthew 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.
19989a42bb27SBarry Smith 
19999a42bb27SBarry Smith     Level: beginner
20009a42bb27SBarry Smith 
2001e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
20029a42bb27SBarry Smith 
20039a42bb27SBarry Smith @*/
20047087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
20059a42bb27SBarry Smith {
20067128ae9fSMatthew G Knepley   PetscSF                 sf;
200784330215SMatthew G. Knepley   PetscSection            s, gs;
2008d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2009ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2010ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
201184330215SMatthew G. Knepley   PetscBool               isInsert;
201284330215SMatthew G. Knepley   PetscErrorCode          ierr;
20139a42bb27SBarry Smith 
20149a42bb27SBarry Smith   PetscFunctionBegin;
2015171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2016d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2017d4d07f1eSToby Isaac     if (link->beginhook) {
2018d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2019d4d07f1eSToby Isaac     }
2020d4d07f1eSToby Isaac   }
20214c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
20227128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
202384330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20247128ae9fSMatthew G Knepley   switch (mode) {
20257128ae9fSMatthew G Knepley   case INSERT_VALUES:
20267128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
202784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20287128ae9fSMatthew G Knepley   case ADD_VALUES:
20297128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
203084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20317128ae9fSMatthew G Knepley   default:
203282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20337128ae9fSMatthew G Knepley   }
203484330215SMatthew G. Knepley   if (sf && !isInsert) {
2035ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20367128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2037a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2038ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
203984330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
204084330215SMatthew G. Knepley   } else if (s && isInsert) {
204184330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
204284330215SMatthew G. Knepley 
204384330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
204484330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
204584330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2046ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
204784330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
204884330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2049b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
205084330215SMatthew G. Knepley 
205184330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
205203442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
205384330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2054b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
205584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
205684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2057b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
205803442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2059b3b16f48SMatthew 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);
2060b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2061b3b16f48SMatthew G. Knepley       if (!gcdof) {
206284330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2063b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2064b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
206584330215SMatthew G. Knepley         const PetscInt *cdofs;
206684330215SMatthew G. Knepley         PetscInt        cind = 0;
206784330215SMatthew G. Knepley 
206884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2069b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
207084330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2071b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
207284330215SMatthew G. Knepley         }
2073b3b16f48SMatthew 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);
207484330215SMatthew G. Knepley     }
2075ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20767128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
20777128ae9fSMatthew G Knepley   } else {
2078843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20797128ae9fSMatthew G Knepley   }
20809a42bb27SBarry Smith   PetscFunctionReturn(0);
20819a42bb27SBarry Smith }
20829a42bb27SBarry Smith 
20839a42bb27SBarry Smith #undef __FUNCT__
20849a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
20859a42bb27SBarry Smith /*@
20869a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
208747c6ae99SBarry Smith 
208847c6ae99SBarry Smith     Neighbor-wise Collective on DM
208947c6ae99SBarry Smith 
209047c6ae99SBarry Smith     Input Parameters:
209147c6ae99SBarry Smith +   dm - the DM object
2092f6813fd5SJed Brown .   l - the local vector
209347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2094f6813fd5SJed Brown -   g - the global vector
209547c6ae99SBarry Smith 
209647c6ae99SBarry Smith 
209747c6ae99SBarry Smith     Level: beginner
209847c6ae99SBarry Smith 
2099e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
210047c6ae99SBarry Smith 
210147c6ae99SBarry Smith @*/
21027087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
210347c6ae99SBarry Smith {
21047128ae9fSMatthew G Knepley   PetscSF                 sf;
210584330215SMatthew G. Knepley   PetscSection            s;
2106d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
210784330215SMatthew G. Knepley   PetscBool               isInsert;
210884330215SMatthew G. Knepley   PetscErrorCode          ierr;
210947c6ae99SBarry Smith 
211047c6ae99SBarry Smith   PetscFunctionBegin;
2111171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21127128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
211384330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21147128ae9fSMatthew G Knepley   switch (mode) {
21157128ae9fSMatthew G Knepley   case INSERT_VALUES:
21167128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
211784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21187128ae9fSMatthew G Knepley   case ADD_VALUES:
21197128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
212084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21217128ae9fSMatthew G Knepley   default:
212282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21237128ae9fSMatthew G Knepley   }
212484330215SMatthew G. Knepley   if (sf && !isInsert) {
2125ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2126ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
212784330215SMatthew G. Knepley 
2128ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21297128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2130a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2131ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21327128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
213384330215SMatthew G. Knepley   } else if (s && isInsert) {
21347128ae9fSMatthew G Knepley   } else {
2135843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21367128ae9fSMatthew G Knepley   }
2137d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2138d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2139d4d07f1eSToby Isaac   }
214047c6ae99SBarry Smith   PetscFunctionReturn(0);
214147c6ae99SBarry Smith }
214247c6ae99SBarry Smith 
214347c6ae99SBarry Smith #undef __FUNCT__
2144f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2145f089877aSRichard Tran Mills /*@
2146bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2147bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2148d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2149f089877aSRichard Tran Mills 
2150bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2151f089877aSRichard Tran Mills 
2152f089877aSRichard Tran Mills    Input Parameters:
2153f089877aSRichard Tran Mills +  dm - the DM object
2154bc0a1609SRichard Tran Mills .  g - the original local vector
2155bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2156f089877aSRichard Tran Mills 
2157bc0a1609SRichard Tran Mills    Output Parameter:
2158bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2159f089877aSRichard Tran Mills 
2160f089877aSRichard Tran Mills    Level: intermediate
2161f089877aSRichard Tran Mills 
2162bc0a1609SRichard Tran Mills    Notes:
2163bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2164bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2165bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2166bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2167bc0a1609SRichard Tran Mills 
2168bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2169bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2170f089877aSRichard Tran Mills 
2171f089877aSRichard Tran Mills @*/
2172f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2173f089877aSRichard Tran Mills {
2174f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2175f089877aSRichard Tran Mills 
2176f089877aSRichard Tran Mills   PetscFunctionBegin;
2177f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2178f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2179f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2180f089877aSRichard Tran Mills }
2181f089877aSRichard Tran Mills 
2182f089877aSRichard Tran Mills #undef __FUNCT__
2183f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2184f089877aSRichard Tran Mills /*@
2185bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2186bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2187d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2188f089877aSRichard Tran Mills 
2189bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2190f089877aSRichard Tran Mills 
2191f089877aSRichard Tran Mills    Input Parameters:
2192bc0a1609SRichard Tran Mills +  da - the DM object
2193bc0a1609SRichard Tran Mills .  g - the original local vector
2194bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2195f089877aSRichard Tran Mills 
2196bc0a1609SRichard Tran Mills    Output Parameter:
2197bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2198f089877aSRichard Tran Mills 
2199f089877aSRichard Tran Mills    Level: intermediate
2200f089877aSRichard Tran Mills 
2201bc0a1609SRichard Tran Mills    Notes:
2202bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2203bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2204bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2205bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2206bc0a1609SRichard Tran Mills 
2207bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2208bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2209f089877aSRichard Tran Mills 
2210f089877aSRichard Tran Mills @*/
2211f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2212f089877aSRichard Tran Mills {
2213f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2214f089877aSRichard Tran Mills 
2215f089877aSRichard Tran Mills   PetscFunctionBegin;
2216f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2217f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2218f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2219f089877aSRichard Tran Mills }
2220f089877aSRichard Tran Mills 
2221f089877aSRichard Tran Mills 
2222f089877aSRichard Tran Mills #undef __FUNCT__
222347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
222447c6ae99SBarry Smith /*@
222547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
222647c6ae99SBarry Smith 
222747c6ae99SBarry Smith     Collective on DM
222847c6ae99SBarry Smith 
222947c6ae99SBarry Smith     Input Parameter:
223047c6ae99SBarry Smith +   dm - the DM object
223191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
223247c6ae99SBarry Smith 
223347c6ae99SBarry Smith     Output Parameter:
223447c6ae99SBarry Smith .   dmc - the coarsened DM
223547c6ae99SBarry Smith 
223647c6ae99SBarry Smith     Level: developer
223747c6ae99SBarry Smith 
2238e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
223947c6ae99SBarry Smith 
224047c6ae99SBarry Smith @*/
22417087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
224247c6ae99SBarry Smith {
224347c6ae99SBarry Smith   PetscErrorCode    ierr;
2244b17ce1afSJed Brown   DMCoarsenHookLink link;
224547c6ae99SBarry Smith 
224647c6ae99SBarry Smith   PetscFunctionBegin;
2247171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
224847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
224947c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
22508892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2251a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
225243842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
22538cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2254644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
22550598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2256656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2257e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2258b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2259b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2260b17ce1afSJed Brown   }
226147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2262b17ce1afSJed Brown   PetscFunctionReturn(0);
2263b17ce1afSJed Brown }
2264b17ce1afSJed Brown 
2265b17ce1afSJed Brown #undef __FUNCT__
2266b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2267bb9467b5SJed Brown /*@C
2268b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2269b17ce1afSJed Brown 
2270b17ce1afSJed Brown    Logically Collective
2271b17ce1afSJed Brown 
2272b17ce1afSJed Brown    Input Arguments:
2273b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2274b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2275b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
22760298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2277b17ce1afSJed Brown 
2278b17ce1afSJed Brown    Calling sequence of coarsenhook:
2279b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2280b17ce1afSJed Brown 
2281b17ce1afSJed Brown +  fine - fine level DM
2282b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2283b17ce1afSJed Brown -  ctx - optional user-defined function context
2284b17ce1afSJed Brown 
2285b17ce1afSJed Brown    Calling sequence for restricthook:
2286c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2287b17ce1afSJed Brown 
2288b17ce1afSJed Brown +  fine - fine level DM
2289b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2290c833c3b5SJed Brown .  rscale - scaling vector for restriction
2291c833c3b5SJed Brown .  inject - matrix restricting by injection
2292b17ce1afSJed Brown .  coarse - coarse level DM to update
2293b17ce1afSJed Brown -  ctx - optional user-defined function context
2294b17ce1afSJed Brown 
2295b17ce1afSJed Brown    Level: advanced
2296b17ce1afSJed Brown 
2297b17ce1afSJed Brown    Notes:
2298b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2299b17ce1afSJed Brown 
2300b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2301b17ce1afSJed Brown 
2302b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2303b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2304b17ce1afSJed Brown 
2305bb9467b5SJed Brown    This function is currently not available from Fortran.
2306bb9467b5SJed Brown 
2307c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2308b17ce1afSJed Brown @*/
2309b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2310b17ce1afSJed Brown {
2311b17ce1afSJed Brown   PetscErrorCode    ierr;
2312b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2313b17ce1afSJed Brown 
2314b17ce1afSJed Brown   PetscFunctionBegin;
2315b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
23166bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2317b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2318b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2319b17ce1afSJed Brown   link->restricthook = restricthook;
2320b17ce1afSJed Brown   link->ctx          = ctx;
23210298fd71SBarry Smith   link->next         = NULL;
2322b17ce1afSJed Brown   *p                 = link;
2323b17ce1afSJed Brown   PetscFunctionReturn(0);
2324b17ce1afSJed Brown }
2325b17ce1afSJed Brown 
2326b17ce1afSJed Brown #undef __FUNCT__
2327b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2328b17ce1afSJed Brown /*@
2329b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2330b17ce1afSJed Brown 
2331b17ce1afSJed Brown    Collective if any hooks are
2332b17ce1afSJed Brown 
2333b17ce1afSJed Brown    Input Arguments:
2334b17ce1afSJed Brown +  fine - finer DM to use as a base
2335b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2336b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2337b17ce1afSJed Brown -  coarse - coarer DM to update
2338b17ce1afSJed Brown 
2339b17ce1afSJed Brown    Level: developer
2340b17ce1afSJed Brown 
2341b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2342b17ce1afSJed Brown @*/
2343b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2344b17ce1afSJed Brown {
2345b17ce1afSJed Brown   PetscErrorCode    ierr;
2346b17ce1afSJed Brown   DMCoarsenHookLink link;
2347b17ce1afSJed Brown 
2348b17ce1afSJed Brown   PetscFunctionBegin;
2349b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
23508865f1eaSKarl Rupp     if (link->restricthook) {
23518865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
23528865f1eaSKarl Rupp     }
2353b17ce1afSJed Brown   }
235447c6ae99SBarry Smith   PetscFunctionReturn(0);
235547c6ae99SBarry Smith }
235647c6ae99SBarry Smith 
235747c6ae99SBarry Smith #undef __FUNCT__
2358be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2359bb9467b5SJed Brown /*@C
2360be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
23615dbd56e3SPeter Brune 
23625dbd56e3SPeter Brune    Logically Collective
23635dbd56e3SPeter Brune 
23645dbd56e3SPeter Brune    Input Arguments:
23655dbd56e3SPeter Brune +  global - global DM
2366ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
23675dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
23680298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23695dbd56e3SPeter Brune 
2370ec4806b8SPeter Brune 
2371ec4806b8SPeter Brune    Calling sequence for ddhook:
2372ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2373ec4806b8SPeter Brune 
2374ec4806b8SPeter Brune +  global - global DM
2375ec4806b8SPeter Brune .  block  - block DM
2376ec4806b8SPeter Brune -  ctx - optional user-defined function context
2377ec4806b8SPeter Brune 
23785dbd56e3SPeter Brune    Calling sequence for restricthook:
2379ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
23805dbd56e3SPeter Brune 
23815dbd56e3SPeter Brune +  global - global DM
23825dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
23835dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2384ec4806b8SPeter Brune .  block  - block DM
23855dbd56e3SPeter Brune -  ctx - optional user-defined function context
23865dbd56e3SPeter Brune 
23875dbd56e3SPeter Brune    Level: advanced
23885dbd56e3SPeter Brune 
23895dbd56e3SPeter Brune    Notes:
2390ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
23915dbd56e3SPeter Brune 
23925dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
23935dbd56e3SPeter Brune 
23945dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2395ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
23965dbd56e3SPeter Brune 
2397bb9467b5SJed Brown    This function is currently not available from Fortran.
2398bb9467b5SJed Brown 
23995dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
24005dbd56e3SPeter Brune @*/
2401be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
24025dbd56e3SPeter Brune {
24035dbd56e3SPeter Brune   PetscErrorCode      ierr;
2404be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
24055dbd56e3SPeter Brune 
24065dbd56e3SPeter Brune   PetscFunctionBegin;
24075dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2408be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2409be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
24105dbd56e3SPeter Brune   link->restricthook = restricthook;
2411be081cd6SPeter Brune   link->ddhook       = ddhook;
24125dbd56e3SPeter Brune   link->ctx          = ctx;
24130298fd71SBarry Smith   link->next         = NULL;
24145dbd56e3SPeter Brune   *p                 = link;
24155dbd56e3SPeter Brune   PetscFunctionReturn(0);
24165dbd56e3SPeter Brune }
24175dbd56e3SPeter Brune 
24185dbd56e3SPeter Brune #undef __FUNCT__
2419be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
24205dbd56e3SPeter Brune /*@
2421be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
24225dbd56e3SPeter Brune 
24235dbd56e3SPeter Brune    Collective if any hooks are
24245dbd56e3SPeter Brune 
24255dbd56e3SPeter Brune    Input Arguments:
24265dbd56e3SPeter Brune +  fine - finer DM to use as a base
2427be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2428be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
24295dbd56e3SPeter Brune -  coarse - coarer DM to update
24305dbd56e3SPeter Brune 
24315dbd56e3SPeter Brune    Level: developer
24325dbd56e3SPeter Brune 
24335dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
24345dbd56e3SPeter Brune @*/
2435be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
24365dbd56e3SPeter Brune {
24375dbd56e3SPeter Brune   PetscErrorCode      ierr;
2438be081cd6SPeter Brune   DMSubDomainHookLink link;
24395dbd56e3SPeter Brune 
24405dbd56e3SPeter Brune   PetscFunctionBegin;
2441be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
24428865f1eaSKarl Rupp     if (link->restricthook) {
24438865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
24448865f1eaSKarl Rupp     }
24455dbd56e3SPeter Brune   }
24465dbd56e3SPeter Brune   PetscFunctionReturn(0);
24475dbd56e3SPeter Brune }
24485dbd56e3SPeter Brune 
24495dbd56e3SPeter Brune #undef __FUNCT__
24505fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
24515fe1f584SPeter Brune /*@
24526a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
24535fe1f584SPeter Brune 
24545fe1f584SPeter Brune     Not Collective
24555fe1f584SPeter Brune 
24565fe1f584SPeter Brune     Input Parameter:
24575fe1f584SPeter Brune .   dm - the DM object
24585fe1f584SPeter Brune 
24595fe1f584SPeter Brune     Output Parameter:
24606a7d9d85SPeter Brune .   level - number of coarsenings
24615fe1f584SPeter Brune 
24625fe1f584SPeter Brune     Level: developer
24635fe1f584SPeter Brune 
24646a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
24655fe1f584SPeter Brune 
24665fe1f584SPeter Brune @*/
24675fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
24685fe1f584SPeter Brune {
24695fe1f584SPeter Brune   PetscFunctionBegin;
24705fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24715fe1f584SPeter Brune   *level = dm->leveldown;
24725fe1f584SPeter Brune   PetscFunctionReturn(0);
24735fe1f584SPeter Brune }
24745fe1f584SPeter Brune 
24755fe1f584SPeter Brune 
24765fe1f584SPeter Brune 
24775fe1f584SPeter Brune #undef __FUNCT__
247847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
247947c6ae99SBarry Smith /*@C
248047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
248147c6ae99SBarry Smith 
248247c6ae99SBarry Smith     Collective on DM
248347c6ae99SBarry Smith 
248447c6ae99SBarry Smith     Input Parameter:
248547c6ae99SBarry Smith +   dm - the DM object
248647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
248747c6ae99SBarry Smith 
248847c6ae99SBarry Smith     Output Parameter:
248947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
249047c6ae99SBarry Smith 
249147c6ae99SBarry Smith     Level: developer
249247c6ae99SBarry Smith 
2493e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
249447c6ae99SBarry Smith 
249547c6ae99SBarry Smith @*/
24967087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
249747c6ae99SBarry Smith {
249847c6ae99SBarry Smith   PetscErrorCode ierr;
249947c6ae99SBarry Smith 
250047c6ae99SBarry Smith   PetscFunctionBegin;
2501171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2502ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
250347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
250447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
250547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
250647c6ae99SBarry Smith   } else if (dm->ops->refine) {
250747c6ae99SBarry Smith     PetscInt i;
250847c6ae99SBarry Smith 
2509ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
251047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2511ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
251247c6ae99SBarry Smith     }
2513ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
251447c6ae99SBarry Smith   PetscFunctionReturn(0);
251547c6ae99SBarry Smith }
251647c6ae99SBarry Smith 
251747c6ae99SBarry Smith #undef __FUNCT__
251847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
251947c6ae99SBarry Smith /*@C
252047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
252147c6ae99SBarry Smith 
252247c6ae99SBarry Smith     Collective on DM
252347c6ae99SBarry Smith 
252447c6ae99SBarry Smith     Input Parameter:
252547c6ae99SBarry Smith +   dm - the DM object
252647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
252747c6ae99SBarry Smith 
252847c6ae99SBarry Smith     Output Parameter:
252947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
253047c6ae99SBarry Smith 
253147c6ae99SBarry Smith     Level: developer
253247c6ae99SBarry Smith 
2533e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
253447c6ae99SBarry Smith 
253547c6ae99SBarry Smith @*/
25367087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
253747c6ae99SBarry Smith {
253847c6ae99SBarry Smith   PetscErrorCode ierr;
253947c6ae99SBarry Smith 
254047c6ae99SBarry Smith   PetscFunctionBegin;
2541171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2542ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
254347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
254447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
254547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
254647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
254747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
254847c6ae99SBarry Smith     PetscInt i;
254947c6ae99SBarry Smith 
2550ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
255147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2552ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
255347c6ae99SBarry Smith     }
2554ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
255547c6ae99SBarry Smith   PetscFunctionReturn(0);
255647c6ae99SBarry Smith }
255747c6ae99SBarry Smith 
255847c6ae99SBarry Smith #undef __FUNCT__
2559e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
256047c6ae99SBarry Smith /*@
2561e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
256247c6ae99SBarry Smith    grids associated with two DMs.
256347c6ae99SBarry Smith 
256447c6ae99SBarry Smith    Collective on DM
256547c6ae99SBarry Smith 
256647c6ae99SBarry Smith    Input Parameters:
256747c6ae99SBarry Smith +  dmc - the coarse grid DM
256847c6ae99SBarry Smith -  dmf - the fine grid DM
256947c6ae99SBarry Smith 
257047c6ae99SBarry Smith    Output Parameters:
257147c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
257247c6ae99SBarry Smith 
257347c6ae99SBarry Smith    Level: intermediate
257447c6ae99SBarry Smith 
257547c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
257647c6ae99SBarry Smith 
2577e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
257847c6ae99SBarry Smith @*/
2579e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
258047c6ae99SBarry Smith {
258147c6ae99SBarry Smith   PetscErrorCode ierr;
258247c6ae99SBarry Smith 
258347c6ae99SBarry Smith   PetscFunctionBegin;
2584171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2585171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
258647c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
258747c6ae99SBarry Smith   PetscFunctionReturn(0);
258847c6ae99SBarry Smith }
258947c6ae99SBarry Smith 
259047c6ae99SBarry Smith #undef __FUNCT__
25911a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
25921a266240SBarry Smith /*@C
25931a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
25941a266240SBarry Smith 
25951a266240SBarry Smith     Not Collective
25961a266240SBarry Smith 
25971a266240SBarry Smith     Input Parameters:
25981a266240SBarry Smith +   dm - the DM object
25991a266240SBarry Smith -   destroy - the destroy function
26001a266240SBarry Smith 
26011a266240SBarry Smith     Level: intermediate
26021a266240SBarry Smith 
2603e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
26041a266240SBarry Smith 
2605f07f9ceaSJed Brown @*/
26061a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
26071a266240SBarry Smith {
26081a266240SBarry Smith   PetscFunctionBegin;
2609171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26101a266240SBarry Smith   dm->ctxdestroy = destroy;
26111a266240SBarry Smith   PetscFunctionReturn(0);
26121a266240SBarry Smith }
26131a266240SBarry Smith 
26141a266240SBarry Smith #undef __FUNCT__
26151b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2616b07ff414SBarry Smith /*@
26171b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
261847c6ae99SBarry Smith 
261947c6ae99SBarry Smith     Not Collective
262047c6ae99SBarry Smith 
262147c6ae99SBarry Smith     Input Parameters:
262247c6ae99SBarry Smith +   dm - the DM object
262347c6ae99SBarry Smith -   ctx - the user context
262447c6ae99SBarry Smith 
262547c6ae99SBarry Smith     Level: intermediate
262647c6ae99SBarry Smith 
2627e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
262847c6ae99SBarry Smith 
262947c6ae99SBarry Smith @*/
26301b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
263147c6ae99SBarry Smith {
263247c6ae99SBarry Smith   PetscFunctionBegin;
2633171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
263447c6ae99SBarry Smith   dm->ctx = ctx;
263547c6ae99SBarry Smith   PetscFunctionReturn(0);
263647c6ae99SBarry Smith }
263747c6ae99SBarry Smith 
263847c6ae99SBarry Smith #undef __FUNCT__
26391b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
264047c6ae99SBarry Smith /*@
26411b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
264247c6ae99SBarry Smith 
264347c6ae99SBarry Smith     Not Collective
264447c6ae99SBarry Smith 
264547c6ae99SBarry Smith     Input Parameter:
264647c6ae99SBarry Smith .   dm - the DM object
264747c6ae99SBarry Smith 
264847c6ae99SBarry Smith     Output Parameter:
264947c6ae99SBarry Smith .   ctx - the user context
265047c6ae99SBarry Smith 
265147c6ae99SBarry Smith     Level: intermediate
265247c6ae99SBarry Smith 
2653e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
265447c6ae99SBarry Smith 
265547c6ae99SBarry Smith @*/
26561b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
265747c6ae99SBarry Smith {
265847c6ae99SBarry Smith   PetscFunctionBegin;
2659171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26601b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
266147c6ae99SBarry Smith   PetscFunctionReturn(0);
266247c6ae99SBarry Smith }
266347c6ae99SBarry Smith 
266447c6ae99SBarry Smith #undef __FUNCT__
266508da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
266608da532bSDmitry Karpeev /*@C
2667df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
266808da532bSDmitry Karpeev 
266908da532bSDmitry Karpeev     Logically Collective on DM
267008da532bSDmitry Karpeev 
267108da532bSDmitry Karpeev     Input Parameter:
267208da532bSDmitry Karpeev +   dm - the DM object
26730298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
267408da532bSDmitry Karpeev 
267508da532bSDmitry Karpeev     Level: intermediate
267608da532bSDmitry Karpeev 
2677835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
267808da532bSDmitry Karpeev          DMSetJacobian()
267908da532bSDmitry Karpeev 
268008da532bSDmitry Karpeev @*/
268108da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
268208da532bSDmitry Karpeev {
268308da532bSDmitry Karpeev   PetscFunctionBegin;
268408da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
268508da532bSDmitry Karpeev   PetscFunctionReturn(0);
268608da532bSDmitry Karpeev }
268708da532bSDmitry Karpeev 
268808da532bSDmitry Karpeev #undef __FUNCT__
268908da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
269008da532bSDmitry Karpeev /*@
269108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
269208da532bSDmitry Karpeev 
269308da532bSDmitry Karpeev     Not Collective
269408da532bSDmitry Karpeev 
269508da532bSDmitry Karpeev     Input Parameter:
269608da532bSDmitry Karpeev .   dm - the DM object to destroy
269708da532bSDmitry Karpeev 
269808da532bSDmitry Karpeev     Output Parameter:
269908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
270008da532bSDmitry Karpeev 
270108da532bSDmitry Karpeev     Level: developer
270208da532bSDmitry Karpeev 
270374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
270408da532bSDmitry Karpeev 
270508da532bSDmitry Karpeev @*/
270608da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
270708da532bSDmitry Karpeev {
270808da532bSDmitry Karpeev   PetscFunctionBegin;
270908da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
271008da532bSDmitry Karpeev   PetscFunctionReturn(0);
271108da532bSDmitry Karpeev }
271208da532bSDmitry Karpeev 
271308da532bSDmitry Karpeev #undef __FUNCT__
271408da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
271508da532bSDmitry Karpeev /*@C
271608da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
271708da532bSDmitry Karpeev 
271808da532bSDmitry Karpeev     Logically Collective on DM
271908da532bSDmitry Karpeev 
272008da532bSDmitry Karpeev     Input Parameters:
2721907376e6SBarry Smith .   dm - the DM object
272208da532bSDmitry Karpeev 
272308da532bSDmitry Karpeev     Output parameters:
272408da532bSDmitry Karpeev +   xl - lower bound
272508da532bSDmitry Karpeev -   xu - upper bound
272608da532bSDmitry Karpeev 
2727907376e6SBarry Smith     Level: advanced
2728907376e6SBarry Smith 
2729907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
273008da532bSDmitry Karpeev 
273174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
273208da532bSDmitry Karpeev 
273308da532bSDmitry Karpeev @*/
273408da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
273508da532bSDmitry Karpeev {
273608da532bSDmitry Karpeev   PetscErrorCode ierr;
27375fd66863SKarl Rupp 
273808da532bSDmitry Karpeev   PetscFunctionBegin;
273908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
274008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
274108da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
274208da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
27438865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
274408da532bSDmitry Karpeev   PetscFunctionReturn(0);
274508da532bSDmitry Karpeev }
274608da532bSDmitry Karpeev 
274708da532bSDmitry Karpeev #undef __FUNCT__
2748b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2749b0ae01b7SPeter Brune /*@
2750b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2751b0ae01b7SPeter Brune 
2752b0ae01b7SPeter Brune     Not Collective
2753b0ae01b7SPeter Brune 
2754b0ae01b7SPeter Brune     Input Parameter:
2755b0ae01b7SPeter Brune .   dm - the DM object
2756b0ae01b7SPeter Brune 
2757b0ae01b7SPeter Brune     Output Parameter:
2758b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2759b0ae01b7SPeter Brune 
2760b0ae01b7SPeter Brune     Level: developer
2761b0ae01b7SPeter Brune 
2762b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2763b0ae01b7SPeter Brune 
2764b0ae01b7SPeter Brune @*/
2765b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2766b0ae01b7SPeter Brune {
2767b0ae01b7SPeter Brune   PetscFunctionBegin;
2768b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2769b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2770b0ae01b7SPeter Brune }
2771b0ae01b7SPeter Brune 
2772b0ae01b7SPeter Brune #undef  __FUNCT__
277308da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2774748fac09SDmitry Karpeev /*@C
277508da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
277608da532bSDmitry Karpeev 
277708da532bSDmitry Karpeev     Collective on DM
277808da532bSDmitry Karpeev 
277908da532bSDmitry Karpeev     Input Parameter:
278008da532bSDmitry Karpeev +   dm - the DM object
27810298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
278208da532bSDmitry Karpeev 
278308da532bSDmitry Karpeev     Level: developer
278408da532bSDmitry Karpeev 
278574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
278608da532bSDmitry Karpeev 
278708da532bSDmitry Karpeev @*/
278808da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
278908da532bSDmitry Karpeev {
279008da532bSDmitry Karpeev   PetscErrorCode ierr;
27915fd66863SKarl Rupp 
279208da532bSDmitry Karpeev   PetscFunctionBegin;
279308da532bSDmitry Karpeev   if (x) {
279408da532bSDmitry Karpeev     if (!dm->x) {
279508da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
279608da532bSDmitry Karpeev     }
279708da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
27988865f1eaSKarl Rupp   } else if (dm->x) {
279908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
280008da532bSDmitry Karpeev   }
280108da532bSDmitry Karpeev   PetscFunctionReturn(0);
280208da532bSDmitry Karpeev }
280308da532bSDmitry Karpeev 
28040298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2805264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2806264ace61SBarry Smith 
2807264ace61SBarry Smith #undef __FUNCT__
2808264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2809264ace61SBarry Smith /*@C
2810264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2811264ace61SBarry Smith 
2812264ace61SBarry Smith   Collective on DM
2813264ace61SBarry Smith 
2814264ace61SBarry Smith   Input Parameters:
2815264ace61SBarry Smith + dm     - The DM object
2816264ace61SBarry Smith - method - The name of the DM type
2817264ace61SBarry Smith 
2818264ace61SBarry Smith   Options Database Key:
2819264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2820264ace61SBarry Smith 
2821264ace61SBarry Smith   Notes:
2822e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2823264ace61SBarry Smith 
2824264ace61SBarry Smith   Level: intermediate
2825264ace61SBarry Smith 
2826264ace61SBarry Smith .keywords: DM, set, type
2827264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2828264ace61SBarry Smith @*/
282919fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2830264ace61SBarry Smith {
2831264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2832264ace61SBarry Smith   PetscBool      match;
2833264ace61SBarry Smith   PetscErrorCode ierr;
2834264ace61SBarry Smith 
2835264ace61SBarry Smith   PetscFunctionBegin;
2836264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2837251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2838264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2839264ace61SBarry Smith 
28400f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
28411c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2842ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2843264ace61SBarry Smith 
2844264ace61SBarry Smith   if (dm->ops->destroy) {
2845264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
28460298fd71SBarry Smith     dm->ops->destroy = NULL;
2847264ace61SBarry Smith   }
2848264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2849264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2850264ace61SBarry Smith   PetscFunctionReturn(0);
2851264ace61SBarry Smith }
2852264ace61SBarry Smith 
2853264ace61SBarry Smith #undef __FUNCT__
2854264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2855264ace61SBarry Smith /*@C
2856264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2857264ace61SBarry Smith 
2858264ace61SBarry Smith   Not Collective
2859264ace61SBarry Smith 
2860264ace61SBarry Smith   Input Parameter:
2861264ace61SBarry Smith . dm  - The DM
2862264ace61SBarry Smith 
2863264ace61SBarry Smith   Output Parameter:
2864264ace61SBarry Smith . type - The DM type name
2865264ace61SBarry Smith 
2866264ace61SBarry Smith   Level: intermediate
2867264ace61SBarry Smith 
2868264ace61SBarry Smith .keywords: DM, get, type, name
2869264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2870264ace61SBarry Smith @*/
287119fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2872264ace61SBarry Smith {
2873264ace61SBarry Smith   PetscErrorCode ierr;
2874264ace61SBarry Smith 
2875264ace61SBarry Smith   PetscFunctionBegin;
2876264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2877c959eef4SJed Brown   PetscValidPointer(type,2);
2878607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2879264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2880264ace61SBarry Smith   PetscFunctionReturn(0);
2881264ace61SBarry Smith }
2882264ace61SBarry Smith 
288367a56275SMatthew G Knepley #undef __FUNCT__
288467a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
288567a56275SMatthew G Knepley /*@C
288667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
288767a56275SMatthew G Knepley 
288867a56275SMatthew G Knepley   Collective on DM
288967a56275SMatthew G Knepley 
289067a56275SMatthew G Knepley   Input Parameters:
289167a56275SMatthew G Knepley + dm - the DM
289267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
289367a56275SMatthew G Knepley 
289467a56275SMatthew G Knepley   Output Parameter:
289567a56275SMatthew G Knepley . M - pointer to new DM
289667a56275SMatthew G Knepley 
289767a56275SMatthew G Knepley   Notes:
289867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
289967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
290067a56275SMatthew G Knepley   of the input DM.
290167a56275SMatthew G Knepley 
290267a56275SMatthew G Knepley   Level: intermediate
290367a56275SMatthew G Knepley 
290467a56275SMatthew G Knepley .seealso: DMCreate()
290567a56275SMatthew G Knepley @*/
290619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
290767a56275SMatthew G Knepley {
290867a56275SMatthew G Knepley   DM             B;
290967a56275SMatthew G Knepley   char           convname[256];
291067a56275SMatthew G Knepley   PetscBool      sametype, issame;
291167a56275SMatthew G Knepley   PetscErrorCode ierr;
291267a56275SMatthew G Knepley 
291367a56275SMatthew G Knepley   PetscFunctionBegin;
291467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
291567a56275SMatthew G Knepley   PetscValidType(dm,1);
291667a56275SMatthew G Knepley   PetscValidPointer(M,3);
2917251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
291867a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
291967a56275SMatthew G Knepley   {
29200298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
292167a56275SMatthew G Knepley 
292267a56275SMatthew G Knepley     /*
292367a56275SMatthew G Knepley        Order of precedence:
292467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
292567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
292667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
292767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
292867a56275SMatthew G Knepley        5) Use a really basic converter.
292967a56275SMatthew G Knepley     */
293067a56275SMatthew G Knepley 
293167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
293267a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
293367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
293467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
293567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
293667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29370005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
293867a56275SMatthew G Knepley     if (conv) goto foundconv;
293967a56275SMatthew G Knepley 
294067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
294182f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
294267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
294367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
294467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
294567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
294667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
294767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29480005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
294967a56275SMatthew G Knepley     if (conv) {
2950fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
295167a56275SMatthew G Knepley       goto foundconv;
295267a56275SMatthew G Knepley     }
295367a56275SMatthew G Knepley 
295467a56275SMatthew G Knepley #if 0
295567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
295667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2957fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
295867a56275SMatthew G Knepley     if (conv) goto foundconv;
295967a56275SMatthew G Knepley 
296067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
296167a56275SMatthew G Knepley     if (dm->ops->convert) {
296267a56275SMatthew G Knepley       conv = dm->ops->convert;
296367a56275SMatthew G Knepley     }
296467a56275SMatthew G Knepley     if (conv) goto foundconv;
296567a56275SMatthew G Knepley #endif
296667a56275SMatthew G Knepley 
296767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
296882f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
296967a56275SMatthew G Knepley 
297067a56275SMatthew G Knepley foundconv:
297167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
297267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
297367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
297467a56275SMatthew G Knepley   }
297567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
297667a56275SMatthew G Knepley   PetscFunctionReturn(0);
297767a56275SMatthew G Knepley }
2978264ace61SBarry Smith 
2979264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2980264ace61SBarry Smith 
2981264ace61SBarry Smith #undef __FUNCT__
2982264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2983264ace61SBarry Smith /*@C
29841c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
29851c84c290SBarry Smith 
29861c84c290SBarry Smith   Not Collective
29871c84c290SBarry Smith 
29881c84c290SBarry Smith   Input Parameters:
29891c84c290SBarry Smith + name        - The name of a new user-defined creation routine
29901c84c290SBarry Smith - create_func - The creation routine itself
29911c84c290SBarry Smith 
29921c84c290SBarry Smith   Notes:
29931c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
29941c84c290SBarry Smith 
29951c84c290SBarry Smith 
29961c84c290SBarry Smith   Sample usage:
29971c84c290SBarry Smith .vb
2998bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
29991c84c290SBarry Smith .ve
30001c84c290SBarry Smith 
30011c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
30021c84c290SBarry Smith .vb
30031c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
30041c84c290SBarry Smith     DMSetType(DM,"my_da");
30051c84c290SBarry Smith .ve
30061c84c290SBarry Smith    or at runtime via the option
30071c84c290SBarry Smith .vb
30081c84c290SBarry Smith     -da_type my_da
30091c84c290SBarry Smith .ve
3010264ace61SBarry Smith 
3011264ace61SBarry Smith   Level: advanced
30121c84c290SBarry Smith 
30131c84c290SBarry Smith .keywords: DM, register
3014bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
30151c84c290SBarry Smith 
3016264ace61SBarry Smith @*/
3017bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3018264ace61SBarry Smith {
3019264ace61SBarry Smith   PetscErrorCode ierr;
3020264ace61SBarry Smith 
3021264ace61SBarry Smith   PetscFunctionBegin;
3022a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3023264ace61SBarry Smith   PetscFunctionReturn(0);
3024264ace61SBarry Smith }
3025264ace61SBarry Smith 
3026b859378eSBarry Smith #undef __FUNCT__
3027b859378eSBarry Smith #define __FUNCT__ "DMLoad"
3028b859378eSBarry Smith /*@C
302955849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3030b859378eSBarry Smith 
3031b859378eSBarry Smith   Collective on PetscViewer
3032b859378eSBarry Smith 
3033b859378eSBarry Smith   Input Parameters:
3034b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3035b859378eSBarry Smith            some related function before a call to DMLoad().
3036b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3037b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3038b859378eSBarry Smith 
3039b859378eSBarry Smith    Level: intermediate
3040b859378eSBarry Smith 
3041b859378eSBarry Smith   Notes:
304255849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3043b859378eSBarry Smith 
3044b859378eSBarry Smith   Notes for advanced users:
3045b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3046b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3047b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3048b859378eSBarry Smith   format is
3049b859378eSBarry Smith .vb
3050b859378eSBarry Smith      has not yet been determined
3051b859378eSBarry Smith .ve
3052b859378eSBarry Smith 
3053b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3054b859378eSBarry Smith @*/
3055b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3056b859378eSBarry Smith {
30579331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3058b859378eSBarry Smith   PetscErrorCode ierr;
3059b859378eSBarry Smith 
3060b859378eSBarry Smith   PetscFunctionBegin;
3061b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3062b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
306332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
30649331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
30659331c7a4SMatthew G. Knepley   if (isbinary) {
30669331c7a4SMatthew G. Knepley     PetscInt classid;
30679331c7a4SMatthew G. Knepley     char     type[256];
3068b859378eSBarry Smith 
3069060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
30709200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3071060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
307232c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
30739331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30749331c7a4SMatthew G. Knepley   } else if (ishdf5) {
30759331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30769331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3077b859378eSBarry Smith   PetscFunctionReturn(0);
3078b859378eSBarry Smith }
3079b859378eSBarry Smith 
30807da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30817da65231SMatthew G Knepley 
30827da65231SMatthew G Knepley #undef __FUNCT__
30837da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3084a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3085a6dfd86eSKarl Rupp {
30861d47ebbbSSatish Balay   PetscInt       f;
30871b30c384SMatthew G Knepley   PetscErrorCode ierr;
30881b30c384SMatthew G Knepley 
30897da65231SMatthew G Knepley   PetscFunctionBegin;
309074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30911d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
309257622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
30937da65231SMatthew G Knepley   }
30947da65231SMatthew G Knepley   PetscFunctionReturn(0);
30957da65231SMatthew G Knepley }
30967da65231SMatthew G Knepley 
30977da65231SMatthew G Knepley #undef __FUNCT__
30987da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3099a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3100a6dfd86eSKarl Rupp {
31011b30c384SMatthew G Knepley   PetscInt       f, g;
31027da65231SMatthew G Knepley   PetscErrorCode ierr;
31037da65231SMatthew G Knepley 
31047da65231SMatthew G Knepley   PetscFunctionBegin;
310574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
31061d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
310774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
31081d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3109e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
31107da65231SMatthew G Knepley     }
311174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
31127da65231SMatthew G Knepley   }
31137da65231SMatthew G Knepley   PetscFunctionReturn(0);
31147da65231SMatthew G Knepley }
3115e7c4fc90SDmitry Karpeev 
3116970e74d5SMatthew G Knepley #undef __FUNCT__
3117e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
31186113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3119e759306cSMatthew G. Knepley {
3120e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3121e759306cSMatthew G. Knepley   PetscInt       p;
3122e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3123e759306cSMatthew G. Knepley 
3124e759306cSMatthew G. Knepley   PetscFunctionBegin;
3125e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3126e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3127e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3128e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3129e759306cSMatthew G. Knepley     if (p == rank) {
3130e759306cSMatthew G. Knepley       Vec x;
3131e759306cSMatthew G. Knepley 
3132e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3133e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
31346113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3135e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3136e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3137e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3138e759306cSMatthew G. Knepley     }
3139e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3140e759306cSMatthew G. Knepley   }
3141e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3142e759306cSMatthew G. Knepley }
3143e759306cSMatthew G. Knepley 
3144e759306cSMatthew G. Knepley #undef __FUNCT__
314588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
314688ed4aceSMatthew G Knepley /*@
314788ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
314888ed4aceSMatthew G Knepley 
314988ed4aceSMatthew G Knepley   Input Parameter:
315088ed4aceSMatthew G Knepley . dm - The DM
315188ed4aceSMatthew G Knepley 
315288ed4aceSMatthew G Knepley   Output Parameter:
315388ed4aceSMatthew G Knepley . section - The PetscSection
315488ed4aceSMatthew G Knepley 
315588ed4aceSMatthew G Knepley   Level: intermediate
315688ed4aceSMatthew G Knepley 
315788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
315888ed4aceSMatthew G Knepley 
315988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
316088ed4aceSMatthew G Knepley @*/
31610adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
31620adebc6cSBarry Smith {
3163fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3164fd59a867SMatthew G. Knepley 
316588ed4aceSMatthew G Knepley   PetscFunctionBegin;
316688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
316788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
31682f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
31692f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3170685405a1SBarry Smith     ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);
31712f0f8703SMatthew G. Knepley   }
317288ed4aceSMatthew G Knepley   *section = dm->defaultSection;
317388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
317488ed4aceSMatthew G Knepley }
317588ed4aceSMatthew G Knepley 
317688ed4aceSMatthew G Knepley #undef __FUNCT__
317788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
317888ed4aceSMatthew G Knepley /*@
317988ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
318088ed4aceSMatthew G Knepley 
318188ed4aceSMatthew G Knepley   Input Parameters:
318288ed4aceSMatthew G Knepley + dm - The DM
318388ed4aceSMatthew G Knepley - section - The PetscSection
318488ed4aceSMatthew G Knepley 
318588ed4aceSMatthew G Knepley   Level: intermediate
318688ed4aceSMatthew G Knepley 
318788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
318888ed4aceSMatthew G Knepley 
318988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
319088ed4aceSMatthew G Knepley @*/
31910adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
31920adebc6cSBarry Smith {
3193c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3194af122d2aSMatthew G Knepley   PetscInt       f;
319588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
319688ed4aceSMatthew G Knepley 
319788ed4aceSMatthew G Knepley   PetscFunctionBegin;
319888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3199c473ab19SMatthew G. Knepley   if (section) {
32001d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
32011d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3202c473ab19SMatthew G. Knepley   }
320388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
320488ed4aceSMatthew G Knepley   dm->defaultSection = section;
3205c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3206af122d2aSMatthew G Knepley   if (numFields) {
3207af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3208af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
32090f21e855SMatthew G. Knepley       PetscObject disc;
3210af122d2aSMatthew G Knepley       const char *name;
3211af122d2aSMatthew G Knepley 
3212af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
32130f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
32140f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3215af122d2aSMatthew G Knepley     }
3216af122d2aSMatthew G Knepley   }
32171d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
32181d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
321988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
322088ed4aceSMatthew G Knepley }
322188ed4aceSMatthew G Knepley 
322288ed4aceSMatthew G Knepley #undef __FUNCT__
32239435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
32249435951eSToby Isaac /*@
32259435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
32269435951eSToby Isaac 
3227e228b242SToby Isaac   not collective
3228e228b242SToby Isaac 
32299435951eSToby Isaac   Input Parameter:
32309435951eSToby Isaac . dm - The DM
32319435951eSToby Isaac 
32329435951eSToby Isaac   Output Parameter:
32339435951eSToby 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.
32349435951eSToby 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.
32359435951eSToby Isaac 
32369435951eSToby Isaac   Level: advanced
32379435951eSToby Isaac 
32389435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
32399435951eSToby Isaac 
32409435951eSToby Isaac .seealso: DMSetDefaultConstraints()
32419435951eSToby Isaac @*/
32429435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
32439435951eSToby Isaac {
32449435951eSToby Isaac   PetscErrorCode ierr;
32459435951eSToby Isaac 
32469435951eSToby Isaac   PetscFunctionBegin;
32479435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32489435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
324945a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
325045a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
32519435951eSToby Isaac   PetscFunctionReturn(0);
32529435951eSToby Isaac }
32539435951eSToby Isaac 
32549435951eSToby Isaac #undef __FUNCT__
32559435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
32569435951eSToby Isaac /*@
32579435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
32589435951eSToby Isaac 
32599435951eSToby 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().
32609435951eSToby Isaac 
32619435951eSToby 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.
32629435951eSToby Isaac 
3263e228b242SToby Isaac   collective on dm
3264e228b242SToby Isaac 
32659435951eSToby Isaac   Input Parameters:
32669435951eSToby Isaac + dm - The DM
3267e228b242SToby 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).
3268e228b242SToby 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).
32699435951eSToby Isaac 
32709435951eSToby Isaac   Level: advanced
32719435951eSToby Isaac 
32729435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32739435951eSToby Isaac 
32749435951eSToby Isaac .seealso: DMGetDefaultConstraints()
32759435951eSToby Isaac @*/
32769435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
32779435951eSToby Isaac {
3278e228b242SToby Isaac   PetscMPIInt result;
32799435951eSToby Isaac   PetscErrorCode ierr;
32809435951eSToby Isaac 
32819435951eSToby Isaac   PetscFunctionBegin;
32829435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3283e228b242SToby Isaac   if (section) {
3284e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3285e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3286e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3287e228b242SToby Isaac   }
3288e228b242SToby Isaac   if (mat) {
3289e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3290e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3291e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3292e228b242SToby Isaac   }
32939435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
32949435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
32959435951eSToby Isaac   dm->defaultConstraintSection = section;
32969435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
32979435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
32989435951eSToby Isaac   dm->defaultConstraintMat = mat;
32999435951eSToby Isaac   PetscFunctionReturn(0);
33009435951eSToby Isaac }
33019435951eSToby Isaac 
3302f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
33039435951eSToby Isaac #undef __FUNCT__
3304284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3305507e4973SMatthew G. Knepley /*
3306507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3307507e4973SMatthew G. Knepley 
3308507e4973SMatthew G. Knepley   Input Parameters:
3309507e4973SMatthew G. Knepley + dm - The DM
3310507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3311507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3312507e4973SMatthew G. Knepley 
3313507e4973SMatthew G. Knepley   Level: intermediate
3314507e4973SMatthew G. Knepley 
3315507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3316507e4973SMatthew G. Knepley */
3317f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3318507e4973SMatthew G. Knepley {
3319507e4973SMatthew G. Knepley   MPI_Comm        comm;
3320507e4973SMatthew G. Knepley   PetscLayout     layout;
3321507e4973SMatthew G. Knepley   const PetscInt *ranges;
3322507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3323507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3324507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3325507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3326507e4973SMatthew G. Knepley 
3327507e4973SMatthew G. Knepley   PetscFunctionBegin;
3328507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3329507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3330507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3331507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3332507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3333507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3334507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3335507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3336507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3337507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3338507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3339507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3340f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3341507e4973SMatthew G. Knepley 
3342507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3343507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3344507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3345507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3346507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3347507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3348507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3349507e4973SMatthew 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;}
3350507e4973SMatthew 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;}
3351507e4973SMatthew G. Knepley     if (gdof < 0) {
3352507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3353507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3354507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3355507e4973SMatthew G. Knepley 
3356507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3357507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3358507e4973SMatthew 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;}
3359507e4973SMatthew G. Knepley       }
3360507e4973SMatthew G. Knepley     }
3361507e4973SMatthew G. Knepley   }
3362507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3363507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3364b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3365507e4973SMatthew G. Knepley   if (!gvalid) {
3366507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3367507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3368507e4973SMatthew G. Knepley   }
3369507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3370507e4973SMatthew G. Knepley }
3371f741bcd2SMatthew G. Knepley #endif
3372507e4973SMatthew G. Knepley 
3373507e4973SMatthew G. Knepley #undef __FUNCT__
337488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
337588ed4aceSMatthew G Knepley /*@
337688ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
337788ed4aceSMatthew G Knepley 
33788b1ab98fSJed Brown   Collective on DM
33798b1ab98fSJed Brown 
338088ed4aceSMatthew G Knepley   Input Parameter:
338188ed4aceSMatthew G Knepley . dm - The DM
338288ed4aceSMatthew G Knepley 
338388ed4aceSMatthew G Knepley   Output Parameter:
338488ed4aceSMatthew G Knepley . section - The PetscSection
338588ed4aceSMatthew G Knepley 
338688ed4aceSMatthew G Knepley   Level: intermediate
338788ed4aceSMatthew G Knepley 
338888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
338988ed4aceSMatthew G Knepley 
339088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
339188ed4aceSMatthew G Knepley @*/
33920adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
33930adebc6cSBarry Smith {
339488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
339588ed4aceSMatthew G Knepley 
339688ed4aceSMatthew G Knepley   PetscFunctionBegin;
339788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
339888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
339988ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3400fd59a867SMatthew G. Knepley     PetscSection s;
3401fd59a867SMatthew G. Knepley 
3402fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3403fd59a867SMatthew 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");
3404fd59a867SMatthew G. Knepley     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSF in order to create a global PetscSection");
340515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3406cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3407ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3408685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
340988ed4aceSMatthew G Knepley   }
341088ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
341188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
341288ed4aceSMatthew G Knepley }
341388ed4aceSMatthew G Knepley 
341488ed4aceSMatthew G Knepley #undef __FUNCT__
3415b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3416b21d0597SMatthew G Knepley /*@
3417b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3418b21d0597SMatthew G Knepley 
3419b21d0597SMatthew G Knepley   Input Parameters:
3420b21d0597SMatthew G Knepley + dm - The DM
34215080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3422b21d0597SMatthew G Knepley 
3423b21d0597SMatthew G Knepley   Level: intermediate
3424b21d0597SMatthew G Knepley 
3425b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3426b21d0597SMatthew G Knepley 
3427b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3428b21d0597SMatthew G Knepley @*/
34290adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
34300adebc6cSBarry Smith {
3431b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3432b21d0597SMatthew G Knepley 
3433b21d0597SMatthew G Knepley   PetscFunctionBegin;
3434b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34355080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34361d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3437b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3438b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3439507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3440f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3441507e4973SMatthew G. Knepley #endif
3442b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3443b21d0597SMatthew G Knepley }
3444b21d0597SMatthew G Knepley 
3445b21d0597SMatthew G Knepley #undef __FUNCT__
344688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
344788ed4aceSMatthew G Knepley /*@
344888ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
344988ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
345088ed4aceSMatthew G Knepley 
345188ed4aceSMatthew G Knepley   Input Parameter:
345288ed4aceSMatthew G Knepley . dm - The DM
345388ed4aceSMatthew G Knepley 
345488ed4aceSMatthew G Knepley   Output Parameter:
345588ed4aceSMatthew G Knepley . sf - The PetscSF
345688ed4aceSMatthew G Knepley 
345788ed4aceSMatthew G Knepley   Level: intermediate
345888ed4aceSMatthew G Knepley 
345988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
346088ed4aceSMatthew G Knepley 
346188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
346288ed4aceSMatthew G Knepley @*/
34630adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
34640adebc6cSBarry Smith {
346588ed4aceSMatthew G Knepley   PetscInt       nroots;
346688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
346788ed4aceSMatthew G Knepley 
346888ed4aceSMatthew G Knepley   PetscFunctionBegin;
346988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
347088ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34710298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
347288ed4aceSMatthew G Knepley   if (nroots < 0) {
347388ed4aceSMatthew G Knepley     PetscSection section, gSection;
347488ed4aceSMatthew G Knepley 
347588ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
347631ea6d37SMatthew G Knepley     if (section) {
347788ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
347888ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
347931ea6d37SMatthew G Knepley     } else {
34800298fd71SBarry Smith       *sf = NULL;
348131ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
348231ea6d37SMatthew G Knepley     }
348388ed4aceSMatthew G Knepley   }
348488ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
348588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
348688ed4aceSMatthew G Knepley }
348788ed4aceSMatthew G Knepley 
348888ed4aceSMatthew G Knepley #undef __FUNCT__
348988ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
349088ed4aceSMatthew G Knepley /*@
349188ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
349288ed4aceSMatthew G Knepley 
349388ed4aceSMatthew G Knepley   Input Parameters:
349488ed4aceSMatthew G Knepley + dm - The DM
349588ed4aceSMatthew G Knepley - sf - The PetscSF
349688ed4aceSMatthew G Knepley 
349788ed4aceSMatthew G Knepley   Level: intermediate
349888ed4aceSMatthew G Knepley 
349988ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
350088ed4aceSMatthew G Knepley 
350188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
350288ed4aceSMatthew G Knepley @*/
35030adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
35040adebc6cSBarry Smith {
350588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
350688ed4aceSMatthew G Knepley 
350788ed4aceSMatthew G Knepley   PetscFunctionBegin;
350888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
350988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
351088ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
351188ed4aceSMatthew G Knepley   dm->defaultSF = sf;
351288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
351388ed4aceSMatthew G Knepley }
351488ed4aceSMatthew G Knepley 
351588ed4aceSMatthew G Knepley #undef __FUNCT__
351688ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
351788ed4aceSMatthew G Knepley /*@C
351888ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
351988ed4aceSMatthew G Knepley   describing the data layout.
352088ed4aceSMatthew G Knepley 
352188ed4aceSMatthew G Knepley   Input Parameters:
352288ed4aceSMatthew G Knepley + dm - The DM
352388ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
352488ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
352588ed4aceSMatthew G Knepley 
352688ed4aceSMatthew G Knepley   Level: intermediate
352788ed4aceSMatthew G Knepley 
352888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
352988ed4aceSMatthew G Knepley @*/
353088ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
353188ed4aceSMatthew G Knepley {
353282f516ccSBarry Smith   MPI_Comm       comm;
353388ed4aceSMatthew G Knepley   PetscLayout    layout;
353488ed4aceSMatthew G Knepley   const PetscInt *ranges;
353588ed4aceSMatthew G Knepley   PetscInt       *local;
353688ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3537ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
353888ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
353988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
354088ed4aceSMatthew G Knepley 
354188ed4aceSMatthew G Knepley   PetscFunctionBegin;
354282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
354388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
354488ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
354588ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
354688ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
354788ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
354888ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
354988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
355088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
355188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
355288ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3553ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35546636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
355588ed4aceSMatthew G Knepley 
35566636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35576636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3558235fbf56SMatthew 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));
35596636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
356088ed4aceSMatthew G Knepley   }
3561785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3562785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
356388ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
35641f588964SMatthew G Knepley     const PetscInt *cind;
35656636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
356688ed4aceSMatthew G Knepley 
356788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
356888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
356988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
357088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
357188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35726636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
357388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35746636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35756636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35766636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3577057b4bcdSMatthew 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);
35786636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35796636e97aSMatthew G Knepley     }
358088ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
358188ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
358288ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
358388ed4aceSMatthew G Knepley     }
358488ed4aceSMatthew G Knepley     if (gdof < 0) {
35856636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
358688ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
358788ed4aceSMatthew G Knepley 
358805376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
358931d3f06eSJed Brown         if (r < 0) r = -(r+2);
359005376888SMatthew 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);
359188ed4aceSMatthew G Knepley         remote[l].rank  = r;
359288ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
359388ed4aceSMatthew G Knepley       }
359488ed4aceSMatthew G Knepley     } else {
35956636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
359688ed4aceSMatthew G Knepley         remote[l].rank  = rank;
359788ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
359888ed4aceSMatthew G Knepley       }
359988ed4aceSMatthew G Knepley     }
360088ed4aceSMatthew G Knepley   }
36016636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
360288ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
360388ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
360488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
360588ed4aceSMatthew G Knepley }
3606af122d2aSMatthew G Knepley 
3607af122d2aSMatthew G Knepley #undef __FUNCT__
3608b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3609b21d0597SMatthew G Knepley /*@
3610b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3611b21d0597SMatthew G Knepley 
3612b21d0597SMatthew G Knepley   Input Parameter:
3613b21d0597SMatthew G Knepley . dm - The DM
3614b21d0597SMatthew G Knepley 
3615b21d0597SMatthew G Knepley   Output Parameter:
3616b21d0597SMatthew G Knepley . sf - The PetscSF
3617b21d0597SMatthew G Knepley 
3618b21d0597SMatthew G Knepley   Level: intermediate
3619b21d0597SMatthew G Knepley 
3620b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3621b21d0597SMatthew G Knepley 
3622057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3623b21d0597SMatthew G Knepley @*/
36240adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
36250adebc6cSBarry Smith {
3626b21d0597SMatthew G Knepley   PetscFunctionBegin;
3627b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3628b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3629b21d0597SMatthew G Knepley   *sf = dm->sf;
3630b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3631b21d0597SMatthew G Knepley }
3632b21d0597SMatthew G Knepley 
3633b21d0597SMatthew G Knepley #undef __FUNCT__
3634057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3635057b4bcdSMatthew G Knepley /*@
3636057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3637057b4bcdSMatthew G Knepley 
3638057b4bcdSMatthew G Knepley   Input Parameters:
3639057b4bcdSMatthew G Knepley + dm - The DM
3640057b4bcdSMatthew G Knepley - sf - The PetscSF
3641057b4bcdSMatthew G Knepley 
3642057b4bcdSMatthew G Knepley   Level: intermediate
3643057b4bcdSMatthew G Knepley 
3644057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3645057b4bcdSMatthew G Knepley @*/
36460adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
36470adebc6cSBarry Smith {
3648057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3649057b4bcdSMatthew G Knepley 
3650057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3651057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3652057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3653057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3654057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3655057b4bcdSMatthew G Knepley   dm->sf = sf;
3656057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3657057b4bcdSMatthew G Knepley }
3658057b4bcdSMatthew G Knepley 
3659057b4bcdSMatthew G Knepley #undef __FUNCT__
36602764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
36612764a2aaSMatthew G. Knepley /*@
36622764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
36632764a2aaSMatthew G. Knepley 
36642764a2aaSMatthew G. Knepley   Input Parameter:
36652764a2aaSMatthew G. Knepley . dm - The DM
36662764a2aaSMatthew G. Knepley 
36672764a2aaSMatthew G. Knepley   Output Parameter:
36682764a2aaSMatthew G. Knepley . prob - The PetscDS
36692764a2aaSMatthew G. Knepley 
36702764a2aaSMatthew G. Knepley   Level: developer
36712764a2aaSMatthew G. Knepley 
36722764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36732764a2aaSMatthew G. Knepley @*/
36742764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3675af122d2aSMatthew G Knepley {
3676af122d2aSMatthew G Knepley   PetscFunctionBegin;
3677af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36780f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36790f21e855SMatthew G. Knepley   *prob = dm->prob;
36800f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36810f21e855SMatthew G. Knepley }
36820f21e855SMatthew G. Knepley 
36830f21e855SMatthew G. Knepley #undef __FUNCT__
36842764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
36852764a2aaSMatthew G. Knepley /*@
36862764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36872764a2aaSMatthew G. Knepley 
36882764a2aaSMatthew G. Knepley   Input Parameters:
36892764a2aaSMatthew G. Knepley + dm - The DM
36902764a2aaSMatthew G. Knepley - prob - The PetscDS
36912764a2aaSMatthew G. Knepley 
36922764a2aaSMatthew G. Knepley   Level: developer
36932764a2aaSMatthew G. Knepley 
36942764a2aaSMatthew G. Knepley .seealso: DMGetDS()
36952764a2aaSMatthew G. Knepley @*/
36962764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
36970f21e855SMatthew G. Knepley {
36980f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36990f21e855SMatthew G. Knepley 
37000f21e855SMatthew G. Knepley   PetscFunctionBegin;
37010f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37022764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
37032764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
37040f21e855SMatthew G. Knepley   dm->prob = prob;
37050f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
37060f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
37070f21e855SMatthew G. Knepley }
37080f21e855SMatthew G. Knepley 
37090f21e855SMatthew G. Knepley #undef __FUNCT__
37100f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
37110f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
37120f21e855SMatthew G. Knepley {
37130f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37140f21e855SMatthew G. Knepley 
37150f21e855SMatthew G. Knepley   PetscFunctionBegin;
37160f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37172764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3718af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3719af122d2aSMatthew G Knepley }
3720af122d2aSMatthew G Knepley 
3721af122d2aSMatthew G Knepley #undef __FUNCT__
3722af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3723af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3724af122d2aSMatthew G Knepley {
37250f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3726af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3727af122d2aSMatthew G Knepley 
3728af122d2aSMatthew G Knepley   PetscFunctionBegin;
3729af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37302764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
37310f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
37320f21e855SMatthew G. Knepley     PetscContainer obj;
37330f21e855SMatthew G. Knepley 
37340f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
37352764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
37360f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3737af122d2aSMatthew G Knepley   }
3738af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3739af122d2aSMatthew G Knepley }
3740af122d2aSMatthew G Knepley 
3741af122d2aSMatthew G Knepley #undef __FUNCT__
3742af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3743c1929be8SMatthew G. Knepley /*@
3744c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3745c1929be8SMatthew G. Knepley 
3746c1929be8SMatthew G. Knepley   Not collective
3747c1929be8SMatthew G. Knepley 
3748c1929be8SMatthew G. Knepley   Input Parameters:
3749c1929be8SMatthew G. Knepley + dm - The DM
3750c1929be8SMatthew G. Knepley - f  - The field number
3751c1929be8SMatthew G. Knepley 
3752c1929be8SMatthew G. Knepley   Output Parameter:
3753c1929be8SMatthew G. Knepley . field - The discretization object
3754c1929be8SMatthew G. Knepley 
3755c1929be8SMatthew G. Knepley   Level: developer
3756c1929be8SMatthew G. Knepley 
3757c1929be8SMatthew G. Knepley .seealso: DMSetField()
3758c1929be8SMatthew G. Knepley @*/
3759af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3760af122d2aSMatthew G Knepley {
37610f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37620f21e855SMatthew G. Knepley 
3763af122d2aSMatthew G Knepley   PetscFunctionBegin;
3764af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37652764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3766decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3767decb47aaSMatthew G. Knepley }
3768decb47aaSMatthew G. Knepley 
3769decb47aaSMatthew G. Knepley #undef __FUNCT__
3770decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3771c1929be8SMatthew G. Knepley /*@
3772c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3773c1929be8SMatthew G. Knepley 
3774c1929be8SMatthew G. Knepley   Logically collective on DM
3775c1929be8SMatthew G. Knepley 
3776c1929be8SMatthew G. Knepley   Input Parameters:
3777c1929be8SMatthew G. Knepley + dm - The DM
3778c1929be8SMatthew G. Knepley . f  - The field number
3779c1929be8SMatthew G. Knepley - field - The discretization object
3780c1929be8SMatthew G. Knepley 
3781c1929be8SMatthew G. Knepley   Level: developer
3782c1929be8SMatthew G. Knepley 
3783c1929be8SMatthew G. Knepley .seealso: DMGetField()
3784c1929be8SMatthew G. Knepley @*/
3785decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3786decb47aaSMatthew G. Knepley {
3787decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3788decb47aaSMatthew G. Knepley 
3789decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3790decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37912764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3792af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3793af122d2aSMatthew G Knepley }
37946636e97aSMatthew G Knepley 
37956636e97aSMatthew G Knepley #undef __FUNCT__
3796b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3797b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3798b64e0483SPeter Brune {
3799b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3800b64e0483SPeter Brune   PetscErrorCode ierr;
3801b64e0483SPeter Brune   Vec coords,ccoords;
38026dbf9973SLawrence Mitchell   Mat inject;
3803b64e0483SPeter Brune   PetscFunctionBegin;
3804b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3805b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3806b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3807b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3808b64e0483SPeter Brune   if (coords && !ccoords) {
3809b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
38106dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
38112adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
38126dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3813b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3814b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3815b64e0483SPeter Brune   }
3816b64e0483SPeter Brune   PetscFunctionReturn(0);
3817b64e0483SPeter Brune }
3818b64e0483SPeter Brune 
3819b64e0483SPeter Brune #undef __FUNCT__
382003dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
382103dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
382203dadc2fSPeter Brune {
382303dadc2fSPeter Brune   DM dm_coord,subdm_coord;
382403dadc2fSPeter Brune   PetscErrorCode ierr;
382503dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
382603dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
382703dadc2fSPeter Brune   PetscFunctionBegin;
382803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
382903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
383003dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
383103dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
383203dadc2fSPeter Brune   if (coords && !ccoords) {
383303dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
383403dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
383524640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
383603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
383703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
383803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
383903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
384203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
384303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
384403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
384503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
384603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
384703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
384803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
384903dadc2fSPeter Brune   }
385003dadc2fSPeter Brune   PetscFunctionReturn(0);
385103dadc2fSPeter Brune }
385203dadc2fSPeter Brune 
385303dadc2fSPeter Brune #undef __FUNCT__
3854c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3855c73cfb54SMatthew G. Knepley /*@
3856c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3857c73cfb54SMatthew G. Knepley 
3858c73cfb54SMatthew G. Knepley   Not collective
3859c73cfb54SMatthew G. Knepley 
3860c73cfb54SMatthew G. Knepley   Input Parameter:
3861c73cfb54SMatthew G. Knepley . dm - The DM
3862c73cfb54SMatthew G. Knepley 
3863c73cfb54SMatthew G. Knepley   Output Parameter:
3864c73cfb54SMatthew G. Knepley . dim - The topological dimension
3865c73cfb54SMatthew G. Knepley 
3866c73cfb54SMatthew G. Knepley   Level: beginner
3867c73cfb54SMatthew G. Knepley 
3868c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3869c73cfb54SMatthew G. Knepley @*/
3870c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3871c73cfb54SMatthew G. Knepley {
3872c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3873c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3874c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3875c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3876c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3877c73cfb54SMatthew G. Knepley }
3878c73cfb54SMatthew G. Knepley 
3879c73cfb54SMatthew G. Knepley #undef __FUNCT__
3880c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3881c73cfb54SMatthew G. Knepley /*@
3882c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3883c73cfb54SMatthew G. Knepley 
3884c73cfb54SMatthew G. Knepley   Collective on dm
3885c73cfb54SMatthew G. Knepley 
3886c73cfb54SMatthew G. Knepley   Input Parameters:
3887c73cfb54SMatthew G. Knepley + dm - The DM
3888c73cfb54SMatthew G. Knepley - dim - The topological dimension
3889c73cfb54SMatthew G. Knepley 
3890c73cfb54SMatthew G. Knepley   Level: beginner
3891c73cfb54SMatthew G. Knepley 
3892c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3893c73cfb54SMatthew G. Knepley @*/
3894c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3895c73cfb54SMatthew G. Knepley {
3896c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3897c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3898c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3899c73cfb54SMatthew G. Knepley   dm->dim = dim;
3900c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3901c73cfb54SMatthew G. Knepley }
3902c73cfb54SMatthew G. Knepley 
3903793f3fe5SMatthew G. Knepley #undef __FUNCT__
3904793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3905793f3fe5SMatthew G. Knepley /*@
3906793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3907793f3fe5SMatthew G. Knepley 
3908793f3fe5SMatthew G. Knepley   Collective on DM
3909793f3fe5SMatthew G. Knepley 
3910793f3fe5SMatthew G. Knepley   Input Parameters:
3911793f3fe5SMatthew G. Knepley + dm - the DM
3912793f3fe5SMatthew G. Knepley - dim - the dimension
3913793f3fe5SMatthew G. Knepley 
3914793f3fe5SMatthew G. Knepley   Output Parameters:
3915793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3916793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3917793f3fe5SMatthew G. Knepley 
3918793f3fe5SMatthew G. Knepley   Note:
3919793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3920793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3921793f3fe5SMatthew G. Knepley   then the interval is empty.
3922793f3fe5SMatthew G. Knepley 
3923793f3fe5SMatthew G. Knepley   Level: intermediate
3924793f3fe5SMatthew G. Knepley 
3925793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3926793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3927793f3fe5SMatthew G. Knepley @*/
3928793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3929793f3fe5SMatthew G. Knepley {
3930793f3fe5SMatthew G. Knepley   PetscInt       d;
3931793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3932793f3fe5SMatthew G. Knepley 
3933793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3934793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3935793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3936793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3937793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3938793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3939793f3fe5SMatthew G. Knepley }
3940793f3fe5SMatthew G. Knepley 
3941793f3fe5SMatthew G. Knepley #undef __FUNCT__
39426636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
39436636e97aSMatthew G Knepley /*@
39446636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
39456636e97aSMatthew G Knepley 
39466636e97aSMatthew G Knepley   Collective on DM
39476636e97aSMatthew G Knepley 
39486636e97aSMatthew G Knepley   Input Parameters:
39496636e97aSMatthew G Knepley + dm - the DM
39506636e97aSMatthew G Knepley - c - coordinate vector
39516636e97aSMatthew G Knepley 
39526636e97aSMatthew G Knepley   Note:
39536636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
39546636e97aSMatthew G Knepley 
39556636e97aSMatthew G Knepley   Level: intermediate
39566636e97aSMatthew G Knepley 
39576636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39586636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
39596636e97aSMatthew G Knepley @*/
39606636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
39616636e97aSMatthew G Knepley {
39626636e97aSMatthew G Knepley   PetscErrorCode ierr;
39636636e97aSMatthew G Knepley 
39646636e97aSMatthew G Knepley   PetscFunctionBegin;
39656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39676636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39686636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39696636e97aSMatthew G Knepley   dm->coordinates = c;
39706636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3971b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
397203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39736636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39746636e97aSMatthew G Knepley }
39756636e97aSMatthew G Knepley 
39766636e97aSMatthew G Knepley #undef __FUNCT__
39776636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
39786636e97aSMatthew G Knepley /*@
39796636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39806636e97aSMatthew G Knepley 
39816636e97aSMatthew G Knepley   Collective on DM
39826636e97aSMatthew G Knepley 
39836636e97aSMatthew G Knepley    Input Parameters:
39846636e97aSMatthew G Knepley +  dm - the DM
39856636e97aSMatthew G Knepley -  c - coordinate vector
39866636e97aSMatthew G Knepley 
39876636e97aSMatthew G Knepley   Note:
39886636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39896636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39906636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39916636e97aSMatthew G Knepley 
39926636e97aSMatthew G Knepley   Level: intermediate
39936636e97aSMatthew G Knepley 
39946636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39956636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
39966636e97aSMatthew G Knepley @*/
39976636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
39986636e97aSMatthew G Knepley {
39996636e97aSMatthew G Knepley   PetscErrorCode ierr;
40006636e97aSMatthew G Knepley 
40016636e97aSMatthew G Knepley   PetscFunctionBegin;
40026636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40036636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
40046636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
40056636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
40068865f1eaSKarl Rupp 
40076636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
40088865f1eaSKarl Rupp 
40096636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
40106636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40116636e97aSMatthew G Knepley }
40126636e97aSMatthew G Knepley 
40136636e97aSMatthew G Knepley #undef __FUNCT__
40146636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
40156636e97aSMatthew G Knepley /*@
40166636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
40176636e97aSMatthew G Knepley 
40186636e97aSMatthew G Knepley   Not Collective
40196636e97aSMatthew G Knepley 
40206636e97aSMatthew G Knepley   Input Parameter:
40216636e97aSMatthew G Knepley . dm - the DM
40226636e97aSMatthew G Knepley 
40236636e97aSMatthew G Knepley   Output Parameter:
40246636e97aSMatthew G Knepley . c - global coordinate vector
40256636e97aSMatthew G Knepley 
40266636e97aSMatthew G Knepley   Note:
40276636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40286636e97aSMatthew G Knepley 
40296636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
40306636e97aSMatthew G Knepley 
40316636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40326636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40336636e97aSMatthew G Knepley 
40346636e97aSMatthew G Knepley   Level: intermediate
40356636e97aSMatthew G Knepley 
40366636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40376636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
40386636e97aSMatthew G Knepley @*/
40396636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
40406636e97aSMatthew G Knepley {
40416636e97aSMatthew G Knepley   PetscErrorCode ierr;
40426636e97aSMatthew G Knepley 
40436636e97aSMatthew G Knepley   PetscFunctionBegin;
40446636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40456636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40461f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
40470298fd71SBarry Smith     DM cdm = NULL;
40486636e97aSMatthew G Knepley 
40496636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40506636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
40516636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
40526636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40536636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40546636e97aSMatthew G Knepley   }
40556636e97aSMatthew G Knepley   *c = dm->coordinates;
40566636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40576636e97aSMatthew G Knepley }
40586636e97aSMatthew G Knepley 
40596636e97aSMatthew G Knepley #undef __FUNCT__
40606636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
40616636e97aSMatthew G Knepley /*@
40626636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
40636636e97aSMatthew G Knepley 
40646636e97aSMatthew G Knepley   Collective on DM
40656636e97aSMatthew G Knepley 
40666636e97aSMatthew G Knepley   Input Parameter:
40676636e97aSMatthew G Knepley . dm - the DM
40686636e97aSMatthew G Knepley 
40696636e97aSMatthew G Knepley   Output Parameter:
40706636e97aSMatthew G Knepley . c - coordinate vector
40716636e97aSMatthew G Knepley 
40726636e97aSMatthew G Knepley   Note:
40736636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40746636e97aSMatthew G Knepley 
40756636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40766636e97aSMatthew G Knepley 
40776636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40786636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40796636e97aSMatthew G Knepley 
40806636e97aSMatthew G Knepley   Level: intermediate
40816636e97aSMatthew G Knepley 
40826636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40836636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40846636e97aSMatthew G Knepley @*/
40856636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40866636e97aSMatthew G Knepley {
40876636e97aSMatthew G Knepley   PetscErrorCode ierr;
40886636e97aSMatthew G Knepley 
40896636e97aSMatthew G Knepley   PetscFunctionBegin;
40906636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40916636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40921f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40930298fd71SBarry Smith     DM cdm = NULL;
40946636e97aSMatthew G Knepley 
40956636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40966636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
40976636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
40986636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40996636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41006636e97aSMatthew G Knepley   }
41016636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
41026636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41036636e97aSMatthew G Knepley }
41046636e97aSMatthew G Knepley 
41056636e97aSMatthew G Knepley #undef __FUNCT__
41066636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
41076636e97aSMatthew G Knepley /*@
41081cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
41096636e97aSMatthew G Knepley 
41106636e97aSMatthew G Knepley   Collective on DM
41116636e97aSMatthew G Knepley 
41126636e97aSMatthew G Knepley   Input Parameter:
41136636e97aSMatthew G Knepley . dm - the DM
41146636e97aSMatthew G Knepley 
41156636e97aSMatthew G Knepley   Output Parameter:
41166636e97aSMatthew G Knepley . cdm - coordinate DM
41176636e97aSMatthew G Knepley 
41186636e97aSMatthew G Knepley   Level: intermediate
41196636e97aSMatthew G Knepley 
41206636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41211cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41226636e97aSMatthew G Knepley @*/
41236636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
41246636e97aSMatthew G Knepley {
41256636e97aSMatthew G Knepley   PetscErrorCode ierr;
41266636e97aSMatthew G Knepley 
41276636e97aSMatthew G Knepley   PetscFunctionBegin;
41286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41296636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
41306636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
413182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
41326636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
41336636e97aSMatthew G Knepley   }
41346636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
41356636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41366636e97aSMatthew G Knepley }
4137e87bb0d3SMatthew G Knepley 
4138e87bb0d3SMatthew G Knepley #undef __FUNCT__
41391cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
41401cfe2091SMatthew G. Knepley /*@
41411cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
41421cfe2091SMatthew G. Knepley 
41431cfe2091SMatthew G. Knepley   Logically Collective on DM
41441cfe2091SMatthew G. Knepley 
41451cfe2091SMatthew G. Knepley   Input Parameters:
41461cfe2091SMatthew G. Knepley + dm - the DM
41471cfe2091SMatthew G. Knepley - cdm - coordinate DM
41481cfe2091SMatthew G. Knepley 
41491cfe2091SMatthew G. Knepley   Level: intermediate
41501cfe2091SMatthew G. Knepley 
41511cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41521cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41531cfe2091SMatthew G. Knepley @*/
41541cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
41551cfe2091SMatthew G. Knepley {
41561cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
41571cfe2091SMatthew G. Knepley 
41581cfe2091SMatthew G. Knepley   PetscFunctionBegin;
41591cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41601cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
41611cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
41621cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
41631cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
41641cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
41651cfe2091SMatthew G. Knepley }
41661cfe2091SMatthew G. Knepley 
41671cfe2091SMatthew G. Knepley #undef __FUNCT__
416846e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
416946e270d4SMatthew G. Knepley /*@
417046e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
417146e270d4SMatthew G. Knepley 
417246e270d4SMatthew G. Knepley   Not Collective
417346e270d4SMatthew G. Knepley 
417446e270d4SMatthew G. Knepley   Input Parameter:
417546e270d4SMatthew G. Knepley . dm - The DM object
417646e270d4SMatthew G. Knepley 
417746e270d4SMatthew G. Knepley   Output Parameter:
417846e270d4SMatthew G. Knepley . dim - The embedding dimension
417946e270d4SMatthew G. Knepley 
418046e270d4SMatthew G. Knepley   Level: intermediate
418146e270d4SMatthew G. Knepley 
418246e270d4SMatthew G. Knepley .keywords: mesh, coordinates
418346e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
418446e270d4SMatthew G. Knepley @*/
418546e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
418646e270d4SMatthew G. Knepley {
418746e270d4SMatthew G. Knepley   PetscFunctionBegin;
418846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
418946e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41909a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41919a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41929a9a41abSToby Isaac   }
419346e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
419446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
419546e270d4SMatthew G. Knepley }
419646e270d4SMatthew G. Knepley 
419746e270d4SMatthew G. Knepley #undef __FUNCT__
419846e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
419946e270d4SMatthew G. Knepley /*@
420046e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
420146e270d4SMatthew G. Knepley 
420246e270d4SMatthew G. Knepley   Not Collective
420346e270d4SMatthew G. Knepley 
420446e270d4SMatthew G. Knepley   Input Parameters:
420546e270d4SMatthew G. Knepley + dm  - The DM object
420646e270d4SMatthew G. Knepley - dim - The embedding dimension
420746e270d4SMatthew G. Knepley 
420846e270d4SMatthew G. Knepley   Level: intermediate
420946e270d4SMatthew G. Knepley 
421046e270d4SMatthew G. Knepley .keywords: mesh, coordinates
421146e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
421246e270d4SMatthew G. Knepley @*/
421346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
421446e270d4SMatthew G. Knepley {
421546e270d4SMatthew G. Knepley   PetscFunctionBegin;
421646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
421746e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
421846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
421946e270d4SMatthew G. Knepley }
422046e270d4SMatthew G. Knepley 
422146e270d4SMatthew G. Knepley #undef __FUNCT__
4222e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4223e8abe2deSMatthew G. Knepley /*@
4224e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4225e8abe2deSMatthew G. Knepley 
4226e8abe2deSMatthew G. Knepley   Not Collective
4227e8abe2deSMatthew G. Knepley 
4228e8abe2deSMatthew G. Knepley   Input Parameter:
4229e8abe2deSMatthew G. Knepley . dm - The DM object
4230e8abe2deSMatthew G. Knepley 
4231e8abe2deSMatthew G. Knepley   Output Parameter:
4232e8abe2deSMatthew G. Knepley . section - The PetscSection object
4233e8abe2deSMatthew G. Knepley 
4234e8abe2deSMatthew G. Knepley   Level: intermediate
4235e8abe2deSMatthew G. Knepley 
4236e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4237e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4238e8abe2deSMatthew G. Knepley @*/
4239e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4240e8abe2deSMatthew G. Knepley {
4241e8abe2deSMatthew G. Knepley   DM             cdm;
4242e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4243e8abe2deSMatthew G. Knepley 
4244e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4245e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4246e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4247e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4248e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4249e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4250e8abe2deSMatthew G. Knepley }
4251e8abe2deSMatthew G. Knepley 
4252e8abe2deSMatthew G. Knepley #undef __FUNCT__
4253e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4254e8abe2deSMatthew G. Knepley /*@
4255e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4256e8abe2deSMatthew G. Knepley 
4257e8abe2deSMatthew G. Knepley   Not Collective
4258e8abe2deSMatthew G. Knepley 
4259e8abe2deSMatthew G. Knepley   Input Parameters:
4260e8abe2deSMatthew G. Knepley + dm      - The DM object
426146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4262e8abe2deSMatthew G. Knepley - section - The PetscSection object
4263e8abe2deSMatthew G. Knepley 
4264e8abe2deSMatthew G. Knepley   Level: intermediate
4265e8abe2deSMatthew G. Knepley 
4266e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4267e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4268e8abe2deSMatthew G. Knepley @*/
426946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4270e8abe2deSMatthew G. Knepley {
4271e8abe2deSMatthew G. Knepley   DM             cdm;
4272e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4273e8abe2deSMatthew G. Knepley 
4274e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4275e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
427646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4277e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4278e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
427946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
428046e270d4SMatthew G. Knepley     PetscInt d = dim;
428146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
428246e270d4SMatthew G. Knepley 
428346e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
428446e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
428546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
428646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
428746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
428846e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
428946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
429046e270d4SMatthew G. Knepley     }
429146e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
429246e270d4SMatthew G. Knepley   }
4293e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4294e8abe2deSMatthew G. Knepley }
4295e8abe2deSMatthew G. Knepley 
4296e8abe2deSMatthew G. Knepley #undef __FUNCT__
4297c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
42985dc8c3f7SMatthew G. Knepley /*@C
42995dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
43005dc8c3f7SMatthew G. Knepley 
43015dc8c3f7SMatthew G. Knepley   Input Parameters:
43025dc8c3f7SMatthew G. Knepley + dm      - The DM object
43035dc8c3f7SMatthew 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
43045dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43055dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43065dc8c3f7SMatthew G. Knepley 
43075dc8c3f7SMatthew G. Knepley   Level: developer
43085dc8c3f7SMatthew G. Knepley 
43095dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43105dc8c3f7SMatthew G. Knepley @*/
43115dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4312c6b900c6SMatthew G. Knepley {
4313c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4314c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4315c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4316c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
43175dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4318c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4319c6b900c6SMatthew G. Knepley }
4320c6b900c6SMatthew G. Knepley 
4321c6b900c6SMatthew G. Knepley #undef __FUNCT__
4322c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
43235dc8c3f7SMatthew G. Knepley /*@C
43245dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
43255dc8c3f7SMatthew G. Knepley 
43265dc8c3f7SMatthew G. Knepley   Input Parameters:
43275dc8c3f7SMatthew G. Knepley + dm      - The DM object
43285dc8c3f7SMatthew 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
43295dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43305dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43315dc8c3f7SMatthew G. Knepley 
43325dc8c3f7SMatthew G. Knepley   Level: developer
43335dc8c3f7SMatthew G. Knepley 
43345dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43355dc8c3f7SMatthew G. Knepley @*/
43365dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4337c6b900c6SMatthew G. Knepley {
4338c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4339c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4340c6b900c6SMatthew G. Knepley 
4341c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4342c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43435dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
43445dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
43455dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
43465dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
43475dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4348c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4349c6b900c6SMatthew G. Knepley }
4350c6b900c6SMatthew G. Knepley 
4351c6b900c6SMatthew G. Knepley #undef __FUNCT__
4352e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4353e87bb0d3SMatthew G Knepley /*@
4354e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4355e87bb0d3SMatthew G Knepley 
4356e87bb0d3SMatthew G Knepley   Not collective
4357e87bb0d3SMatthew G Knepley 
4358e87bb0d3SMatthew G Knepley   Input Parameters:
4359e87bb0d3SMatthew G Knepley + dm - The DM
4360e87bb0d3SMatthew G Knepley - v - The Vec of points
4361e87bb0d3SMatthew G Knepley 
436261e3bb9bSMatthew G Knepley   Output Parameter:
4363e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4364e87bb0d3SMatthew G Knepley 
4365e87bb0d3SMatthew G Knepley   Level: developer
436661e3bb9bSMatthew G Knepley 
436761e3bb9bSMatthew G Knepley .keywords: point location, mesh
436861e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
436961e3bb9bSMatthew G Knepley @*/
4370e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4371e87bb0d3SMatthew G Knepley {
4372735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4373735aa83eSMatthew G Knepley 
4374e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4375e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4376e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4377e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
437847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4379735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4380735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
438182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
438247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4383e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4384e87bb0d3SMatthew G Knepley }
438514f150ffSMatthew G. Knepley 
438614f150ffSMatthew G. Knepley #undef __FUNCT__
438714f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4388f4d763aaSMatthew G. Knepley /*@
4389f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4390f4d763aaSMatthew G. Knepley 
4391f4d763aaSMatthew G. Knepley   Input Parameter:
4392f4d763aaSMatthew G. Knepley . dm - The original DM
4393f4d763aaSMatthew G. Knepley 
4394f4d763aaSMatthew G. Knepley   Output Parameter:
4395f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4396f4d763aaSMatthew G. Knepley 
4397f4d763aaSMatthew G. Knepley   Level: intermediate
4398f4d763aaSMatthew G. Knepley 
4399f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4400f4d763aaSMatthew G. Knepley @*/
440114f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
440214f150ffSMatthew G. Knepley {
4403c26acbdeSMatthew G. Knepley   PetscSection   section;
4404c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
440514f150ffSMatthew G. Knepley   PetscErrorCode ierr;
440614f150ffSMatthew G. Knepley 
440714f150ffSMatthew G. Knepley   PetscFunctionBegin;
440814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
440914f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4410c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4411c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4412c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4413c26acbdeSMatthew G. Knepley     *odm = dm;
4414c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4415c26acbdeSMatthew G. Knepley   }
441614f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4417c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
441814f150ffSMatthew G. Knepley     PetscSF      sf;
441914f150ffSMatthew G. Knepley 
442014f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
442114f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
442214f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
442314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
442414f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
442515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
442614f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
442714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
442814f150ffSMatthew G. Knepley   }
442914f150ffSMatthew G. Knepley   *odm = dm->dmBC;
443014f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
443114f150ffSMatthew G. Knepley }
4432f4d763aaSMatthew G. Knepley 
4433f4d763aaSMatthew G. Knepley #undef __FUNCT__
4434f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4435f4d763aaSMatthew G. Knepley /*@
4436cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4437f4d763aaSMatthew G. Knepley 
4438f4d763aaSMatthew G. Knepley   Input Parameter:
4439f4d763aaSMatthew G. Knepley . dm - The original DM
4440f4d763aaSMatthew G. Knepley 
4441cdb7a50dSMatthew G. Knepley   Output Parameters:
4442cdb7a50dSMatthew G. Knepley + num - The output sequence number
4443cdb7a50dSMatthew G. Knepley - val - The output sequence value
4444f4d763aaSMatthew G. Knepley 
4445f4d763aaSMatthew G. Knepley   Level: intermediate
4446f4d763aaSMatthew G. Knepley 
4447f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4448f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4449f4d763aaSMatthew G. Knepley 
4450f4d763aaSMatthew G. Knepley .seealso: VecView()
4451f4d763aaSMatthew G. Knepley @*/
4452cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4453f4d763aaSMatthew G. Knepley {
4454f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4455f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4456cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4457cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4458f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4459f4d763aaSMatthew G. Knepley }
4460f4d763aaSMatthew G. Knepley 
4461f4d763aaSMatthew G. Knepley #undef __FUNCT__
4462f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4463f4d763aaSMatthew G. Knepley /*@
4464cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4465f4d763aaSMatthew G. Knepley 
4466f4d763aaSMatthew G. Knepley   Input Parameters:
4467f4d763aaSMatthew G. Knepley + dm - The original DM
4468cdb7a50dSMatthew G. Knepley . num - The output sequence number
4469cdb7a50dSMatthew G. Knepley - val - The output sequence value
4470f4d763aaSMatthew G. Knepley 
4471f4d763aaSMatthew G. Knepley   Level: intermediate
4472f4d763aaSMatthew G. Knepley 
4473f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4474f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4475f4d763aaSMatthew G. Knepley 
4476f4d763aaSMatthew G. Knepley .seealso: VecView()
4477f4d763aaSMatthew G. Knepley @*/
4478cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4479f4d763aaSMatthew G. Knepley {
4480f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4481f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4482f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4483cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4484cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4485cdb7a50dSMatthew G. Knepley }
4486cdb7a50dSMatthew G. Knepley 
4487cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4488cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4489cdb7a50dSMatthew G. Knepley /*@C
4490cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4491cdb7a50dSMatthew G. Knepley 
4492cdb7a50dSMatthew G. Knepley   Input Parameters:
4493cdb7a50dSMatthew G. Knepley + dm   - The original DM
4494cdb7a50dSMatthew G. Knepley . name - The sequence name
4495cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4496cdb7a50dSMatthew G. Knepley 
4497cdb7a50dSMatthew G. Knepley   Output Parameter:
4498cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4499cdb7a50dSMatthew G. Knepley 
4500cdb7a50dSMatthew G. Knepley   Level: intermediate
4501cdb7a50dSMatthew G. Knepley 
4502cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4503cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4504cdb7a50dSMatthew G. Knepley 
4505cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4506cdb7a50dSMatthew G. Knepley @*/
4507cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4508cdb7a50dSMatthew G. Knepley {
4509cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4510cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4511cdb7a50dSMatthew G. Knepley 
4512cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4513cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4514cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4515cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4516cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4517cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4518cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4519cdb7a50dSMatthew G. Knepley     PetscScalar value;
4520cdb7a50dSMatthew G. Knepley 
4521cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
45224aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4523cdb7a50dSMatthew G. Knepley #endif
4524cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4525f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4526f4d763aaSMatthew G. Knepley }
45278e4ac7eaSMatthew G. Knepley 
45288e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45298e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
45308e4ac7eaSMatthew G. Knepley /*@
45318e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
45328e4ac7eaSMatthew G. Knepley 
45338e4ac7eaSMatthew G. Knepley   Not collective
45348e4ac7eaSMatthew G. Knepley 
45358e4ac7eaSMatthew G. Knepley   Input Parameter:
45368e4ac7eaSMatthew G. Knepley . dm - The DM
45378e4ac7eaSMatthew G. Knepley 
45388e4ac7eaSMatthew G. Knepley   Output Parameter:
45398e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
45408e4ac7eaSMatthew G. Knepley 
45418e4ac7eaSMatthew G. Knepley   Level: beginner
45428e4ac7eaSMatthew G. Knepley 
45438e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
45448e4ac7eaSMatthew G. Knepley @*/
45458e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
45468e4ac7eaSMatthew G. Knepley {
45478e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45488e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45498e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
45508e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
45518e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45528e4ac7eaSMatthew G. Knepley }
45538e4ac7eaSMatthew G. Knepley 
45548e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45558e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
45568e4ac7eaSMatthew G. Knepley /*@
45578e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
45588e4ac7eaSMatthew G. Knepley 
45598e4ac7eaSMatthew G. Knepley   Collective on dm
45608e4ac7eaSMatthew G. Knepley 
45618e4ac7eaSMatthew G. Knepley   Input Parameters:
45628e4ac7eaSMatthew G. Knepley + dm - The DM
45638e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
45648e4ac7eaSMatthew G. Knepley 
45658e4ac7eaSMatthew G. Knepley   Level: beginner
45668e4ac7eaSMatthew G. Knepley 
45678e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
45688e4ac7eaSMatthew G. Knepley @*/
45698e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
45708e4ac7eaSMatthew G. Knepley {
45718e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45728e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45738e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
45748e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
45758e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45768e4ac7eaSMatthew G. Knepley }
4577c58f1c22SToby Isaac 
4578c58f1c22SToby Isaac #undef __FUNCT__
4579c58f1c22SToby Isaac #define __FUNCT__
4580c58f1c22SToby Isaac 
4581c58f1c22SToby Isaac #undef __FUNCT__
4582c58f1c22SToby Isaac #define __FUNCT__ "DMCreateLabel"
4583c58f1c22SToby Isaac /*@C
4584c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
4585c58f1c22SToby Isaac 
4586c58f1c22SToby Isaac   Not Collective
4587c58f1c22SToby Isaac 
4588c58f1c22SToby Isaac   Input Parameters:
4589c58f1c22SToby Isaac + dm   - The DM object
4590c58f1c22SToby Isaac - name - The label name
4591c58f1c22SToby Isaac 
4592c58f1c22SToby Isaac   Level: intermediate
4593c58f1c22SToby Isaac 
4594c58f1c22SToby Isaac .keywords: mesh
4595c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4596c58f1c22SToby Isaac @*/
4597c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
4598c58f1c22SToby Isaac {
4599c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
4600c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
4601c58f1c22SToby Isaac   PetscErrorCode ierr;
4602c58f1c22SToby Isaac 
4603c58f1c22SToby Isaac   PetscFunctionBegin;
4604c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4605c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4606c58f1c22SToby Isaac   while (next) {
4607c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
4608c58f1c22SToby Isaac     if (flg) break;
4609c58f1c22SToby Isaac     next = next->next;
4610c58f1c22SToby Isaac   }
4611c58f1c22SToby Isaac   if (!flg) {
4612c58f1c22SToby Isaac     DMLabelLink tmpLabel;
4613c58f1c22SToby Isaac 
4614c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
4615c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
4616c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
4617c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
4618c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
4619c58f1c22SToby Isaac   }
4620c58f1c22SToby Isaac   PetscFunctionReturn(0);
4621c58f1c22SToby Isaac }
4622c58f1c22SToby Isaac 
4623c58f1c22SToby Isaac #undef __FUNCT__
4624c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelValue"
4625c58f1c22SToby Isaac /*@C
4626c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
4627c58f1c22SToby Isaac 
4628c58f1c22SToby Isaac   Not Collective
4629c58f1c22SToby Isaac 
4630c58f1c22SToby Isaac   Input Parameters:
4631c58f1c22SToby Isaac + dm   - The DM object
4632c58f1c22SToby Isaac . name - The label name
4633c58f1c22SToby Isaac - point - The mesh point
4634c58f1c22SToby Isaac 
4635c58f1c22SToby Isaac   Output Parameter:
4636c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
4637c58f1c22SToby Isaac 
4638c58f1c22SToby Isaac   Level: beginner
4639c58f1c22SToby Isaac 
4640c58f1c22SToby Isaac .keywords: mesh
4641c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
4642c58f1c22SToby Isaac @*/
4643c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
4644c58f1c22SToby Isaac {
4645c58f1c22SToby Isaac   DMLabel        label;
4646c58f1c22SToby Isaac   PetscErrorCode ierr;
4647c58f1c22SToby Isaac 
4648c58f1c22SToby Isaac   PetscFunctionBegin;
4649c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4650c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4651c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4652c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
4653c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
4654c58f1c22SToby Isaac   PetscFunctionReturn(0);
4655c58f1c22SToby Isaac }
4656c58f1c22SToby Isaac 
4657c58f1c22SToby Isaac #undef __FUNCT__
4658c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelValue"
4659c58f1c22SToby Isaac /*@C
4660c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
4661c58f1c22SToby Isaac 
4662c58f1c22SToby Isaac   Not Collective
4663c58f1c22SToby Isaac 
4664c58f1c22SToby Isaac   Input Parameters:
4665c58f1c22SToby Isaac + dm   - The DM object
4666c58f1c22SToby Isaac . name - The label name
4667c58f1c22SToby Isaac . point - The mesh point
4668c58f1c22SToby Isaac - value - The label value for this point
4669c58f1c22SToby Isaac 
4670c58f1c22SToby Isaac   Output Parameter:
4671c58f1c22SToby Isaac 
4672c58f1c22SToby Isaac   Level: beginner
4673c58f1c22SToby Isaac 
4674c58f1c22SToby Isaac .keywords: mesh
4675c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
4676c58f1c22SToby Isaac @*/
4677c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4678c58f1c22SToby Isaac {
4679c58f1c22SToby Isaac   DMLabel        label;
4680c58f1c22SToby Isaac   PetscErrorCode ierr;
4681c58f1c22SToby Isaac 
4682c58f1c22SToby Isaac   PetscFunctionBegin;
4683c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4684c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4685c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4686c58f1c22SToby Isaac   if (!label) {
4687c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
4688c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4689c58f1c22SToby Isaac   }
4690c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
4691c58f1c22SToby Isaac   PetscFunctionReturn(0);
4692c58f1c22SToby Isaac }
4693c58f1c22SToby Isaac 
4694c58f1c22SToby Isaac #undef __FUNCT__
4695c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelValue"
4696c58f1c22SToby Isaac /*@C
4697c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
4698c58f1c22SToby Isaac 
4699c58f1c22SToby Isaac   Not Collective
4700c58f1c22SToby Isaac 
4701c58f1c22SToby Isaac   Input Parameters:
4702c58f1c22SToby Isaac + dm   - The DM object
4703c58f1c22SToby Isaac . name - The label name
4704c58f1c22SToby Isaac . point - The mesh point
4705c58f1c22SToby Isaac - value - The label value for this point
4706c58f1c22SToby Isaac 
4707c58f1c22SToby Isaac   Output Parameter:
4708c58f1c22SToby Isaac 
4709c58f1c22SToby Isaac   Level: beginner
4710c58f1c22SToby Isaac 
4711c58f1c22SToby Isaac .keywords: mesh
4712c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
4713c58f1c22SToby Isaac @*/
4714c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4715c58f1c22SToby Isaac {
4716c58f1c22SToby Isaac   DMLabel        label;
4717c58f1c22SToby Isaac   PetscErrorCode ierr;
4718c58f1c22SToby Isaac 
4719c58f1c22SToby Isaac   PetscFunctionBegin;
4720c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4721c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4722c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4723c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4724c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
4725c58f1c22SToby Isaac   PetscFunctionReturn(0);
4726c58f1c22SToby Isaac }
4727c58f1c22SToby Isaac 
4728c58f1c22SToby Isaac #undef __FUNCT__
4729c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelSize"
4730c58f1c22SToby Isaac /*@C
4731c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
4732c58f1c22SToby Isaac 
4733c58f1c22SToby Isaac   Not Collective
4734c58f1c22SToby Isaac 
4735c58f1c22SToby Isaac   Input Parameters:
4736c58f1c22SToby Isaac + dm   - The DM object
4737c58f1c22SToby Isaac - name - The label name
4738c58f1c22SToby Isaac 
4739c58f1c22SToby Isaac   Output Parameter:
4740c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
4741c58f1c22SToby Isaac 
4742c58f1c22SToby Isaac   Level: beginner
4743c58f1c22SToby Isaac 
4744c58f1c22SToby Isaac .keywords: mesh
4745c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
4746c58f1c22SToby Isaac @*/
4747c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
4748c58f1c22SToby Isaac {
4749c58f1c22SToby Isaac   DMLabel        label;
4750c58f1c22SToby Isaac   PetscErrorCode ierr;
4751c58f1c22SToby Isaac 
4752c58f1c22SToby Isaac   PetscFunctionBegin;
4753c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4754c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4755c58f1c22SToby Isaac   PetscValidPointer(size, 3);
4756c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4757c58f1c22SToby Isaac   *size = 0;
4758c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4759c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
4760c58f1c22SToby Isaac   PetscFunctionReturn(0);
4761c58f1c22SToby Isaac }
4762c58f1c22SToby Isaac 
4763c58f1c22SToby Isaac #undef __FUNCT__
4764c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelIdIS"
4765c58f1c22SToby Isaac /*@C
4766c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
4767c58f1c22SToby Isaac 
4768c58f1c22SToby Isaac   Not Collective
4769c58f1c22SToby Isaac 
4770c58f1c22SToby Isaac   Input Parameters:
4771c58f1c22SToby Isaac + mesh - The DM object
4772c58f1c22SToby Isaac - name - The label name
4773c58f1c22SToby Isaac 
4774c58f1c22SToby Isaac   Output Parameter:
4775c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
4776c58f1c22SToby Isaac 
4777c58f1c22SToby Isaac   Level: beginner
4778c58f1c22SToby Isaac 
4779c58f1c22SToby Isaac .keywords: mesh
4780c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
4781c58f1c22SToby Isaac @*/
4782c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
4783c58f1c22SToby Isaac {
4784c58f1c22SToby Isaac   DMLabel        label;
4785c58f1c22SToby Isaac   PetscErrorCode ierr;
4786c58f1c22SToby Isaac 
4787c58f1c22SToby Isaac   PetscFunctionBegin;
4788c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4789c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4790c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
4791c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4792c58f1c22SToby Isaac   *ids = NULL;
4793c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4794c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
4795c58f1c22SToby Isaac   PetscFunctionReturn(0);
4796c58f1c22SToby Isaac }
4797c58f1c22SToby Isaac 
4798c58f1c22SToby Isaac #undef __FUNCT__
4799c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumSize"
4800c58f1c22SToby Isaac /*@C
4801c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
4802c58f1c22SToby Isaac 
4803c58f1c22SToby Isaac   Not Collective
4804c58f1c22SToby Isaac 
4805c58f1c22SToby Isaac   Input Parameters:
4806c58f1c22SToby Isaac + dm - The DM object
4807c58f1c22SToby Isaac . name - The label name
4808c58f1c22SToby Isaac - value - The stratum value
4809c58f1c22SToby Isaac 
4810c58f1c22SToby Isaac   Output Parameter:
4811c58f1c22SToby Isaac . size - The stratum size
4812c58f1c22SToby Isaac 
4813c58f1c22SToby Isaac   Level: beginner
4814c58f1c22SToby Isaac 
4815c58f1c22SToby Isaac .keywords: mesh
4816c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
4817c58f1c22SToby Isaac @*/
4818c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
4819c58f1c22SToby Isaac {
4820c58f1c22SToby Isaac   DMLabel        label;
4821c58f1c22SToby Isaac   PetscErrorCode ierr;
4822c58f1c22SToby Isaac 
4823c58f1c22SToby Isaac   PetscFunctionBegin;
4824c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4825c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4826c58f1c22SToby Isaac   PetscValidPointer(size, 4);
4827c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4828c58f1c22SToby Isaac   *size = 0;
4829c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4830c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
4831c58f1c22SToby Isaac   PetscFunctionReturn(0);
4832c58f1c22SToby Isaac }
4833c58f1c22SToby Isaac 
4834c58f1c22SToby Isaac #undef __FUNCT__
4835c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumIS"
4836c58f1c22SToby Isaac /*@C
4837c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
4838c58f1c22SToby Isaac 
4839c58f1c22SToby Isaac   Not Collective
4840c58f1c22SToby Isaac 
4841c58f1c22SToby Isaac   Input Parameters:
4842c58f1c22SToby Isaac + dm - The DM object
4843c58f1c22SToby Isaac . name - The label name
4844c58f1c22SToby Isaac - value - The stratum value
4845c58f1c22SToby Isaac 
4846c58f1c22SToby Isaac   Output Parameter:
4847c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
4848c58f1c22SToby Isaac 
4849c58f1c22SToby Isaac   Level: beginner
4850c58f1c22SToby Isaac 
4851c58f1c22SToby Isaac .keywords: mesh
4852c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
4853c58f1c22SToby Isaac @*/
4854c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
4855c58f1c22SToby Isaac {
4856c58f1c22SToby Isaac   DMLabel        label;
4857c58f1c22SToby Isaac   PetscErrorCode ierr;
4858c58f1c22SToby Isaac 
4859c58f1c22SToby Isaac   PetscFunctionBegin;
4860c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4861c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4862c58f1c22SToby Isaac   PetscValidPointer(points, 4);
4863c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4864c58f1c22SToby Isaac   *points = NULL;
4865c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4866c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
4867c58f1c22SToby Isaac   PetscFunctionReturn(0);
4868c58f1c22SToby Isaac }
4869c58f1c22SToby Isaac 
4870c58f1c22SToby Isaac #undef __FUNCT__
4871c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelStratum"
4872c58f1c22SToby Isaac /*@C
4873c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
4874c58f1c22SToby Isaac 
4875c58f1c22SToby Isaac   Not Collective
4876c58f1c22SToby Isaac 
4877c58f1c22SToby Isaac   Input Parameters:
4878c58f1c22SToby Isaac + dm   - The DM object
4879c58f1c22SToby Isaac . name - The label name
4880c58f1c22SToby Isaac - value - The label value for this point
4881c58f1c22SToby Isaac 
4882c58f1c22SToby Isaac   Output Parameter:
4883c58f1c22SToby Isaac 
4884c58f1c22SToby Isaac   Level: beginner
4885c58f1c22SToby Isaac 
4886c58f1c22SToby Isaac .keywords: mesh
4887c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
4888c58f1c22SToby Isaac @*/
4889c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
4890c58f1c22SToby Isaac {
4891c58f1c22SToby Isaac   DMLabel        label;
4892c58f1c22SToby Isaac   PetscErrorCode ierr;
4893c58f1c22SToby Isaac 
4894c58f1c22SToby Isaac   PetscFunctionBegin;
4895c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4896c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4897c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4898c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4899c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
4900c58f1c22SToby Isaac   PetscFunctionReturn(0);
4901c58f1c22SToby Isaac }
4902c58f1c22SToby Isaac 
4903c58f1c22SToby Isaac #undef __FUNCT__
4904c58f1c22SToby Isaac #define __FUNCT__ "DMGetNumLabels"
4905c58f1c22SToby Isaac /*@
4906c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
4907c58f1c22SToby Isaac 
4908c58f1c22SToby Isaac   Not Collective
4909c58f1c22SToby Isaac 
4910c58f1c22SToby Isaac   Input Parameter:
4911c58f1c22SToby Isaac . dm   - The DM object
4912c58f1c22SToby Isaac 
4913c58f1c22SToby Isaac   Output Parameter:
4914c58f1c22SToby Isaac . numLabels - the number of Labels
4915c58f1c22SToby Isaac 
4916c58f1c22SToby Isaac   Level: intermediate
4917c58f1c22SToby Isaac 
4918c58f1c22SToby Isaac .keywords: mesh
4919c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4920c58f1c22SToby Isaac @*/
4921c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
4922c58f1c22SToby Isaac {
4923c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
4924c58f1c22SToby Isaac   PetscInt  n    = 0;
4925c58f1c22SToby Isaac 
4926c58f1c22SToby Isaac   PetscFunctionBegin;
4927c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4928c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
4929c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
4930c58f1c22SToby Isaac   *numLabels = n;
4931c58f1c22SToby Isaac   PetscFunctionReturn(0);
4932c58f1c22SToby Isaac }
4933c58f1c22SToby Isaac 
4934c58f1c22SToby Isaac #undef __FUNCT__
4935c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelName"
4936c58f1c22SToby Isaac /*@C
4937c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
4938c58f1c22SToby Isaac 
4939c58f1c22SToby Isaac   Not Collective
4940c58f1c22SToby Isaac 
4941c58f1c22SToby Isaac   Input Parameters:
4942c58f1c22SToby Isaac + dm - The DM object
4943c58f1c22SToby Isaac - n  - the label number
4944c58f1c22SToby Isaac 
4945c58f1c22SToby Isaac   Output Parameter:
4946c58f1c22SToby Isaac . name - the label name
4947c58f1c22SToby Isaac 
4948c58f1c22SToby Isaac   Level: intermediate
4949c58f1c22SToby Isaac 
4950c58f1c22SToby Isaac .keywords: mesh
4951c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4952c58f1c22SToby Isaac @*/
4953c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
4954c58f1c22SToby Isaac {
4955c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
4956c58f1c22SToby Isaac   PetscInt  l    = 0;
4957c58f1c22SToby Isaac 
4958c58f1c22SToby Isaac   PetscFunctionBegin;
4959c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4960c58f1c22SToby Isaac   PetscValidPointer(name, 3);
4961c58f1c22SToby Isaac   while (next) {
4962c58f1c22SToby Isaac     if (l == n) {
4963c58f1c22SToby Isaac       *name = next->label->name;
4964c58f1c22SToby Isaac       PetscFunctionReturn(0);
4965c58f1c22SToby Isaac     }
4966c58f1c22SToby Isaac     ++l;
4967c58f1c22SToby Isaac     next = next->next;
4968c58f1c22SToby Isaac   }
4969c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
4970c58f1c22SToby Isaac }
4971c58f1c22SToby Isaac 
4972c58f1c22SToby Isaac #undef __FUNCT__
4973c58f1c22SToby Isaac #define __FUNCT__ "DMHasLabel"
4974c58f1c22SToby Isaac /*@C
4975c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
4976c58f1c22SToby Isaac 
4977c58f1c22SToby Isaac   Not Collective
4978c58f1c22SToby Isaac 
4979c58f1c22SToby Isaac   Input Parameters:
4980c58f1c22SToby Isaac + dm   - The DM object
4981c58f1c22SToby Isaac - name - The label name
4982c58f1c22SToby Isaac 
4983c58f1c22SToby Isaac   Output Parameter:
4984c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
4985c58f1c22SToby Isaac 
4986c58f1c22SToby Isaac   Level: intermediate
4987c58f1c22SToby Isaac 
4988c58f1c22SToby Isaac .keywords: mesh
4989c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4990c58f1c22SToby Isaac @*/
4991c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
4992c58f1c22SToby Isaac {
4993c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
4994c58f1c22SToby Isaac   PetscErrorCode ierr;
4995c58f1c22SToby Isaac 
4996c58f1c22SToby Isaac   PetscFunctionBegin;
4997c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4998c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4999c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5000c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5001c58f1c22SToby Isaac   while (next) {
5002c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5003c58f1c22SToby Isaac     if (*hasLabel) break;
5004c58f1c22SToby Isaac     next = next->next;
5005c58f1c22SToby Isaac   }
5006c58f1c22SToby Isaac   PetscFunctionReturn(0);
5007c58f1c22SToby Isaac }
5008c58f1c22SToby Isaac 
5009c58f1c22SToby Isaac #undef __FUNCT__
5010c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabel"
5011c58f1c22SToby Isaac /*@C
5012c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5013c58f1c22SToby Isaac 
5014c58f1c22SToby Isaac   Not Collective
5015c58f1c22SToby Isaac 
5016c58f1c22SToby Isaac   Input Parameters:
5017c58f1c22SToby Isaac + dm   - The DM object
5018c58f1c22SToby Isaac - name - The label name
5019c58f1c22SToby Isaac 
5020c58f1c22SToby Isaac   Output Parameter:
5021c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5022c58f1c22SToby Isaac 
5023c58f1c22SToby Isaac   Level: intermediate
5024c58f1c22SToby Isaac 
5025c58f1c22SToby Isaac .keywords: mesh
5026c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5027c58f1c22SToby Isaac @*/
5028c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5029c58f1c22SToby Isaac {
5030c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5031c58f1c22SToby Isaac   PetscBool      hasLabel;
5032c58f1c22SToby Isaac   PetscErrorCode ierr;
5033c58f1c22SToby Isaac 
5034c58f1c22SToby Isaac   PetscFunctionBegin;
5035c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5036c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5037c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5038c58f1c22SToby Isaac   *label = NULL;
5039c58f1c22SToby Isaac   while (next) {
5040c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5041c58f1c22SToby Isaac     if (hasLabel) {
5042c58f1c22SToby Isaac       *label = next->label;
5043c58f1c22SToby Isaac       break;
5044c58f1c22SToby Isaac     }
5045c58f1c22SToby Isaac     next = next->next;
5046c58f1c22SToby Isaac   }
5047c58f1c22SToby Isaac   PetscFunctionReturn(0);
5048c58f1c22SToby Isaac }
5049c58f1c22SToby Isaac 
5050c58f1c22SToby Isaac #undef __FUNCT__
5051c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelByNum"
5052c58f1c22SToby Isaac /*@C
5053c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5054c58f1c22SToby Isaac 
5055c58f1c22SToby Isaac   Not Collective
5056c58f1c22SToby Isaac 
5057c58f1c22SToby Isaac   Input Parameters:
5058c58f1c22SToby Isaac + dm - The DM object
5059c58f1c22SToby Isaac - n  - the label number
5060c58f1c22SToby Isaac 
5061c58f1c22SToby Isaac   Output Parameter:
5062c58f1c22SToby Isaac . label - the label
5063c58f1c22SToby Isaac 
5064c58f1c22SToby Isaac   Level: intermediate
5065c58f1c22SToby Isaac 
5066c58f1c22SToby Isaac .keywords: mesh
5067c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5068c58f1c22SToby Isaac @*/
5069c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5070c58f1c22SToby Isaac {
5071c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5072c58f1c22SToby Isaac   PetscInt    l    = 0;
5073c58f1c22SToby Isaac 
5074c58f1c22SToby Isaac   PetscFunctionBegin;
5075c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5076c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5077c58f1c22SToby Isaac   while (next) {
5078c58f1c22SToby Isaac     if (l == n) {
5079c58f1c22SToby Isaac       *label = next->label;
5080c58f1c22SToby Isaac       PetscFunctionReturn(0);
5081c58f1c22SToby Isaac     }
5082c58f1c22SToby Isaac     ++l;
5083c58f1c22SToby Isaac     next = next->next;
5084c58f1c22SToby Isaac   }
5085c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5086c58f1c22SToby Isaac }
5087c58f1c22SToby Isaac 
5088c58f1c22SToby Isaac #undef __FUNCT__
5089c58f1c22SToby Isaac #define __FUNCT__ "DMAddLabel"
5090c58f1c22SToby Isaac /*@C
5091c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5092c58f1c22SToby Isaac 
5093c58f1c22SToby Isaac   Not Collective
5094c58f1c22SToby Isaac 
5095c58f1c22SToby Isaac   Input Parameters:
5096c58f1c22SToby Isaac + dm   - The DM object
5097c58f1c22SToby Isaac - label - The DMLabel
5098c58f1c22SToby Isaac 
5099c58f1c22SToby Isaac   Level: developer
5100c58f1c22SToby Isaac 
5101c58f1c22SToby Isaac .keywords: mesh
5102c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5103c58f1c22SToby Isaac @*/
5104c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5105c58f1c22SToby Isaac {
5106c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5107c58f1c22SToby Isaac   PetscBool      hasLabel;
5108c58f1c22SToby Isaac   PetscErrorCode ierr;
5109c58f1c22SToby Isaac 
5110c58f1c22SToby Isaac   PetscFunctionBegin;
5111c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5112c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5113c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5114c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5115c58f1c22SToby Isaac   tmpLabel->label  = label;
5116c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5117c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5118c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5119c58f1c22SToby Isaac   PetscFunctionReturn(0);
5120c58f1c22SToby Isaac }
5121c58f1c22SToby Isaac 
5122c58f1c22SToby Isaac #undef __FUNCT__
5123c58f1c22SToby Isaac #define __FUNCT__ "DMRemoveLabel"
5124c58f1c22SToby Isaac /*@C
5125c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5126c58f1c22SToby Isaac 
5127c58f1c22SToby Isaac   Not Collective
5128c58f1c22SToby Isaac 
5129c58f1c22SToby Isaac   Input Parameters:
5130c58f1c22SToby Isaac + dm   - The DM object
5131c58f1c22SToby Isaac - name - The label name
5132c58f1c22SToby Isaac 
5133c58f1c22SToby Isaac   Output Parameter:
5134c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5135c58f1c22SToby Isaac 
5136c58f1c22SToby Isaac   Level: developer
5137c58f1c22SToby Isaac 
5138c58f1c22SToby Isaac .keywords: mesh
5139c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5140c58f1c22SToby Isaac @*/
5141c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5142c58f1c22SToby Isaac {
5143c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5144c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5145c58f1c22SToby Isaac   PetscBool      hasLabel;
5146c58f1c22SToby Isaac   PetscErrorCode ierr;
5147c58f1c22SToby Isaac 
5148c58f1c22SToby Isaac   PetscFunctionBegin;
5149c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5150c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5151c58f1c22SToby Isaac   *label = NULL;
5152c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5153c58f1c22SToby Isaac   while (next) {
5154c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5155c58f1c22SToby Isaac     if (hasLabel) {
5156c58f1c22SToby Isaac       if (last) last->next       = next->next;
5157c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5158c58f1c22SToby Isaac       next->next = NULL;
5159c58f1c22SToby Isaac       *label     = next->label;
5160c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5161c58f1c22SToby Isaac       if (hasLabel) {
5162c58f1c22SToby Isaac         dm->depthLabel = NULL;
5163c58f1c22SToby Isaac       }
5164c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5165c58f1c22SToby Isaac       break;
5166c58f1c22SToby Isaac     }
5167c58f1c22SToby Isaac     last = next;
5168c58f1c22SToby Isaac     next = next->next;
5169c58f1c22SToby Isaac   }
5170c58f1c22SToby Isaac   PetscFunctionReturn(0);
5171c58f1c22SToby Isaac }
5172c58f1c22SToby Isaac 
5173c58f1c22SToby Isaac #undef __FUNCT__
5174c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelOutput"
5175c58f1c22SToby Isaac /*@C
5176c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5177c58f1c22SToby Isaac 
5178c58f1c22SToby Isaac   Not Collective
5179c58f1c22SToby Isaac 
5180c58f1c22SToby Isaac   Input Parameters:
5181c58f1c22SToby Isaac + dm   - The DM object
5182c58f1c22SToby Isaac - name - The label name
5183c58f1c22SToby Isaac 
5184c58f1c22SToby Isaac   Output Parameter:
5185c58f1c22SToby Isaac . output - The flag for output
5186c58f1c22SToby Isaac 
5187c58f1c22SToby Isaac   Level: developer
5188c58f1c22SToby Isaac 
5189c58f1c22SToby Isaac .keywords: mesh
5190c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5191c58f1c22SToby Isaac @*/
5192c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5193c58f1c22SToby Isaac {
5194c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5195c58f1c22SToby Isaac   PetscErrorCode ierr;
5196c58f1c22SToby Isaac 
5197c58f1c22SToby Isaac   PetscFunctionBegin;
5198c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5199c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5200c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5201c58f1c22SToby Isaac   while (next) {
5202c58f1c22SToby Isaac     PetscBool flg;
5203c58f1c22SToby Isaac 
5204c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5205c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5206c58f1c22SToby Isaac     next = next->next;
5207c58f1c22SToby Isaac   }
5208c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5209c58f1c22SToby Isaac }
5210c58f1c22SToby Isaac 
5211c58f1c22SToby Isaac #undef __FUNCT__
5212c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelOutput"
5213c58f1c22SToby Isaac /*@C
5214c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5215c58f1c22SToby Isaac 
5216c58f1c22SToby Isaac   Not Collective
5217c58f1c22SToby Isaac 
5218c58f1c22SToby Isaac   Input Parameters:
5219c58f1c22SToby Isaac + dm     - The DM object
5220c58f1c22SToby Isaac . name   - The label name
5221c58f1c22SToby Isaac - output - The flag for output
5222c58f1c22SToby Isaac 
5223c58f1c22SToby Isaac   Level: developer
5224c58f1c22SToby Isaac 
5225c58f1c22SToby Isaac .keywords: mesh
5226c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5227c58f1c22SToby Isaac @*/
5228c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5229c58f1c22SToby Isaac {
5230c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5231c58f1c22SToby Isaac   PetscErrorCode ierr;
5232c58f1c22SToby Isaac 
5233c58f1c22SToby Isaac   PetscFunctionBegin;
5234c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5235c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5236c58f1c22SToby Isaac   while (next) {
5237c58f1c22SToby Isaac     PetscBool flg;
5238c58f1c22SToby Isaac 
5239c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5240c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5241c58f1c22SToby Isaac     next = next->next;
5242c58f1c22SToby Isaac   }
5243c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5244c58f1c22SToby Isaac }
5245c58f1c22SToby Isaac 
5246c58f1c22SToby Isaac 
5247c58f1c22SToby Isaac #undef __FUNCT__
5248c58f1c22SToby Isaac #define __FUNCT__ "DMCopyLabels"
5249c58f1c22SToby Isaac /*@
5250c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5251c58f1c22SToby Isaac 
5252c58f1c22SToby Isaac   Collective on DM
5253c58f1c22SToby Isaac 
5254c58f1c22SToby Isaac   Input Parameter:
5255c58f1c22SToby Isaac . dmA - The DMPlex object with initial labels
5256c58f1c22SToby Isaac 
5257c58f1c22SToby Isaac   Output Parameter:
5258c58f1c22SToby Isaac . dmB - The DMPlex object with copied labels
5259c58f1c22SToby Isaac 
5260c58f1c22SToby Isaac   Level: intermediate
5261c58f1c22SToby Isaac 
5262c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5263c58f1c22SToby Isaac 
5264c58f1c22SToby Isaac .keywords: mesh
5265c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5266c58f1c22SToby Isaac @*/
5267c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5268c58f1c22SToby Isaac {
5269c58f1c22SToby Isaac   PetscInt       numLabels, l;
5270c58f1c22SToby Isaac   PetscErrorCode ierr;
5271c58f1c22SToby Isaac 
5272c58f1c22SToby Isaac   PetscFunctionBegin;
5273c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5274c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5275c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5276c58f1c22SToby Isaac     DMLabel     label, labelNew;
5277c58f1c22SToby Isaac     const char *name;
5278c58f1c22SToby Isaac     PetscBool   flg;
5279c58f1c22SToby Isaac 
5280c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5281c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5282c58f1c22SToby Isaac     if (flg) continue;
5283c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5284c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5285c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5286c58f1c22SToby Isaac   }
5287c58f1c22SToby Isaac   PetscFunctionReturn(0);
5288c58f1c22SToby Isaac }
5289a8fb8f29SToby Isaac 
5290a8fb8f29SToby Isaac #undef __FUNCT__
5291a8fb8f29SToby Isaac #define __FUNCT__ "DMGetCoarseDM"
5292a8fb8f29SToby Isaac /*@
5293a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5294a8fb8f29SToby Isaac 
5295a8fb8f29SToby Isaac   Input Parameter:
5296a8fb8f29SToby Isaac . dm - The DM object
5297a8fb8f29SToby Isaac 
5298a8fb8f29SToby Isaac   Output Parameter:
5299a8fb8f29SToby Isaac . cdm - The coarse DM
5300a8fb8f29SToby Isaac 
5301a8fb8f29SToby Isaac   Level: intermediate
5302a8fb8f29SToby Isaac 
5303a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5304a8fb8f29SToby Isaac @*/
5305a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5306a8fb8f29SToby Isaac {
5307a8fb8f29SToby Isaac   PetscFunctionBegin;
5308a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5309a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5310a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5311a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5312a8fb8f29SToby Isaac }
5313a8fb8f29SToby Isaac 
5314a8fb8f29SToby Isaac #undef __FUNCT__
5315a8fb8f29SToby Isaac #define __FUNCT__ "DMSetCoarseDM"
5316a8fb8f29SToby Isaac /*@
5317a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5318a8fb8f29SToby Isaac 
5319a8fb8f29SToby Isaac   Input Parameters:
5320a8fb8f29SToby Isaac + dm - The DM object
5321a8fb8f29SToby Isaac - cdm - The coarse DM
5322a8fb8f29SToby Isaac 
5323a8fb8f29SToby Isaac   Level: intermediate
5324a8fb8f29SToby Isaac 
5325a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5326a8fb8f29SToby Isaac @*/
5327a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5328a8fb8f29SToby Isaac {
5329a8fb8f29SToby Isaac   PetscErrorCode ierr;
5330a8fb8f29SToby Isaac 
5331a8fb8f29SToby Isaac   PetscFunctionBegin;
5332a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5333a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5334a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5335a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5336a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5337a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5338a8fb8f29SToby Isaac }
5339a8fb8f29SToby Isaac 
5340*88bdff64SToby Isaac #undef __FUNCT__
5341*88bdff64SToby Isaac #define __FUNCT__ "DMGetFineDM"
5342*88bdff64SToby Isaac /*@
5343*88bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
5344*88bdff64SToby Isaac 
5345*88bdff64SToby Isaac   Input Parameter:
5346*88bdff64SToby Isaac . dm - The DM object
5347*88bdff64SToby Isaac 
5348*88bdff64SToby Isaac   Output Parameter:
5349*88bdff64SToby Isaac . fdm - The fine DM
5350*88bdff64SToby Isaac 
5351*88bdff64SToby Isaac   Level: intermediate
5352*88bdff64SToby Isaac 
5353*88bdff64SToby Isaac .seealso: DMSetFineDM()
5354*88bdff64SToby Isaac @*/
5355*88bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
5356*88bdff64SToby Isaac {
5357*88bdff64SToby Isaac   PetscFunctionBegin;
5358*88bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5359*88bdff64SToby Isaac   PetscValidPointer(fdm, 2);
5360*88bdff64SToby Isaac   *fdm = dm->fineMesh;
5361*88bdff64SToby Isaac   PetscFunctionReturn(0);
5362*88bdff64SToby Isaac }
5363*88bdff64SToby Isaac 
5364*88bdff64SToby Isaac #undef __FUNCT__
5365*88bdff64SToby Isaac #define __FUNCT__ "DMSetFineDM"
5366*88bdff64SToby Isaac /*@
5367*88bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
5368*88bdff64SToby Isaac 
5369*88bdff64SToby Isaac   Input Parameters:
5370*88bdff64SToby Isaac + dm - The DM object
5371*88bdff64SToby Isaac - fdm - The fine DM
5372*88bdff64SToby Isaac 
5373*88bdff64SToby Isaac   Level: intermediate
5374*88bdff64SToby Isaac 
5375*88bdff64SToby Isaac .seealso: DMGetFineDM()
5376*88bdff64SToby Isaac @*/
5377*88bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
5378*88bdff64SToby Isaac {
5379*88bdff64SToby Isaac   PetscErrorCode ierr;
5380*88bdff64SToby Isaac 
5381*88bdff64SToby Isaac   PetscFunctionBegin;
5382*88bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5383*88bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
5384*88bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
5385*88bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
5386*88bdff64SToby Isaac   dm->fineMesh = fdm;
5387*88bdff64SToby Isaac   PetscFunctionReturn(0);
5388*88bdff64SToby Isaac }
5389*88bdff64SToby Isaac 
5390