xref: /petsc/src/dm/interface/dm.c (revision a8fb8f2974ee66bb003afa65900fab794a3643ec)
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;
69*a8fb8f29SToby 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__
46447c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
46547c6ae99SBarry Smith /*@
4668472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
46747c6ae99SBarry Smith 
46847c6ae99SBarry Smith     Collective on DM
46947c6ae99SBarry Smith 
47047c6ae99SBarry Smith     Input Parameter:
47147c6ae99SBarry Smith .   dm - the DM object to destroy
47247c6ae99SBarry Smith 
47347c6ae99SBarry Smith     Level: developer
47447c6ae99SBarry Smith 
475e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
47647c6ae99SBarry Smith 
47747c6ae99SBarry Smith @*/
478fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
47947c6ae99SBarry Smith {
480c83e3748SMatthew G. Knepley   PetscInt       i, cnt = 0;
481dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
48247c6ae99SBarry Smith   PetscErrorCode ierr;
48347c6ae99SBarry Smith 
48447c6ae99SBarry Smith   PetscFunctionBegin;
4856bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4866bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
48787e657c6SBarry Smith 
48887e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
489732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4908865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4918865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
492732e2eb9SMatthew G Knepley   }
493dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4942348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4952348bcf4SPeter Brune   if ((*dm)->x) {
4962348bcf4SPeter Brune     DM obj;
4972348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4982348bcf4SPeter Brune     if (obj == *dm) cnt++;
4992348bcf4SPeter Brune   }
500732e2eb9SMatthew G Knepley 
5016bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
502732e2eb9SMatthew G Knepley   /*
503732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
504732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
505732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
506732e2eb9SMatthew G Knepley   */
5076bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5086bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
509732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5106bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5116bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
512732e2eb9SMatthew G Knepley   }
513f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5140298fd71SBarry Smith   (*dm)->namedglobal = NULL;
515f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5162348bcf4SPeter Brune     nnext = nlink->next;
5172348bcf4SPeter 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);
5182348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5192348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5202348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5212348bcf4SPeter Brune   }
522f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5230298fd71SBarry Smith   (*dm)->namedlocal = NULL;
524f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
525f490541aSPeter Brune     nnext = nlink->next;
526f490541aSPeter 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);
527f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
528f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
529f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
530f490541aSPeter Brune   }
5312348bcf4SPeter Brune 
532b17ce1afSJed Brown   /* Destroy the list of hooks */
533c833c3b5SJed Brown   {
534c833c3b5SJed Brown     DMCoarsenHookLink link,next;
535b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
536b17ce1afSJed Brown       next = link->next;
537b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
538b17ce1afSJed Brown     }
5390298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
540c833c3b5SJed Brown   }
541c833c3b5SJed Brown   {
542c833c3b5SJed Brown     DMRefineHookLink link,next;
543c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
544c833c3b5SJed Brown       next = link->next;
545c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
546c833c3b5SJed Brown     }
5470298fd71SBarry Smith     (*dm)->refinehook = NULL;
548c833c3b5SJed Brown   }
549be081cd6SPeter Brune   {
550be081cd6SPeter Brune     DMSubDomainHookLink link,next;
551be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
552be081cd6SPeter Brune       next = link->next;
553be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
554be081cd6SPeter Brune     }
5550298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
556be081cd6SPeter Brune   }
557baf369e7SPeter Brune   {
558baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
559baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
560baf369e7SPeter Brune       next = link->next;
561baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
562baf369e7SPeter Brune     }
5630298fd71SBarry Smith     (*dm)->gtolhook = NULL;
564baf369e7SPeter Brune   }
565d4d07f1eSToby Isaac   {
566d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
567d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
568d4d07f1eSToby Isaac       next = link->next;
569d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
570d4d07f1eSToby Isaac     }
571d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
572d4d07f1eSToby Isaac   }
573aa1993deSMatthew G Knepley   /* Destroy the work arrays */
574aa1993deSMatthew G Knepley   {
575aa1993deSMatthew G Knepley     DMWorkLink link,next;
576aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
577aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
578aa1993deSMatthew G Knepley       next = link->next;
579aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
580aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
581aa1993deSMatthew G Knepley     }
5820298fd71SBarry Smith     (*dm)->workin = NULL;
583aa1993deSMatthew G Knepley   }
584c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
585c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
586c58f1c22SToby Isaac 
587c58f1c22SToby Isaac     /* destroy the labels */
588c58f1c22SToby Isaac     while (next) {
589c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
590c58f1c22SToby Isaac 
591c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
592c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
593c58f1c22SToby Isaac       next = tmp;
594c58f1c22SToby Isaac     }
595c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
596c58f1c22SToby Isaac   }
597b17ce1afSJed Brown 
59852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
59952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
60052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
60152536dc3SBarry Smith 
6021a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6031a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6041a266240SBarry Smith   }
60587e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
60671cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6074dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6086bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6096bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
610073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
61188ed4aceSMatthew G Knepley 
61288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
61388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6148b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
615fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
616fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
61788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
61888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6198e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
620af122d2aSMatthew G Knepley 
621*a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
6226636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6236636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6246636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6255dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6266636e97aSMatthew G Knepley 
6272764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
62814f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
629e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
630e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
631732e2eb9SMatthew G Knepley 
6326bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
633435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
634732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
63547c6ae99SBarry Smith   PetscFunctionReturn(0);
63647c6ae99SBarry Smith }
63747c6ae99SBarry Smith 
63847c6ae99SBarry Smith #undef __FUNCT__
639d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
640d7bf68aeSBarry Smith /*@
641d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
642d7bf68aeSBarry Smith 
643d7bf68aeSBarry Smith     Collective on DM
644d7bf68aeSBarry Smith 
645d7bf68aeSBarry Smith     Input Parameter:
646d7bf68aeSBarry Smith .   dm - the DM object to setup
647d7bf68aeSBarry Smith 
648d7bf68aeSBarry Smith     Level: developer
649d7bf68aeSBarry Smith 
650e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
651d7bf68aeSBarry Smith 
652d7bf68aeSBarry Smith @*/
6537087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
654d7bf68aeSBarry Smith {
655d7bf68aeSBarry Smith   PetscErrorCode ierr;
656d7bf68aeSBarry Smith 
657d7bf68aeSBarry Smith   PetscFunctionBegin;
658171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6598387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
660d7bf68aeSBarry Smith   if (dm->ops->setup) {
661d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
662d7bf68aeSBarry Smith   }
6638387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
664d7bf68aeSBarry Smith   PetscFunctionReturn(0);
665d7bf68aeSBarry Smith }
666d7bf68aeSBarry Smith 
667d7bf68aeSBarry Smith #undef __FUNCT__
668d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
669d7bf68aeSBarry Smith /*@
670d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
671d7bf68aeSBarry Smith 
672d7bf68aeSBarry Smith     Collective on DM
673d7bf68aeSBarry Smith 
674d7bf68aeSBarry Smith     Input Parameter:
675d7bf68aeSBarry Smith .   dm - the DM object to set options for
676d7bf68aeSBarry Smith 
677732e2eb9SMatthew G Knepley     Options Database:
6784164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
6794164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
6804164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
6814164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
682732e2eb9SMatthew G Knepley 
683d7bf68aeSBarry Smith     Level: developer
684d7bf68aeSBarry Smith 
685e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
686d7bf68aeSBarry Smith 
687d7bf68aeSBarry Smith @*/
6887087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
689d7bf68aeSBarry Smith {
6907781c08eSBarry Smith   char           typeName[256];
691ca266f36SBarry Smith   PetscBool      flg;
692d7bf68aeSBarry Smith   PetscErrorCode ierr;
693d7bf68aeSBarry Smith 
694d7bf68aeSBarry Smith   PetscFunctionBegin;
695171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
696691be533SLawrence Mitchell   if (dm->sf) {
697691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
698691be533SLawrence Mitchell   }
699691be533SLawrence Mitchell   if (dm->defaultSF) {
700691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
701691be533SLawrence Mitchell   }
7023194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7030298fd71SBarry 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);
704a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
705f9ba7244SBarry Smith   if (flg) {
706f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
707f9ba7244SBarry Smith   }
708a264d7a6SBarry 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);
709073dac72SJed Brown   if (flg) {
710521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
711073dac72SJed Brown   }
7120298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
713f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
714e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
715f9ba7244SBarry Smith   }
716f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
717f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
71882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
719d7bf68aeSBarry Smith   PetscFunctionReturn(0);
720d7bf68aeSBarry Smith }
721d7bf68aeSBarry Smith 
722d7bf68aeSBarry Smith #undef __FUNCT__
72347c6ae99SBarry Smith #define __FUNCT__ "DMView"
724fc9bc008SSatish Balay /*@C
725224748a4SBarry Smith     DMView - Views a DM
72647c6ae99SBarry Smith 
72747c6ae99SBarry Smith     Collective on DM
72847c6ae99SBarry Smith 
72947c6ae99SBarry Smith     Input Parameter:
73047c6ae99SBarry Smith +   dm - the DM object to view
73147c6ae99SBarry Smith -   v - the viewer
73247c6ae99SBarry Smith 
733224748a4SBarry Smith     Level: beginner
73447c6ae99SBarry Smith 
735e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
73647c6ae99SBarry Smith 
73747c6ae99SBarry Smith @*/
7387087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
73947c6ae99SBarry Smith {
74047c6ae99SBarry Smith   PetscErrorCode ierr;
74132c0f0efSBarry Smith   PetscBool      isbinary;
74247c6ae99SBarry Smith 
74347c6ae99SBarry Smith   PetscFunctionBegin;
744171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7453014e516SBarry Smith   if (!v) {
746ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
7473014e516SBarry Smith   }
74898c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
74932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
75032c0f0efSBarry Smith   if (isbinary) {
75155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
75232c0f0efSBarry Smith     char     type[256];
75332c0f0efSBarry Smith 
75432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
75532c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
75632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
75732c0f0efSBarry Smith   }
7580c010503SBarry Smith   if (dm->ops->view) {
7590c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
76047c6ae99SBarry Smith   }
76147c6ae99SBarry Smith   PetscFunctionReturn(0);
76247c6ae99SBarry Smith }
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith #undef __FUNCT__
76547c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
76647c6ae99SBarry Smith /*@
7678472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
76847c6ae99SBarry Smith 
76947c6ae99SBarry Smith     Collective on DM
77047c6ae99SBarry Smith 
77147c6ae99SBarry Smith     Input Parameter:
77247c6ae99SBarry Smith .   dm - the DM object
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith     Output Parameter:
77547c6ae99SBarry Smith .   vec - the global vector
77647c6ae99SBarry Smith 
777073dac72SJed Brown     Level: beginner
77847c6ae99SBarry Smith 
779e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
78047c6ae99SBarry Smith 
78147c6ae99SBarry Smith @*/
7827087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
78347c6ae99SBarry Smith {
78447c6ae99SBarry Smith   PetscErrorCode ierr;
78547c6ae99SBarry Smith 
78647c6ae99SBarry Smith   PetscFunctionBegin;
787171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78847c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
78947c6ae99SBarry Smith   PetscFunctionReturn(0);
79047c6ae99SBarry Smith }
79147c6ae99SBarry Smith 
79247c6ae99SBarry Smith #undef __FUNCT__
79347c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
79447c6ae99SBarry Smith /*@
7958472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
79647c6ae99SBarry Smith 
79747c6ae99SBarry Smith     Not Collective
79847c6ae99SBarry Smith 
79947c6ae99SBarry Smith     Input Parameter:
80047c6ae99SBarry Smith .   dm - the DM object
80147c6ae99SBarry Smith 
80247c6ae99SBarry Smith     Output Parameter:
80347c6ae99SBarry Smith .   vec - the local vector
80447c6ae99SBarry Smith 
805073dac72SJed Brown     Level: beginner
80647c6ae99SBarry Smith 
807e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith @*/
8107087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
81147c6ae99SBarry Smith {
81247c6ae99SBarry Smith   PetscErrorCode ierr;
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith   PetscFunctionBegin;
815171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81647c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
81747c6ae99SBarry Smith   PetscFunctionReturn(0);
81847c6ae99SBarry Smith }
81947c6ae99SBarry Smith 
82047c6ae99SBarry Smith #undef __FUNCT__
8211411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
8221411c6eeSJed Brown /*@
8231411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8241411c6eeSJed Brown 
8251411c6eeSJed Brown    Collective on DM
8261411c6eeSJed Brown 
8271411c6eeSJed Brown    Input Parameter:
8281411c6eeSJed Brown .  dm - the DM that provides the mapping
8291411c6eeSJed Brown 
8301411c6eeSJed Brown    Output Parameter:
8311411c6eeSJed Brown .  ltog - the mapping
8321411c6eeSJed Brown 
8331411c6eeSJed Brown    Level: intermediate
8341411c6eeSJed Brown 
8351411c6eeSJed Brown    Notes:
8361411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8371411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8381411c6eeSJed Brown 
839fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8401411c6eeSJed Brown @*/
8417087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8421411c6eeSJed Brown {
8431411c6eeSJed Brown   PetscErrorCode ierr;
8441411c6eeSJed Brown 
8451411c6eeSJed Brown   PetscFunctionBegin;
8461411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8471411c6eeSJed Brown   PetscValidPointer(ltog,2);
8481411c6eeSJed Brown   if (!dm->ltogmap) {
84937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
85037d0c07bSMatthew G Knepley 
85137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
85237d0c07bSMatthew G Knepley     if (section) {
85337d0c07bSMatthew G Knepley       PetscInt *ltog;
85437d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
85537d0c07bSMatthew G Knepley 
85637d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
85737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
85837d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
859785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
86037d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
86137d0c07bSMatthew G Knepley         PetscInt dof, off, c;
86237d0c07bSMatthew G Knepley 
86337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
86437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
86537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
86637d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
86737d0c07bSMatthew G Knepley           ltog[l] = off+c;
86837d0c07bSMatthew G Knepley         }
86937d0c07bSMatthew G Knepley       }
870f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
8713bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
87237d0c07bSMatthew G Knepley     } else {
873184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
874184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
8751411c6eeSJed Brown     }
87637d0c07bSMatthew G Knepley   }
8771411c6eeSJed Brown   *ltog = dm->ltogmap;
8781411c6eeSJed Brown   PetscFunctionReturn(0);
8791411c6eeSJed Brown }
8801411c6eeSJed Brown 
8811411c6eeSJed Brown #undef __FUNCT__
8821411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
8831411c6eeSJed Brown /*@
8841411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
8851411c6eeSJed Brown 
8861411c6eeSJed Brown    Not Collective
8871411c6eeSJed Brown 
8881411c6eeSJed Brown    Input Parameter:
8891411c6eeSJed Brown .  dm - the DM with block structure
8901411c6eeSJed Brown 
8911411c6eeSJed Brown    Output Parameter:
8921411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
8931411c6eeSJed Brown 
8941411c6eeSJed Brown    Level: intermediate
8951411c6eeSJed Brown 
896fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
8971411c6eeSJed Brown @*/
8987087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
8991411c6eeSJed Brown {
9001411c6eeSJed Brown   PetscFunctionBegin;
9011411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9021411c6eeSJed Brown   PetscValidPointer(bs,2);
9031411c6eeSJed 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");
9041411c6eeSJed Brown   *bs = dm->bs;
9051411c6eeSJed Brown   PetscFunctionReturn(0);
9061411c6eeSJed Brown }
9071411c6eeSJed Brown 
9081411c6eeSJed Brown #undef __FUNCT__
909e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
91047c6ae99SBarry Smith /*@
9118472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
91247c6ae99SBarry Smith 
91347c6ae99SBarry Smith     Collective on DM
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith     Input Parameter:
91647c6ae99SBarry Smith +   dm1 - the DM object
91747c6ae99SBarry Smith -   dm2 - the second, finer DM object
91847c6ae99SBarry Smith 
91947c6ae99SBarry Smith     Output Parameter:
92047c6ae99SBarry Smith +  mat - the interpolation
92147c6ae99SBarry Smith -  vec - the scaling (optional)
92247c6ae99SBarry Smith 
92347c6ae99SBarry Smith     Level: developer
92447c6ae99SBarry Smith 
92585afcc9aSBarry 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
92685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
927d52bd9f3SBarry Smith 
9281f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
929d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
93085afcc9aSBarry Smith 
93185afcc9aSBarry Smith 
932e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
93347c6ae99SBarry Smith 
93447c6ae99SBarry Smith @*/
935e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
93647c6ae99SBarry Smith {
93747c6ae99SBarry Smith   PetscErrorCode ierr;
93847c6ae99SBarry Smith 
93947c6ae99SBarry Smith   PetscFunctionBegin;
940171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
941171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
942cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
94325296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
944cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
94547c6ae99SBarry Smith   PetscFunctionReturn(0);
94647c6ae99SBarry Smith }
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith #undef __FUNCT__
949e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
95047c6ae99SBarry Smith /*@
9518472ad0fSDave May     DMCreateInjection - Gets injection 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:
9606dbf9973SLawrence Mitchell .   mat - the injection
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith     Level: developer
96347c6ae99SBarry Smith 
96485afcc9aSBarry 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
96585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
96685afcc9aSBarry Smith 
967e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith @*/
9706dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
97147c6ae99SBarry Smith {
97247c6ae99SBarry Smith   PetscErrorCode ierr;
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith   PetscFunctionBegin;
975171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
976171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
9776dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
97847c6ae99SBarry Smith   PetscFunctionReturn(0);
97947c6ae99SBarry Smith }
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith #undef __FUNCT__
982e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
983b412c318SBarry Smith /*@
984b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
98547c6ae99SBarry Smith 
98647c6ae99SBarry Smith     Collective on DM
98747c6ae99SBarry Smith 
98847c6ae99SBarry Smith     Input Parameter:
98947c6ae99SBarry Smith +   dm - the DM object
990b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith     Output Parameter:
99347c6ae99SBarry Smith .   coloring - the coloring
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Level: developer
99647c6ae99SBarry Smith 
997b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
99847c6ae99SBarry Smith 
999aab9d709SJed Brown @*/
1000b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
100147c6ae99SBarry Smith {
100247c6ae99SBarry Smith   PetscErrorCode ierr;
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith   PetscFunctionBegin;
1005171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1006ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1007b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
100847c6ae99SBarry Smith   PetscFunctionReturn(0);
100947c6ae99SBarry Smith }
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith #undef __FUNCT__
1012950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1013b412c318SBarry Smith /*@
10148472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith     Collective on DM
101747c6ae99SBarry Smith 
101847c6ae99SBarry Smith     Input Parameter:
1019b412c318SBarry Smith .   dm - the DM object
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith     Output Parameter:
102247c6ae99SBarry Smith .   mat - the empty Jacobian
102347c6ae99SBarry Smith 
1024073dac72SJed Brown     Level: beginner
102547c6ae99SBarry Smith 
102694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
102794013140SBarry Smith        do not need to do it yourself.
102894013140SBarry Smith 
102994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1030aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
103194013140SBarry Smith 
103294013140SBarry 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
103394013140SBarry Smith        internally by PETSc.
103494013140SBarry Smith 
103594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1036aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
103794013140SBarry Smith 
1038b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
103947c6ae99SBarry Smith 
1040aab9d709SJed Brown @*/
1041b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
104247c6ae99SBarry Smith {
104347c6ae99SBarry Smith   PetscErrorCode ierr;
104447c6ae99SBarry Smith 
104547c6ae99SBarry Smith   PetscFunctionBegin;
1046171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1047607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1048c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1049c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1050b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
105147c6ae99SBarry Smith   PetscFunctionReturn(0);
105247c6ae99SBarry Smith }
105347c6ae99SBarry Smith 
105447c6ae99SBarry Smith #undef __FUNCT__
1055732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1056732e2eb9SMatthew G Knepley /*@
1057950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1058732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1059732e2eb9SMatthew G Knepley 
10608472ad0fSDave May   Logically Collective on DM
1061732e2eb9SMatthew G Knepley 
1062732e2eb9SMatthew G Knepley   Input Parameter:
1063732e2eb9SMatthew G Knepley + dm - the DM
1064732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1065732e2eb9SMatthew G Knepley 
1066732e2eb9SMatthew G Knepley   Level: developer
1067950540a4SJed Brown .seealso DMCreateMatrix()
1068732e2eb9SMatthew G Knepley @*/
1069732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1070732e2eb9SMatthew G Knepley {
1071732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1072732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1073732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1074732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1075732e2eb9SMatthew G Knepley }
1076732e2eb9SMatthew G Knepley 
1077732e2eb9SMatthew G Knepley #undef __FUNCT__
1078a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1079a89ea682SMatthew G Knepley /*@C
1080aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1081a89ea682SMatthew G Knepley 
1082a89ea682SMatthew G Knepley   Not Collective
1083a89ea682SMatthew G Knepley 
1084a89ea682SMatthew G Knepley   Input Parameters:
1085a89ea682SMatthew G Knepley + dm - the DM object
1086aa1993deSMatthew G Knepley . count - The minium size
1087aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1088a89ea682SMatthew G Knepley 
1089a89ea682SMatthew G Knepley   Output Parameter:
1090a89ea682SMatthew G Knepley . array - the work array
1091a89ea682SMatthew G Knepley 
1092a89ea682SMatthew G Knepley   Level: developer
1093a89ea682SMatthew G Knepley 
1094a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1095a89ea682SMatthew G Knepley @*/
1096aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1097a89ea682SMatthew G Knepley {
1098a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1099aa1993deSMatthew G Knepley   DMWorkLink     link;
1100854ce69bSBarry Smith   size_t         dsize;
1101a89ea682SMatthew G Knepley 
1102a89ea682SMatthew G Knepley   PetscFunctionBegin;
1103a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1104aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1105aa1993deSMatthew G Knepley   if (dm->workin) {
1106aa1993deSMatthew G Knepley     link       = dm->workin;
1107aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1108aa1993deSMatthew G Knepley   } else {
1109b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1110a89ea682SMatthew G Knepley   }
1111854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1112854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1113aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1114854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1115854ce69bSBarry Smith     link->bytes = dsize*count;
1116aa1993deSMatthew G Knepley   }
1117aa1993deSMatthew G Knepley   link->next   = dm->workout;
1118aa1993deSMatthew G Knepley   dm->workout  = link;
1119aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1120a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1121a89ea682SMatthew G Knepley }
1122a89ea682SMatthew G Knepley 
1123aa1993deSMatthew G Knepley #undef __FUNCT__
1124aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1125aa1993deSMatthew G Knepley /*@C
1126aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1127aa1993deSMatthew G Knepley 
1128aa1993deSMatthew G Knepley   Not Collective
1129aa1993deSMatthew G Knepley 
1130aa1993deSMatthew G Knepley   Input Parameters:
1131aa1993deSMatthew G Knepley + dm - the DM object
1132aa1993deSMatthew G Knepley . count - The minium size
1133aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1134aa1993deSMatthew G Knepley 
1135aa1993deSMatthew G Knepley   Output Parameter:
1136aa1993deSMatthew G Knepley . array - the work array
1137aa1993deSMatthew G Knepley 
1138aa1993deSMatthew G Knepley   Level: developer
1139aa1993deSMatthew G Knepley 
1140aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1141aa1993deSMatthew G Knepley @*/
1142aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1143aa1993deSMatthew G Knepley {
1144aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1145aa1993deSMatthew G Knepley 
1146aa1993deSMatthew G Knepley   PetscFunctionBegin;
1147aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1148aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1149aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1150aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1151aa1993deSMatthew G Knepley       *p           = link->next;
1152aa1993deSMatthew G Knepley       link->next   = dm->workin;
1153aa1993deSMatthew G Knepley       dm->workin   = link;
11540298fd71SBarry Smith       *(void**)mem = NULL;
1155aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1156aa1993deSMatthew G Knepley     }
1157aa1993deSMatthew G Knepley   }
1158aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1159aa1993deSMatthew G Knepley }
1160e7c4fc90SDmitry Karpeev 
1161e7c4fc90SDmitry Karpeev #undef __FUNCT__
1162435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1163435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1164435a35e8SMatthew G Knepley {
1165435a35e8SMatthew G Knepley   PetscFunctionBegin;
1166435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
116782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1168435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1169435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1170435a35e8SMatthew G Knepley }
1171435a35e8SMatthew G Knepley 
1172435a35e8SMatthew G Knepley #undef __FUNCT__
11734d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
11744f3b5142SJed Brown /*@C
11754d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
11764d343eeaSMatthew G Knepley 
11774d343eeaSMatthew G Knepley   Not collective
11784d343eeaSMatthew G Knepley 
11794d343eeaSMatthew G Knepley   Input Parameter:
11804d343eeaSMatthew G Knepley . dm - the DM object
11814d343eeaSMatthew G Knepley 
11824d343eeaSMatthew G Knepley   Output Parameters:
11830298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
11840298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
11850298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
11864d343eeaSMatthew G Knepley 
11874d343eeaSMatthew G Knepley   Level: intermediate
11884d343eeaSMatthew G Knepley 
118921c9b008SJed Brown   Notes:
119021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
119121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
119221c9b008SJed Brown   PetscFree().
119321c9b008SJed Brown 
11944d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11954d343eeaSMatthew G Knepley @*/
119637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11974d343eeaSMatthew G Knepley {
119837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11994d343eeaSMatthew G Knepley   PetscErrorCode ierr;
12004d343eeaSMatthew G Knepley 
12014d343eeaSMatthew G Knepley   PetscFunctionBegin;
12024d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
120369ca1f37SDmitry Karpeev   if (numFields) {
120469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
120569ca1f37SDmitry Karpeev     *numFields = 0;
120669ca1f37SDmitry Karpeev   }
120737d0c07bSMatthew G Knepley   if (fieldNames) {
120837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
12090298fd71SBarry Smith     *fieldNames = NULL;
121069ca1f37SDmitry Karpeev   }
121169ca1f37SDmitry Karpeev   if (fields) {
121269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
12130298fd71SBarry Smith     *fields = NULL;
121469ca1f37SDmitry Karpeev   }
121537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
121637d0c07bSMatthew G Knepley   if (section) {
121737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
121837d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
121937d0c07bSMatthew G Knepley 
122037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
122137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1222dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
122337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
122437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
122537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
122637d0c07bSMatthew G Knepley     }
122737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
122837d0c07bSMatthew G Knepley       PetscInt gdof;
122937d0c07bSMatthew G Knepley 
123037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
123137d0c07bSMatthew G Knepley       if (gdof > 0) {
123237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
123337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
123437d0c07bSMatthew G Knepley 
123537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
123637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
123737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
123837d0c07bSMatthew G Knepley         }
123937d0c07bSMatthew G Knepley       }
124037d0c07bSMatthew G Knepley     }
124137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1242785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
124337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
124437d0c07bSMatthew G Knepley     }
124537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
124637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
124737d0c07bSMatthew G Knepley 
124837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
124937d0c07bSMatthew G Knepley       if (gdof > 0) {
125037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
125137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
125237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
125337d0c07bSMatthew G Knepley 
125437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
125537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
125637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
125737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
125837d0c07bSMatthew G Knepley           }
125937d0c07bSMatthew G Knepley         }
126037d0c07bSMatthew G Knepley       }
126137d0c07bSMatthew G Knepley     }
12628865f1eaSKarl Rupp     if (numFields) *numFields = nF;
126337d0c07bSMatthew G Knepley     if (fieldNames) {
1264785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
126537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
126637d0c07bSMatthew G Knepley         const char *fieldName;
126737d0c07bSMatthew G Knepley 
126837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
126937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
127037d0c07bSMatthew G Knepley       }
127137d0c07bSMatthew G Knepley     }
127237d0c07bSMatthew G Knepley     if (fields) {
1273785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
127437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
127582f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
127637d0c07bSMatthew G Knepley       }
127737d0c07bSMatthew G Knepley     }
127837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
12798865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
12808865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
128169ca1f37SDmitry Karpeev   }
12824d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
12834d343eeaSMatthew G Knepley }
12844d343eeaSMatthew G Knepley 
128516621825SDmitry Karpeev 
128616621825SDmitry Karpeev #undef __FUNCT__
128716621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
128816621825SDmitry Karpeev /*@C
128916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
129016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
129116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1292e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1293e7c4fc90SDmitry Karpeev 
1294e7c4fc90SDmitry Karpeev   Not collective
1295e7c4fc90SDmitry Karpeev 
1296e7c4fc90SDmitry Karpeev   Input Parameter:
1297e7c4fc90SDmitry Karpeev . dm - the DM object
1298e7c4fc90SDmitry Karpeev 
1299e7c4fc90SDmitry Karpeev   Output Parameters:
13000298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
13010298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
13020298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
13030298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1304e7c4fc90SDmitry Karpeev 
1305e7c4fc90SDmitry Karpeev   Level: intermediate
1306e7c4fc90SDmitry Karpeev 
1307e7c4fc90SDmitry Karpeev   Notes:
1308e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1309e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1310e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1311e7c4fc90SDmitry Karpeev 
1312e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1313e7c4fc90SDmitry Karpeev @*/
131416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1315e7c4fc90SDmitry Karpeev {
1316e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1317e7c4fc90SDmitry Karpeev 
1318e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1319e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13208865f1eaSKarl Rupp   if (len) {
13218865f1eaSKarl Rupp     PetscValidPointer(len,2);
13228865f1eaSKarl Rupp     *len = 0;
13238865f1eaSKarl Rupp   }
13248865f1eaSKarl Rupp   if (namelist) {
13258865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
13268865f1eaSKarl Rupp     *namelist = 0;
13278865f1eaSKarl Rupp   }
13288865f1eaSKarl Rupp   if (islist) {
13298865f1eaSKarl Rupp     PetscValidPointer(islist,4);
13308865f1eaSKarl Rupp     *islist = 0;
13318865f1eaSKarl Rupp   }
13328865f1eaSKarl Rupp   if (dmlist) {
13338865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
13348865f1eaSKarl Rupp     *dmlist = 0;
13358865f1eaSKarl Rupp   }
1336f3f0edfdSDmitry Karpeev   /*
1337f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1338f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1339f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1340f3f0edfdSDmitry Karpeev    */
1341ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
134216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1343435a35e8SMatthew G Knepley     PetscSection section;
1344435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1345435a35e8SMatthew G Knepley 
1346435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1347435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1348435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1349435a35e8SMatthew G Knepley       *len = numFields;
135003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
135103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
135203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1353435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1354435a35e8SMatthew G Knepley         const char *fieldName;
1355435a35e8SMatthew G Knepley 
135603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
135703dc3394SMatthew G. Knepley         if (namelist) {
1358435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1359435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1360435a35e8SMatthew G Knepley         }
136103dc3394SMatthew G. Knepley       }
1362435a35e8SMatthew G Knepley     } else {
136369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1364e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
13650298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1366e7c4fc90SDmitry Karpeev     }
13678865f1eaSKarl Rupp   } else {
136816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
136916621825SDmitry Karpeev   }
137016621825SDmitry Karpeev   PetscFunctionReturn(0);
137116621825SDmitry Karpeev }
137216621825SDmitry Karpeev 
137316621825SDmitry Karpeev #undef __FUNCT__
1374435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1375564cec59SMatthew G. Knepley /*@
1376435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1377435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1378435a35e8SMatthew G Knepley 
1379435a35e8SMatthew G Knepley   Not collective
1380435a35e8SMatthew G Knepley 
1381435a35e8SMatthew G Knepley   Input Parameters:
1382435a35e8SMatthew G Knepley + dm - the DM object
1383435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
13840298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1385435a35e8SMatthew G Knepley 
1386435a35e8SMatthew G Knepley   Output Parameters:
1387435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1388435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1389435a35e8SMatthew G Knepley 
1390435a35e8SMatthew G Knepley   Level: intermediate
1391435a35e8SMatthew G Knepley 
1392435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1393435a35e8SMatthew G Knepley @*/
1394435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1395435a35e8SMatthew G Knepley {
1396435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1397435a35e8SMatthew G Knepley 
1398435a35e8SMatthew G Knepley   PetscFunctionBegin;
1399435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1400435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
14018865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
14028865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1403435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1404435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
140582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1406435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1407435a35e8SMatthew G Knepley }
1408435a35e8SMatthew G Knepley 
140916621825SDmitry Karpeev 
141016621825SDmitry Karpeev #undef __FUNCT__
141116621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
141216621825SDmitry Karpeev /*@C
14138d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
14148d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
14158d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
14168d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
14178d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
141816621825SDmitry Karpeev 
141916621825SDmitry Karpeev   Not collective
142016621825SDmitry Karpeev 
142116621825SDmitry Karpeev   Input Parameter:
142216621825SDmitry Karpeev . dm - the DM object
142316621825SDmitry Karpeev 
142416621825SDmitry Karpeev   Output Parameters:
14250298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
14260298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
14270298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
14280298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
14290298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
143016621825SDmitry Karpeev 
143116621825SDmitry Karpeev   Level: intermediate
143216621825SDmitry Karpeev 
143316621825SDmitry Karpeev   Notes:
143416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
143516621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
143616621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
143716621825SDmitry Karpeev 
14388d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
143916621825SDmitry Karpeev @*/
14408d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
144116621825SDmitry Karpeev {
144216621825SDmitry Karpeev   PetscErrorCode      ierr;
1443be081cd6SPeter Brune   DMSubDomainHookLink link;
1444be081cd6SPeter Brune   PetscInt            i,l;
144516621825SDmitry Karpeev 
144616621825SDmitry Karpeev   PetscFunctionBegin;
144716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
144814a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14490298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14500298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14510298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14520298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1453f3f0edfdSDmitry Karpeev   /*
1454f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1455f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1456f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1457f3f0edfdSDmitry Karpeev    */
1458ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
145916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1460be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
146114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
146214a18fd3SPeter Brune     if (dmlist) {
1463be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1464be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1465be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1466be081cd6SPeter Brune         }
1467d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1468e7c4fc90SDmitry Karpeev       }
146914a18fd3SPeter Brune     }
147014a18fd3SPeter Brune     if (len) *len = l;
147114a18fd3SPeter Brune   }
1472e30e807fSPeter Brune   PetscFunctionReturn(0);
1473e30e807fSPeter Brune }
1474e30e807fSPeter Brune 
1475e30e807fSPeter Brune 
1476e30e807fSPeter Brune #undef __FUNCT__
1477e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1478e30e807fSPeter Brune /*@C
1479e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1480e30e807fSPeter Brune 
1481e30e807fSPeter Brune   Not collective
1482e30e807fSPeter Brune 
1483e30e807fSPeter Brune   Input Parameters:
1484e30e807fSPeter Brune + dm - the DM object
1485e30e807fSPeter Brune . n  - the number of subdomain scatters
1486e30e807fSPeter Brune - subdms - the local subdomains
1487e30e807fSPeter Brune 
1488e30e807fSPeter Brune   Output Parameters:
1489e30e807fSPeter Brune + n     - the number of scatters returned
1490e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1491e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1492e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1493e30e807fSPeter Brune 
1494e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1495e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1496e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1497e30e807fSPeter Brune   solution and residual data.
1498e30e807fSPeter Brune 
1499e30e807fSPeter Brune   Level: developer
1500e30e807fSPeter Brune 
1501e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1502e30e807fSPeter Brune @*/
1503e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1504e30e807fSPeter Brune {
1505e30e807fSPeter Brune   PetscErrorCode ierr;
1506e30e807fSPeter Brune 
1507e30e807fSPeter Brune   PetscFunctionBegin;
1508e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1509e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1510e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1511e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
151282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1513e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1514e7c4fc90SDmitry Karpeev }
1515e7c4fc90SDmitry Karpeev 
1516731c8d9eSDmitry Karpeev #undef __FUNCT__
151747c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
151847c6ae99SBarry Smith /*@
151947c6ae99SBarry Smith   DMRefine - Refines a DM object
152047c6ae99SBarry Smith 
152147c6ae99SBarry Smith   Collective on DM
152247c6ae99SBarry Smith 
152347c6ae99SBarry Smith   Input Parameter:
152447c6ae99SBarry Smith + dm   - the DM object
152591d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
152647c6ae99SBarry Smith 
152747c6ae99SBarry Smith   Output Parameter:
15280298fd71SBarry Smith . dmf - the refined DM, or NULL
1529ae0a1c52SMatthew G Knepley 
15300298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
153147c6ae99SBarry Smith 
153247c6ae99SBarry Smith   Level: developer
153347c6ae99SBarry Smith 
1534e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
153547c6ae99SBarry Smith @*/
15367087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
153747c6ae99SBarry Smith {
153847c6ae99SBarry Smith   PetscErrorCode   ierr;
1539c833c3b5SJed Brown   DMRefineHookLink link;
154047c6ae99SBarry Smith 
154147c6ae99SBarry Smith   PetscFunctionBegin;
1542732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
154347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15444057135bSMatthew G Knepley   if (*dmf) {
154543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15468865f1eaSKarl Rupp 
15478cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15488865f1eaSKarl Rupp 
1549644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15500598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1551656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15528865f1eaSKarl Rupp 
1553e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1554c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
15558865f1eaSKarl Rupp       if (link->refinehook) {
15568865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
15578865f1eaSKarl Rupp       }
1558c833c3b5SJed Brown     }
1559c833c3b5SJed Brown   }
1560c833c3b5SJed Brown   PetscFunctionReturn(0);
1561c833c3b5SJed Brown }
1562c833c3b5SJed Brown 
1563c833c3b5SJed Brown #undef __FUNCT__
1564c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1565bb9467b5SJed Brown /*@C
1566c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1567c833c3b5SJed Brown 
1568c833c3b5SJed Brown    Logically Collective
1569c833c3b5SJed Brown 
1570c833c3b5SJed Brown    Input Arguments:
1571c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1572c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1573c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
15740298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1575c833c3b5SJed Brown 
1576c833c3b5SJed Brown    Calling sequence of refinehook:
1577c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1578c833c3b5SJed Brown 
1579c833c3b5SJed Brown +  coarse - coarse level DM
1580c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1581c833c3b5SJed Brown -  ctx - optional user-defined function context
1582c833c3b5SJed Brown 
1583c833c3b5SJed Brown    Calling sequence for interphook:
1584c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1585c833c3b5SJed Brown 
1586c833c3b5SJed Brown +  coarse - coarse level DM
1587c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1588c833c3b5SJed Brown .  fine - fine level DM to update
1589c833c3b5SJed Brown -  ctx - optional user-defined function context
1590c833c3b5SJed Brown 
1591c833c3b5SJed Brown    Level: advanced
1592c833c3b5SJed Brown 
1593c833c3b5SJed Brown    Notes:
1594c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1595c833c3b5SJed Brown 
1596c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1597c833c3b5SJed Brown 
1598bb9467b5SJed Brown    This function is currently not available from Fortran.
1599bb9467b5SJed Brown 
1600c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1601c833c3b5SJed Brown @*/
1602c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1603c833c3b5SJed Brown {
1604c833c3b5SJed Brown   PetscErrorCode   ierr;
1605c833c3b5SJed Brown   DMRefineHookLink link,*p;
1606c833c3b5SJed Brown 
1607c833c3b5SJed Brown   PetscFunctionBegin;
1608c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1609c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1610c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1611c833c3b5SJed Brown   link->refinehook = refinehook;
1612c833c3b5SJed Brown   link->interphook = interphook;
1613c833c3b5SJed Brown   link->ctx        = ctx;
16140298fd71SBarry Smith   link->next       = NULL;
1615c833c3b5SJed Brown   *p               = link;
1616c833c3b5SJed Brown   PetscFunctionReturn(0);
1617c833c3b5SJed Brown }
1618c833c3b5SJed Brown 
1619c833c3b5SJed Brown #undef __FUNCT__
1620c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1621c833c3b5SJed Brown /*@
1622c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1623c833c3b5SJed Brown 
1624c833c3b5SJed Brown    Collective if any hooks are
1625c833c3b5SJed Brown 
1626c833c3b5SJed Brown    Input Arguments:
1627c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1628c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1629c833c3b5SJed Brown -  fine - finer DM to update
1630c833c3b5SJed Brown 
1631c833c3b5SJed Brown    Level: developer
1632c833c3b5SJed Brown 
1633c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1634c833c3b5SJed Brown @*/
1635c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1636c833c3b5SJed Brown {
1637c833c3b5SJed Brown   PetscErrorCode   ierr;
1638c833c3b5SJed Brown   DMRefineHookLink link;
1639c833c3b5SJed Brown 
1640c833c3b5SJed Brown   PetscFunctionBegin;
1641c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16428865f1eaSKarl Rupp     if (link->interphook) {
16438865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16448865f1eaSKarl Rupp     }
16454057135bSMatthew G Knepley   }
164647c6ae99SBarry Smith   PetscFunctionReturn(0);
164747c6ae99SBarry Smith }
164847c6ae99SBarry Smith 
164947c6ae99SBarry Smith #undef __FUNCT__
1650eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1651eb3f98d2SBarry Smith /*@
1652eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1653eb3f98d2SBarry Smith 
1654eb3f98d2SBarry Smith     Not Collective
1655eb3f98d2SBarry Smith 
1656eb3f98d2SBarry Smith     Input Parameter:
1657eb3f98d2SBarry Smith .   dm - the DM object
1658eb3f98d2SBarry Smith 
1659eb3f98d2SBarry Smith     Output Parameter:
1660eb3f98d2SBarry Smith .   level - number of refinements
1661eb3f98d2SBarry Smith 
1662eb3f98d2SBarry Smith     Level: developer
1663eb3f98d2SBarry Smith 
16646a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1665eb3f98d2SBarry Smith 
1666eb3f98d2SBarry Smith @*/
1667eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1668eb3f98d2SBarry Smith {
1669eb3f98d2SBarry Smith   PetscFunctionBegin;
1670eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1671eb3f98d2SBarry Smith   *level = dm->levelup;
1672eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1673eb3f98d2SBarry Smith }
1674eb3f98d2SBarry Smith 
1675eb3f98d2SBarry Smith #undef __FUNCT__
1676baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1677bb9467b5SJed Brown /*@C
1678baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1679baf369e7SPeter Brune 
1680baf369e7SPeter Brune    Logically Collective
1681baf369e7SPeter Brune 
1682baf369e7SPeter Brune    Input Arguments:
1683baf369e7SPeter Brune +  dm - the DM
1684baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1685baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
16860298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1687baf369e7SPeter Brune 
1688baf369e7SPeter Brune    Calling sequence for beginhook:
1689baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1690baf369e7SPeter Brune 
1691baf369e7SPeter Brune +  dm - global DM
1692baf369e7SPeter Brune .  g - global vector
1693baf369e7SPeter Brune .  mode - mode
1694baf369e7SPeter Brune .  l - local vector
1695baf369e7SPeter Brune -  ctx - optional user-defined function context
1696baf369e7SPeter Brune 
1697baf369e7SPeter Brune 
1698baf369e7SPeter Brune    Calling sequence for endhook:
1699ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1700baf369e7SPeter Brune 
1701baf369e7SPeter Brune +  global - global DM
1702baf369e7SPeter Brune -  ctx - optional user-defined function context
1703baf369e7SPeter Brune 
1704baf369e7SPeter Brune    Level: advanced
1705baf369e7SPeter Brune 
1706baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1707baf369e7SPeter Brune @*/
1708baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1709baf369e7SPeter Brune {
1710baf369e7SPeter Brune   PetscErrorCode          ierr;
1711baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1712baf369e7SPeter Brune 
1713baf369e7SPeter Brune   PetscFunctionBegin;
1714baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1715baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1716baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1717baf369e7SPeter Brune   link->beginhook = beginhook;
1718baf369e7SPeter Brune   link->endhook   = endhook;
1719baf369e7SPeter Brune   link->ctx       = ctx;
17200298fd71SBarry Smith   link->next      = NULL;
1721baf369e7SPeter Brune   *p              = link;
1722baf369e7SPeter Brune   PetscFunctionReturn(0);
1723baf369e7SPeter Brune }
1724baf369e7SPeter Brune 
1725baf369e7SPeter Brune #undef __FUNCT__
17264c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
17274c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
17284c274da1SToby Isaac {
17294c274da1SToby Isaac   Mat cMat;
17304c274da1SToby Isaac   Vec cVec;
17314c274da1SToby Isaac   PetscSection section, cSec;
17324c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
17334c274da1SToby Isaac   PetscErrorCode ierr;
17344c274da1SToby Isaac 
17354c274da1SToby Isaac   PetscFunctionBegin;
17364c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17374c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
17384c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
17394c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
17407711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
17414c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
17424c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
17434c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
17444c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
17454c274da1SToby Isaac       if (dof) {
17464c274da1SToby Isaac         PetscScalar *vals;
17474c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
17484c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
17494c274da1SToby Isaac       }
17504c274da1SToby Isaac     }
17514c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
17524c274da1SToby Isaac   }
17534c274da1SToby Isaac   PetscFunctionReturn(0);
17544c274da1SToby Isaac }
17554c274da1SToby Isaac 
17564c274da1SToby Isaac #undef __FUNCT__
175747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
175847c6ae99SBarry Smith /*@
175947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
176047c6ae99SBarry Smith 
176147c6ae99SBarry Smith     Neighbor-wise Collective on DM
176247c6ae99SBarry Smith 
176347c6ae99SBarry Smith     Input Parameters:
176447c6ae99SBarry Smith +   dm - the DM object
176547c6ae99SBarry Smith .   g - the global vector
176647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
176747c6ae99SBarry Smith -   l - the local vector
176847c6ae99SBarry Smith 
176947c6ae99SBarry Smith 
177047c6ae99SBarry Smith     Level: beginner
177147c6ae99SBarry Smith 
1772e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
177347c6ae99SBarry Smith 
177447c6ae99SBarry Smith @*/
17757087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
177647c6ae99SBarry Smith {
17777128ae9fSMatthew G Knepley   PetscSF                 sf;
177847c6ae99SBarry Smith   PetscErrorCode          ierr;
1779baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
178047c6ae99SBarry Smith 
178147c6ae99SBarry Smith   PetscFunctionBegin;
1782171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1783baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
17848865f1eaSKarl Rupp     if (link->beginhook) {
17858865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
17868865f1eaSKarl Rupp     }
1787baf369e7SPeter Brune   }
17887128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17897128ae9fSMatthew G Knepley   if (sf) {
1790ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1791ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
17927128ae9fSMatthew G Knepley 
179382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17947128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1795ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
17967128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17977128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1798ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
17997128ae9fSMatthew G Knepley   } else {
1800843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18017128ae9fSMatthew G Knepley   }
180247c6ae99SBarry Smith   PetscFunctionReturn(0);
180347c6ae99SBarry Smith }
180447c6ae99SBarry Smith 
180547c6ae99SBarry Smith #undef __FUNCT__
180647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
180747c6ae99SBarry Smith /*@
180847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
180947c6ae99SBarry Smith 
181047c6ae99SBarry Smith     Neighbor-wise Collective on DM
181147c6ae99SBarry Smith 
181247c6ae99SBarry Smith     Input Parameters:
181347c6ae99SBarry Smith +   dm - the DM object
181447c6ae99SBarry Smith .   g - the global vector
181547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
181647c6ae99SBarry Smith -   l - the local vector
181747c6ae99SBarry Smith 
181847c6ae99SBarry Smith 
181947c6ae99SBarry Smith     Level: beginner
182047c6ae99SBarry Smith 
1821e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
182247c6ae99SBarry Smith 
182347c6ae99SBarry Smith @*/
18247087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
182547c6ae99SBarry Smith {
18267128ae9fSMatthew G Knepley   PetscSF                 sf;
182747c6ae99SBarry Smith   PetscErrorCode          ierr;
1828ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1829ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1830baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
183147c6ae99SBarry Smith 
183247c6ae99SBarry Smith   PetscFunctionBegin;
1833171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18347128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18357128ae9fSMatthew G Knepley   if (sf) {
183682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18377128ae9fSMatthew G Knepley 
18387128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1839ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18407128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18417128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1842ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18437128ae9fSMatthew G Knepley   } else {
1844843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18457128ae9fSMatthew G Knepley   }
18464c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1847baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1848baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1849baf369e7SPeter Brune   }
185047c6ae99SBarry Smith   PetscFunctionReturn(0);
185147c6ae99SBarry Smith }
185247c6ae99SBarry Smith 
185347c6ae99SBarry Smith #undef __FUNCT__
1854d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1855d4d07f1eSToby Isaac /*@C
1856d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1857d4d07f1eSToby Isaac 
1858d4d07f1eSToby Isaac    Logically Collective
1859d4d07f1eSToby Isaac 
1860d4d07f1eSToby Isaac    Input Arguments:
1861d4d07f1eSToby Isaac +  dm - the DM
1862d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1863d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1864d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1865d4d07f1eSToby Isaac 
1866d4d07f1eSToby Isaac    Calling sequence for beginhook:
1867d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1868d4d07f1eSToby Isaac 
1869d4d07f1eSToby Isaac +  dm - global DM
1870d4d07f1eSToby Isaac .  l - local vector
1871d4d07f1eSToby Isaac .  mode - mode
1872d4d07f1eSToby Isaac .  g - global vector
1873d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1874d4d07f1eSToby Isaac 
1875d4d07f1eSToby Isaac 
1876d4d07f1eSToby Isaac    Calling sequence for endhook:
1877d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1878d4d07f1eSToby Isaac 
1879d4d07f1eSToby Isaac +  global - global DM
1880d4d07f1eSToby Isaac .  l - local vector
1881d4d07f1eSToby Isaac .  mode - mode
1882d4d07f1eSToby Isaac .  g - global vector
1883d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1884d4d07f1eSToby Isaac 
1885d4d07f1eSToby Isaac    Level: advanced
1886d4d07f1eSToby Isaac 
1887d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1888d4d07f1eSToby Isaac @*/
1889d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1890d4d07f1eSToby Isaac {
1891d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1892d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1893d4d07f1eSToby Isaac 
1894d4d07f1eSToby Isaac   PetscFunctionBegin;
1895d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1896d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1897d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1898d4d07f1eSToby Isaac   link->beginhook = beginhook;
1899d4d07f1eSToby Isaac   link->endhook   = endhook;
1900d4d07f1eSToby Isaac   link->ctx       = ctx;
1901d4d07f1eSToby Isaac   link->next      = NULL;
1902d4d07f1eSToby Isaac   *p              = link;
1903d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1904d4d07f1eSToby Isaac }
1905d4d07f1eSToby Isaac 
1906d4d07f1eSToby Isaac #undef __FUNCT__
19074c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
19084c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
19094c274da1SToby Isaac {
19104c274da1SToby Isaac   Mat cMat;
19114c274da1SToby Isaac   Vec cVec;
19124c274da1SToby Isaac   PetscSection section, cSec;
19134c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19144c274da1SToby Isaac   PetscErrorCode ierr;
19154c274da1SToby Isaac 
19164c274da1SToby Isaac   PetscFunctionBegin;
19174c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19184c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19194c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
19204c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19217711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19224c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19234c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19244c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19254c274da1SToby Isaac       if (dof) {
19264c274da1SToby Isaac         PetscInt d;
19274c274da1SToby Isaac         PetscScalar *vals;
19284c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
19294c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
19304c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
19314c274da1SToby Isaac          * we just extracted */
19324c274da1SToby Isaac         for (d = 0; d < dof; d++) {
19334c274da1SToby Isaac           vals[d] = 0.;
19344c274da1SToby Isaac         }
19354c274da1SToby Isaac       }
19364c274da1SToby Isaac     }
19374c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
19384c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19394c274da1SToby Isaac   }
19404c274da1SToby Isaac   PetscFunctionReturn(0);
19414c274da1SToby Isaac }
19424c274da1SToby Isaac 
19434c274da1SToby Isaac #undef __FUNCT__
19449a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
194547c6ae99SBarry Smith /*@
19469a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
19479a42bb27SBarry Smith 
19489a42bb27SBarry Smith     Neighbor-wise Collective on DM
19499a42bb27SBarry Smith 
19509a42bb27SBarry Smith     Input Parameters:
19519a42bb27SBarry Smith +   dm - the DM object
1952f6813fd5SJed Brown .   l - the local vector
19531eb28f2eSBarry 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.
19541eb28f2eSBarry Smith -   g - the global vector
19559a42bb27SBarry Smith 
1956d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
195784330215SMatthew 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.
19589a42bb27SBarry Smith 
19599a42bb27SBarry Smith     Level: beginner
19609a42bb27SBarry Smith 
1961e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
19629a42bb27SBarry Smith 
19639a42bb27SBarry Smith @*/
19647087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
19659a42bb27SBarry Smith {
19667128ae9fSMatthew G Knepley   PetscSF                 sf;
196784330215SMatthew G. Knepley   PetscSection            s, gs;
1968d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
1969ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
1970ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
197184330215SMatthew G. Knepley   PetscBool               isInsert;
197284330215SMatthew G. Knepley   PetscErrorCode          ierr;
19739a42bb27SBarry Smith 
19749a42bb27SBarry Smith   PetscFunctionBegin;
1975171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1976d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1977d4d07f1eSToby Isaac     if (link->beginhook) {
1978d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
1979d4d07f1eSToby Isaac     }
1980d4d07f1eSToby Isaac   }
19814c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
19827128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
198384330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19847128ae9fSMatthew G Knepley   switch (mode) {
19857128ae9fSMatthew G Knepley   case INSERT_VALUES:
19867128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
198784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
19887128ae9fSMatthew G Knepley   case ADD_VALUES:
19897128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
199084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
19917128ae9fSMatthew G Knepley   default:
199282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19937128ae9fSMatthew G Knepley   }
199484330215SMatthew G. Knepley   if (sf && !isInsert) {
1995ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
19967128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
1997a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
1998ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
199984330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
200084330215SMatthew G. Knepley   } else if (s && isInsert) {
200184330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
200284330215SMatthew G. Knepley 
200384330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
200484330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
200584330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2006ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
200784330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
200884330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2009b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
201084330215SMatthew G. Knepley 
201184330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
201203442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
201384330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2014b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
201584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
201684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2017b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
201803442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2019b3b16f48SMatthew 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);
2020b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2021b3b16f48SMatthew G. Knepley       if (!gcdof) {
202284330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2023b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2024b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
202584330215SMatthew G. Knepley         const PetscInt *cdofs;
202684330215SMatthew G. Knepley         PetscInt        cind = 0;
202784330215SMatthew G. Knepley 
202884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2029b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
203084330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2031b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
203284330215SMatthew G. Knepley         }
2033b3b16f48SMatthew 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);
203484330215SMatthew G. Knepley     }
2035ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20367128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
20377128ae9fSMatthew G Knepley   } else {
2038843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20397128ae9fSMatthew G Knepley   }
20409a42bb27SBarry Smith   PetscFunctionReturn(0);
20419a42bb27SBarry Smith }
20429a42bb27SBarry Smith 
20439a42bb27SBarry Smith #undef __FUNCT__
20449a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
20459a42bb27SBarry Smith /*@
20469a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
204747c6ae99SBarry Smith 
204847c6ae99SBarry Smith     Neighbor-wise Collective on DM
204947c6ae99SBarry Smith 
205047c6ae99SBarry Smith     Input Parameters:
205147c6ae99SBarry Smith +   dm - the DM object
2052f6813fd5SJed Brown .   l - the local vector
205347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2054f6813fd5SJed Brown -   g - the global vector
205547c6ae99SBarry Smith 
205647c6ae99SBarry Smith 
205747c6ae99SBarry Smith     Level: beginner
205847c6ae99SBarry Smith 
2059e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
206047c6ae99SBarry Smith 
206147c6ae99SBarry Smith @*/
20627087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
206347c6ae99SBarry Smith {
20647128ae9fSMatthew G Knepley   PetscSF                 sf;
206584330215SMatthew G. Knepley   PetscSection            s;
2066d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
206784330215SMatthew G. Knepley   PetscBool               isInsert;
206884330215SMatthew G. Knepley   PetscErrorCode          ierr;
206947c6ae99SBarry Smith 
207047c6ae99SBarry Smith   PetscFunctionBegin;
2071171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20727128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
207384330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20747128ae9fSMatthew G Knepley   switch (mode) {
20757128ae9fSMatthew G Knepley   case INSERT_VALUES:
20767128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
207784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20787128ae9fSMatthew G Knepley   case ADD_VALUES:
20797128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
208084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20817128ae9fSMatthew G Knepley   default:
208282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20837128ae9fSMatthew G Knepley   }
208484330215SMatthew G. Knepley   if (sf && !isInsert) {
2085ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2086ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
208784330215SMatthew G. Knepley 
2088ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20897128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2090a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2091ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20927128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
209384330215SMatthew G. Knepley   } else if (s && isInsert) {
20947128ae9fSMatthew G Knepley   } else {
2095843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20967128ae9fSMatthew G Knepley   }
2097d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2098d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2099d4d07f1eSToby Isaac   }
210047c6ae99SBarry Smith   PetscFunctionReturn(0);
210147c6ae99SBarry Smith }
210247c6ae99SBarry Smith 
210347c6ae99SBarry Smith #undef __FUNCT__
2104f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2105f089877aSRichard Tran Mills /*@
2106bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2107bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2108d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2109f089877aSRichard Tran Mills 
2110bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2111f089877aSRichard Tran Mills 
2112f089877aSRichard Tran Mills    Input Parameters:
2113f089877aSRichard Tran Mills +  dm - the DM object
2114bc0a1609SRichard Tran Mills .  g - the original local vector
2115bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2116f089877aSRichard Tran Mills 
2117bc0a1609SRichard Tran Mills    Output Parameter:
2118bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2119f089877aSRichard Tran Mills 
2120f089877aSRichard Tran Mills    Level: intermediate
2121f089877aSRichard Tran Mills 
2122bc0a1609SRichard Tran Mills    Notes:
2123bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2124bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2125bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2126bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2127bc0a1609SRichard Tran Mills 
2128bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2129bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2130f089877aSRichard Tran Mills 
2131f089877aSRichard Tran Mills @*/
2132f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2133f089877aSRichard Tran Mills {
2134f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2135f089877aSRichard Tran Mills 
2136f089877aSRichard Tran Mills   PetscFunctionBegin;
2137f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2138f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2139f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2140f089877aSRichard Tran Mills }
2141f089877aSRichard Tran Mills 
2142f089877aSRichard Tran Mills #undef __FUNCT__
2143f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2144f089877aSRichard Tran Mills /*@
2145bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2146bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2147d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2148f089877aSRichard Tran Mills 
2149bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2150f089877aSRichard Tran Mills 
2151f089877aSRichard Tran Mills    Input Parameters:
2152bc0a1609SRichard Tran Mills +  da - the DM object
2153bc0a1609SRichard Tran Mills .  g - the original local vector
2154bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2155f089877aSRichard Tran Mills 
2156bc0a1609SRichard Tran Mills    Output Parameter:
2157bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2158f089877aSRichard Tran Mills 
2159f089877aSRichard Tran Mills    Level: intermediate
2160f089877aSRichard Tran Mills 
2161bc0a1609SRichard Tran Mills    Notes:
2162bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2163bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2164bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2165bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2166bc0a1609SRichard Tran Mills 
2167bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2168bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2169f089877aSRichard Tran Mills 
2170f089877aSRichard Tran Mills @*/
2171f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2172f089877aSRichard Tran Mills {
2173f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2174f089877aSRichard Tran Mills 
2175f089877aSRichard Tran Mills   PetscFunctionBegin;
2176f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2177f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2178f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2179f089877aSRichard Tran Mills }
2180f089877aSRichard Tran Mills 
2181f089877aSRichard Tran Mills 
2182f089877aSRichard Tran Mills #undef __FUNCT__
218347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
218447c6ae99SBarry Smith /*@
218547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
218647c6ae99SBarry Smith 
218747c6ae99SBarry Smith     Collective on DM
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith     Input Parameter:
219047c6ae99SBarry Smith +   dm - the DM object
219191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
219247c6ae99SBarry Smith 
219347c6ae99SBarry Smith     Output Parameter:
219447c6ae99SBarry Smith .   dmc - the coarsened DM
219547c6ae99SBarry Smith 
219647c6ae99SBarry Smith     Level: developer
219747c6ae99SBarry Smith 
2198e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
219947c6ae99SBarry Smith 
220047c6ae99SBarry Smith @*/
22017087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
220247c6ae99SBarry Smith {
220347c6ae99SBarry Smith   PetscErrorCode    ierr;
2204b17ce1afSJed Brown   DMCoarsenHookLink link;
220547c6ae99SBarry Smith 
220647c6ae99SBarry Smith   PetscFunctionBegin;
2207171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
220847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
220947c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
22108892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2211*a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
2212*a8fb8f29SToby Isaac   dm->coarseMesh            = *dmc;
221343842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
22148cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2215644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
22160598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2217656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2218e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2219b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2220b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2221b17ce1afSJed Brown   }
222247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2223b17ce1afSJed Brown   PetscFunctionReturn(0);
2224b17ce1afSJed Brown }
2225b17ce1afSJed Brown 
2226b17ce1afSJed Brown #undef __FUNCT__
2227b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2228bb9467b5SJed Brown /*@C
2229b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2230b17ce1afSJed Brown 
2231b17ce1afSJed Brown    Logically Collective
2232b17ce1afSJed Brown 
2233b17ce1afSJed Brown    Input Arguments:
2234b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2235b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2236b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
22370298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2238b17ce1afSJed Brown 
2239b17ce1afSJed Brown    Calling sequence of coarsenhook:
2240b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2241b17ce1afSJed Brown 
2242b17ce1afSJed Brown +  fine - fine level DM
2243b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2244b17ce1afSJed Brown -  ctx - optional user-defined function context
2245b17ce1afSJed Brown 
2246b17ce1afSJed Brown    Calling sequence for restricthook:
2247c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2248b17ce1afSJed Brown 
2249b17ce1afSJed Brown +  fine - fine level DM
2250b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2251c833c3b5SJed Brown .  rscale - scaling vector for restriction
2252c833c3b5SJed Brown .  inject - matrix restricting by injection
2253b17ce1afSJed Brown .  coarse - coarse level DM to update
2254b17ce1afSJed Brown -  ctx - optional user-defined function context
2255b17ce1afSJed Brown 
2256b17ce1afSJed Brown    Level: advanced
2257b17ce1afSJed Brown 
2258b17ce1afSJed Brown    Notes:
2259b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2260b17ce1afSJed Brown 
2261b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2262b17ce1afSJed Brown 
2263b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2264b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2265b17ce1afSJed Brown 
2266bb9467b5SJed Brown    This function is currently not available from Fortran.
2267bb9467b5SJed Brown 
2268c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2269b17ce1afSJed Brown @*/
2270b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2271b17ce1afSJed Brown {
2272b17ce1afSJed Brown   PetscErrorCode    ierr;
2273b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2274b17ce1afSJed Brown 
2275b17ce1afSJed Brown   PetscFunctionBegin;
2276b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
22776bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2278b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2279b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2280b17ce1afSJed Brown   link->restricthook = restricthook;
2281b17ce1afSJed Brown   link->ctx          = ctx;
22820298fd71SBarry Smith   link->next         = NULL;
2283b17ce1afSJed Brown   *p                 = link;
2284b17ce1afSJed Brown   PetscFunctionReturn(0);
2285b17ce1afSJed Brown }
2286b17ce1afSJed Brown 
2287b17ce1afSJed Brown #undef __FUNCT__
2288b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2289b17ce1afSJed Brown /*@
2290b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2291b17ce1afSJed Brown 
2292b17ce1afSJed Brown    Collective if any hooks are
2293b17ce1afSJed Brown 
2294b17ce1afSJed Brown    Input Arguments:
2295b17ce1afSJed Brown +  fine - finer DM to use as a base
2296b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2297b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2298b17ce1afSJed Brown -  coarse - coarer DM to update
2299b17ce1afSJed Brown 
2300b17ce1afSJed Brown    Level: developer
2301b17ce1afSJed Brown 
2302b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2303b17ce1afSJed Brown @*/
2304b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2305b17ce1afSJed Brown {
2306b17ce1afSJed Brown   PetscErrorCode    ierr;
2307b17ce1afSJed Brown   DMCoarsenHookLink link;
2308b17ce1afSJed Brown 
2309b17ce1afSJed Brown   PetscFunctionBegin;
2310b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
23118865f1eaSKarl Rupp     if (link->restricthook) {
23128865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
23138865f1eaSKarl Rupp     }
2314b17ce1afSJed Brown   }
231547c6ae99SBarry Smith   PetscFunctionReturn(0);
231647c6ae99SBarry Smith }
231747c6ae99SBarry Smith 
231847c6ae99SBarry Smith #undef __FUNCT__
2319be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2320bb9467b5SJed Brown /*@C
2321be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
23225dbd56e3SPeter Brune 
23235dbd56e3SPeter Brune    Logically Collective
23245dbd56e3SPeter Brune 
23255dbd56e3SPeter Brune    Input Arguments:
23265dbd56e3SPeter Brune +  global - global DM
2327ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
23285dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
23290298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23305dbd56e3SPeter Brune 
2331ec4806b8SPeter Brune 
2332ec4806b8SPeter Brune    Calling sequence for ddhook:
2333ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2334ec4806b8SPeter Brune 
2335ec4806b8SPeter Brune +  global - global DM
2336ec4806b8SPeter Brune .  block  - block DM
2337ec4806b8SPeter Brune -  ctx - optional user-defined function context
2338ec4806b8SPeter Brune 
23395dbd56e3SPeter Brune    Calling sequence for restricthook:
2340ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
23415dbd56e3SPeter Brune 
23425dbd56e3SPeter Brune +  global - global DM
23435dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
23445dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2345ec4806b8SPeter Brune .  block  - block DM
23465dbd56e3SPeter Brune -  ctx - optional user-defined function context
23475dbd56e3SPeter Brune 
23485dbd56e3SPeter Brune    Level: advanced
23495dbd56e3SPeter Brune 
23505dbd56e3SPeter Brune    Notes:
2351ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
23525dbd56e3SPeter Brune 
23535dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
23545dbd56e3SPeter Brune 
23555dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2356ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
23575dbd56e3SPeter Brune 
2358bb9467b5SJed Brown    This function is currently not available from Fortran.
2359bb9467b5SJed Brown 
23605dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
23615dbd56e3SPeter Brune @*/
2362be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
23635dbd56e3SPeter Brune {
23645dbd56e3SPeter Brune   PetscErrorCode      ierr;
2365be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
23665dbd56e3SPeter Brune 
23675dbd56e3SPeter Brune   PetscFunctionBegin;
23685dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2369be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2370be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
23715dbd56e3SPeter Brune   link->restricthook = restricthook;
2372be081cd6SPeter Brune   link->ddhook       = ddhook;
23735dbd56e3SPeter Brune   link->ctx          = ctx;
23740298fd71SBarry Smith   link->next         = NULL;
23755dbd56e3SPeter Brune   *p                 = link;
23765dbd56e3SPeter Brune   PetscFunctionReturn(0);
23775dbd56e3SPeter Brune }
23785dbd56e3SPeter Brune 
23795dbd56e3SPeter Brune #undef __FUNCT__
2380be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
23815dbd56e3SPeter Brune /*@
2382be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
23835dbd56e3SPeter Brune 
23845dbd56e3SPeter Brune    Collective if any hooks are
23855dbd56e3SPeter Brune 
23865dbd56e3SPeter Brune    Input Arguments:
23875dbd56e3SPeter Brune +  fine - finer DM to use as a base
2388be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2389be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
23905dbd56e3SPeter Brune -  coarse - coarer DM to update
23915dbd56e3SPeter Brune 
23925dbd56e3SPeter Brune    Level: developer
23935dbd56e3SPeter Brune 
23945dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
23955dbd56e3SPeter Brune @*/
2396be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
23975dbd56e3SPeter Brune {
23985dbd56e3SPeter Brune   PetscErrorCode      ierr;
2399be081cd6SPeter Brune   DMSubDomainHookLink link;
24005dbd56e3SPeter Brune 
24015dbd56e3SPeter Brune   PetscFunctionBegin;
2402be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
24038865f1eaSKarl Rupp     if (link->restricthook) {
24048865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
24058865f1eaSKarl Rupp     }
24065dbd56e3SPeter Brune   }
24075dbd56e3SPeter Brune   PetscFunctionReturn(0);
24085dbd56e3SPeter Brune }
24095dbd56e3SPeter Brune 
24105dbd56e3SPeter Brune #undef __FUNCT__
24115fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
24125fe1f584SPeter Brune /*@
24136a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
24145fe1f584SPeter Brune 
24155fe1f584SPeter Brune     Not Collective
24165fe1f584SPeter Brune 
24175fe1f584SPeter Brune     Input Parameter:
24185fe1f584SPeter Brune .   dm - the DM object
24195fe1f584SPeter Brune 
24205fe1f584SPeter Brune     Output Parameter:
24216a7d9d85SPeter Brune .   level - number of coarsenings
24225fe1f584SPeter Brune 
24235fe1f584SPeter Brune     Level: developer
24245fe1f584SPeter Brune 
24256a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
24265fe1f584SPeter Brune 
24275fe1f584SPeter Brune @*/
24285fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
24295fe1f584SPeter Brune {
24305fe1f584SPeter Brune   PetscFunctionBegin;
24315fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24325fe1f584SPeter Brune   *level = dm->leveldown;
24335fe1f584SPeter Brune   PetscFunctionReturn(0);
24345fe1f584SPeter Brune }
24355fe1f584SPeter Brune 
24365fe1f584SPeter Brune 
24375fe1f584SPeter Brune 
24385fe1f584SPeter Brune #undef __FUNCT__
243947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
244047c6ae99SBarry Smith /*@C
244147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
244247c6ae99SBarry Smith 
244347c6ae99SBarry Smith     Collective on DM
244447c6ae99SBarry Smith 
244547c6ae99SBarry Smith     Input Parameter:
244647c6ae99SBarry Smith +   dm - the DM object
244747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
244847c6ae99SBarry Smith 
244947c6ae99SBarry Smith     Output Parameter:
245047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
245147c6ae99SBarry Smith 
245247c6ae99SBarry Smith     Level: developer
245347c6ae99SBarry Smith 
2454e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
245547c6ae99SBarry Smith 
245647c6ae99SBarry Smith @*/
24577087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
245847c6ae99SBarry Smith {
245947c6ae99SBarry Smith   PetscErrorCode ierr;
246047c6ae99SBarry Smith 
246147c6ae99SBarry Smith   PetscFunctionBegin;
2462171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2463ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
246447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
246547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
246647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
246747c6ae99SBarry Smith   } else if (dm->ops->refine) {
246847c6ae99SBarry Smith     PetscInt i;
246947c6ae99SBarry Smith 
2470ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
247147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2472ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
247347c6ae99SBarry Smith     }
2474ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
247547c6ae99SBarry Smith   PetscFunctionReturn(0);
247647c6ae99SBarry Smith }
247747c6ae99SBarry Smith 
247847c6ae99SBarry Smith #undef __FUNCT__
247947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
248047c6ae99SBarry Smith /*@C
248147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
248247c6ae99SBarry Smith 
248347c6ae99SBarry Smith     Collective on DM
248447c6ae99SBarry Smith 
248547c6ae99SBarry Smith     Input Parameter:
248647c6ae99SBarry Smith +   dm - the DM object
248747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
248847c6ae99SBarry Smith 
248947c6ae99SBarry Smith     Output Parameter:
249047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
249147c6ae99SBarry Smith 
249247c6ae99SBarry Smith     Level: developer
249347c6ae99SBarry Smith 
2494e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
249547c6ae99SBarry Smith 
249647c6ae99SBarry Smith @*/
24977087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
249847c6ae99SBarry Smith {
249947c6ae99SBarry Smith   PetscErrorCode ierr;
250047c6ae99SBarry Smith 
250147c6ae99SBarry Smith   PetscFunctionBegin;
2502171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2503ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
250447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
250547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
250647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
250747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
250847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
250947c6ae99SBarry Smith     PetscInt i;
251047c6ae99SBarry Smith 
2511ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
251247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2513ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
251447c6ae99SBarry Smith     }
2515ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
251647c6ae99SBarry Smith   PetscFunctionReturn(0);
251747c6ae99SBarry Smith }
251847c6ae99SBarry Smith 
251947c6ae99SBarry Smith #undef __FUNCT__
2520e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
252147c6ae99SBarry Smith /*@
2522e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
252347c6ae99SBarry Smith    grids associated with two DMs.
252447c6ae99SBarry Smith 
252547c6ae99SBarry Smith    Collective on DM
252647c6ae99SBarry Smith 
252747c6ae99SBarry Smith    Input Parameters:
252847c6ae99SBarry Smith +  dmc - the coarse grid DM
252947c6ae99SBarry Smith -  dmf - the fine grid DM
253047c6ae99SBarry Smith 
253147c6ae99SBarry Smith    Output Parameters:
253247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
253347c6ae99SBarry Smith 
253447c6ae99SBarry Smith    Level: intermediate
253547c6ae99SBarry Smith 
253647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
253747c6ae99SBarry Smith 
2538e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
253947c6ae99SBarry Smith @*/
2540e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
254147c6ae99SBarry Smith {
254247c6ae99SBarry Smith   PetscErrorCode ierr;
254347c6ae99SBarry Smith 
254447c6ae99SBarry Smith   PetscFunctionBegin;
2545171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2546171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
254747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
254847c6ae99SBarry Smith   PetscFunctionReturn(0);
254947c6ae99SBarry Smith }
255047c6ae99SBarry Smith 
255147c6ae99SBarry Smith #undef __FUNCT__
25521a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
25531a266240SBarry Smith /*@C
25541a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
25551a266240SBarry Smith 
25561a266240SBarry Smith     Not Collective
25571a266240SBarry Smith 
25581a266240SBarry Smith     Input Parameters:
25591a266240SBarry Smith +   dm - the DM object
25601a266240SBarry Smith -   destroy - the destroy function
25611a266240SBarry Smith 
25621a266240SBarry Smith     Level: intermediate
25631a266240SBarry Smith 
2564e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
25651a266240SBarry Smith 
2566f07f9ceaSJed Brown @*/
25671a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
25681a266240SBarry Smith {
25691a266240SBarry Smith   PetscFunctionBegin;
2570171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25711a266240SBarry Smith   dm->ctxdestroy = destroy;
25721a266240SBarry Smith   PetscFunctionReturn(0);
25731a266240SBarry Smith }
25741a266240SBarry Smith 
25751a266240SBarry Smith #undef __FUNCT__
25761b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2577b07ff414SBarry Smith /*@
25781b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
257947c6ae99SBarry Smith 
258047c6ae99SBarry Smith     Not Collective
258147c6ae99SBarry Smith 
258247c6ae99SBarry Smith     Input Parameters:
258347c6ae99SBarry Smith +   dm - the DM object
258447c6ae99SBarry Smith -   ctx - the user context
258547c6ae99SBarry Smith 
258647c6ae99SBarry Smith     Level: intermediate
258747c6ae99SBarry Smith 
2588e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
258947c6ae99SBarry Smith 
259047c6ae99SBarry Smith @*/
25911b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
259247c6ae99SBarry Smith {
259347c6ae99SBarry Smith   PetscFunctionBegin;
2594171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
259547c6ae99SBarry Smith   dm->ctx = ctx;
259647c6ae99SBarry Smith   PetscFunctionReturn(0);
259747c6ae99SBarry Smith }
259847c6ae99SBarry Smith 
259947c6ae99SBarry Smith #undef __FUNCT__
26001b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
260147c6ae99SBarry Smith /*@
26021b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
260347c6ae99SBarry Smith 
260447c6ae99SBarry Smith     Not Collective
260547c6ae99SBarry Smith 
260647c6ae99SBarry Smith     Input Parameter:
260747c6ae99SBarry Smith .   dm - the DM object
260847c6ae99SBarry Smith 
260947c6ae99SBarry Smith     Output Parameter:
261047c6ae99SBarry Smith .   ctx - the user context
261147c6ae99SBarry Smith 
261247c6ae99SBarry Smith     Level: intermediate
261347c6ae99SBarry Smith 
2614e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
261547c6ae99SBarry Smith 
261647c6ae99SBarry Smith @*/
26171b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
261847c6ae99SBarry Smith {
261947c6ae99SBarry Smith   PetscFunctionBegin;
2620171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26211b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
262247c6ae99SBarry Smith   PetscFunctionReturn(0);
262347c6ae99SBarry Smith }
262447c6ae99SBarry Smith 
262547c6ae99SBarry Smith #undef __FUNCT__
262608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
262708da532bSDmitry Karpeev /*@C
2628df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
262908da532bSDmitry Karpeev 
263008da532bSDmitry Karpeev     Logically Collective on DM
263108da532bSDmitry Karpeev 
263208da532bSDmitry Karpeev     Input Parameter:
263308da532bSDmitry Karpeev +   dm - the DM object
26340298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
263508da532bSDmitry Karpeev 
263608da532bSDmitry Karpeev     Level: intermediate
263708da532bSDmitry Karpeev 
2638835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
263908da532bSDmitry Karpeev          DMSetJacobian()
264008da532bSDmitry Karpeev 
264108da532bSDmitry Karpeev @*/
264208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
264308da532bSDmitry Karpeev {
264408da532bSDmitry Karpeev   PetscFunctionBegin;
264508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
264608da532bSDmitry Karpeev   PetscFunctionReturn(0);
264708da532bSDmitry Karpeev }
264808da532bSDmitry Karpeev 
264908da532bSDmitry Karpeev #undef __FUNCT__
265008da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
265108da532bSDmitry Karpeev /*@
265208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
265308da532bSDmitry Karpeev 
265408da532bSDmitry Karpeev     Not Collective
265508da532bSDmitry Karpeev 
265608da532bSDmitry Karpeev     Input Parameter:
265708da532bSDmitry Karpeev .   dm - the DM object to destroy
265808da532bSDmitry Karpeev 
265908da532bSDmitry Karpeev     Output Parameter:
266008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
266108da532bSDmitry Karpeev 
266208da532bSDmitry Karpeev     Level: developer
266308da532bSDmitry Karpeev 
266474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
266508da532bSDmitry Karpeev 
266608da532bSDmitry Karpeev @*/
266708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
266808da532bSDmitry Karpeev {
266908da532bSDmitry Karpeev   PetscFunctionBegin;
267008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
267108da532bSDmitry Karpeev   PetscFunctionReturn(0);
267208da532bSDmitry Karpeev }
267308da532bSDmitry Karpeev 
267408da532bSDmitry Karpeev #undef __FUNCT__
267508da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
267608da532bSDmitry Karpeev /*@C
267708da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
267808da532bSDmitry Karpeev 
267908da532bSDmitry Karpeev     Logically Collective on DM
268008da532bSDmitry Karpeev 
268108da532bSDmitry Karpeev     Input Parameters:
2682907376e6SBarry Smith .   dm - the DM object
268308da532bSDmitry Karpeev 
268408da532bSDmitry Karpeev     Output parameters:
268508da532bSDmitry Karpeev +   xl - lower bound
268608da532bSDmitry Karpeev -   xu - upper bound
268708da532bSDmitry Karpeev 
2688907376e6SBarry Smith     Level: advanced
2689907376e6SBarry Smith 
2690907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
269108da532bSDmitry Karpeev 
269274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
269308da532bSDmitry Karpeev 
269408da532bSDmitry Karpeev @*/
269508da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
269608da532bSDmitry Karpeev {
269708da532bSDmitry Karpeev   PetscErrorCode ierr;
26985fd66863SKarl Rupp 
269908da532bSDmitry Karpeev   PetscFunctionBegin;
270008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
270108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
270208da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
270308da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
27048865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
270508da532bSDmitry Karpeev   PetscFunctionReturn(0);
270608da532bSDmitry Karpeev }
270708da532bSDmitry Karpeev 
270808da532bSDmitry Karpeev #undef __FUNCT__
2709b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2710b0ae01b7SPeter Brune /*@
2711b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2712b0ae01b7SPeter Brune 
2713b0ae01b7SPeter Brune     Not Collective
2714b0ae01b7SPeter Brune 
2715b0ae01b7SPeter Brune     Input Parameter:
2716b0ae01b7SPeter Brune .   dm - the DM object
2717b0ae01b7SPeter Brune 
2718b0ae01b7SPeter Brune     Output Parameter:
2719b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2720b0ae01b7SPeter Brune 
2721b0ae01b7SPeter Brune     Level: developer
2722b0ae01b7SPeter Brune 
2723b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2724b0ae01b7SPeter Brune 
2725b0ae01b7SPeter Brune @*/
2726b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2727b0ae01b7SPeter Brune {
2728b0ae01b7SPeter Brune   PetscFunctionBegin;
2729b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2730b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2731b0ae01b7SPeter Brune }
2732b0ae01b7SPeter Brune 
2733b0ae01b7SPeter Brune #undef  __FUNCT__
273408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2735748fac09SDmitry Karpeev /*@C
273608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
273708da532bSDmitry Karpeev 
273808da532bSDmitry Karpeev     Collective on DM
273908da532bSDmitry Karpeev 
274008da532bSDmitry Karpeev     Input Parameter:
274108da532bSDmitry Karpeev +   dm - the DM object
27420298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
274308da532bSDmitry Karpeev 
274408da532bSDmitry Karpeev     Level: developer
274508da532bSDmitry Karpeev 
274674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
274708da532bSDmitry Karpeev 
274808da532bSDmitry Karpeev @*/
274908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
275008da532bSDmitry Karpeev {
275108da532bSDmitry Karpeev   PetscErrorCode ierr;
27525fd66863SKarl Rupp 
275308da532bSDmitry Karpeev   PetscFunctionBegin;
275408da532bSDmitry Karpeev   if (x) {
275508da532bSDmitry Karpeev     if (!dm->x) {
275608da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
275708da532bSDmitry Karpeev     }
275808da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
27598865f1eaSKarl Rupp   } else if (dm->x) {
276008da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
276108da532bSDmitry Karpeev   }
276208da532bSDmitry Karpeev   PetscFunctionReturn(0);
276308da532bSDmitry Karpeev }
276408da532bSDmitry Karpeev 
27650298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2766264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2767264ace61SBarry Smith 
2768264ace61SBarry Smith #undef __FUNCT__
2769264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2770264ace61SBarry Smith /*@C
2771264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2772264ace61SBarry Smith 
2773264ace61SBarry Smith   Collective on DM
2774264ace61SBarry Smith 
2775264ace61SBarry Smith   Input Parameters:
2776264ace61SBarry Smith + dm     - The DM object
2777264ace61SBarry Smith - method - The name of the DM type
2778264ace61SBarry Smith 
2779264ace61SBarry Smith   Options Database Key:
2780264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2781264ace61SBarry Smith 
2782264ace61SBarry Smith   Notes:
2783e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2784264ace61SBarry Smith 
2785264ace61SBarry Smith   Level: intermediate
2786264ace61SBarry Smith 
2787264ace61SBarry Smith .keywords: DM, set, type
2788264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2789264ace61SBarry Smith @*/
279019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2791264ace61SBarry Smith {
2792264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2793264ace61SBarry Smith   PetscBool      match;
2794264ace61SBarry Smith   PetscErrorCode ierr;
2795264ace61SBarry Smith 
2796264ace61SBarry Smith   PetscFunctionBegin;
2797264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2798251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2799264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2800264ace61SBarry Smith 
28010f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
28021c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2803ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2804264ace61SBarry Smith 
2805264ace61SBarry Smith   if (dm->ops->destroy) {
2806264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
28070298fd71SBarry Smith     dm->ops->destroy = NULL;
2808264ace61SBarry Smith   }
2809264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2810264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2811264ace61SBarry Smith   PetscFunctionReturn(0);
2812264ace61SBarry Smith }
2813264ace61SBarry Smith 
2814264ace61SBarry Smith #undef __FUNCT__
2815264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2816264ace61SBarry Smith /*@C
2817264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2818264ace61SBarry Smith 
2819264ace61SBarry Smith   Not Collective
2820264ace61SBarry Smith 
2821264ace61SBarry Smith   Input Parameter:
2822264ace61SBarry Smith . dm  - The DM
2823264ace61SBarry Smith 
2824264ace61SBarry Smith   Output Parameter:
2825264ace61SBarry Smith . type - The DM type name
2826264ace61SBarry Smith 
2827264ace61SBarry Smith   Level: intermediate
2828264ace61SBarry Smith 
2829264ace61SBarry Smith .keywords: DM, get, type, name
2830264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2831264ace61SBarry Smith @*/
283219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2833264ace61SBarry Smith {
2834264ace61SBarry Smith   PetscErrorCode ierr;
2835264ace61SBarry Smith 
2836264ace61SBarry Smith   PetscFunctionBegin;
2837264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2838c959eef4SJed Brown   PetscValidPointer(type,2);
2839607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2840264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2841264ace61SBarry Smith   PetscFunctionReturn(0);
2842264ace61SBarry Smith }
2843264ace61SBarry Smith 
284467a56275SMatthew G Knepley #undef __FUNCT__
284567a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
284667a56275SMatthew G Knepley /*@C
284767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
284867a56275SMatthew G Knepley 
284967a56275SMatthew G Knepley   Collective on DM
285067a56275SMatthew G Knepley 
285167a56275SMatthew G Knepley   Input Parameters:
285267a56275SMatthew G Knepley + dm - the DM
285367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
285467a56275SMatthew G Knepley 
285567a56275SMatthew G Knepley   Output Parameter:
285667a56275SMatthew G Knepley . M - pointer to new DM
285767a56275SMatthew G Knepley 
285867a56275SMatthew G Knepley   Notes:
285967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
286067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
286167a56275SMatthew G Knepley   of the input DM.
286267a56275SMatthew G Knepley 
286367a56275SMatthew G Knepley   Level: intermediate
286467a56275SMatthew G Knepley 
286567a56275SMatthew G Knepley .seealso: DMCreate()
286667a56275SMatthew G Knepley @*/
286719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
286867a56275SMatthew G Knepley {
286967a56275SMatthew G Knepley   DM             B;
287067a56275SMatthew G Knepley   char           convname[256];
287167a56275SMatthew G Knepley   PetscBool      sametype, issame;
287267a56275SMatthew G Knepley   PetscErrorCode ierr;
287367a56275SMatthew G Knepley 
287467a56275SMatthew G Knepley   PetscFunctionBegin;
287567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
287667a56275SMatthew G Knepley   PetscValidType(dm,1);
287767a56275SMatthew G Knepley   PetscValidPointer(M,3);
2878251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
287967a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
288067a56275SMatthew G Knepley   {
28810298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
288267a56275SMatthew G Knepley 
288367a56275SMatthew G Knepley     /*
288467a56275SMatthew G Knepley        Order of precedence:
288567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
288667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
288767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
288867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
288967a56275SMatthew G Knepley        5) Use a really basic converter.
289067a56275SMatthew G Knepley     */
289167a56275SMatthew G Knepley 
289267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
289367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
289467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
289567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
289667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
289767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28980005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
289967a56275SMatthew G Knepley     if (conv) goto foundconv;
290067a56275SMatthew G Knepley 
290167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
290282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
290367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
290467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
290567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
290667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
290767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
290867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29090005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
291067a56275SMatthew G Knepley     if (conv) {
2911fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
291267a56275SMatthew G Knepley       goto foundconv;
291367a56275SMatthew G Knepley     }
291467a56275SMatthew G Knepley 
291567a56275SMatthew G Knepley #if 0
291667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
291767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2918fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
291967a56275SMatthew G Knepley     if (conv) goto foundconv;
292067a56275SMatthew G Knepley 
292167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
292267a56275SMatthew G Knepley     if (dm->ops->convert) {
292367a56275SMatthew G Knepley       conv = dm->ops->convert;
292467a56275SMatthew G Knepley     }
292567a56275SMatthew G Knepley     if (conv) goto foundconv;
292667a56275SMatthew G Knepley #endif
292767a56275SMatthew G Knepley 
292867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
292982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
293067a56275SMatthew G Knepley 
293167a56275SMatthew G Knepley foundconv:
293267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
293367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
293467a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
293567a56275SMatthew G Knepley   }
293667a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
293767a56275SMatthew G Knepley   PetscFunctionReturn(0);
293867a56275SMatthew G Knepley }
2939264ace61SBarry Smith 
2940264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2941264ace61SBarry Smith 
2942264ace61SBarry Smith #undef __FUNCT__
2943264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2944264ace61SBarry Smith /*@C
29451c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
29461c84c290SBarry Smith 
29471c84c290SBarry Smith   Not Collective
29481c84c290SBarry Smith 
29491c84c290SBarry Smith   Input Parameters:
29501c84c290SBarry Smith + name        - The name of a new user-defined creation routine
29511c84c290SBarry Smith - create_func - The creation routine itself
29521c84c290SBarry Smith 
29531c84c290SBarry Smith   Notes:
29541c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
29551c84c290SBarry Smith 
29561c84c290SBarry Smith 
29571c84c290SBarry Smith   Sample usage:
29581c84c290SBarry Smith .vb
2959bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
29601c84c290SBarry Smith .ve
29611c84c290SBarry Smith 
29621c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
29631c84c290SBarry Smith .vb
29641c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
29651c84c290SBarry Smith     DMSetType(DM,"my_da");
29661c84c290SBarry Smith .ve
29671c84c290SBarry Smith    or at runtime via the option
29681c84c290SBarry Smith .vb
29691c84c290SBarry Smith     -da_type my_da
29701c84c290SBarry Smith .ve
2971264ace61SBarry Smith 
2972264ace61SBarry Smith   Level: advanced
29731c84c290SBarry Smith 
29741c84c290SBarry Smith .keywords: DM, register
2975bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
29761c84c290SBarry Smith 
2977264ace61SBarry Smith @*/
2978bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2979264ace61SBarry Smith {
2980264ace61SBarry Smith   PetscErrorCode ierr;
2981264ace61SBarry Smith 
2982264ace61SBarry Smith   PetscFunctionBegin;
2983a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2984264ace61SBarry Smith   PetscFunctionReturn(0);
2985264ace61SBarry Smith }
2986264ace61SBarry Smith 
2987b859378eSBarry Smith #undef __FUNCT__
2988b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2989b859378eSBarry Smith /*@C
299055849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2991b859378eSBarry Smith 
2992b859378eSBarry Smith   Collective on PetscViewer
2993b859378eSBarry Smith 
2994b859378eSBarry Smith   Input Parameters:
2995b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2996b859378eSBarry Smith            some related function before a call to DMLoad().
2997b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2998b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2999b859378eSBarry Smith 
3000b859378eSBarry Smith    Level: intermediate
3001b859378eSBarry Smith 
3002b859378eSBarry Smith   Notes:
300355849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3004b859378eSBarry Smith 
3005b859378eSBarry Smith   Notes for advanced users:
3006b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3007b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3008b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3009b859378eSBarry Smith   format is
3010b859378eSBarry Smith .vb
3011b859378eSBarry Smith      has not yet been determined
3012b859378eSBarry Smith .ve
3013b859378eSBarry Smith 
3014b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3015b859378eSBarry Smith @*/
3016b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3017b859378eSBarry Smith {
30189331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3019b859378eSBarry Smith   PetscErrorCode ierr;
3020b859378eSBarry Smith 
3021b859378eSBarry Smith   PetscFunctionBegin;
3022b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3023b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
302432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
30259331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
30269331c7a4SMatthew G. Knepley   if (isbinary) {
30279331c7a4SMatthew G. Knepley     PetscInt classid;
30289331c7a4SMatthew G. Knepley     char     type[256];
3029b859378eSBarry Smith 
3030060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
30319200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3032060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
303332c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
30349331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30359331c7a4SMatthew G. Knepley   } else if (ishdf5) {
30369331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30379331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3038b859378eSBarry Smith   PetscFunctionReturn(0);
3039b859378eSBarry Smith }
3040b859378eSBarry Smith 
30417da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30427da65231SMatthew G Knepley 
30437da65231SMatthew G Knepley #undef __FUNCT__
30447da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3045a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3046a6dfd86eSKarl Rupp {
30471d47ebbbSSatish Balay   PetscInt       f;
30481b30c384SMatthew G Knepley   PetscErrorCode ierr;
30491b30c384SMatthew G Knepley 
30507da65231SMatthew G Knepley   PetscFunctionBegin;
305174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30521d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
305357622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
30547da65231SMatthew G Knepley   }
30557da65231SMatthew G Knepley   PetscFunctionReturn(0);
30567da65231SMatthew G Knepley }
30577da65231SMatthew G Knepley 
30587da65231SMatthew G Knepley #undef __FUNCT__
30597da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3060a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3061a6dfd86eSKarl Rupp {
30621b30c384SMatthew G Knepley   PetscInt       f, g;
30637da65231SMatthew G Knepley   PetscErrorCode ierr;
30647da65231SMatthew G Knepley 
30657da65231SMatthew G Knepley   PetscFunctionBegin;
306674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30671d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
306874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
30691d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3070e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
30717da65231SMatthew G Knepley     }
307274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
30737da65231SMatthew G Knepley   }
30747da65231SMatthew G Knepley   PetscFunctionReturn(0);
30757da65231SMatthew G Knepley }
3076e7c4fc90SDmitry Karpeev 
3077970e74d5SMatthew G Knepley #undef __FUNCT__
3078e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
30796113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3080e759306cSMatthew G. Knepley {
3081e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3082e759306cSMatthew G. Knepley   PetscInt       p;
3083e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3084e759306cSMatthew G. Knepley 
3085e759306cSMatthew G. Knepley   PetscFunctionBegin;
3086e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3087e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3088e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3089e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3090e759306cSMatthew G. Knepley     if (p == rank) {
3091e759306cSMatthew G. Knepley       Vec x;
3092e759306cSMatthew G. Knepley 
3093e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3094e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
30956113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3096e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3097e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3098e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3099e759306cSMatthew G. Knepley     }
3100e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3101e759306cSMatthew G. Knepley   }
3102e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3103e759306cSMatthew G. Knepley }
3104e759306cSMatthew G. Knepley 
3105e759306cSMatthew G. Knepley #undef __FUNCT__
310688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
310788ed4aceSMatthew G Knepley /*@
310888ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
310988ed4aceSMatthew G Knepley 
311088ed4aceSMatthew G Knepley   Input Parameter:
311188ed4aceSMatthew G Knepley . dm - The DM
311288ed4aceSMatthew G Knepley 
311388ed4aceSMatthew G Knepley   Output Parameter:
311488ed4aceSMatthew G Knepley . section - The PetscSection
311588ed4aceSMatthew G Knepley 
311688ed4aceSMatthew G Knepley   Level: intermediate
311788ed4aceSMatthew G Knepley 
311888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
311988ed4aceSMatthew G Knepley 
312088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
312188ed4aceSMatthew G Knepley @*/
31220adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
31230adebc6cSBarry Smith {
3124fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3125fd59a867SMatthew G. Knepley 
312688ed4aceSMatthew G Knepley   PetscFunctionBegin;
312788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
312888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
31292f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
31302f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3131685405a1SBarry Smith     ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);
31322f0f8703SMatthew G. Knepley   }
313388ed4aceSMatthew G Knepley   *section = dm->defaultSection;
313488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
313588ed4aceSMatthew G Knepley }
313688ed4aceSMatthew G Knepley 
313788ed4aceSMatthew G Knepley #undef __FUNCT__
313888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
313988ed4aceSMatthew G Knepley /*@
314088ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
314188ed4aceSMatthew G Knepley 
314288ed4aceSMatthew G Knepley   Input Parameters:
314388ed4aceSMatthew G Knepley + dm - The DM
314488ed4aceSMatthew G Knepley - section - The PetscSection
314588ed4aceSMatthew G Knepley 
314688ed4aceSMatthew G Knepley   Level: intermediate
314788ed4aceSMatthew G Knepley 
314888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
314988ed4aceSMatthew G Knepley 
315088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
315188ed4aceSMatthew G Knepley @*/
31520adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
31530adebc6cSBarry Smith {
3154c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3155af122d2aSMatthew G Knepley   PetscInt       f;
315688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
315788ed4aceSMatthew G Knepley 
315888ed4aceSMatthew G Knepley   PetscFunctionBegin;
315988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3160c473ab19SMatthew G. Knepley   if (section) {
31611d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
31621d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3163c473ab19SMatthew G. Knepley   }
316488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
316588ed4aceSMatthew G Knepley   dm->defaultSection = section;
3166c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3167af122d2aSMatthew G Knepley   if (numFields) {
3168af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3169af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
31700f21e855SMatthew G. Knepley       PetscObject disc;
3171af122d2aSMatthew G Knepley       const char *name;
3172af122d2aSMatthew G Knepley 
3173af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
31740f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
31750f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3176af122d2aSMatthew G Knepley     }
3177af122d2aSMatthew G Knepley   }
31781d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
31791d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
318088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
318188ed4aceSMatthew G Knepley }
318288ed4aceSMatthew G Knepley 
318388ed4aceSMatthew G Knepley #undef __FUNCT__
31849435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
31859435951eSToby Isaac /*@
31869435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
31879435951eSToby Isaac 
3188e228b242SToby Isaac   not collective
3189e228b242SToby Isaac 
31909435951eSToby Isaac   Input Parameter:
31919435951eSToby Isaac . dm - The DM
31929435951eSToby Isaac 
31939435951eSToby Isaac   Output Parameter:
31949435951eSToby 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.
31959435951eSToby 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.
31969435951eSToby Isaac 
31979435951eSToby Isaac   Level: advanced
31989435951eSToby Isaac 
31999435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
32009435951eSToby Isaac 
32019435951eSToby Isaac .seealso: DMSetDefaultConstraints()
32029435951eSToby Isaac @*/
32039435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
32049435951eSToby Isaac {
32059435951eSToby Isaac   PetscErrorCode ierr;
32069435951eSToby Isaac 
32079435951eSToby Isaac   PetscFunctionBegin;
32089435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32099435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
321045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
321145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
32129435951eSToby Isaac   PetscFunctionReturn(0);
32139435951eSToby Isaac }
32149435951eSToby Isaac 
32159435951eSToby Isaac #undef __FUNCT__
32169435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
32179435951eSToby Isaac /*@
32189435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
32199435951eSToby Isaac 
32209435951eSToby 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().
32219435951eSToby Isaac 
32229435951eSToby 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.
32239435951eSToby Isaac 
3224e228b242SToby Isaac   collective on dm
3225e228b242SToby Isaac 
32269435951eSToby Isaac   Input Parameters:
32279435951eSToby Isaac + dm - The DM
3228e228b242SToby 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).
3229e228b242SToby 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).
32309435951eSToby Isaac 
32319435951eSToby Isaac   Level: advanced
32329435951eSToby Isaac 
32339435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32349435951eSToby Isaac 
32359435951eSToby Isaac .seealso: DMGetDefaultConstraints()
32369435951eSToby Isaac @*/
32379435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
32389435951eSToby Isaac {
3239e228b242SToby Isaac   PetscMPIInt result;
32409435951eSToby Isaac   PetscErrorCode ierr;
32419435951eSToby Isaac 
32429435951eSToby Isaac   PetscFunctionBegin;
32439435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3244e228b242SToby Isaac   if (section) {
3245e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3246e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3247e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3248e228b242SToby Isaac   }
3249e228b242SToby Isaac   if (mat) {
3250e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3251e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3252e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3253e228b242SToby Isaac   }
32549435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
32559435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
32569435951eSToby Isaac   dm->defaultConstraintSection = section;
32579435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
32589435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
32599435951eSToby Isaac   dm->defaultConstraintMat = mat;
32609435951eSToby Isaac   PetscFunctionReturn(0);
32619435951eSToby Isaac }
32629435951eSToby Isaac 
3263f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
32649435951eSToby Isaac #undef __FUNCT__
3265284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3266507e4973SMatthew G. Knepley /*
3267507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3268507e4973SMatthew G. Knepley 
3269507e4973SMatthew G. Knepley   Input Parameters:
3270507e4973SMatthew G. Knepley + dm - The DM
3271507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3272507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3273507e4973SMatthew G. Knepley 
3274507e4973SMatthew G. Knepley   Level: intermediate
3275507e4973SMatthew G. Knepley 
3276507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3277507e4973SMatthew G. Knepley */
3278f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3279507e4973SMatthew G. Knepley {
3280507e4973SMatthew G. Knepley   MPI_Comm        comm;
3281507e4973SMatthew G. Knepley   PetscLayout     layout;
3282507e4973SMatthew G. Knepley   const PetscInt *ranges;
3283507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3284507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3285507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3286507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3287507e4973SMatthew G. Knepley 
3288507e4973SMatthew G. Knepley   PetscFunctionBegin;
3289507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3290507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3291507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3292507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3293507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3294507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3295507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3296507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3297507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3298507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3299507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3300507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3301f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3302507e4973SMatthew G. Knepley 
3303507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3304507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3305507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3306507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3307507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3308507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3309507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3310507e4973SMatthew 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;}
3311507e4973SMatthew 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;}
3312507e4973SMatthew G. Knepley     if (gdof < 0) {
3313507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3314507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3315507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3316507e4973SMatthew G. Knepley 
3317507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3318507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3319507e4973SMatthew 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;}
3320507e4973SMatthew G. Knepley       }
3321507e4973SMatthew G. Knepley     }
3322507e4973SMatthew G. Knepley   }
3323507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3324507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3325b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3326507e4973SMatthew G. Knepley   if (!gvalid) {
3327507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3328507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3329507e4973SMatthew G. Knepley   }
3330507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3331507e4973SMatthew G. Knepley }
3332f741bcd2SMatthew G. Knepley #endif
3333507e4973SMatthew G. Knepley 
3334507e4973SMatthew G. Knepley #undef __FUNCT__
333588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
333688ed4aceSMatthew G Knepley /*@
333788ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
333888ed4aceSMatthew G Knepley 
33398b1ab98fSJed Brown   Collective on DM
33408b1ab98fSJed Brown 
334188ed4aceSMatthew G Knepley   Input Parameter:
334288ed4aceSMatthew G Knepley . dm - The DM
334388ed4aceSMatthew G Knepley 
334488ed4aceSMatthew G Knepley   Output Parameter:
334588ed4aceSMatthew G Knepley . section - The PetscSection
334688ed4aceSMatthew G Knepley 
334788ed4aceSMatthew G Knepley   Level: intermediate
334888ed4aceSMatthew G Knepley 
334988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
335088ed4aceSMatthew G Knepley 
335188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
335288ed4aceSMatthew G Knepley @*/
33530adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
33540adebc6cSBarry Smith {
335588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
335688ed4aceSMatthew G Knepley 
335788ed4aceSMatthew G Knepley   PetscFunctionBegin;
335888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
335988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
336088ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3361fd59a867SMatthew G. Knepley     PetscSection s;
3362fd59a867SMatthew G. Knepley 
3363fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3364fd59a867SMatthew 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");
3365fd59a867SMatthew 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");
336615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3367cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3368ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3369685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
337088ed4aceSMatthew G Knepley   }
337188ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
337288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
337388ed4aceSMatthew G Knepley }
337488ed4aceSMatthew G Knepley 
337588ed4aceSMatthew G Knepley #undef __FUNCT__
3376b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3377b21d0597SMatthew G Knepley /*@
3378b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3379b21d0597SMatthew G Knepley 
3380b21d0597SMatthew G Knepley   Input Parameters:
3381b21d0597SMatthew G Knepley + dm - The DM
33825080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3383b21d0597SMatthew G Knepley 
3384b21d0597SMatthew G Knepley   Level: intermediate
3385b21d0597SMatthew G Knepley 
3386b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3387b21d0597SMatthew G Knepley 
3388b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3389b21d0597SMatthew G Knepley @*/
33900adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
33910adebc6cSBarry Smith {
3392b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3393b21d0597SMatthew G Knepley 
3394b21d0597SMatthew G Knepley   PetscFunctionBegin;
3395b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33965080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33971d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3398b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3399b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3400507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3401f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3402507e4973SMatthew G. Knepley #endif
3403b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3404b21d0597SMatthew G Knepley }
3405b21d0597SMatthew G Knepley 
3406b21d0597SMatthew G Knepley #undef __FUNCT__
340788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
340888ed4aceSMatthew G Knepley /*@
340988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
341088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
341188ed4aceSMatthew G Knepley 
341288ed4aceSMatthew G Knepley   Input Parameter:
341388ed4aceSMatthew G Knepley . dm - The DM
341488ed4aceSMatthew G Knepley 
341588ed4aceSMatthew G Knepley   Output Parameter:
341688ed4aceSMatthew G Knepley . sf - The PetscSF
341788ed4aceSMatthew G Knepley 
341888ed4aceSMatthew G Knepley   Level: intermediate
341988ed4aceSMatthew G Knepley 
342088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
342188ed4aceSMatthew G Knepley 
342288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
342388ed4aceSMatthew G Knepley @*/
34240adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
34250adebc6cSBarry Smith {
342688ed4aceSMatthew G Knepley   PetscInt       nroots;
342788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
342888ed4aceSMatthew G Knepley 
342988ed4aceSMatthew G Knepley   PetscFunctionBegin;
343088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
343188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34320298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
343388ed4aceSMatthew G Knepley   if (nroots < 0) {
343488ed4aceSMatthew G Knepley     PetscSection section, gSection;
343588ed4aceSMatthew G Knepley 
343688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
343731ea6d37SMatthew G Knepley     if (section) {
343888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
343988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
344031ea6d37SMatthew G Knepley     } else {
34410298fd71SBarry Smith       *sf = NULL;
344231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
344331ea6d37SMatthew G Knepley     }
344488ed4aceSMatthew G Knepley   }
344588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
344688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
344788ed4aceSMatthew G Knepley }
344888ed4aceSMatthew G Knepley 
344988ed4aceSMatthew G Knepley #undef __FUNCT__
345088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
345188ed4aceSMatthew G Knepley /*@
345288ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
345388ed4aceSMatthew G Knepley 
345488ed4aceSMatthew G Knepley   Input Parameters:
345588ed4aceSMatthew G Knepley + dm - The DM
345688ed4aceSMatthew G Knepley - sf - The PetscSF
345788ed4aceSMatthew G Knepley 
345888ed4aceSMatthew G Knepley   Level: intermediate
345988ed4aceSMatthew G Knepley 
346088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
346188ed4aceSMatthew G Knepley 
346288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
346388ed4aceSMatthew G Knepley @*/
34640adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
34650adebc6cSBarry Smith {
346688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
346788ed4aceSMatthew G Knepley 
346888ed4aceSMatthew G Knepley   PetscFunctionBegin;
346988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
347088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
347188ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
347288ed4aceSMatthew G Knepley   dm->defaultSF = sf;
347388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
347488ed4aceSMatthew G Knepley }
347588ed4aceSMatthew G Knepley 
347688ed4aceSMatthew G Knepley #undef __FUNCT__
347788ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
347888ed4aceSMatthew G Knepley /*@C
347988ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
348088ed4aceSMatthew G Knepley   describing the data layout.
348188ed4aceSMatthew G Knepley 
348288ed4aceSMatthew G Knepley   Input Parameters:
348388ed4aceSMatthew G Knepley + dm - The DM
348488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
348588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
348688ed4aceSMatthew G Knepley 
348788ed4aceSMatthew G Knepley   Level: intermediate
348888ed4aceSMatthew G Knepley 
348988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
349088ed4aceSMatthew G Knepley @*/
349188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
349288ed4aceSMatthew G Knepley {
349382f516ccSBarry Smith   MPI_Comm       comm;
349488ed4aceSMatthew G Knepley   PetscLayout    layout;
349588ed4aceSMatthew G Knepley   const PetscInt *ranges;
349688ed4aceSMatthew G Knepley   PetscInt       *local;
349788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3498ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
349988ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
350088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
350188ed4aceSMatthew G Knepley 
350288ed4aceSMatthew G Knepley   PetscFunctionBegin;
350382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
350488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
350588ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
350688ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
350788ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
350888ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
350988ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
351088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
351188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
351288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
351388ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3514ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35156636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
351688ed4aceSMatthew G Knepley 
35176636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35186636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3519235fbf56SMatthew 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));
35206636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
352188ed4aceSMatthew G Knepley   }
3522785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3523785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
352488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
35251f588964SMatthew G Knepley     const PetscInt *cind;
35266636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
352788ed4aceSMatthew G Knepley 
352888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
352988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
353088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
353188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
353288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35336636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
353488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35356636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35366636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35376636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3538057b4bcdSMatthew 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);
35396636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35406636e97aSMatthew G Knepley     }
354188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
354288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
354388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
354488ed4aceSMatthew G Knepley     }
354588ed4aceSMatthew G Knepley     if (gdof < 0) {
35466636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
354788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
354888ed4aceSMatthew G Knepley 
354905376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
355031d3f06eSJed Brown         if (r < 0) r = -(r+2);
355105376888SMatthew 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);
355288ed4aceSMatthew G Knepley         remote[l].rank  = r;
355388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
355488ed4aceSMatthew G Knepley       }
355588ed4aceSMatthew G Knepley     } else {
35566636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
355788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
355888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
355988ed4aceSMatthew G Knepley       }
356088ed4aceSMatthew G Knepley     }
356188ed4aceSMatthew G Knepley   }
35626636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
356388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
356488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
356588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
356688ed4aceSMatthew G Knepley }
3567af122d2aSMatthew G Knepley 
3568af122d2aSMatthew G Knepley #undef __FUNCT__
3569b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3570b21d0597SMatthew G Knepley /*@
3571b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3572b21d0597SMatthew G Knepley 
3573b21d0597SMatthew G Knepley   Input Parameter:
3574b21d0597SMatthew G Knepley . dm - The DM
3575b21d0597SMatthew G Knepley 
3576b21d0597SMatthew G Knepley   Output Parameter:
3577b21d0597SMatthew G Knepley . sf - The PetscSF
3578b21d0597SMatthew G Knepley 
3579b21d0597SMatthew G Knepley   Level: intermediate
3580b21d0597SMatthew G Knepley 
3581b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3582b21d0597SMatthew G Knepley 
3583057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3584b21d0597SMatthew G Knepley @*/
35850adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
35860adebc6cSBarry Smith {
3587b21d0597SMatthew G Knepley   PetscFunctionBegin;
3588b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3589b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3590b21d0597SMatthew G Knepley   *sf = dm->sf;
3591b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3592b21d0597SMatthew G Knepley }
3593b21d0597SMatthew G Knepley 
3594b21d0597SMatthew G Knepley #undef __FUNCT__
3595057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3596057b4bcdSMatthew G Knepley /*@
3597057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3598057b4bcdSMatthew G Knepley 
3599057b4bcdSMatthew G Knepley   Input Parameters:
3600057b4bcdSMatthew G Knepley + dm - The DM
3601057b4bcdSMatthew G Knepley - sf - The PetscSF
3602057b4bcdSMatthew G Knepley 
3603057b4bcdSMatthew G Knepley   Level: intermediate
3604057b4bcdSMatthew G Knepley 
3605057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3606057b4bcdSMatthew G Knepley @*/
36070adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
36080adebc6cSBarry Smith {
3609057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3610057b4bcdSMatthew G Knepley 
3611057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3612057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3613057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3614057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3615057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3616057b4bcdSMatthew G Knepley   dm->sf = sf;
3617057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3618057b4bcdSMatthew G Knepley }
3619057b4bcdSMatthew G Knepley 
3620057b4bcdSMatthew G Knepley #undef __FUNCT__
36212764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
36222764a2aaSMatthew G. Knepley /*@
36232764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
36242764a2aaSMatthew G. Knepley 
36252764a2aaSMatthew G. Knepley   Input Parameter:
36262764a2aaSMatthew G. Knepley . dm - The DM
36272764a2aaSMatthew G. Knepley 
36282764a2aaSMatthew G. Knepley   Output Parameter:
36292764a2aaSMatthew G. Knepley . prob - The PetscDS
36302764a2aaSMatthew G. Knepley 
36312764a2aaSMatthew G. Knepley   Level: developer
36322764a2aaSMatthew G. Knepley 
36332764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36342764a2aaSMatthew G. Knepley @*/
36352764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3636af122d2aSMatthew G Knepley {
3637af122d2aSMatthew G Knepley   PetscFunctionBegin;
3638af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36390f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36400f21e855SMatthew G. Knepley   *prob = dm->prob;
36410f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36420f21e855SMatthew G. Knepley }
36430f21e855SMatthew G. Knepley 
36440f21e855SMatthew G. Knepley #undef __FUNCT__
36452764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
36462764a2aaSMatthew G. Knepley /*@
36472764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36482764a2aaSMatthew G. Knepley 
36492764a2aaSMatthew G. Knepley   Input Parameters:
36502764a2aaSMatthew G. Knepley + dm - The DM
36512764a2aaSMatthew G. Knepley - prob - The PetscDS
36522764a2aaSMatthew G. Knepley 
36532764a2aaSMatthew G. Knepley   Level: developer
36542764a2aaSMatthew G. Knepley 
36552764a2aaSMatthew G. Knepley .seealso: DMGetDS()
36562764a2aaSMatthew G. Knepley @*/
36572764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
36580f21e855SMatthew G. Knepley {
36590f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36600f21e855SMatthew G. Knepley 
36610f21e855SMatthew G. Knepley   PetscFunctionBegin;
36620f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36632764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
36642764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
36650f21e855SMatthew G. Knepley   dm->prob = prob;
36660f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
36670f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36680f21e855SMatthew G. Knepley }
36690f21e855SMatthew G. Knepley 
36700f21e855SMatthew G. Knepley #undef __FUNCT__
36710f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
36720f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
36730f21e855SMatthew G. Knepley {
36740f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36750f21e855SMatthew G. Knepley 
36760f21e855SMatthew G. Knepley   PetscFunctionBegin;
36770f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36782764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3679af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3680af122d2aSMatthew G Knepley }
3681af122d2aSMatthew G Knepley 
3682af122d2aSMatthew G Knepley #undef __FUNCT__
3683af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3684af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3685af122d2aSMatthew G Knepley {
36860f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3687af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3688af122d2aSMatthew G Knepley 
3689af122d2aSMatthew G Knepley   PetscFunctionBegin;
3690af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36912764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
36920f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
36930f21e855SMatthew G. Knepley     PetscContainer obj;
36940f21e855SMatthew G. Knepley 
36950f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
36962764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
36970f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3698af122d2aSMatthew G Knepley   }
3699af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3700af122d2aSMatthew G Knepley }
3701af122d2aSMatthew G Knepley 
3702af122d2aSMatthew G Knepley #undef __FUNCT__
3703af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3704c1929be8SMatthew G. Knepley /*@
3705c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3706c1929be8SMatthew G. Knepley 
3707c1929be8SMatthew G. Knepley   Not collective
3708c1929be8SMatthew G. Knepley 
3709c1929be8SMatthew G. Knepley   Input Parameters:
3710c1929be8SMatthew G. Knepley + dm - The DM
3711c1929be8SMatthew G. Knepley - f  - The field number
3712c1929be8SMatthew G. Knepley 
3713c1929be8SMatthew G. Knepley   Output Parameter:
3714c1929be8SMatthew G. Knepley . field - The discretization object
3715c1929be8SMatthew G. Knepley 
3716c1929be8SMatthew G. Knepley   Level: developer
3717c1929be8SMatthew G. Knepley 
3718c1929be8SMatthew G. Knepley .seealso: DMSetField()
3719c1929be8SMatthew G. Knepley @*/
3720af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3721af122d2aSMatthew G Knepley {
37220f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37230f21e855SMatthew G. Knepley 
3724af122d2aSMatthew G Knepley   PetscFunctionBegin;
3725af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37262764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3727decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3728decb47aaSMatthew G. Knepley }
3729decb47aaSMatthew G. Knepley 
3730decb47aaSMatthew G. Knepley #undef __FUNCT__
3731decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3732c1929be8SMatthew G. Knepley /*@
3733c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3734c1929be8SMatthew G. Knepley 
3735c1929be8SMatthew G. Knepley   Logically collective on DM
3736c1929be8SMatthew G. Knepley 
3737c1929be8SMatthew G. Knepley   Input Parameters:
3738c1929be8SMatthew G. Knepley + dm - The DM
3739c1929be8SMatthew G. Knepley . f  - The field number
3740c1929be8SMatthew G. Knepley - field - The discretization object
3741c1929be8SMatthew G. Knepley 
3742c1929be8SMatthew G. Knepley   Level: developer
3743c1929be8SMatthew G. Knepley 
3744c1929be8SMatthew G. Knepley .seealso: DMGetField()
3745c1929be8SMatthew G. Knepley @*/
3746decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3747decb47aaSMatthew G. Knepley {
3748decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3749decb47aaSMatthew G. Knepley 
3750decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3751decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37522764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3753af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3754af122d2aSMatthew G Knepley }
37556636e97aSMatthew G Knepley 
37566636e97aSMatthew G Knepley #undef __FUNCT__
3757b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3758b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3759b64e0483SPeter Brune {
3760b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3761b64e0483SPeter Brune   PetscErrorCode ierr;
3762b64e0483SPeter Brune   Vec coords,ccoords;
37636dbf9973SLawrence Mitchell   Mat inject;
3764b64e0483SPeter Brune   PetscFunctionBegin;
3765b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3766b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3767b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3768b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3769b64e0483SPeter Brune   if (coords && !ccoords) {
3770b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
37716dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
37722adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
37736dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3774b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3775b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3776b64e0483SPeter Brune   }
3777b64e0483SPeter Brune   PetscFunctionReturn(0);
3778b64e0483SPeter Brune }
3779b64e0483SPeter Brune 
3780b64e0483SPeter Brune #undef __FUNCT__
378103dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
378203dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
378303dadc2fSPeter Brune {
378403dadc2fSPeter Brune   DM dm_coord,subdm_coord;
378503dadc2fSPeter Brune   PetscErrorCode ierr;
378603dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
378703dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
378803dadc2fSPeter Brune   PetscFunctionBegin;
378903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
379003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
379103dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
379203dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
379303dadc2fSPeter Brune   if (coords && !ccoords) {
379403dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
379503dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
379624640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
379703dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
379803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
379903dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
380003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
380103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
380203dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
380303dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
380403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
380503dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
380603dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
380703dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
380803dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
380903dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
381003dadc2fSPeter Brune   }
381103dadc2fSPeter Brune   PetscFunctionReturn(0);
381203dadc2fSPeter Brune }
381303dadc2fSPeter Brune 
381403dadc2fSPeter Brune #undef __FUNCT__
3815c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3816c73cfb54SMatthew G. Knepley /*@
3817c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3818c73cfb54SMatthew G. Knepley 
3819c73cfb54SMatthew G. Knepley   Not collective
3820c73cfb54SMatthew G. Knepley 
3821c73cfb54SMatthew G. Knepley   Input Parameter:
3822c73cfb54SMatthew G. Knepley . dm - The DM
3823c73cfb54SMatthew G. Knepley 
3824c73cfb54SMatthew G. Knepley   Output Parameter:
3825c73cfb54SMatthew G. Knepley . dim - The topological dimension
3826c73cfb54SMatthew G. Knepley 
3827c73cfb54SMatthew G. Knepley   Level: beginner
3828c73cfb54SMatthew G. Knepley 
3829c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3830c73cfb54SMatthew G. Knepley @*/
3831c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3832c73cfb54SMatthew G. Knepley {
3833c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3834c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3835c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3836c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3837c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3838c73cfb54SMatthew G. Knepley }
3839c73cfb54SMatthew G. Knepley 
3840c73cfb54SMatthew G. Knepley #undef __FUNCT__
3841c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3842c73cfb54SMatthew G. Knepley /*@
3843c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3844c73cfb54SMatthew G. Knepley 
3845c73cfb54SMatthew G. Knepley   Collective on dm
3846c73cfb54SMatthew G. Knepley 
3847c73cfb54SMatthew G. Knepley   Input Parameters:
3848c73cfb54SMatthew G. Knepley + dm - The DM
3849c73cfb54SMatthew G. Knepley - dim - The topological dimension
3850c73cfb54SMatthew G. Knepley 
3851c73cfb54SMatthew G. Knepley   Level: beginner
3852c73cfb54SMatthew G. Knepley 
3853c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3854c73cfb54SMatthew G. Knepley @*/
3855c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3856c73cfb54SMatthew G. Knepley {
3857c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3858c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3859c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3860c73cfb54SMatthew G. Knepley   dm->dim = dim;
3861c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3862c73cfb54SMatthew G. Knepley }
3863c73cfb54SMatthew G. Knepley 
3864793f3fe5SMatthew G. Knepley #undef __FUNCT__
3865793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3866793f3fe5SMatthew G. Knepley /*@
3867793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3868793f3fe5SMatthew G. Knepley 
3869793f3fe5SMatthew G. Knepley   Collective on DM
3870793f3fe5SMatthew G. Knepley 
3871793f3fe5SMatthew G. Knepley   Input Parameters:
3872793f3fe5SMatthew G. Knepley + dm - the DM
3873793f3fe5SMatthew G. Knepley - dim - the dimension
3874793f3fe5SMatthew G. Knepley 
3875793f3fe5SMatthew G. Knepley   Output Parameters:
3876793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3877793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3878793f3fe5SMatthew G. Knepley 
3879793f3fe5SMatthew G. Knepley   Note:
3880793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3881793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3882793f3fe5SMatthew G. Knepley   then the interval is empty.
3883793f3fe5SMatthew G. Knepley 
3884793f3fe5SMatthew G. Knepley   Level: intermediate
3885793f3fe5SMatthew G. Knepley 
3886793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3887793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3888793f3fe5SMatthew G. Knepley @*/
3889793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3890793f3fe5SMatthew G. Knepley {
3891793f3fe5SMatthew G. Knepley   PetscInt       d;
3892793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3893793f3fe5SMatthew G. Knepley 
3894793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3895793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3896793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3897793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3898793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3899793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3900793f3fe5SMatthew G. Knepley }
3901793f3fe5SMatthew G. Knepley 
3902793f3fe5SMatthew G. Knepley #undef __FUNCT__
39036636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
39046636e97aSMatthew G Knepley /*@
39056636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
39066636e97aSMatthew G Knepley 
39076636e97aSMatthew G Knepley   Collective on DM
39086636e97aSMatthew G Knepley 
39096636e97aSMatthew G Knepley   Input Parameters:
39106636e97aSMatthew G Knepley + dm - the DM
39116636e97aSMatthew G Knepley - c - coordinate vector
39126636e97aSMatthew G Knepley 
39136636e97aSMatthew G Knepley   Note:
39146636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
39156636e97aSMatthew G Knepley 
39166636e97aSMatthew G Knepley   Level: intermediate
39176636e97aSMatthew G Knepley 
39186636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39196636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
39206636e97aSMatthew G Knepley @*/
39216636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
39226636e97aSMatthew G Knepley {
39236636e97aSMatthew G Knepley   PetscErrorCode ierr;
39246636e97aSMatthew G Knepley 
39256636e97aSMatthew G Knepley   PetscFunctionBegin;
39266636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39276636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39286636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39296636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39306636e97aSMatthew G Knepley   dm->coordinates = c;
39316636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3932b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
393303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39356636e97aSMatthew G Knepley }
39366636e97aSMatthew G Knepley 
39376636e97aSMatthew G Knepley #undef __FUNCT__
39386636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
39396636e97aSMatthew G Knepley /*@
39406636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39416636e97aSMatthew G Knepley 
39426636e97aSMatthew G Knepley   Collective on DM
39436636e97aSMatthew G Knepley 
39446636e97aSMatthew G Knepley    Input Parameters:
39456636e97aSMatthew G Knepley +  dm - the DM
39466636e97aSMatthew G Knepley -  c - coordinate vector
39476636e97aSMatthew G Knepley 
39486636e97aSMatthew G Knepley   Note:
39496636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39506636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39516636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39526636e97aSMatthew G Knepley 
39536636e97aSMatthew G Knepley   Level: intermediate
39546636e97aSMatthew G Knepley 
39556636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39566636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
39576636e97aSMatthew G Knepley @*/
39586636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
39596636e97aSMatthew G Knepley {
39606636e97aSMatthew G Knepley   PetscErrorCode ierr;
39616636e97aSMatthew G Knepley 
39626636e97aSMatthew G Knepley   PetscFunctionBegin;
39636636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39646636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39656636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39666636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
39678865f1eaSKarl Rupp 
39686636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
39698865f1eaSKarl Rupp 
39706636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39716636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39726636e97aSMatthew G Knepley }
39736636e97aSMatthew G Knepley 
39746636e97aSMatthew G Knepley #undef __FUNCT__
39756636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
39766636e97aSMatthew G Knepley /*@
39776636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
39786636e97aSMatthew G Knepley 
39796636e97aSMatthew G Knepley   Not Collective
39806636e97aSMatthew G Knepley 
39816636e97aSMatthew G Knepley   Input Parameter:
39826636e97aSMatthew G Knepley . dm - the DM
39836636e97aSMatthew G Knepley 
39846636e97aSMatthew G Knepley   Output Parameter:
39856636e97aSMatthew G Knepley . c - global coordinate vector
39866636e97aSMatthew G Knepley 
39876636e97aSMatthew G Knepley   Note:
39886636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
39896636e97aSMatthew G Knepley 
39906636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
39916636e97aSMatthew G Knepley 
39926636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
39936636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
39946636e97aSMatthew G Knepley 
39956636e97aSMatthew G Knepley   Level: intermediate
39966636e97aSMatthew G Knepley 
39976636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39986636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
39996636e97aSMatthew G Knepley @*/
40006636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
40016636e97aSMatthew G Knepley {
40026636e97aSMatthew G Knepley   PetscErrorCode ierr;
40036636e97aSMatthew G Knepley 
40046636e97aSMatthew G Knepley   PetscFunctionBegin;
40056636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40066636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40071f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
40080298fd71SBarry Smith     DM cdm = NULL;
40096636e97aSMatthew G Knepley 
40106636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40116636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
40126636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
40136636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40146636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40156636e97aSMatthew G Knepley   }
40166636e97aSMatthew G Knepley   *c = dm->coordinates;
40176636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40186636e97aSMatthew G Knepley }
40196636e97aSMatthew G Knepley 
40206636e97aSMatthew G Knepley #undef __FUNCT__
40216636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
40226636e97aSMatthew G Knepley /*@
40236636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
40246636e97aSMatthew G Knepley 
40256636e97aSMatthew G Knepley   Collective on DM
40266636e97aSMatthew G Knepley 
40276636e97aSMatthew G Knepley   Input Parameter:
40286636e97aSMatthew G Knepley . dm - the DM
40296636e97aSMatthew G Knepley 
40306636e97aSMatthew G Knepley   Output Parameter:
40316636e97aSMatthew G Knepley . c - coordinate vector
40326636e97aSMatthew G Knepley 
40336636e97aSMatthew G Knepley   Note:
40346636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40356636e97aSMatthew G Knepley 
40366636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40376636e97aSMatthew G Knepley 
40386636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40396636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40406636e97aSMatthew G Knepley 
40416636e97aSMatthew G Knepley   Level: intermediate
40426636e97aSMatthew G Knepley 
40436636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40446636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40456636e97aSMatthew G Knepley @*/
40466636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40476636e97aSMatthew G Knepley {
40486636e97aSMatthew G Knepley   PetscErrorCode ierr;
40496636e97aSMatthew G Knepley 
40506636e97aSMatthew G Knepley   PetscFunctionBegin;
40516636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40526636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40531f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40540298fd71SBarry Smith     DM cdm = NULL;
40556636e97aSMatthew G Knepley 
40566636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40576636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
40586636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
40596636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40606636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40616636e97aSMatthew G Knepley   }
40626636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
40636636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40646636e97aSMatthew G Knepley }
40656636e97aSMatthew G Knepley 
40666636e97aSMatthew G Knepley #undef __FUNCT__
40676636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
40686636e97aSMatthew G Knepley /*@
40691cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
40706636e97aSMatthew G Knepley 
40716636e97aSMatthew G Knepley   Collective on DM
40726636e97aSMatthew G Knepley 
40736636e97aSMatthew G Knepley   Input Parameter:
40746636e97aSMatthew G Knepley . dm - the DM
40756636e97aSMatthew G Knepley 
40766636e97aSMatthew G Knepley   Output Parameter:
40776636e97aSMatthew G Knepley . cdm - coordinate DM
40786636e97aSMatthew G Knepley 
40796636e97aSMatthew G Knepley   Level: intermediate
40806636e97aSMatthew G Knepley 
40816636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40821cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
40836636e97aSMatthew G Knepley @*/
40846636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
40856636e97aSMatthew G Knepley {
40866636e97aSMatthew G Knepley   PetscErrorCode ierr;
40876636e97aSMatthew G Knepley 
40886636e97aSMatthew G Knepley   PetscFunctionBegin;
40896636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40906636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
40916636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
409282f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
40936636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
40946636e97aSMatthew G Knepley   }
40956636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
40966636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40976636e97aSMatthew G Knepley }
4098e87bb0d3SMatthew G Knepley 
4099e87bb0d3SMatthew G Knepley #undef __FUNCT__
41001cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
41011cfe2091SMatthew G. Knepley /*@
41021cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
41031cfe2091SMatthew G. Knepley 
41041cfe2091SMatthew G. Knepley   Logically Collective on DM
41051cfe2091SMatthew G. Knepley 
41061cfe2091SMatthew G. Knepley   Input Parameters:
41071cfe2091SMatthew G. Knepley + dm - the DM
41081cfe2091SMatthew G. Knepley - cdm - coordinate DM
41091cfe2091SMatthew G. Knepley 
41101cfe2091SMatthew G. Knepley   Level: intermediate
41111cfe2091SMatthew G. Knepley 
41121cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41131cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41141cfe2091SMatthew G. Knepley @*/
41151cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
41161cfe2091SMatthew G. Knepley {
41171cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
41181cfe2091SMatthew G. Knepley 
41191cfe2091SMatthew G. Knepley   PetscFunctionBegin;
41201cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41211cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
41221cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
41231cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
41241cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
41251cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
41261cfe2091SMatthew G. Knepley }
41271cfe2091SMatthew G. Knepley 
41281cfe2091SMatthew G. Knepley #undef __FUNCT__
412946e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
413046e270d4SMatthew G. Knepley /*@
413146e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
413246e270d4SMatthew G. Knepley 
413346e270d4SMatthew G. Knepley   Not Collective
413446e270d4SMatthew G. Knepley 
413546e270d4SMatthew G. Knepley   Input Parameter:
413646e270d4SMatthew G. Knepley . dm - The DM object
413746e270d4SMatthew G. Knepley 
413846e270d4SMatthew G. Knepley   Output Parameter:
413946e270d4SMatthew G. Knepley . dim - The embedding dimension
414046e270d4SMatthew G. Knepley 
414146e270d4SMatthew G. Knepley   Level: intermediate
414246e270d4SMatthew G. Knepley 
414346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
414446e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
414546e270d4SMatthew G. Knepley @*/
414646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
414746e270d4SMatthew G. Knepley {
414846e270d4SMatthew G. Knepley   PetscFunctionBegin;
414946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
415046e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41519a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41529a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41539a9a41abSToby Isaac   }
415446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
415546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
415646e270d4SMatthew G. Knepley }
415746e270d4SMatthew G. Knepley 
415846e270d4SMatthew G. Knepley #undef __FUNCT__
415946e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
416046e270d4SMatthew G. Knepley /*@
416146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
416246e270d4SMatthew G. Knepley 
416346e270d4SMatthew G. Knepley   Not Collective
416446e270d4SMatthew G. Knepley 
416546e270d4SMatthew G. Knepley   Input Parameters:
416646e270d4SMatthew G. Knepley + dm  - The DM object
416746e270d4SMatthew G. Knepley - dim - The embedding dimension
416846e270d4SMatthew G. Knepley 
416946e270d4SMatthew G. Knepley   Level: intermediate
417046e270d4SMatthew G. Knepley 
417146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
417246e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
417346e270d4SMatthew G. Knepley @*/
417446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
417546e270d4SMatthew G. Knepley {
417646e270d4SMatthew G. Knepley   PetscFunctionBegin;
417746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
417846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
417946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
418046e270d4SMatthew G. Knepley }
418146e270d4SMatthew G. Knepley 
418246e270d4SMatthew G. Knepley #undef __FUNCT__
4183e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4184e8abe2deSMatthew G. Knepley /*@
4185e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4186e8abe2deSMatthew G. Knepley 
4187e8abe2deSMatthew G. Knepley   Not Collective
4188e8abe2deSMatthew G. Knepley 
4189e8abe2deSMatthew G. Knepley   Input Parameter:
4190e8abe2deSMatthew G. Knepley . dm - The DM object
4191e8abe2deSMatthew G. Knepley 
4192e8abe2deSMatthew G. Knepley   Output Parameter:
4193e8abe2deSMatthew G. Knepley . section - The PetscSection object
4194e8abe2deSMatthew G. Knepley 
4195e8abe2deSMatthew G. Knepley   Level: intermediate
4196e8abe2deSMatthew G. Knepley 
4197e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4198e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4199e8abe2deSMatthew G. Knepley @*/
4200e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4201e8abe2deSMatthew G. Knepley {
4202e8abe2deSMatthew G. Knepley   DM             cdm;
4203e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4204e8abe2deSMatthew G. Knepley 
4205e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4206e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4207e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4208e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4209e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4210e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4211e8abe2deSMatthew G. Knepley }
4212e8abe2deSMatthew G. Knepley 
4213e8abe2deSMatthew G. Knepley #undef __FUNCT__
4214e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4215e8abe2deSMatthew G. Knepley /*@
4216e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4217e8abe2deSMatthew G. Knepley 
4218e8abe2deSMatthew G. Knepley   Not Collective
4219e8abe2deSMatthew G. Knepley 
4220e8abe2deSMatthew G. Knepley   Input Parameters:
4221e8abe2deSMatthew G. Knepley + dm      - The DM object
422246e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4223e8abe2deSMatthew G. Knepley - section - The PetscSection object
4224e8abe2deSMatthew G. Knepley 
4225e8abe2deSMatthew G. Knepley   Level: intermediate
4226e8abe2deSMatthew G. Knepley 
4227e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4228e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4229e8abe2deSMatthew G. Knepley @*/
423046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4231e8abe2deSMatthew G. Knepley {
4232e8abe2deSMatthew G. Knepley   DM             cdm;
4233e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4234e8abe2deSMatthew G. Knepley 
4235e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4236e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
423746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4238e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4239e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
424046e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
424146e270d4SMatthew G. Knepley     PetscInt d = dim;
424246e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
424346e270d4SMatthew G. Knepley 
424446e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
424546e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
424646e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
424746e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
424846e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
424946e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
425046e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
425146e270d4SMatthew G. Knepley     }
425246e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
425346e270d4SMatthew G. Knepley   }
4254e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4255e8abe2deSMatthew G. Knepley }
4256e8abe2deSMatthew G. Knepley 
4257e8abe2deSMatthew G. Knepley #undef __FUNCT__
4258c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
42595dc8c3f7SMatthew G. Knepley /*@C
42605dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42615dc8c3f7SMatthew G. Knepley 
42625dc8c3f7SMatthew G. Knepley   Input Parameters:
42635dc8c3f7SMatthew G. Knepley + dm      - The DM object
42645dc8c3f7SMatthew 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
42655dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42665dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42675dc8c3f7SMatthew G. Knepley 
42685dc8c3f7SMatthew G. Knepley   Level: developer
42695dc8c3f7SMatthew G. Knepley 
42705dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42715dc8c3f7SMatthew G. Knepley @*/
42725dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4273c6b900c6SMatthew G. Knepley {
4274c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4275c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4276c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4277c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
42785dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4279c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4280c6b900c6SMatthew G. Knepley }
4281c6b900c6SMatthew G. Knepley 
4282c6b900c6SMatthew G. Knepley #undef __FUNCT__
4283c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
42845dc8c3f7SMatthew G. Knepley /*@C
42855dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42865dc8c3f7SMatthew G. Knepley 
42875dc8c3f7SMatthew G. Knepley   Input Parameters:
42885dc8c3f7SMatthew G. Knepley + dm      - The DM object
42895dc8c3f7SMatthew 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
42905dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42915dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42925dc8c3f7SMatthew G. Knepley 
42935dc8c3f7SMatthew G. Knepley   Level: developer
42945dc8c3f7SMatthew G. Knepley 
42955dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42965dc8c3f7SMatthew G. Knepley @*/
42975dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4298c6b900c6SMatthew G. Knepley {
4299c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4300c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4301c6b900c6SMatthew G. Knepley 
4302c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4303c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43045dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
43055dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
43065dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
43075dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
43085dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4309c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4310c6b900c6SMatthew G. Knepley }
4311c6b900c6SMatthew G. Knepley 
4312c6b900c6SMatthew G. Knepley #undef __FUNCT__
4313e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4314e87bb0d3SMatthew G Knepley /*@
4315e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4316e87bb0d3SMatthew G Knepley 
4317e87bb0d3SMatthew G Knepley   Not collective
4318e87bb0d3SMatthew G Knepley 
4319e87bb0d3SMatthew G Knepley   Input Parameters:
4320e87bb0d3SMatthew G Knepley + dm - The DM
4321e87bb0d3SMatthew G Knepley - v - The Vec of points
4322e87bb0d3SMatthew G Knepley 
432361e3bb9bSMatthew G Knepley   Output Parameter:
4324e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4325e87bb0d3SMatthew G Knepley 
4326e87bb0d3SMatthew G Knepley   Level: developer
432761e3bb9bSMatthew G Knepley 
432861e3bb9bSMatthew G Knepley .keywords: point location, mesh
432961e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
433061e3bb9bSMatthew G Knepley @*/
4331e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4332e87bb0d3SMatthew G Knepley {
4333735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4334735aa83eSMatthew G Knepley 
4335e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4336e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4337e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4338e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
433947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4340735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4341735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
434282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
434347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4344e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4345e87bb0d3SMatthew G Knepley }
434614f150ffSMatthew G. Knepley 
434714f150ffSMatthew G. Knepley #undef __FUNCT__
434814f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4349f4d763aaSMatthew G. Knepley /*@
4350f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4351f4d763aaSMatthew G. Knepley 
4352f4d763aaSMatthew G. Knepley   Input Parameter:
4353f4d763aaSMatthew G. Knepley . dm - The original DM
4354f4d763aaSMatthew G. Knepley 
4355f4d763aaSMatthew G. Knepley   Output Parameter:
4356f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4357f4d763aaSMatthew G. Knepley 
4358f4d763aaSMatthew G. Knepley   Level: intermediate
4359f4d763aaSMatthew G. Knepley 
4360f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4361f4d763aaSMatthew G. Knepley @*/
436214f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
436314f150ffSMatthew G. Knepley {
4364c26acbdeSMatthew G. Knepley   PetscSection   section;
4365c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
436614f150ffSMatthew G. Knepley   PetscErrorCode ierr;
436714f150ffSMatthew G. Knepley 
436814f150ffSMatthew G. Knepley   PetscFunctionBegin;
436914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
437014f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4371c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4372c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4373c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4374c26acbdeSMatthew G. Knepley     *odm = dm;
4375c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4376c26acbdeSMatthew G. Knepley   }
437714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4378c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
437914f150ffSMatthew G. Knepley     PetscSF      sf;
438014f150ffSMatthew G. Knepley 
438114f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
438214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
438314f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
438414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
438514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
438615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
438714f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
438814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
438914f150ffSMatthew G. Knepley   }
439014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
439114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
439214f150ffSMatthew G. Knepley }
4393f4d763aaSMatthew G. Knepley 
4394f4d763aaSMatthew G. Knepley #undef __FUNCT__
4395f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4396f4d763aaSMatthew G. Knepley /*@
4397cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4398f4d763aaSMatthew G. Knepley 
4399f4d763aaSMatthew G. Knepley   Input Parameter:
4400f4d763aaSMatthew G. Knepley . dm - The original DM
4401f4d763aaSMatthew G. Knepley 
4402cdb7a50dSMatthew G. Knepley   Output Parameters:
4403cdb7a50dSMatthew G. Knepley + num - The output sequence number
4404cdb7a50dSMatthew G. Knepley - val - The output sequence value
4405f4d763aaSMatthew G. Knepley 
4406f4d763aaSMatthew G. Knepley   Level: intermediate
4407f4d763aaSMatthew G. Knepley 
4408f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4409f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4410f4d763aaSMatthew G. Knepley 
4411f4d763aaSMatthew G. Knepley .seealso: VecView()
4412f4d763aaSMatthew G. Knepley @*/
4413cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4414f4d763aaSMatthew G. Knepley {
4415f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4416f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4417cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4418cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4419f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4420f4d763aaSMatthew G. Knepley }
4421f4d763aaSMatthew G. Knepley 
4422f4d763aaSMatthew G. Knepley #undef __FUNCT__
4423f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4424f4d763aaSMatthew G. Knepley /*@
4425cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4426f4d763aaSMatthew G. Knepley 
4427f4d763aaSMatthew G. Knepley   Input Parameters:
4428f4d763aaSMatthew G. Knepley + dm - The original DM
4429cdb7a50dSMatthew G. Knepley . num - The output sequence number
4430cdb7a50dSMatthew G. Knepley - val - The output sequence value
4431f4d763aaSMatthew G. Knepley 
4432f4d763aaSMatthew G. Knepley   Level: intermediate
4433f4d763aaSMatthew G. Knepley 
4434f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4435f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4436f4d763aaSMatthew G. Knepley 
4437f4d763aaSMatthew G. Knepley .seealso: VecView()
4438f4d763aaSMatthew G. Knepley @*/
4439cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4440f4d763aaSMatthew G. Knepley {
4441f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4442f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4443f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4444cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4445cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4446cdb7a50dSMatthew G. Knepley }
4447cdb7a50dSMatthew G. Knepley 
4448cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4449cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4450cdb7a50dSMatthew G. Knepley /*@C
4451cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4452cdb7a50dSMatthew G. Knepley 
4453cdb7a50dSMatthew G. Knepley   Input Parameters:
4454cdb7a50dSMatthew G. Knepley + dm   - The original DM
4455cdb7a50dSMatthew G. Knepley . name - The sequence name
4456cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4457cdb7a50dSMatthew G. Knepley 
4458cdb7a50dSMatthew G. Knepley   Output Parameter:
4459cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4460cdb7a50dSMatthew G. Knepley 
4461cdb7a50dSMatthew G. Knepley   Level: intermediate
4462cdb7a50dSMatthew G. Knepley 
4463cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4464cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4465cdb7a50dSMatthew G. Knepley 
4466cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4467cdb7a50dSMatthew G. Knepley @*/
4468cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4469cdb7a50dSMatthew G. Knepley {
4470cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4471cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4472cdb7a50dSMatthew G. Knepley 
4473cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4474cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4475cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4476cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4477cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4478cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4479cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4480cdb7a50dSMatthew G. Knepley     PetscScalar value;
4481cdb7a50dSMatthew G. Knepley 
4482cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
44834aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4484cdb7a50dSMatthew G. Knepley #endif
4485cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4486f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4487f4d763aaSMatthew G. Knepley }
44888e4ac7eaSMatthew G. Knepley 
44898e4ac7eaSMatthew G. Knepley #undef __FUNCT__
44908e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
44918e4ac7eaSMatthew G. Knepley /*@
44928e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
44938e4ac7eaSMatthew G. Knepley 
44948e4ac7eaSMatthew G. Knepley   Not collective
44958e4ac7eaSMatthew G. Knepley 
44968e4ac7eaSMatthew G. Knepley   Input Parameter:
44978e4ac7eaSMatthew G. Knepley . dm - The DM
44988e4ac7eaSMatthew G. Knepley 
44998e4ac7eaSMatthew G. Knepley   Output Parameter:
45008e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
45018e4ac7eaSMatthew G. Knepley 
45028e4ac7eaSMatthew G. Knepley   Level: beginner
45038e4ac7eaSMatthew G. Knepley 
45048e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
45058e4ac7eaSMatthew G. Knepley @*/
45068e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
45078e4ac7eaSMatthew G. Knepley {
45088e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45098e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45108e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
45118e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
45128e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45138e4ac7eaSMatthew G. Knepley }
45148e4ac7eaSMatthew G. Knepley 
45158e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45168e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
45178e4ac7eaSMatthew G. Knepley /*@
45188e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
45198e4ac7eaSMatthew G. Knepley 
45208e4ac7eaSMatthew G. Knepley   Collective on dm
45218e4ac7eaSMatthew G. Knepley 
45228e4ac7eaSMatthew G. Knepley   Input Parameters:
45238e4ac7eaSMatthew G. Knepley + dm - The DM
45248e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
45258e4ac7eaSMatthew G. Knepley 
45268e4ac7eaSMatthew G. Knepley   Level: beginner
45278e4ac7eaSMatthew G. Knepley 
45288e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
45298e4ac7eaSMatthew G. Knepley @*/
45308e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
45318e4ac7eaSMatthew G. Knepley {
45328e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45338e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45348e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
45358e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
45368e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45378e4ac7eaSMatthew G. Knepley }
4538c58f1c22SToby Isaac 
4539c58f1c22SToby Isaac #undef __FUNCT__
4540c58f1c22SToby Isaac #define __FUNCT__
4541c58f1c22SToby Isaac 
4542c58f1c22SToby Isaac #undef __FUNCT__
4543c58f1c22SToby Isaac #define __FUNCT__ "DMCreateLabel"
4544c58f1c22SToby Isaac /*@C
4545c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
4546c58f1c22SToby Isaac 
4547c58f1c22SToby Isaac   Not Collective
4548c58f1c22SToby Isaac 
4549c58f1c22SToby Isaac   Input Parameters:
4550c58f1c22SToby Isaac + dm   - The DM object
4551c58f1c22SToby Isaac - name - The label name
4552c58f1c22SToby Isaac 
4553c58f1c22SToby Isaac   Level: intermediate
4554c58f1c22SToby Isaac 
4555c58f1c22SToby Isaac .keywords: mesh
4556c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4557c58f1c22SToby Isaac @*/
4558c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
4559c58f1c22SToby Isaac {
4560c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
4561c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
4562c58f1c22SToby Isaac   PetscErrorCode ierr;
4563c58f1c22SToby Isaac 
4564c58f1c22SToby Isaac   PetscFunctionBegin;
4565c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4566c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4567c58f1c22SToby Isaac   while (next) {
4568c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
4569c58f1c22SToby Isaac     if (flg) break;
4570c58f1c22SToby Isaac     next = next->next;
4571c58f1c22SToby Isaac   }
4572c58f1c22SToby Isaac   if (!flg) {
4573c58f1c22SToby Isaac     DMLabelLink tmpLabel;
4574c58f1c22SToby Isaac 
4575c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
4576c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
4577c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
4578c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
4579c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
4580c58f1c22SToby Isaac   }
4581c58f1c22SToby Isaac   PetscFunctionReturn(0);
4582c58f1c22SToby Isaac }
4583c58f1c22SToby Isaac 
4584c58f1c22SToby Isaac #undef __FUNCT__
4585c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelValue"
4586c58f1c22SToby Isaac /*@C
4587c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
4588c58f1c22SToby Isaac 
4589c58f1c22SToby Isaac   Not Collective
4590c58f1c22SToby Isaac 
4591c58f1c22SToby Isaac   Input Parameters:
4592c58f1c22SToby Isaac + dm   - The DM object
4593c58f1c22SToby Isaac . name - The label name
4594c58f1c22SToby Isaac - point - The mesh point
4595c58f1c22SToby Isaac 
4596c58f1c22SToby Isaac   Output Parameter:
4597c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
4598c58f1c22SToby Isaac 
4599c58f1c22SToby Isaac   Level: beginner
4600c58f1c22SToby Isaac 
4601c58f1c22SToby Isaac .keywords: mesh
4602c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
4603c58f1c22SToby Isaac @*/
4604c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
4605c58f1c22SToby Isaac {
4606c58f1c22SToby Isaac   DMLabel        label;
4607c58f1c22SToby Isaac   PetscErrorCode ierr;
4608c58f1c22SToby Isaac 
4609c58f1c22SToby Isaac   PetscFunctionBegin;
4610c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4611c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4612c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4613c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
4614c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
4615c58f1c22SToby Isaac   PetscFunctionReturn(0);
4616c58f1c22SToby Isaac }
4617c58f1c22SToby Isaac 
4618c58f1c22SToby Isaac #undef __FUNCT__
4619c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelValue"
4620c58f1c22SToby Isaac /*@C
4621c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
4622c58f1c22SToby Isaac 
4623c58f1c22SToby Isaac   Not Collective
4624c58f1c22SToby Isaac 
4625c58f1c22SToby Isaac   Input Parameters:
4626c58f1c22SToby Isaac + dm   - The DM object
4627c58f1c22SToby Isaac . name - The label name
4628c58f1c22SToby Isaac . point - The mesh point
4629c58f1c22SToby Isaac - value - The label value for this point
4630c58f1c22SToby Isaac 
4631c58f1c22SToby Isaac   Output Parameter:
4632c58f1c22SToby Isaac 
4633c58f1c22SToby Isaac   Level: beginner
4634c58f1c22SToby Isaac 
4635c58f1c22SToby Isaac .keywords: mesh
4636c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
4637c58f1c22SToby Isaac @*/
4638c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4639c58f1c22SToby Isaac {
4640c58f1c22SToby Isaac   DMLabel        label;
4641c58f1c22SToby Isaac   PetscErrorCode ierr;
4642c58f1c22SToby Isaac 
4643c58f1c22SToby Isaac   PetscFunctionBegin;
4644c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4645c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4646c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4647c58f1c22SToby Isaac   if (!label) {
4648c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
4649c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4650c58f1c22SToby Isaac   }
4651c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
4652c58f1c22SToby Isaac   PetscFunctionReturn(0);
4653c58f1c22SToby Isaac }
4654c58f1c22SToby Isaac 
4655c58f1c22SToby Isaac #undef __FUNCT__
4656c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelValue"
4657c58f1c22SToby Isaac /*@C
4658c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
4659c58f1c22SToby Isaac 
4660c58f1c22SToby Isaac   Not Collective
4661c58f1c22SToby Isaac 
4662c58f1c22SToby Isaac   Input Parameters:
4663c58f1c22SToby Isaac + dm   - The DM object
4664c58f1c22SToby Isaac . name - The label name
4665c58f1c22SToby Isaac . point - The mesh point
4666c58f1c22SToby Isaac - value - The label value for this point
4667c58f1c22SToby Isaac 
4668c58f1c22SToby Isaac   Output Parameter:
4669c58f1c22SToby Isaac 
4670c58f1c22SToby Isaac   Level: beginner
4671c58f1c22SToby Isaac 
4672c58f1c22SToby Isaac .keywords: mesh
4673c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
4674c58f1c22SToby Isaac @*/
4675c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4676c58f1c22SToby Isaac {
4677c58f1c22SToby Isaac   DMLabel        label;
4678c58f1c22SToby Isaac   PetscErrorCode ierr;
4679c58f1c22SToby Isaac 
4680c58f1c22SToby Isaac   PetscFunctionBegin;
4681c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4682c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4683c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4684c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4685c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
4686c58f1c22SToby Isaac   PetscFunctionReturn(0);
4687c58f1c22SToby Isaac }
4688c58f1c22SToby Isaac 
4689c58f1c22SToby Isaac #undef __FUNCT__
4690c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelSize"
4691c58f1c22SToby Isaac /*@C
4692c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
4693c58f1c22SToby Isaac 
4694c58f1c22SToby Isaac   Not Collective
4695c58f1c22SToby Isaac 
4696c58f1c22SToby Isaac   Input Parameters:
4697c58f1c22SToby Isaac + dm   - The DM object
4698c58f1c22SToby Isaac - name - The label name
4699c58f1c22SToby Isaac 
4700c58f1c22SToby Isaac   Output Parameter:
4701c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
4702c58f1c22SToby Isaac 
4703c58f1c22SToby Isaac   Level: beginner
4704c58f1c22SToby Isaac 
4705c58f1c22SToby Isaac .keywords: mesh
4706c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
4707c58f1c22SToby Isaac @*/
4708c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
4709c58f1c22SToby Isaac {
4710c58f1c22SToby Isaac   DMLabel        label;
4711c58f1c22SToby Isaac   PetscErrorCode ierr;
4712c58f1c22SToby Isaac 
4713c58f1c22SToby Isaac   PetscFunctionBegin;
4714c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4715c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4716c58f1c22SToby Isaac   PetscValidPointer(size, 3);
4717c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4718c58f1c22SToby Isaac   *size = 0;
4719c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4720c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
4721c58f1c22SToby Isaac   PetscFunctionReturn(0);
4722c58f1c22SToby Isaac }
4723c58f1c22SToby Isaac 
4724c58f1c22SToby Isaac #undef __FUNCT__
4725c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelIdIS"
4726c58f1c22SToby Isaac /*@C
4727c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
4728c58f1c22SToby Isaac 
4729c58f1c22SToby Isaac   Not Collective
4730c58f1c22SToby Isaac 
4731c58f1c22SToby Isaac   Input Parameters:
4732c58f1c22SToby Isaac + mesh - The DM object
4733c58f1c22SToby Isaac - name - The label name
4734c58f1c22SToby Isaac 
4735c58f1c22SToby Isaac   Output Parameter:
4736c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
4737c58f1c22SToby Isaac 
4738c58f1c22SToby Isaac   Level: beginner
4739c58f1c22SToby Isaac 
4740c58f1c22SToby Isaac .keywords: mesh
4741c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
4742c58f1c22SToby Isaac @*/
4743c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
4744c58f1c22SToby Isaac {
4745c58f1c22SToby Isaac   DMLabel        label;
4746c58f1c22SToby Isaac   PetscErrorCode ierr;
4747c58f1c22SToby Isaac 
4748c58f1c22SToby Isaac   PetscFunctionBegin;
4749c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4750c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4751c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
4752c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4753c58f1c22SToby Isaac   *ids = NULL;
4754c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4755c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
4756c58f1c22SToby Isaac   PetscFunctionReturn(0);
4757c58f1c22SToby Isaac }
4758c58f1c22SToby Isaac 
4759c58f1c22SToby Isaac #undef __FUNCT__
4760c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumSize"
4761c58f1c22SToby Isaac /*@C
4762c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
4763c58f1c22SToby Isaac 
4764c58f1c22SToby Isaac   Not Collective
4765c58f1c22SToby Isaac 
4766c58f1c22SToby Isaac   Input Parameters:
4767c58f1c22SToby Isaac + dm - The DM object
4768c58f1c22SToby Isaac . name - The label name
4769c58f1c22SToby Isaac - value - The stratum value
4770c58f1c22SToby Isaac 
4771c58f1c22SToby Isaac   Output Parameter:
4772c58f1c22SToby Isaac . size - The stratum size
4773c58f1c22SToby Isaac 
4774c58f1c22SToby Isaac   Level: beginner
4775c58f1c22SToby Isaac 
4776c58f1c22SToby Isaac .keywords: mesh
4777c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
4778c58f1c22SToby Isaac @*/
4779c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
4780c58f1c22SToby Isaac {
4781c58f1c22SToby Isaac   DMLabel        label;
4782c58f1c22SToby Isaac   PetscErrorCode ierr;
4783c58f1c22SToby Isaac 
4784c58f1c22SToby Isaac   PetscFunctionBegin;
4785c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4786c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4787c58f1c22SToby Isaac   PetscValidPointer(size, 4);
4788c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4789c58f1c22SToby Isaac   *size = 0;
4790c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4791c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
4792c58f1c22SToby Isaac   PetscFunctionReturn(0);
4793c58f1c22SToby Isaac }
4794c58f1c22SToby Isaac 
4795c58f1c22SToby Isaac #undef __FUNCT__
4796c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumIS"
4797c58f1c22SToby Isaac /*@C
4798c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
4799c58f1c22SToby Isaac 
4800c58f1c22SToby Isaac   Not Collective
4801c58f1c22SToby Isaac 
4802c58f1c22SToby Isaac   Input Parameters:
4803c58f1c22SToby Isaac + dm - The DM object
4804c58f1c22SToby Isaac . name - The label name
4805c58f1c22SToby Isaac - value - The stratum value
4806c58f1c22SToby Isaac 
4807c58f1c22SToby Isaac   Output Parameter:
4808c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
4809c58f1c22SToby Isaac 
4810c58f1c22SToby Isaac   Level: beginner
4811c58f1c22SToby Isaac 
4812c58f1c22SToby Isaac .keywords: mesh
4813c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
4814c58f1c22SToby Isaac @*/
4815c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
4816c58f1c22SToby Isaac {
4817c58f1c22SToby Isaac   DMLabel        label;
4818c58f1c22SToby Isaac   PetscErrorCode ierr;
4819c58f1c22SToby Isaac 
4820c58f1c22SToby Isaac   PetscFunctionBegin;
4821c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4822c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4823c58f1c22SToby Isaac   PetscValidPointer(points, 4);
4824c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4825c58f1c22SToby Isaac   *points = NULL;
4826c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4827c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
4828c58f1c22SToby Isaac   PetscFunctionReturn(0);
4829c58f1c22SToby Isaac }
4830c58f1c22SToby Isaac 
4831c58f1c22SToby Isaac #undef __FUNCT__
4832c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelStratum"
4833c58f1c22SToby Isaac /*@C
4834c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
4835c58f1c22SToby Isaac 
4836c58f1c22SToby Isaac   Not Collective
4837c58f1c22SToby Isaac 
4838c58f1c22SToby Isaac   Input Parameters:
4839c58f1c22SToby Isaac + dm   - The DM object
4840c58f1c22SToby Isaac . name - The label name
4841c58f1c22SToby Isaac - value - The label value for this point
4842c58f1c22SToby Isaac 
4843c58f1c22SToby Isaac   Output Parameter:
4844c58f1c22SToby Isaac 
4845c58f1c22SToby Isaac   Level: beginner
4846c58f1c22SToby Isaac 
4847c58f1c22SToby Isaac .keywords: mesh
4848c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
4849c58f1c22SToby Isaac @*/
4850c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
4851c58f1c22SToby Isaac {
4852c58f1c22SToby Isaac   DMLabel        label;
4853c58f1c22SToby Isaac   PetscErrorCode ierr;
4854c58f1c22SToby Isaac 
4855c58f1c22SToby Isaac   PetscFunctionBegin;
4856c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4857c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4858c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4859c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4860c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
4861c58f1c22SToby Isaac   PetscFunctionReturn(0);
4862c58f1c22SToby Isaac }
4863c58f1c22SToby Isaac 
4864c58f1c22SToby Isaac #undef __FUNCT__
4865c58f1c22SToby Isaac #define __FUNCT__ "DMGetNumLabels"
4866c58f1c22SToby Isaac /*@
4867c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
4868c58f1c22SToby Isaac 
4869c58f1c22SToby Isaac   Not Collective
4870c58f1c22SToby Isaac 
4871c58f1c22SToby Isaac   Input Parameter:
4872c58f1c22SToby Isaac . dm   - The DM object
4873c58f1c22SToby Isaac 
4874c58f1c22SToby Isaac   Output Parameter:
4875c58f1c22SToby Isaac . numLabels - the number of Labels
4876c58f1c22SToby Isaac 
4877c58f1c22SToby Isaac   Level: intermediate
4878c58f1c22SToby Isaac 
4879c58f1c22SToby Isaac .keywords: mesh
4880c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4881c58f1c22SToby Isaac @*/
4882c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
4883c58f1c22SToby Isaac {
4884c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
4885c58f1c22SToby Isaac   PetscInt  n    = 0;
4886c58f1c22SToby Isaac 
4887c58f1c22SToby Isaac   PetscFunctionBegin;
4888c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4889c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
4890c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
4891c58f1c22SToby Isaac   *numLabels = n;
4892c58f1c22SToby Isaac   PetscFunctionReturn(0);
4893c58f1c22SToby Isaac }
4894c58f1c22SToby Isaac 
4895c58f1c22SToby Isaac #undef __FUNCT__
4896c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelName"
4897c58f1c22SToby Isaac /*@C
4898c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
4899c58f1c22SToby Isaac 
4900c58f1c22SToby Isaac   Not Collective
4901c58f1c22SToby Isaac 
4902c58f1c22SToby Isaac   Input Parameters:
4903c58f1c22SToby Isaac + dm - The DM object
4904c58f1c22SToby Isaac - n  - the label number
4905c58f1c22SToby Isaac 
4906c58f1c22SToby Isaac   Output Parameter:
4907c58f1c22SToby Isaac . name - the label name
4908c58f1c22SToby Isaac 
4909c58f1c22SToby Isaac   Level: intermediate
4910c58f1c22SToby Isaac 
4911c58f1c22SToby Isaac .keywords: mesh
4912c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4913c58f1c22SToby Isaac @*/
4914c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
4915c58f1c22SToby Isaac {
4916c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
4917c58f1c22SToby Isaac   PetscInt  l    = 0;
4918c58f1c22SToby Isaac 
4919c58f1c22SToby Isaac   PetscFunctionBegin;
4920c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4921c58f1c22SToby Isaac   PetscValidPointer(name, 3);
4922c58f1c22SToby Isaac   while (next) {
4923c58f1c22SToby Isaac     if (l == n) {
4924c58f1c22SToby Isaac       *name = next->label->name;
4925c58f1c22SToby Isaac       PetscFunctionReturn(0);
4926c58f1c22SToby Isaac     }
4927c58f1c22SToby Isaac     ++l;
4928c58f1c22SToby Isaac     next = next->next;
4929c58f1c22SToby Isaac   }
4930c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
4931c58f1c22SToby Isaac }
4932c58f1c22SToby Isaac 
4933c58f1c22SToby Isaac #undef __FUNCT__
4934c58f1c22SToby Isaac #define __FUNCT__ "DMHasLabel"
4935c58f1c22SToby Isaac /*@C
4936c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
4937c58f1c22SToby Isaac 
4938c58f1c22SToby Isaac   Not Collective
4939c58f1c22SToby Isaac 
4940c58f1c22SToby Isaac   Input Parameters:
4941c58f1c22SToby Isaac + dm   - The DM object
4942c58f1c22SToby Isaac - name - The label name
4943c58f1c22SToby Isaac 
4944c58f1c22SToby Isaac   Output Parameter:
4945c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
4946c58f1c22SToby Isaac 
4947c58f1c22SToby Isaac   Level: intermediate
4948c58f1c22SToby Isaac 
4949c58f1c22SToby Isaac .keywords: mesh
4950c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4951c58f1c22SToby Isaac @*/
4952c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
4953c58f1c22SToby Isaac {
4954c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
4955c58f1c22SToby Isaac   PetscErrorCode ierr;
4956c58f1c22SToby Isaac 
4957c58f1c22SToby Isaac   PetscFunctionBegin;
4958c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4959c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4960c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
4961c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
4962c58f1c22SToby Isaac   while (next) {
4963c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
4964c58f1c22SToby Isaac     if (*hasLabel) break;
4965c58f1c22SToby Isaac     next = next->next;
4966c58f1c22SToby Isaac   }
4967c58f1c22SToby Isaac   PetscFunctionReturn(0);
4968c58f1c22SToby Isaac }
4969c58f1c22SToby Isaac 
4970c58f1c22SToby Isaac #undef __FUNCT__
4971c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabel"
4972c58f1c22SToby Isaac /*@C
4973c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
4974c58f1c22SToby Isaac 
4975c58f1c22SToby Isaac   Not Collective
4976c58f1c22SToby Isaac 
4977c58f1c22SToby Isaac   Input Parameters:
4978c58f1c22SToby Isaac + dm   - The DM object
4979c58f1c22SToby Isaac - name - The label name
4980c58f1c22SToby Isaac 
4981c58f1c22SToby Isaac   Output Parameter:
4982c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
4983c58f1c22SToby Isaac 
4984c58f1c22SToby Isaac   Level: intermediate
4985c58f1c22SToby Isaac 
4986c58f1c22SToby Isaac .keywords: mesh
4987c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4988c58f1c22SToby Isaac @*/
4989c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
4990c58f1c22SToby Isaac {
4991c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
4992c58f1c22SToby Isaac   PetscBool      hasLabel;
4993c58f1c22SToby Isaac   PetscErrorCode ierr;
4994c58f1c22SToby Isaac 
4995c58f1c22SToby Isaac   PetscFunctionBegin;
4996c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4997c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4998c58f1c22SToby Isaac   PetscValidPointer(label, 3);
4999c58f1c22SToby Isaac   *label = NULL;
5000c58f1c22SToby Isaac   while (next) {
5001c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5002c58f1c22SToby Isaac     if (hasLabel) {
5003c58f1c22SToby Isaac       *label = next->label;
5004c58f1c22SToby Isaac       break;
5005c58f1c22SToby Isaac     }
5006c58f1c22SToby Isaac     next = next->next;
5007c58f1c22SToby Isaac   }
5008c58f1c22SToby Isaac   PetscFunctionReturn(0);
5009c58f1c22SToby Isaac }
5010c58f1c22SToby Isaac 
5011c58f1c22SToby Isaac #undef __FUNCT__
5012c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelByNum"
5013c58f1c22SToby Isaac /*@C
5014c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5015c58f1c22SToby Isaac 
5016c58f1c22SToby Isaac   Not Collective
5017c58f1c22SToby Isaac 
5018c58f1c22SToby Isaac   Input Parameters:
5019c58f1c22SToby Isaac + dm - The DM object
5020c58f1c22SToby Isaac - n  - the label number
5021c58f1c22SToby Isaac 
5022c58f1c22SToby Isaac   Output Parameter:
5023c58f1c22SToby Isaac . label - the label
5024c58f1c22SToby Isaac 
5025c58f1c22SToby Isaac   Level: intermediate
5026c58f1c22SToby Isaac 
5027c58f1c22SToby Isaac .keywords: mesh
5028c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5029c58f1c22SToby Isaac @*/
5030c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5031c58f1c22SToby Isaac {
5032c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5033c58f1c22SToby Isaac   PetscInt    l    = 0;
5034c58f1c22SToby Isaac 
5035c58f1c22SToby Isaac   PetscFunctionBegin;
5036c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5037c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5038c58f1c22SToby Isaac   while (next) {
5039c58f1c22SToby Isaac     if (l == n) {
5040c58f1c22SToby Isaac       *label = next->label;
5041c58f1c22SToby Isaac       PetscFunctionReturn(0);
5042c58f1c22SToby Isaac     }
5043c58f1c22SToby Isaac     ++l;
5044c58f1c22SToby Isaac     next = next->next;
5045c58f1c22SToby Isaac   }
5046c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5047c58f1c22SToby Isaac }
5048c58f1c22SToby Isaac 
5049c58f1c22SToby Isaac #undef __FUNCT__
5050c58f1c22SToby Isaac #define __FUNCT__ "DMAddLabel"
5051c58f1c22SToby Isaac /*@C
5052c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5053c58f1c22SToby Isaac 
5054c58f1c22SToby Isaac   Not Collective
5055c58f1c22SToby Isaac 
5056c58f1c22SToby Isaac   Input Parameters:
5057c58f1c22SToby Isaac + dm   - The DM object
5058c58f1c22SToby Isaac - label - The DMLabel
5059c58f1c22SToby Isaac 
5060c58f1c22SToby Isaac   Level: developer
5061c58f1c22SToby Isaac 
5062c58f1c22SToby Isaac .keywords: mesh
5063c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5064c58f1c22SToby Isaac @*/
5065c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5066c58f1c22SToby Isaac {
5067c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5068c58f1c22SToby Isaac   PetscBool      hasLabel;
5069c58f1c22SToby Isaac   PetscErrorCode ierr;
5070c58f1c22SToby Isaac 
5071c58f1c22SToby Isaac   PetscFunctionBegin;
5072c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5073c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5074c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5075c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5076c58f1c22SToby Isaac   tmpLabel->label  = label;
5077c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5078c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5079c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5080c58f1c22SToby Isaac   PetscFunctionReturn(0);
5081c58f1c22SToby Isaac }
5082c58f1c22SToby Isaac 
5083c58f1c22SToby Isaac #undef __FUNCT__
5084c58f1c22SToby Isaac #define __FUNCT__ "DMRemoveLabel"
5085c58f1c22SToby Isaac /*@C
5086c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5087c58f1c22SToby Isaac 
5088c58f1c22SToby Isaac   Not Collective
5089c58f1c22SToby Isaac 
5090c58f1c22SToby Isaac   Input Parameters:
5091c58f1c22SToby Isaac + dm   - The DM object
5092c58f1c22SToby Isaac - name - The label name
5093c58f1c22SToby Isaac 
5094c58f1c22SToby Isaac   Output Parameter:
5095c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5096c58f1c22SToby Isaac 
5097c58f1c22SToby Isaac   Level: developer
5098c58f1c22SToby Isaac 
5099c58f1c22SToby Isaac .keywords: mesh
5100c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5101c58f1c22SToby Isaac @*/
5102c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5103c58f1c22SToby Isaac {
5104c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5105c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5106c58f1c22SToby Isaac   PetscBool      hasLabel;
5107c58f1c22SToby Isaac   PetscErrorCode ierr;
5108c58f1c22SToby Isaac 
5109c58f1c22SToby Isaac   PetscFunctionBegin;
5110c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5111c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5112c58f1c22SToby Isaac   *label = NULL;
5113c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5114c58f1c22SToby Isaac   while (next) {
5115c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5116c58f1c22SToby Isaac     if (hasLabel) {
5117c58f1c22SToby Isaac       if (last) last->next       = next->next;
5118c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5119c58f1c22SToby Isaac       next->next = NULL;
5120c58f1c22SToby Isaac       *label     = next->label;
5121c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5122c58f1c22SToby Isaac       if (hasLabel) {
5123c58f1c22SToby Isaac         dm->depthLabel = NULL;
5124c58f1c22SToby Isaac       }
5125c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5126c58f1c22SToby Isaac       break;
5127c58f1c22SToby Isaac     }
5128c58f1c22SToby Isaac     last = next;
5129c58f1c22SToby Isaac     next = next->next;
5130c58f1c22SToby Isaac   }
5131c58f1c22SToby Isaac   PetscFunctionReturn(0);
5132c58f1c22SToby Isaac }
5133c58f1c22SToby Isaac 
5134c58f1c22SToby Isaac #undef __FUNCT__
5135c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelOutput"
5136c58f1c22SToby Isaac /*@C
5137c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5138c58f1c22SToby Isaac 
5139c58f1c22SToby Isaac   Not Collective
5140c58f1c22SToby Isaac 
5141c58f1c22SToby Isaac   Input Parameters:
5142c58f1c22SToby Isaac + dm   - The DM object
5143c58f1c22SToby Isaac - name - The label name
5144c58f1c22SToby Isaac 
5145c58f1c22SToby Isaac   Output Parameter:
5146c58f1c22SToby Isaac . output - The flag for output
5147c58f1c22SToby Isaac 
5148c58f1c22SToby Isaac   Level: developer
5149c58f1c22SToby Isaac 
5150c58f1c22SToby Isaac .keywords: mesh
5151c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5152c58f1c22SToby Isaac @*/
5153c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5154c58f1c22SToby Isaac {
5155c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5156c58f1c22SToby Isaac   PetscErrorCode ierr;
5157c58f1c22SToby Isaac 
5158c58f1c22SToby Isaac   PetscFunctionBegin;
5159c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5160c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5161c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5162c58f1c22SToby Isaac   while (next) {
5163c58f1c22SToby Isaac     PetscBool flg;
5164c58f1c22SToby Isaac 
5165c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5166c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5167c58f1c22SToby Isaac     next = next->next;
5168c58f1c22SToby Isaac   }
5169c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5170c58f1c22SToby Isaac }
5171c58f1c22SToby Isaac 
5172c58f1c22SToby Isaac #undef __FUNCT__
5173c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelOutput"
5174c58f1c22SToby Isaac /*@C
5175c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5176c58f1c22SToby Isaac 
5177c58f1c22SToby Isaac   Not Collective
5178c58f1c22SToby Isaac 
5179c58f1c22SToby Isaac   Input Parameters:
5180c58f1c22SToby Isaac + dm     - The DM object
5181c58f1c22SToby Isaac . name   - The label name
5182c58f1c22SToby Isaac - output - The flag for output
5183c58f1c22SToby Isaac 
5184c58f1c22SToby Isaac   Level: developer
5185c58f1c22SToby Isaac 
5186c58f1c22SToby Isaac .keywords: mesh
5187c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5188c58f1c22SToby Isaac @*/
5189c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5190c58f1c22SToby Isaac {
5191c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5192c58f1c22SToby Isaac   PetscErrorCode ierr;
5193c58f1c22SToby Isaac 
5194c58f1c22SToby Isaac   PetscFunctionBegin;
5195c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5196c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5197c58f1c22SToby Isaac   while (next) {
5198c58f1c22SToby Isaac     PetscBool flg;
5199c58f1c22SToby Isaac 
5200c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5201c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5202c58f1c22SToby Isaac     next = next->next;
5203c58f1c22SToby Isaac   }
5204c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5205c58f1c22SToby Isaac }
5206c58f1c22SToby Isaac 
5207c58f1c22SToby Isaac 
5208c58f1c22SToby Isaac #undef __FUNCT__
5209c58f1c22SToby Isaac #define __FUNCT__ "DMCopyLabels"
5210c58f1c22SToby Isaac /*@
5211c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5212c58f1c22SToby Isaac 
5213c58f1c22SToby Isaac   Collective on DM
5214c58f1c22SToby Isaac 
5215c58f1c22SToby Isaac   Input Parameter:
5216c58f1c22SToby Isaac . dmA - The DMPlex object with initial labels
5217c58f1c22SToby Isaac 
5218c58f1c22SToby Isaac   Output Parameter:
5219c58f1c22SToby Isaac . dmB - The DMPlex object with copied labels
5220c58f1c22SToby Isaac 
5221c58f1c22SToby Isaac   Level: intermediate
5222c58f1c22SToby Isaac 
5223c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5224c58f1c22SToby Isaac 
5225c58f1c22SToby Isaac .keywords: mesh
5226c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5227c58f1c22SToby Isaac @*/
5228c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5229c58f1c22SToby Isaac {
5230c58f1c22SToby Isaac   PetscInt       numLabels, l;
5231c58f1c22SToby Isaac   PetscErrorCode ierr;
5232c58f1c22SToby Isaac 
5233c58f1c22SToby Isaac   PetscFunctionBegin;
5234c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5235c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5236c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5237c58f1c22SToby Isaac     DMLabel     label, labelNew;
5238c58f1c22SToby Isaac     const char *name;
5239c58f1c22SToby Isaac     PetscBool   flg;
5240c58f1c22SToby Isaac 
5241c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5242c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5243c58f1c22SToby Isaac     if (flg) continue;
5244c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5245c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5246c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5247c58f1c22SToby Isaac   }
5248c58f1c22SToby Isaac   PetscFunctionReturn(0);
5249c58f1c22SToby Isaac }
5250*a8fb8f29SToby Isaac 
5251*a8fb8f29SToby Isaac #undef __FUNCT__
5252*a8fb8f29SToby Isaac #define __FUNCT__ "DMGetCoarseDM"
5253*a8fb8f29SToby Isaac /*@
5254*a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5255*a8fb8f29SToby Isaac 
5256*a8fb8f29SToby Isaac   Input Parameter:
5257*a8fb8f29SToby Isaac . dm - The DM object
5258*a8fb8f29SToby Isaac 
5259*a8fb8f29SToby Isaac   Output Parameter:
5260*a8fb8f29SToby Isaac . cdm - The coarse DM
5261*a8fb8f29SToby Isaac 
5262*a8fb8f29SToby Isaac   Level: intermediate
5263*a8fb8f29SToby Isaac 
5264*a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5265*a8fb8f29SToby Isaac @*/
5266*a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5267*a8fb8f29SToby Isaac {
5268*a8fb8f29SToby Isaac   PetscFunctionBegin;
5269*a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5270*a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5271*a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5272*a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5273*a8fb8f29SToby Isaac }
5274*a8fb8f29SToby Isaac 
5275*a8fb8f29SToby Isaac #undef __FUNCT__
5276*a8fb8f29SToby Isaac #define __FUNCT__ "DMSetCoarseDM"
5277*a8fb8f29SToby Isaac /*@
5278*a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5279*a8fb8f29SToby Isaac 
5280*a8fb8f29SToby Isaac   Input Parameters:
5281*a8fb8f29SToby Isaac + dm - The DM object
5282*a8fb8f29SToby Isaac - cdm - The coarse DM
5283*a8fb8f29SToby Isaac 
5284*a8fb8f29SToby Isaac   Level: intermediate
5285*a8fb8f29SToby Isaac 
5286*a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5287*a8fb8f29SToby Isaac @*/
5288*a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5289*a8fb8f29SToby Isaac {
5290*a8fb8f29SToby Isaac   PetscErrorCode ierr;
5291*a8fb8f29SToby Isaac 
5292*a8fb8f29SToby Isaac   PetscFunctionBegin;
5293*a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5294*a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5295*a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5296*a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5297*a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5298*a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5299*a8fb8f29SToby Isaac }
5300*a8fb8f29SToby Isaac 
5301