xref: /petsc/src/dm/interface/dm.c (revision bcc5ffd92252408533a007b566200b59df89ee0a)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
30c312b8eSJed Brown #include <petscsf.h>
42764a2aaSMatthew G. Knepley #include <petscds.h>
547c6ae99SBarry Smith 
6732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
7cea54e9dSPatrick Farrell PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_CreateInterpolation;
867a56275SMatthew G Knepley 
9bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
10bff4a2f0SMatthew G. Knepley 
1147c6ae99SBarry Smith #undef __FUNCT__
12a4121054SBarry Smith #define __FUNCT__ "DMCreate"
13a4121054SBarry Smith /*@
14de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
15a4121054SBarry Smith 
16a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
17a4121054SBarry Smith    error when you try to use the vector.
18a4121054SBarry Smith 
19a4121054SBarry Smith   Collective on MPI_Comm
20a4121054SBarry Smith 
21a4121054SBarry Smith   Input Parameter:
22a4121054SBarry Smith . comm - The communicator for the DM object
23a4121054SBarry Smith 
24a4121054SBarry Smith   Output Parameter:
25a4121054SBarry Smith . dm - The DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Level: beginner
28a4121054SBarry Smith 
298472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
30a4121054SBarry Smith @*/
317087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
32a4121054SBarry Smith {
33a4121054SBarry Smith   DM             v;
34a4121054SBarry Smith   PetscErrorCode ierr;
35a4121054SBarry Smith 
36a4121054SBarry Smith   PetscFunctionBegin;
371411c6eeSJed Brown   PetscValidPointer(dm,2);
380298fd71SBarry Smith   *dm = NULL;
398b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
42607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
43a4121054SBarry Smith 
4473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
45e7c4fc90SDmitry Karpeev 
460298fd71SBarry Smith   v->ltogmap                  = NULL;
471411c6eeSJed Brown   v->bs                       = 1;
48171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
4988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
51c58f1c22SToby Isaac   v->labels                   = NULL;
52c58f1c22SToby Isaac   v->depthLabel               = NULL;
530298fd71SBarry Smith   v->defaultSection           = NULL;
540298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
55fba222abSToby Isaac   v->defaultConstraintSection = NULL;
56fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
57c6b900c6SMatthew G. Knepley   v->L                        = NULL;
58c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
595dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
609a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
61435a35e8SMatthew G Knepley   {
62435a35e8SMatthew G Knepley     PetscInt i;
63435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
640298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
65435a35e8SMatthew G Knepley     }
66435a35e8SMatthew G Knepley   }
672764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6814f150ffSMatthew G. Knepley   v->dmBC = NULL;
69a8fb8f29SToby Isaac   v->coarseMesh = NULL;
70f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
71cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
72c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
73b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
74*bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
75c58f1c22SToby Isaac   v->labels->refct = 1;
76*bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->boundary));CHKERRQ(ierr);
77*bcc5ffd9SToby Isaac   v->boundary->refct = 1;
781411c6eeSJed Brown   *dm = v;
79a4121054SBarry Smith   PetscFunctionReturn(0);
80a4121054SBarry Smith }
81a4121054SBarry Smith 
82a4121054SBarry Smith #undef __FUNCT__
8338221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
8438221697SMatthew G. Knepley /*@
8538221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
8638221697SMatthew G. Knepley 
8738221697SMatthew G. Knepley   Collective on MPI_Comm
8838221697SMatthew G. Knepley 
8938221697SMatthew G. Knepley   Input Parameter:
9038221697SMatthew G. Knepley . dm - The original DM object
9138221697SMatthew G. Knepley 
9238221697SMatthew G. Knepley   Output Parameter:
9338221697SMatthew G. Knepley . newdm  - The new DM object
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley   Level: beginner
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley .keywords: DM, topology, create
9838221697SMatthew G. Knepley @*/
9938221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
10038221697SMatthew G. Knepley {
10138221697SMatthew G. Knepley   PetscSF        sf;
10238221697SMatthew G. Knepley   Vec            coords;
10338221697SMatthew G. Knepley   void          *ctx;
1041de53e9aSMatthew G. Knepley   PetscInt       dim;
10538221697SMatthew G. Knepley   PetscErrorCode ierr;
10638221697SMatthew G. Knepley 
10738221697SMatthew G. Knepley   PetscFunctionBegin;
10838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10938221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
11038221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
111c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
112c58f1c22SToby Isaac   dm->labels->refct++;
113c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
114c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1151de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1161de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
11738221697SMatthew G. Knepley   if (dm->ops->clone) {
11838221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
11938221697SMatthew G. Knepley   }
120cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
12138221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
12238221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
12338221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
12438221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
12538221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
12638221697SMatthew G. Knepley   if (coords) {
12738221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
12838221697SMatthew G. Knepley   } else {
12938221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
13038221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
13138221697SMatthew G. Knepley   }
132c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
133c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1345dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1355dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1365dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
137c6b900c6SMatthew G. Knepley   }
13838221697SMatthew G. Knepley   PetscFunctionReturn(0);
13938221697SMatthew G. Knepley }
14038221697SMatthew G. Knepley 
14138221697SMatthew G. Knepley #undef __FUNCT__
1429a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1439a42bb27SBarry Smith /*@C
144564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1459a42bb27SBarry Smith 
1468472ad0fSDave May    Logically Collective on DM
1479a42bb27SBarry Smith 
1489a42bb27SBarry Smith    Input Parameter:
1499a42bb27SBarry Smith +  da - initial distributed array
1508154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1519a42bb27SBarry Smith 
1529a42bb27SBarry Smith    Options Database:
153dd85299cSBarry Smith .   -dm_vec_type ctype
1549a42bb27SBarry Smith 
1559a42bb27SBarry Smith    Level: intermediate
1569a42bb27SBarry Smith 
1578472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1589a42bb27SBarry Smith @*/
15919fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1609a42bb27SBarry Smith {
1619a42bb27SBarry Smith   PetscErrorCode ierr;
1629a42bb27SBarry Smith 
1639a42bb27SBarry Smith   PetscFunctionBegin;
1649a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1659a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
16619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1679a42bb27SBarry Smith   PetscFunctionReturn(0);
1689a42bb27SBarry Smith }
1699a42bb27SBarry Smith 
1709a42bb27SBarry Smith #undef __FUNCT__
171c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
172c0dedaeaSBarry Smith /*@C
173c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
174c0dedaeaSBarry Smith 
1758472ad0fSDave May    Logically Collective on DM
176c0dedaeaSBarry Smith 
177c0dedaeaSBarry Smith    Input Parameter:
178c0dedaeaSBarry Smith .  da - initial distributed array
179c0dedaeaSBarry Smith 
180c0dedaeaSBarry Smith    Output Parameter:
181c0dedaeaSBarry Smith .  ctype - the vector type
182c0dedaeaSBarry Smith 
183c0dedaeaSBarry Smith    Level: intermediate
184c0dedaeaSBarry Smith 
1858472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
186c0dedaeaSBarry Smith @*/
187c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
188c0dedaeaSBarry Smith {
189c0dedaeaSBarry Smith   PetscFunctionBegin;
190c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
191c0dedaeaSBarry Smith   *ctype = da->vectype;
192c0dedaeaSBarry Smith   PetscFunctionReturn(0);
193c0dedaeaSBarry Smith }
194c0dedaeaSBarry Smith 
195c0dedaeaSBarry Smith #undef __FUNCT__
1965f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1975f1ad066SMatthew G Knepley /*@
19834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1995f1ad066SMatthew G Knepley 
2005f1ad066SMatthew G Knepley   Not collective
2015f1ad066SMatthew G Knepley 
2025f1ad066SMatthew G Knepley   Input Parameter:
2035f1ad066SMatthew G Knepley . v - The Vec
2045f1ad066SMatthew G Knepley 
2055f1ad066SMatthew G Knepley   Output Parameter:
2065f1ad066SMatthew G Knepley . dm - The DM
2075f1ad066SMatthew G Knepley 
2085f1ad066SMatthew G Knepley   Level: intermediate
2095f1ad066SMatthew G Knepley 
2105f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2115f1ad066SMatthew G Knepley @*/
2125f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2135f1ad066SMatthew G Knepley {
2145f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley   PetscFunctionBegin;
2175f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2185f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2195f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2205f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2215f1ad066SMatthew G Knepley }
2225f1ad066SMatthew G Knepley 
2235f1ad066SMatthew G Knepley #undef __FUNCT__
2245f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2255f1ad066SMatthew G Knepley /*@
226d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Not collective
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley   Input Parameters:
2315f1ad066SMatthew G Knepley + v - The Vec
2325f1ad066SMatthew G Knepley - dm - The DM
2335f1ad066SMatthew G Knepley 
234d9805387SMatthew 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.
235d9805387SMatthew G. Knepley 
2365f1ad066SMatthew G Knepley   Level: intermediate
2375f1ad066SMatthew G Knepley 
2385f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2395f1ad066SMatthew G Knepley @*/
2405f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2415f1ad066SMatthew G Knepley {
2425f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley   PetscFunctionBegin;
2455f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
246d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2475f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2485f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2495f1ad066SMatthew G Knepley }
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley #undef __FUNCT__
252521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
253521d9a4cSLisandro Dalcin /*@C
254521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
255521d9a4cSLisandro Dalcin 
256521d9a4cSLisandro Dalcin    Logically Collective on DM
257521d9a4cSLisandro Dalcin 
258521d9a4cSLisandro Dalcin    Input Parameter:
259521d9a4cSLisandro Dalcin +  dm - the DM context
260521d9a4cSLisandro Dalcin .  ctype - the matrix type
261521d9a4cSLisandro Dalcin 
262521d9a4cSLisandro Dalcin    Options Database:
263521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
264521d9a4cSLisandro Dalcin 
265521d9a4cSLisandro Dalcin    Level: intermediate
266521d9a4cSLisandro Dalcin 
267c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
268521d9a4cSLisandro Dalcin @*/
26919fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
270521d9a4cSLisandro Dalcin {
271521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27288f0584fSBarry Smith 
273521d9a4cSLisandro Dalcin   PetscFunctionBegin;
274521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
275521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
27619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
277521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
278521d9a4cSLisandro Dalcin }
279521d9a4cSLisandro Dalcin 
280521d9a4cSLisandro Dalcin #undef __FUNCT__
281c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
282c0dedaeaSBarry Smith /*@C
283c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
284c0dedaeaSBarry Smith 
285c0dedaeaSBarry Smith    Logically Collective on DM
286c0dedaeaSBarry Smith 
287c0dedaeaSBarry Smith    Input Parameter:
288c0dedaeaSBarry Smith .  dm - the DM context
289c0dedaeaSBarry Smith 
290c0dedaeaSBarry Smith    Output Parameter:
291c0dedaeaSBarry Smith .  ctype - the matrix type
292c0dedaeaSBarry Smith 
293c0dedaeaSBarry Smith    Options Database:
294c0dedaeaSBarry Smith .   -dm_mat_type ctype
295c0dedaeaSBarry Smith 
296c0dedaeaSBarry Smith    Level: intermediate
297c0dedaeaSBarry Smith 
298c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
299c0dedaeaSBarry Smith @*/
300c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
301c0dedaeaSBarry Smith {
302c0dedaeaSBarry Smith   PetscFunctionBegin;
303c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
304c0dedaeaSBarry Smith   *ctype = dm->mattype;
305c0dedaeaSBarry Smith   PetscFunctionReturn(0);
306c0dedaeaSBarry Smith }
307c0dedaeaSBarry Smith 
308c0dedaeaSBarry Smith #undef __FUNCT__
309c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
310c688c046SMatthew G Knepley /*@
31134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
312c688c046SMatthew G Knepley 
313c688c046SMatthew G Knepley   Not collective
314c688c046SMatthew G Knepley 
315c688c046SMatthew G Knepley   Input Parameter:
316c688c046SMatthew G Knepley . A - The Mat
317c688c046SMatthew G Knepley 
318c688c046SMatthew G Knepley   Output Parameter:
319c688c046SMatthew G Knepley . dm - The DM
320c688c046SMatthew G Knepley 
321c688c046SMatthew G Knepley   Level: intermediate
322c688c046SMatthew G Knepley 
323c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
324c688c046SMatthew G Knepley @*/
325c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
326c688c046SMatthew G Knepley {
327c688c046SMatthew G Knepley   PetscErrorCode ierr;
328c688c046SMatthew G Knepley 
329c688c046SMatthew G Knepley   PetscFunctionBegin;
330c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
331c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
332c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
333c688c046SMatthew G Knepley   PetscFunctionReturn(0);
334c688c046SMatthew G Knepley }
335c688c046SMatthew G Knepley 
336c688c046SMatthew G Knepley #undef __FUNCT__
337c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
338c688c046SMatthew G Knepley /*@
339c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
340c688c046SMatthew G Knepley 
341c688c046SMatthew G Knepley   Not collective
342c688c046SMatthew G Knepley 
343c688c046SMatthew G Knepley   Input Parameters:
344c688c046SMatthew G Knepley + A - The Mat
345c688c046SMatthew G Knepley - dm - The DM
346c688c046SMatthew G Knepley 
347c688c046SMatthew G Knepley   Level: intermediate
348c688c046SMatthew G Knepley 
349c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
350c688c046SMatthew G Knepley @*/
351c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
352c688c046SMatthew G Knepley {
353c688c046SMatthew G Knepley   PetscErrorCode ierr;
354c688c046SMatthew G Knepley 
355c688c046SMatthew G Knepley   PetscFunctionBegin;
356c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3578865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
358c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
359c688c046SMatthew G Knepley   PetscFunctionReturn(0);
360c688c046SMatthew G Knepley }
361c688c046SMatthew G Knepley 
362c688c046SMatthew G Knepley #undef __FUNCT__
3639a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3649a42bb27SBarry Smith /*@C
3659a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3666757b960SDave May    DM options in the database.
3679a42bb27SBarry Smith 
3688353ddbbSDave May    Logically Collective on DM
3699a42bb27SBarry Smith 
3709a42bb27SBarry Smith    Input Parameter:
3718353ddbbSDave May +  da - the DM context
3729a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3739a42bb27SBarry Smith 
3749a42bb27SBarry Smith    Notes:
3759a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3769a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3779a42bb27SBarry Smith 
3789a42bb27SBarry Smith    Level: advanced
3799a42bb27SBarry Smith 
3808353ddbbSDave May .keywords: DM, set, options, prefix, database
3819a42bb27SBarry Smith 
3829a42bb27SBarry Smith .seealso: DMSetFromOptions()
3839a42bb27SBarry Smith @*/
3847087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3859a42bb27SBarry Smith {
3869a42bb27SBarry Smith   PetscErrorCode ierr;
3879a42bb27SBarry Smith 
3889a42bb27SBarry Smith   PetscFunctionBegin;
3899a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3909a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
391691be533SLawrence Mitchell   if (dm->sf) {
392691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
393691be533SLawrence Mitchell   }
394691be533SLawrence Mitchell   if (dm->defaultSF) {
395691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
396691be533SLawrence Mitchell   }
3979a42bb27SBarry Smith   PetscFunctionReturn(0);
3989a42bb27SBarry Smith }
3999a42bb27SBarry Smith 
4009a42bb27SBarry Smith #undef __FUNCT__
40131697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
40231697293SDave May /*@C
40331697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
40431697293SDave May    DM options in the database.
40531697293SDave May 
40631697293SDave May    Logically Collective on DM
40731697293SDave May 
40831697293SDave May    Input Parameters:
40931697293SDave May +  dm - the DM context
41031697293SDave May -  prefix - the prefix string to prepend to all DM option requests
41131697293SDave May 
41231697293SDave May    Notes:
41331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
41431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
41531697293SDave May 
41631697293SDave May    Level: advanced
41731697293SDave May 
41831697293SDave May .keywords: DM, append, options, prefix, database
41931697293SDave May 
42031697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
42131697293SDave May @*/
42231697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
42331697293SDave May {
42431697293SDave May   PetscErrorCode ierr;
42531697293SDave May 
42631697293SDave May   PetscFunctionBegin;
42731697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42831697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
42931697293SDave May   PetscFunctionReturn(0);
43031697293SDave May }
43131697293SDave May 
43231697293SDave May #undef __FUNCT__
43331697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
43431697293SDave May /*@C
43531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
43631697293SDave May    DM options in the database.
43731697293SDave May 
43831697293SDave May    Not Collective
43931697293SDave May 
44031697293SDave May    Input Parameters:
44131697293SDave May .  dm - the DM context
44231697293SDave May 
44331697293SDave May    Output Parameters:
44431697293SDave May .  prefix - pointer to the prefix string used is returned
44531697293SDave May 
44631697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
44731697293SDave May    sufficient length to hold the prefix.
44831697293SDave May 
44931697293SDave May    Level: advanced
45031697293SDave May 
45131697293SDave May .keywords: DM, set, options, prefix, database
45231697293SDave May 
45331697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
45431697293SDave May @*/
45531697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
45631697293SDave May {
45731697293SDave May   PetscErrorCode ierr;
45831697293SDave May 
45931697293SDave May   PetscFunctionBegin;
46031697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46131697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
46231697293SDave May   PetscFunctionReturn(0);
46331697293SDave May }
46431697293SDave May 
46531697293SDave May #undef __FUNCT__
46688bdff64SToby Isaac #define __FUNCT__ "DMCountNonCyclicReferences"
46788bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
46888bdff64SToby Isaac {
46988bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
47088bdff64SToby Isaac   DMNamedVecLink nlink;
47188bdff64SToby Isaac   PetscErrorCode ierr;
47288bdff64SToby Isaac 
47388bdff64SToby Isaac   PetscFunctionBegin;
47488bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
47588bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
47688bdff64SToby Isaac     if (dm->localin[i])  refct--;
47788bdff64SToby Isaac     if (dm->globalin[i]) refct--;
47888bdff64SToby Isaac   }
47988bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
48088bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
48188bdff64SToby Isaac   if (dm->x) {
48288bdff64SToby Isaac     DM obj;
48388bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
48488bdff64SToby Isaac     if (obj == dm) refct--;
48588bdff64SToby Isaac   }
48688bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
48788bdff64SToby Isaac     refct--;
48888bdff64SToby Isaac     if (recurseCoarse) {
48988bdff64SToby Isaac       PetscInt coarseCount;
49088bdff64SToby Isaac 
49188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
49288bdff64SToby Isaac       refct += coarseCount;
49388bdff64SToby Isaac     }
49488bdff64SToby Isaac   }
49588bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
49688bdff64SToby Isaac     refct--;
49788bdff64SToby Isaac     if (recurseFine) {
49888bdff64SToby Isaac       PetscInt fineCount;
49988bdff64SToby Isaac 
50088bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
50188bdff64SToby Isaac       refct += fineCount;
50288bdff64SToby Isaac     }
50388bdff64SToby Isaac   }
50488bdff64SToby Isaac   *ncrefct = refct;
50588bdff64SToby Isaac   PetscFunctionReturn(0);
50688bdff64SToby Isaac }
50788bdff64SToby Isaac 
508*bcc5ffd9SToby Isaac PetscErrorCode DMBoundaryDestroy(DMBoundaryLinkList *boundary);
509a6ba4734SToby Isaac 
51088bdff64SToby Isaac #undef __FUNCT__
51147c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
51247c6ae99SBarry Smith /*@
5138472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
51447c6ae99SBarry Smith 
51547c6ae99SBarry Smith     Collective on DM
51647c6ae99SBarry Smith 
51747c6ae99SBarry Smith     Input Parameter:
51847c6ae99SBarry Smith .   dm - the DM object to destroy
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith     Level: developer
52147c6ae99SBarry Smith 
522e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
52347c6ae99SBarry Smith 
52447c6ae99SBarry Smith @*/
525fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
52647c6ae99SBarry Smith {
52788bdff64SToby Isaac   PetscInt       i, cnt;
528dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
52947c6ae99SBarry Smith   PetscErrorCode ierr;
53047c6ae99SBarry Smith 
53147c6ae99SBarry Smith   PetscFunctionBegin;
5326bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5336bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
53487e657c6SBarry Smith 
53588bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
53688bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
53788bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
53888bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
539732e2eb9SMatthew G Knepley   /*
540732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
541732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
542732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
543732e2eb9SMatthew G Knepley   */
5446bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5456bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
546732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5476bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5486bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
549732e2eb9SMatthew G Knepley   }
550f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5510298fd71SBarry Smith   (*dm)->namedglobal = NULL;
552f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5532348bcf4SPeter Brune     nnext = nlink->next;
5542348bcf4SPeter 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);
5552348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5562348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5572348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5582348bcf4SPeter Brune   }
559f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5600298fd71SBarry Smith   (*dm)->namedlocal = NULL;
561f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
562f490541aSPeter Brune     nnext = nlink->next;
563f490541aSPeter 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);
564f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
565f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
566f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
567f490541aSPeter Brune   }
5682348bcf4SPeter Brune 
569b17ce1afSJed Brown   /* Destroy the list of hooks */
570c833c3b5SJed Brown   {
571c833c3b5SJed Brown     DMCoarsenHookLink link,next;
572b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
573b17ce1afSJed Brown       next = link->next;
574b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
575b17ce1afSJed Brown     }
5760298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
577c833c3b5SJed Brown   }
578c833c3b5SJed Brown   {
579c833c3b5SJed Brown     DMRefineHookLink link,next;
580c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
581c833c3b5SJed Brown       next = link->next;
582c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
583c833c3b5SJed Brown     }
5840298fd71SBarry Smith     (*dm)->refinehook = NULL;
585c833c3b5SJed Brown   }
586be081cd6SPeter Brune   {
587be081cd6SPeter Brune     DMSubDomainHookLink link,next;
588be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
589be081cd6SPeter Brune       next = link->next;
590be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
591be081cd6SPeter Brune     }
5920298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
593be081cd6SPeter Brune   }
594baf369e7SPeter Brune   {
595baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
596baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
597baf369e7SPeter Brune       next = link->next;
598baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
599baf369e7SPeter Brune     }
6000298fd71SBarry Smith     (*dm)->gtolhook = NULL;
601baf369e7SPeter Brune   }
602d4d07f1eSToby Isaac   {
603d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
604d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
605d4d07f1eSToby Isaac       next = link->next;
606d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
607d4d07f1eSToby Isaac     }
608d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
609d4d07f1eSToby Isaac   }
610aa1993deSMatthew G Knepley   /* Destroy the work arrays */
611aa1993deSMatthew G Knepley   {
612aa1993deSMatthew G Knepley     DMWorkLink link,next;
613aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
614aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
615aa1993deSMatthew G Knepley       next = link->next;
616aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
617aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
618aa1993deSMatthew G Knepley     }
6190298fd71SBarry Smith     (*dm)->workin = NULL;
620aa1993deSMatthew G Knepley   }
621c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
622c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
623c58f1c22SToby Isaac 
624c58f1c22SToby Isaac     /* destroy the labels */
625c58f1c22SToby Isaac     while (next) {
626c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
627c58f1c22SToby Isaac 
628c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
629c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
630c58f1c22SToby Isaac       next = tmp;
631c58f1c22SToby Isaac     }
632c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
633c58f1c22SToby Isaac   }
634*bcc5ffd9SToby Isaac   ierr = DMBoundaryDestroy(&(*dm)->boundary);CHKERRQ(ierr);
635b17ce1afSJed Brown 
63652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
63752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
63852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
63952536dc3SBarry Smith 
6401a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6411a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6421a266240SBarry Smith   }
64387e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
64471cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6454dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6466bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6476bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
648073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
64988ed4aceSMatthew G Knepley 
65088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
65188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6528b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
653fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
654fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
65588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
65688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6578e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
658af122d2aSMatthew G Knepley 
65988bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
66088bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
66188bdff64SToby Isaac   }
662a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
66388bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
66488bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
66588bdff64SToby Isaac   }
66688bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
6676636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6686636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6696636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6705dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6716636e97aSMatthew G Knepley 
6722764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
67314f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
674e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
675e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
676732e2eb9SMatthew G Knepley 
6776bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
678435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
679732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
68047c6ae99SBarry Smith   PetscFunctionReturn(0);
68147c6ae99SBarry Smith }
68247c6ae99SBarry Smith 
68347c6ae99SBarry Smith #undef __FUNCT__
684d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
685d7bf68aeSBarry Smith /*@
686d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
687d7bf68aeSBarry Smith 
688d7bf68aeSBarry Smith     Collective on DM
689d7bf68aeSBarry Smith 
690d7bf68aeSBarry Smith     Input Parameter:
691d7bf68aeSBarry Smith .   dm - the DM object to setup
692d7bf68aeSBarry Smith 
693d7bf68aeSBarry Smith     Level: developer
694d7bf68aeSBarry Smith 
695e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
696d7bf68aeSBarry Smith 
697d7bf68aeSBarry Smith @*/
6987087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
699d7bf68aeSBarry Smith {
700d7bf68aeSBarry Smith   PetscErrorCode ierr;
701d7bf68aeSBarry Smith 
702d7bf68aeSBarry Smith   PetscFunctionBegin;
703171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7048387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
705d7bf68aeSBarry Smith   if (dm->ops->setup) {
706d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
707d7bf68aeSBarry Smith   }
7088387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
709d7bf68aeSBarry Smith   PetscFunctionReturn(0);
710d7bf68aeSBarry Smith }
711d7bf68aeSBarry Smith 
712d7bf68aeSBarry Smith #undef __FUNCT__
713d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
714d7bf68aeSBarry Smith /*@
715d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
716d7bf68aeSBarry Smith 
717d7bf68aeSBarry Smith     Collective on DM
718d7bf68aeSBarry Smith 
719d7bf68aeSBarry Smith     Input Parameter:
720d7bf68aeSBarry Smith .   dm - the DM object to set options for
721d7bf68aeSBarry Smith 
722732e2eb9SMatthew G Knepley     Options Database:
7234164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7244164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7254164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7264164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
727732e2eb9SMatthew G Knepley 
728d7bf68aeSBarry Smith     Level: developer
729d7bf68aeSBarry Smith 
730e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
731d7bf68aeSBarry Smith 
732d7bf68aeSBarry Smith @*/
7337087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
734d7bf68aeSBarry Smith {
7357781c08eSBarry Smith   char           typeName[256];
736ca266f36SBarry Smith   PetscBool      flg;
737d7bf68aeSBarry Smith   PetscErrorCode ierr;
738d7bf68aeSBarry Smith 
739d7bf68aeSBarry Smith   PetscFunctionBegin;
740171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
741691be533SLawrence Mitchell   if (dm->sf) {
742691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
743691be533SLawrence Mitchell   }
744691be533SLawrence Mitchell   if (dm->defaultSF) {
745691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
746691be533SLawrence Mitchell   }
7473194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7480298fd71SBarry 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);
749a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
750f9ba7244SBarry Smith   if (flg) {
751f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
752f9ba7244SBarry Smith   }
753a264d7a6SBarry 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);
754073dac72SJed Brown   if (flg) {
755521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
756073dac72SJed Brown   }
7570298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
758f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
759e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
760f9ba7244SBarry Smith   }
761f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
762f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
76382fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
764d7bf68aeSBarry Smith   PetscFunctionReturn(0);
765d7bf68aeSBarry Smith }
766d7bf68aeSBarry Smith 
767d7bf68aeSBarry Smith #undef __FUNCT__
76847c6ae99SBarry Smith #define __FUNCT__ "DMView"
769fc9bc008SSatish Balay /*@C
770224748a4SBarry Smith     DMView - Views a DM
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith     Collective on DM
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith     Input Parameter:
77547c6ae99SBarry Smith +   dm - the DM object to view
77647c6ae99SBarry Smith -   v - the viewer
77747c6ae99SBarry Smith 
778224748a4SBarry Smith     Level: beginner
77947c6ae99SBarry Smith 
780e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
78147c6ae99SBarry Smith 
78247c6ae99SBarry Smith @*/
7837087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
78447c6ae99SBarry Smith {
78547c6ae99SBarry Smith   PetscErrorCode ierr;
78632c0f0efSBarry Smith   PetscBool      isbinary;
78747c6ae99SBarry Smith 
78847c6ae99SBarry Smith   PetscFunctionBegin;
789171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7903014e516SBarry Smith   if (!v) {
791ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
7923014e516SBarry Smith   }
79398c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
79432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
79532c0f0efSBarry Smith   if (isbinary) {
79655849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
79732c0f0efSBarry Smith     char     type[256];
79832c0f0efSBarry Smith 
79932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
80032c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
80132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
80232c0f0efSBarry Smith   }
8030c010503SBarry Smith   if (dm->ops->view) {
8040c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
80547c6ae99SBarry Smith   }
80647c6ae99SBarry Smith   PetscFunctionReturn(0);
80747c6ae99SBarry Smith }
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith #undef __FUNCT__
81047c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
81147c6ae99SBarry Smith /*@
8128472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith     Collective on DM
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith     Input Parameter:
81747c6ae99SBarry Smith .   dm - the DM object
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith     Output Parameter:
82047c6ae99SBarry Smith .   vec - the global vector
82147c6ae99SBarry Smith 
822073dac72SJed Brown     Level: beginner
82347c6ae99SBarry Smith 
824e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
82547c6ae99SBarry Smith 
82647c6ae99SBarry Smith @*/
8277087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
82847c6ae99SBarry Smith {
82947c6ae99SBarry Smith   PetscErrorCode ierr;
83047c6ae99SBarry Smith 
83147c6ae99SBarry Smith   PetscFunctionBegin;
832171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83347c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
83447c6ae99SBarry Smith   PetscFunctionReturn(0);
83547c6ae99SBarry Smith }
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith #undef __FUNCT__
83847c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
83947c6ae99SBarry Smith /*@
8408472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith     Not Collective
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith     Input Parameter:
84547c6ae99SBarry Smith .   dm - the DM object
84647c6ae99SBarry Smith 
84747c6ae99SBarry Smith     Output Parameter:
84847c6ae99SBarry Smith .   vec - the local vector
84947c6ae99SBarry Smith 
850073dac72SJed Brown     Level: beginner
85147c6ae99SBarry Smith 
852e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
85347c6ae99SBarry Smith 
85447c6ae99SBarry Smith @*/
8557087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
85647c6ae99SBarry Smith {
85747c6ae99SBarry Smith   PetscErrorCode ierr;
85847c6ae99SBarry Smith 
85947c6ae99SBarry Smith   PetscFunctionBegin;
860171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
86247c6ae99SBarry Smith   PetscFunctionReturn(0);
86347c6ae99SBarry Smith }
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith #undef __FUNCT__
8661411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
8671411c6eeSJed Brown /*@
8681411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8691411c6eeSJed Brown 
8701411c6eeSJed Brown    Collective on DM
8711411c6eeSJed Brown 
8721411c6eeSJed Brown    Input Parameter:
8731411c6eeSJed Brown .  dm - the DM that provides the mapping
8741411c6eeSJed Brown 
8751411c6eeSJed Brown    Output Parameter:
8761411c6eeSJed Brown .  ltog - the mapping
8771411c6eeSJed Brown 
8781411c6eeSJed Brown    Level: intermediate
8791411c6eeSJed Brown 
8801411c6eeSJed Brown    Notes:
8811411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8821411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8831411c6eeSJed Brown 
884fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8851411c6eeSJed Brown @*/
8867087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8871411c6eeSJed Brown {
8881411c6eeSJed Brown   PetscErrorCode ierr;
8891411c6eeSJed Brown 
8901411c6eeSJed Brown   PetscFunctionBegin;
8911411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8921411c6eeSJed Brown   PetscValidPointer(ltog,2);
8931411c6eeSJed Brown   if (!dm->ltogmap) {
89437d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
89537d0c07bSMatthew G Knepley 
89637d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
89737d0c07bSMatthew G Knepley     if (section) {
89837d0c07bSMatthew G Knepley       PetscInt *ltog;
89937d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
90037d0c07bSMatthew G Knepley 
90137d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
90237d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
90337d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
904785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
90537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
90637d0c07bSMatthew G Knepley         PetscInt dof, off, c;
90737d0c07bSMatthew G Knepley 
90837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
90937d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
91037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
91137d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
91237d0c07bSMatthew G Knepley           ltog[l] = off+c;
91337d0c07bSMatthew G Knepley         }
91437d0c07bSMatthew G Knepley       }
915f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9163bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
91737d0c07bSMatthew G Knepley     } else {
918184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
919184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9201411c6eeSJed Brown     }
92137d0c07bSMatthew G Knepley   }
9221411c6eeSJed Brown   *ltog = dm->ltogmap;
9231411c6eeSJed Brown   PetscFunctionReturn(0);
9241411c6eeSJed Brown }
9251411c6eeSJed Brown 
9261411c6eeSJed Brown #undef __FUNCT__
9271411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
9281411c6eeSJed Brown /*@
9291411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9301411c6eeSJed Brown 
9311411c6eeSJed Brown    Not Collective
9321411c6eeSJed Brown 
9331411c6eeSJed Brown    Input Parameter:
9341411c6eeSJed Brown .  dm - the DM with block structure
9351411c6eeSJed Brown 
9361411c6eeSJed Brown    Output Parameter:
9371411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9381411c6eeSJed Brown 
9391411c6eeSJed Brown    Level: intermediate
9401411c6eeSJed Brown 
941fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9421411c6eeSJed Brown @*/
9437087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9441411c6eeSJed Brown {
9451411c6eeSJed Brown   PetscFunctionBegin;
9461411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9471411c6eeSJed Brown   PetscValidPointer(bs,2);
9481411c6eeSJed 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");
9491411c6eeSJed Brown   *bs = dm->bs;
9501411c6eeSJed Brown   PetscFunctionReturn(0);
9511411c6eeSJed Brown }
9521411c6eeSJed Brown 
9531411c6eeSJed Brown #undef __FUNCT__
954e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
95547c6ae99SBarry Smith /*@
9568472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith     Collective on DM
95947c6ae99SBarry Smith 
96047c6ae99SBarry Smith     Input Parameter:
96147c6ae99SBarry Smith +   dm1 - the DM object
96247c6ae99SBarry Smith -   dm2 - the second, finer DM object
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith     Output Parameter:
96547c6ae99SBarry Smith +  mat - the interpolation
96647c6ae99SBarry Smith -  vec - the scaling (optional)
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith     Level: developer
96947c6ae99SBarry Smith 
97085afcc9aSBarry 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
97185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
972d52bd9f3SBarry Smith 
9731f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
974d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
97585afcc9aSBarry Smith 
97685afcc9aSBarry Smith 
977e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
97847c6ae99SBarry Smith 
97947c6ae99SBarry Smith @*/
980e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
98147c6ae99SBarry Smith {
98247c6ae99SBarry Smith   PetscErrorCode ierr;
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith   PetscFunctionBegin;
985171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
986171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
987cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
98825296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
989cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
99047c6ae99SBarry Smith   PetscFunctionReturn(0);
99147c6ae99SBarry Smith }
99247c6ae99SBarry Smith 
99347c6ae99SBarry Smith #undef __FUNCT__
994e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
99547c6ae99SBarry Smith /*@
9968472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
99747c6ae99SBarry Smith 
99847c6ae99SBarry Smith     Collective on DM
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Input Parameter:
100147c6ae99SBarry Smith +   dm1 - the DM object
100247c6ae99SBarry Smith -   dm2 - the second, finer DM object
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith     Output Parameter:
10056dbf9973SLawrence Mitchell .   mat - the injection
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith     Level: developer
100847c6ae99SBarry Smith 
100985afcc9aSBarry 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
101085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
101185afcc9aSBarry Smith 
1012e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith @*/
10156dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
101647c6ae99SBarry Smith {
101747c6ae99SBarry Smith   PetscErrorCode ierr;
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith   PetscFunctionBegin;
1020171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1021171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10226dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
102347c6ae99SBarry Smith   PetscFunctionReturn(0);
102447c6ae99SBarry Smith }
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith #undef __FUNCT__
1027e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
1028b412c318SBarry Smith /*@
1029b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
103047c6ae99SBarry Smith 
103147c6ae99SBarry Smith     Collective on DM
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith     Input Parameter:
103447c6ae99SBarry Smith +   dm - the DM object
1035b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith     Output Parameter:
103847c6ae99SBarry Smith .   coloring - the coloring
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith     Level: developer
104147c6ae99SBarry Smith 
1042b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
104347c6ae99SBarry Smith 
1044aab9d709SJed Brown @*/
1045b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
104647c6ae99SBarry Smith {
104747c6ae99SBarry Smith   PetscErrorCode ierr;
104847c6ae99SBarry Smith 
104947c6ae99SBarry Smith   PetscFunctionBegin;
1050171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1051ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1052b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
105347c6ae99SBarry Smith   PetscFunctionReturn(0);
105447c6ae99SBarry Smith }
105547c6ae99SBarry Smith 
105647c6ae99SBarry Smith #undef __FUNCT__
1057950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1058b412c318SBarry Smith /*@
10598472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
106047c6ae99SBarry Smith 
106147c6ae99SBarry Smith     Collective on DM
106247c6ae99SBarry Smith 
106347c6ae99SBarry Smith     Input Parameter:
1064b412c318SBarry Smith .   dm - the DM object
106547c6ae99SBarry Smith 
106647c6ae99SBarry Smith     Output Parameter:
106747c6ae99SBarry Smith .   mat - the empty Jacobian
106847c6ae99SBarry Smith 
1069073dac72SJed Brown     Level: beginner
107047c6ae99SBarry Smith 
107194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
107294013140SBarry Smith        do not need to do it yourself.
107394013140SBarry Smith 
107494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1075aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
107694013140SBarry Smith 
107794013140SBarry 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
107894013140SBarry Smith        internally by PETSc.
107994013140SBarry Smith 
108094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1081aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
108294013140SBarry Smith 
1083b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
108447c6ae99SBarry Smith 
1085aab9d709SJed Brown @*/
1086b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
108747c6ae99SBarry Smith {
108847c6ae99SBarry Smith   PetscErrorCode ierr;
108947c6ae99SBarry Smith 
109047c6ae99SBarry Smith   PetscFunctionBegin;
1091171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1092607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1093c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1094c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1095b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
109647c6ae99SBarry Smith   PetscFunctionReturn(0);
109747c6ae99SBarry Smith }
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith #undef __FUNCT__
1100732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1101732e2eb9SMatthew G Knepley /*@
1102950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1103732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1104732e2eb9SMatthew G Knepley 
11058472ad0fSDave May   Logically Collective on DM
1106732e2eb9SMatthew G Knepley 
1107732e2eb9SMatthew G Knepley   Input Parameter:
1108732e2eb9SMatthew G Knepley + dm - the DM
1109732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1110732e2eb9SMatthew G Knepley 
1111732e2eb9SMatthew G Knepley   Level: developer
1112950540a4SJed Brown .seealso DMCreateMatrix()
1113732e2eb9SMatthew G Knepley @*/
1114732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1115732e2eb9SMatthew G Knepley {
1116732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1117732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1118732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1119732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1120732e2eb9SMatthew G Knepley }
1121732e2eb9SMatthew G Knepley 
1122732e2eb9SMatthew G Knepley #undef __FUNCT__
1123a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1124a89ea682SMatthew G Knepley /*@C
1125aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1126a89ea682SMatthew G Knepley 
1127a89ea682SMatthew G Knepley   Not Collective
1128a89ea682SMatthew G Knepley 
1129a89ea682SMatthew G Knepley   Input Parameters:
1130a89ea682SMatthew G Knepley + dm - the DM object
1131aa1993deSMatthew G Knepley . count - The minium size
1132aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1133a89ea682SMatthew G Knepley 
1134a89ea682SMatthew G Knepley   Output Parameter:
1135a89ea682SMatthew G Knepley . array - the work array
1136a89ea682SMatthew G Knepley 
1137a89ea682SMatthew G Knepley   Level: developer
1138a89ea682SMatthew G Knepley 
1139a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1140a89ea682SMatthew G Knepley @*/
1141aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1142a89ea682SMatthew G Knepley {
1143a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1144aa1993deSMatthew G Knepley   DMWorkLink     link;
1145854ce69bSBarry Smith   size_t         dsize;
1146a89ea682SMatthew G Knepley 
1147a89ea682SMatthew G Knepley   PetscFunctionBegin;
1148a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1149aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1150aa1993deSMatthew G Knepley   if (dm->workin) {
1151aa1993deSMatthew G Knepley     link       = dm->workin;
1152aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1153aa1993deSMatthew G Knepley   } else {
1154b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1155a89ea682SMatthew G Knepley   }
1156854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1157854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1158aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1159854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1160854ce69bSBarry Smith     link->bytes = dsize*count;
1161aa1993deSMatthew G Knepley   }
1162aa1993deSMatthew G Knepley   link->next   = dm->workout;
1163aa1993deSMatthew G Knepley   dm->workout  = link;
1164aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1165a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1166a89ea682SMatthew G Knepley }
1167a89ea682SMatthew G Knepley 
1168aa1993deSMatthew G Knepley #undef __FUNCT__
1169aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1170aa1993deSMatthew G Knepley /*@C
1171aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1172aa1993deSMatthew G Knepley 
1173aa1993deSMatthew G Knepley   Not Collective
1174aa1993deSMatthew G Knepley 
1175aa1993deSMatthew G Knepley   Input Parameters:
1176aa1993deSMatthew G Knepley + dm - the DM object
1177aa1993deSMatthew G Knepley . count - The minium size
1178aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1179aa1993deSMatthew G Knepley 
1180aa1993deSMatthew G Knepley   Output Parameter:
1181aa1993deSMatthew G Knepley . array - the work array
1182aa1993deSMatthew G Knepley 
1183aa1993deSMatthew G Knepley   Level: developer
1184aa1993deSMatthew G Knepley 
1185aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1186aa1993deSMatthew G Knepley @*/
1187aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1188aa1993deSMatthew G Knepley {
1189aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1190aa1993deSMatthew G Knepley 
1191aa1993deSMatthew G Knepley   PetscFunctionBegin;
1192aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1193aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1194aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1195aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1196aa1993deSMatthew G Knepley       *p           = link->next;
1197aa1993deSMatthew G Knepley       link->next   = dm->workin;
1198aa1993deSMatthew G Knepley       dm->workin   = link;
11990298fd71SBarry Smith       *(void**)mem = NULL;
1200aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1201aa1993deSMatthew G Knepley     }
1202aa1993deSMatthew G Knepley   }
1203aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1204aa1993deSMatthew G Knepley }
1205e7c4fc90SDmitry Karpeev 
1206e7c4fc90SDmitry Karpeev #undef __FUNCT__
1207435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1208435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1209435a35e8SMatthew G Knepley {
1210435a35e8SMatthew G Knepley   PetscFunctionBegin;
1211435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
121282f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1213435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1214435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1215435a35e8SMatthew G Knepley }
1216435a35e8SMatthew G Knepley 
1217435a35e8SMatthew G Knepley #undef __FUNCT__
12184d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
12194f3b5142SJed Brown /*@C
12204d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12214d343eeaSMatthew G Knepley 
12224d343eeaSMatthew G Knepley   Not collective
12234d343eeaSMatthew G Knepley 
12244d343eeaSMatthew G Knepley   Input Parameter:
12254d343eeaSMatthew G Knepley . dm - the DM object
12264d343eeaSMatthew G Knepley 
12274d343eeaSMatthew G Knepley   Output Parameters:
12280298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
12290298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
12300298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
12314d343eeaSMatthew G Knepley 
12324d343eeaSMatthew G Knepley   Level: intermediate
12334d343eeaSMatthew G Knepley 
123421c9b008SJed Brown   Notes:
123521c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
123621c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
123721c9b008SJed Brown   PetscFree().
123821c9b008SJed Brown 
12394d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
12404d343eeaSMatthew G Knepley @*/
124137d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
12424d343eeaSMatthew G Knepley {
124337d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
12444d343eeaSMatthew G Knepley   PetscErrorCode ierr;
12454d343eeaSMatthew G Knepley 
12464d343eeaSMatthew G Knepley   PetscFunctionBegin;
12474d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
124869ca1f37SDmitry Karpeev   if (numFields) {
124969ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
125069ca1f37SDmitry Karpeev     *numFields = 0;
125169ca1f37SDmitry Karpeev   }
125237d0c07bSMatthew G Knepley   if (fieldNames) {
125337d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
12540298fd71SBarry Smith     *fieldNames = NULL;
125569ca1f37SDmitry Karpeev   }
125669ca1f37SDmitry Karpeev   if (fields) {
125769ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
12580298fd71SBarry Smith     *fields = NULL;
125969ca1f37SDmitry Karpeev   }
126037d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
126137d0c07bSMatthew G Knepley   if (section) {
126237d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
126337d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
126437d0c07bSMatthew G Knepley 
126537d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
126637d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1267dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
126837d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
126937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
127037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
127137d0c07bSMatthew G Knepley     }
127237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
127337d0c07bSMatthew G Knepley       PetscInt gdof;
127437d0c07bSMatthew G Knepley 
127537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
127637d0c07bSMatthew G Knepley       if (gdof > 0) {
127737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
127837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
127937d0c07bSMatthew G Knepley 
128037d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
128137d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
128237d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
128337d0c07bSMatthew G Knepley         }
128437d0c07bSMatthew G Knepley       }
128537d0c07bSMatthew G Knepley     }
128637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1287785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
128837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
128937d0c07bSMatthew G Knepley     }
129037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
129137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
129237d0c07bSMatthew G Knepley 
129337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
129437d0c07bSMatthew G Knepley       if (gdof > 0) {
129537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
129637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
129737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
129837d0c07bSMatthew G Knepley 
129937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
130037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
130137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
130237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
130337d0c07bSMatthew G Knepley           }
130437d0c07bSMatthew G Knepley         }
130537d0c07bSMatthew G Knepley       }
130637d0c07bSMatthew G Knepley     }
13078865f1eaSKarl Rupp     if (numFields) *numFields = nF;
130837d0c07bSMatthew G Knepley     if (fieldNames) {
1309785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
131037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
131137d0c07bSMatthew G Knepley         const char *fieldName;
131237d0c07bSMatthew G Knepley 
131337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
131437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
131537d0c07bSMatthew G Knepley       }
131637d0c07bSMatthew G Knepley     }
131737d0c07bSMatthew G Knepley     if (fields) {
1318785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
131937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
132082f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
132137d0c07bSMatthew G Knepley       }
132237d0c07bSMatthew G Knepley     }
132337d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13248865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
13258865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
132669ca1f37SDmitry Karpeev   }
13274d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
13284d343eeaSMatthew G Knepley }
13294d343eeaSMatthew G Knepley 
133016621825SDmitry Karpeev 
133116621825SDmitry Karpeev #undef __FUNCT__
133216621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
133316621825SDmitry Karpeev /*@C
133416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
133516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
133616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1337e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1338e7c4fc90SDmitry Karpeev 
1339e7c4fc90SDmitry Karpeev   Not collective
1340e7c4fc90SDmitry Karpeev 
1341e7c4fc90SDmitry Karpeev   Input Parameter:
1342e7c4fc90SDmitry Karpeev . dm - the DM object
1343e7c4fc90SDmitry Karpeev 
1344e7c4fc90SDmitry Karpeev   Output Parameters:
13450298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
13460298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
13470298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
13480298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1349e7c4fc90SDmitry Karpeev 
1350e7c4fc90SDmitry Karpeev   Level: intermediate
1351e7c4fc90SDmitry Karpeev 
1352e7c4fc90SDmitry Karpeev   Notes:
1353e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1354e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1355e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1356e7c4fc90SDmitry Karpeev 
1357e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1358e7c4fc90SDmitry Karpeev @*/
135916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1360e7c4fc90SDmitry Karpeev {
1361e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1362e7c4fc90SDmitry Karpeev 
1363e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1364e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13658865f1eaSKarl Rupp   if (len) {
13668865f1eaSKarl Rupp     PetscValidPointer(len,2);
13678865f1eaSKarl Rupp     *len = 0;
13688865f1eaSKarl Rupp   }
13698865f1eaSKarl Rupp   if (namelist) {
13708865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
13718865f1eaSKarl Rupp     *namelist = 0;
13728865f1eaSKarl Rupp   }
13738865f1eaSKarl Rupp   if (islist) {
13748865f1eaSKarl Rupp     PetscValidPointer(islist,4);
13758865f1eaSKarl Rupp     *islist = 0;
13768865f1eaSKarl Rupp   }
13778865f1eaSKarl Rupp   if (dmlist) {
13788865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
13798865f1eaSKarl Rupp     *dmlist = 0;
13808865f1eaSKarl Rupp   }
1381f3f0edfdSDmitry Karpeev   /*
1382f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1383f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1384f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1385f3f0edfdSDmitry Karpeev    */
1386ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
138716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1388435a35e8SMatthew G Knepley     PetscSection section;
1389435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1390435a35e8SMatthew G Knepley 
1391435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1392435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1393435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1394435a35e8SMatthew G Knepley       *len = numFields;
139503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
139603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
139703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1398435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1399435a35e8SMatthew G Knepley         const char *fieldName;
1400435a35e8SMatthew G Knepley 
140103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
140203dc3394SMatthew G. Knepley         if (namelist) {
1403435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1404435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1405435a35e8SMatthew G Knepley         }
140603dc3394SMatthew G. Knepley       }
1407435a35e8SMatthew G Knepley     } else {
140869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1409e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14100298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1411e7c4fc90SDmitry Karpeev     }
14128865f1eaSKarl Rupp   } else {
141316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
141416621825SDmitry Karpeev   }
141516621825SDmitry Karpeev   PetscFunctionReturn(0);
141616621825SDmitry Karpeev }
141716621825SDmitry Karpeev 
141816621825SDmitry Karpeev #undef __FUNCT__
1419435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1420564cec59SMatthew G. Knepley /*@
1421435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1422435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1423435a35e8SMatthew G Knepley 
1424435a35e8SMatthew G Knepley   Not collective
1425435a35e8SMatthew G Knepley 
1426435a35e8SMatthew G Knepley   Input Parameters:
1427435a35e8SMatthew G Knepley + dm - the DM object
1428435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
14290298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1430435a35e8SMatthew G Knepley 
1431435a35e8SMatthew G Knepley   Output Parameters:
1432435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1433435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1434435a35e8SMatthew G Knepley 
1435435a35e8SMatthew G Knepley   Level: intermediate
1436435a35e8SMatthew G Knepley 
1437435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1438435a35e8SMatthew G Knepley @*/
1439435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1440435a35e8SMatthew G Knepley {
1441435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1442435a35e8SMatthew G Knepley 
1443435a35e8SMatthew G Knepley   PetscFunctionBegin;
1444435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1445435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
14468865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
14478865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1448435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1449435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
145082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1451435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1452435a35e8SMatthew G Knepley }
1453435a35e8SMatthew G Knepley 
145416621825SDmitry Karpeev 
145516621825SDmitry Karpeev #undef __FUNCT__
145616621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
145716621825SDmitry Karpeev /*@C
14588d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
14598d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
14608d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
14618d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
14628d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
146316621825SDmitry Karpeev 
146416621825SDmitry Karpeev   Not collective
146516621825SDmitry Karpeev 
146616621825SDmitry Karpeev   Input Parameter:
146716621825SDmitry Karpeev . dm - the DM object
146816621825SDmitry Karpeev 
146916621825SDmitry Karpeev   Output Parameters:
14700298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
14710298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
14720298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
14730298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
14740298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
147516621825SDmitry Karpeev 
147616621825SDmitry Karpeev   Level: intermediate
147716621825SDmitry Karpeev 
147816621825SDmitry Karpeev   Notes:
147916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
148016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
148116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
148216621825SDmitry Karpeev 
14838d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
148416621825SDmitry Karpeev @*/
14858d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
148616621825SDmitry Karpeev {
148716621825SDmitry Karpeev   PetscErrorCode      ierr;
1488be081cd6SPeter Brune   DMSubDomainHookLink link;
1489be081cd6SPeter Brune   PetscInt            i,l;
149016621825SDmitry Karpeev 
149116621825SDmitry Karpeev   PetscFunctionBegin;
149216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
149314a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14940298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14950298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14960298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14970298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1498f3f0edfdSDmitry Karpeev   /*
1499f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1500f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1501f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1502f3f0edfdSDmitry Karpeev    */
1503ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
150416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1505be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
150614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
150714a18fd3SPeter Brune     if (dmlist) {
1508be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1509be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1510be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1511be081cd6SPeter Brune         }
1512d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1513e7c4fc90SDmitry Karpeev       }
151414a18fd3SPeter Brune     }
151514a18fd3SPeter Brune     if (len) *len = l;
151614a18fd3SPeter Brune   }
1517e30e807fSPeter Brune   PetscFunctionReturn(0);
1518e30e807fSPeter Brune }
1519e30e807fSPeter Brune 
1520e30e807fSPeter Brune 
1521e30e807fSPeter Brune #undef __FUNCT__
1522e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1523e30e807fSPeter Brune /*@C
1524e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1525e30e807fSPeter Brune 
1526e30e807fSPeter Brune   Not collective
1527e30e807fSPeter Brune 
1528e30e807fSPeter Brune   Input Parameters:
1529e30e807fSPeter Brune + dm - the DM object
1530e30e807fSPeter Brune . n  - the number of subdomain scatters
1531e30e807fSPeter Brune - subdms - the local subdomains
1532e30e807fSPeter Brune 
1533e30e807fSPeter Brune   Output Parameters:
1534e30e807fSPeter Brune + n     - the number of scatters returned
1535e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1536e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1537e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1538e30e807fSPeter Brune 
1539e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1540e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1541e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1542e30e807fSPeter Brune   solution and residual data.
1543e30e807fSPeter Brune 
1544e30e807fSPeter Brune   Level: developer
1545e30e807fSPeter Brune 
1546e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1547e30e807fSPeter Brune @*/
1548e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1549e30e807fSPeter Brune {
1550e30e807fSPeter Brune   PetscErrorCode ierr;
1551e30e807fSPeter Brune 
1552e30e807fSPeter Brune   PetscFunctionBegin;
1553e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1554e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1555e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1556e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
155782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1558e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1559e7c4fc90SDmitry Karpeev }
1560e7c4fc90SDmitry Karpeev 
1561731c8d9eSDmitry Karpeev #undef __FUNCT__
156247c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
156347c6ae99SBarry Smith /*@
156447c6ae99SBarry Smith   DMRefine - Refines a DM object
156547c6ae99SBarry Smith 
156647c6ae99SBarry Smith   Collective on DM
156747c6ae99SBarry Smith 
156847c6ae99SBarry Smith   Input Parameter:
156947c6ae99SBarry Smith + dm   - the DM object
157091d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
157147c6ae99SBarry Smith 
157247c6ae99SBarry Smith   Output Parameter:
15730298fd71SBarry Smith . dmf - the refined DM, or NULL
1574ae0a1c52SMatthew G Knepley 
15750298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
157647c6ae99SBarry Smith 
157747c6ae99SBarry Smith   Level: developer
157847c6ae99SBarry Smith 
1579e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
158047c6ae99SBarry Smith @*/
15817087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
158247c6ae99SBarry Smith {
158347c6ae99SBarry Smith   PetscErrorCode   ierr;
1584c833c3b5SJed Brown   DMRefineHookLink link;
158547c6ae99SBarry Smith 
158647c6ae99SBarry Smith   PetscFunctionBegin;
1587732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
158847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15894057135bSMatthew G Knepley   if (*dmf) {
159043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15918865f1eaSKarl Rupp 
15928cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15938865f1eaSKarl Rupp 
1594644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15950598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1596656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15978865f1eaSKarl Rupp 
1598e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1599c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
16008865f1eaSKarl Rupp       if (link->refinehook) {
16018865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16028865f1eaSKarl Rupp       }
1603c833c3b5SJed Brown     }
1604c833c3b5SJed Brown   }
1605c833c3b5SJed Brown   PetscFunctionReturn(0);
1606c833c3b5SJed Brown }
1607c833c3b5SJed Brown 
1608c833c3b5SJed Brown #undef __FUNCT__
1609c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1610bb9467b5SJed Brown /*@C
1611c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1612c833c3b5SJed Brown 
1613c833c3b5SJed Brown    Logically Collective
1614c833c3b5SJed Brown 
1615c833c3b5SJed Brown    Input Arguments:
1616c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1617c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1618c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16190298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1620c833c3b5SJed Brown 
1621c833c3b5SJed Brown    Calling sequence of refinehook:
1622c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1623c833c3b5SJed Brown 
1624c833c3b5SJed Brown +  coarse - coarse level DM
1625c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1626c833c3b5SJed Brown -  ctx - optional user-defined function context
1627c833c3b5SJed Brown 
1628c833c3b5SJed Brown    Calling sequence for interphook:
1629c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1630c833c3b5SJed Brown 
1631c833c3b5SJed Brown +  coarse - coarse level DM
1632c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1633c833c3b5SJed Brown .  fine - fine level DM to update
1634c833c3b5SJed Brown -  ctx - optional user-defined function context
1635c833c3b5SJed Brown 
1636c833c3b5SJed Brown    Level: advanced
1637c833c3b5SJed Brown 
1638c833c3b5SJed Brown    Notes:
1639c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1640c833c3b5SJed Brown 
1641c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1642c833c3b5SJed Brown 
1643bb9467b5SJed Brown    This function is currently not available from Fortran.
1644bb9467b5SJed Brown 
1645c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1646c833c3b5SJed Brown @*/
1647c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1648c833c3b5SJed Brown {
1649c833c3b5SJed Brown   PetscErrorCode   ierr;
1650c833c3b5SJed Brown   DMRefineHookLink link,*p;
1651c833c3b5SJed Brown 
1652c833c3b5SJed Brown   PetscFunctionBegin;
1653c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1654c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1655c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1656c833c3b5SJed Brown   link->refinehook = refinehook;
1657c833c3b5SJed Brown   link->interphook = interphook;
1658c833c3b5SJed Brown   link->ctx        = ctx;
16590298fd71SBarry Smith   link->next       = NULL;
1660c833c3b5SJed Brown   *p               = link;
1661c833c3b5SJed Brown   PetscFunctionReturn(0);
1662c833c3b5SJed Brown }
1663c833c3b5SJed Brown 
1664c833c3b5SJed Brown #undef __FUNCT__
1665c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1666c833c3b5SJed Brown /*@
1667c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1668c833c3b5SJed Brown 
1669c833c3b5SJed Brown    Collective if any hooks are
1670c833c3b5SJed Brown 
1671c833c3b5SJed Brown    Input Arguments:
1672c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1673c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1674c833c3b5SJed Brown -  fine - finer DM to update
1675c833c3b5SJed Brown 
1676c833c3b5SJed Brown    Level: developer
1677c833c3b5SJed Brown 
1678c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1679c833c3b5SJed Brown @*/
1680c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1681c833c3b5SJed Brown {
1682c833c3b5SJed Brown   PetscErrorCode   ierr;
1683c833c3b5SJed Brown   DMRefineHookLink link;
1684c833c3b5SJed Brown 
1685c833c3b5SJed Brown   PetscFunctionBegin;
1686c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16878865f1eaSKarl Rupp     if (link->interphook) {
16888865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16898865f1eaSKarl Rupp     }
16904057135bSMatthew G Knepley   }
169147c6ae99SBarry Smith   PetscFunctionReturn(0);
169247c6ae99SBarry Smith }
169347c6ae99SBarry Smith 
169447c6ae99SBarry Smith #undef __FUNCT__
1695eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1696eb3f98d2SBarry Smith /*@
1697eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1698eb3f98d2SBarry Smith 
1699eb3f98d2SBarry Smith     Not Collective
1700eb3f98d2SBarry Smith 
1701eb3f98d2SBarry Smith     Input Parameter:
1702eb3f98d2SBarry Smith .   dm - the DM object
1703eb3f98d2SBarry Smith 
1704eb3f98d2SBarry Smith     Output Parameter:
1705eb3f98d2SBarry Smith .   level - number of refinements
1706eb3f98d2SBarry Smith 
1707eb3f98d2SBarry Smith     Level: developer
1708eb3f98d2SBarry Smith 
17096a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1710eb3f98d2SBarry Smith 
1711eb3f98d2SBarry Smith @*/
1712eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1713eb3f98d2SBarry Smith {
1714eb3f98d2SBarry Smith   PetscFunctionBegin;
1715eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1716eb3f98d2SBarry Smith   *level = dm->levelup;
1717eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1718eb3f98d2SBarry Smith }
1719eb3f98d2SBarry Smith 
1720eb3f98d2SBarry Smith #undef __FUNCT__
1721baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1722bb9467b5SJed Brown /*@C
1723baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1724baf369e7SPeter Brune 
1725baf369e7SPeter Brune    Logically Collective
1726baf369e7SPeter Brune 
1727baf369e7SPeter Brune    Input Arguments:
1728baf369e7SPeter Brune +  dm - the DM
1729baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1730baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
17310298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1732baf369e7SPeter Brune 
1733baf369e7SPeter Brune    Calling sequence for beginhook:
1734baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1735baf369e7SPeter Brune 
1736baf369e7SPeter Brune +  dm - global DM
1737baf369e7SPeter Brune .  g - global vector
1738baf369e7SPeter Brune .  mode - mode
1739baf369e7SPeter Brune .  l - local vector
1740baf369e7SPeter Brune -  ctx - optional user-defined function context
1741baf369e7SPeter Brune 
1742baf369e7SPeter Brune 
1743baf369e7SPeter Brune    Calling sequence for endhook:
1744ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1745baf369e7SPeter Brune 
1746baf369e7SPeter Brune +  global - global DM
1747baf369e7SPeter Brune -  ctx - optional user-defined function context
1748baf369e7SPeter Brune 
1749baf369e7SPeter Brune    Level: advanced
1750baf369e7SPeter Brune 
1751baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1752baf369e7SPeter Brune @*/
1753baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1754baf369e7SPeter Brune {
1755baf369e7SPeter Brune   PetscErrorCode          ierr;
1756baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1757baf369e7SPeter Brune 
1758baf369e7SPeter Brune   PetscFunctionBegin;
1759baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1760baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1761baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1762baf369e7SPeter Brune   link->beginhook = beginhook;
1763baf369e7SPeter Brune   link->endhook   = endhook;
1764baf369e7SPeter Brune   link->ctx       = ctx;
17650298fd71SBarry Smith   link->next      = NULL;
1766baf369e7SPeter Brune   *p              = link;
1767baf369e7SPeter Brune   PetscFunctionReturn(0);
1768baf369e7SPeter Brune }
1769baf369e7SPeter Brune 
1770baf369e7SPeter Brune #undef __FUNCT__
17714c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
17724c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
17734c274da1SToby Isaac {
17744c274da1SToby Isaac   Mat cMat;
17754c274da1SToby Isaac   Vec cVec;
17764c274da1SToby Isaac   PetscSection section, cSec;
17774c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
17784c274da1SToby Isaac   PetscErrorCode ierr;
17794c274da1SToby Isaac 
17804c274da1SToby Isaac   PetscFunctionBegin;
17814c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17824c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
17834c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
17844c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
17857711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
17864c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
17874c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
17884c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
17894c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
17904c274da1SToby Isaac       if (dof) {
17914c274da1SToby Isaac         PetscScalar *vals;
17924c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
17934c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
17944c274da1SToby Isaac       }
17954c274da1SToby Isaac     }
17964c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
17974c274da1SToby Isaac   }
17984c274da1SToby Isaac   PetscFunctionReturn(0);
17994c274da1SToby Isaac }
18004c274da1SToby Isaac 
18014c274da1SToby Isaac #undef __FUNCT__
180247c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
180347c6ae99SBarry Smith /*@
180447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
180547c6ae99SBarry Smith 
180647c6ae99SBarry Smith     Neighbor-wise Collective on DM
180747c6ae99SBarry Smith 
180847c6ae99SBarry Smith     Input Parameters:
180947c6ae99SBarry Smith +   dm - the DM object
181047c6ae99SBarry Smith .   g - the global vector
181147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
181247c6ae99SBarry Smith -   l - the local vector
181347c6ae99SBarry Smith 
181447c6ae99SBarry Smith 
181547c6ae99SBarry Smith     Level: beginner
181647c6ae99SBarry Smith 
1817e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
181847c6ae99SBarry Smith 
181947c6ae99SBarry Smith @*/
18207087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
182147c6ae99SBarry Smith {
18227128ae9fSMatthew G Knepley   PetscSF                 sf;
182347c6ae99SBarry Smith   PetscErrorCode          ierr;
1824baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
182547c6ae99SBarry Smith 
182647c6ae99SBarry Smith   PetscFunctionBegin;
1827171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1828baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
18298865f1eaSKarl Rupp     if (link->beginhook) {
18308865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
18318865f1eaSKarl Rupp     }
1832baf369e7SPeter Brune   }
18337128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18347128ae9fSMatthew G Knepley   if (sf) {
1835ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1836ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
18377128ae9fSMatthew G Knepley 
183882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18397128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1840ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18417128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18427128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1843ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18447128ae9fSMatthew G Knepley   } else {
1845843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18467128ae9fSMatthew G Knepley   }
184747c6ae99SBarry Smith   PetscFunctionReturn(0);
184847c6ae99SBarry Smith }
184947c6ae99SBarry Smith 
185047c6ae99SBarry Smith #undef __FUNCT__
185147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
185247c6ae99SBarry Smith /*@
185347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
185447c6ae99SBarry Smith 
185547c6ae99SBarry Smith     Neighbor-wise Collective on DM
185647c6ae99SBarry Smith 
185747c6ae99SBarry Smith     Input Parameters:
185847c6ae99SBarry Smith +   dm - the DM object
185947c6ae99SBarry Smith .   g - the global vector
186047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
186147c6ae99SBarry Smith -   l - the local vector
186247c6ae99SBarry Smith 
186347c6ae99SBarry Smith 
186447c6ae99SBarry Smith     Level: beginner
186547c6ae99SBarry Smith 
1866e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
186747c6ae99SBarry Smith 
186847c6ae99SBarry Smith @*/
18697087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
187047c6ae99SBarry Smith {
18717128ae9fSMatthew G Knepley   PetscSF                 sf;
187247c6ae99SBarry Smith   PetscErrorCode          ierr;
1873ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1874ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1875baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
187647c6ae99SBarry Smith 
187747c6ae99SBarry Smith   PetscFunctionBegin;
1878171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18797128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18807128ae9fSMatthew G Knepley   if (sf) {
188182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18827128ae9fSMatthew G Knepley 
18837128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1884ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18857128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18867128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1887ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18887128ae9fSMatthew G Knepley   } else {
1889843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18907128ae9fSMatthew G Knepley   }
18914c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1892baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1893baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1894baf369e7SPeter Brune   }
189547c6ae99SBarry Smith   PetscFunctionReturn(0);
189647c6ae99SBarry Smith }
189747c6ae99SBarry Smith 
189847c6ae99SBarry Smith #undef __FUNCT__
1899d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1900d4d07f1eSToby Isaac /*@C
1901d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1902d4d07f1eSToby Isaac 
1903d4d07f1eSToby Isaac    Logically Collective
1904d4d07f1eSToby Isaac 
1905d4d07f1eSToby Isaac    Input Arguments:
1906d4d07f1eSToby Isaac +  dm - the DM
1907d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1908d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1909d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1910d4d07f1eSToby Isaac 
1911d4d07f1eSToby Isaac    Calling sequence for beginhook:
1912d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1913d4d07f1eSToby Isaac 
1914d4d07f1eSToby Isaac +  dm - global DM
1915d4d07f1eSToby Isaac .  l - local vector
1916d4d07f1eSToby Isaac .  mode - mode
1917d4d07f1eSToby Isaac .  g - global vector
1918d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1919d4d07f1eSToby Isaac 
1920d4d07f1eSToby Isaac 
1921d4d07f1eSToby Isaac    Calling sequence for endhook:
1922d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1923d4d07f1eSToby Isaac 
1924d4d07f1eSToby Isaac +  global - global DM
1925d4d07f1eSToby Isaac .  l - local vector
1926d4d07f1eSToby Isaac .  mode - mode
1927d4d07f1eSToby Isaac .  g - global vector
1928d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1929d4d07f1eSToby Isaac 
1930d4d07f1eSToby Isaac    Level: advanced
1931d4d07f1eSToby Isaac 
1932d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1933d4d07f1eSToby Isaac @*/
1934d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1935d4d07f1eSToby Isaac {
1936d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1937d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1938d4d07f1eSToby Isaac 
1939d4d07f1eSToby Isaac   PetscFunctionBegin;
1940d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1941d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1942d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1943d4d07f1eSToby Isaac   link->beginhook = beginhook;
1944d4d07f1eSToby Isaac   link->endhook   = endhook;
1945d4d07f1eSToby Isaac   link->ctx       = ctx;
1946d4d07f1eSToby Isaac   link->next      = NULL;
1947d4d07f1eSToby Isaac   *p              = link;
1948d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1949d4d07f1eSToby Isaac }
1950d4d07f1eSToby Isaac 
1951d4d07f1eSToby Isaac #undef __FUNCT__
19524c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
19534c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
19544c274da1SToby Isaac {
19554c274da1SToby Isaac   Mat cMat;
19564c274da1SToby Isaac   Vec cVec;
19574c274da1SToby Isaac   PetscSection section, cSec;
19584c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19594c274da1SToby Isaac   PetscErrorCode ierr;
19604c274da1SToby Isaac 
19614c274da1SToby Isaac   PetscFunctionBegin;
19624c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19634c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19644c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
19654c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19667711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19674c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19684c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19694c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19704c274da1SToby Isaac       if (dof) {
19714c274da1SToby Isaac         PetscInt d;
19724c274da1SToby Isaac         PetscScalar *vals;
19734c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
19744c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
19754c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
19764c274da1SToby Isaac          * we just extracted */
19774c274da1SToby Isaac         for (d = 0; d < dof; d++) {
19784c274da1SToby Isaac           vals[d] = 0.;
19794c274da1SToby Isaac         }
19804c274da1SToby Isaac       }
19814c274da1SToby Isaac     }
19824c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
19834c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19844c274da1SToby Isaac   }
19854c274da1SToby Isaac   PetscFunctionReturn(0);
19864c274da1SToby Isaac }
19874c274da1SToby Isaac 
19884c274da1SToby Isaac #undef __FUNCT__
19899a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
199047c6ae99SBarry Smith /*@
19919a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
19929a42bb27SBarry Smith 
19939a42bb27SBarry Smith     Neighbor-wise Collective on DM
19949a42bb27SBarry Smith 
19959a42bb27SBarry Smith     Input Parameters:
19969a42bb27SBarry Smith +   dm - the DM object
1997f6813fd5SJed Brown .   l - the local vector
19981eb28f2eSBarry 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.
19991eb28f2eSBarry Smith -   g - the global vector
20009a42bb27SBarry Smith 
2001d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
200284330215SMatthew 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.
20039a42bb27SBarry Smith 
20049a42bb27SBarry Smith     Level: beginner
20059a42bb27SBarry Smith 
2006e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
20079a42bb27SBarry Smith 
20089a42bb27SBarry Smith @*/
20097087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
20109a42bb27SBarry Smith {
20117128ae9fSMatthew G Knepley   PetscSF                 sf;
201284330215SMatthew G. Knepley   PetscSection            s, gs;
2013d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2014ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2015ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
201684330215SMatthew G. Knepley   PetscBool               isInsert;
201784330215SMatthew G. Knepley   PetscErrorCode          ierr;
20189a42bb27SBarry Smith 
20199a42bb27SBarry Smith   PetscFunctionBegin;
2020171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2021d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2022d4d07f1eSToby Isaac     if (link->beginhook) {
2023d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2024d4d07f1eSToby Isaac     }
2025d4d07f1eSToby Isaac   }
20264c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
20277128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
202884330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20297128ae9fSMatthew G Knepley   switch (mode) {
20307128ae9fSMatthew G Knepley   case INSERT_VALUES:
20317128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
203284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20337128ae9fSMatthew G Knepley   case ADD_VALUES:
20347128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
203584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20367128ae9fSMatthew G Knepley   default:
203782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20387128ae9fSMatthew G Knepley   }
203984330215SMatthew G. Knepley   if (sf && !isInsert) {
2040ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20417128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2042a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2043ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
204484330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
204584330215SMatthew G. Knepley   } else if (s && isInsert) {
204684330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
204784330215SMatthew G. Knepley 
204884330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
204984330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
205084330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2051ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
205284330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
205384330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2054b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
205584330215SMatthew G. Knepley 
205684330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
205703442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
205884330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2059b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
206084330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
206184330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2062b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
206303442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2064b3b16f48SMatthew 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);
2065b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2066b3b16f48SMatthew G. Knepley       if (!gcdof) {
206784330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2068b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2069b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
207084330215SMatthew G. Knepley         const PetscInt *cdofs;
207184330215SMatthew G. Knepley         PetscInt        cind = 0;
207284330215SMatthew G. Knepley 
207384330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2074b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
207584330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2076b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
207784330215SMatthew G. Knepley         }
2078b3b16f48SMatthew 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);
207984330215SMatthew G. Knepley     }
2080ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
20827128ae9fSMatthew G Knepley   } else {
2083843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20847128ae9fSMatthew G Knepley   }
20859a42bb27SBarry Smith   PetscFunctionReturn(0);
20869a42bb27SBarry Smith }
20879a42bb27SBarry Smith 
20889a42bb27SBarry Smith #undef __FUNCT__
20899a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
20909a42bb27SBarry Smith /*@
20919a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
209247c6ae99SBarry Smith 
209347c6ae99SBarry Smith     Neighbor-wise Collective on DM
209447c6ae99SBarry Smith 
209547c6ae99SBarry Smith     Input Parameters:
209647c6ae99SBarry Smith +   dm - the DM object
2097f6813fd5SJed Brown .   l - the local vector
209847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2099f6813fd5SJed Brown -   g - the global vector
210047c6ae99SBarry Smith 
210147c6ae99SBarry Smith 
210247c6ae99SBarry Smith     Level: beginner
210347c6ae99SBarry Smith 
2104e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
210547c6ae99SBarry Smith 
210647c6ae99SBarry Smith @*/
21077087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
210847c6ae99SBarry Smith {
21097128ae9fSMatthew G Knepley   PetscSF                 sf;
211084330215SMatthew G. Knepley   PetscSection            s;
2111d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
211284330215SMatthew G. Knepley   PetscBool               isInsert;
211384330215SMatthew G. Knepley   PetscErrorCode          ierr;
211447c6ae99SBarry Smith 
211547c6ae99SBarry Smith   PetscFunctionBegin;
2116171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21177128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
211884330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21197128ae9fSMatthew G Knepley   switch (mode) {
21207128ae9fSMatthew G Knepley   case INSERT_VALUES:
21217128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
212284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21237128ae9fSMatthew G Knepley   case ADD_VALUES:
21247128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
212584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21267128ae9fSMatthew G Knepley   default:
212782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21287128ae9fSMatthew G Knepley   }
212984330215SMatthew G. Knepley   if (sf && !isInsert) {
2130ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2131ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
213284330215SMatthew G. Knepley 
2133ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21347128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2135a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2136ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21377128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
213884330215SMatthew G. Knepley   } else if (s && isInsert) {
21397128ae9fSMatthew G Knepley   } else {
2140843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21417128ae9fSMatthew G Knepley   }
2142d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2143d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2144d4d07f1eSToby Isaac   }
214547c6ae99SBarry Smith   PetscFunctionReturn(0);
214647c6ae99SBarry Smith }
214747c6ae99SBarry Smith 
214847c6ae99SBarry Smith #undef __FUNCT__
2149f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2150f089877aSRichard Tran Mills /*@
2151bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2152bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2153d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2154f089877aSRichard Tran Mills 
2155bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2156f089877aSRichard Tran Mills 
2157f089877aSRichard Tran Mills    Input Parameters:
2158f089877aSRichard Tran Mills +  dm - the DM object
2159bc0a1609SRichard Tran Mills .  g - the original local vector
2160bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2161f089877aSRichard Tran Mills 
2162bc0a1609SRichard Tran Mills    Output Parameter:
2163bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2164f089877aSRichard Tran Mills 
2165f089877aSRichard Tran Mills    Level: intermediate
2166f089877aSRichard Tran Mills 
2167bc0a1609SRichard Tran Mills    Notes:
2168bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2169bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2170bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2171bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2172bc0a1609SRichard Tran Mills 
2173bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2174bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2175f089877aSRichard Tran Mills 
2176f089877aSRichard Tran Mills @*/
2177f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2178f089877aSRichard Tran Mills {
2179f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2180f089877aSRichard Tran Mills 
2181f089877aSRichard Tran Mills   PetscFunctionBegin;
2182f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2183f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2184f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2185f089877aSRichard Tran Mills }
2186f089877aSRichard Tran Mills 
2187f089877aSRichard Tran Mills #undef __FUNCT__
2188f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2189f089877aSRichard Tran Mills /*@
2190bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2191bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2192d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2193f089877aSRichard Tran Mills 
2194bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2195f089877aSRichard Tran Mills 
2196f089877aSRichard Tran Mills    Input Parameters:
2197bc0a1609SRichard Tran Mills +  da - the DM object
2198bc0a1609SRichard Tran Mills .  g - the original local vector
2199bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2200f089877aSRichard Tran Mills 
2201bc0a1609SRichard Tran Mills    Output Parameter:
2202bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2203f089877aSRichard Tran Mills 
2204f089877aSRichard Tran Mills    Level: intermediate
2205f089877aSRichard Tran Mills 
2206bc0a1609SRichard Tran Mills    Notes:
2207bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2208bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2209bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2210bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2211bc0a1609SRichard Tran Mills 
2212bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2213bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2214f089877aSRichard Tran Mills 
2215f089877aSRichard Tran Mills @*/
2216f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2217f089877aSRichard Tran Mills {
2218f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2219f089877aSRichard Tran Mills 
2220f089877aSRichard Tran Mills   PetscFunctionBegin;
2221f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2222f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2223f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2224f089877aSRichard Tran Mills }
2225f089877aSRichard Tran Mills 
2226f089877aSRichard Tran Mills 
2227f089877aSRichard Tran Mills #undef __FUNCT__
222847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
222947c6ae99SBarry Smith /*@
223047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
223147c6ae99SBarry Smith 
223247c6ae99SBarry Smith     Collective on DM
223347c6ae99SBarry Smith 
223447c6ae99SBarry Smith     Input Parameter:
223547c6ae99SBarry Smith +   dm - the DM object
223691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
223747c6ae99SBarry Smith 
223847c6ae99SBarry Smith     Output Parameter:
223947c6ae99SBarry Smith .   dmc - the coarsened DM
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith     Level: developer
224247c6ae99SBarry Smith 
2243e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
224447c6ae99SBarry Smith 
224547c6ae99SBarry Smith @*/
22467087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
224747c6ae99SBarry Smith {
224847c6ae99SBarry Smith   PetscErrorCode    ierr;
2249b17ce1afSJed Brown   DMCoarsenHookLink link;
225047c6ae99SBarry Smith 
225147c6ae99SBarry Smith   PetscFunctionBegin;
2252171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
225347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
225447c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
22558892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2256a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
225743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
22588cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2259644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
22600598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2261656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2262e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2263b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2264b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2265b17ce1afSJed Brown   }
226647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2267b17ce1afSJed Brown   PetscFunctionReturn(0);
2268b17ce1afSJed Brown }
2269b17ce1afSJed Brown 
2270b17ce1afSJed Brown #undef __FUNCT__
2271b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2272bb9467b5SJed Brown /*@C
2273b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2274b17ce1afSJed Brown 
2275b17ce1afSJed Brown    Logically Collective
2276b17ce1afSJed Brown 
2277b17ce1afSJed Brown    Input Arguments:
2278b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2279b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2280b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
22810298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2282b17ce1afSJed Brown 
2283b17ce1afSJed Brown    Calling sequence of coarsenhook:
2284b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2285b17ce1afSJed Brown 
2286b17ce1afSJed Brown +  fine - fine level DM
2287b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2288b17ce1afSJed Brown -  ctx - optional user-defined function context
2289b17ce1afSJed Brown 
2290b17ce1afSJed Brown    Calling sequence for restricthook:
2291c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2292b17ce1afSJed Brown 
2293b17ce1afSJed Brown +  fine - fine level DM
2294b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2295c833c3b5SJed Brown .  rscale - scaling vector for restriction
2296c833c3b5SJed Brown .  inject - matrix restricting by injection
2297b17ce1afSJed Brown .  coarse - coarse level DM to update
2298b17ce1afSJed Brown -  ctx - optional user-defined function context
2299b17ce1afSJed Brown 
2300b17ce1afSJed Brown    Level: advanced
2301b17ce1afSJed Brown 
2302b17ce1afSJed Brown    Notes:
2303b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2304b17ce1afSJed Brown 
2305b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2306b17ce1afSJed Brown 
2307b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2308b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2309b17ce1afSJed Brown 
2310bb9467b5SJed Brown    This function is currently not available from Fortran.
2311bb9467b5SJed Brown 
2312c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2313b17ce1afSJed Brown @*/
2314b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2315b17ce1afSJed Brown {
2316b17ce1afSJed Brown   PetscErrorCode    ierr;
2317b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2318b17ce1afSJed Brown 
2319b17ce1afSJed Brown   PetscFunctionBegin;
2320b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
23216bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2322b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2323b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2324b17ce1afSJed Brown   link->restricthook = restricthook;
2325b17ce1afSJed Brown   link->ctx          = ctx;
23260298fd71SBarry Smith   link->next         = NULL;
2327b17ce1afSJed Brown   *p                 = link;
2328b17ce1afSJed Brown   PetscFunctionReturn(0);
2329b17ce1afSJed Brown }
2330b17ce1afSJed Brown 
2331b17ce1afSJed Brown #undef __FUNCT__
2332b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2333b17ce1afSJed Brown /*@
2334b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2335b17ce1afSJed Brown 
2336b17ce1afSJed Brown    Collective if any hooks are
2337b17ce1afSJed Brown 
2338b17ce1afSJed Brown    Input Arguments:
2339b17ce1afSJed Brown +  fine - finer DM to use as a base
2340b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2341b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2342b17ce1afSJed Brown -  coarse - coarer DM to update
2343b17ce1afSJed Brown 
2344b17ce1afSJed Brown    Level: developer
2345b17ce1afSJed Brown 
2346b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2347b17ce1afSJed Brown @*/
2348b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2349b17ce1afSJed Brown {
2350b17ce1afSJed Brown   PetscErrorCode    ierr;
2351b17ce1afSJed Brown   DMCoarsenHookLink link;
2352b17ce1afSJed Brown 
2353b17ce1afSJed Brown   PetscFunctionBegin;
2354b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
23558865f1eaSKarl Rupp     if (link->restricthook) {
23568865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
23578865f1eaSKarl Rupp     }
2358b17ce1afSJed Brown   }
235947c6ae99SBarry Smith   PetscFunctionReturn(0);
236047c6ae99SBarry Smith }
236147c6ae99SBarry Smith 
236247c6ae99SBarry Smith #undef __FUNCT__
2363be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2364bb9467b5SJed Brown /*@C
2365be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
23665dbd56e3SPeter Brune 
23675dbd56e3SPeter Brune    Logically Collective
23685dbd56e3SPeter Brune 
23695dbd56e3SPeter Brune    Input Arguments:
23705dbd56e3SPeter Brune +  global - global DM
2371ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
23725dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
23730298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23745dbd56e3SPeter Brune 
2375ec4806b8SPeter Brune 
2376ec4806b8SPeter Brune    Calling sequence for ddhook:
2377ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2378ec4806b8SPeter Brune 
2379ec4806b8SPeter Brune +  global - global DM
2380ec4806b8SPeter Brune .  block  - block DM
2381ec4806b8SPeter Brune -  ctx - optional user-defined function context
2382ec4806b8SPeter Brune 
23835dbd56e3SPeter Brune    Calling sequence for restricthook:
2384ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
23855dbd56e3SPeter Brune 
23865dbd56e3SPeter Brune +  global - global DM
23875dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
23885dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2389ec4806b8SPeter Brune .  block  - block DM
23905dbd56e3SPeter Brune -  ctx - optional user-defined function context
23915dbd56e3SPeter Brune 
23925dbd56e3SPeter Brune    Level: advanced
23935dbd56e3SPeter Brune 
23945dbd56e3SPeter Brune    Notes:
2395ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
23965dbd56e3SPeter Brune 
23975dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
23985dbd56e3SPeter Brune 
23995dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2400ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
24015dbd56e3SPeter Brune 
2402bb9467b5SJed Brown    This function is currently not available from Fortran.
2403bb9467b5SJed Brown 
24045dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
24055dbd56e3SPeter Brune @*/
2406be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
24075dbd56e3SPeter Brune {
24085dbd56e3SPeter Brune   PetscErrorCode      ierr;
2409be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
24105dbd56e3SPeter Brune 
24115dbd56e3SPeter Brune   PetscFunctionBegin;
24125dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2413be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2414be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
24155dbd56e3SPeter Brune   link->restricthook = restricthook;
2416be081cd6SPeter Brune   link->ddhook       = ddhook;
24175dbd56e3SPeter Brune   link->ctx          = ctx;
24180298fd71SBarry Smith   link->next         = NULL;
24195dbd56e3SPeter Brune   *p                 = link;
24205dbd56e3SPeter Brune   PetscFunctionReturn(0);
24215dbd56e3SPeter Brune }
24225dbd56e3SPeter Brune 
24235dbd56e3SPeter Brune #undef __FUNCT__
2424be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
24255dbd56e3SPeter Brune /*@
2426be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
24275dbd56e3SPeter Brune 
24285dbd56e3SPeter Brune    Collective if any hooks are
24295dbd56e3SPeter Brune 
24305dbd56e3SPeter Brune    Input Arguments:
24315dbd56e3SPeter Brune +  fine - finer DM to use as a base
2432be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2433be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
24345dbd56e3SPeter Brune -  coarse - coarer DM to update
24355dbd56e3SPeter Brune 
24365dbd56e3SPeter Brune    Level: developer
24375dbd56e3SPeter Brune 
24385dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
24395dbd56e3SPeter Brune @*/
2440be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
24415dbd56e3SPeter Brune {
24425dbd56e3SPeter Brune   PetscErrorCode      ierr;
2443be081cd6SPeter Brune   DMSubDomainHookLink link;
24445dbd56e3SPeter Brune 
24455dbd56e3SPeter Brune   PetscFunctionBegin;
2446be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
24478865f1eaSKarl Rupp     if (link->restricthook) {
24488865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
24498865f1eaSKarl Rupp     }
24505dbd56e3SPeter Brune   }
24515dbd56e3SPeter Brune   PetscFunctionReturn(0);
24525dbd56e3SPeter Brune }
24535dbd56e3SPeter Brune 
24545dbd56e3SPeter Brune #undef __FUNCT__
24555fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
24565fe1f584SPeter Brune /*@
24576a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
24585fe1f584SPeter Brune 
24595fe1f584SPeter Brune     Not Collective
24605fe1f584SPeter Brune 
24615fe1f584SPeter Brune     Input Parameter:
24625fe1f584SPeter Brune .   dm - the DM object
24635fe1f584SPeter Brune 
24645fe1f584SPeter Brune     Output Parameter:
24656a7d9d85SPeter Brune .   level - number of coarsenings
24665fe1f584SPeter Brune 
24675fe1f584SPeter Brune     Level: developer
24685fe1f584SPeter Brune 
24696a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
24705fe1f584SPeter Brune 
24715fe1f584SPeter Brune @*/
24725fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
24735fe1f584SPeter Brune {
24745fe1f584SPeter Brune   PetscFunctionBegin;
24755fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24765fe1f584SPeter Brune   *level = dm->leveldown;
24775fe1f584SPeter Brune   PetscFunctionReturn(0);
24785fe1f584SPeter Brune }
24795fe1f584SPeter Brune 
24805fe1f584SPeter Brune 
24815fe1f584SPeter Brune 
24825fe1f584SPeter Brune #undef __FUNCT__
248347c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
248447c6ae99SBarry Smith /*@C
248547c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
248647c6ae99SBarry Smith 
248747c6ae99SBarry Smith     Collective on DM
248847c6ae99SBarry Smith 
248947c6ae99SBarry Smith     Input Parameter:
249047c6ae99SBarry Smith +   dm - the DM object
249147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
249247c6ae99SBarry Smith 
249347c6ae99SBarry Smith     Output Parameter:
249447c6ae99SBarry Smith .   dmf - the refined DM hierarchy
249547c6ae99SBarry Smith 
249647c6ae99SBarry Smith     Level: developer
249747c6ae99SBarry Smith 
2498e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
249947c6ae99SBarry Smith 
250047c6ae99SBarry Smith @*/
25017087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
250247c6ae99SBarry Smith {
250347c6ae99SBarry Smith   PetscErrorCode ierr;
250447c6ae99SBarry Smith 
250547c6ae99SBarry Smith   PetscFunctionBegin;
2506171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2507ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
250847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
250947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
251047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
251147c6ae99SBarry Smith   } else if (dm->ops->refine) {
251247c6ae99SBarry Smith     PetscInt i;
251347c6ae99SBarry Smith 
2514ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
251547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2516ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
251747c6ae99SBarry Smith     }
2518ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
251947c6ae99SBarry Smith   PetscFunctionReturn(0);
252047c6ae99SBarry Smith }
252147c6ae99SBarry Smith 
252247c6ae99SBarry Smith #undef __FUNCT__
252347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
252447c6ae99SBarry Smith /*@C
252547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
252647c6ae99SBarry Smith 
252747c6ae99SBarry Smith     Collective on DM
252847c6ae99SBarry Smith 
252947c6ae99SBarry Smith     Input Parameter:
253047c6ae99SBarry Smith +   dm - the DM object
253147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
253247c6ae99SBarry Smith 
253347c6ae99SBarry Smith     Output Parameter:
253447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
253547c6ae99SBarry Smith 
253647c6ae99SBarry Smith     Level: developer
253747c6ae99SBarry Smith 
2538e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
253947c6ae99SBarry Smith 
254047c6ae99SBarry Smith @*/
25417087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
254247c6ae99SBarry Smith {
254347c6ae99SBarry Smith   PetscErrorCode ierr;
254447c6ae99SBarry Smith 
254547c6ae99SBarry Smith   PetscFunctionBegin;
2546171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2547ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
254847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
254947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
255047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
255147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
255247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
255347c6ae99SBarry Smith     PetscInt i;
255447c6ae99SBarry Smith 
2555ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
255647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2557ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
255847c6ae99SBarry Smith     }
2559ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
256047c6ae99SBarry Smith   PetscFunctionReturn(0);
256147c6ae99SBarry Smith }
256247c6ae99SBarry Smith 
256347c6ae99SBarry Smith #undef __FUNCT__
2564e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
256547c6ae99SBarry Smith /*@
2566e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
256747c6ae99SBarry Smith    grids associated with two DMs.
256847c6ae99SBarry Smith 
256947c6ae99SBarry Smith    Collective on DM
257047c6ae99SBarry Smith 
257147c6ae99SBarry Smith    Input Parameters:
257247c6ae99SBarry Smith +  dmc - the coarse grid DM
257347c6ae99SBarry Smith -  dmf - the fine grid DM
257447c6ae99SBarry Smith 
257547c6ae99SBarry Smith    Output Parameters:
257647c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
257747c6ae99SBarry Smith 
257847c6ae99SBarry Smith    Level: intermediate
257947c6ae99SBarry Smith 
258047c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
258147c6ae99SBarry Smith 
2582e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
258347c6ae99SBarry Smith @*/
2584e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
258547c6ae99SBarry Smith {
258647c6ae99SBarry Smith   PetscErrorCode ierr;
258747c6ae99SBarry Smith 
258847c6ae99SBarry Smith   PetscFunctionBegin;
2589171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2590171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
259147c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
259247c6ae99SBarry Smith   PetscFunctionReturn(0);
259347c6ae99SBarry Smith }
259447c6ae99SBarry Smith 
259547c6ae99SBarry Smith #undef __FUNCT__
25961a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
25971a266240SBarry Smith /*@C
25981a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
25991a266240SBarry Smith 
26001a266240SBarry Smith     Not Collective
26011a266240SBarry Smith 
26021a266240SBarry Smith     Input Parameters:
26031a266240SBarry Smith +   dm - the DM object
26041a266240SBarry Smith -   destroy - the destroy function
26051a266240SBarry Smith 
26061a266240SBarry Smith     Level: intermediate
26071a266240SBarry Smith 
2608e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
26091a266240SBarry Smith 
2610f07f9ceaSJed Brown @*/
26111a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
26121a266240SBarry Smith {
26131a266240SBarry Smith   PetscFunctionBegin;
2614171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26151a266240SBarry Smith   dm->ctxdestroy = destroy;
26161a266240SBarry Smith   PetscFunctionReturn(0);
26171a266240SBarry Smith }
26181a266240SBarry Smith 
26191a266240SBarry Smith #undef __FUNCT__
26201b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2621b07ff414SBarry Smith /*@
26221b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
262347c6ae99SBarry Smith 
262447c6ae99SBarry Smith     Not Collective
262547c6ae99SBarry Smith 
262647c6ae99SBarry Smith     Input Parameters:
262747c6ae99SBarry Smith +   dm - the DM object
262847c6ae99SBarry Smith -   ctx - the user context
262947c6ae99SBarry Smith 
263047c6ae99SBarry Smith     Level: intermediate
263147c6ae99SBarry Smith 
2632e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
263347c6ae99SBarry Smith 
263447c6ae99SBarry Smith @*/
26351b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
263647c6ae99SBarry Smith {
263747c6ae99SBarry Smith   PetscFunctionBegin;
2638171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
263947c6ae99SBarry Smith   dm->ctx = ctx;
264047c6ae99SBarry Smith   PetscFunctionReturn(0);
264147c6ae99SBarry Smith }
264247c6ae99SBarry Smith 
264347c6ae99SBarry Smith #undef __FUNCT__
26441b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
264547c6ae99SBarry Smith /*@
26461b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
264747c6ae99SBarry Smith 
264847c6ae99SBarry Smith     Not Collective
264947c6ae99SBarry Smith 
265047c6ae99SBarry Smith     Input Parameter:
265147c6ae99SBarry Smith .   dm - the DM object
265247c6ae99SBarry Smith 
265347c6ae99SBarry Smith     Output Parameter:
265447c6ae99SBarry Smith .   ctx - the user context
265547c6ae99SBarry Smith 
265647c6ae99SBarry Smith     Level: intermediate
265747c6ae99SBarry Smith 
2658e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
265947c6ae99SBarry Smith 
266047c6ae99SBarry Smith @*/
26611b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
266247c6ae99SBarry Smith {
266347c6ae99SBarry Smith   PetscFunctionBegin;
2664171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26651b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
266647c6ae99SBarry Smith   PetscFunctionReturn(0);
266747c6ae99SBarry Smith }
266847c6ae99SBarry Smith 
266947c6ae99SBarry Smith #undef __FUNCT__
267008da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
267108da532bSDmitry Karpeev /*@C
2672df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
267308da532bSDmitry Karpeev 
267408da532bSDmitry Karpeev     Logically Collective on DM
267508da532bSDmitry Karpeev 
267608da532bSDmitry Karpeev     Input Parameter:
267708da532bSDmitry Karpeev +   dm - the DM object
26780298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
267908da532bSDmitry Karpeev 
268008da532bSDmitry Karpeev     Level: intermediate
268108da532bSDmitry Karpeev 
2682835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
268308da532bSDmitry Karpeev          DMSetJacobian()
268408da532bSDmitry Karpeev 
268508da532bSDmitry Karpeev @*/
268608da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
268708da532bSDmitry Karpeev {
268808da532bSDmitry Karpeev   PetscFunctionBegin;
268908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
269008da532bSDmitry Karpeev   PetscFunctionReturn(0);
269108da532bSDmitry Karpeev }
269208da532bSDmitry Karpeev 
269308da532bSDmitry Karpeev #undef __FUNCT__
269408da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
269508da532bSDmitry Karpeev /*@
269608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
269708da532bSDmitry Karpeev 
269808da532bSDmitry Karpeev     Not Collective
269908da532bSDmitry Karpeev 
270008da532bSDmitry Karpeev     Input Parameter:
270108da532bSDmitry Karpeev .   dm - the DM object to destroy
270208da532bSDmitry Karpeev 
270308da532bSDmitry Karpeev     Output Parameter:
270408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
270508da532bSDmitry Karpeev 
270608da532bSDmitry Karpeev     Level: developer
270708da532bSDmitry Karpeev 
270874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
270908da532bSDmitry Karpeev 
271008da532bSDmitry Karpeev @*/
271108da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
271208da532bSDmitry Karpeev {
271308da532bSDmitry Karpeev   PetscFunctionBegin;
271408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
271508da532bSDmitry Karpeev   PetscFunctionReturn(0);
271608da532bSDmitry Karpeev }
271708da532bSDmitry Karpeev 
271808da532bSDmitry Karpeev #undef __FUNCT__
271908da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
272008da532bSDmitry Karpeev /*@C
272108da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
272208da532bSDmitry Karpeev 
272308da532bSDmitry Karpeev     Logically Collective on DM
272408da532bSDmitry Karpeev 
272508da532bSDmitry Karpeev     Input Parameters:
2726907376e6SBarry Smith .   dm - the DM object
272708da532bSDmitry Karpeev 
272808da532bSDmitry Karpeev     Output parameters:
272908da532bSDmitry Karpeev +   xl - lower bound
273008da532bSDmitry Karpeev -   xu - upper bound
273108da532bSDmitry Karpeev 
2732907376e6SBarry Smith     Level: advanced
2733907376e6SBarry Smith 
2734907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
273508da532bSDmitry Karpeev 
273674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
273708da532bSDmitry Karpeev 
273808da532bSDmitry Karpeev @*/
273908da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
274008da532bSDmitry Karpeev {
274108da532bSDmitry Karpeev   PetscErrorCode ierr;
27425fd66863SKarl Rupp 
274308da532bSDmitry Karpeev   PetscFunctionBegin;
274408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
274508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
274608da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
274708da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
27488865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
274908da532bSDmitry Karpeev   PetscFunctionReturn(0);
275008da532bSDmitry Karpeev }
275108da532bSDmitry Karpeev 
275208da532bSDmitry Karpeev #undef __FUNCT__
2753b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2754b0ae01b7SPeter Brune /*@
2755b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2756b0ae01b7SPeter Brune 
2757b0ae01b7SPeter Brune     Not Collective
2758b0ae01b7SPeter Brune 
2759b0ae01b7SPeter Brune     Input Parameter:
2760b0ae01b7SPeter Brune .   dm - the DM object
2761b0ae01b7SPeter Brune 
2762b0ae01b7SPeter Brune     Output Parameter:
2763b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2764b0ae01b7SPeter Brune 
2765b0ae01b7SPeter Brune     Level: developer
2766b0ae01b7SPeter Brune 
2767b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2768b0ae01b7SPeter Brune 
2769b0ae01b7SPeter Brune @*/
2770b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2771b0ae01b7SPeter Brune {
2772b0ae01b7SPeter Brune   PetscFunctionBegin;
2773b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2774b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2775b0ae01b7SPeter Brune }
2776b0ae01b7SPeter Brune 
2777b0ae01b7SPeter Brune #undef  __FUNCT__
277808da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2779748fac09SDmitry Karpeev /*@C
278008da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
278108da532bSDmitry Karpeev 
278208da532bSDmitry Karpeev     Collective on DM
278308da532bSDmitry Karpeev 
278408da532bSDmitry Karpeev     Input Parameter:
278508da532bSDmitry Karpeev +   dm - the DM object
27860298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
278708da532bSDmitry Karpeev 
278808da532bSDmitry Karpeev     Level: developer
278908da532bSDmitry Karpeev 
279074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
279108da532bSDmitry Karpeev 
279208da532bSDmitry Karpeev @*/
279308da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
279408da532bSDmitry Karpeev {
279508da532bSDmitry Karpeev   PetscErrorCode ierr;
27965fd66863SKarl Rupp 
279708da532bSDmitry Karpeev   PetscFunctionBegin;
279808da532bSDmitry Karpeev   if (x) {
279908da532bSDmitry Karpeev     if (!dm->x) {
280008da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
280108da532bSDmitry Karpeev     }
280208da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
28038865f1eaSKarl Rupp   } else if (dm->x) {
280408da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
280508da532bSDmitry Karpeev   }
280608da532bSDmitry Karpeev   PetscFunctionReturn(0);
280708da532bSDmitry Karpeev }
280808da532bSDmitry Karpeev 
28090298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2810264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2811264ace61SBarry Smith 
2812264ace61SBarry Smith #undef __FUNCT__
2813264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2814264ace61SBarry Smith /*@C
2815264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2816264ace61SBarry Smith 
2817264ace61SBarry Smith   Collective on DM
2818264ace61SBarry Smith 
2819264ace61SBarry Smith   Input Parameters:
2820264ace61SBarry Smith + dm     - The DM object
2821264ace61SBarry Smith - method - The name of the DM type
2822264ace61SBarry Smith 
2823264ace61SBarry Smith   Options Database Key:
2824264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2825264ace61SBarry Smith 
2826264ace61SBarry Smith   Notes:
2827e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2828264ace61SBarry Smith 
2829264ace61SBarry Smith   Level: intermediate
2830264ace61SBarry Smith 
2831264ace61SBarry Smith .keywords: DM, set, type
2832264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2833264ace61SBarry Smith @*/
283419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2835264ace61SBarry Smith {
2836264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2837264ace61SBarry Smith   PetscBool      match;
2838264ace61SBarry Smith   PetscErrorCode ierr;
2839264ace61SBarry Smith 
2840264ace61SBarry Smith   PetscFunctionBegin;
2841264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2842251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2843264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2844264ace61SBarry Smith 
28450f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
28461c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2847ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2848264ace61SBarry Smith 
2849264ace61SBarry Smith   if (dm->ops->destroy) {
2850264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
28510298fd71SBarry Smith     dm->ops->destroy = NULL;
2852264ace61SBarry Smith   }
2853264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2854264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2855264ace61SBarry Smith   PetscFunctionReturn(0);
2856264ace61SBarry Smith }
2857264ace61SBarry Smith 
2858264ace61SBarry Smith #undef __FUNCT__
2859264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2860264ace61SBarry Smith /*@C
2861264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2862264ace61SBarry Smith 
2863264ace61SBarry Smith   Not Collective
2864264ace61SBarry Smith 
2865264ace61SBarry Smith   Input Parameter:
2866264ace61SBarry Smith . dm  - The DM
2867264ace61SBarry Smith 
2868264ace61SBarry Smith   Output Parameter:
2869264ace61SBarry Smith . type - The DM type name
2870264ace61SBarry Smith 
2871264ace61SBarry Smith   Level: intermediate
2872264ace61SBarry Smith 
2873264ace61SBarry Smith .keywords: DM, get, type, name
2874264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2875264ace61SBarry Smith @*/
287619fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2877264ace61SBarry Smith {
2878264ace61SBarry Smith   PetscErrorCode ierr;
2879264ace61SBarry Smith 
2880264ace61SBarry Smith   PetscFunctionBegin;
2881264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2882c959eef4SJed Brown   PetscValidPointer(type,2);
2883607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2884264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2885264ace61SBarry Smith   PetscFunctionReturn(0);
2886264ace61SBarry Smith }
2887264ace61SBarry Smith 
288867a56275SMatthew G Knepley #undef __FUNCT__
288967a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
289067a56275SMatthew G Knepley /*@C
289167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
289267a56275SMatthew G Knepley 
289367a56275SMatthew G Knepley   Collective on DM
289467a56275SMatthew G Knepley 
289567a56275SMatthew G Knepley   Input Parameters:
289667a56275SMatthew G Knepley + dm - the DM
289767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
289867a56275SMatthew G Knepley 
289967a56275SMatthew G Knepley   Output Parameter:
290067a56275SMatthew G Knepley . M - pointer to new DM
290167a56275SMatthew G Knepley 
290267a56275SMatthew G Knepley   Notes:
290367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
290467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
290567a56275SMatthew G Knepley   of the input DM.
290667a56275SMatthew G Knepley 
290767a56275SMatthew G Knepley   Level: intermediate
290867a56275SMatthew G Knepley 
290967a56275SMatthew G Knepley .seealso: DMCreate()
291067a56275SMatthew G Knepley @*/
291119fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
291267a56275SMatthew G Knepley {
291367a56275SMatthew G Knepley   DM             B;
291467a56275SMatthew G Knepley   char           convname[256];
291567a56275SMatthew G Knepley   PetscBool      sametype, issame;
291667a56275SMatthew G Knepley   PetscErrorCode ierr;
291767a56275SMatthew G Knepley 
291867a56275SMatthew G Knepley   PetscFunctionBegin;
291967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
292067a56275SMatthew G Knepley   PetscValidType(dm,1);
292167a56275SMatthew G Knepley   PetscValidPointer(M,3);
2922251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
292367a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
292467a56275SMatthew G Knepley   {
29250298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
292667a56275SMatthew G Knepley 
292767a56275SMatthew G Knepley     /*
292867a56275SMatthew G Knepley        Order of precedence:
292967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
293067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
293167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
293267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
293367a56275SMatthew G Knepley        5) Use a really basic converter.
293467a56275SMatthew G Knepley     */
293567a56275SMatthew G Knepley 
293667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
293767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
293867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
293967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
294067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
294167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29420005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
294367a56275SMatthew G Knepley     if (conv) goto foundconv;
294467a56275SMatthew G Knepley 
294567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
294682f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
294767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
294867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
294967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
295067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
295167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
295267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29530005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
295467a56275SMatthew G Knepley     if (conv) {
2955fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
295667a56275SMatthew G Knepley       goto foundconv;
295767a56275SMatthew G Knepley     }
295867a56275SMatthew G Knepley 
295967a56275SMatthew G Knepley #if 0
296067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
296167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2962fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
296367a56275SMatthew G Knepley     if (conv) goto foundconv;
296467a56275SMatthew G Knepley 
296567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
296667a56275SMatthew G Knepley     if (dm->ops->convert) {
296767a56275SMatthew G Knepley       conv = dm->ops->convert;
296867a56275SMatthew G Knepley     }
296967a56275SMatthew G Knepley     if (conv) goto foundconv;
297067a56275SMatthew G Knepley #endif
297167a56275SMatthew G Knepley 
297267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
297382f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
297467a56275SMatthew G Knepley 
297567a56275SMatthew G Knepley foundconv:
297667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
297767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
297867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
297967a56275SMatthew G Knepley   }
298067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
298167a56275SMatthew G Knepley   PetscFunctionReturn(0);
298267a56275SMatthew G Knepley }
2983264ace61SBarry Smith 
2984264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2985264ace61SBarry Smith 
2986264ace61SBarry Smith #undef __FUNCT__
2987264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2988264ace61SBarry Smith /*@C
29891c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
29901c84c290SBarry Smith 
29911c84c290SBarry Smith   Not Collective
29921c84c290SBarry Smith 
29931c84c290SBarry Smith   Input Parameters:
29941c84c290SBarry Smith + name        - The name of a new user-defined creation routine
29951c84c290SBarry Smith - create_func - The creation routine itself
29961c84c290SBarry Smith 
29971c84c290SBarry Smith   Notes:
29981c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
29991c84c290SBarry Smith 
30001c84c290SBarry Smith 
30011c84c290SBarry Smith   Sample usage:
30021c84c290SBarry Smith .vb
3003bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
30041c84c290SBarry Smith .ve
30051c84c290SBarry Smith 
30061c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
30071c84c290SBarry Smith .vb
30081c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
30091c84c290SBarry Smith     DMSetType(DM,"my_da");
30101c84c290SBarry Smith .ve
30111c84c290SBarry Smith    or at runtime via the option
30121c84c290SBarry Smith .vb
30131c84c290SBarry Smith     -da_type my_da
30141c84c290SBarry Smith .ve
3015264ace61SBarry Smith 
3016264ace61SBarry Smith   Level: advanced
30171c84c290SBarry Smith 
30181c84c290SBarry Smith .keywords: DM, register
3019bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
30201c84c290SBarry Smith 
3021264ace61SBarry Smith @*/
3022bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3023264ace61SBarry Smith {
3024264ace61SBarry Smith   PetscErrorCode ierr;
3025264ace61SBarry Smith 
3026264ace61SBarry Smith   PetscFunctionBegin;
3027a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3028264ace61SBarry Smith   PetscFunctionReturn(0);
3029264ace61SBarry Smith }
3030264ace61SBarry Smith 
3031b859378eSBarry Smith #undef __FUNCT__
3032b859378eSBarry Smith #define __FUNCT__ "DMLoad"
3033b859378eSBarry Smith /*@C
303455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3035b859378eSBarry Smith 
3036b859378eSBarry Smith   Collective on PetscViewer
3037b859378eSBarry Smith 
3038b859378eSBarry Smith   Input Parameters:
3039b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3040b859378eSBarry Smith            some related function before a call to DMLoad().
3041b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3042b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3043b859378eSBarry Smith 
3044b859378eSBarry Smith    Level: intermediate
3045b859378eSBarry Smith 
3046b859378eSBarry Smith   Notes:
304755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3048b859378eSBarry Smith 
3049b859378eSBarry Smith   Notes for advanced users:
3050b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3051b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3052b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3053b859378eSBarry Smith   format is
3054b859378eSBarry Smith .vb
3055b859378eSBarry Smith      has not yet been determined
3056b859378eSBarry Smith .ve
3057b859378eSBarry Smith 
3058b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3059b859378eSBarry Smith @*/
3060b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3061b859378eSBarry Smith {
30629331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3063b859378eSBarry Smith   PetscErrorCode ierr;
3064b859378eSBarry Smith 
3065b859378eSBarry Smith   PetscFunctionBegin;
3066b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3067b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
306832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
30699331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
30709331c7a4SMatthew G. Knepley   if (isbinary) {
30719331c7a4SMatthew G. Knepley     PetscInt classid;
30729331c7a4SMatthew G. Knepley     char     type[256];
3073b859378eSBarry Smith 
3074060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
30759200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3076060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
307732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
30789331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30799331c7a4SMatthew G. Knepley   } else if (ishdf5) {
30809331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30819331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3082b859378eSBarry Smith   PetscFunctionReturn(0);
3083b859378eSBarry Smith }
3084b859378eSBarry Smith 
30857da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30867da65231SMatthew G Knepley 
30877da65231SMatthew G Knepley #undef __FUNCT__
30887da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3089a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3090a6dfd86eSKarl Rupp {
30911d47ebbbSSatish Balay   PetscInt       f;
30921b30c384SMatthew G Knepley   PetscErrorCode ierr;
30931b30c384SMatthew G Knepley 
30947da65231SMatthew G Knepley   PetscFunctionBegin;
309574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30961d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
309757622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
30987da65231SMatthew G Knepley   }
30997da65231SMatthew G Knepley   PetscFunctionReturn(0);
31007da65231SMatthew G Knepley }
31017da65231SMatthew G Knepley 
31027da65231SMatthew G Knepley #undef __FUNCT__
31037da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3104a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3105a6dfd86eSKarl Rupp {
31061b30c384SMatthew G Knepley   PetscInt       f, g;
31077da65231SMatthew G Knepley   PetscErrorCode ierr;
31087da65231SMatthew G Knepley 
31097da65231SMatthew G Knepley   PetscFunctionBegin;
311074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
31111d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
311274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
31131d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3114e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
31157da65231SMatthew G Knepley     }
311674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
31177da65231SMatthew G Knepley   }
31187da65231SMatthew G Knepley   PetscFunctionReturn(0);
31197da65231SMatthew G Knepley }
3120e7c4fc90SDmitry Karpeev 
3121970e74d5SMatthew G Knepley #undef __FUNCT__
3122e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
31236113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3124e759306cSMatthew G. Knepley {
3125e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3126e759306cSMatthew G. Knepley   PetscInt       p;
3127e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3128e759306cSMatthew G. Knepley 
3129e759306cSMatthew G. Knepley   PetscFunctionBegin;
3130e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3131e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3132e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3133e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3134e759306cSMatthew G. Knepley     if (p == rank) {
3135e759306cSMatthew G. Knepley       Vec x;
3136e759306cSMatthew G. Knepley 
3137e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3138e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
31396113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3140e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3141e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3142e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3143e759306cSMatthew G. Knepley     }
3144e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3145e759306cSMatthew G. Knepley   }
3146e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3147e759306cSMatthew G. Knepley }
3148e759306cSMatthew G. Knepley 
3149e759306cSMatthew G. Knepley #undef __FUNCT__
315088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
315188ed4aceSMatthew G Knepley /*@
315288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
315388ed4aceSMatthew G Knepley 
315488ed4aceSMatthew G Knepley   Input Parameter:
315588ed4aceSMatthew G Knepley . dm - The DM
315688ed4aceSMatthew G Knepley 
315788ed4aceSMatthew G Knepley   Output Parameter:
315888ed4aceSMatthew G Knepley . section - The PetscSection
315988ed4aceSMatthew G Knepley 
316088ed4aceSMatthew G Knepley   Level: intermediate
316188ed4aceSMatthew G Knepley 
316288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
316388ed4aceSMatthew G Knepley 
316488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
316588ed4aceSMatthew G Knepley @*/
31660adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
31670adebc6cSBarry Smith {
3168fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3169fd59a867SMatthew G. Knepley 
317088ed4aceSMatthew G Knepley   PetscFunctionBegin;
317188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
317288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
31732f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
31742f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3175685405a1SBarry Smith     ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);
31762f0f8703SMatthew G. Knepley   }
317788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
317888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
317988ed4aceSMatthew G Knepley }
318088ed4aceSMatthew G Knepley 
318188ed4aceSMatthew G Knepley #undef __FUNCT__
318288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
318388ed4aceSMatthew G Knepley /*@
318488ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
318588ed4aceSMatthew G Knepley 
318688ed4aceSMatthew G Knepley   Input Parameters:
318788ed4aceSMatthew G Knepley + dm - The DM
318888ed4aceSMatthew G Knepley - section - The PetscSection
318988ed4aceSMatthew G Knepley 
319088ed4aceSMatthew G Knepley   Level: intermediate
319188ed4aceSMatthew G Knepley 
319288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
319388ed4aceSMatthew G Knepley 
319488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
319588ed4aceSMatthew G Knepley @*/
31960adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
31970adebc6cSBarry Smith {
3198c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3199af122d2aSMatthew G Knepley   PetscInt       f;
320088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
320188ed4aceSMatthew G Knepley 
320288ed4aceSMatthew G Knepley   PetscFunctionBegin;
320388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3204c473ab19SMatthew G. Knepley   if (section) {
32051d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
32061d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3207c473ab19SMatthew G. Knepley   }
320888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
320988ed4aceSMatthew G Knepley   dm->defaultSection = section;
3210c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3211af122d2aSMatthew G Knepley   if (numFields) {
3212af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3213af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
32140f21e855SMatthew G. Knepley       PetscObject disc;
3215af122d2aSMatthew G Knepley       const char *name;
3216af122d2aSMatthew G Knepley 
3217af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
32180f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
32190f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3220af122d2aSMatthew G Knepley     }
3221af122d2aSMatthew G Knepley   }
32221d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
32231d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
322488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
322588ed4aceSMatthew G Knepley }
322688ed4aceSMatthew G Knepley 
322788ed4aceSMatthew G Knepley #undef __FUNCT__
32289435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
32299435951eSToby Isaac /*@
32309435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
32319435951eSToby Isaac 
3232e228b242SToby Isaac   not collective
3233e228b242SToby Isaac 
32349435951eSToby Isaac   Input Parameter:
32359435951eSToby Isaac . dm - The DM
32369435951eSToby Isaac 
32379435951eSToby Isaac   Output Parameter:
32389435951eSToby 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.
32399435951eSToby 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.
32409435951eSToby Isaac 
32419435951eSToby Isaac   Level: advanced
32429435951eSToby Isaac 
32439435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
32449435951eSToby Isaac 
32459435951eSToby Isaac .seealso: DMSetDefaultConstraints()
32469435951eSToby Isaac @*/
32479435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
32489435951eSToby Isaac {
32499435951eSToby Isaac   PetscErrorCode ierr;
32509435951eSToby Isaac 
32519435951eSToby Isaac   PetscFunctionBegin;
32529435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32539435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
325445a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
325545a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
32569435951eSToby Isaac   PetscFunctionReturn(0);
32579435951eSToby Isaac }
32589435951eSToby Isaac 
32599435951eSToby Isaac #undef __FUNCT__
32609435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
32619435951eSToby Isaac /*@
32629435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
32639435951eSToby Isaac 
32649435951eSToby 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().
32659435951eSToby Isaac 
32669435951eSToby 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.
32679435951eSToby Isaac 
3268e228b242SToby Isaac   collective on dm
3269e228b242SToby Isaac 
32709435951eSToby Isaac   Input Parameters:
32719435951eSToby Isaac + dm - The DM
3272e228b242SToby 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).
3273e228b242SToby 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).
32749435951eSToby Isaac 
32759435951eSToby Isaac   Level: advanced
32769435951eSToby Isaac 
32779435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32789435951eSToby Isaac 
32799435951eSToby Isaac .seealso: DMGetDefaultConstraints()
32809435951eSToby Isaac @*/
32819435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
32829435951eSToby Isaac {
3283e228b242SToby Isaac   PetscMPIInt result;
32849435951eSToby Isaac   PetscErrorCode ierr;
32859435951eSToby Isaac 
32869435951eSToby Isaac   PetscFunctionBegin;
32879435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3288e228b242SToby Isaac   if (section) {
3289e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3290e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3291e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3292e228b242SToby Isaac   }
3293e228b242SToby Isaac   if (mat) {
3294e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3295e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3296e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3297e228b242SToby Isaac   }
32989435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
32999435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
33009435951eSToby Isaac   dm->defaultConstraintSection = section;
33019435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
33029435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
33039435951eSToby Isaac   dm->defaultConstraintMat = mat;
33049435951eSToby Isaac   PetscFunctionReturn(0);
33059435951eSToby Isaac }
33069435951eSToby Isaac 
3307f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
33089435951eSToby Isaac #undef __FUNCT__
3309284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3310507e4973SMatthew G. Knepley /*
3311507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3312507e4973SMatthew G. Knepley 
3313507e4973SMatthew G. Knepley   Input Parameters:
3314507e4973SMatthew G. Knepley + dm - The DM
3315507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3316507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3317507e4973SMatthew G. Knepley 
3318507e4973SMatthew G. Knepley   Level: intermediate
3319507e4973SMatthew G. Knepley 
3320507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3321507e4973SMatthew G. Knepley */
3322f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3323507e4973SMatthew G. Knepley {
3324507e4973SMatthew G. Knepley   MPI_Comm        comm;
3325507e4973SMatthew G. Knepley   PetscLayout     layout;
3326507e4973SMatthew G. Knepley   const PetscInt *ranges;
3327507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3328507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3329507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3330507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3331507e4973SMatthew G. Knepley 
3332507e4973SMatthew G. Knepley   PetscFunctionBegin;
3333507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3334507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3335507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3336507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3337507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3338507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3339507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3340507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3341507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3342507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3343507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3344507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3345f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3346507e4973SMatthew G. Knepley 
3347507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3348507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3349507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3350507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3351507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3352507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3353507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3354507e4973SMatthew 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;}
3355507e4973SMatthew 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;}
3356507e4973SMatthew G. Knepley     if (gdof < 0) {
3357507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3358507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3359507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3360507e4973SMatthew G. Knepley 
3361507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3362507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3363507e4973SMatthew 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;}
3364507e4973SMatthew G. Knepley       }
3365507e4973SMatthew G. Knepley     }
3366507e4973SMatthew G. Knepley   }
3367507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3368507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3369b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3370507e4973SMatthew G. Knepley   if (!gvalid) {
3371507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3372507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3373507e4973SMatthew G. Knepley   }
3374507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3375507e4973SMatthew G. Knepley }
3376f741bcd2SMatthew G. Knepley #endif
3377507e4973SMatthew G. Knepley 
3378507e4973SMatthew G. Knepley #undef __FUNCT__
337988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
338088ed4aceSMatthew G Knepley /*@
338188ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
338288ed4aceSMatthew G Knepley 
33838b1ab98fSJed Brown   Collective on DM
33848b1ab98fSJed Brown 
338588ed4aceSMatthew G Knepley   Input Parameter:
338688ed4aceSMatthew G Knepley . dm - The DM
338788ed4aceSMatthew G Knepley 
338888ed4aceSMatthew G Knepley   Output Parameter:
338988ed4aceSMatthew G Knepley . section - The PetscSection
339088ed4aceSMatthew G Knepley 
339188ed4aceSMatthew G Knepley   Level: intermediate
339288ed4aceSMatthew G Knepley 
339388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
339488ed4aceSMatthew G Knepley 
339588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
339688ed4aceSMatthew G Knepley @*/
33970adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
33980adebc6cSBarry Smith {
339988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
340088ed4aceSMatthew G Knepley 
340188ed4aceSMatthew G Knepley   PetscFunctionBegin;
340288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
340388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
340488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3405fd59a867SMatthew G. Knepley     PetscSection s;
3406fd59a867SMatthew G. Knepley 
3407fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3408fd59a867SMatthew 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");
3409fd59a867SMatthew 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");
341015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3411cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3412ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3413685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
341488ed4aceSMatthew G Knepley   }
341588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
341688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
341788ed4aceSMatthew G Knepley }
341888ed4aceSMatthew G Knepley 
341988ed4aceSMatthew G Knepley #undef __FUNCT__
3420b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3421b21d0597SMatthew G Knepley /*@
3422b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3423b21d0597SMatthew G Knepley 
3424b21d0597SMatthew G Knepley   Input Parameters:
3425b21d0597SMatthew G Knepley + dm - The DM
34265080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3427b21d0597SMatthew G Knepley 
3428b21d0597SMatthew G Knepley   Level: intermediate
3429b21d0597SMatthew G Knepley 
3430b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3431b21d0597SMatthew G Knepley 
3432b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3433b21d0597SMatthew G Knepley @*/
34340adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
34350adebc6cSBarry Smith {
3436b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3437b21d0597SMatthew G Knepley 
3438b21d0597SMatthew G Knepley   PetscFunctionBegin;
3439b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34405080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34411d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3442b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3443b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3444507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3445f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3446507e4973SMatthew G. Knepley #endif
3447b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3448b21d0597SMatthew G Knepley }
3449b21d0597SMatthew G Knepley 
3450b21d0597SMatthew G Knepley #undef __FUNCT__
345188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
345288ed4aceSMatthew G Knepley /*@
345388ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
345488ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
345588ed4aceSMatthew G Knepley 
345688ed4aceSMatthew G Knepley   Input Parameter:
345788ed4aceSMatthew G Knepley . dm - The DM
345888ed4aceSMatthew G Knepley 
345988ed4aceSMatthew G Knepley   Output Parameter:
346088ed4aceSMatthew G Knepley . sf - The PetscSF
346188ed4aceSMatthew G Knepley 
346288ed4aceSMatthew G Knepley   Level: intermediate
346388ed4aceSMatthew G Knepley 
346488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
346588ed4aceSMatthew G Knepley 
346688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
346788ed4aceSMatthew G Knepley @*/
34680adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
34690adebc6cSBarry Smith {
347088ed4aceSMatthew G Knepley   PetscInt       nroots;
347188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
347288ed4aceSMatthew G Knepley 
347388ed4aceSMatthew G Knepley   PetscFunctionBegin;
347488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
347588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34760298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
347788ed4aceSMatthew G Knepley   if (nroots < 0) {
347888ed4aceSMatthew G Knepley     PetscSection section, gSection;
347988ed4aceSMatthew G Knepley 
348088ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
348131ea6d37SMatthew G Knepley     if (section) {
348288ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
348388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
348431ea6d37SMatthew G Knepley     } else {
34850298fd71SBarry Smith       *sf = NULL;
348631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
348731ea6d37SMatthew G Knepley     }
348888ed4aceSMatthew G Knepley   }
348988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
349088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
349188ed4aceSMatthew G Knepley }
349288ed4aceSMatthew G Knepley 
349388ed4aceSMatthew G Knepley #undef __FUNCT__
349488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
349588ed4aceSMatthew G Knepley /*@
349688ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
349788ed4aceSMatthew G Knepley 
349888ed4aceSMatthew G Knepley   Input Parameters:
349988ed4aceSMatthew G Knepley + dm - The DM
350088ed4aceSMatthew G Knepley - sf - The PetscSF
350188ed4aceSMatthew G Knepley 
350288ed4aceSMatthew G Knepley   Level: intermediate
350388ed4aceSMatthew G Knepley 
350488ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
350588ed4aceSMatthew G Knepley 
350688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
350788ed4aceSMatthew G Knepley @*/
35080adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
35090adebc6cSBarry Smith {
351088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
351188ed4aceSMatthew G Knepley 
351288ed4aceSMatthew G Knepley   PetscFunctionBegin;
351388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
351488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
351588ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
351688ed4aceSMatthew G Knepley   dm->defaultSF = sf;
351788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
351888ed4aceSMatthew G Knepley }
351988ed4aceSMatthew G Knepley 
352088ed4aceSMatthew G Knepley #undef __FUNCT__
352188ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
352288ed4aceSMatthew G Knepley /*@C
352388ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
352488ed4aceSMatthew G Knepley   describing the data layout.
352588ed4aceSMatthew G Knepley 
352688ed4aceSMatthew G Knepley   Input Parameters:
352788ed4aceSMatthew G Knepley + dm - The DM
352888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
352988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
353088ed4aceSMatthew G Knepley 
353188ed4aceSMatthew G Knepley   Level: intermediate
353288ed4aceSMatthew G Knepley 
353388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
353488ed4aceSMatthew G Knepley @*/
353588ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
353688ed4aceSMatthew G Knepley {
353782f516ccSBarry Smith   MPI_Comm       comm;
353888ed4aceSMatthew G Knepley   PetscLayout    layout;
353988ed4aceSMatthew G Knepley   const PetscInt *ranges;
354088ed4aceSMatthew G Knepley   PetscInt       *local;
354188ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3542ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
354388ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
354488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
354588ed4aceSMatthew G Knepley 
354688ed4aceSMatthew G Knepley   PetscFunctionBegin;
354782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
354888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
354988ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
355088ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
355188ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
355288ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
355388ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
355488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
355588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
355688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
355788ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3558ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35596636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
356088ed4aceSMatthew G Knepley 
35616636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35626636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3563235fbf56SMatthew 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));
35646636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
356588ed4aceSMatthew G Knepley   }
3566785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3567785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
356888ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
35691f588964SMatthew G Knepley     const PetscInt *cind;
35706636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
357188ed4aceSMatthew G Knepley 
357288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
357388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
357488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
357588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
357688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35776636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
357888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35796636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35806636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35816636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3582057b4bcdSMatthew 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);
35836636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35846636e97aSMatthew G Knepley     }
358588ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
358688ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
358788ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
358888ed4aceSMatthew G Knepley     }
358988ed4aceSMatthew G Knepley     if (gdof < 0) {
35906636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
359188ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
359288ed4aceSMatthew G Knepley 
359305376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
359431d3f06eSJed Brown         if (r < 0) r = -(r+2);
359505376888SMatthew 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);
359688ed4aceSMatthew G Knepley         remote[l].rank  = r;
359788ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
359888ed4aceSMatthew G Knepley       }
359988ed4aceSMatthew G Knepley     } else {
36006636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
360188ed4aceSMatthew G Knepley         remote[l].rank  = rank;
360288ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
360388ed4aceSMatthew G Knepley       }
360488ed4aceSMatthew G Knepley     }
360588ed4aceSMatthew G Knepley   }
36066636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
360788ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
360888ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
360988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
361088ed4aceSMatthew G Knepley }
3611af122d2aSMatthew G Knepley 
3612af122d2aSMatthew G Knepley #undef __FUNCT__
3613b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3614b21d0597SMatthew G Knepley /*@
3615b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3616b21d0597SMatthew G Knepley 
3617b21d0597SMatthew G Knepley   Input Parameter:
3618b21d0597SMatthew G Knepley . dm - The DM
3619b21d0597SMatthew G Knepley 
3620b21d0597SMatthew G Knepley   Output Parameter:
3621b21d0597SMatthew G Knepley . sf - The PetscSF
3622b21d0597SMatthew G Knepley 
3623b21d0597SMatthew G Knepley   Level: intermediate
3624b21d0597SMatthew G Knepley 
3625b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3626b21d0597SMatthew G Knepley 
3627057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3628b21d0597SMatthew G Knepley @*/
36290adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
36300adebc6cSBarry Smith {
3631b21d0597SMatthew G Knepley   PetscFunctionBegin;
3632b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3633b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3634b21d0597SMatthew G Knepley   *sf = dm->sf;
3635b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3636b21d0597SMatthew G Knepley }
3637b21d0597SMatthew G Knepley 
3638b21d0597SMatthew G Knepley #undef __FUNCT__
3639057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3640057b4bcdSMatthew G Knepley /*@
3641057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3642057b4bcdSMatthew G Knepley 
3643057b4bcdSMatthew G Knepley   Input Parameters:
3644057b4bcdSMatthew G Knepley + dm - The DM
3645057b4bcdSMatthew G Knepley - sf - The PetscSF
3646057b4bcdSMatthew G Knepley 
3647057b4bcdSMatthew G Knepley   Level: intermediate
3648057b4bcdSMatthew G Knepley 
3649057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3650057b4bcdSMatthew G Knepley @*/
36510adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
36520adebc6cSBarry Smith {
3653057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3654057b4bcdSMatthew G Knepley 
3655057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3656057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3657057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3658057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3659057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3660057b4bcdSMatthew G Knepley   dm->sf = sf;
3661057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3662057b4bcdSMatthew G Knepley }
3663057b4bcdSMatthew G Knepley 
3664057b4bcdSMatthew G Knepley #undef __FUNCT__
36652764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
36662764a2aaSMatthew G. Knepley /*@
36672764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
36682764a2aaSMatthew G. Knepley 
36692764a2aaSMatthew G. Knepley   Input Parameter:
36702764a2aaSMatthew G. Knepley . dm - The DM
36712764a2aaSMatthew G. Knepley 
36722764a2aaSMatthew G. Knepley   Output Parameter:
36732764a2aaSMatthew G. Knepley . prob - The PetscDS
36742764a2aaSMatthew G. Knepley 
36752764a2aaSMatthew G. Knepley   Level: developer
36762764a2aaSMatthew G. Knepley 
36772764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36782764a2aaSMatthew G. Knepley @*/
36792764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3680af122d2aSMatthew G Knepley {
3681af122d2aSMatthew G Knepley   PetscFunctionBegin;
3682af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36830f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36840f21e855SMatthew G. Knepley   *prob = dm->prob;
36850f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36860f21e855SMatthew G. Knepley }
36870f21e855SMatthew G. Knepley 
36880f21e855SMatthew G. Knepley #undef __FUNCT__
36892764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
36902764a2aaSMatthew G. Knepley /*@
36912764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36922764a2aaSMatthew G. Knepley 
36932764a2aaSMatthew G. Knepley   Input Parameters:
36942764a2aaSMatthew G. Knepley + dm - The DM
36952764a2aaSMatthew G. Knepley - prob - The PetscDS
36962764a2aaSMatthew G. Knepley 
36972764a2aaSMatthew G. Knepley   Level: developer
36982764a2aaSMatthew G. Knepley 
36992764a2aaSMatthew G. Knepley .seealso: DMGetDS()
37002764a2aaSMatthew G. Knepley @*/
37012764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
37020f21e855SMatthew G. Knepley {
37030f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37040f21e855SMatthew G. Knepley 
37050f21e855SMatthew G. Knepley   PetscFunctionBegin;
37060f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37072764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
37082764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
37090f21e855SMatthew G. Knepley   dm->prob = prob;
37100f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
37110f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
37120f21e855SMatthew G. Knepley }
37130f21e855SMatthew G. Knepley 
37140f21e855SMatthew G. Knepley #undef __FUNCT__
37150f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
37160f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
37170f21e855SMatthew G. Knepley {
37180f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37190f21e855SMatthew G. Knepley 
37200f21e855SMatthew G. Knepley   PetscFunctionBegin;
37210f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37222764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3723af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3724af122d2aSMatthew G Knepley }
3725af122d2aSMatthew G Knepley 
3726af122d2aSMatthew G Knepley #undef __FUNCT__
3727af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3728af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3729af122d2aSMatthew G Knepley {
37300f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3731af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3732af122d2aSMatthew G Knepley 
3733af122d2aSMatthew G Knepley   PetscFunctionBegin;
3734af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37352764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
37360f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
37370f21e855SMatthew G. Knepley     PetscContainer obj;
37380f21e855SMatthew G. Knepley 
37390f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
37402764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
37410f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3742af122d2aSMatthew G Knepley   }
3743af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3744af122d2aSMatthew G Knepley }
3745af122d2aSMatthew G Knepley 
3746af122d2aSMatthew G Knepley #undef __FUNCT__
3747af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3748c1929be8SMatthew G. Knepley /*@
3749c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3750c1929be8SMatthew G. Knepley 
3751c1929be8SMatthew G. Knepley   Not collective
3752c1929be8SMatthew G. Knepley 
3753c1929be8SMatthew G. Knepley   Input Parameters:
3754c1929be8SMatthew G. Knepley + dm - The DM
3755c1929be8SMatthew G. Knepley - f  - The field number
3756c1929be8SMatthew G. Knepley 
3757c1929be8SMatthew G. Knepley   Output Parameter:
3758c1929be8SMatthew G. Knepley . field - The discretization object
3759c1929be8SMatthew G. Knepley 
3760c1929be8SMatthew G. Knepley   Level: developer
3761c1929be8SMatthew G. Knepley 
3762c1929be8SMatthew G. Knepley .seealso: DMSetField()
3763c1929be8SMatthew G. Knepley @*/
3764af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3765af122d2aSMatthew G Knepley {
37660f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37670f21e855SMatthew G. Knepley 
3768af122d2aSMatthew G Knepley   PetscFunctionBegin;
3769af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37702764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3771decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3772decb47aaSMatthew G. Knepley }
3773decb47aaSMatthew G. Knepley 
3774decb47aaSMatthew G. Knepley #undef __FUNCT__
3775decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3776c1929be8SMatthew G. Knepley /*@
3777c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3778c1929be8SMatthew G. Knepley 
3779c1929be8SMatthew G. Knepley   Logically collective on DM
3780c1929be8SMatthew G. Knepley 
3781c1929be8SMatthew G. Knepley   Input Parameters:
3782c1929be8SMatthew G. Knepley + dm - The DM
3783c1929be8SMatthew G. Knepley . f  - The field number
3784c1929be8SMatthew G. Knepley - field - The discretization object
3785c1929be8SMatthew G. Knepley 
3786c1929be8SMatthew G. Knepley   Level: developer
3787c1929be8SMatthew G. Knepley 
3788c1929be8SMatthew G. Knepley .seealso: DMGetField()
3789c1929be8SMatthew G. Knepley @*/
3790decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3791decb47aaSMatthew G. Knepley {
3792decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3793decb47aaSMatthew G. Knepley 
3794decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3795decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37962764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3797af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3798af122d2aSMatthew G Knepley }
37996636e97aSMatthew G Knepley 
38006636e97aSMatthew G Knepley #undef __FUNCT__
3801b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3802b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3803b64e0483SPeter Brune {
3804b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3805b64e0483SPeter Brune   PetscErrorCode ierr;
3806b64e0483SPeter Brune   Vec coords,ccoords;
38076dbf9973SLawrence Mitchell   Mat inject;
3808b64e0483SPeter Brune   PetscFunctionBegin;
3809b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3810b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3811b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3812b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3813b64e0483SPeter Brune   if (coords && !ccoords) {
3814b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
38156dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
38162adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
38176dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3818b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3819b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3820b64e0483SPeter Brune   }
3821b64e0483SPeter Brune   PetscFunctionReturn(0);
3822b64e0483SPeter Brune }
3823b64e0483SPeter Brune 
3824b64e0483SPeter Brune #undef __FUNCT__
382503dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
382603dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
382703dadc2fSPeter Brune {
382803dadc2fSPeter Brune   DM dm_coord,subdm_coord;
382903dadc2fSPeter Brune   PetscErrorCode ierr;
383003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
383103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
383203dadc2fSPeter Brune   PetscFunctionBegin;
383303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
383403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
383503dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
383603dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
383703dadc2fSPeter Brune   if (coords && !ccoords) {
383803dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
383903dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
384024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
384103dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
384203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384603dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
384703dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
384803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
384903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
385003dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
385103dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
385203dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
385303dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
385403dadc2fSPeter Brune   }
385503dadc2fSPeter Brune   PetscFunctionReturn(0);
385603dadc2fSPeter Brune }
385703dadc2fSPeter Brune 
385803dadc2fSPeter Brune #undef __FUNCT__
3859c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3860c73cfb54SMatthew G. Knepley /*@
3861c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3862c73cfb54SMatthew G. Knepley 
3863c73cfb54SMatthew G. Knepley   Not collective
3864c73cfb54SMatthew G. Knepley 
3865c73cfb54SMatthew G. Knepley   Input Parameter:
3866c73cfb54SMatthew G. Knepley . dm - The DM
3867c73cfb54SMatthew G. Knepley 
3868c73cfb54SMatthew G. Knepley   Output Parameter:
3869c73cfb54SMatthew G. Knepley . dim - The topological dimension
3870c73cfb54SMatthew G. Knepley 
3871c73cfb54SMatthew G. Knepley   Level: beginner
3872c73cfb54SMatthew G. Knepley 
3873c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3874c73cfb54SMatthew G. Knepley @*/
3875c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3876c73cfb54SMatthew G. Knepley {
3877c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3878c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3879c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3880c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3881c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3882c73cfb54SMatthew G. Knepley }
3883c73cfb54SMatthew G. Knepley 
3884c73cfb54SMatthew G. Knepley #undef __FUNCT__
3885c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3886c73cfb54SMatthew G. Knepley /*@
3887c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3888c73cfb54SMatthew G. Knepley 
3889c73cfb54SMatthew G. Knepley   Collective on dm
3890c73cfb54SMatthew G. Knepley 
3891c73cfb54SMatthew G. Knepley   Input Parameters:
3892c73cfb54SMatthew G. Knepley + dm - The DM
3893c73cfb54SMatthew G. Knepley - dim - The topological dimension
3894c73cfb54SMatthew G. Knepley 
3895c73cfb54SMatthew G. Knepley   Level: beginner
3896c73cfb54SMatthew G. Knepley 
3897c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3898c73cfb54SMatthew G. Knepley @*/
3899c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3900c73cfb54SMatthew G. Knepley {
3901c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3902c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3903c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3904c73cfb54SMatthew G. Knepley   dm->dim = dim;
3905c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3906c73cfb54SMatthew G. Knepley }
3907c73cfb54SMatthew G. Knepley 
3908793f3fe5SMatthew G. Knepley #undef __FUNCT__
3909793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3910793f3fe5SMatthew G. Knepley /*@
3911793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3912793f3fe5SMatthew G. Knepley 
3913793f3fe5SMatthew G. Knepley   Collective on DM
3914793f3fe5SMatthew G. Knepley 
3915793f3fe5SMatthew G. Knepley   Input Parameters:
3916793f3fe5SMatthew G. Knepley + dm - the DM
3917793f3fe5SMatthew G. Knepley - dim - the dimension
3918793f3fe5SMatthew G. Knepley 
3919793f3fe5SMatthew G. Knepley   Output Parameters:
3920793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3921793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3922793f3fe5SMatthew G. Knepley 
3923793f3fe5SMatthew G. Knepley   Note:
3924793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3925793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3926793f3fe5SMatthew G. Knepley   then the interval is empty.
3927793f3fe5SMatthew G. Knepley 
3928793f3fe5SMatthew G. Knepley   Level: intermediate
3929793f3fe5SMatthew G. Knepley 
3930793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3931793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3932793f3fe5SMatthew G. Knepley @*/
3933793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3934793f3fe5SMatthew G. Knepley {
3935793f3fe5SMatthew G. Knepley   PetscInt       d;
3936793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3937793f3fe5SMatthew G. Knepley 
3938793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3939793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3940793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3941793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3942793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3943793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3944793f3fe5SMatthew G. Knepley }
3945793f3fe5SMatthew G. Knepley 
3946793f3fe5SMatthew G. Knepley #undef __FUNCT__
39476636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
39486636e97aSMatthew G Knepley /*@
39496636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
39506636e97aSMatthew G Knepley 
39516636e97aSMatthew G Knepley   Collective on DM
39526636e97aSMatthew G Knepley 
39536636e97aSMatthew G Knepley   Input Parameters:
39546636e97aSMatthew G Knepley + dm - the DM
39556636e97aSMatthew G Knepley - c - coordinate vector
39566636e97aSMatthew G Knepley 
39576636e97aSMatthew G Knepley   Note:
39586636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
39596636e97aSMatthew G Knepley 
39606636e97aSMatthew G Knepley   Level: intermediate
39616636e97aSMatthew G Knepley 
39626636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39636636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
39646636e97aSMatthew G Knepley @*/
39656636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
39666636e97aSMatthew G Knepley {
39676636e97aSMatthew G Knepley   PetscErrorCode ierr;
39686636e97aSMatthew G Knepley 
39696636e97aSMatthew G Knepley   PetscFunctionBegin;
39706636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39716636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39726636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39736636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39746636e97aSMatthew G Knepley   dm->coordinates = c;
39756636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3976b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
397703dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39786636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39796636e97aSMatthew G Knepley }
39806636e97aSMatthew G Knepley 
39816636e97aSMatthew G Knepley #undef __FUNCT__
39826636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
39836636e97aSMatthew G Knepley /*@
39846636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39856636e97aSMatthew G Knepley 
39866636e97aSMatthew G Knepley   Collective on DM
39876636e97aSMatthew G Knepley 
39886636e97aSMatthew G Knepley    Input Parameters:
39896636e97aSMatthew G Knepley +  dm - the DM
39906636e97aSMatthew G Knepley -  c - coordinate vector
39916636e97aSMatthew G Knepley 
39926636e97aSMatthew G Knepley   Note:
39936636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39946636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39956636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39966636e97aSMatthew G Knepley 
39976636e97aSMatthew G Knepley   Level: intermediate
39986636e97aSMatthew G Knepley 
39996636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40006636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
40016636e97aSMatthew G Knepley @*/
40026636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
40036636e97aSMatthew G Knepley {
40046636e97aSMatthew G Knepley   PetscErrorCode ierr;
40056636e97aSMatthew G Knepley 
40066636e97aSMatthew G Knepley   PetscFunctionBegin;
40076636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40086636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
40096636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
40106636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
40118865f1eaSKarl Rupp 
40126636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
40138865f1eaSKarl Rupp 
40146636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
40156636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40166636e97aSMatthew G Knepley }
40176636e97aSMatthew G Knepley 
40186636e97aSMatthew G Knepley #undef __FUNCT__
40196636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
40206636e97aSMatthew G Knepley /*@
40216636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
40226636e97aSMatthew G Knepley 
40236636e97aSMatthew G Knepley   Not Collective
40246636e97aSMatthew G Knepley 
40256636e97aSMatthew G Knepley   Input Parameter:
40266636e97aSMatthew G Knepley . dm - the DM
40276636e97aSMatthew G Knepley 
40286636e97aSMatthew G Knepley   Output Parameter:
40296636e97aSMatthew G Knepley . c - global coordinate vector
40306636e97aSMatthew G Knepley 
40316636e97aSMatthew G Knepley   Note:
40326636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40336636e97aSMatthew G Knepley 
40346636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
40356636e97aSMatthew G Knepley 
40366636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40376636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40386636e97aSMatthew G Knepley 
40396636e97aSMatthew G Knepley   Level: intermediate
40406636e97aSMatthew G Knepley 
40416636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40426636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
40436636e97aSMatthew G Knepley @*/
40446636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
40456636e97aSMatthew G Knepley {
40466636e97aSMatthew G Knepley   PetscErrorCode ierr;
40476636e97aSMatthew G Knepley 
40486636e97aSMatthew G Knepley   PetscFunctionBegin;
40496636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40506636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40511f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
40520298fd71SBarry Smith     DM cdm = NULL;
40536636e97aSMatthew G Knepley 
40546636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40556636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
40566636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
40576636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40586636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40596636e97aSMatthew G Knepley   }
40606636e97aSMatthew G Knepley   *c = dm->coordinates;
40616636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40626636e97aSMatthew G Knepley }
40636636e97aSMatthew G Knepley 
40646636e97aSMatthew G Knepley #undef __FUNCT__
40656636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
40666636e97aSMatthew G Knepley /*@
40676636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
40686636e97aSMatthew G Knepley 
40696636e97aSMatthew G Knepley   Collective on DM
40706636e97aSMatthew G Knepley 
40716636e97aSMatthew G Knepley   Input Parameter:
40726636e97aSMatthew G Knepley . dm - the DM
40736636e97aSMatthew G Knepley 
40746636e97aSMatthew G Knepley   Output Parameter:
40756636e97aSMatthew G Knepley . c - coordinate vector
40766636e97aSMatthew G Knepley 
40776636e97aSMatthew G Knepley   Note:
40786636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40796636e97aSMatthew G Knepley 
40806636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40816636e97aSMatthew G Knepley 
40826636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40836636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40846636e97aSMatthew G Knepley 
40856636e97aSMatthew G Knepley   Level: intermediate
40866636e97aSMatthew G Knepley 
40876636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40886636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40896636e97aSMatthew G Knepley @*/
40906636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40916636e97aSMatthew G Knepley {
40926636e97aSMatthew G Knepley   PetscErrorCode ierr;
40936636e97aSMatthew G Knepley 
40946636e97aSMatthew G Knepley   PetscFunctionBegin;
40956636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40966636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40971f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40980298fd71SBarry Smith     DM cdm = NULL;
40996636e97aSMatthew G Knepley 
41006636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
41016636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
41026636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
41036636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41046636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41056636e97aSMatthew G Knepley   }
41066636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
41076636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41086636e97aSMatthew G Knepley }
41096636e97aSMatthew G Knepley 
41106636e97aSMatthew G Knepley #undef __FUNCT__
41116636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
41126636e97aSMatthew G Knepley /*@
41131cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
41146636e97aSMatthew G Knepley 
41156636e97aSMatthew G Knepley   Collective on DM
41166636e97aSMatthew G Knepley 
41176636e97aSMatthew G Knepley   Input Parameter:
41186636e97aSMatthew G Knepley . dm - the DM
41196636e97aSMatthew G Knepley 
41206636e97aSMatthew G Knepley   Output Parameter:
41216636e97aSMatthew G Knepley . cdm - coordinate DM
41226636e97aSMatthew G Knepley 
41236636e97aSMatthew G Knepley   Level: intermediate
41246636e97aSMatthew G Knepley 
41256636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41261cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41276636e97aSMatthew G Knepley @*/
41286636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
41296636e97aSMatthew G Knepley {
41306636e97aSMatthew G Knepley   PetscErrorCode ierr;
41316636e97aSMatthew G Knepley 
41326636e97aSMatthew G Knepley   PetscFunctionBegin;
41336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41346636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
41356636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
413682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
41376636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
41386636e97aSMatthew G Knepley   }
41396636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
41406636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41416636e97aSMatthew G Knepley }
4142e87bb0d3SMatthew G Knepley 
4143e87bb0d3SMatthew G Knepley #undef __FUNCT__
41441cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
41451cfe2091SMatthew G. Knepley /*@
41461cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
41471cfe2091SMatthew G. Knepley 
41481cfe2091SMatthew G. Knepley   Logically Collective on DM
41491cfe2091SMatthew G. Knepley 
41501cfe2091SMatthew G. Knepley   Input Parameters:
41511cfe2091SMatthew G. Knepley + dm - the DM
41521cfe2091SMatthew G. Knepley - cdm - coordinate DM
41531cfe2091SMatthew G. Knepley 
41541cfe2091SMatthew G. Knepley   Level: intermediate
41551cfe2091SMatthew G. Knepley 
41561cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41571cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41581cfe2091SMatthew G. Knepley @*/
41591cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
41601cfe2091SMatthew G. Knepley {
41611cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
41621cfe2091SMatthew G. Knepley 
41631cfe2091SMatthew G. Knepley   PetscFunctionBegin;
41641cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41651cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
41661cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
41671cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
41681cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
41691cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
41701cfe2091SMatthew G. Knepley }
41711cfe2091SMatthew G. Knepley 
41721cfe2091SMatthew G. Knepley #undef __FUNCT__
417346e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
417446e270d4SMatthew G. Knepley /*@
417546e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
417646e270d4SMatthew G. Knepley 
417746e270d4SMatthew G. Knepley   Not Collective
417846e270d4SMatthew G. Knepley 
417946e270d4SMatthew G. Knepley   Input Parameter:
418046e270d4SMatthew G. Knepley . dm - The DM object
418146e270d4SMatthew G. Knepley 
418246e270d4SMatthew G. Knepley   Output Parameter:
418346e270d4SMatthew G. Knepley . dim - The embedding dimension
418446e270d4SMatthew G. Knepley 
418546e270d4SMatthew G. Knepley   Level: intermediate
418646e270d4SMatthew G. Knepley 
418746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
418846e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
418946e270d4SMatthew G. Knepley @*/
419046e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
419146e270d4SMatthew G. Knepley {
419246e270d4SMatthew G. Knepley   PetscFunctionBegin;
419346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
419446e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41959a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41969a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41979a9a41abSToby Isaac   }
419846e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
419946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
420046e270d4SMatthew G. Knepley }
420146e270d4SMatthew G. Knepley 
420246e270d4SMatthew G. Knepley #undef __FUNCT__
420346e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
420446e270d4SMatthew G. Knepley /*@
420546e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
420646e270d4SMatthew G. Knepley 
420746e270d4SMatthew G. Knepley   Not Collective
420846e270d4SMatthew G. Knepley 
420946e270d4SMatthew G. Knepley   Input Parameters:
421046e270d4SMatthew G. Knepley + dm  - The DM object
421146e270d4SMatthew G. Knepley - dim - The embedding dimension
421246e270d4SMatthew G. Knepley 
421346e270d4SMatthew G. Knepley   Level: intermediate
421446e270d4SMatthew G. Knepley 
421546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
421646e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
421746e270d4SMatthew G. Knepley @*/
421846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
421946e270d4SMatthew G. Knepley {
422046e270d4SMatthew G. Knepley   PetscFunctionBegin;
422146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
422246e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
422346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
422446e270d4SMatthew G. Knepley }
422546e270d4SMatthew G. Knepley 
422646e270d4SMatthew G. Knepley #undef __FUNCT__
4227e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4228e8abe2deSMatthew G. Knepley /*@
4229e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4230e8abe2deSMatthew G. Knepley 
4231e8abe2deSMatthew G. Knepley   Not Collective
4232e8abe2deSMatthew G. Knepley 
4233e8abe2deSMatthew G. Knepley   Input Parameter:
4234e8abe2deSMatthew G. Knepley . dm - The DM object
4235e8abe2deSMatthew G. Knepley 
4236e8abe2deSMatthew G. Knepley   Output Parameter:
4237e8abe2deSMatthew G. Knepley . section - The PetscSection object
4238e8abe2deSMatthew G. Knepley 
4239e8abe2deSMatthew G. Knepley   Level: intermediate
4240e8abe2deSMatthew G. Knepley 
4241e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4242e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4243e8abe2deSMatthew G. Knepley @*/
4244e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4245e8abe2deSMatthew G. Knepley {
4246e8abe2deSMatthew G. Knepley   DM             cdm;
4247e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4248e8abe2deSMatthew G. Knepley 
4249e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4250e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4251e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4252e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4253e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4254e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4255e8abe2deSMatthew G. Knepley }
4256e8abe2deSMatthew G. Knepley 
4257e8abe2deSMatthew G. Knepley #undef __FUNCT__
4258e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4259e8abe2deSMatthew G. Knepley /*@
4260e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4261e8abe2deSMatthew G. Knepley 
4262e8abe2deSMatthew G. Knepley   Not Collective
4263e8abe2deSMatthew G. Knepley 
4264e8abe2deSMatthew G. Knepley   Input Parameters:
4265e8abe2deSMatthew G. Knepley + dm      - The DM object
426646e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4267e8abe2deSMatthew G. Knepley - section - The PetscSection object
4268e8abe2deSMatthew G. Knepley 
4269e8abe2deSMatthew G. Knepley   Level: intermediate
4270e8abe2deSMatthew G. Knepley 
4271e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4272e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4273e8abe2deSMatthew G. Knepley @*/
427446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4275e8abe2deSMatthew G. Knepley {
4276e8abe2deSMatthew G. Knepley   DM             cdm;
4277e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4278e8abe2deSMatthew G. Knepley 
4279e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4280e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
428146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4282e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4283e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
428446e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
428546e270d4SMatthew G. Knepley     PetscInt d = dim;
428646e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
428746e270d4SMatthew G. Knepley 
428846e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
428946e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
429046e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
429146e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
429246e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
429346e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
429446e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
429546e270d4SMatthew G. Knepley     }
429646e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
429746e270d4SMatthew G. Knepley   }
4298e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4299e8abe2deSMatthew G. Knepley }
4300e8abe2deSMatthew G. Knepley 
4301e8abe2deSMatthew G. Knepley #undef __FUNCT__
4302c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
43035dc8c3f7SMatthew G. Knepley /*@C
43045dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
43055dc8c3f7SMatthew G. Knepley 
43065dc8c3f7SMatthew G. Knepley   Input Parameters:
43075dc8c3f7SMatthew G. Knepley + dm      - The DM object
43085dc8c3f7SMatthew 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
43095dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43105dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43115dc8c3f7SMatthew G. Knepley 
43125dc8c3f7SMatthew G. Knepley   Level: developer
43135dc8c3f7SMatthew G. Knepley 
43145dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43155dc8c3f7SMatthew G. Knepley @*/
43165dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4317c6b900c6SMatthew G. Knepley {
4318c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4319c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4320c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4321c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
43225dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4323c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4324c6b900c6SMatthew G. Knepley }
4325c6b900c6SMatthew G. Knepley 
4326c6b900c6SMatthew G. Knepley #undef __FUNCT__
4327c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
43285dc8c3f7SMatthew G. Knepley /*@C
43295dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
43305dc8c3f7SMatthew G. Knepley 
43315dc8c3f7SMatthew G. Knepley   Input Parameters:
43325dc8c3f7SMatthew G. Knepley + dm      - The DM object
43335dc8c3f7SMatthew 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
43345dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43355dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43365dc8c3f7SMatthew G. Knepley 
43375dc8c3f7SMatthew G. Knepley   Level: developer
43385dc8c3f7SMatthew G. Knepley 
43395dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43405dc8c3f7SMatthew G. Knepley @*/
43415dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4342c6b900c6SMatthew G. Knepley {
4343c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4344c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4345c6b900c6SMatthew G. Knepley 
4346c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4347c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43485dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
43495dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
43505dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
43515dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
43525dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4353c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4354c6b900c6SMatthew G. Knepley }
4355c6b900c6SMatthew G. Knepley 
4356c6b900c6SMatthew G. Knepley #undef __FUNCT__
4357e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4358e87bb0d3SMatthew G Knepley /*@
4359e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4360e87bb0d3SMatthew G Knepley 
4361e87bb0d3SMatthew G Knepley   Not collective
4362e87bb0d3SMatthew G Knepley 
4363e87bb0d3SMatthew G Knepley   Input Parameters:
4364e87bb0d3SMatthew G Knepley + dm - The DM
4365e87bb0d3SMatthew G Knepley - v - The Vec of points
4366e87bb0d3SMatthew G Knepley 
436761e3bb9bSMatthew G Knepley   Output Parameter:
4368e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4369e87bb0d3SMatthew G Knepley 
4370e87bb0d3SMatthew G Knepley   Level: developer
437161e3bb9bSMatthew G Knepley 
437261e3bb9bSMatthew G Knepley .keywords: point location, mesh
437361e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
437461e3bb9bSMatthew G Knepley @*/
4375e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4376e87bb0d3SMatthew G Knepley {
4377735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4378735aa83eSMatthew G Knepley 
4379e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4380e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4381e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4382e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
438347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4384735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4385735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
438682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
438747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4388e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4389e87bb0d3SMatthew G Knepley }
439014f150ffSMatthew G. Knepley 
439114f150ffSMatthew G. Knepley #undef __FUNCT__
439214f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4393f4d763aaSMatthew G. Knepley /*@
4394f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4395f4d763aaSMatthew G. Knepley 
4396f4d763aaSMatthew G. Knepley   Input Parameter:
4397f4d763aaSMatthew G. Knepley . dm - The original DM
4398f4d763aaSMatthew G. Knepley 
4399f4d763aaSMatthew G. Knepley   Output Parameter:
4400f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4401f4d763aaSMatthew G. Knepley 
4402f4d763aaSMatthew G. Knepley   Level: intermediate
4403f4d763aaSMatthew G. Knepley 
4404f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4405f4d763aaSMatthew G. Knepley @*/
440614f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
440714f150ffSMatthew G. Knepley {
4408c26acbdeSMatthew G. Knepley   PetscSection   section;
4409c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
441014f150ffSMatthew G. Knepley   PetscErrorCode ierr;
441114f150ffSMatthew G. Knepley 
441214f150ffSMatthew G. Knepley   PetscFunctionBegin;
441314f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
441414f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4415c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4416c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4417c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4418c26acbdeSMatthew G. Knepley     *odm = dm;
4419c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4420c26acbdeSMatthew G. Knepley   }
442114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4422c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
442314f150ffSMatthew G. Knepley     PetscSF      sf;
442414f150ffSMatthew G. Knepley 
442514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
442614f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
442714f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
442814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
442914f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
443015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
443114f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
443214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
443314f150ffSMatthew G. Knepley   }
443414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
443514f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
443614f150ffSMatthew G. Knepley }
4437f4d763aaSMatthew G. Knepley 
4438f4d763aaSMatthew G. Knepley #undef __FUNCT__
4439f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4440f4d763aaSMatthew G. Knepley /*@
4441cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4442f4d763aaSMatthew G. Knepley 
4443f4d763aaSMatthew G. Knepley   Input Parameter:
4444f4d763aaSMatthew G. Knepley . dm - The original DM
4445f4d763aaSMatthew G. Knepley 
4446cdb7a50dSMatthew G. Knepley   Output Parameters:
4447cdb7a50dSMatthew G. Knepley + num - The output sequence number
4448cdb7a50dSMatthew G. Knepley - val - The output sequence value
4449f4d763aaSMatthew G. Knepley 
4450f4d763aaSMatthew G. Knepley   Level: intermediate
4451f4d763aaSMatthew G. Knepley 
4452f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4453f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4454f4d763aaSMatthew G. Knepley 
4455f4d763aaSMatthew G. Knepley .seealso: VecView()
4456f4d763aaSMatthew G. Knepley @*/
4457cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4458f4d763aaSMatthew G. Knepley {
4459f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4460f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4461cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4462cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4463f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4464f4d763aaSMatthew G. Knepley }
4465f4d763aaSMatthew G. Knepley 
4466f4d763aaSMatthew G. Knepley #undef __FUNCT__
4467f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4468f4d763aaSMatthew G. Knepley /*@
4469cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4470f4d763aaSMatthew G. Knepley 
4471f4d763aaSMatthew G. Knepley   Input Parameters:
4472f4d763aaSMatthew G. Knepley + dm - The original DM
4473cdb7a50dSMatthew G. Knepley . num - The output sequence number
4474cdb7a50dSMatthew G. Knepley - val - The output sequence value
4475f4d763aaSMatthew G. Knepley 
4476f4d763aaSMatthew G. Knepley   Level: intermediate
4477f4d763aaSMatthew G. Knepley 
4478f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4479f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4480f4d763aaSMatthew G. Knepley 
4481f4d763aaSMatthew G. Knepley .seealso: VecView()
4482f4d763aaSMatthew G. Knepley @*/
4483cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4484f4d763aaSMatthew G. Knepley {
4485f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4486f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4487f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4488cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4489cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4490cdb7a50dSMatthew G. Knepley }
4491cdb7a50dSMatthew G. Knepley 
4492cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4493cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4494cdb7a50dSMatthew G. Knepley /*@C
4495cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4496cdb7a50dSMatthew G. Knepley 
4497cdb7a50dSMatthew G. Knepley   Input Parameters:
4498cdb7a50dSMatthew G. Knepley + dm   - The original DM
4499cdb7a50dSMatthew G. Knepley . name - The sequence name
4500cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4501cdb7a50dSMatthew G. Knepley 
4502cdb7a50dSMatthew G. Knepley   Output Parameter:
4503cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4504cdb7a50dSMatthew G. Knepley 
4505cdb7a50dSMatthew G. Knepley   Level: intermediate
4506cdb7a50dSMatthew G. Knepley 
4507cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4508cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4509cdb7a50dSMatthew G. Knepley 
4510cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4511cdb7a50dSMatthew G. Knepley @*/
4512cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4513cdb7a50dSMatthew G. Knepley {
4514cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4515cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4516cdb7a50dSMatthew G. Knepley 
4517cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4518cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4519cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4520cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4521cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4522cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4523cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4524cdb7a50dSMatthew G. Knepley     PetscScalar value;
4525cdb7a50dSMatthew G. Knepley 
4526cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
45274aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4528cdb7a50dSMatthew G. Knepley #endif
4529cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4530f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4531f4d763aaSMatthew G. Knepley }
45328e4ac7eaSMatthew G. Knepley 
45338e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45348e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
45358e4ac7eaSMatthew G. Knepley /*@
45368e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
45378e4ac7eaSMatthew G. Knepley 
45388e4ac7eaSMatthew G. Knepley   Not collective
45398e4ac7eaSMatthew G. Knepley 
45408e4ac7eaSMatthew G. Knepley   Input Parameter:
45418e4ac7eaSMatthew G. Knepley . dm - The DM
45428e4ac7eaSMatthew G. Knepley 
45438e4ac7eaSMatthew G. Knepley   Output Parameter:
45448e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
45458e4ac7eaSMatthew G. Knepley 
45468e4ac7eaSMatthew G. Knepley   Level: beginner
45478e4ac7eaSMatthew G. Knepley 
45488e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
45498e4ac7eaSMatthew G. Knepley @*/
45508e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
45518e4ac7eaSMatthew G. Knepley {
45528e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45538e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45548e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
45558e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
45568e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45578e4ac7eaSMatthew G. Knepley }
45588e4ac7eaSMatthew G. Knepley 
45598e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45608e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
45618e4ac7eaSMatthew G. Knepley /*@
45628e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
45638e4ac7eaSMatthew G. Knepley 
45648e4ac7eaSMatthew G. Knepley   Collective on dm
45658e4ac7eaSMatthew G. Knepley 
45668e4ac7eaSMatthew G. Knepley   Input Parameters:
45678e4ac7eaSMatthew G. Knepley + dm - The DM
45688e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
45698e4ac7eaSMatthew G. Knepley 
45708e4ac7eaSMatthew G. Knepley   Level: beginner
45718e4ac7eaSMatthew G. Knepley 
45728e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
45738e4ac7eaSMatthew G. Knepley @*/
45748e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
45758e4ac7eaSMatthew G. Knepley {
45768e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45778e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45788e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
45798e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
45808e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45818e4ac7eaSMatthew G. Knepley }
4582c58f1c22SToby Isaac 
4583c58f1c22SToby Isaac #undef __FUNCT__
4584c58f1c22SToby Isaac #define __FUNCT__
4585c58f1c22SToby Isaac 
4586c58f1c22SToby Isaac #undef __FUNCT__
4587c58f1c22SToby Isaac #define __FUNCT__ "DMCreateLabel"
4588c58f1c22SToby Isaac /*@C
4589c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
4590c58f1c22SToby Isaac 
4591c58f1c22SToby Isaac   Not Collective
4592c58f1c22SToby Isaac 
4593c58f1c22SToby Isaac   Input Parameters:
4594c58f1c22SToby Isaac + dm   - The DM object
4595c58f1c22SToby Isaac - name - The label name
4596c58f1c22SToby Isaac 
4597c58f1c22SToby Isaac   Level: intermediate
4598c58f1c22SToby Isaac 
4599c58f1c22SToby Isaac .keywords: mesh
4600c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4601c58f1c22SToby Isaac @*/
4602c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
4603c58f1c22SToby Isaac {
4604c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
4605c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
4606c58f1c22SToby Isaac   PetscErrorCode ierr;
4607c58f1c22SToby Isaac 
4608c58f1c22SToby Isaac   PetscFunctionBegin;
4609c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4610c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4611c58f1c22SToby Isaac   while (next) {
4612c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
4613c58f1c22SToby Isaac     if (flg) break;
4614c58f1c22SToby Isaac     next = next->next;
4615c58f1c22SToby Isaac   }
4616c58f1c22SToby Isaac   if (!flg) {
4617c58f1c22SToby Isaac     DMLabelLink tmpLabel;
4618c58f1c22SToby Isaac 
4619c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
4620c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
4621c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
4622c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
4623c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
4624c58f1c22SToby Isaac   }
4625c58f1c22SToby Isaac   PetscFunctionReturn(0);
4626c58f1c22SToby Isaac }
4627c58f1c22SToby Isaac 
4628c58f1c22SToby Isaac #undef __FUNCT__
4629c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelValue"
4630c58f1c22SToby Isaac /*@C
4631c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
4632c58f1c22SToby Isaac 
4633c58f1c22SToby Isaac   Not Collective
4634c58f1c22SToby Isaac 
4635c58f1c22SToby Isaac   Input Parameters:
4636c58f1c22SToby Isaac + dm   - The DM object
4637c58f1c22SToby Isaac . name - The label name
4638c58f1c22SToby Isaac - point - The mesh point
4639c58f1c22SToby Isaac 
4640c58f1c22SToby Isaac   Output Parameter:
4641c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
4642c58f1c22SToby Isaac 
4643c58f1c22SToby Isaac   Level: beginner
4644c58f1c22SToby Isaac 
4645c58f1c22SToby Isaac .keywords: mesh
4646c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
4647c58f1c22SToby Isaac @*/
4648c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
4649c58f1c22SToby Isaac {
4650c58f1c22SToby Isaac   DMLabel        label;
4651c58f1c22SToby Isaac   PetscErrorCode ierr;
4652c58f1c22SToby Isaac 
4653c58f1c22SToby Isaac   PetscFunctionBegin;
4654c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4655c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4656c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4657c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
4658c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
4659c58f1c22SToby Isaac   PetscFunctionReturn(0);
4660c58f1c22SToby Isaac }
4661c58f1c22SToby Isaac 
4662c58f1c22SToby Isaac #undef __FUNCT__
4663c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelValue"
4664c58f1c22SToby Isaac /*@C
4665c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
4666c58f1c22SToby Isaac 
4667c58f1c22SToby Isaac   Not Collective
4668c58f1c22SToby Isaac 
4669c58f1c22SToby Isaac   Input Parameters:
4670c58f1c22SToby Isaac + dm   - The DM object
4671c58f1c22SToby Isaac . name - The label name
4672c58f1c22SToby Isaac . point - The mesh point
4673c58f1c22SToby Isaac - value - The label value for this point
4674c58f1c22SToby Isaac 
4675c58f1c22SToby Isaac   Output Parameter:
4676c58f1c22SToby Isaac 
4677c58f1c22SToby Isaac   Level: beginner
4678c58f1c22SToby Isaac 
4679c58f1c22SToby Isaac .keywords: mesh
4680c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
4681c58f1c22SToby Isaac @*/
4682c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4683c58f1c22SToby Isaac {
4684c58f1c22SToby Isaac   DMLabel        label;
4685c58f1c22SToby Isaac   PetscErrorCode ierr;
4686c58f1c22SToby Isaac 
4687c58f1c22SToby Isaac   PetscFunctionBegin;
4688c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4689c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4690c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4691c58f1c22SToby Isaac   if (!label) {
4692c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
4693c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4694c58f1c22SToby Isaac   }
4695c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
4696c58f1c22SToby Isaac   PetscFunctionReturn(0);
4697c58f1c22SToby Isaac }
4698c58f1c22SToby Isaac 
4699c58f1c22SToby Isaac #undef __FUNCT__
4700c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelValue"
4701c58f1c22SToby Isaac /*@C
4702c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
4703c58f1c22SToby Isaac 
4704c58f1c22SToby Isaac   Not Collective
4705c58f1c22SToby Isaac 
4706c58f1c22SToby Isaac   Input Parameters:
4707c58f1c22SToby Isaac + dm   - The DM object
4708c58f1c22SToby Isaac . name - The label name
4709c58f1c22SToby Isaac . point - The mesh point
4710c58f1c22SToby Isaac - value - The label value for this point
4711c58f1c22SToby Isaac 
4712c58f1c22SToby Isaac   Output Parameter:
4713c58f1c22SToby Isaac 
4714c58f1c22SToby Isaac   Level: beginner
4715c58f1c22SToby Isaac 
4716c58f1c22SToby Isaac .keywords: mesh
4717c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
4718c58f1c22SToby Isaac @*/
4719c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4720c58f1c22SToby Isaac {
4721c58f1c22SToby Isaac   DMLabel        label;
4722c58f1c22SToby Isaac   PetscErrorCode ierr;
4723c58f1c22SToby Isaac 
4724c58f1c22SToby Isaac   PetscFunctionBegin;
4725c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4726c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4727c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4728c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4729c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
4730c58f1c22SToby Isaac   PetscFunctionReturn(0);
4731c58f1c22SToby Isaac }
4732c58f1c22SToby Isaac 
4733c58f1c22SToby Isaac #undef __FUNCT__
4734c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelSize"
4735c58f1c22SToby Isaac /*@C
4736c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
4737c58f1c22SToby Isaac 
4738c58f1c22SToby Isaac   Not Collective
4739c58f1c22SToby Isaac 
4740c58f1c22SToby Isaac   Input Parameters:
4741c58f1c22SToby Isaac + dm   - The DM object
4742c58f1c22SToby Isaac - name - The label name
4743c58f1c22SToby Isaac 
4744c58f1c22SToby Isaac   Output Parameter:
4745c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
4746c58f1c22SToby Isaac 
4747c58f1c22SToby Isaac   Level: beginner
4748c58f1c22SToby Isaac 
4749c58f1c22SToby Isaac .keywords: mesh
4750c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
4751c58f1c22SToby Isaac @*/
4752c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
4753c58f1c22SToby Isaac {
4754c58f1c22SToby Isaac   DMLabel        label;
4755c58f1c22SToby Isaac   PetscErrorCode ierr;
4756c58f1c22SToby Isaac 
4757c58f1c22SToby Isaac   PetscFunctionBegin;
4758c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4759c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4760c58f1c22SToby Isaac   PetscValidPointer(size, 3);
4761c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4762c58f1c22SToby Isaac   *size = 0;
4763c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4764c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
4765c58f1c22SToby Isaac   PetscFunctionReturn(0);
4766c58f1c22SToby Isaac }
4767c58f1c22SToby Isaac 
4768c58f1c22SToby Isaac #undef __FUNCT__
4769c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelIdIS"
4770c58f1c22SToby Isaac /*@C
4771c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
4772c58f1c22SToby Isaac 
4773c58f1c22SToby Isaac   Not Collective
4774c58f1c22SToby Isaac 
4775c58f1c22SToby Isaac   Input Parameters:
4776c58f1c22SToby Isaac + mesh - The DM object
4777c58f1c22SToby Isaac - name - The label name
4778c58f1c22SToby Isaac 
4779c58f1c22SToby Isaac   Output Parameter:
4780c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
4781c58f1c22SToby Isaac 
4782c58f1c22SToby Isaac   Level: beginner
4783c58f1c22SToby Isaac 
4784c58f1c22SToby Isaac .keywords: mesh
4785c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
4786c58f1c22SToby Isaac @*/
4787c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
4788c58f1c22SToby Isaac {
4789c58f1c22SToby Isaac   DMLabel        label;
4790c58f1c22SToby Isaac   PetscErrorCode ierr;
4791c58f1c22SToby Isaac 
4792c58f1c22SToby Isaac   PetscFunctionBegin;
4793c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4794c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4795c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
4796c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4797c58f1c22SToby Isaac   *ids = NULL;
4798c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4799c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
4800c58f1c22SToby Isaac   PetscFunctionReturn(0);
4801c58f1c22SToby Isaac }
4802c58f1c22SToby Isaac 
4803c58f1c22SToby Isaac #undef __FUNCT__
4804c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumSize"
4805c58f1c22SToby Isaac /*@C
4806c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
4807c58f1c22SToby Isaac 
4808c58f1c22SToby Isaac   Not Collective
4809c58f1c22SToby Isaac 
4810c58f1c22SToby Isaac   Input Parameters:
4811c58f1c22SToby Isaac + dm - The DM object
4812c58f1c22SToby Isaac . name - The label name
4813c58f1c22SToby Isaac - value - The stratum value
4814c58f1c22SToby Isaac 
4815c58f1c22SToby Isaac   Output Parameter:
4816c58f1c22SToby Isaac . size - The stratum size
4817c58f1c22SToby Isaac 
4818c58f1c22SToby Isaac   Level: beginner
4819c58f1c22SToby Isaac 
4820c58f1c22SToby Isaac .keywords: mesh
4821c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
4822c58f1c22SToby Isaac @*/
4823c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
4824c58f1c22SToby Isaac {
4825c58f1c22SToby Isaac   DMLabel        label;
4826c58f1c22SToby Isaac   PetscErrorCode ierr;
4827c58f1c22SToby Isaac 
4828c58f1c22SToby Isaac   PetscFunctionBegin;
4829c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4830c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4831c58f1c22SToby Isaac   PetscValidPointer(size, 4);
4832c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4833c58f1c22SToby Isaac   *size = 0;
4834c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4835c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
4836c58f1c22SToby Isaac   PetscFunctionReturn(0);
4837c58f1c22SToby Isaac }
4838c58f1c22SToby Isaac 
4839c58f1c22SToby Isaac #undef __FUNCT__
4840c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumIS"
4841c58f1c22SToby Isaac /*@C
4842c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
4843c58f1c22SToby Isaac 
4844c58f1c22SToby Isaac   Not Collective
4845c58f1c22SToby Isaac 
4846c58f1c22SToby Isaac   Input Parameters:
4847c58f1c22SToby Isaac + dm - The DM object
4848c58f1c22SToby Isaac . name - The label name
4849c58f1c22SToby Isaac - value - The stratum value
4850c58f1c22SToby Isaac 
4851c58f1c22SToby Isaac   Output Parameter:
4852c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
4853c58f1c22SToby Isaac 
4854c58f1c22SToby Isaac   Level: beginner
4855c58f1c22SToby Isaac 
4856c58f1c22SToby Isaac .keywords: mesh
4857c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
4858c58f1c22SToby Isaac @*/
4859c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
4860c58f1c22SToby Isaac {
4861c58f1c22SToby Isaac   DMLabel        label;
4862c58f1c22SToby Isaac   PetscErrorCode ierr;
4863c58f1c22SToby Isaac 
4864c58f1c22SToby Isaac   PetscFunctionBegin;
4865c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4866c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4867c58f1c22SToby Isaac   PetscValidPointer(points, 4);
4868c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4869c58f1c22SToby Isaac   *points = NULL;
4870c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4871c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
4872c58f1c22SToby Isaac   PetscFunctionReturn(0);
4873c58f1c22SToby Isaac }
4874c58f1c22SToby Isaac 
4875c58f1c22SToby Isaac #undef __FUNCT__
4876c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelStratum"
4877c58f1c22SToby Isaac /*@C
4878c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
4879c58f1c22SToby Isaac 
4880c58f1c22SToby Isaac   Not Collective
4881c58f1c22SToby Isaac 
4882c58f1c22SToby Isaac   Input Parameters:
4883c58f1c22SToby Isaac + dm   - The DM object
4884c58f1c22SToby Isaac . name - The label name
4885c58f1c22SToby Isaac - value - The label value for this point
4886c58f1c22SToby Isaac 
4887c58f1c22SToby Isaac   Output Parameter:
4888c58f1c22SToby Isaac 
4889c58f1c22SToby Isaac   Level: beginner
4890c58f1c22SToby Isaac 
4891c58f1c22SToby Isaac .keywords: mesh
4892c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
4893c58f1c22SToby Isaac @*/
4894c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
4895c58f1c22SToby Isaac {
4896c58f1c22SToby Isaac   DMLabel        label;
4897c58f1c22SToby Isaac   PetscErrorCode ierr;
4898c58f1c22SToby Isaac 
4899c58f1c22SToby Isaac   PetscFunctionBegin;
4900c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4901c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4902c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4903c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4904c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
4905c58f1c22SToby Isaac   PetscFunctionReturn(0);
4906c58f1c22SToby Isaac }
4907c58f1c22SToby Isaac 
4908c58f1c22SToby Isaac #undef __FUNCT__
4909c58f1c22SToby Isaac #define __FUNCT__ "DMGetNumLabels"
4910c58f1c22SToby Isaac /*@
4911c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
4912c58f1c22SToby Isaac 
4913c58f1c22SToby Isaac   Not Collective
4914c58f1c22SToby Isaac 
4915c58f1c22SToby Isaac   Input Parameter:
4916c58f1c22SToby Isaac . dm   - The DM object
4917c58f1c22SToby Isaac 
4918c58f1c22SToby Isaac   Output Parameter:
4919c58f1c22SToby Isaac . numLabels - the number of Labels
4920c58f1c22SToby Isaac 
4921c58f1c22SToby Isaac   Level: intermediate
4922c58f1c22SToby Isaac 
4923c58f1c22SToby Isaac .keywords: mesh
4924c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4925c58f1c22SToby Isaac @*/
4926c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
4927c58f1c22SToby Isaac {
4928c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
4929c58f1c22SToby Isaac   PetscInt  n    = 0;
4930c58f1c22SToby Isaac 
4931c58f1c22SToby Isaac   PetscFunctionBegin;
4932c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4933c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
4934c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
4935c58f1c22SToby Isaac   *numLabels = n;
4936c58f1c22SToby Isaac   PetscFunctionReturn(0);
4937c58f1c22SToby Isaac }
4938c58f1c22SToby Isaac 
4939c58f1c22SToby Isaac #undef __FUNCT__
4940c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelName"
4941c58f1c22SToby Isaac /*@C
4942c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
4943c58f1c22SToby Isaac 
4944c58f1c22SToby Isaac   Not Collective
4945c58f1c22SToby Isaac 
4946c58f1c22SToby Isaac   Input Parameters:
4947c58f1c22SToby Isaac + dm - The DM object
4948c58f1c22SToby Isaac - n  - the label number
4949c58f1c22SToby Isaac 
4950c58f1c22SToby Isaac   Output Parameter:
4951c58f1c22SToby Isaac . name - the label name
4952c58f1c22SToby Isaac 
4953c58f1c22SToby Isaac   Level: intermediate
4954c58f1c22SToby Isaac 
4955c58f1c22SToby Isaac .keywords: mesh
4956c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4957c58f1c22SToby Isaac @*/
4958c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
4959c58f1c22SToby Isaac {
4960c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
4961c58f1c22SToby Isaac   PetscInt  l    = 0;
4962c58f1c22SToby Isaac 
4963c58f1c22SToby Isaac   PetscFunctionBegin;
4964c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4965c58f1c22SToby Isaac   PetscValidPointer(name, 3);
4966c58f1c22SToby Isaac   while (next) {
4967c58f1c22SToby Isaac     if (l == n) {
4968c58f1c22SToby Isaac       *name = next->label->name;
4969c58f1c22SToby Isaac       PetscFunctionReturn(0);
4970c58f1c22SToby Isaac     }
4971c58f1c22SToby Isaac     ++l;
4972c58f1c22SToby Isaac     next = next->next;
4973c58f1c22SToby Isaac   }
4974c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
4975c58f1c22SToby Isaac }
4976c58f1c22SToby Isaac 
4977c58f1c22SToby Isaac #undef __FUNCT__
4978c58f1c22SToby Isaac #define __FUNCT__ "DMHasLabel"
4979c58f1c22SToby Isaac /*@C
4980c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
4981c58f1c22SToby Isaac 
4982c58f1c22SToby Isaac   Not Collective
4983c58f1c22SToby Isaac 
4984c58f1c22SToby Isaac   Input Parameters:
4985c58f1c22SToby Isaac + dm   - The DM object
4986c58f1c22SToby Isaac - name - The label name
4987c58f1c22SToby Isaac 
4988c58f1c22SToby Isaac   Output Parameter:
4989c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
4990c58f1c22SToby Isaac 
4991c58f1c22SToby Isaac   Level: intermediate
4992c58f1c22SToby Isaac 
4993c58f1c22SToby Isaac .keywords: mesh
4994c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4995c58f1c22SToby Isaac @*/
4996c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
4997c58f1c22SToby Isaac {
4998c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
4999c58f1c22SToby Isaac   PetscErrorCode ierr;
5000c58f1c22SToby Isaac 
5001c58f1c22SToby Isaac   PetscFunctionBegin;
5002c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5003c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5004c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5005c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5006c58f1c22SToby Isaac   while (next) {
5007c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5008c58f1c22SToby Isaac     if (*hasLabel) break;
5009c58f1c22SToby Isaac     next = next->next;
5010c58f1c22SToby Isaac   }
5011c58f1c22SToby Isaac   PetscFunctionReturn(0);
5012c58f1c22SToby Isaac }
5013c58f1c22SToby Isaac 
5014c58f1c22SToby Isaac #undef __FUNCT__
5015c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabel"
5016c58f1c22SToby Isaac /*@C
5017c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5018c58f1c22SToby Isaac 
5019c58f1c22SToby Isaac   Not Collective
5020c58f1c22SToby Isaac 
5021c58f1c22SToby Isaac   Input Parameters:
5022c58f1c22SToby Isaac + dm   - The DM object
5023c58f1c22SToby Isaac - name - The label name
5024c58f1c22SToby Isaac 
5025c58f1c22SToby Isaac   Output Parameter:
5026c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5027c58f1c22SToby Isaac 
5028c58f1c22SToby Isaac   Level: intermediate
5029c58f1c22SToby Isaac 
5030c58f1c22SToby Isaac .keywords: mesh
5031c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5032c58f1c22SToby Isaac @*/
5033c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5034c58f1c22SToby Isaac {
5035c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5036c58f1c22SToby Isaac   PetscBool      hasLabel;
5037c58f1c22SToby Isaac   PetscErrorCode ierr;
5038c58f1c22SToby Isaac 
5039c58f1c22SToby Isaac   PetscFunctionBegin;
5040c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5041c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5042c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5043c58f1c22SToby Isaac   *label = NULL;
5044c58f1c22SToby Isaac   while (next) {
5045c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5046c58f1c22SToby Isaac     if (hasLabel) {
5047c58f1c22SToby Isaac       *label = next->label;
5048c58f1c22SToby Isaac       break;
5049c58f1c22SToby Isaac     }
5050c58f1c22SToby Isaac     next = next->next;
5051c58f1c22SToby Isaac   }
5052c58f1c22SToby Isaac   PetscFunctionReturn(0);
5053c58f1c22SToby Isaac }
5054c58f1c22SToby Isaac 
5055c58f1c22SToby Isaac #undef __FUNCT__
5056c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelByNum"
5057c58f1c22SToby Isaac /*@C
5058c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5059c58f1c22SToby Isaac 
5060c58f1c22SToby Isaac   Not Collective
5061c58f1c22SToby Isaac 
5062c58f1c22SToby Isaac   Input Parameters:
5063c58f1c22SToby Isaac + dm - The DM object
5064c58f1c22SToby Isaac - n  - the label number
5065c58f1c22SToby Isaac 
5066c58f1c22SToby Isaac   Output Parameter:
5067c58f1c22SToby Isaac . label - the label
5068c58f1c22SToby Isaac 
5069c58f1c22SToby Isaac   Level: intermediate
5070c58f1c22SToby Isaac 
5071c58f1c22SToby Isaac .keywords: mesh
5072c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5073c58f1c22SToby Isaac @*/
5074c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5075c58f1c22SToby Isaac {
5076c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5077c58f1c22SToby Isaac   PetscInt    l    = 0;
5078c58f1c22SToby Isaac 
5079c58f1c22SToby Isaac   PetscFunctionBegin;
5080c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5081c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5082c58f1c22SToby Isaac   while (next) {
5083c58f1c22SToby Isaac     if (l == n) {
5084c58f1c22SToby Isaac       *label = next->label;
5085c58f1c22SToby Isaac       PetscFunctionReturn(0);
5086c58f1c22SToby Isaac     }
5087c58f1c22SToby Isaac     ++l;
5088c58f1c22SToby Isaac     next = next->next;
5089c58f1c22SToby Isaac   }
5090c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5091c58f1c22SToby Isaac }
5092c58f1c22SToby Isaac 
5093c58f1c22SToby Isaac #undef __FUNCT__
5094c58f1c22SToby Isaac #define __FUNCT__ "DMAddLabel"
5095c58f1c22SToby Isaac /*@C
5096c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5097c58f1c22SToby Isaac 
5098c58f1c22SToby Isaac   Not Collective
5099c58f1c22SToby Isaac 
5100c58f1c22SToby Isaac   Input Parameters:
5101c58f1c22SToby Isaac + dm   - The DM object
5102c58f1c22SToby Isaac - label - The DMLabel
5103c58f1c22SToby Isaac 
5104c58f1c22SToby Isaac   Level: developer
5105c58f1c22SToby Isaac 
5106c58f1c22SToby Isaac .keywords: mesh
5107c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5108c58f1c22SToby Isaac @*/
5109c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5110c58f1c22SToby Isaac {
5111c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5112c58f1c22SToby Isaac   PetscBool      hasLabel;
5113c58f1c22SToby Isaac   PetscErrorCode ierr;
5114c58f1c22SToby Isaac 
5115c58f1c22SToby Isaac   PetscFunctionBegin;
5116c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5117c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5118c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5119c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5120c58f1c22SToby Isaac   tmpLabel->label  = label;
5121c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5122c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5123c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5124c58f1c22SToby Isaac   PetscFunctionReturn(0);
5125c58f1c22SToby Isaac }
5126c58f1c22SToby Isaac 
5127c58f1c22SToby Isaac #undef __FUNCT__
5128c58f1c22SToby Isaac #define __FUNCT__ "DMRemoveLabel"
5129c58f1c22SToby Isaac /*@C
5130c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5131c58f1c22SToby Isaac 
5132c58f1c22SToby Isaac   Not Collective
5133c58f1c22SToby Isaac 
5134c58f1c22SToby Isaac   Input Parameters:
5135c58f1c22SToby Isaac + dm   - The DM object
5136c58f1c22SToby Isaac - name - The label name
5137c58f1c22SToby Isaac 
5138c58f1c22SToby Isaac   Output Parameter:
5139c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5140c58f1c22SToby Isaac 
5141c58f1c22SToby Isaac   Level: developer
5142c58f1c22SToby Isaac 
5143c58f1c22SToby Isaac .keywords: mesh
5144c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5145c58f1c22SToby Isaac @*/
5146c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5147c58f1c22SToby Isaac {
5148c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5149c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5150c58f1c22SToby Isaac   PetscBool      hasLabel;
5151c58f1c22SToby Isaac   PetscErrorCode ierr;
5152c58f1c22SToby Isaac 
5153c58f1c22SToby Isaac   PetscFunctionBegin;
5154c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5155c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5156c58f1c22SToby Isaac   *label = NULL;
5157c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5158c58f1c22SToby Isaac   while (next) {
5159c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5160c58f1c22SToby Isaac     if (hasLabel) {
5161c58f1c22SToby Isaac       if (last) last->next       = next->next;
5162c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5163c58f1c22SToby Isaac       next->next = NULL;
5164c58f1c22SToby Isaac       *label     = next->label;
5165c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5166c58f1c22SToby Isaac       if (hasLabel) {
5167c58f1c22SToby Isaac         dm->depthLabel = NULL;
5168c58f1c22SToby Isaac       }
5169c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5170c58f1c22SToby Isaac       break;
5171c58f1c22SToby Isaac     }
5172c58f1c22SToby Isaac     last = next;
5173c58f1c22SToby Isaac     next = next->next;
5174c58f1c22SToby Isaac   }
5175c58f1c22SToby Isaac   PetscFunctionReturn(0);
5176c58f1c22SToby Isaac }
5177c58f1c22SToby Isaac 
5178c58f1c22SToby Isaac #undef __FUNCT__
5179c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelOutput"
5180c58f1c22SToby Isaac /*@C
5181c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5182c58f1c22SToby Isaac 
5183c58f1c22SToby Isaac   Not Collective
5184c58f1c22SToby Isaac 
5185c58f1c22SToby Isaac   Input Parameters:
5186c58f1c22SToby Isaac + dm   - The DM object
5187c58f1c22SToby Isaac - name - The label name
5188c58f1c22SToby Isaac 
5189c58f1c22SToby Isaac   Output Parameter:
5190c58f1c22SToby Isaac . output - The flag for output
5191c58f1c22SToby Isaac 
5192c58f1c22SToby Isaac   Level: developer
5193c58f1c22SToby Isaac 
5194c58f1c22SToby Isaac .keywords: mesh
5195c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5196c58f1c22SToby Isaac @*/
5197c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5198c58f1c22SToby Isaac {
5199c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5200c58f1c22SToby Isaac   PetscErrorCode ierr;
5201c58f1c22SToby Isaac 
5202c58f1c22SToby Isaac   PetscFunctionBegin;
5203c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5204c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5205c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5206c58f1c22SToby Isaac   while (next) {
5207c58f1c22SToby Isaac     PetscBool flg;
5208c58f1c22SToby Isaac 
5209c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5210c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5211c58f1c22SToby Isaac     next = next->next;
5212c58f1c22SToby Isaac   }
5213c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5214c58f1c22SToby Isaac }
5215c58f1c22SToby Isaac 
5216c58f1c22SToby Isaac #undef __FUNCT__
5217c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelOutput"
5218c58f1c22SToby Isaac /*@C
5219c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5220c58f1c22SToby Isaac 
5221c58f1c22SToby Isaac   Not Collective
5222c58f1c22SToby Isaac 
5223c58f1c22SToby Isaac   Input Parameters:
5224c58f1c22SToby Isaac + dm     - The DM object
5225c58f1c22SToby Isaac . name   - The label name
5226c58f1c22SToby Isaac - output - The flag for output
5227c58f1c22SToby Isaac 
5228c58f1c22SToby Isaac   Level: developer
5229c58f1c22SToby Isaac 
5230c58f1c22SToby Isaac .keywords: mesh
5231c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5232c58f1c22SToby Isaac @*/
5233c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5234c58f1c22SToby Isaac {
5235c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5236c58f1c22SToby Isaac   PetscErrorCode ierr;
5237c58f1c22SToby Isaac 
5238c58f1c22SToby Isaac   PetscFunctionBegin;
5239c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5240c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5241c58f1c22SToby Isaac   while (next) {
5242c58f1c22SToby Isaac     PetscBool flg;
5243c58f1c22SToby Isaac 
5244c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5245c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5246c58f1c22SToby Isaac     next = next->next;
5247c58f1c22SToby Isaac   }
5248c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5249c58f1c22SToby Isaac }
5250c58f1c22SToby Isaac 
5251c58f1c22SToby Isaac 
5252c58f1c22SToby Isaac #undef __FUNCT__
5253c58f1c22SToby Isaac #define __FUNCT__ "DMCopyLabels"
5254c58f1c22SToby Isaac /*@
5255c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5256c58f1c22SToby Isaac 
5257c58f1c22SToby Isaac   Collective on DM
5258c58f1c22SToby Isaac 
5259c58f1c22SToby Isaac   Input Parameter:
5260c58f1c22SToby Isaac . dmA - The DMPlex object with initial labels
5261c58f1c22SToby Isaac 
5262c58f1c22SToby Isaac   Output Parameter:
5263c58f1c22SToby Isaac . dmB - The DMPlex object with copied labels
5264c58f1c22SToby Isaac 
5265c58f1c22SToby Isaac   Level: intermediate
5266c58f1c22SToby Isaac 
5267c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5268c58f1c22SToby Isaac 
5269c58f1c22SToby Isaac .keywords: mesh
5270c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5271c58f1c22SToby Isaac @*/
5272c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5273c58f1c22SToby Isaac {
5274c58f1c22SToby Isaac   PetscInt       numLabels, l;
5275c58f1c22SToby Isaac   PetscErrorCode ierr;
5276c58f1c22SToby Isaac 
5277c58f1c22SToby Isaac   PetscFunctionBegin;
5278c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5279c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5280c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5281c58f1c22SToby Isaac     DMLabel     label, labelNew;
5282c58f1c22SToby Isaac     const char *name;
5283c58f1c22SToby Isaac     PetscBool   flg;
5284c58f1c22SToby Isaac 
5285c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5286c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5287c58f1c22SToby Isaac     if (flg) continue;
5288c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5289c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5290c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5291c58f1c22SToby Isaac   }
5292c58f1c22SToby Isaac   PetscFunctionReturn(0);
5293c58f1c22SToby Isaac }
5294a8fb8f29SToby Isaac 
5295a8fb8f29SToby Isaac #undef __FUNCT__
5296a8fb8f29SToby Isaac #define __FUNCT__ "DMGetCoarseDM"
5297a8fb8f29SToby Isaac /*@
5298a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5299a8fb8f29SToby Isaac 
5300a8fb8f29SToby Isaac   Input Parameter:
5301a8fb8f29SToby Isaac . dm - The DM object
5302a8fb8f29SToby Isaac 
5303a8fb8f29SToby Isaac   Output Parameter:
5304a8fb8f29SToby Isaac . cdm - The coarse DM
5305a8fb8f29SToby Isaac 
5306a8fb8f29SToby Isaac   Level: intermediate
5307a8fb8f29SToby Isaac 
5308a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5309a8fb8f29SToby Isaac @*/
5310a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5311a8fb8f29SToby Isaac {
5312a8fb8f29SToby Isaac   PetscFunctionBegin;
5313a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5314a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5315a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5316a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5317a8fb8f29SToby Isaac }
5318a8fb8f29SToby Isaac 
5319a8fb8f29SToby Isaac #undef __FUNCT__
5320a8fb8f29SToby Isaac #define __FUNCT__ "DMSetCoarseDM"
5321a8fb8f29SToby Isaac /*@
5322a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5323a8fb8f29SToby Isaac 
5324a8fb8f29SToby Isaac   Input Parameters:
5325a8fb8f29SToby Isaac + dm - The DM object
5326a8fb8f29SToby Isaac - cdm - The coarse DM
5327a8fb8f29SToby Isaac 
5328a8fb8f29SToby Isaac   Level: intermediate
5329a8fb8f29SToby Isaac 
5330a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5331a8fb8f29SToby Isaac @*/
5332a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5333a8fb8f29SToby Isaac {
5334a8fb8f29SToby Isaac   PetscErrorCode ierr;
5335a8fb8f29SToby Isaac 
5336a8fb8f29SToby Isaac   PetscFunctionBegin;
5337a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5338a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5339a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5340a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5341a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5342a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5343a8fb8f29SToby Isaac }
5344a8fb8f29SToby Isaac 
534588bdff64SToby Isaac #undef __FUNCT__
534688bdff64SToby Isaac #define __FUNCT__ "DMGetFineDM"
534788bdff64SToby Isaac /*@
534888bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
534988bdff64SToby Isaac 
535088bdff64SToby Isaac   Input Parameter:
535188bdff64SToby Isaac . dm - The DM object
535288bdff64SToby Isaac 
535388bdff64SToby Isaac   Output Parameter:
535488bdff64SToby Isaac . fdm - The fine DM
535588bdff64SToby Isaac 
535688bdff64SToby Isaac   Level: intermediate
535788bdff64SToby Isaac 
535888bdff64SToby Isaac .seealso: DMSetFineDM()
535988bdff64SToby Isaac @*/
536088bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
536188bdff64SToby Isaac {
536288bdff64SToby Isaac   PetscFunctionBegin;
536388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
536488bdff64SToby Isaac   PetscValidPointer(fdm, 2);
536588bdff64SToby Isaac   *fdm = dm->fineMesh;
536688bdff64SToby Isaac   PetscFunctionReturn(0);
536788bdff64SToby Isaac }
536888bdff64SToby Isaac 
536988bdff64SToby Isaac #undef __FUNCT__
537088bdff64SToby Isaac #define __FUNCT__ "DMSetFineDM"
537188bdff64SToby Isaac /*@
537288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
537388bdff64SToby Isaac 
537488bdff64SToby Isaac   Input Parameters:
537588bdff64SToby Isaac + dm - The DM object
537688bdff64SToby Isaac - fdm - The fine DM
537788bdff64SToby Isaac 
537888bdff64SToby Isaac   Level: intermediate
537988bdff64SToby Isaac 
538088bdff64SToby Isaac .seealso: DMGetFineDM()
538188bdff64SToby Isaac @*/
538288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
538388bdff64SToby Isaac {
538488bdff64SToby Isaac   PetscErrorCode ierr;
538588bdff64SToby Isaac 
538688bdff64SToby Isaac   PetscFunctionBegin;
538788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
538888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
538988bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
539088bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
539188bdff64SToby Isaac   dm->fineMesh = fdm;
539288bdff64SToby Isaac   PetscFunctionReturn(0);
539388bdff64SToby Isaac }
539488bdff64SToby Isaac 
5395a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5396a6ba4734SToby Isaac 
5397a6ba4734SToby Isaac #undef __FUNCT__
5398*bcc5ffd9SToby Isaac #define __FUNCT__ "DMBoundaryDuplicate"
5399*bcc5ffd9SToby Isaac static PetscErrorCode DMBoundaryDuplicate(DMBoundaryLinkList bd, DMBoundaryLinkList *boundary)
5400a6ba4734SToby Isaac {
5401*bcc5ffd9SToby Isaac   DMBoundary     b = bd->next, b2, bold = NULL;
5402a6ba4734SToby Isaac   PetscErrorCode ierr;
5403a6ba4734SToby Isaac 
5404a6ba4734SToby Isaac   PetscFunctionBegin;
5405*bcc5ffd9SToby Isaac   ierr = PetscNew(boundary);CHKERRQ(ierr);
5406*bcc5ffd9SToby Isaac   (*boundary)->refct = 1;
5407*bcc5ffd9SToby Isaac   (*boundary)->next = NULL;
5408a6ba4734SToby Isaac   for (; b; b = b->next, bold = b2) {
5409a6ba4734SToby Isaac     ierr = PetscNew(&b2);CHKERRQ(ierr);
5410a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
5411a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
5412a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
5413a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
5414a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numcomps, &b2->comps);CHKERRQ(ierr);
5415a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->comps, b->comps, b->numcomps*sizeof(PetscInt));CHKERRQ(ierr);
5416a6ba4734SToby Isaac     b2->label     = NULL;
5417a6ba4734SToby Isaac     b2->essential = b->essential;
5418a6ba4734SToby Isaac     b2->field     = b->field;
5419a6ba4734SToby Isaac     b2->numcomps  = b->numcomps;
5420a6ba4734SToby Isaac     b2->func      = b->func;
5421a6ba4734SToby Isaac     b2->numids    = b->numids;
5422a6ba4734SToby Isaac     b2->ctx       = b->ctx;
5423a6ba4734SToby Isaac     b2->next      = NULL;
5424*bcc5ffd9SToby Isaac     if (!(*boundary)->next) (*boundary)->next   = b2;
5425a6ba4734SToby Isaac     if (bold)        bold->next = b2;
5426a6ba4734SToby Isaac   }
5427a6ba4734SToby Isaac   PetscFunctionReturn(0);
5428a6ba4734SToby Isaac }
5429a6ba4734SToby Isaac 
5430a6ba4734SToby Isaac #undef __FUNCT__
5431*bcc5ffd9SToby Isaac #define __FUNCT__ "DMBoundaryDestroy"
5432*bcc5ffd9SToby Isaac PetscErrorCode DMBoundaryDestroy(DMBoundaryLinkList *boundary)
5433a6ba4734SToby Isaac {
5434a6ba4734SToby Isaac   DMBoundary     b, next;
5435a6ba4734SToby Isaac   PetscErrorCode ierr;
5436a6ba4734SToby Isaac 
5437a6ba4734SToby Isaac   PetscFunctionBegin;
5438a6ba4734SToby Isaac   if (!boundary) PetscFunctionReturn(0);
5439*bcc5ffd9SToby Isaac   if (--((*boundary)->refct)) {
5440a6ba4734SToby Isaac     *boundary = NULL;
5441*bcc5ffd9SToby Isaac     PetscFunctionReturn(0);
5442*bcc5ffd9SToby Isaac   }
5443*bcc5ffd9SToby Isaac   b = (*boundary)->next;
5444a6ba4734SToby Isaac   for (; b; b = next) {
5445a6ba4734SToby Isaac     next = b->next;
5446a6ba4734SToby Isaac     ierr = PetscFree(b->comps);CHKERRQ(ierr);
5447a6ba4734SToby Isaac     ierr = PetscFree(b->ids);CHKERRQ(ierr);
5448a6ba4734SToby Isaac     ierr = PetscFree(b->name);CHKERRQ(ierr);
5449a6ba4734SToby Isaac     ierr = PetscFree(b->labelname);CHKERRQ(ierr);
5450a6ba4734SToby Isaac     ierr = PetscFree(b);CHKERRQ(ierr);
5451a6ba4734SToby Isaac   }
5452*bcc5ffd9SToby Isaac   ierr = PetscFree(*boundary);CHKERRQ(ierr);
5453a6ba4734SToby Isaac   PetscFunctionReturn(0);
5454a6ba4734SToby Isaac }
5455a6ba4734SToby Isaac 
5456a6ba4734SToby Isaac #undef __FUNCT__
5457a6ba4734SToby Isaac #define __FUNCT__ "DMCopyBoundary"
5458a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5459a6ba4734SToby Isaac {
5460a6ba4734SToby Isaac   DMBoundary     b;
5461a6ba4734SToby Isaac   PetscErrorCode ierr;
5462a6ba4734SToby Isaac 
5463a6ba4734SToby Isaac   PetscFunctionBegin;
5464*bcc5ffd9SToby Isaac   ierr = DMBoundaryDestroy(&dmNew->boundary);CHKERRQ(ierr);
5465*bcc5ffd9SToby Isaac   ierr = DMBoundaryDuplicate(dm->boundary, &dmNew->boundary);CHKERRQ(ierr);
5466*bcc5ffd9SToby Isaac   for (b = dmNew->boundary->next; b; b = b->next) {
5467a6ba4734SToby Isaac     if (b->labelname) {
5468a6ba4734SToby Isaac       ierr = DMGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
5469a6ba4734SToby Isaac       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5470a6ba4734SToby Isaac     }
5471a6ba4734SToby Isaac   }
5472a6ba4734SToby Isaac   PetscFunctionReturn(0);
5473a6ba4734SToby Isaac }
5474a6ba4734SToby Isaac 
5475a6ba4734SToby Isaac #undef __FUNCT__
5476a6ba4734SToby Isaac #define __FUNCT__ "DMAddBoundary"
5477a6ba4734SToby Isaac /*@C
5478a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5479a6ba4734SToby Isaac 
5480a6ba4734SToby Isaac   Input Parameters:
5481a6ba4734SToby Isaac + dm          - The mesh object
5482a6ba4734SToby Isaac . isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5483a6ba4734SToby Isaac . name        - The BC name
5484a6ba4734SToby Isaac . labelname   - The label defining constrained points
5485a6ba4734SToby Isaac . field       - The field to constrain
5486a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5487a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5488a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5489a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5490a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5491a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5492a6ba4734SToby Isaac 
5493a6ba4734SToby Isaac   Options Database Keys:
5494a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5495a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5496a6ba4734SToby Isaac 
5497a6ba4734SToby Isaac   Level: developer
5498a6ba4734SToby Isaac 
5499a6ba4734SToby Isaac .seealso: DMGetBoundary()
5500a6ba4734SToby Isaac @*/
5501a6ba4734SToby Isaac PetscErrorCode DMAddBoundary(DM dm, PetscBool isEssential, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(), PetscInt numids, const PetscInt *ids, void *ctx)
5502a6ba4734SToby Isaac {
5503a6ba4734SToby Isaac   DMBoundary     b;
5504a6ba4734SToby Isaac   PetscErrorCode ierr;
5505a6ba4734SToby Isaac 
5506a6ba4734SToby Isaac   PetscFunctionBegin;
5507a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5508a6ba4734SToby Isaac   ierr = PetscNew(&b);CHKERRQ(ierr);
5509a6ba4734SToby Isaac   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
5510a6ba4734SToby Isaac   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
5511a6ba4734SToby Isaac   ierr = PetscMalloc1(numcomps, &b->comps);CHKERRQ(ierr);
5512a6ba4734SToby Isaac   if (numcomps) {ierr = PetscMemcpy(b->comps, comps, numcomps*sizeof(PetscInt));CHKERRQ(ierr);}
5513a6ba4734SToby Isaac   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
5514a6ba4734SToby Isaac   if (numids) {ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);}
5515a6ba4734SToby Isaac   if (b->labelname) {
5516a6ba4734SToby Isaac     ierr = DMGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
5517a6ba4734SToby Isaac     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5518a6ba4734SToby Isaac   }
5519a6ba4734SToby Isaac   b->essential       = isEssential;
5520a6ba4734SToby Isaac   b->field           = field;
5521a6ba4734SToby Isaac   b->numcomps        = numcomps;
5522a6ba4734SToby Isaac   b->func            = bcFunc;
5523a6ba4734SToby Isaac   b->numids          = numids;
5524a6ba4734SToby Isaac   b->ctx             = ctx;
5525*bcc5ffd9SToby Isaac   b->next            = dm->boundary->next;
5526*bcc5ffd9SToby Isaac   dm->boundary->next = b;
5527a6ba4734SToby Isaac   PetscFunctionReturn(0);
5528a6ba4734SToby Isaac }
5529a6ba4734SToby Isaac 
5530a6ba4734SToby Isaac #undef __FUNCT__
5531a6ba4734SToby Isaac #define __FUNCT__ "DMGetNumBoundary"
5532a6ba4734SToby Isaac /*@
5533a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5534a6ba4734SToby Isaac 
5535a6ba4734SToby Isaac   Input Parameters:
5536a6ba4734SToby Isaac . dm - The mesh object
5537a6ba4734SToby Isaac 
5538a6ba4734SToby Isaac   Output Parameters:
5539a6ba4734SToby Isaac . numBd - The number of BC
5540a6ba4734SToby Isaac 
5541a6ba4734SToby Isaac   Level: intermediate
5542a6ba4734SToby Isaac 
5543a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5544a6ba4734SToby Isaac @*/
5545a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5546a6ba4734SToby Isaac {
5547*bcc5ffd9SToby Isaac   DMBoundary b = dm->boundary->next;
5548a6ba4734SToby Isaac 
5549a6ba4734SToby Isaac   PetscFunctionBegin;
5550a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5551a6ba4734SToby Isaac   PetscValidPointer(numBd, 2);
5552a6ba4734SToby Isaac   *numBd = 0;
5553a6ba4734SToby Isaac   while (b) {++(*numBd); b = b->next;}
5554a6ba4734SToby Isaac   PetscFunctionReturn(0);
5555a6ba4734SToby Isaac }
5556a6ba4734SToby Isaac 
5557a6ba4734SToby Isaac #undef __FUNCT__
5558a6ba4734SToby Isaac #define __FUNCT__ "DMGetBoundary"
5559a6ba4734SToby Isaac /*@C
5560a6ba4734SToby Isaac   DMGetBoundary - Add a boundary condition to the model
5561a6ba4734SToby Isaac 
5562a6ba4734SToby Isaac   Input Parameters:
5563a6ba4734SToby Isaac + dm          - The mesh object
5564a6ba4734SToby Isaac - bd          - The BC number
5565a6ba4734SToby Isaac 
5566a6ba4734SToby Isaac   Output Parameters:
5567a6ba4734SToby Isaac + isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5568a6ba4734SToby Isaac . name        - The BC name
5569a6ba4734SToby Isaac . labelname   - The label defining constrained points
5570a6ba4734SToby Isaac . field       - The field to constrain
5571a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5572a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5573a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5574a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5575a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5576a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5577a6ba4734SToby Isaac 
5578a6ba4734SToby Isaac   Options Database Keys:
5579a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5580a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5581a6ba4734SToby Isaac 
5582a6ba4734SToby Isaac   Level: developer
5583a6ba4734SToby Isaac 
5584a6ba4734SToby Isaac .seealso: DMAddBoundary()
5585a6ba4734SToby Isaac @*/
5586a6ba4734SToby Isaac PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, PetscBool *isEssential, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(), PetscInt *numids, const PetscInt **ids, void **ctx)
5587a6ba4734SToby Isaac {
5588*bcc5ffd9SToby Isaac   DMBoundary b    = dm->boundary->next;
5589a6ba4734SToby Isaac   PetscInt   n    = 0;
5590a6ba4734SToby Isaac 
5591a6ba4734SToby Isaac   PetscFunctionBegin;
5592a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5593a6ba4734SToby Isaac   while (b) {
5594a6ba4734SToby Isaac     if (n == bd) break;
5595a6ba4734SToby Isaac     b = b->next;
5596a6ba4734SToby Isaac     ++n;
5597a6ba4734SToby Isaac   }
5598a6ba4734SToby Isaac   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
5599a6ba4734SToby Isaac   if (isEssential) {
5600a6ba4734SToby Isaac     PetscValidPointer(isEssential, 3);
5601a6ba4734SToby Isaac     *isEssential = b->essential;
5602a6ba4734SToby Isaac   }
5603a6ba4734SToby Isaac   if (name) {
5604a6ba4734SToby Isaac     PetscValidPointer(name, 4);
5605a6ba4734SToby Isaac     *name = b->name;
5606a6ba4734SToby Isaac   }
5607a6ba4734SToby Isaac   if (labelname) {
5608a6ba4734SToby Isaac     PetscValidPointer(labelname, 5);
5609a6ba4734SToby Isaac     *labelname = b->labelname;
5610a6ba4734SToby Isaac   }
5611a6ba4734SToby Isaac   if (field) {
5612a6ba4734SToby Isaac     PetscValidPointer(field, 6);
5613a6ba4734SToby Isaac     *field = b->field;
5614a6ba4734SToby Isaac   }
5615a6ba4734SToby Isaac   if (numcomps) {
5616a6ba4734SToby Isaac     PetscValidPointer(numcomps, 7);
5617a6ba4734SToby Isaac     *numcomps = b->numcomps;
5618a6ba4734SToby Isaac   }
5619a6ba4734SToby Isaac   if (comps) {
5620a6ba4734SToby Isaac     PetscValidPointer(comps, 8);
5621a6ba4734SToby Isaac     *comps = b->comps;
5622a6ba4734SToby Isaac   }
5623a6ba4734SToby Isaac   if (func) {
5624a6ba4734SToby Isaac     PetscValidPointer(func, 9);
5625a6ba4734SToby Isaac     *func = b->func;
5626a6ba4734SToby Isaac   }
5627a6ba4734SToby Isaac   if (numids) {
5628a6ba4734SToby Isaac     PetscValidPointer(numids, 10);
5629a6ba4734SToby Isaac     *numids = b->numids;
5630a6ba4734SToby Isaac   }
5631a6ba4734SToby Isaac   if (ids) {
5632a6ba4734SToby Isaac     PetscValidPointer(ids, 11);
5633a6ba4734SToby Isaac     *ids = b->ids;
5634a6ba4734SToby Isaac   }
5635a6ba4734SToby Isaac   if (ctx) {
5636a6ba4734SToby Isaac     PetscValidPointer(ctx, 12);
5637a6ba4734SToby Isaac     *ctx = b->ctx;
5638a6ba4734SToby Isaac   }
5639a6ba4734SToby Isaac   PetscFunctionReturn(0);
5640a6ba4734SToby Isaac }
5641a6ba4734SToby Isaac 
5642a6ba4734SToby Isaac #undef __FUNCT__
5643a6ba4734SToby Isaac #define __FUNCT__ "DMIsBoundaryPoint"
5644a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
5645a6ba4734SToby Isaac {
5646*bcc5ffd9SToby Isaac   DMBoundary     b    = dm->boundary->next;
5647a6ba4734SToby Isaac   PetscErrorCode ierr;
5648a6ba4734SToby Isaac 
5649a6ba4734SToby Isaac   PetscFunctionBegin;
5650a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5651a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
5652a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
5653a6ba4734SToby Isaac   while (b && !(*isBd)) {
5654a6ba4734SToby Isaac     if (b->label) {
5655a6ba4734SToby Isaac       PetscInt i;
5656a6ba4734SToby Isaac 
5657a6ba4734SToby Isaac       for (i = 0; i < b->numids && !(*isBd); ++i) {
5658a6ba4734SToby Isaac         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
5659a6ba4734SToby Isaac       }
5660a6ba4734SToby Isaac     }
5661a6ba4734SToby Isaac     b = b->next;
5662a6ba4734SToby Isaac   }
5663a6ba4734SToby Isaac   PetscFunctionReturn(0);
5664a6ba4734SToby Isaac }
56654d6f44ffSToby Isaac 
56664d6f44ffSToby Isaac #undef __FUNCT__
56674d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunction"
56684d6f44ffSToby Isaac /*@C
56694d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
56704d6f44ffSToby Isaac 
56714d6f44ffSToby Isaac   Input Parameters:
56724d6f44ffSToby Isaac + dm      - The DM
56734d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
56744d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
56754d6f44ffSToby Isaac - mode    - The insertion mode for values
56764d6f44ffSToby Isaac 
56774d6f44ffSToby Isaac   Output Parameter:
56784d6f44ffSToby Isaac . X - vector
56794d6f44ffSToby Isaac 
56804d6f44ffSToby Isaac    Calling sequence of func:
56814d6f44ffSToby Isaac $    func(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
56824d6f44ffSToby Isaac 
56834d6f44ffSToby Isaac +  dim - The spatial dimension
56844d6f44ffSToby Isaac .  x   - The coordinates
56854d6f44ffSToby Isaac .  Nf  - The number of fields
56864d6f44ffSToby Isaac .  u   - The output field values
56874d6f44ffSToby Isaac -  ctx - optional user-defined function context
56884d6f44ffSToby Isaac 
56894d6f44ffSToby Isaac   Level: developer
56904d6f44ffSToby Isaac 
56912716604bSToby Isaac .seealso: DMComputeL2Diff()
56924d6f44ffSToby Isaac @*/
56934d6f44ffSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
56944d6f44ffSToby Isaac {
56954d6f44ffSToby Isaac   Vec            localX;
56964d6f44ffSToby Isaac   PetscErrorCode ierr;
56974d6f44ffSToby Isaac 
56984d6f44ffSToby Isaac   PetscFunctionBegin;
56994d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57004d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
57014d6f44ffSToby Isaac   ierr = DMProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
57024d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
57034d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
57044d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
57054d6f44ffSToby Isaac   PetscFunctionReturn(0);
57064d6f44ffSToby Isaac }
57074d6f44ffSToby Isaac 
57084d6f44ffSToby Isaac #undef __FUNCT__
57094d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal"
57104d6f44ffSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
57114d6f44ffSToby Isaac {
57124d6f44ffSToby Isaac   PetscErrorCode ierr;
57134d6f44ffSToby Isaac 
57144d6f44ffSToby Isaac   PetscFunctionBegin;
57154d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57164d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
57174d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
57184d6f44ffSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
57194d6f44ffSToby Isaac   PetscFunctionReturn(0);
57204d6f44ffSToby Isaac }
57214d6f44ffSToby Isaac 
57224d6f44ffSToby Isaac #undef __FUNCT__
57234d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLabelLocal"
57244d6f44ffSToby Isaac PetscErrorCode DMProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
57254d6f44ffSToby Isaac {
57264d6f44ffSToby Isaac   PetscErrorCode ierr;
57274d6f44ffSToby Isaac 
57284d6f44ffSToby Isaac   PetscFunctionBegin;
57294d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57304d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
57314d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
57324d6f44ffSToby Isaac   ierr = (dm->ops->projectfunctionlabellocal) (dm, label, numIds, ids, funcs, ctxs, mode, localX);CHKERRQ(ierr);
57334d6f44ffSToby Isaac   PetscFunctionReturn(0);
57344d6f44ffSToby Isaac }
57352716604bSToby Isaac 
57362716604bSToby Isaac #undef __FUNCT__
57372716604bSToby Isaac #define __FUNCT__ "DMComputeL2Diff"
57382716604bSToby Isaac /*@C
57392716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
57402716604bSToby Isaac 
57412716604bSToby Isaac   Input Parameters:
57422716604bSToby Isaac + dm    - The DM
57432716604bSToby Isaac . funcs - The functions to evaluate for each field component
57442716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
57452716604bSToby Isaac - X     - The coefficient vector u_h
57462716604bSToby Isaac 
57472716604bSToby Isaac   Output Parameter:
57482716604bSToby Isaac . diff - The diff ||u - u_h||_2
57492716604bSToby Isaac 
57502716604bSToby Isaac   Level: developer
57512716604bSToby Isaac 
57522716604bSToby Isaac .seealso: DMProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff()
57532716604bSToby Isaac @*/
57542716604bSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
57552716604bSToby Isaac {
57562716604bSToby Isaac   PetscErrorCode ierr;
57572716604bSToby Isaac 
57582716604bSToby Isaac   PetscFunctionBegin;
57592716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57602716604bSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,4);
57612716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
57622716604bSToby Isaac   ierr = (dm->ops->computel2diff)(dm,funcs,ctxs,X,diff);CHKERRQ(ierr);
57632716604bSToby Isaac   PetscFunctionReturn(0);
57642716604bSToby Isaac }
5765