xref: /petsc/src/dm/interface/dm.c (revision 2ca110d9ff3e1e60e87ab3c8f99929f34cd247e6)
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;
73ad4599aSBarry Smith PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_CreateInterpolation, DM_CreateRestriction;
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);
74bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
75c58f1c22SToby Isaac   v->labels->refct = 1;
76bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->boundary));CHKERRQ(ierr);
77bcc5ffd9SToby 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   }
1203f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
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);
125be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
126be4c1c3eSMatthew G. Knepley     DM           ncdm;
127be4c1c3eSMatthew G. Knepley     PetscSection cs;
128be4c1c3eSMatthew G. Knepley     PetscInt     pEnd = -1;
129be4c1c3eSMatthew G. Knepley 
130be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
131be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
132be4c1c3eSMatthew G. Knepley     if (pEnd >= 0) {
133be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
13413bffc4cSMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
135a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
136be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
137be4c1c3eSMatthew G. Knepley     }
138be4c1c3eSMatthew G. Knepley   }
13938221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   if (coords) {
14138221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
14238221697SMatthew G. Knepley   } else {
14338221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14438221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14538221697SMatthew G. Knepley   }
146c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
147c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1485dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1495dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1505dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
151c6b900c6SMatthew G. Knepley   }
15238221697SMatthew G. Knepley   PetscFunctionReturn(0);
15338221697SMatthew G. Knepley }
15438221697SMatthew G. Knepley 
15538221697SMatthew G. Knepley #undef __FUNCT__
1569a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1579a42bb27SBarry Smith /*@C
158564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1599a42bb27SBarry Smith 
1608472ad0fSDave May    Logically Collective on DM
1619a42bb27SBarry Smith 
1629a42bb27SBarry Smith    Input Parameter:
1639a42bb27SBarry Smith +  da - initial distributed array
1648154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1659a42bb27SBarry Smith 
1669a42bb27SBarry Smith    Options Database:
167dd85299cSBarry Smith .   -dm_vec_type ctype
1689a42bb27SBarry Smith 
1699a42bb27SBarry Smith    Level: intermediate
1709a42bb27SBarry Smith 
1718472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1729a42bb27SBarry Smith @*/
17319fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1749a42bb27SBarry Smith {
1759a42bb27SBarry Smith   PetscErrorCode ierr;
1769a42bb27SBarry Smith 
1779a42bb27SBarry Smith   PetscFunctionBegin;
1789a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1799a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
18019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1819a42bb27SBarry Smith   PetscFunctionReturn(0);
1829a42bb27SBarry Smith }
1839a42bb27SBarry Smith 
1849a42bb27SBarry Smith #undef __FUNCT__
185c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
186c0dedaeaSBarry Smith /*@C
187c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
188c0dedaeaSBarry Smith 
1898472ad0fSDave May    Logically Collective on DM
190c0dedaeaSBarry Smith 
191c0dedaeaSBarry Smith    Input Parameter:
192c0dedaeaSBarry Smith .  da - initial distributed array
193c0dedaeaSBarry Smith 
194c0dedaeaSBarry Smith    Output Parameter:
195c0dedaeaSBarry Smith .  ctype - the vector type
196c0dedaeaSBarry Smith 
197c0dedaeaSBarry Smith    Level: intermediate
198c0dedaeaSBarry Smith 
1998472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
200c0dedaeaSBarry Smith @*/
201c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
202c0dedaeaSBarry Smith {
203c0dedaeaSBarry Smith   PetscFunctionBegin;
204c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
205c0dedaeaSBarry Smith   *ctype = da->vectype;
206c0dedaeaSBarry Smith   PetscFunctionReturn(0);
207c0dedaeaSBarry Smith }
208c0dedaeaSBarry Smith 
209c0dedaeaSBarry Smith #undef __FUNCT__
2105f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
2115f1ad066SMatthew G Knepley /*@
21234f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2135f1ad066SMatthew G Knepley 
2145f1ad066SMatthew G Knepley   Not collective
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley   Input Parameter:
2175f1ad066SMatthew G Knepley . v - The Vec
2185f1ad066SMatthew G Knepley 
2195f1ad066SMatthew G Knepley   Output Parameter:
2205f1ad066SMatthew G Knepley . dm - The DM
2215f1ad066SMatthew G Knepley 
2225f1ad066SMatthew G Knepley   Level: intermediate
2235f1ad066SMatthew G Knepley 
2245f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2255f1ad066SMatthew G Knepley @*/
2265f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2275f1ad066SMatthew G Knepley {
2285f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley   PetscFunctionBegin;
2315f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2325f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2335f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2345f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2355f1ad066SMatthew G Knepley }
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley #undef __FUNCT__
2385f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2395f1ad066SMatthew G Knepley /*@
240d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2415f1ad066SMatthew G Knepley 
2425f1ad066SMatthew G Knepley   Not collective
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley   Input Parameters:
2455f1ad066SMatthew G Knepley + v - The Vec
2465f1ad066SMatthew G Knepley - dm - The DM
2475f1ad066SMatthew G Knepley 
248d9805387SMatthew 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.
249d9805387SMatthew G. Knepley 
2505f1ad066SMatthew G Knepley   Level: intermediate
2515f1ad066SMatthew G Knepley 
2525f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2535f1ad066SMatthew G Knepley @*/
2545f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2555f1ad066SMatthew G Knepley {
2565f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley   PetscFunctionBegin;
2595f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
260d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2615f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2625f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2635f1ad066SMatthew G Knepley }
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley #undef __FUNCT__
266521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
267521d9a4cSLisandro Dalcin /*@C
268521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
269521d9a4cSLisandro Dalcin 
270521d9a4cSLisandro Dalcin    Logically Collective on DM
271521d9a4cSLisandro Dalcin 
272521d9a4cSLisandro Dalcin    Input Parameter:
273521d9a4cSLisandro Dalcin +  dm - the DM context
274521d9a4cSLisandro Dalcin .  ctype - the matrix type
275521d9a4cSLisandro Dalcin 
276521d9a4cSLisandro Dalcin    Options Database:
277521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
278521d9a4cSLisandro Dalcin 
279521d9a4cSLisandro Dalcin    Level: intermediate
280521d9a4cSLisandro Dalcin 
281c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
282521d9a4cSLisandro Dalcin @*/
28319fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
284521d9a4cSLisandro Dalcin {
285521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
28688f0584fSBarry Smith 
287521d9a4cSLisandro Dalcin   PetscFunctionBegin;
288521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
289521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
29019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
291521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
292521d9a4cSLisandro Dalcin }
293521d9a4cSLisandro Dalcin 
294521d9a4cSLisandro Dalcin #undef __FUNCT__
295c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
296c0dedaeaSBarry Smith /*@C
297c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
298c0dedaeaSBarry Smith 
299c0dedaeaSBarry Smith    Logically Collective on DM
300c0dedaeaSBarry Smith 
301c0dedaeaSBarry Smith    Input Parameter:
302c0dedaeaSBarry Smith .  dm - the DM context
303c0dedaeaSBarry Smith 
304c0dedaeaSBarry Smith    Output Parameter:
305c0dedaeaSBarry Smith .  ctype - the matrix type
306c0dedaeaSBarry Smith 
307c0dedaeaSBarry Smith    Options Database:
308c0dedaeaSBarry Smith .   -dm_mat_type ctype
309c0dedaeaSBarry Smith 
310c0dedaeaSBarry Smith    Level: intermediate
311c0dedaeaSBarry Smith 
312c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
313c0dedaeaSBarry Smith @*/
314c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
315c0dedaeaSBarry Smith {
316c0dedaeaSBarry Smith   PetscFunctionBegin;
317c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
318c0dedaeaSBarry Smith   *ctype = dm->mattype;
319c0dedaeaSBarry Smith   PetscFunctionReturn(0);
320c0dedaeaSBarry Smith }
321c0dedaeaSBarry Smith 
322c0dedaeaSBarry Smith #undef __FUNCT__
323c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
324c688c046SMatthew G Knepley /*@
32534f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
326c688c046SMatthew G Knepley 
327c688c046SMatthew G Knepley   Not collective
328c688c046SMatthew G Knepley 
329c688c046SMatthew G Knepley   Input Parameter:
330c688c046SMatthew G Knepley . A - The Mat
331c688c046SMatthew G Knepley 
332c688c046SMatthew G Knepley   Output Parameter:
333c688c046SMatthew G Knepley . dm - The DM
334c688c046SMatthew G Knepley 
335c688c046SMatthew G Knepley   Level: intermediate
336c688c046SMatthew G Knepley 
337c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
338c688c046SMatthew G Knepley @*/
339c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
340c688c046SMatthew G Knepley {
341c688c046SMatthew G Knepley   PetscErrorCode ierr;
342c688c046SMatthew G Knepley 
343c688c046SMatthew G Knepley   PetscFunctionBegin;
344c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
345c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
346c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
347c688c046SMatthew G Knepley   PetscFunctionReturn(0);
348c688c046SMatthew G Knepley }
349c688c046SMatthew G Knepley 
350c688c046SMatthew G Knepley #undef __FUNCT__
351c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
352c688c046SMatthew G Knepley /*@
353c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
354c688c046SMatthew G Knepley 
355c688c046SMatthew G Knepley   Not collective
356c688c046SMatthew G Knepley 
357c688c046SMatthew G Knepley   Input Parameters:
358c688c046SMatthew G Knepley + A - The Mat
359c688c046SMatthew G Knepley - dm - The DM
360c688c046SMatthew G Knepley 
361c688c046SMatthew G Knepley   Level: intermediate
362c688c046SMatthew G Knepley 
363c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
364c688c046SMatthew G Knepley @*/
365c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
366c688c046SMatthew G Knepley {
367c688c046SMatthew G Knepley   PetscErrorCode ierr;
368c688c046SMatthew G Knepley 
369c688c046SMatthew G Knepley   PetscFunctionBegin;
370c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3718865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
372c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
373c688c046SMatthew G Knepley   PetscFunctionReturn(0);
374c688c046SMatthew G Knepley }
375c688c046SMatthew G Knepley 
376c688c046SMatthew G Knepley #undef __FUNCT__
3779a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3789a42bb27SBarry Smith /*@C
3799a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3806757b960SDave May    DM options in the database.
3819a42bb27SBarry Smith 
3828353ddbbSDave May    Logically Collective on DM
3839a42bb27SBarry Smith 
3849a42bb27SBarry Smith    Input Parameter:
3858353ddbbSDave May +  da - the DM context
3869a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3879a42bb27SBarry Smith 
3889a42bb27SBarry Smith    Notes:
3899a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3909a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3919a42bb27SBarry Smith 
3929a42bb27SBarry Smith    Level: advanced
3939a42bb27SBarry Smith 
3948353ddbbSDave May .keywords: DM, set, options, prefix, database
3959a42bb27SBarry Smith 
3969a42bb27SBarry Smith .seealso: DMSetFromOptions()
3979a42bb27SBarry Smith @*/
3987087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3999a42bb27SBarry Smith {
4009a42bb27SBarry Smith   PetscErrorCode ierr;
4019a42bb27SBarry Smith 
4029a42bb27SBarry Smith   PetscFunctionBegin;
4039a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4049a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
405691be533SLawrence Mitchell   if (dm->sf) {
406691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
407691be533SLawrence Mitchell   }
408691be533SLawrence Mitchell   if (dm->defaultSF) {
409691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
410691be533SLawrence Mitchell   }
4119a42bb27SBarry Smith   PetscFunctionReturn(0);
4129a42bb27SBarry Smith }
4139a42bb27SBarry Smith 
4149a42bb27SBarry Smith #undef __FUNCT__
41531697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
41631697293SDave May /*@C
41731697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
41831697293SDave May    DM options in the database.
41931697293SDave May 
42031697293SDave May    Logically Collective on DM
42131697293SDave May 
42231697293SDave May    Input Parameters:
42331697293SDave May +  dm - the DM context
42431697293SDave May -  prefix - the prefix string to prepend to all DM option requests
42531697293SDave May 
42631697293SDave May    Notes:
42731697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
42831697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
42931697293SDave May 
43031697293SDave May    Level: advanced
43131697293SDave May 
43231697293SDave May .keywords: DM, append, options, prefix, database
43331697293SDave May 
43431697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
43531697293SDave May @*/
43631697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
43731697293SDave May {
43831697293SDave May   PetscErrorCode ierr;
43931697293SDave May 
44031697293SDave May   PetscFunctionBegin;
44131697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44231697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
44331697293SDave May   PetscFunctionReturn(0);
44431697293SDave May }
44531697293SDave May 
44631697293SDave May #undef __FUNCT__
44731697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
44831697293SDave May /*@C
44931697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
45031697293SDave May    DM options in the database.
45131697293SDave May 
45231697293SDave May    Not Collective
45331697293SDave May 
45431697293SDave May    Input Parameters:
45531697293SDave May .  dm - the DM context
45631697293SDave May 
45731697293SDave May    Output Parameters:
45831697293SDave May .  prefix - pointer to the prefix string used is returned
45931697293SDave May 
46031697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
46131697293SDave May    sufficient length to hold the prefix.
46231697293SDave May 
46331697293SDave May    Level: advanced
46431697293SDave May 
46531697293SDave May .keywords: DM, set, options, prefix, database
46631697293SDave May 
46731697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
46831697293SDave May @*/
46931697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
47031697293SDave May {
47131697293SDave May   PetscErrorCode ierr;
47231697293SDave May 
47331697293SDave May   PetscFunctionBegin;
47431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47531697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
47631697293SDave May   PetscFunctionReturn(0);
47731697293SDave May }
47831697293SDave May 
47931697293SDave May #undef __FUNCT__
48088bdff64SToby Isaac #define __FUNCT__ "DMCountNonCyclicReferences"
48188bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
48288bdff64SToby Isaac {
48388bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
48488bdff64SToby Isaac   DMNamedVecLink nlink;
48588bdff64SToby Isaac   PetscErrorCode ierr;
48688bdff64SToby Isaac 
48788bdff64SToby Isaac   PetscFunctionBegin;
48888bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
48988bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
49088bdff64SToby Isaac     if (dm->localin[i])  refct--;
49188bdff64SToby Isaac     if (dm->globalin[i]) refct--;
49288bdff64SToby Isaac   }
49388bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
49488bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
49588bdff64SToby Isaac   if (dm->x) {
49688bdff64SToby Isaac     DM obj;
49788bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
49888bdff64SToby Isaac     if (obj == dm) refct--;
49988bdff64SToby Isaac   }
50088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
50188bdff64SToby Isaac     refct--;
50288bdff64SToby Isaac     if (recurseCoarse) {
50388bdff64SToby Isaac       PetscInt coarseCount;
50488bdff64SToby Isaac 
50588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
50688bdff64SToby Isaac       refct += coarseCount;
50788bdff64SToby Isaac     }
50888bdff64SToby Isaac   }
50988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
51088bdff64SToby Isaac     refct--;
51188bdff64SToby Isaac     if (recurseFine) {
51288bdff64SToby Isaac       PetscInt fineCount;
51388bdff64SToby Isaac 
51488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
51588bdff64SToby Isaac       refct += fineCount;
51688bdff64SToby Isaac     }
51788bdff64SToby Isaac   }
51888bdff64SToby Isaac   *ncrefct = refct;
51988bdff64SToby Isaac   PetscFunctionReturn(0);
52088bdff64SToby Isaac }
52188bdff64SToby Isaac 
522bcc5ffd9SToby Isaac PetscErrorCode DMBoundaryDestroy(DMBoundaryLinkList *boundary);
523a6ba4734SToby Isaac 
52488bdff64SToby Isaac #undef __FUNCT__
525354557abSToby Isaac #define __FUNCT__ "DMDestroyLabelLinkList"
526354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
527354557abSToby Isaac {
528354557abSToby Isaac   PetscErrorCode ierr;
529354557abSToby Isaac 
530354557abSToby Isaac   PetscFunctionBegin;
531354557abSToby Isaac   if (!--(dm->labels->refct)) {
532354557abSToby Isaac     DMLabelLink next = dm->labels->next;
533354557abSToby Isaac 
534354557abSToby Isaac     /* destroy the labels */
535354557abSToby Isaac     while (next) {
536354557abSToby Isaac       DMLabelLink tmp = next->next;
537354557abSToby Isaac 
538354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
539354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
540354557abSToby Isaac       next = tmp;
541354557abSToby Isaac     }
542354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
543354557abSToby Isaac   }
544354557abSToby Isaac   PetscFunctionReturn(0);
545354557abSToby Isaac }
546354557abSToby Isaac 
547354557abSToby Isaac #undef __FUNCT__
54847c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
54947c6ae99SBarry Smith /*@
5508472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
55147c6ae99SBarry Smith 
55247c6ae99SBarry Smith     Collective on DM
55347c6ae99SBarry Smith 
55447c6ae99SBarry Smith     Input Parameter:
55547c6ae99SBarry Smith .   dm - the DM object to destroy
55647c6ae99SBarry Smith 
55747c6ae99SBarry Smith     Level: developer
55847c6ae99SBarry Smith 
559e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
56047c6ae99SBarry Smith 
56147c6ae99SBarry Smith @*/
562fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
56347c6ae99SBarry Smith {
56488bdff64SToby Isaac   PetscInt       i, cnt;
565dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
56647c6ae99SBarry Smith   PetscErrorCode ierr;
56747c6ae99SBarry Smith 
56847c6ae99SBarry Smith   PetscFunctionBegin;
5696bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5706bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
57187e657c6SBarry Smith 
57288bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
57388bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
57488bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
57588bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
576732e2eb9SMatthew G Knepley   /*
577732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
578732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
579732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
580732e2eb9SMatthew G Knepley   */
5816bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5826bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
583732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5846bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5856bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
586732e2eb9SMatthew G Knepley   }
587f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5880298fd71SBarry Smith   (*dm)->namedglobal = NULL;
589f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5902348bcf4SPeter Brune     nnext = nlink->next;
5912348bcf4SPeter 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);
5922348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5932348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5942348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5952348bcf4SPeter Brune   }
596f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5970298fd71SBarry Smith   (*dm)->namedlocal = NULL;
598f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
599f490541aSPeter Brune     nnext = nlink->next;
600f490541aSPeter 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);
601f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
602f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
603f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
604f490541aSPeter Brune   }
6052348bcf4SPeter Brune 
606b17ce1afSJed Brown   /* Destroy the list of hooks */
607c833c3b5SJed Brown   {
608c833c3b5SJed Brown     DMCoarsenHookLink link,next;
609b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
610b17ce1afSJed Brown       next = link->next;
611b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
612b17ce1afSJed Brown     }
6130298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
614c833c3b5SJed Brown   }
615c833c3b5SJed Brown   {
616c833c3b5SJed Brown     DMRefineHookLink link,next;
617c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
618c833c3b5SJed Brown       next = link->next;
619c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
620c833c3b5SJed Brown     }
6210298fd71SBarry Smith     (*dm)->refinehook = NULL;
622c833c3b5SJed Brown   }
623be081cd6SPeter Brune   {
624be081cd6SPeter Brune     DMSubDomainHookLink link,next;
625be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
626be081cd6SPeter Brune       next = link->next;
627be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
628be081cd6SPeter Brune     }
6290298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
630be081cd6SPeter Brune   }
631baf369e7SPeter Brune   {
632baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
633baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
634baf369e7SPeter Brune       next = link->next;
635baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
636baf369e7SPeter Brune     }
6370298fd71SBarry Smith     (*dm)->gtolhook = NULL;
638baf369e7SPeter Brune   }
639d4d07f1eSToby Isaac   {
640d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
641d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
642d4d07f1eSToby Isaac       next = link->next;
643d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
644d4d07f1eSToby Isaac     }
645d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
646d4d07f1eSToby Isaac   }
647aa1993deSMatthew G Knepley   /* Destroy the work arrays */
648aa1993deSMatthew G Knepley   {
649aa1993deSMatthew G Knepley     DMWorkLink link,next;
650aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
651aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
652aa1993deSMatthew G Knepley       next = link->next;
653aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
654aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
655aa1993deSMatthew G Knepley     }
6560298fd71SBarry Smith     (*dm)->workin = NULL;
657aa1993deSMatthew G Knepley   }
658c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
659c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
660c58f1c22SToby Isaac 
661c58f1c22SToby Isaac     /* destroy the labels */
662c58f1c22SToby Isaac     while (next) {
663c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
664c58f1c22SToby Isaac 
665c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
666c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
667c58f1c22SToby Isaac       next = tmp;
668c58f1c22SToby Isaac     }
669c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
670c58f1c22SToby Isaac   }
671bcc5ffd9SToby Isaac   ierr = DMBoundaryDestroy(&(*dm)->boundary);CHKERRQ(ierr);
672b17ce1afSJed Brown 
67352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
67452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
67552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
67652536dc3SBarry Smith 
6771a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6781a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6791a266240SBarry Smith   }
68087e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
68171cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6824dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6836bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6846bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
685073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
68688ed4aceSMatthew G Knepley 
68788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
68888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6898b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
690fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
691fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
69288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
69388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6948e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
695af122d2aSMatthew G Knepley 
69688bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
69788bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
69888bdff64SToby Isaac   }
699a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
70088bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
70188bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
70288bdff64SToby Isaac   }
70388bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
7046636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7056636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7066636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7075dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
7086636e97aSMatthew G Knepley 
7092764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
71014f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
711e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
712e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
713732e2eb9SMatthew G Knepley 
7146bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
715435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
716732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
71747c6ae99SBarry Smith   PetscFunctionReturn(0);
71847c6ae99SBarry Smith }
71947c6ae99SBarry Smith 
72047c6ae99SBarry Smith #undef __FUNCT__
721d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
722d7bf68aeSBarry Smith /*@
723d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
724d7bf68aeSBarry Smith 
725d7bf68aeSBarry Smith     Collective on DM
726d7bf68aeSBarry Smith 
727d7bf68aeSBarry Smith     Input Parameter:
728d7bf68aeSBarry Smith .   dm - the DM object to setup
729d7bf68aeSBarry Smith 
730d7bf68aeSBarry Smith     Level: developer
731d7bf68aeSBarry Smith 
732e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
733d7bf68aeSBarry Smith 
734d7bf68aeSBarry Smith @*/
7357087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
736d7bf68aeSBarry Smith {
737d7bf68aeSBarry Smith   PetscErrorCode ierr;
738d7bf68aeSBarry Smith 
739d7bf68aeSBarry Smith   PetscFunctionBegin;
740171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7418387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
742d7bf68aeSBarry Smith   if (dm->ops->setup) {
743d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
744d7bf68aeSBarry Smith   }
7458387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
746d7bf68aeSBarry Smith   PetscFunctionReturn(0);
747d7bf68aeSBarry Smith }
748d7bf68aeSBarry Smith 
749d7bf68aeSBarry Smith #undef __FUNCT__
750d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
751d7bf68aeSBarry Smith /*@
752d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
753d7bf68aeSBarry Smith 
754d7bf68aeSBarry Smith     Collective on DM
755d7bf68aeSBarry Smith 
756d7bf68aeSBarry Smith     Input Parameter:
757d7bf68aeSBarry Smith .   dm - the DM object to set options for
758d7bf68aeSBarry Smith 
759732e2eb9SMatthew G Knepley     Options Database:
7604164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7614164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7624164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7634164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
764732e2eb9SMatthew G Knepley 
765d7bf68aeSBarry Smith     Level: developer
766d7bf68aeSBarry Smith 
767e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
768d7bf68aeSBarry Smith 
769d7bf68aeSBarry Smith @*/
7707087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
771d7bf68aeSBarry Smith {
7727781c08eSBarry Smith   char           typeName[256];
773ca266f36SBarry Smith   PetscBool      flg;
774d7bf68aeSBarry Smith   PetscErrorCode ierr;
775d7bf68aeSBarry Smith 
776d7bf68aeSBarry Smith   PetscFunctionBegin;
777171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
778691be533SLawrence Mitchell   if (dm->sf) {
779691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
780691be533SLawrence Mitchell   }
781691be533SLawrence Mitchell   if (dm->defaultSF) {
782691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
783691be533SLawrence Mitchell   }
7843194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7850298fd71SBarry 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);
786a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
787f9ba7244SBarry Smith   if (flg) {
788f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
789f9ba7244SBarry Smith   }
790a264d7a6SBarry 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);
791073dac72SJed Brown   if (flg) {
792521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
793073dac72SJed Brown   }
7940298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
795f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
796e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
797f9ba7244SBarry Smith   }
798f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7990633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
80082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
801d7bf68aeSBarry Smith   PetscFunctionReturn(0);
802d7bf68aeSBarry Smith }
803d7bf68aeSBarry Smith 
804d7bf68aeSBarry Smith #undef __FUNCT__
80547c6ae99SBarry Smith #define __FUNCT__ "DMView"
806fc9bc008SSatish Balay /*@C
807224748a4SBarry Smith     DMView - Views a DM
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith     Collective on DM
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith     Input Parameter:
81247c6ae99SBarry Smith +   dm - the DM object to view
81347c6ae99SBarry Smith -   v - the viewer
81447c6ae99SBarry Smith 
815224748a4SBarry Smith     Level: beginner
81647c6ae99SBarry Smith 
817e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith @*/
8207087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
82147c6ae99SBarry Smith {
82247c6ae99SBarry Smith   PetscErrorCode ierr;
82332c0f0efSBarry Smith   PetscBool      isbinary;
82447c6ae99SBarry Smith 
82547c6ae99SBarry Smith   PetscFunctionBegin;
826171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8273014e516SBarry Smith   if (!v) {
828ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8293014e516SBarry Smith   }
83098c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
83132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
83232c0f0efSBarry Smith   if (isbinary) {
83355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
83432c0f0efSBarry Smith     char     type[256];
83532c0f0efSBarry Smith 
83632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
83732c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
83832c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
83932c0f0efSBarry Smith   }
8400c010503SBarry Smith   if (dm->ops->view) {
8410c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
84247c6ae99SBarry Smith   }
84347c6ae99SBarry Smith   PetscFunctionReturn(0);
84447c6ae99SBarry Smith }
84547c6ae99SBarry Smith 
84647c6ae99SBarry Smith #undef __FUNCT__
84747c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
84847c6ae99SBarry Smith /*@
8498472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
85047c6ae99SBarry Smith 
85147c6ae99SBarry Smith     Collective on DM
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Input Parameter:
85447c6ae99SBarry Smith .   dm - the DM object
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith     Output Parameter:
85747c6ae99SBarry Smith .   vec - the global vector
85847c6ae99SBarry Smith 
859073dac72SJed Brown     Level: beginner
86047c6ae99SBarry Smith 
861e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith @*/
8647087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
86547c6ae99SBarry Smith {
86647c6ae99SBarry Smith   PetscErrorCode ierr;
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith   PetscFunctionBegin;
869171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87047c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
87147c6ae99SBarry Smith   PetscFunctionReturn(0);
87247c6ae99SBarry Smith }
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith #undef __FUNCT__
87547c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
87647c6ae99SBarry Smith /*@
8778472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith     Not Collective
88047c6ae99SBarry Smith 
88147c6ae99SBarry Smith     Input Parameter:
88247c6ae99SBarry Smith .   dm - the DM object
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith     Output Parameter:
88547c6ae99SBarry Smith .   vec - the local vector
88647c6ae99SBarry Smith 
887073dac72SJed Brown     Level: beginner
88847c6ae99SBarry Smith 
889e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
89047c6ae99SBarry Smith 
89147c6ae99SBarry Smith @*/
8927087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
89347c6ae99SBarry Smith {
89447c6ae99SBarry Smith   PetscErrorCode ierr;
89547c6ae99SBarry Smith 
89647c6ae99SBarry Smith   PetscFunctionBegin;
897171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89847c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
89947c6ae99SBarry Smith   PetscFunctionReturn(0);
90047c6ae99SBarry Smith }
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith #undef __FUNCT__
9031411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
9041411c6eeSJed Brown /*@
9051411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9061411c6eeSJed Brown 
9071411c6eeSJed Brown    Collective on DM
9081411c6eeSJed Brown 
9091411c6eeSJed Brown    Input Parameter:
9101411c6eeSJed Brown .  dm - the DM that provides the mapping
9111411c6eeSJed Brown 
9121411c6eeSJed Brown    Output Parameter:
9131411c6eeSJed Brown .  ltog - the mapping
9141411c6eeSJed Brown 
9151411c6eeSJed Brown    Level: intermediate
9161411c6eeSJed Brown 
9171411c6eeSJed Brown    Notes:
9181411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9191411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9201411c6eeSJed Brown 
921fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9221411c6eeSJed Brown @*/
9237087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9241411c6eeSJed Brown {
9251411c6eeSJed Brown   PetscErrorCode ierr;
9261411c6eeSJed Brown 
9271411c6eeSJed Brown   PetscFunctionBegin;
9281411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9291411c6eeSJed Brown   PetscValidPointer(ltog,2);
9301411c6eeSJed Brown   if (!dm->ltogmap) {
93137d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
93237d0c07bSMatthew G Knepley 
93337d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
93437d0c07bSMatthew G Knepley     if (section) {
93537d0c07bSMatthew G Knepley       PetscInt *ltog;
93637d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
93737d0c07bSMatthew G Knepley 
93837d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
93937d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
94037d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
941785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
94237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
94337d0c07bSMatthew G Knepley         PetscInt dof, off, c;
94437d0c07bSMatthew G Knepley 
94537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
94637d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
94737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
94837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
94937d0c07bSMatthew G Knepley           ltog[l] = off+c;
95037d0c07bSMatthew G Knepley         }
95137d0c07bSMatthew G Knepley       }
952f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9533bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
95437d0c07bSMatthew G Knepley     } else {
955184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
956184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9571411c6eeSJed Brown     }
95837d0c07bSMatthew G Knepley   }
9591411c6eeSJed Brown   *ltog = dm->ltogmap;
9601411c6eeSJed Brown   PetscFunctionReturn(0);
9611411c6eeSJed Brown }
9621411c6eeSJed Brown 
9631411c6eeSJed Brown #undef __FUNCT__
9641411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
9651411c6eeSJed Brown /*@
9661411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9671411c6eeSJed Brown 
9681411c6eeSJed Brown    Not Collective
9691411c6eeSJed Brown 
9701411c6eeSJed Brown    Input Parameter:
9711411c6eeSJed Brown .  dm - the DM with block structure
9721411c6eeSJed Brown 
9731411c6eeSJed Brown    Output Parameter:
9741411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9751411c6eeSJed Brown 
9761411c6eeSJed Brown    Level: intermediate
9771411c6eeSJed Brown 
978fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9791411c6eeSJed Brown @*/
9807087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9811411c6eeSJed Brown {
9821411c6eeSJed Brown   PetscFunctionBegin;
9831411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9841411c6eeSJed Brown   PetscValidPointer(bs,2);
9851411c6eeSJed 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");
9861411c6eeSJed Brown   *bs = dm->bs;
9871411c6eeSJed Brown   PetscFunctionReturn(0);
9881411c6eeSJed Brown }
9891411c6eeSJed Brown 
9901411c6eeSJed Brown #undef __FUNCT__
991e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
99247c6ae99SBarry Smith /*@
9938472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Collective on DM
99647c6ae99SBarry Smith 
99747c6ae99SBarry Smith     Input Parameter:
99847c6ae99SBarry Smith +   dm1 - the DM object
99947c6ae99SBarry Smith -   dm2 - the second, finer DM object
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith     Output Parameter:
100247c6ae99SBarry Smith +  mat - the interpolation
100347c6ae99SBarry Smith -  vec - the scaling (optional)
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith     Level: developer
100647c6ae99SBarry Smith 
100785afcc9aSBarry 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
100885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1009d52bd9f3SBarry Smith 
10101f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1011d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
101285afcc9aSBarry Smith 
101385afcc9aSBarry Smith 
10143ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith @*/
1017e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
101847c6ae99SBarry Smith {
101947c6ae99SBarry Smith   PetscErrorCode ierr;
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith   PetscFunctionBegin;
1022171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1023171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1024cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
102525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1026cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
102747c6ae99SBarry Smith   PetscFunctionReturn(0);
102847c6ae99SBarry Smith }
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith #undef __FUNCT__
10313ad4599aSBarry Smith #define __FUNCT__ "DMCreateRestriction"
10323ad4599aSBarry Smith /*@
10333ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10343ad4599aSBarry Smith 
10353ad4599aSBarry Smith     Collective on DM
10363ad4599aSBarry Smith 
10373ad4599aSBarry Smith     Input Parameter:
10383ad4599aSBarry Smith +   dm1 - the DM object
10393ad4599aSBarry Smith -   dm2 - the second, finer DM object
10403ad4599aSBarry Smith 
10413ad4599aSBarry Smith     Output Parameter:
10423ad4599aSBarry Smith .  mat - the restriction
10433ad4599aSBarry Smith 
10443ad4599aSBarry Smith 
10453ad4599aSBarry Smith     Level: developer
10463ad4599aSBarry Smith 
10473ad4599aSBarry 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
10483ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10493ad4599aSBarry Smith 
10503ad4599aSBarry Smith 
10513ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10523ad4599aSBarry Smith 
10533ad4599aSBarry Smith @*/
10543ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10553ad4599aSBarry Smith {
10563ad4599aSBarry Smith   PetscErrorCode ierr;
10573ad4599aSBarry Smith 
10583ad4599aSBarry Smith   PetscFunctionBegin;
10593ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10603ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10613ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10623ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10633ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10643ad4599aSBarry Smith   PetscFunctionReturn(0);
10653ad4599aSBarry Smith }
10663ad4599aSBarry Smith 
10673ad4599aSBarry Smith #undef __FUNCT__
1068e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
106947c6ae99SBarry Smith /*@
10708472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
107147c6ae99SBarry Smith 
107247c6ae99SBarry Smith     Collective on DM
107347c6ae99SBarry Smith 
107447c6ae99SBarry Smith     Input Parameter:
107547c6ae99SBarry Smith +   dm1 - the DM object
107647c6ae99SBarry Smith -   dm2 - the second, finer DM object
107747c6ae99SBarry Smith 
107847c6ae99SBarry Smith     Output Parameter:
10796dbf9973SLawrence Mitchell .   mat - the injection
108047c6ae99SBarry Smith 
108147c6ae99SBarry Smith     Level: developer
108247c6ae99SBarry Smith 
108385afcc9aSBarry 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
108485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
108585afcc9aSBarry Smith 
1086e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
108747c6ae99SBarry Smith 
108847c6ae99SBarry Smith @*/
10896dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
109047c6ae99SBarry Smith {
109147c6ae99SBarry Smith   PetscErrorCode ierr;
109247c6ae99SBarry Smith 
109347c6ae99SBarry Smith   PetscFunctionBegin;
1094171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1095171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
109623384938SToby Isaac   if (!*dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
10976dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
109847c6ae99SBarry Smith   PetscFunctionReturn(0);
109947c6ae99SBarry Smith }
110047c6ae99SBarry Smith 
110147c6ae99SBarry Smith #undef __FUNCT__
1102e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
1103b412c318SBarry Smith /*@
1104b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
110547c6ae99SBarry Smith 
110647c6ae99SBarry Smith     Collective on DM
110747c6ae99SBarry Smith 
110847c6ae99SBarry Smith     Input Parameter:
110947c6ae99SBarry Smith +   dm - the DM object
1110b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
111147c6ae99SBarry Smith 
111247c6ae99SBarry Smith     Output Parameter:
111347c6ae99SBarry Smith .   coloring - the coloring
111447c6ae99SBarry Smith 
111547c6ae99SBarry Smith     Level: developer
111647c6ae99SBarry Smith 
1117b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
111847c6ae99SBarry Smith 
1119aab9d709SJed Brown @*/
1120b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
112147c6ae99SBarry Smith {
112247c6ae99SBarry Smith   PetscErrorCode ierr;
112347c6ae99SBarry Smith 
112447c6ae99SBarry Smith   PetscFunctionBegin;
1125171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1126ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1127b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
112847c6ae99SBarry Smith   PetscFunctionReturn(0);
112947c6ae99SBarry Smith }
113047c6ae99SBarry Smith 
113147c6ae99SBarry Smith #undef __FUNCT__
1132950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1133b412c318SBarry Smith /*@
11348472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
113547c6ae99SBarry Smith 
113647c6ae99SBarry Smith     Collective on DM
113747c6ae99SBarry Smith 
113847c6ae99SBarry Smith     Input Parameter:
1139b412c318SBarry Smith .   dm - the DM object
114047c6ae99SBarry Smith 
114147c6ae99SBarry Smith     Output Parameter:
114247c6ae99SBarry Smith .   mat - the empty Jacobian
114347c6ae99SBarry Smith 
1144073dac72SJed Brown     Level: beginner
114547c6ae99SBarry Smith 
114694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
114794013140SBarry Smith        do not need to do it yourself.
114894013140SBarry Smith 
114994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1150aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
115194013140SBarry Smith 
115294013140SBarry 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
115394013140SBarry Smith        internally by PETSc.
115494013140SBarry Smith 
115594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1156aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
115794013140SBarry Smith 
1158b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
115947c6ae99SBarry Smith 
1160aab9d709SJed Brown @*/
1161b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
116247c6ae99SBarry Smith {
116347c6ae99SBarry Smith   PetscErrorCode ierr;
116447c6ae99SBarry Smith 
116547c6ae99SBarry Smith   PetscFunctionBegin;
1166171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1167607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1168c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1169c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1170b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
117147c6ae99SBarry Smith   PetscFunctionReturn(0);
117247c6ae99SBarry Smith }
117347c6ae99SBarry Smith 
117447c6ae99SBarry Smith #undef __FUNCT__
1175732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1176732e2eb9SMatthew G Knepley /*@
1177950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1178732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1179732e2eb9SMatthew G Knepley 
11808472ad0fSDave May   Logically Collective on DM
1181732e2eb9SMatthew G Knepley 
1182732e2eb9SMatthew G Knepley   Input Parameter:
1183732e2eb9SMatthew G Knepley + dm - the DM
1184732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1185732e2eb9SMatthew G Knepley 
1186732e2eb9SMatthew G Knepley   Level: developer
1187950540a4SJed Brown .seealso DMCreateMatrix()
1188732e2eb9SMatthew G Knepley @*/
1189732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1190732e2eb9SMatthew G Knepley {
1191732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1192732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1193732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1194732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1195732e2eb9SMatthew G Knepley }
1196732e2eb9SMatthew G Knepley 
1197732e2eb9SMatthew G Knepley #undef __FUNCT__
1198a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1199a89ea682SMatthew G Knepley /*@C
1200aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1201a89ea682SMatthew G Knepley 
1202a89ea682SMatthew G Knepley   Not Collective
1203a89ea682SMatthew G Knepley 
1204a89ea682SMatthew G Knepley   Input Parameters:
1205a89ea682SMatthew G Knepley + dm - the DM object
1206aa1993deSMatthew G Knepley . count - The minium size
1207aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1208a89ea682SMatthew G Knepley 
1209a89ea682SMatthew G Knepley   Output Parameter:
1210a89ea682SMatthew G Knepley . array - the work array
1211a89ea682SMatthew G Knepley 
1212a89ea682SMatthew G Knepley   Level: developer
1213a89ea682SMatthew G Knepley 
1214a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1215a89ea682SMatthew G Knepley @*/
1216aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1217a89ea682SMatthew G Knepley {
1218a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1219aa1993deSMatthew G Knepley   DMWorkLink     link;
1220854ce69bSBarry Smith   size_t         dsize;
1221a89ea682SMatthew G Knepley 
1222a89ea682SMatthew G Knepley   PetscFunctionBegin;
1223a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1224aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1225aa1993deSMatthew G Knepley   if (dm->workin) {
1226aa1993deSMatthew G Knepley     link       = dm->workin;
1227aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1228aa1993deSMatthew G Knepley   } else {
1229b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1230a89ea682SMatthew G Knepley   }
1231854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1232854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1233aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1234854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1235854ce69bSBarry Smith     link->bytes = dsize*count;
1236aa1993deSMatthew G Knepley   }
1237aa1993deSMatthew G Knepley   link->next   = dm->workout;
1238aa1993deSMatthew G Knepley   dm->workout  = link;
1239aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1240a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1241a89ea682SMatthew G Knepley }
1242a89ea682SMatthew G Knepley 
1243aa1993deSMatthew G Knepley #undef __FUNCT__
1244aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1245aa1993deSMatthew G Knepley /*@C
1246aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1247aa1993deSMatthew G Knepley 
1248aa1993deSMatthew G Knepley   Not Collective
1249aa1993deSMatthew G Knepley 
1250aa1993deSMatthew G Knepley   Input Parameters:
1251aa1993deSMatthew G Knepley + dm - the DM object
1252aa1993deSMatthew G Knepley . count - The minium size
1253aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1254aa1993deSMatthew G Knepley 
1255aa1993deSMatthew G Knepley   Output Parameter:
1256aa1993deSMatthew G Knepley . array - the work array
1257aa1993deSMatthew G Knepley 
1258aa1993deSMatthew G Knepley   Level: developer
1259aa1993deSMatthew G Knepley 
1260aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1261aa1993deSMatthew G Knepley @*/
1262aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1263aa1993deSMatthew G Knepley {
1264aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1265aa1993deSMatthew G Knepley 
1266aa1993deSMatthew G Knepley   PetscFunctionBegin;
1267aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1268aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1269aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1270aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1271aa1993deSMatthew G Knepley       *p           = link->next;
1272aa1993deSMatthew G Knepley       link->next   = dm->workin;
1273aa1993deSMatthew G Knepley       dm->workin   = link;
12740298fd71SBarry Smith       *(void**)mem = NULL;
1275aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1276aa1993deSMatthew G Knepley     }
1277aa1993deSMatthew G Knepley   }
1278aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1279aa1993deSMatthew G Knepley }
1280e7c4fc90SDmitry Karpeev 
1281e7c4fc90SDmitry Karpeev #undef __FUNCT__
1282435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1283435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1284435a35e8SMatthew G Knepley {
1285435a35e8SMatthew G Knepley   PetscFunctionBegin;
1286435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
128782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1288435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1289435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1290435a35e8SMatthew G Knepley }
1291435a35e8SMatthew G Knepley 
1292435a35e8SMatthew G Knepley #undef __FUNCT__
12934d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
12944f3b5142SJed Brown /*@C
12954d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12964d343eeaSMatthew G Knepley 
12974d343eeaSMatthew G Knepley   Not collective
12984d343eeaSMatthew G Knepley 
12994d343eeaSMatthew G Knepley   Input Parameter:
13004d343eeaSMatthew G Knepley . dm - the DM object
13014d343eeaSMatthew G Knepley 
13024d343eeaSMatthew G Knepley   Output Parameters:
13030298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13040298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13050298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13064d343eeaSMatthew G Knepley 
13074d343eeaSMatthew G Knepley   Level: intermediate
13084d343eeaSMatthew G Knepley 
130921c9b008SJed Brown   Notes:
131021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
131121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
131221c9b008SJed Brown   PetscFree().
131321c9b008SJed Brown 
13144d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13154d343eeaSMatthew G Knepley @*/
131637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13174d343eeaSMatthew G Knepley {
131837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13194d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13204d343eeaSMatthew G Knepley 
13214d343eeaSMatthew G Knepley   PetscFunctionBegin;
13224d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
132369ca1f37SDmitry Karpeev   if (numFields) {
132469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
132569ca1f37SDmitry Karpeev     *numFields = 0;
132669ca1f37SDmitry Karpeev   }
132737d0c07bSMatthew G Knepley   if (fieldNames) {
132837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13290298fd71SBarry Smith     *fieldNames = NULL;
133069ca1f37SDmitry Karpeev   }
133169ca1f37SDmitry Karpeev   if (fields) {
133269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13330298fd71SBarry Smith     *fields = NULL;
133469ca1f37SDmitry Karpeev   }
133537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
133637d0c07bSMatthew G Knepley   if (section) {
133737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
133837d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
133937d0c07bSMatthew G Knepley 
134037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
134137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1342dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
134337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
134437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
134537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
134637d0c07bSMatthew G Knepley     }
134737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
134837d0c07bSMatthew G Knepley       PetscInt gdof;
134937d0c07bSMatthew G Knepley 
135037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
135137d0c07bSMatthew G Knepley       if (gdof > 0) {
135237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
135337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
135437d0c07bSMatthew G Knepley 
135537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
135637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
135737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
135837d0c07bSMatthew G Knepley         }
135937d0c07bSMatthew G Knepley       }
136037d0c07bSMatthew G Knepley     }
136137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1362785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
136337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
136437d0c07bSMatthew G Knepley     }
136537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
136637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
136737d0c07bSMatthew G Knepley 
136837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
136937d0c07bSMatthew G Knepley       if (gdof > 0) {
137037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
137137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
137237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
137337d0c07bSMatthew G Knepley 
137437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
137537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
137637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
137737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
137837d0c07bSMatthew G Knepley           }
137937d0c07bSMatthew G Knepley         }
138037d0c07bSMatthew G Knepley       }
138137d0c07bSMatthew G Knepley     }
13828865f1eaSKarl Rupp     if (numFields) *numFields = nF;
138337d0c07bSMatthew G Knepley     if (fieldNames) {
1384785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
138537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
138637d0c07bSMatthew G Knepley         const char *fieldName;
138737d0c07bSMatthew G Knepley 
138837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
138937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
139037d0c07bSMatthew G Knepley       }
139137d0c07bSMatthew G Knepley     }
139237d0c07bSMatthew G Knepley     if (fields) {
1393785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
139437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
139582f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
139637d0c07bSMatthew G Knepley       }
139737d0c07bSMatthew G Knepley     }
139837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13998865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
14008865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
140169ca1f37SDmitry Karpeev   }
14024d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14034d343eeaSMatthew G Knepley }
14044d343eeaSMatthew G Knepley 
140516621825SDmitry Karpeev 
140616621825SDmitry Karpeev #undef __FUNCT__
140716621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
140816621825SDmitry Karpeev /*@C
140916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
141016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
141116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1412e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1413e7c4fc90SDmitry Karpeev 
1414e7c4fc90SDmitry Karpeev   Not collective
1415e7c4fc90SDmitry Karpeev 
1416e7c4fc90SDmitry Karpeev   Input Parameter:
1417e7c4fc90SDmitry Karpeev . dm - the DM object
1418e7c4fc90SDmitry Karpeev 
1419e7c4fc90SDmitry Karpeev   Output Parameters:
14200298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14210298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14220298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14230298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1424e7c4fc90SDmitry Karpeev 
1425e7c4fc90SDmitry Karpeev   Level: intermediate
1426e7c4fc90SDmitry Karpeev 
1427e7c4fc90SDmitry Karpeev   Notes:
1428e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1429e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1430e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1431e7c4fc90SDmitry Karpeev 
1432e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1433e7c4fc90SDmitry Karpeev @*/
143416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1435e7c4fc90SDmitry Karpeev {
1436e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1437e7c4fc90SDmitry Karpeev 
1438e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1439e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14408865f1eaSKarl Rupp   if (len) {
14418865f1eaSKarl Rupp     PetscValidPointer(len,2);
14428865f1eaSKarl Rupp     *len = 0;
14438865f1eaSKarl Rupp   }
14448865f1eaSKarl Rupp   if (namelist) {
14458865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14468865f1eaSKarl Rupp     *namelist = 0;
14478865f1eaSKarl Rupp   }
14488865f1eaSKarl Rupp   if (islist) {
14498865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14508865f1eaSKarl Rupp     *islist = 0;
14518865f1eaSKarl Rupp   }
14528865f1eaSKarl Rupp   if (dmlist) {
14538865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14548865f1eaSKarl Rupp     *dmlist = 0;
14558865f1eaSKarl Rupp   }
1456f3f0edfdSDmitry Karpeev   /*
1457f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1458f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1459f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1460f3f0edfdSDmitry Karpeev    */
1461ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
146216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1463435a35e8SMatthew G Knepley     PetscSection section;
1464435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1465435a35e8SMatthew G Knepley 
1466435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1467435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1468435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1469435a35e8SMatthew G Knepley       *len = numFields;
147003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
147103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
147203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1473435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1474435a35e8SMatthew G Knepley         const char *fieldName;
1475435a35e8SMatthew G Knepley 
147603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
147703dc3394SMatthew G. Knepley         if (namelist) {
1478435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1479435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1480435a35e8SMatthew G Knepley         }
148103dc3394SMatthew G. Knepley       }
1482435a35e8SMatthew G Knepley     } else {
148369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1484e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14850298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1486e7c4fc90SDmitry Karpeev     }
14878865f1eaSKarl Rupp   } else {
148816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
148916621825SDmitry Karpeev   }
149016621825SDmitry Karpeev   PetscFunctionReturn(0);
149116621825SDmitry Karpeev }
149216621825SDmitry Karpeev 
149316621825SDmitry Karpeev #undef __FUNCT__
1494435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1495564cec59SMatthew G. Knepley /*@
1496435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1497435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1498435a35e8SMatthew G Knepley 
1499435a35e8SMatthew G Knepley   Not collective
1500435a35e8SMatthew G Knepley 
1501435a35e8SMatthew G Knepley   Input Parameters:
1502435a35e8SMatthew G Knepley + dm - the DM object
1503435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
15040298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1505435a35e8SMatthew G Knepley 
1506435a35e8SMatthew G Knepley   Output Parameters:
1507435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1508435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1509435a35e8SMatthew G Knepley 
1510435a35e8SMatthew G Knepley   Level: intermediate
1511435a35e8SMatthew G Knepley 
1512435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1513435a35e8SMatthew G Knepley @*/
1514435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1515435a35e8SMatthew G Knepley {
1516435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1517435a35e8SMatthew G Knepley 
1518435a35e8SMatthew G Knepley   PetscFunctionBegin;
1519435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1520435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15218865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15228865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1523435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1524435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
152582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1526435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1527435a35e8SMatthew G Knepley }
1528435a35e8SMatthew G Knepley 
152916621825SDmitry Karpeev 
153016621825SDmitry Karpeev #undef __FUNCT__
153116621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
153216621825SDmitry Karpeev /*@C
15338d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
15348d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
15358d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
15368d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
15378d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
153816621825SDmitry Karpeev 
153916621825SDmitry Karpeev   Not collective
154016621825SDmitry Karpeev 
154116621825SDmitry Karpeev   Input Parameter:
154216621825SDmitry Karpeev . dm - the DM object
154316621825SDmitry Karpeev 
154416621825SDmitry Karpeev   Output Parameters:
15450298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15460298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15470298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15480298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15490298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
155016621825SDmitry Karpeev 
155116621825SDmitry Karpeev   Level: intermediate
155216621825SDmitry Karpeev 
155316621825SDmitry Karpeev   Notes:
155416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
155516621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
155616621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
155716621825SDmitry Karpeev 
15588d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
155916621825SDmitry Karpeev @*/
15608d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
156116621825SDmitry Karpeev {
156216621825SDmitry Karpeev   PetscErrorCode      ierr;
1563be081cd6SPeter Brune   DMSubDomainHookLink link;
1564be081cd6SPeter Brune   PetscInt            i,l;
156516621825SDmitry Karpeev 
156616621825SDmitry Karpeev   PetscFunctionBegin;
156716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
156814a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
15690298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
15700298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
15710298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
15720298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1573f3f0edfdSDmitry Karpeev   /*
1574f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1575f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1576f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1577f3f0edfdSDmitry Karpeev    */
1578ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
157916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1580be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
158114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
158214a18fd3SPeter Brune     if (dmlist) {
1583*2ca110d9SMatthew G. Knepley       if (!*dmlist) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_POINTER,"Method mapped to dm->ops->createdomaindecomposition must allocate at least one DM");
1584be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1585be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1586be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1587be081cd6SPeter Brune         }
1588d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1589e7c4fc90SDmitry Karpeev       }
159014a18fd3SPeter Brune     }
159114a18fd3SPeter Brune     if (len) *len = l;
159214a18fd3SPeter Brune   }
1593e30e807fSPeter Brune   PetscFunctionReturn(0);
1594e30e807fSPeter Brune }
1595e30e807fSPeter Brune 
1596e30e807fSPeter Brune 
1597e30e807fSPeter Brune #undef __FUNCT__
1598e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1599e30e807fSPeter Brune /*@C
1600e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1601e30e807fSPeter Brune 
1602e30e807fSPeter Brune   Not collective
1603e30e807fSPeter Brune 
1604e30e807fSPeter Brune   Input Parameters:
1605e30e807fSPeter Brune + dm - the DM object
1606e30e807fSPeter Brune . n  - the number of subdomain scatters
1607e30e807fSPeter Brune - subdms - the local subdomains
1608e30e807fSPeter Brune 
1609e30e807fSPeter Brune   Output Parameters:
1610e30e807fSPeter Brune + n     - the number of scatters returned
1611e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1612e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1613e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1614e30e807fSPeter Brune 
1615e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1616e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1617e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1618e30e807fSPeter Brune   solution and residual data.
1619e30e807fSPeter Brune 
1620e30e807fSPeter Brune   Level: developer
1621e30e807fSPeter Brune 
1622e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1623e30e807fSPeter Brune @*/
1624e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1625e30e807fSPeter Brune {
1626e30e807fSPeter Brune   PetscErrorCode ierr;
1627e30e807fSPeter Brune 
1628e30e807fSPeter Brune   PetscFunctionBegin;
1629e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1630e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1631e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1632e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
163382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1634e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1635e7c4fc90SDmitry Karpeev }
1636e7c4fc90SDmitry Karpeev 
1637731c8d9eSDmitry Karpeev #undef __FUNCT__
163847c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
163947c6ae99SBarry Smith /*@
164047c6ae99SBarry Smith   DMRefine - Refines a DM object
164147c6ae99SBarry Smith 
164247c6ae99SBarry Smith   Collective on DM
164347c6ae99SBarry Smith 
164447c6ae99SBarry Smith   Input Parameter:
164547c6ae99SBarry Smith + dm   - the DM object
164691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
164747c6ae99SBarry Smith 
164847c6ae99SBarry Smith   Output Parameter:
16490298fd71SBarry Smith . dmf - the refined DM, or NULL
1650ae0a1c52SMatthew G Knepley 
16510298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
165247c6ae99SBarry Smith 
165347c6ae99SBarry Smith   Level: developer
165447c6ae99SBarry Smith 
1655e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
165647c6ae99SBarry Smith @*/
16577087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
165847c6ae99SBarry Smith {
165947c6ae99SBarry Smith   PetscErrorCode   ierr;
1660c833c3b5SJed Brown   DMRefineHookLink link;
166147c6ae99SBarry Smith 
166247c6ae99SBarry Smith   PetscFunctionBegin;
1663732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1664fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
166547c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
16664057135bSMatthew G Knepley   if (*dmf) {
166743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
16688865f1eaSKarl Rupp 
16698cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
16708865f1eaSKarl Rupp 
1671644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
16720598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1673656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
16748865f1eaSKarl Rupp 
1675e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1676c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
16778865f1eaSKarl Rupp       if (link->refinehook) {
16788865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16798865f1eaSKarl Rupp       }
1680c833c3b5SJed Brown     }
1681c833c3b5SJed Brown   }
1682c833c3b5SJed Brown   PetscFunctionReturn(0);
1683c833c3b5SJed Brown }
1684c833c3b5SJed Brown 
1685c833c3b5SJed Brown #undef __FUNCT__
1686c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1687bb9467b5SJed Brown /*@C
1688c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1689c833c3b5SJed Brown 
1690c833c3b5SJed Brown    Logically Collective
1691c833c3b5SJed Brown 
1692c833c3b5SJed Brown    Input Arguments:
1693c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1694c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1695c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16960298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1697c833c3b5SJed Brown 
1698c833c3b5SJed Brown    Calling sequence of refinehook:
1699c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1700c833c3b5SJed Brown 
1701c833c3b5SJed Brown +  coarse - coarse level DM
1702c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1703c833c3b5SJed Brown -  ctx - optional user-defined function context
1704c833c3b5SJed Brown 
1705c833c3b5SJed Brown    Calling sequence for interphook:
1706c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1707c833c3b5SJed Brown 
1708c833c3b5SJed Brown +  coarse - coarse level DM
1709c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1710c833c3b5SJed Brown .  fine - fine level DM to update
1711c833c3b5SJed Brown -  ctx - optional user-defined function context
1712c833c3b5SJed Brown 
1713c833c3b5SJed Brown    Level: advanced
1714c833c3b5SJed Brown 
1715c833c3b5SJed Brown    Notes:
1716c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1717c833c3b5SJed Brown 
1718c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1719c833c3b5SJed Brown 
1720bb9467b5SJed Brown    This function is currently not available from Fortran.
1721bb9467b5SJed Brown 
1722c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1723c833c3b5SJed Brown @*/
1724c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1725c833c3b5SJed Brown {
1726c833c3b5SJed Brown   PetscErrorCode   ierr;
1727c833c3b5SJed Brown   DMRefineHookLink link,*p;
1728c833c3b5SJed Brown 
1729c833c3b5SJed Brown   PetscFunctionBegin;
1730c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1731c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1732c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1733c833c3b5SJed Brown   link->refinehook = refinehook;
1734c833c3b5SJed Brown   link->interphook = interphook;
1735c833c3b5SJed Brown   link->ctx        = ctx;
17360298fd71SBarry Smith   link->next       = NULL;
1737c833c3b5SJed Brown   *p               = link;
1738c833c3b5SJed Brown   PetscFunctionReturn(0);
1739c833c3b5SJed Brown }
1740c833c3b5SJed Brown 
1741c833c3b5SJed Brown #undef __FUNCT__
1742c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1743c833c3b5SJed Brown /*@
1744c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1745c833c3b5SJed Brown 
1746c833c3b5SJed Brown    Collective if any hooks are
1747c833c3b5SJed Brown 
1748c833c3b5SJed Brown    Input Arguments:
1749c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1750c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1751c833c3b5SJed Brown -  fine - finer DM to update
1752c833c3b5SJed Brown 
1753c833c3b5SJed Brown    Level: developer
1754c833c3b5SJed Brown 
1755c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1756c833c3b5SJed Brown @*/
1757c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1758c833c3b5SJed Brown {
1759c833c3b5SJed Brown   PetscErrorCode   ierr;
1760c833c3b5SJed Brown   DMRefineHookLink link;
1761c833c3b5SJed Brown 
1762c833c3b5SJed Brown   PetscFunctionBegin;
1763c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
17648865f1eaSKarl Rupp     if (link->interphook) {
17658865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
17668865f1eaSKarl Rupp     }
17674057135bSMatthew G Knepley   }
176847c6ae99SBarry Smith   PetscFunctionReturn(0);
176947c6ae99SBarry Smith }
177047c6ae99SBarry Smith 
177147c6ae99SBarry Smith #undef __FUNCT__
1772eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1773eb3f98d2SBarry Smith /*@
1774eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1775eb3f98d2SBarry Smith 
1776eb3f98d2SBarry Smith     Not Collective
1777eb3f98d2SBarry Smith 
1778eb3f98d2SBarry Smith     Input Parameter:
1779eb3f98d2SBarry Smith .   dm - the DM object
1780eb3f98d2SBarry Smith 
1781eb3f98d2SBarry Smith     Output Parameter:
1782eb3f98d2SBarry Smith .   level - number of refinements
1783eb3f98d2SBarry Smith 
1784eb3f98d2SBarry Smith     Level: developer
1785eb3f98d2SBarry Smith 
17866a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1787eb3f98d2SBarry Smith 
1788eb3f98d2SBarry Smith @*/
1789eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1790eb3f98d2SBarry Smith {
1791eb3f98d2SBarry Smith   PetscFunctionBegin;
1792eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1793eb3f98d2SBarry Smith   *level = dm->levelup;
1794eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1795eb3f98d2SBarry Smith }
1796eb3f98d2SBarry Smith 
1797eb3f98d2SBarry Smith #undef __FUNCT__
1798fef3a512SBarry Smith #define __FUNCT__ "DMSetRefineLevel"
1799fef3a512SBarry Smith /*@
1800fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1801fef3a512SBarry Smith 
1802fef3a512SBarry Smith     Not Collective
1803fef3a512SBarry Smith 
1804fef3a512SBarry Smith     Input Parameter:
1805fef3a512SBarry Smith +   dm - the DM object
1806fef3a512SBarry Smith -   level - number of refinements
1807fef3a512SBarry Smith 
1808fef3a512SBarry Smith     Level: advanced
1809fef3a512SBarry Smith 
1810fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1811fef3a512SBarry Smith 
1812fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1813fef3a512SBarry Smith 
1814fef3a512SBarry Smith @*/
1815fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1816fef3a512SBarry Smith {
1817fef3a512SBarry Smith   PetscFunctionBegin;
1818fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1819fef3a512SBarry Smith   dm->levelup = level;
1820fef3a512SBarry Smith   PetscFunctionReturn(0);
1821fef3a512SBarry Smith }
1822fef3a512SBarry Smith 
1823fef3a512SBarry Smith #undef __FUNCT__
1824baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1825bb9467b5SJed Brown /*@C
1826baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1827baf369e7SPeter Brune 
1828baf369e7SPeter Brune    Logically Collective
1829baf369e7SPeter Brune 
1830baf369e7SPeter Brune    Input Arguments:
1831baf369e7SPeter Brune +  dm - the DM
1832baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1833baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
18340298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1835baf369e7SPeter Brune 
1836baf369e7SPeter Brune    Calling sequence for beginhook:
1837baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1838baf369e7SPeter Brune 
1839baf369e7SPeter Brune +  dm - global DM
1840baf369e7SPeter Brune .  g - global vector
1841baf369e7SPeter Brune .  mode - mode
1842baf369e7SPeter Brune .  l - local vector
1843baf369e7SPeter Brune -  ctx - optional user-defined function context
1844baf369e7SPeter Brune 
1845baf369e7SPeter Brune 
1846baf369e7SPeter Brune    Calling sequence for endhook:
1847ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1848baf369e7SPeter Brune 
1849baf369e7SPeter Brune +  global - global DM
1850baf369e7SPeter Brune -  ctx - optional user-defined function context
1851baf369e7SPeter Brune 
1852baf369e7SPeter Brune    Level: advanced
1853baf369e7SPeter Brune 
1854baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1855baf369e7SPeter Brune @*/
1856baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1857baf369e7SPeter Brune {
1858baf369e7SPeter Brune   PetscErrorCode          ierr;
1859baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1860baf369e7SPeter Brune 
1861baf369e7SPeter Brune   PetscFunctionBegin;
1862baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1863baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1864baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1865baf369e7SPeter Brune   link->beginhook = beginhook;
1866baf369e7SPeter Brune   link->endhook   = endhook;
1867baf369e7SPeter Brune   link->ctx       = ctx;
18680298fd71SBarry Smith   link->next      = NULL;
1869baf369e7SPeter Brune   *p              = link;
1870baf369e7SPeter Brune   PetscFunctionReturn(0);
1871baf369e7SPeter Brune }
1872baf369e7SPeter Brune 
1873baf369e7SPeter Brune #undef __FUNCT__
18744c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
18754c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
18764c274da1SToby Isaac {
18774c274da1SToby Isaac   Mat cMat;
18784c274da1SToby Isaac   Vec cVec;
18794c274da1SToby Isaac   PetscSection section, cSec;
18804c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18814c274da1SToby Isaac   PetscErrorCode ierr;
18824c274da1SToby Isaac 
18834c274da1SToby Isaac   PetscFunctionBegin;
18844c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18854c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18864c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
18875db9a05bSToby Isaac     PetscInt nRows;
18885db9a05bSToby Isaac 
18895db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
18905db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
18914c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
18927711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
18934c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
18944c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
18954c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
18964c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
18974c274da1SToby Isaac       if (dof) {
18984c274da1SToby Isaac         PetscScalar *vals;
18994c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19004c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19014c274da1SToby Isaac       }
19024c274da1SToby Isaac     }
19034c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19044c274da1SToby Isaac   }
19054c274da1SToby Isaac   PetscFunctionReturn(0);
19064c274da1SToby Isaac }
19074c274da1SToby Isaac 
19084c274da1SToby Isaac #undef __FUNCT__
190947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
191047c6ae99SBarry Smith /*@
191147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
191247c6ae99SBarry Smith 
191347c6ae99SBarry Smith     Neighbor-wise Collective on DM
191447c6ae99SBarry Smith 
191547c6ae99SBarry Smith     Input Parameters:
191647c6ae99SBarry Smith +   dm - the DM object
191747c6ae99SBarry Smith .   g - the global vector
191847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
191947c6ae99SBarry Smith -   l - the local vector
192047c6ae99SBarry Smith 
192147c6ae99SBarry Smith 
192247c6ae99SBarry Smith     Level: beginner
192347c6ae99SBarry Smith 
1924e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
192547c6ae99SBarry Smith 
192647c6ae99SBarry Smith @*/
19277087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
192847c6ae99SBarry Smith {
19297128ae9fSMatthew G Knepley   PetscSF                 sf;
193047c6ae99SBarry Smith   PetscErrorCode          ierr;
1931baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
193247c6ae99SBarry Smith 
193347c6ae99SBarry Smith   PetscFunctionBegin;
1934171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1935baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
19368865f1eaSKarl Rupp     if (link->beginhook) {
19378865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
19388865f1eaSKarl Rupp     }
1939baf369e7SPeter Brune   }
19407128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19417128ae9fSMatthew G Knepley   if (sf) {
1942ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1943ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
19447128ae9fSMatthew G Knepley 
194582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19467128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1947ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19487128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19497128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1950ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19517128ae9fSMatthew G Knepley   } else {
1952843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19537128ae9fSMatthew G Knepley   }
195447c6ae99SBarry Smith   PetscFunctionReturn(0);
195547c6ae99SBarry Smith }
195647c6ae99SBarry Smith 
195747c6ae99SBarry Smith #undef __FUNCT__
195847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
195947c6ae99SBarry Smith /*@
196047c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
196147c6ae99SBarry Smith 
196247c6ae99SBarry Smith     Neighbor-wise Collective on DM
196347c6ae99SBarry Smith 
196447c6ae99SBarry Smith     Input Parameters:
196547c6ae99SBarry Smith +   dm - the DM object
196647c6ae99SBarry Smith .   g - the global vector
196747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
196847c6ae99SBarry Smith -   l - the local vector
196947c6ae99SBarry Smith 
197047c6ae99SBarry Smith 
197147c6ae99SBarry Smith     Level: beginner
197247c6ae99SBarry Smith 
1973e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
197447c6ae99SBarry Smith 
197547c6ae99SBarry Smith @*/
19767087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
197747c6ae99SBarry Smith {
19787128ae9fSMatthew G Knepley   PetscSF                 sf;
197947c6ae99SBarry Smith   PetscErrorCode          ierr;
1980ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1981ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1982baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
198347c6ae99SBarry Smith 
198447c6ae99SBarry Smith   PetscFunctionBegin;
1985171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19867128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19877128ae9fSMatthew G Knepley   if (sf) {
198882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19897128ae9fSMatthew G Knepley 
19907128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1991ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19927128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19937128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1994ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19957128ae9fSMatthew G Knepley   } else {
1996843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19977128ae9fSMatthew G Knepley   }
19984c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1999baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2000baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2001baf369e7SPeter Brune   }
200247c6ae99SBarry Smith   PetscFunctionReturn(0);
200347c6ae99SBarry Smith }
200447c6ae99SBarry Smith 
200547c6ae99SBarry Smith #undef __FUNCT__
2006d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
2007d4d07f1eSToby Isaac /*@C
2008d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2009d4d07f1eSToby Isaac 
2010d4d07f1eSToby Isaac    Logically Collective
2011d4d07f1eSToby Isaac 
2012d4d07f1eSToby Isaac    Input Arguments:
2013d4d07f1eSToby Isaac +  dm - the DM
2014d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2015d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2016d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2017d4d07f1eSToby Isaac 
2018d4d07f1eSToby Isaac    Calling sequence for beginhook:
2019d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2020d4d07f1eSToby Isaac 
2021d4d07f1eSToby Isaac +  dm - global DM
2022d4d07f1eSToby Isaac .  l - local vector
2023d4d07f1eSToby Isaac .  mode - mode
2024d4d07f1eSToby Isaac .  g - global vector
2025d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2026d4d07f1eSToby Isaac 
2027d4d07f1eSToby Isaac 
2028d4d07f1eSToby Isaac    Calling sequence for endhook:
2029d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2030d4d07f1eSToby Isaac 
2031d4d07f1eSToby Isaac +  global - global DM
2032d4d07f1eSToby Isaac .  l - local vector
2033d4d07f1eSToby Isaac .  mode - mode
2034d4d07f1eSToby Isaac .  g - global vector
2035d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2036d4d07f1eSToby Isaac 
2037d4d07f1eSToby Isaac    Level: advanced
2038d4d07f1eSToby Isaac 
2039d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2040d4d07f1eSToby Isaac @*/
2041d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2042d4d07f1eSToby Isaac {
2043d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2044d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2045d4d07f1eSToby Isaac 
2046d4d07f1eSToby Isaac   PetscFunctionBegin;
2047d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2048d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2049d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
2050d4d07f1eSToby Isaac   link->beginhook = beginhook;
2051d4d07f1eSToby Isaac   link->endhook   = endhook;
2052d4d07f1eSToby Isaac   link->ctx       = ctx;
2053d4d07f1eSToby Isaac   link->next      = NULL;
2054d4d07f1eSToby Isaac   *p              = link;
2055d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2056d4d07f1eSToby Isaac }
2057d4d07f1eSToby Isaac 
2058d4d07f1eSToby Isaac #undef __FUNCT__
20594c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
20604c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
20614c274da1SToby Isaac {
20624c274da1SToby Isaac   Mat cMat;
20634c274da1SToby Isaac   Vec cVec;
20644c274da1SToby Isaac   PetscSection section, cSec;
20654c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20664c274da1SToby Isaac   PetscErrorCode ierr;
20674c274da1SToby Isaac 
20684c274da1SToby Isaac   PetscFunctionBegin;
20694c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20704c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20714c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
20725db9a05bSToby Isaac     PetscInt nRows;
20735db9a05bSToby Isaac 
20745db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
20755db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
20764c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
20777711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20784c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20794c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20804c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20814c274da1SToby Isaac       if (dof) {
20824c274da1SToby Isaac         PetscInt d;
20834c274da1SToby Isaac         PetscScalar *vals;
20844c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
20854c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
20864c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
20874c274da1SToby Isaac          * we just extracted */
20884c274da1SToby Isaac         for (d = 0; d < dof; d++) {
20894c274da1SToby Isaac           vals[d] = 0.;
20904c274da1SToby Isaac         }
20914c274da1SToby Isaac       }
20924c274da1SToby Isaac     }
20934c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
20944c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
20954c274da1SToby Isaac   }
20964c274da1SToby Isaac   PetscFunctionReturn(0);
20974c274da1SToby Isaac }
20984c274da1SToby Isaac 
20994c274da1SToby Isaac #undef __FUNCT__
21009a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
210147c6ae99SBarry Smith /*@
21029a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21039a42bb27SBarry Smith 
21049a42bb27SBarry Smith     Neighbor-wise Collective on DM
21059a42bb27SBarry Smith 
21069a42bb27SBarry Smith     Input Parameters:
21079a42bb27SBarry Smith +   dm - the DM object
2108f6813fd5SJed Brown .   l - the local vector
21091eb28f2eSBarry 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.
21101eb28f2eSBarry Smith -   g - the global vector
21119a42bb27SBarry Smith 
2112d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
211384330215SMatthew 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.
21149a42bb27SBarry Smith 
21159a42bb27SBarry Smith     Level: beginner
21169a42bb27SBarry Smith 
2117e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
21189a42bb27SBarry Smith 
21199a42bb27SBarry Smith @*/
21207087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
21219a42bb27SBarry Smith {
21227128ae9fSMatthew G Knepley   PetscSF                 sf;
212384330215SMatthew G. Knepley   PetscSection            s, gs;
2124d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2125ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2126ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
212784330215SMatthew G. Knepley   PetscBool               isInsert;
212884330215SMatthew G. Knepley   PetscErrorCode          ierr;
21299a42bb27SBarry Smith 
21309a42bb27SBarry Smith   PetscFunctionBegin;
2131171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2132d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2133d4d07f1eSToby Isaac     if (link->beginhook) {
2134d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2135d4d07f1eSToby Isaac     }
2136d4d07f1eSToby Isaac   }
21374c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
21387128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
213984330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21407128ae9fSMatthew G Knepley   switch (mode) {
21417128ae9fSMatthew G Knepley   case INSERT_VALUES:
21427128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
214384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21447128ae9fSMatthew G Knepley   case ADD_VALUES:
21457128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
214684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21477128ae9fSMatthew G Knepley   default:
214882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21497128ae9fSMatthew G Knepley   }
215084330215SMatthew G. Knepley   if (sf && !isInsert) {
2151ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21527128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2153a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2154ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
215584330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
215684330215SMatthew G. Knepley   } else if (s && isInsert) {
215784330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
215884330215SMatthew G. Knepley 
215984330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
216084330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
216184330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2162ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
216384330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
216484330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2165b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
216684330215SMatthew G. Knepley 
216784330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
216803442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
216984330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2170b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
217184330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
217284330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2173b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
217403442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2175b3b16f48SMatthew 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);
2176b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2177b3b16f48SMatthew G. Knepley       if (!gcdof) {
217884330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2179b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2180b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
218184330215SMatthew G. Knepley         const PetscInt *cdofs;
218284330215SMatthew G. Knepley         PetscInt        cind = 0;
218384330215SMatthew G. Knepley 
218484330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2185b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
218684330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2187b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
218884330215SMatthew G. Knepley         }
2189b3b16f48SMatthew 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);
219084330215SMatthew G. Knepley     }
2191ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21927128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
21937128ae9fSMatthew G Knepley   } else {
2194843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21957128ae9fSMatthew G Knepley   }
21969a42bb27SBarry Smith   PetscFunctionReturn(0);
21979a42bb27SBarry Smith }
21989a42bb27SBarry Smith 
21999a42bb27SBarry Smith #undef __FUNCT__
22009a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
22019a42bb27SBarry Smith /*@
22029a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
220347c6ae99SBarry Smith 
220447c6ae99SBarry Smith     Neighbor-wise Collective on DM
220547c6ae99SBarry Smith 
220647c6ae99SBarry Smith     Input Parameters:
220747c6ae99SBarry Smith +   dm - the DM object
2208f6813fd5SJed Brown .   l - the local vector
220947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2210f6813fd5SJed Brown -   g - the global vector
221147c6ae99SBarry Smith 
221247c6ae99SBarry Smith 
221347c6ae99SBarry Smith     Level: beginner
221447c6ae99SBarry Smith 
2215e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
221647c6ae99SBarry Smith 
221747c6ae99SBarry Smith @*/
22187087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
221947c6ae99SBarry Smith {
22207128ae9fSMatthew G Knepley   PetscSF                 sf;
222184330215SMatthew G. Knepley   PetscSection            s;
2222d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
222384330215SMatthew G. Knepley   PetscBool               isInsert;
222484330215SMatthew G. Knepley   PetscErrorCode          ierr;
222547c6ae99SBarry Smith 
222647c6ae99SBarry Smith   PetscFunctionBegin;
2227171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22287128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
222984330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22307128ae9fSMatthew G Knepley   switch (mode) {
22317128ae9fSMatthew G Knepley   case INSERT_VALUES:
22327128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
223384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22347128ae9fSMatthew G Knepley   case ADD_VALUES:
22357128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
223684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22377128ae9fSMatthew G Knepley   default:
223882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22397128ae9fSMatthew G Knepley   }
224084330215SMatthew G. Knepley   if (sf && !isInsert) {
2241ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2242ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
224384330215SMatthew G. Knepley 
2244ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22457128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2246a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2247ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22487128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
224984330215SMatthew G. Knepley   } else if (s && isInsert) {
22507128ae9fSMatthew G Knepley   } else {
2251843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22527128ae9fSMatthew G Knepley   }
2253d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2254d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2255d4d07f1eSToby Isaac   }
225647c6ae99SBarry Smith   PetscFunctionReturn(0);
225747c6ae99SBarry Smith }
225847c6ae99SBarry Smith 
225947c6ae99SBarry Smith #undef __FUNCT__
2260f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2261f089877aSRichard Tran Mills /*@
2262bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2263bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2264d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2265f089877aSRichard Tran Mills 
2266bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2267f089877aSRichard Tran Mills 
2268f089877aSRichard Tran Mills    Input Parameters:
2269f089877aSRichard Tran Mills +  dm - the DM object
2270bc0a1609SRichard Tran Mills .  g - the original local vector
2271bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2272f089877aSRichard Tran Mills 
2273bc0a1609SRichard Tran Mills    Output Parameter:
2274bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2275f089877aSRichard Tran Mills 
2276f089877aSRichard Tran Mills    Level: intermediate
2277f089877aSRichard Tran Mills 
2278bc0a1609SRichard Tran Mills    Notes:
2279bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2280bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2281bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2282bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2283bc0a1609SRichard Tran Mills 
2284bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2285bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2286f089877aSRichard Tran Mills 
2287f089877aSRichard Tran Mills @*/
2288f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2289f089877aSRichard Tran Mills {
2290f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2291f089877aSRichard Tran Mills 
2292f089877aSRichard Tran Mills   PetscFunctionBegin;
2293f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2294f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2295f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2296f089877aSRichard Tran Mills }
2297f089877aSRichard Tran Mills 
2298f089877aSRichard Tran Mills #undef __FUNCT__
2299f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2300f089877aSRichard Tran Mills /*@
2301bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2302bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2303d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2304f089877aSRichard Tran Mills 
2305bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2306f089877aSRichard Tran Mills 
2307f089877aSRichard Tran Mills    Input Parameters:
2308bc0a1609SRichard Tran Mills +  da - the DM object
2309bc0a1609SRichard Tran Mills .  g - the original local vector
2310bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2311f089877aSRichard Tran Mills 
2312bc0a1609SRichard Tran Mills    Output Parameter:
2313bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2314f089877aSRichard Tran Mills 
2315f089877aSRichard Tran Mills    Level: intermediate
2316f089877aSRichard Tran Mills 
2317bc0a1609SRichard Tran Mills    Notes:
2318bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2319bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2320bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2321bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2322bc0a1609SRichard Tran Mills 
2323bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2324bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2325f089877aSRichard Tran Mills 
2326f089877aSRichard Tran Mills @*/
2327f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2328f089877aSRichard Tran Mills {
2329f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2330f089877aSRichard Tran Mills 
2331f089877aSRichard Tran Mills   PetscFunctionBegin;
2332f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2333f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2334f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2335f089877aSRichard Tran Mills }
2336f089877aSRichard Tran Mills 
2337f089877aSRichard Tran Mills 
2338f089877aSRichard Tran Mills #undef __FUNCT__
233947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
234047c6ae99SBarry Smith /*@
234147c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
234247c6ae99SBarry Smith 
234347c6ae99SBarry Smith     Collective on DM
234447c6ae99SBarry Smith 
234547c6ae99SBarry Smith     Input Parameter:
234647c6ae99SBarry Smith +   dm - the DM object
234791d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
234847c6ae99SBarry Smith 
234947c6ae99SBarry Smith     Output Parameter:
235047c6ae99SBarry Smith .   dmc - the coarsened DM
235147c6ae99SBarry Smith 
235247c6ae99SBarry Smith     Level: developer
235347c6ae99SBarry Smith 
2354e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
235547c6ae99SBarry Smith 
235647c6ae99SBarry Smith @*/
23577087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
235847c6ae99SBarry Smith {
235947c6ae99SBarry Smith   PetscErrorCode    ierr;
2360b17ce1afSJed Brown   DMCoarsenHookLink link;
236147c6ae99SBarry Smith 
236247c6ae99SBarry Smith   PetscFunctionBegin;
2363171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2364fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
236547a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
236647c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
23678892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2368a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
236943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
23708cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2371644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
23720598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2373656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2374e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2375b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2376b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2377b17ce1afSJed Brown   }
237847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2379b17ce1afSJed Brown   PetscFunctionReturn(0);
2380b17ce1afSJed Brown }
2381b17ce1afSJed Brown 
2382b17ce1afSJed Brown #undef __FUNCT__
2383b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2384bb9467b5SJed Brown /*@C
2385b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2386b17ce1afSJed Brown 
2387b17ce1afSJed Brown    Logically Collective
2388b17ce1afSJed Brown 
2389b17ce1afSJed Brown    Input Arguments:
2390b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2391b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2392b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
23930298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2394b17ce1afSJed Brown 
2395b17ce1afSJed Brown    Calling sequence of coarsenhook:
2396b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2397b17ce1afSJed Brown 
2398b17ce1afSJed Brown +  fine - fine level DM
2399b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2400b17ce1afSJed Brown -  ctx - optional user-defined function context
2401b17ce1afSJed Brown 
2402b17ce1afSJed Brown    Calling sequence for restricthook:
2403c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2404b17ce1afSJed Brown 
2405b17ce1afSJed Brown +  fine - fine level DM
2406b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2407c833c3b5SJed Brown .  rscale - scaling vector for restriction
2408c833c3b5SJed Brown .  inject - matrix restricting by injection
2409b17ce1afSJed Brown .  coarse - coarse level DM to update
2410b17ce1afSJed Brown -  ctx - optional user-defined function context
2411b17ce1afSJed Brown 
2412b17ce1afSJed Brown    Level: advanced
2413b17ce1afSJed Brown 
2414b17ce1afSJed Brown    Notes:
2415b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2416b17ce1afSJed Brown 
2417b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2418b17ce1afSJed Brown 
2419b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2420b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2421b17ce1afSJed Brown 
2422bb9467b5SJed Brown    This function is currently not available from Fortran.
2423bb9467b5SJed Brown 
2424c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2425b17ce1afSJed Brown @*/
2426b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2427b17ce1afSJed Brown {
2428b17ce1afSJed Brown   PetscErrorCode    ierr;
2429b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2430b17ce1afSJed Brown 
2431b17ce1afSJed Brown   PetscFunctionBegin;
2432b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
24336bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2434b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2435b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2436b17ce1afSJed Brown   link->restricthook = restricthook;
2437b17ce1afSJed Brown   link->ctx          = ctx;
24380298fd71SBarry Smith   link->next         = NULL;
2439b17ce1afSJed Brown   *p                 = link;
2440b17ce1afSJed Brown   PetscFunctionReturn(0);
2441b17ce1afSJed Brown }
2442b17ce1afSJed Brown 
2443b17ce1afSJed Brown #undef __FUNCT__
2444b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2445b17ce1afSJed Brown /*@
2446b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2447b17ce1afSJed Brown 
2448b17ce1afSJed Brown    Collective if any hooks are
2449b17ce1afSJed Brown 
2450b17ce1afSJed Brown    Input Arguments:
2451b17ce1afSJed Brown +  fine - finer DM to use as a base
2452b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2453b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2454b17ce1afSJed Brown -  coarse - coarer DM to update
2455b17ce1afSJed Brown 
2456b17ce1afSJed Brown    Level: developer
2457b17ce1afSJed Brown 
2458b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2459b17ce1afSJed Brown @*/
2460b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2461b17ce1afSJed Brown {
2462b17ce1afSJed Brown   PetscErrorCode    ierr;
2463b17ce1afSJed Brown   DMCoarsenHookLink link;
2464b17ce1afSJed Brown 
2465b17ce1afSJed Brown   PetscFunctionBegin;
2466b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
24678865f1eaSKarl Rupp     if (link->restricthook) {
24688865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
24698865f1eaSKarl Rupp     }
2470b17ce1afSJed Brown   }
247147c6ae99SBarry Smith   PetscFunctionReturn(0);
247247c6ae99SBarry Smith }
247347c6ae99SBarry Smith 
247447c6ae99SBarry Smith #undef __FUNCT__
2475be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2476bb9467b5SJed Brown /*@C
2477be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
24785dbd56e3SPeter Brune 
24795dbd56e3SPeter Brune    Logically Collective
24805dbd56e3SPeter Brune 
24815dbd56e3SPeter Brune    Input Arguments:
24825dbd56e3SPeter Brune +  global - global DM
2483ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
24845dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
24850298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
24865dbd56e3SPeter Brune 
2487ec4806b8SPeter Brune 
2488ec4806b8SPeter Brune    Calling sequence for ddhook:
2489ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2490ec4806b8SPeter Brune 
2491ec4806b8SPeter Brune +  global - global DM
2492ec4806b8SPeter Brune .  block  - block DM
2493ec4806b8SPeter Brune -  ctx - optional user-defined function context
2494ec4806b8SPeter Brune 
24955dbd56e3SPeter Brune    Calling sequence for restricthook:
2496ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
24975dbd56e3SPeter Brune 
24985dbd56e3SPeter Brune +  global - global DM
24995dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
25005dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2501ec4806b8SPeter Brune .  block  - block DM
25025dbd56e3SPeter Brune -  ctx - optional user-defined function context
25035dbd56e3SPeter Brune 
25045dbd56e3SPeter Brune    Level: advanced
25055dbd56e3SPeter Brune 
25065dbd56e3SPeter Brune    Notes:
2507ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
25085dbd56e3SPeter Brune 
25095dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
25105dbd56e3SPeter Brune 
25115dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2512ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
25135dbd56e3SPeter Brune 
2514bb9467b5SJed Brown    This function is currently not available from Fortran.
2515bb9467b5SJed Brown 
25165dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
25175dbd56e3SPeter Brune @*/
2518be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
25195dbd56e3SPeter Brune {
25205dbd56e3SPeter Brune   PetscErrorCode      ierr;
2521be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
25225dbd56e3SPeter Brune 
25235dbd56e3SPeter Brune   PetscFunctionBegin;
25245dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2525be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2526be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
25275dbd56e3SPeter Brune   link->restricthook = restricthook;
2528be081cd6SPeter Brune   link->ddhook       = ddhook;
25295dbd56e3SPeter Brune   link->ctx          = ctx;
25300298fd71SBarry Smith   link->next         = NULL;
25315dbd56e3SPeter Brune   *p                 = link;
25325dbd56e3SPeter Brune   PetscFunctionReturn(0);
25335dbd56e3SPeter Brune }
25345dbd56e3SPeter Brune 
25355dbd56e3SPeter Brune #undef __FUNCT__
2536be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
25375dbd56e3SPeter Brune /*@
2538be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
25395dbd56e3SPeter Brune 
25405dbd56e3SPeter Brune    Collective if any hooks are
25415dbd56e3SPeter Brune 
25425dbd56e3SPeter Brune    Input Arguments:
25435dbd56e3SPeter Brune +  fine - finer DM to use as a base
2544be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2545be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
25465dbd56e3SPeter Brune -  coarse - coarer DM to update
25475dbd56e3SPeter Brune 
25485dbd56e3SPeter Brune    Level: developer
25495dbd56e3SPeter Brune 
25505dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
25515dbd56e3SPeter Brune @*/
2552be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
25535dbd56e3SPeter Brune {
25545dbd56e3SPeter Brune   PetscErrorCode      ierr;
2555be081cd6SPeter Brune   DMSubDomainHookLink link;
25565dbd56e3SPeter Brune 
25575dbd56e3SPeter Brune   PetscFunctionBegin;
2558be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
25598865f1eaSKarl Rupp     if (link->restricthook) {
25608865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
25618865f1eaSKarl Rupp     }
25625dbd56e3SPeter Brune   }
25635dbd56e3SPeter Brune   PetscFunctionReturn(0);
25645dbd56e3SPeter Brune }
25655dbd56e3SPeter Brune 
25665dbd56e3SPeter Brune #undef __FUNCT__
25675fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
25685fe1f584SPeter Brune /*@
25696a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
25705fe1f584SPeter Brune 
25715fe1f584SPeter Brune     Not Collective
25725fe1f584SPeter Brune 
25735fe1f584SPeter Brune     Input Parameter:
25745fe1f584SPeter Brune .   dm - the DM object
25755fe1f584SPeter Brune 
25765fe1f584SPeter Brune     Output Parameter:
25776a7d9d85SPeter Brune .   level - number of coarsenings
25785fe1f584SPeter Brune 
25795fe1f584SPeter Brune     Level: developer
25805fe1f584SPeter Brune 
25816a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
25825fe1f584SPeter Brune 
25835fe1f584SPeter Brune @*/
25845fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
25855fe1f584SPeter Brune {
25865fe1f584SPeter Brune   PetscFunctionBegin;
25875fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25885fe1f584SPeter Brune   *level = dm->leveldown;
25895fe1f584SPeter Brune   PetscFunctionReturn(0);
25905fe1f584SPeter Brune }
25915fe1f584SPeter Brune 
25925fe1f584SPeter Brune 
25935fe1f584SPeter Brune 
25945fe1f584SPeter Brune #undef __FUNCT__
259547c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
259647c6ae99SBarry Smith /*@C
259747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
259847c6ae99SBarry Smith 
259947c6ae99SBarry Smith     Collective on DM
260047c6ae99SBarry Smith 
260147c6ae99SBarry Smith     Input Parameter:
260247c6ae99SBarry Smith +   dm - the DM object
260347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
260447c6ae99SBarry Smith 
260547c6ae99SBarry Smith     Output Parameter:
260647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
260747c6ae99SBarry Smith 
260847c6ae99SBarry Smith     Level: developer
260947c6ae99SBarry Smith 
2610e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
261147c6ae99SBarry Smith 
261247c6ae99SBarry Smith @*/
26137087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
261447c6ae99SBarry Smith {
261547c6ae99SBarry Smith   PetscErrorCode ierr;
261647c6ae99SBarry Smith 
261747c6ae99SBarry Smith   PetscFunctionBegin;
2618171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2619ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
262047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
262147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
262247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
262347c6ae99SBarry Smith   } else if (dm->ops->refine) {
262447c6ae99SBarry Smith     PetscInt i;
262547c6ae99SBarry Smith 
2626ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
262747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2628ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
262947c6ae99SBarry Smith     }
2630ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
263147c6ae99SBarry Smith   PetscFunctionReturn(0);
263247c6ae99SBarry Smith }
263347c6ae99SBarry Smith 
263447c6ae99SBarry Smith #undef __FUNCT__
263547c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
263647c6ae99SBarry Smith /*@C
263747c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
263847c6ae99SBarry Smith 
263947c6ae99SBarry Smith     Collective on DM
264047c6ae99SBarry Smith 
264147c6ae99SBarry Smith     Input Parameter:
264247c6ae99SBarry Smith +   dm - the DM object
264347c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
264447c6ae99SBarry Smith 
264547c6ae99SBarry Smith     Output Parameter:
264647c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
264747c6ae99SBarry Smith 
264847c6ae99SBarry Smith     Level: developer
264947c6ae99SBarry Smith 
2650e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
265147c6ae99SBarry Smith 
265247c6ae99SBarry Smith @*/
26537087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
265447c6ae99SBarry Smith {
265547c6ae99SBarry Smith   PetscErrorCode ierr;
265647c6ae99SBarry Smith 
265747c6ae99SBarry Smith   PetscFunctionBegin;
2658171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2659ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
266047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
266147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
266247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
266347c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
266447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
266547c6ae99SBarry Smith     PetscInt i;
266647c6ae99SBarry Smith 
2667ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
266847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2669ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
267047c6ae99SBarry Smith     }
2671ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
267247c6ae99SBarry Smith   PetscFunctionReturn(0);
267347c6ae99SBarry Smith }
267447c6ae99SBarry Smith 
267547c6ae99SBarry Smith #undef __FUNCT__
2676e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
267747c6ae99SBarry Smith /*@
2678e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
267947c6ae99SBarry Smith    grids associated with two DMs.
268047c6ae99SBarry Smith 
268147c6ae99SBarry Smith    Collective on DM
268247c6ae99SBarry Smith 
268347c6ae99SBarry Smith    Input Parameters:
268447c6ae99SBarry Smith +  dmc - the coarse grid DM
268547c6ae99SBarry Smith -  dmf - the fine grid DM
268647c6ae99SBarry Smith 
268747c6ae99SBarry Smith    Output Parameters:
268847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
268947c6ae99SBarry Smith 
269047c6ae99SBarry Smith    Level: intermediate
269147c6ae99SBarry Smith 
269247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
269347c6ae99SBarry Smith 
2694e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
269547c6ae99SBarry Smith @*/
2696e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
269747c6ae99SBarry Smith {
269847c6ae99SBarry Smith   PetscErrorCode ierr;
269947c6ae99SBarry Smith 
270047c6ae99SBarry Smith   PetscFunctionBegin;
2701171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2702171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
270347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
270447c6ae99SBarry Smith   PetscFunctionReturn(0);
270547c6ae99SBarry Smith }
270647c6ae99SBarry Smith 
270747c6ae99SBarry Smith #undef __FUNCT__
27081a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
27091a266240SBarry Smith /*@C
27101a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
27111a266240SBarry Smith 
27121a266240SBarry Smith     Not Collective
27131a266240SBarry Smith 
27141a266240SBarry Smith     Input Parameters:
27151a266240SBarry Smith +   dm - the DM object
27161a266240SBarry Smith -   destroy - the destroy function
27171a266240SBarry Smith 
27181a266240SBarry Smith     Level: intermediate
27191a266240SBarry Smith 
2720e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
27211a266240SBarry Smith 
2722f07f9ceaSJed Brown @*/
27231a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
27241a266240SBarry Smith {
27251a266240SBarry Smith   PetscFunctionBegin;
2726171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27271a266240SBarry Smith   dm->ctxdestroy = destroy;
27281a266240SBarry Smith   PetscFunctionReturn(0);
27291a266240SBarry Smith }
27301a266240SBarry Smith 
27311a266240SBarry Smith #undef __FUNCT__
27321b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2733b07ff414SBarry Smith /*@
27341b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
273547c6ae99SBarry Smith 
273647c6ae99SBarry Smith     Not Collective
273747c6ae99SBarry Smith 
273847c6ae99SBarry Smith     Input Parameters:
273947c6ae99SBarry Smith +   dm - the DM object
274047c6ae99SBarry Smith -   ctx - the user context
274147c6ae99SBarry Smith 
274247c6ae99SBarry Smith     Level: intermediate
274347c6ae99SBarry Smith 
2744e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
274547c6ae99SBarry Smith 
274647c6ae99SBarry Smith @*/
27471b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
274847c6ae99SBarry Smith {
274947c6ae99SBarry Smith   PetscFunctionBegin;
2750171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
275147c6ae99SBarry Smith   dm->ctx = ctx;
275247c6ae99SBarry Smith   PetscFunctionReturn(0);
275347c6ae99SBarry Smith }
275447c6ae99SBarry Smith 
275547c6ae99SBarry Smith #undef __FUNCT__
27561b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
275747c6ae99SBarry Smith /*@
27581b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
275947c6ae99SBarry Smith 
276047c6ae99SBarry Smith     Not Collective
276147c6ae99SBarry Smith 
276247c6ae99SBarry Smith     Input Parameter:
276347c6ae99SBarry Smith .   dm - the DM object
276447c6ae99SBarry Smith 
276547c6ae99SBarry Smith     Output Parameter:
276647c6ae99SBarry Smith .   ctx - the user context
276747c6ae99SBarry Smith 
276847c6ae99SBarry Smith     Level: intermediate
276947c6ae99SBarry Smith 
2770e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
277147c6ae99SBarry Smith 
277247c6ae99SBarry Smith @*/
27731b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
277447c6ae99SBarry Smith {
277547c6ae99SBarry Smith   PetscFunctionBegin;
2776171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27771b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
277847c6ae99SBarry Smith   PetscFunctionReturn(0);
277947c6ae99SBarry Smith }
278047c6ae99SBarry Smith 
278147c6ae99SBarry Smith #undef __FUNCT__
278208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
278308da532bSDmitry Karpeev /*@C
2784df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
278508da532bSDmitry Karpeev 
278608da532bSDmitry Karpeev     Logically Collective on DM
278708da532bSDmitry Karpeev 
278808da532bSDmitry Karpeev     Input Parameter:
278908da532bSDmitry Karpeev +   dm - the DM object
27900298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
279108da532bSDmitry Karpeev 
279208da532bSDmitry Karpeev     Level: intermediate
279308da532bSDmitry Karpeev 
2794835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
279508da532bSDmitry Karpeev          DMSetJacobian()
279608da532bSDmitry Karpeev 
279708da532bSDmitry Karpeev @*/
279808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
279908da532bSDmitry Karpeev {
280008da532bSDmitry Karpeev   PetscFunctionBegin;
280108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
280208da532bSDmitry Karpeev   PetscFunctionReturn(0);
280308da532bSDmitry Karpeev }
280408da532bSDmitry Karpeev 
280508da532bSDmitry Karpeev #undef __FUNCT__
280608da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
280708da532bSDmitry Karpeev /*@
280808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
280908da532bSDmitry Karpeev 
281008da532bSDmitry Karpeev     Not Collective
281108da532bSDmitry Karpeev 
281208da532bSDmitry Karpeev     Input Parameter:
281308da532bSDmitry Karpeev .   dm - the DM object to destroy
281408da532bSDmitry Karpeev 
281508da532bSDmitry Karpeev     Output Parameter:
281608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
281708da532bSDmitry Karpeev 
281808da532bSDmitry Karpeev     Level: developer
281908da532bSDmitry Karpeev 
282074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
282108da532bSDmitry Karpeev 
282208da532bSDmitry Karpeev @*/
282308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
282408da532bSDmitry Karpeev {
282508da532bSDmitry Karpeev   PetscFunctionBegin;
282608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
282708da532bSDmitry Karpeev   PetscFunctionReturn(0);
282808da532bSDmitry Karpeev }
282908da532bSDmitry Karpeev 
283008da532bSDmitry Karpeev #undef __FUNCT__
283108da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
283208da532bSDmitry Karpeev /*@C
283308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
283408da532bSDmitry Karpeev 
283508da532bSDmitry Karpeev     Logically Collective on DM
283608da532bSDmitry Karpeev 
283708da532bSDmitry Karpeev     Input Parameters:
2838907376e6SBarry Smith .   dm - the DM object
283908da532bSDmitry Karpeev 
284008da532bSDmitry Karpeev     Output parameters:
284108da532bSDmitry Karpeev +   xl - lower bound
284208da532bSDmitry Karpeev -   xu - upper bound
284308da532bSDmitry Karpeev 
2844907376e6SBarry Smith     Level: advanced
2845907376e6SBarry Smith 
2846907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
284708da532bSDmitry Karpeev 
284874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
284908da532bSDmitry Karpeev 
285008da532bSDmitry Karpeev @*/
285108da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
285208da532bSDmitry Karpeev {
285308da532bSDmitry Karpeev   PetscErrorCode ierr;
28545fd66863SKarl Rupp 
285508da532bSDmitry Karpeev   PetscFunctionBegin;
285608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
285708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
285808da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
285908da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
28608865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
286108da532bSDmitry Karpeev   PetscFunctionReturn(0);
286208da532bSDmitry Karpeev }
286308da532bSDmitry Karpeev 
286408da532bSDmitry Karpeev #undef __FUNCT__
2865b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2866b0ae01b7SPeter Brune /*@
2867b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2868b0ae01b7SPeter Brune 
2869b0ae01b7SPeter Brune     Not Collective
2870b0ae01b7SPeter Brune 
2871b0ae01b7SPeter Brune     Input Parameter:
2872b0ae01b7SPeter Brune .   dm - the DM object
2873b0ae01b7SPeter Brune 
2874b0ae01b7SPeter Brune     Output Parameter:
2875b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2876b0ae01b7SPeter Brune 
2877b0ae01b7SPeter Brune     Level: developer
2878b0ae01b7SPeter Brune 
2879b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2880b0ae01b7SPeter Brune 
2881b0ae01b7SPeter Brune @*/
2882b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2883b0ae01b7SPeter Brune {
2884b0ae01b7SPeter Brune   PetscFunctionBegin;
2885b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2886b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2887b0ae01b7SPeter Brune }
2888b0ae01b7SPeter Brune 
2889b0ae01b7SPeter Brune #undef __FUNCT__
28903ad4599aSBarry Smith #define __FUNCT__ "DMHasCreateRestriction"
28913ad4599aSBarry Smith /*@
28923ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
28933ad4599aSBarry Smith 
28943ad4599aSBarry Smith     Not Collective
28953ad4599aSBarry Smith 
28963ad4599aSBarry Smith     Input Parameter:
28973ad4599aSBarry Smith .   dm - the DM object
28983ad4599aSBarry Smith 
28993ad4599aSBarry Smith     Output Parameter:
29003ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
29013ad4599aSBarry Smith 
29023ad4599aSBarry Smith     Level: developer
29033ad4599aSBarry Smith 
29043ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
29053ad4599aSBarry Smith 
29063ad4599aSBarry Smith @*/
29073ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
29083ad4599aSBarry Smith {
29093ad4599aSBarry Smith   PetscFunctionBegin;
29103ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
29113ad4599aSBarry Smith   PetscFunctionReturn(0);
29123ad4599aSBarry Smith }
29133ad4599aSBarry Smith 
29143ad4599aSBarry Smith #undef  __FUNCT__
291508da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2916748fac09SDmitry Karpeev /*@C
291708da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
291808da532bSDmitry Karpeev 
291908da532bSDmitry Karpeev     Collective on DM
292008da532bSDmitry Karpeev 
292108da532bSDmitry Karpeev     Input Parameter:
292208da532bSDmitry Karpeev +   dm - the DM object
29230298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
292408da532bSDmitry Karpeev 
292508da532bSDmitry Karpeev     Level: developer
292608da532bSDmitry Karpeev 
292774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
292808da532bSDmitry Karpeev 
292908da532bSDmitry Karpeev @*/
293008da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
293108da532bSDmitry Karpeev {
293208da532bSDmitry Karpeev   PetscErrorCode ierr;
29335fd66863SKarl Rupp 
293408da532bSDmitry Karpeev   PetscFunctionBegin;
293508da532bSDmitry Karpeev   if (x) {
293608da532bSDmitry Karpeev     if (!dm->x) {
293708da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
293808da532bSDmitry Karpeev     }
293908da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
29408865f1eaSKarl Rupp   } else if (dm->x) {
294108da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
294208da532bSDmitry Karpeev   }
294308da532bSDmitry Karpeev   PetscFunctionReturn(0);
294408da532bSDmitry Karpeev }
294508da532bSDmitry Karpeev 
29460298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2947264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2948264ace61SBarry Smith 
2949264ace61SBarry Smith #undef __FUNCT__
2950264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2951264ace61SBarry Smith /*@C
2952264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2953264ace61SBarry Smith 
2954264ace61SBarry Smith   Collective on DM
2955264ace61SBarry Smith 
2956264ace61SBarry Smith   Input Parameters:
2957264ace61SBarry Smith + dm     - The DM object
2958264ace61SBarry Smith - method - The name of the DM type
2959264ace61SBarry Smith 
2960264ace61SBarry Smith   Options Database Key:
2961264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2962264ace61SBarry Smith 
2963264ace61SBarry Smith   Notes:
2964e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2965264ace61SBarry Smith 
2966264ace61SBarry Smith   Level: intermediate
2967264ace61SBarry Smith 
2968264ace61SBarry Smith .keywords: DM, set, type
2969264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2970264ace61SBarry Smith @*/
297119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2972264ace61SBarry Smith {
2973264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2974264ace61SBarry Smith   PetscBool      match;
2975264ace61SBarry Smith   PetscErrorCode ierr;
2976264ace61SBarry Smith 
2977264ace61SBarry Smith   PetscFunctionBegin;
2978264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2979251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2980264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2981264ace61SBarry Smith 
29820f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
29831c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2984ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2985264ace61SBarry Smith 
2986264ace61SBarry Smith   if (dm->ops->destroy) {
2987264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
29880298fd71SBarry Smith     dm->ops->destroy = NULL;
2989264ace61SBarry Smith   }
2990264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2991264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2992264ace61SBarry Smith   PetscFunctionReturn(0);
2993264ace61SBarry Smith }
2994264ace61SBarry Smith 
2995264ace61SBarry Smith #undef __FUNCT__
2996264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2997264ace61SBarry Smith /*@C
2998264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2999264ace61SBarry Smith 
3000264ace61SBarry Smith   Not Collective
3001264ace61SBarry Smith 
3002264ace61SBarry Smith   Input Parameter:
3003264ace61SBarry Smith . dm  - The DM
3004264ace61SBarry Smith 
3005264ace61SBarry Smith   Output Parameter:
3006264ace61SBarry Smith . type - The DM type name
3007264ace61SBarry Smith 
3008264ace61SBarry Smith   Level: intermediate
3009264ace61SBarry Smith 
3010264ace61SBarry Smith .keywords: DM, get, type, name
3011264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3012264ace61SBarry Smith @*/
301319fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3014264ace61SBarry Smith {
3015264ace61SBarry Smith   PetscErrorCode ierr;
3016264ace61SBarry Smith 
3017264ace61SBarry Smith   PetscFunctionBegin;
3018264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3019c959eef4SJed Brown   PetscValidPointer(type,2);
3020607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3021264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3022264ace61SBarry Smith   PetscFunctionReturn(0);
3023264ace61SBarry Smith }
3024264ace61SBarry Smith 
302567a56275SMatthew G Knepley #undef __FUNCT__
302667a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
302767a56275SMatthew G Knepley /*@C
302867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
302967a56275SMatthew G Knepley 
303067a56275SMatthew G Knepley   Collective on DM
303167a56275SMatthew G Knepley 
303267a56275SMatthew G Knepley   Input Parameters:
303367a56275SMatthew G Knepley + dm - the DM
303467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
303567a56275SMatthew G Knepley 
303667a56275SMatthew G Knepley   Output Parameter:
303767a56275SMatthew G Knepley . M - pointer to new DM
303867a56275SMatthew G Knepley 
303967a56275SMatthew G Knepley   Notes:
304067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
304167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
304267a56275SMatthew G Knepley   of the input DM.
304367a56275SMatthew G Knepley 
304467a56275SMatthew G Knepley   Level: intermediate
304567a56275SMatthew G Knepley 
304667a56275SMatthew G Knepley .seealso: DMCreate()
304767a56275SMatthew G Knepley @*/
304819fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
304967a56275SMatthew G Knepley {
305067a56275SMatthew G Knepley   DM             B;
305167a56275SMatthew G Knepley   char           convname[256];
3052c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
305367a56275SMatthew G Knepley   PetscErrorCode ierr;
305467a56275SMatthew G Knepley 
305567a56275SMatthew G Knepley   PetscFunctionBegin;
305667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
305767a56275SMatthew G Knepley   PetscValidType(dm,1);
305867a56275SMatthew G Knepley   PetscValidPointer(M,3);
3059251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3060c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3061c067b6caSMatthew G. Knepley   if (sametype) {
3062c067b6caSMatthew G. Knepley     *M   = dm;
3063c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3064c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3065c067b6caSMatthew G. Knepley   } else {
30660298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
306767a56275SMatthew G Knepley 
306867a56275SMatthew G Knepley     /*
306967a56275SMatthew G Knepley        Order of precedence:
307067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
307167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
307267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
307367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
307467a56275SMatthew G Knepley        5) Use a really basic converter.
307567a56275SMatthew G Knepley     */
307667a56275SMatthew G Knepley 
307767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
307867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
307967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
308067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
308167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
308267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
30830005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
308467a56275SMatthew G Knepley     if (conv) goto foundconv;
308567a56275SMatthew G Knepley 
308667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
308782f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
308867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
308967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
309067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
309167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
309267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
309367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
30940005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
309567a56275SMatthew G Knepley     if (conv) {
3096fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
309767a56275SMatthew G Knepley       goto foundconv;
309867a56275SMatthew G Knepley     }
309967a56275SMatthew G Knepley 
310067a56275SMatthew G Knepley #if 0
310167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
310267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3103fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
310467a56275SMatthew G Knepley     if (conv) goto foundconv;
310567a56275SMatthew G Knepley 
310667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
310767a56275SMatthew G Knepley     if (dm->ops->convert) {
310867a56275SMatthew G Knepley       conv = dm->ops->convert;
310967a56275SMatthew G Knepley     }
311067a56275SMatthew G Knepley     if (conv) goto foundconv;
311167a56275SMatthew G Knepley #endif
311267a56275SMatthew G Knepley 
311367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
311482f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
311567a56275SMatthew G Knepley 
311667a56275SMatthew G Knepley foundconv:
311767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
311867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
311912fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
312012fa691eSMatthew G. Knepley     if (dm->maxCell) {
312112fa691eSMatthew G. Knepley       const PetscReal *maxCell, *L;
312212fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
312312fa691eSMatthew G. Knepley       ierr = DMGetPeriodicity(dm, &maxCell, &L, &bd);CHKERRQ(ierr);
312412fa691eSMatthew G. Knepley       ierr = DMSetPeriodicity(*M,  maxCell,  L,  bd);CHKERRQ(ierr);
312512fa691eSMatthew G. Knepley     }
312667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
312767a56275SMatthew G Knepley   }
312867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
312967a56275SMatthew G Knepley   PetscFunctionReturn(0);
313067a56275SMatthew G Knepley }
3131264ace61SBarry Smith 
3132264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3133264ace61SBarry Smith 
3134264ace61SBarry Smith #undef __FUNCT__
3135264ace61SBarry Smith #define __FUNCT__ "DMRegister"
3136264ace61SBarry Smith /*@C
31371c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
31381c84c290SBarry Smith 
31391c84c290SBarry Smith   Not Collective
31401c84c290SBarry Smith 
31411c84c290SBarry Smith   Input Parameters:
31421c84c290SBarry Smith + name        - The name of a new user-defined creation routine
31431c84c290SBarry Smith - create_func - The creation routine itself
31441c84c290SBarry Smith 
31451c84c290SBarry Smith   Notes:
31461c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
31471c84c290SBarry Smith 
31481c84c290SBarry Smith 
31491c84c290SBarry Smith   Sample usage:
31501c84c290SBarry Smith .vb
3151bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
31521c84c290SBarry Smith .ve
31531c84c290SBarry Smith 
31541c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
31551c84c290SBarry Smith .vb
31561c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
31571c84c290SBarry Smith     DMSetType(DM,"my_da");
31581c84c290SBarry Smith .ve
31591c84c290SBarry Smith    or at runtime via the option
31601c84c290SBarry Smith .vb
31611c84c290SBarry Smith     -da_type my_da
31621c84c290SBarry Smith .ve
3163264ace61SBarry Smith 
3164264ace61SBarry Smith   Level: advanced
31651c84c290SBarry Smith 
31661c84c290SBarry Smith .keywords: DM, register
3167bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
31681c84c290SBarry Smith 
3169264ace61SBarry Smith @*/
3170bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3171264ace61SBarry Smith {
3172264ace61SBarry Smith   PetscErrorCode ierr;
3173264ace61SBarry Smith 
3174264ace61SBarry Smith   PetscFunctionBegin;
3175a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3176264ace61SBarry Smith   PetscFunctionReturn(0);
3177264ace61SBarry Smith }
3178264ace61SBarry Smith 
3179b859378eSBarry Smith #undef __FUNCT__
3180b859378eSBarry Smith #define __FUNCT__ "DMLoad"
3181b859378eSBarry Smith /*@C
318255849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3183b859378eSBarry Smith 
3184b859378eSBarry Smith   Collective on PetscViewer
3185b859378eSBarry Smith 
3186b859378eSBarry Smith   Input Parameters:
3187b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3188b859378eSBarry Smith            some related function before a call to DMLoad().
3189b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3190b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3191b859378eSBarry Smith 
3192b859378eSBarry Smith    Level: intermediate
3193b859378eSBarry Smith 
3194b859378eSBarry Smith   Notes:
319555849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3196b859378eSBarry Smith 
3197b859378eSBarry Smith   Notes for advanced users:
3198b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3199b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3200b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3201b859378eSBarry Smith   format is
3202b859378eSBarry Smith .vb
3203b859378eSBarry Smith      has not yet been determined
3204b859378eSBarry Smith .ve
3205b859378eSBarry Smith 
3206b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3207b859378eSBarry Smith @*/
3208b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3209b859378eSBarry Smith {
32109331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3211b859378eSBarry Smith   PetscErrorCode ierr;
3212b859378eSBarry Smith 
3213b859378eSBarry Smith   PetscFunctionBegin;
3214b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3215b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
321632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
32179331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
32189331c7a4SMatthew G. Knepley   if (isbinary) {
32199331c7a4SMatthew G. Knepley     PetscInt classid;
32209331c7a4SMatthew G. Knepley     char     type[256];
3221b859378eSBarry Smith 
3222060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
32239200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3224060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
322532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
32269331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32279331c7a4SMatthew G. Knepley   } else if (ishdf5) {
32289331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32299331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3230b859378eSBarry Smith   PetscFunctionReturn(0);
3231b859378eSBarry Smith }
3232b859378eSBarry Smith 
32337da65231SMatthew G Knepley /******************************** FEM Support **********************************/
32347da65231SMatthew G Knepley 
32357da65231SMatthew G Knepley #undef __FUNCT__
32367da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3237a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3238a6dfd86eSKarl Rupp {
32391d47ebbbSSatish Balay   PetscInt       f;
32401b30c384SMatthew G Knepley   PetscErrorCode ierr;
32411b30c384SMatthew G Knepley 
32427da65231SMatthew G Knepley   PetscFunctionBegin;
324374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32441d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
324557622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
32467da65231SMatthew G Knepley   }
32477da65231SMatthew G Knepley   PetscFunctionReturn(0);
32487da65231SMatthew G Knepley }
32497da65231SMatthew G Knepley 
32507da65231SMatthew G Knepley #undef __FUNCT__
32517da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3252a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3253a6dfd86eSKarl Rupp {
32541b30c384SMatthew G Knepley   PetscInt       f, g;
32557da65231SMatthew G Knepley   PetscErrorCode ierr;
32567da65231SMatthew G Knepley 
32577da65231SMatthew G Knepley   PetscFunctionBegin;
325874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32591d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
326074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
32611d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3262e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
32637da65231SMatthew G Knepley     }
326474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
32657da65231SMatthew G Knepley   }
32667da65231SMatthew G Knepley   PetscFunctionReturn(0);
32677da65231SMatthew G Knepley }
3268e7c4fc90SDmitry Karpeev 
3269970e74d5SMatthew G Knepley #undef __FUNCT__
3270e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
32716113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3272e759306cSMatthew G. Knepley {
3273e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3274e759306cSMatthew G. Knepley   PetscInt       p;
3275e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3276e759306cSMatthew G. Knepley 
3277e759306cSMatthew G. Knepley   PetscFunctionBegin;
3278e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3279e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3280e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3281e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3282e759306cSMatthew G. Knepley     if (p == rank) {
3283e759306cSMatthew G. Knepley       Vec x;
3284e759306cSMatthew G. Knepley 
3285e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3286e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
32876113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3288e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3289e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3290e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3291e759306cSMatthew G. Knepley     }
3292e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3293e759306cSMatthew G. Knepley   }
3294e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3295e759306cSMatthew G. Knepley }
3296e759306cSMatthew G. Knepley 
3297e759306cSMatthew G. Knepley #undef __FUNCT__
329888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
329988ed4aceSMatthew G Knepley /*@
330088ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
330188ed4aceSMatthew G Knepley 
330288ed4aceSMatthew G Knepley   Input Parameter:
330388ed4aceSMatthew G Knepley . dm - The DM
330488ed4aceSMatthew G Knepley 
330588ed4aceSMatthew G Knepley   Output Parameter:
330688ed4aceSMatthew G Knepley . section - The PetscSection
330788ed4aceSMatthew G Knepley 
330888ed4aceSMatthew G Knepley   Level: intermediate
330988ed4aceSMatthew G Knepley 
331088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
331188ed4aceSMatthew G Knepley 
331288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
331388ed4aceSMatthew G Knepley @*/
33140adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
33150adebc6cSBarry Smith {
3316fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3317fd59a867SMatthew G. Knepley 
331888ed4aceSMatthew G Knepley   PetscFunctionBegin;
331988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
332088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
33212f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
33222f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3323ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
33242f0f8703SMatthew G. Knepley   }
332588ed4aceSMatthew G Knepley   *section = dm->defaultSection;
332688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
332788ed4aceSMatthew G Knepley }
332888ed4aceSMatthew G Knepley 
332988ed4aceSMatthew G Knepley #undef __FUNCT__
333088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
333188ed4aceSMatthew G Knepley /*@
333288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
333388ed4aceSMatthew G Knepley 
333488ed4aceSMatthew G Knepley   Input Parameters:
333588ed4aceSMatthew G Knepley + dm - The DM
333688ed4aceSMatthew G Knepley - section - The PetscSection
333788ed4aceSMatthew G Knepley 
333888ed4aceSMatthew G Knepley   Level: intermediate
333988ed4aceSMatthew G Knepley 
334088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
334188ed4aceSMatthew G Knepley 
334288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
334388ed4aceSMatthew G Knepley @*/
33440adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
33450adebc6cSBarry Smith {
3346c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3347af122d2aSMatthew G Knepley   PetscInt       f;
334888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
334988ed4aceSMatthew G Knepley 
335088ed4aceSMatthew G Knepley   PetscFunctionBegin;
335188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3352c473ab19SMatthew G. Knepley   if (section) {
33531d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33541d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3355c473ab19SMatthew G. Knepley   }
335688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
335788ed4aceSMatthew G Knepley   dm->defaultSection = section;
3358c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3359af122d2aSMatthew G Knepley   if (numFields) {
3360af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3361af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
33620f21e855SMatthew G. Knepley       PetscObject disc;
3363af122d2aSMatthew G Knepley       const char *name;
3364af122d2aSMatthew G Knepley 
3365af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
33660f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
33670f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3368af122d2aSMatthew G Knepley     }
3369af122d2aSMatthew G Knepley   }
33701d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
33711d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
337288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
337388ed4aceSMatthew G Knepley }
337488ed4aceSMatthew G Knepley 
337588ed4aceSMatthew G Knepley #undef __FUNCT__
33769435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
33779435951eSToby Isaac /*@
33789435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
33799435951eSToby Isaac 
3380e228b242SToby Isaac   not collective
3381e228b242SToby Isaac 
33829435951eSToby Isaac   Input Parameter:
33839435951eSToby Isaac . dm - The DM
33849435951eSToby Isaac 
33859435951eSToby Isaac   Output Parameter:
33869435951eSToby 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.
33879435951eSToby 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.
33889435951eSToby Isaac 
33899435951eSToby Isaac   Level: advanced
33909435951eSToby Isaac 
33919435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
33929435951eSToby Isaac 
33939435951eSToby Isaac .seealso: DMSetDefaultConstraints()
33949435951eSToby Isaac @*/
33959435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
33969435951eSToby Isaac {
33979435951eSToby Isaac   PetscErrorCode ierr;
33989435951eSToby Isaac 
33999435951eSToby Isaac   PetscFunctionBegin;
34009435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34019435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
340245a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
340345a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
34049435951eSToby Isaac   PetscFunctionReturn(0);
34059435951eSToby Isaac }
34069435951eSToby Isaac 
34079435951eSToby Isaac #undef __FUNCT__
34089435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
34099435951eSToby Isaac /*@
34109435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
34119435951eSToby Isaac 
34129435951eSToby 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().
34139435951eSToby Isaac 
34149435951eSToby 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.
34159435951eSToby Isaac 
3416e228b242SToby Isaac   collective on dm
3417e228b242SToby Isaac 
34189435951eSToby Isaac   Input Parameters:
34199435951eSToby Isaac + dm - The DM
3420e228b242SToby 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).
3421e228b242SToby 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).
34229435951eSToby Isaac 
34239435951eSToby Isaac   Level: advanced
34249435951eSToby Isaac 
34259435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
34269435951eSToby Isaac 
34279435951eSToby Isaac .seealso: DMGetDefaultConstraints()
34289435951eSToby Isaac @*/
34299435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
34309435951eSToby Isaac {
3431e228b242SToby Isaac   PetscMPIInt result;
34329435951eSToby Isaac   PetscErrorCode ierr;
34339435951eSToby Isaac 
34349435951eSToby Isaac   PetscFunctionBegin;
34359435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3436e228b242SToby Isaac   if (section) {
3437e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3438e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3439f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3440e228b242SToby Isaac   }
3441e228b242SToby Isaac   if (mat) {
3442e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3443e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3444f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3445e228b242SToby Isaac   }
34469435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
34479435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
34489435951eSToby Isaac   dm->defaultConstraintSection = section;
34499435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
34509435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
34519435951eSToby Isaac   dm->defaultConstraintMat = mat;
34529435951eSToby Isaac   PetscFunctionReturn(0);
34539435951eSToby Isaac }
34549435951eSToby Isaac 
3455f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
34569435951eSToby Isaac #undef __FUNCT__
3457284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3458507e4973SMatthew G. Knepley /*
3459507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3460507e4973SMatthew G. Knepley 
3461507e4973SMatthew G. Knepley   Input Parameters:
3462507e4973SMatthew G. Knepley + dm - The DM
3463507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3464507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3465507e4973SMatthew G. Knepley 
3466507e4973SMatthew G. Knepley   Level: intermediate
3467507e4973SMatthew G. Knepley 
3468507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3469507e4973SMatthew G. Knepley */
3470f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3471507e4973SMatthew G. Knepley {
3472507e4973SMatthew G. Knepley   MPI_Comm        comm;
3473507e4973SMatthew G. Knepley   PetscLayout     layout;
3474507e4973SMatthew G. Knepley   const PetscInt *ranges;
3475507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3476507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3477507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3478507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3479507e4973SMatthew G. Knepley 
3480507e4973SMatthew G. Knepley   PetscFunctionBegin;
3481507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3482507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3483507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3484507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3485507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3486507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3487507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3488507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3489507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3490507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3491507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3492507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3493f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3494507e4973SMatthew G. Knepley 
3495507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3496507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3497507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3498507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3499507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3500507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3501507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3502507e4973SMatthew 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;}
3503507e4973SMatthew 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;}
3504507e4973SMatthew G. Knepley     if (gdof < 0) {
3505507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3506507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3507507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3508507e4973SMatthew G. Knepley 
3509507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3510507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3511507e4973SMatthew 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;}
3512507e4973SMatthew G. Knepley       }
3513507e4973SMatthew G. Knepley     }
3514507e4973SMatthew G. Knepley   }
3515507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3516507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3517b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3518507e4973SMatthew G. Knepley   if (!gvalid) {
3519507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3520507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3521507e4973SMatthew G. Knepley   }
3522507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3523507e4973SMatthew G. Knepley }
3524f741bcd2SMatthew G. Knepley #endif
3525507e4973SMatthew G. Knepley 
3526507e4973SMatthew G. Knepley #undef __FUNCT__
352788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
352888ed4aceSMatthew G Knepley /*@
352988ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
353088ed4aceSMatthew G Knepley 
35318b1ab98fSJed Brown   Collective on DM
35328b1ab98fSJed Brown 
353388ed4aceSMatthew G Knepley   Input Parameter:
353488ed4aceSMatthew G Knepley . dm - The DM
353588ed4aceSMatthew G Knepley 
353688ed4aceSMatthew G Knepley   Output Parameter:
353788ed4aceSMatthew G Knepley . section - The PetscSection
353888ed4aceSMatthew G Knepley 
353988ed4aceSMatthew G Knepley   Level: intermediate
354088ed4aceSMatthew G Knepley 
354188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
354288ed4aceSMatthew G Knepley 
354388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
354488ed4aceSMatthew G Knepley @*/
35450adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
35460adebc6cSBarry Smith {
354788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
354888ed4aceSMatthew G Knepley 
354988ed4aceSMatthew G Knepley   PetscFunctionBegin;
355088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
355188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
355288ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3553fd59a867SMatthew G. Knepley     PetscSection s;
3554fd59a867SMatthew G. Knepley 
3555fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3556fd59a867SMatthew 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");
3557fd59a867SMatthew 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");
355815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3559cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3560ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3561685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
356288ed4aceSMatthew G Knepley   }
356388ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
356488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
356588ed4aceSMatthew G Knepley }
356688ed4aceSMatthew G Knepley 
356788ed4aceSMatthew G Knepley #undef __FUNCT__
3568b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3569b21d0597SMatthew G Knepley /*@
3570b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3571b21d0597SMatthew G Knepley 
3572b21d0597SMatthew G Knepley   Input Parameters:
3573b21d0597SMatthew G Knepley + dm - The DM
35745080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3575b21d0597SMatthew G Knepley 
3576b21d0597SMatthew G Knepley   Level: intermediate
3577b21d0597SMatthew G Knepley 
3578b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3579b21d0597SMatthew G Knepley 
3580b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3581b21d0597SMatthew G Knepley @*/
35820adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
35830adebc6cSBarry Smith {
3584b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3585b21d0597SMatthew G Knepley 
3586b21d0597SMatthew G Knepley   PetscFunctionBegin;
3587b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35885080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
35891d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3590b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3591b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3592507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3593f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3594507e4973SMatthew G. Knepley #endif
3595b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3596b21d0597SMatthew G Knepley }
3597b21d0597SMatthew G Knepley 
3598b21d0597SMatthew G Knepley #undef __FUNCT__
359988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
360088ed4aceSMatthew G Knepley /*@
360188ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
360288ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
360388ed4aceSMatthew G Knepley 
360488ed4aceSMatthew G Knepley   Input Parameter:
360588ed4aceSMatthew G Knepley . dm - The DM
360688ed4aceSMatthew G Knepley 
360788ed4aceSMatthew G Knepley   Output Parameter:
360888ed4aceSMatthew G Knepley . sf - The PetscSF
360988ed4aceSMatthew G Knepley 
361088ed4aceSMatthew G Knepley   Level: intermediate
361188ed4aceSMatthew G Knepley 
361288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
361388ed4aceSMatthew G Knepley 
361488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
361588ed4aceSMatthew G Knepley @*/
36160adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
36170adebc6cSBarry Smith {
361888ed4aceSMatthew G Knepley   PetscInt       nroots;
361988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
362088ed4aceSMatthew G Knepley 
362188ed4aceSMatthew G Knepley   PetscFunctionBegin;
362288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
362388ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
36240298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
362588ed4aceSMatthew G Knepley   if (nroots < 0) {
362688ed4aceSMatthew G Knepley     PetscSection section, gSection;
362788ed4aceSMatthew G Knepley 
362888ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
362931ea6d37SMatthew G Knepley     if (section) {
363088ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
363188ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
363231ea6d37SMatthew G Knepley     } else {
36330298fd71SBarry Smith       *sf = NULL;
363431ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
363531ea6d37SMatthew G Knepley     }
363688ed4aceSMatthew G Knepley   }
363788ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
363888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
363988ed4aceSMatthew G Knepley }
364088ed4aceSMatthew G Knepley 
364188ed4aceSMatthew G Knepley #undef __FUNCT__
364288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
364388ed4aceSMatthew G Knepley /*@
364488ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
364588ed4aceSMatthew G Knepley 
364688ed4aceSMatthew G Knepley   Input Parameters:
364788ed4aceSMatthew G Knepley + dm - The DM
364888ed4aceSMatthew G Knepley - sf - The PetscSF
364988ed4aceSMatthew G Knepley 
365088ed4aceSMatthew G Knepley   Level: intermediate
365188ed4aceSMatthew G Knepley 
365288ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
365388ed4aceSMatthew G Knepley 
365488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
365588ed4aceSMatthew G Knepley @*/
36560adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
36570adebc6cSBarry Smith {
365888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
365988ed4aceSMatthew G Knepley 
366088ed4aceSMatthew G Knepley   PetscFunctionBegin;
366188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
366288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
366388ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
366488ed4aceSMatthew G Knepley   dm->defaultSF = sf;
366588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
366688ed4aceSMatthew G Knepley }
366788ed4aceSMatthew G Knepley 
366888ed4aceSMatthew G Knepley #undef __FUNCT__
366988ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
367088ed4aceSMatthew G Knepley /*@C
367188ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
367288ed4aceSMatthew G Knepley   describing the data layout.
367388ed4aceSMatthew G Knepley 
367488ed4aceSMatthew G Knepley   Input Parameters:
367588ed4aceSMatthew G Knepley + dm - The DM
367688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
367788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
367888ed4aceSMatthew G Knepley 
367988ed4aceSMatthew G Knepley   Level: intermediate
368088ed4aceSMatthew G Knepley 
368188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
368288ed4aceSMatthew G Knepley @*/
368388ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
368488ed4aceSMatthew G Knepley {
368582f516ccSBarry Smith   MPI_Comm       comm;
368688ed4aceSMatthew G Knepley   PetscLayout    layout;
368788ed4aceSMatthew G Knepley   const PetscInt *ranges;
368888ed4aceSMatthew G Knepley   PetscInt       *local;
368988ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3690ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
369188ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
369288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
369388ed4aceSMatthew G Knepley 
369488ed4aceSMatthew G Knepley   PetscFunctionBegin;
369582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
369688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369788ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
369888ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
369988ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
370088ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
370188ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
370288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
370388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
370488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
370588ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3706ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
37076636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
370888ed4aceSMatthew G Knepley 
37096636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37106636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3711235fbf56SMatthew 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));
37126636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
371388ed4aceSMatthew G Knepley   }
3714785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3715785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
371688ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
37171f588964SMatthew G Knepley     const PetscInt *cind;
37186636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
371988ed4aceSMatthew G Knepley 
372088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
372188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
372288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
372388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
372488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37256636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
372688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
37276636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
37286636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
37296636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3730057b4bcdSMatthew 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);
37316636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
37326636e97aSMatthew G Knepley     }
373388ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
373488ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
373588ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
373688ed4aceSMatthew G Knepley     }
373788ed4aceSMatthew G Knepley     if (gdof < 0) {
37386636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
373988ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
374088ed4aceSMatthew G Knepley 
374105376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
374231d3f06eSJed Brown         if (r < 0) r = -(r+2);
374305376888SMatthew 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);
374488ed4aceSMatthew G Knepley         remote[l].rank  = r;
374588ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
374688ed4aceSMatthew G Knepley       }
374788ed4aceSMatthew G Knepley     } else {
37486636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
374988ed4aceSMatthew G Knepley         remote[l].rank  = rank;
375088ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
375188ed4aceSMatthew G Knepley       }
375288ed4aceSMatthew G Knepley     }
375388ed4aceSMatthew G Knepley   }
37546636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
375588ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
375688ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
375788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
375888ed4aceSMatthew G Knepley }
3759af122d2aSMatthew G Knepley 
3760af122d2aSMatthew G Knepley #undef __FUNCT__
3761b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3762b21d0597SMatthew G Knepley /*@
3763b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3764b21d0597SMatthew G Knepley 
3765b21d0597SMatthew G Knepley   Input Parameter:
3766b21d0597SMatthew G Knepley . dm - The DM
3767b21d0597SMatthew G Knepley 
3768b21d0597SMatthew G Knepley   Output Parameter:
3769b21d0597SMatthew G Knepley . sf - The PetscSF
3770b21d0597SMatthew G Knepley 
3771b21d0597SMatthew G Knepley   Level: intermediate
3772b21d0597SMatthew G Knepley 
3773b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3774b21d0597SMatthew G Knepley 
3775057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3776b21d0597SMatthew G Knepley @*/
37770adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
37780adebc6cSBarry Smith {
3779b21d0597SMatthew G Knepley   PetscFunctionBegin;
3780b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3781b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3782b21d0597SMatthew G Knepley   *sf = dm->sf;
3783b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3784b21d0597SMatthew G Knepley }
3785b21d0597SMatthew G Knepley 
3786b21d0597SMatthew G Knepley #undef __FUNCT__
3787057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3788057b4bcdSMatthew G Knepley /*@
3789057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3790057b4bcdSMatthew G Knepley 
3791057b4bcdSMatthew G Knepley   Input Parameters:
3792057b4bcdSMatthew G Knepley + dm - The DM
3793057b4bcdSMatthew G Knepley - sf - The PetscSF
3794057b4bcdSMatthew G Knepley 
3795057b4bcdSMatthew G Knepley   Level: intermediate
3796057b4bcdSMatthew G Knepley 
3797057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3798057b4bcdSMatthew G Knepley @*/
37990adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
38000adebc6cSBarry Smith {
3801057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3802057b4bcdSMatthew G Knepley 
3803057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3804057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3805057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3806057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3807057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3808057b4bcdSMatthew G Knepley   dm->sf = sf;
3809057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3810057b4bcdSMatthew G Knepley }
3811057b4bcdSMatthew G Knepley 
3812057b4bcdSMatthew G Knepley #undef __FUNCT__
38132764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
38142764a2aaSMatthew G. Knepley /*@
38152764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
38162764a2aaSMatthew G. Knepley 
38172764a2aaSMatthew G. Knepley   Input Parameter:
38182764a2aaSMatthew G. Knepley . dm - The DM
38192764a2aaSMatthew G. Knepley 
38202764a2aaSMatthew G. Knepley   Output Parameter:
38212764a2aaSMatthew G. Knepley . prob - The PetscDS
38222764a2aaSMatthew G. Knepley 
38232764a2aaSMatthew G. Knepley   Level: developer
38242764a2aaSMatthew G. Knepley 
38252764a2aaSMatthew G. Knepley .seealso: DMSetDS()
38262764a2aaSMatthew G. Knepley @*/
38272764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3828af122d2aSMatthew G Knepley {
3829af122d2aSMatthew G Knepley   PetscFunctionBegin;
3830af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38310f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
38320f21e855SMatthew G. Knepley   *prob = dm->prob;
38330f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38340f21e855SMatthew G. Knepley }
38350f21e855SMatthew G. Knepley 
38360f21e855SMatthew G. Knepley #undef __FUNCT__
38372764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
38382764a2aaSMatthew G. Knepley /*@
38392764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
38402764a2aaSMatthew G. Knepley 
38412764a2aaSMatthew G. Knepley   Input Parameters:
38422764a2aaSMatthew G. Knepley + dm - The DM
38432764a2aaSMatthew G. Knepley - prob - The PetscDS
38442764a2aaSMatthew G. Knepley 
38452764a2aaSMatthew G. Knepley   Level: developer
38462764a2aaSMatthew G. Knepley 
38472764a2aaSMatthew G. Knepley .seealso: DMGetDS()
38482764a2aaSMatthew G. Knepley @*/
38492764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
38500f21e855SMatthew G. Knepley {
38510f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38520f21e855SMatthew G. Knepley 
38530f21e855SMatthew G. Knepley   PetscFunctionBegin;
38540f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38552764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
38562764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
38570f21e855SMatthew G. Knepley   dm->prob = prob;
38580f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
38590f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38600f21e855SMatthew G. Knepley }
38610f21e855SMatthew G. Knepley 
38620f21e855SMatthew G. Knepley #undef __FUNCT__
38630f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
38640f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
38650f21e855SMatthew G. Knepley {
38660f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38670f21e855SMatthew G. Knepley 
38680f21e855SMatthew G. Knepley   PetscFunctionBegin;
38690f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38702764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3871af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3872af122d2aSMatthew G Knepley }
3873af122d2aSMatthew G Knepley 
3874af122d2aSMatthew G Knepley #undef __FUNCT__
3875af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3876af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3877af122d2aSMatthew G Knepley {
38780f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3879af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3880af122d2aSMatthew G Knepley 
3881af122d2aSMatthew G Knepley   PetscFunctionBegin;
3882af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38832764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
38840f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
38850f21e855SMatthew G. Knepley     PetscContainer obj;
38860f21e855SMatthew G. Knepley 
38870f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
38882764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
38890f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3890af122d2aSMatthew G Knepley   }
3891af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3892af122d2aSMatthew G Knepley }
3893af122d2aSMatthew G Knepley 
3894af122d2aSMatthew G Knepley #undef __FUNCT__
3895af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3896c1929be8SMatthew G. Knepley /*@
3897c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3898c1929be8SMatthew G. Knepley 
3899c1929be8SMatthew G. Knepley   Not collective
3900c1929be8SMatthew G. Knepley 
3901c1929be8SMatthew G. Knepley   Input Parameters:
3902c1929be8SMatthew G. Knepley + dm - The DM
3903c1929be8SMatthew G. Knepley - f  - The field number
3904c1929be8SMatthew G. Knepley 
3905c1929be8SMatthew G. Knepley   Output Parameter:
3906c1929be8SMatthew G. Knepley . field - The discretization object
3907c1929be8SMatthew G. Knepley 
3908c1929be8SMatthew G. Knepley   Level: developer
3909c1929be8SMatthew G. Knepley 
3910c1929be8SMatthew G. Knepley .seealso: DMSetField()
3911c1929be8SMatthew G. Knepley @*/
3912af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3913af122d2aSMatthew G Knepley {
39140f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39150f21e855SMatthew G. Knepley 
3916af122d2aSMatthew G Knepley   PetscFunctionBegin;
3917af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39182764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3919decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3920decb47aaSMatthew G. Knepley }
3921decb47aaSMatthew G. Knepley 
3922decb47aaSMatthew G. Knepley #undef __FUNCT__
3923decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3924c1929be8SMatthew G. Knepley /*@
3925c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3926c1929be8SMatthew G. Knepley 
3927c1929be8SMatthew G. Knepley   Logically collective on DM
3928c1929be8SMatthew G. Knepley 
3929c1929be8SMatthew G. Knepley   Input Parameters:
3930c1929be8SMatthew G. Knepley + dm - The DM
3931c1929be8SMatthew G. Knepley . f  - The field number
3932c1929be8SMatthew G. Knepley - field - The discretization object
3933c1929be8SMatthew G. Knepley 
3934c1929be8SMatthew G. Knepley   Level: developer
3935c1929be8SMatthew G. Knepley 
3936c1929be8SMatthew G. Knepley .seealso: DMGetField()
3937c1929be8SMatthew G. Knepley @*/
3938decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3939decb47aaSMatthew G. Knepley {
3940decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3941decb47aaSMatthew G. Knepley 
3942decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3943decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39442764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3945af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3946af122d2aSMatthew G Knepley }
39476636e97aSMatthew G Knepley 
39486636e97aSMatthew G Knepley #undef __FUNCT__
3949b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3950b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3951b64e0483SPeter Brune {
3952b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3953b64e0483SPeter Brune   PetscErrorCode ierr;
3954b64e0483SPeter Brune   Vec coords,ccoords;
39556dbf9973SLawrence Mitchell   Mat inject;
3956b64e0483SPeter Brune   PetscFunctionBegin;
3957b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3958b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3959b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3960b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3961b64e0483SPeter Brune   if (coords && !ccoords) {
3962b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
39636dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
39642adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
39656dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3966b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3967b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3968b64e0483SPeter Brune   }
3969b64e0483SPeter Brune   PetscFunctionReturn(0);
3970b64e0483SPeter Brune }
3971b64e0483SPeter Brune 
3972b64e0483SPeter Brune #undef __FUNCT__
397303dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
397403dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
397503dadc2fSPeter Brune {
397603dadc2fSPeter Brune   DM dm_coord,subdm_coord;
397703dadc2fSPeter Brune   PetscErrorCode ierr;
397803dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
397903dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
398003dadc2fSPeter Brune   PetscFunctionBegin;
398103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
398203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
398303dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
398403dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
398503dadc2fSPeter Brune   if (coords && !ccoords) {
398603dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
398703dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
398824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
398903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
399003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
399103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
399203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
399303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
399403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
399503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
399603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
399703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
399803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
399903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
400003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
400103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
400203dadc2fSPeter Brune   }
400303dadc2fSPeter Brune   PetscFunctionReturn(0);
400403dadc2fSPeter Brune }
400503dadc2fSPeter Brune 
400603dadc2fSPeter Brune #undef __FUNCT__
4007c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
4008c73cfb54SMatthew G. Knepley /*@
4009c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4010c73cfb54SMatthew G. Knepley 
4011c73cfb54SMatthew G. Knepley   Not collective
4012c73cfb54SMatthew G. Knepley 
4013c73cfb54SMatthew G. Knepley   Input Parameter:
4014c73cfb54SMatthew G. Knepley . dm - The DM
4015c73cfb54SMatthew G. Knepley 
4016c73cfb54SMatthew G. Knepley   Output Parameter:
4017c73cfb54SMatthew G. Knepley . dim - The topological dimension
4018c73cfb54SMatthew G. Knepley 
4019c73cfb54SMatthew G. Knepley   Level: beginner
4020c73cfb54SMatthew G. Knepley 
4021c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4022c73cfb54SMatthew G. Knepley @*/
4023c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4024c73cfb54SMatthew G. Knepley {
4025c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4026c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4027c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4028c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4029c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4030c73cfb54SMatthew G. Knepley }
4031c73cfb54SMatthew G. Knepley 
4032c73cfb54SMatthew G. Knepley #undef __FUNCT__
4033c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
4034c73cfb54SMatthew G. Knepley /*@
4035c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4036c73cfb54SMatthew G. Knepley 
4037c73cfb54SMatthew G. Knepley   Collective on dm
4038c73cfb54SMatthew G. Knepley 
4039c73cfb54SMatthew G. Knepley   Input Parameters:
4040c73cfb54SMatthew G. Knepley + dm - The DM
4041c73cfb54SMatthew G. Knepley - dim - The topological dimension
4042c73cfb54SMatthew G. Knepley 
4043c73cfb54SMatthew G. Knepley   Level: beginner
4044c73cfb54SMatthew G. Knepley 
4045c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4046c73cfb54SMatthew G. Knepley @*/
4047c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4048c73cfb54SMatthew G. Knepley {
4049c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4050c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4051c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4052c73cfb54SMatthew G. Knepley   dm->dim = dim;
4053c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4054c73cfb54SMatthew G. Knepley }
4055c73cfb54SMatthew G. Knepley 
4056793f3fe5SMatthew G. Knepley #undef __FUNCT__
4057793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
4058793f3fe5SMatthew G. Knepley /*@
4059793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4060793f3fe5SMatthew G. Knepley 
4061793f3fe5SMatthew G. Knepley   Collective on DM
4062793f3fe5SMatthew G. Knepley 
4063793f3fe5SMatthew G. Knepley   Input Parameters:
4064793f3fe5SMatthew G. Knepley + dm - the DM
4065793f3fe5SMatthew G. Knepley - dim - the dimension
4066793f3fe5SMatthew G. Knepley 
4067793f3fe5SMatthew G. Knepley   Output Parameters:
4068793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4069793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4070793f3fe5SMatthew G. Knepley 
4071793f3fe5SMatthew G. Knepley   Note:
4072793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4073793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4074793f3fe5SMatthew G. Knepley   then the interval is empty.
4075793f3fe5SMatthew G. Knepley 
4076793f3fe5SMatthew G. Knepley   Level: intermediate
4077793f3fe5SMatthew G. Knepley 
4078793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4079793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4080793f3fe5SMatthew G. Knepley @*/
4081793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4082793f3fe5SMatthew G. Knepley {
4083793f3fe5SMatthew G. Knepley   PetscInt       d;
4084793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4085793f3fe5SMatthew G. Knepley 
4086793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4087793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4088793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4089793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4090793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4091793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4092793f3fe5SMatthew G. Knepley }
4093793f3fe5SMatthew G. Knepley 
4094793f3fe5SMatthew G. Knepley #undef __FUNCT__
40956636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
40966636e97aSMatthew G Knepley /*@
40976636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
40986636e97aSMatthew G Knepley 
40996636e97aSMatthew G Knepley   Collective on DM
41006636e97aSMatthew G Knepley 
41016636e97aSMatthew G Knepley   Input Parameters:
41026636e97aSMatthew G Knepley + dm - the DM
41036636e97aSMatthew G Knepley - c - coordinate vector
41046636e97aSMatthew G Knepley 
41056636e97aSMatthew G Knepley   Note:
41066636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
41076636e97aSMatthew G Knepley 
41086636e97aSMatthew G Knepley   Level: intermediate
41096636e97aSMatthew G Knepley 
41106636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41116636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
41126636e97aSMatthew G Knepley @*/
41136636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
41146636e97aSMatthew G Knepley {
41156636e97aSMatthew G Knepley   PetscErrorCode ierr;
41166636e97aSMatthew G Knepley 
41176636e97aSMatthew G Knepley   PetscFunctionBegin;
41186636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41196636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41206636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41216636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41226636e97aSMatthew G Knepley   dm->coordinates = c;
41236636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4124b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
412503dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
41266636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41276636e97aSMatthew G Knepley }
41286636e97aSMatthew G Knepley 
41296636e97aSMatthew G Knepley #undef __FUNCT__
41306636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
41316636e97aSMatthew G Knepley /*@
41326636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
41336636e97aSMatthew G Knepley 
41346636e97aSMatthew G Knepley   Collective on DM
41356636e97aSMatthew G Knepley 
41366636e97aSMatthew G Knepley    Input Parameters:
41376636e97aSMatthew G Knepley +  dm - the DM
41386636e97aSMatthew G Knepley -  c - coordinate vector
41396636e97aSMatthew G Knepley 
41406636e97aSMatthew G Knepley   Note:
41416636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
41426636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
41436636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
41446636e97aSMatthew G Knepley 
41456636e97aSMatthew G Knepley   Level: intermediate
41466636e97aSMatthew G Knepley 
41476636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41486636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
41496636e97aSMatthew G Knepley @*/
41506636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
41516636e97aSMatthew G Knepley {
41526636e97aSMatthew G Knepley   PetscErrorCode ierr;
41536636e97aSMatthew G Knepley 
41546636e97aSMatthew G Knepley   PetscFunctionBegin;
41556636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41566636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41576636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41586636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
41598865f1eaSKarl Rupp 
41606636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
41618865f1eaSKarl Rupp 
41626636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41636636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41646636e97aSMatthew G Knepley }
41656636e97aSMatthew G Knepley 
41666636e97aSMatthew G Knepley #undef __FUNCT__
41676636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
41686636e97aSMatthew G Knepley /*@
41696636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
41706636e97aSMatthew G Knepley 
41716636e97aSMatthew G Knepley   Not Collective
41726636e97aSMatthew G Knepley 
41736636e97aSMatthew G Knepley   Input Parameter:
41746636e97aSMatthew G Knepley . dm - the DM
41756636e97aSMatthew G Knepley 
41766636e97aSMatthew G Knepley   Output Parameter:
41776636e97aSMatthew G Knepley . c - global coordinate vector
41786636e97aSMatthew G Knepley 
41796636e97aSMatthew G Knepley   Note:
41806636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
41816636e97aSMatthew G Knepley 
41826636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
41836636e97aSMatthew G Knepley 
41846636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
41856636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
41866636e97aSMatthew G Knepley 
41876636e97aSMatthew G Knepley   Level: intermediate
41886636e97aSMatthew G Knepley 
41896636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41906636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
41916636e97aSMatthew G Knepley @*/
41926636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
41936636e97aSMatthew G Knepley {
41946636e97aSMatthew G Knepley   PetscErrorCode ierr;
41956636e97aSMatthew G Knepley 
41966636e97aSMatthew G Knepley   PetscFunctionBegin;
41976636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41986636e97aSMatthew G Knepley   PetscValidPointer(c,2);
41991f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
42000298fd71SBarry Smith     DM cdm = NULL;
42016636e97aSMatthew G Knepley 
42026636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42036636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
42046636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
42056636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42066636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42076636e97aSMatthew G Knepley   }
42086636e97aSMatthew G Knepley   *c = dm->coordinates;
42096636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42106636e97aSMatthew G Knepley }
42116636e97aSMatthew G Knepley 
42126636e97aSMatthew G Knepley #undef __FUNCT__
42136636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
42146636e97aSMatthew G Knepley /*@
42156636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
42166636e97aSMatthew G Knepley 
42176636e97aSMatthew G Knepley   Collective on DM
42186636e97aSMatthew G Knepley 
42196636e97aSMatthew G Knepley   Input Parameter:
42206636e97aSMatthew G Knepley . dm - the DM
42216636e97aSMatthew G Knepley 
42226636e97aSMatthew G Knepley   Output Parameter:
42236636e97aSMatthew G Knepley . c - coordinate vector
42246636e97aSMatthew G Knepley 
42256636e97aSMatthew G Knepley   Note:
42266636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42276636e97aSMatthew G Knepley 
42286636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
42296636e97aSMatthew G Knepley 
42306636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42316636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42326636e97aSMatthew G Knepley 
42336636e97aSMatthew G Knepley   Level: intermediate
42346636e97aSMatthew G Knepley 
42356636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42366636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
42376636e97aSMatthew G Knepley @*/
42386636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
42396636e97aSMatthew G Knepley {
42406636e97aSMatthew G Knepley   PetscErrorCode ierr;
42416636e97aSMatthew G Knepley 
42426636e97aSMatthew G Knepley   PetscFunctionBegin;
42436636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42446636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42451f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
42460298fd71SBarry Smith     DM cdm = NULL;
42476636e97aSMatthew G Knepley 
42486636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42496636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
42506636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
42516636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42526636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42536636e97aSMatthew G Knepley   }
42546636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
42556636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42566636e97aSMatthew G Knepley }
42576636e97aSMatthew G Knepley 
42586636e97aSMatthew G Knepley #undef __FUNCT__
42596636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
42606636e97aSMatthew G Knepley /*@
42611cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
42626636e97aSMatthew G Knepley 
42636636e97aSMatthew G Knepley   Collective on DM
42646636e97aSMatthew G Knepley 
42656636e97aSMatthew G Knepley   Input Parameter:
42666636e97aSMatthew G Knepley . dm - the DM
42676636e97aSMatthew G Knepley 
42686636e97aSMatthew G Knepley   Output Parameter:
42696636e97aSMatthew G Knepley . cdm - coordinate DM
42706636e97aSMatthew G Knepley 
42716636e97aSMatthew G Knepley   Level: intermediate
42726636e97aSMatthew G Knepley 
42736636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42741cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
42756636e97aSMatthew G Knepley @*/
42766636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
42776636e97aSMatthew G Knepley {
42786636e97aSMatthew G Knepley   PetscErrorCode ierr;
42796636e97aSMatthew G Knepley 
42806636e97aSMatthew G Knepley   PetscFunctionBegin;
42816636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42826636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
42836636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
428482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
42856636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
42866636e97aSMatthew G Knepley   }
42876636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
42886636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42896636e97aSMatthew G Knepley }
4290e87bb0d3SMatthew G Knepley 
4291e87bb0d3SMatthew G Knepley #undef __FUNCT__
42921cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
42931cfe2091SMatthew G. Knepley /*@
42941cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
42951cfe2091SMatthew G. Knepley 
42961cfe2091SMatthew G. Knepley   Logically Collective on DM
42971cfe2091SMatthew G. Knepley 
42981cfe2091SMatthew G. Knepley   Input Parameters:
42991cfe2091SMatthew G. Knepley + dm - the DM
43001cfe2091SMatthew G. Knepley - cdm - coordinate DM
43011cfe2091SMatthew G. Knepley 
43021cfe2091SMatthew G. Knepley   Level: intermediate
43031cfe2091SMatthew G. Knepley 
43041cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43051cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43061cfe2091SMatthew G. Knepley @*/
43071cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
43081cfe2091SMatthew G. Knepley {
43091cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
43101cfe2091SMatthew G. Knepley 
43111cfe2091SMatthew G. Knepley   PetscFunctionBegin;
43121cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43131cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
43141cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
43151cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
43161cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
43171cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
43181cfe2091SMatthew G. Knepley }
43191cfe2091SMatthew G. Knepley 
43201cfe2091SMatthew G. Knepley #undef __FUNCT__
432146e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
432246e270d4SMatthew G. Knepley /*@
432346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
432446e270d4SMatthew G. Knepley 
432546e270d4SMatthew G. Knepley   Not Collective
432646e270d4SMatthew G. Knepley 
432746e270d4SMatthew G. Knepley   Input Parameter:
432846e270d4SMatthew G. Knepley . dm - The DM object
432946e270d4SMatthew G. Knepley 
433046e270d4SMatthew G. Knepley   Output Parameter:
433146e270d4SMatthew G. Knepley . dim - The embedding dimension
433246e270d4SMatthew G. Knepley 
433346e270d4SMatthew G. Knepley   Level: intermediate
433446e270d4SMatthew G. Knepley 
433546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
433646e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
433746e270d4SMatthew G. Knepley @*/
433846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
433946e270d4SMatthew G. Knepley {
434046e270d4SMatthew G. Knepley   PetscFunctionBegin;
434146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
434246e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
43439a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
43449a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
43459a9a41abSToby Isaac   }
434646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
434746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
434846e270d4SMatthew G. Knepley }
434946e270d4SMatthew G. Knepley 
435046e270d4SMatthew G. Knepley #undef __FUNCT__
435146e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
435246e270d4SMatthew G. Knepley /*@
435346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
435446e270d4SMatthew G. Knepley 
435546e270d4SMatthew G. Knepley   Not Collective
435646e270d4SMatthew G. Knepley 
435746e270d4SMatthew G. Knepley   Input Parameters:
435846e270d4SMatthew G. Knepley + dm  - The DM object
435946e270d4SMatthew G. Knepley - dim - The embedding dimension
436046e270d4SMatthew G. Knepley 
436146e270d4SMatthew G. Knepley   Level: intermediate
436246e270d4SMatthew G. Knepley 
436346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
436446e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
436546e270d4SMatthew G. Knepley @*/
436646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
436746e270d4SMatthew G. Knepley {
436846e270d4SMatthew G. Knepley   PetscFunctionBegin;
436946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
437046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
437146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
437246e270d4SMatthew G. Knepley }
437346e270d4SMatthew G. Knepley 
437446e270d4SMatthew G. Knepley #undef __FUNCT__
4375e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4376e8abe2deSMatthew G. Knepley /*@
4377e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4378e8abe2deSMatthew G. Knepley 
4379e8abe2deSMatthew G. Knepley   Not Collective
4380e8abe2deSMatthew G. Knepley 
4381e8abe2deSMatthew G. Knepley   Input Parameter:
4382e8abe2deSMatthew G. Knepley . dm - The DM object
4383e8abe2deSMatthew G. Knepley 
4384e8abe2deSMatthew G. Knepley   Output Parameter:
4385e8abe2deSMatthew G. Knepley . section - The PetscSection object
4386e8abe2deSMatthew G. Knepley 
4387e8abe2deSMatthew G. Knepley   Level: intermediate
4388e8abe2deSMatthew G. Knepley 
4389e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4390e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4391e8abe2deSMatthew G. Knepley @*/
4392e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4393e8abe2deSMatthew G. Knepley {
4394e8abe2deSMatthew G. Knepley   DM             cdm;
4395e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4396e8abe2deSMatthew G. Knepley 
4397e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4398e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4399e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4400e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4401e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4402e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4403e8abe2deSMatthew G. Knepley }
4404e8abe2deSMatthew G. Knepley 
4405e8abe2deSMatthew G. Knepley #undef __FUNCT__
4406e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4407e8abe2deSMatthew G. Knepley /*@
4408e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4409e8abe2deSMatthew G. Knepley 
4410e8abe2deSMatthew G. Knepley   Not Collective
4411e8abe2deSMatthew G. Knepley 
4412e8abe2deSMatthew G. Knepley   Input Parameters:
4413e8abe2deSMatthew G. Knepley + dm      - The DM object
441446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4415e8abe2deSMatthew G. Knepley - section - The PetscSection object
4416e8abe2deSMatthew G. Knepley 
4417e8abe2deSMatthew G. Knepley   Level: intermediate
4418e8abe2deSMatthew G. Knepley 
4419e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4420e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4421e8abe2deSMatthew G. Knepley @*/
442246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4423e8abe2deSMatthew G. Knepley {
4424e8abe2deSMatthew G. Knepley   DM             cdm;
4425e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4426e8abe2deSMatthew G. Knepley 
4427e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4428e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
442946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4430e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4431e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
443246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
443346e270d4SMatthew G. Knepley     PetscInt d = dim;
443446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
443546e270d4SMatthew G. Knepley 
443646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
443746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
443846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
443946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
444046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
444146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
444246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
444346e270d4SMatthew G. Knepley     }
444446e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
444546e270d4SMatthew G. Knepley   }
4446e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4447e8abe2deSMatthew G. Knepley }
4448e8abe2deSMatthew G. Knepley 
4449e8abe2deSMatthew G. Knepley #undef __FUNCT__
4450c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
44515dc8c3f7SMatthew G. Knepley /*@C
44525dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
44535dc8c3f7SMatthew G. Knepley 
44545dc8c3f7SMatthew G. Knepley   Input Parameters:
44555dc8c3f7SMatthew G. Knepley + dm      - The DM object
44565dc8c3f7SMatthew 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
44575dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44585dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44595dc8c3f7SMatthew G. Knepley 
44605dc8c3f7SMatthew G. Knepley   Level: developer
44615dc8c3f7SMatthew G. Knepley 
44625dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44635dc8c3f7SMatthew G. Knepley @*/
44645dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4465c6b900c6SMatthew G. Knepley {
4466c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4467c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4468c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4469c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
44705dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4471c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4472c6b900c6SMatthew G. Knepley }
4473c6b900c6SMatthew G. Knepley 
4474c6b900c6SMatthew G. Knepley #undef __FUNCT__
4475c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
44765dc8c3f7SMatthew G. Knepley /*@C
44775dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
44785dc8c3f7SMatthew G. Knepley 
44795dc8c3f7SMatthew G. Knepley   Input Parameters:
44805dc8c3f7SMatthew G. Knepley + dm      - The DM object
44815dc8c3f7SMatthew 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
44825dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44835dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44845dc8c3f7SMatthew G. Knepley 
44855dc8c3f7SMatthew G. Knepley   Level: developer
44865dc8c3f7SMatthew G. Knepley 
44875dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44885dc8c3f7SMatthew G. Knepley @*/
44895dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4490c6b900c6SMatthew G. Knepley {
4491c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4492c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4493c6b900c6SMatthew G. Knepley 
4494c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4495c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44965dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
44975dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
44985dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44995dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
45005dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4501c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4502c6b900c6SMatthew G. Knepley }
4503c6b900c6SMatthew G. Knepley 
4504c6b900c6SMatthew G. Knepley #undef __FUNCT__
45052e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinate"
45062e17dfb7SMatthew G. Knepley /*@
45072e17dfb7SMatthew G. Knepley   DMLocalizeCoordinate - If a mesh is periodic (a torus with lengths L_i, some of which can be infinite), project the coordinate onto [0, L_i) in each dimension.
45082e17dfb7SMatthew G. Knepley 
45092e17dfb7SMatthew G. Knepley   Input Parameters:
45102e17dfb7SMatthew G. Knepley + dm     - The DM
45112e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
45122e17dfb7SMatthew G. Knepley 
45132e17dfb7SMatthew G. Knepley   Output Parameter:
45142e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45152e17dfb7SMatthew G. Knepley 
45162e17dfb7SMatthew G. Knepley   Level: developer
45172e17dfb7SMatthew G. Knepley 
45182e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45192e17dfb7SMatthew G. Knepley @*/
45202e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscScalar out[])
45212e17dfb7SMatthew G. Knepley {
45222e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
45232e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
45242e17dfb7SMatthew G. Knepley 
45252e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45262e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
45272e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45282e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45292e17dfb7SMatthew G. Knepley   } else {
45302e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45312e17dfb7SMatthew G. Knepley       out[d] = in[d] - dm->L[d]*floor(PetscRealPart(in[d])/dm->L[d]);
45322e17dfb7SMatthew G. Knepley     }
45332e17dfb7SMatthew G. Knepley   }
45342e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45352e17dfb7SMatthew G. Knepley }
45362e17dfb7SMatthew G. Knepley 
45372e17dfb7SMatthew G. Knepley #undef __FUNCT__
45382e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinate_Internal"
45392e17dfb7SMatthew G. Knepley /*
45402e17dfb7SMatthew G. Knepley   DMLocalizeCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
45412e17dfb7SMatthew G. Knepley 
45422e17dfb7SMatthew G. Knepley   Input Parameters:
45432e17dfb7SMatthew G. Knepley + dm     - The DM
45442e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45452e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45462e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
45472e17dfb7SMatthew G. Knepley 
45482e17dfb7SMatthew G. Knepley   Output Parameter:
45492e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45502e17dfb7SMatthew G. Knepley 
45512e17dfb7SMatthew G. Knepley   Level: developer
45522e17dfb7SMatthew G. Knepley 
45532e17dfb7SMatthew G. Knepley   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
45542e17dfb7SMatthew G. Knepley 
45552e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45562e17dfb7SMatthew G. Knepley */
45572e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45582e17dfb7SMatthew G. Knepley {
45592e17dfb7SMatthew G. Knepley   PetscInt d;
45602e17dfb7SMatthew G. Knepley 
45612e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45622e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45632e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45642e17dfb7SMatthew G. Knepley   } else {
45652e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45662e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
45672e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
45682e17dfb7SMatthew G. Knepley       } else {
45692e17dfb7SMatthew G. Knepley         out[d] = in[d];
45702e17dfb7SMatthew G. Knepley       }
45712e17dfb7SMatthew G. Knepley     }
45722e17dfb7SMatthew G. Knepley   }
45732e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45742e17dfb7SMatthew G. Knepley }
45752e17dfb7SMatthew G. Knepley #undef __FUNCT__
45762e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinateReal_Internal"
45772e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
45782e17dfb7SMatthew G. Knepley {
45792e17dfb7SMatthew G. Knepley   PetscInt d;
45802e17dfb7SMatthew G. Knepley 
45812e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45822e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45832e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45842e17dfb7SMatthew G. Knepley   } else {
45852e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45862e17dfb7SMatthew G. Knepley       if (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d]) {
45872e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
45882e17dfb7SMatthew G. Knepley       } else {
45892e17dfb7SMatthew G. Knepley         out[d] = in[d];
45902e17dfb7SMatthew G. Knepley       }
45912e17dfb7SMatthew G. Knepley     }
45922e17dfb7SMatthew G. Knepley   }
45932e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45942e17dfb7SMatthew G. Knepley }
45952e17dfb7SMatthew G. Knepley 
45962e17dfb7SMatthew G. Knepley #undef __FUNCT__
45972e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeAddCoordinate_Internal"
45982e17dfb7SMatthew G. Knepley /*
45992e17dfb7SMatthew G. Knepley   DMLocalizeAddCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
46002e17dfb7SMatthew G. Knepley 
46012e17dfb7SMatthew G. Knepley   Input Parameters:
46022e17dfb7SMatthew G. Knepley + dm     - The DM
46032e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46042e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46052e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
46062e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
46072e17dfb7SMatthew G. Knepley 
46082e17dfb7SMatthew G. Knepley   Output Parameter:
46092e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
46102e17dfb7SMatthew G. Knepley 
46112e17dfb7SMatthew G. Knepley   Level: developer
46122e17dfb7SMatthew G. Knepley 
46132e17dfb7SMatthew G. Knepley   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
46142e17dfb7SMatthew G. Knepley 
46152e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
46162e17dfb7SMatthew G. Knepley */
46172e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46182e17dfb7SMatthew G. Knepley {
46192e17dfb7SMatthew G. Knepley   PetscInt d;
46202e17dfb7SMatthew G. Knepley 
46212e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46222e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46232e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
46242e17dfb7SMatthew G. Knepley   } else {
46252e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
46262e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
46272e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46282e17dfb7SMatthew G. Knepley       } else {
46292e17dfb7SMatthew G. Knepley         out[d] += in[d];
46302e17dfb7SMatthew G. Knepley       }
46312e17dfb7SMatthew G. Knepley     }
46322e17dfb7SMatthew G. Knepley   }
46332e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46342e17dfb7SMatthew G. Knepley }
46352e17dfb7SMatthew G. Knepley 
46362e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetDepthStratum(DM, PetscInt, PetscInt *, PetscInt *);
46372e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetHeightStratum(DM, PetscInt, PetscInt *, PetscInt *);
46382e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecGetClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
46392e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecRestoreClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
46402e17dfb7SMatthew G. Knepley 
46412e17dfb7SMatthew G. Knepley #undef __FUNCT__
464236447a5eSToby Isaac #define __FUNCT__ "DMGetCoordinatesLocalized"
464336447a5eSToby Isaac /*@
464436447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
464536447a5eSToby Isaac 
464636447a5eSToby Isaac   Input Parameter:
464736447a5eSToby Isaac . dm - The DM
464836447a5eSToby Isaac 
464936447a5eSToby Isaac   Output Parameter:
465036447a5eSToby Isaac   areLocalized - True if localized
465136447a5eSToby Isaac 
465236447a5eSToby Isaac   Level: developer
465336447a5eSToby Isaac 
465436447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
465536447a5eSToby Isaac @*/
465636447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
465736447a5eSToby Isaac {
465836447a5eSToby Isaac   DM             cdm;
465936447a5eSToby Isaac   PetscSection   coordSection;
466036447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
466136447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
466236447a5eSToby Isaac   PetscErrorCode ierr;
466336447a5eSToby Isaac 
466436447a5eSToby Isaac   PetscFunctionBegin;
466536447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46668b09590cSToby Isaac   if (!dm->maxCell) {
46678b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
46688b09590cSToby Isaac     PetscFunctionReturn(0);
46698b09590cSToby Isaac   }
467036447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
467136447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
467236447a5eSToby Isaac   {
467336447a5eSToby Isaac     PetscBool isplex;
467436447a5eSToby Isaac 
467536447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
467636447a5eSToby Isaac     if (isplex) {
467736447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
467836447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
467936447a5eSToby Isaac   }
468036447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
468136447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
468236447a5eSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
468336447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
468436447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
468536447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
468636447a5eSToby Isaac       break;
468736447a5eSToby Isaac     }
468836447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
468936447a5eSToby Isaac     if (!dof) {
469036447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
469136447a5eSToby Isaac       break;
469236447a5eSToby Isaac     }
469336447a5eSToby Isaac   }
469436447a5eSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
469536447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
469636447a5eSToby Isaac   PetscFunctionReturn(0);
469736447a5eSToby Isaac }
469836447a5eSToby Isaac 
469936447a5eSToby Isaac 
470036447a5eSToby Isaac #undef __FUNCT__
47012e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinates"
47022e17dfb7SMatthew G. Knepley /*@
47032e17dfb7SMatthew G. Knepley   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for each cell
47042e17dfb7SMatthew G. Knepley 
47052e17dfb7SMatthew G. Knepley   Input Parameter:
47062e17dfb7SMatthew G. Knepley . dm - The DM
47072e17dfb7SMatthew G. Knepley 
47082e17dfb7SMatthew G. Knepley   Level: developer
47092e17dfb7SMatthew G. Knepley 
47102e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
47112e17dfb7SMatthew G. Knepley @*/
47122e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
47132e17dfb7SMatthew G. Knepley {
47142e17dfb7SMatthew G. Knepley   DM             cdm;
47152e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
47162e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
47172e17dfb7SMatthew G. Knepley   PetscScalar   *coords, *coords2, *anchor;
4718e0ae35bbSToby Isaac   PetscInt       Nc, cStart, cEnd, c, vStart, vEnd, v, sStart, sEnd, dof, d, off, off2, bs, coordSize;
4719e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
47202e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
47212e17dfb7SMatthew G. Knepley 
47222e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47232e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47242e17dfb7SMatthew G. Knepley   if (!dm->maxCell) PetscFunctionReturn(0);
47252e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
47262e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
47272e17dfb7SMatthew G. Knepley   {
47282e17dfb7SMatthew G. Knepley     PetscBool isplex;
47292e17dfb7SMatthew G. Knepley 
47302e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
47312e17dfb7SMatthew G. Knepley     if (isplex) {
47322e17dfb7SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
47332e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
47342e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
47352e17dfb7SMatthew G. Knepley   }
47362e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
47372e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
4738e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
4739e0ae35bbSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
4740e0ae35bbSToby Isaac   for (c = cStart; c < cEnd; ++c) {
4741e0ae35bbSToby Isaac     if (c < sStart || c >= sEnd) {
4742e0ae35bbSToby Isaac       alreadyLocalized = PETSC_FALSE;
4743e0ae35bbSToby Isaac       break;
4744e0ae35bbSToby Isaac     }
4745e0ae35bbSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
4746e0ae35bbSToby Isaac     if (!dof) {
4747e0ae35bbSToby Isaac       alreadyLocalized = PETSC_FALSE;
4748e0ae35bbSToby Isaac       break;
4749e0ae35bbSToby Isaac     }
4750e0ae35bbSToby Isaac   }
4751e0ae35bbSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
4752e0ae35bbSToby Isaac   if (alreadyLocalizedGlobal) PetscFunctionReturn(0);
47532e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
47542e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
47552e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
47562e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
47572e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetChart(cSection, cStart, vEnd);CHKERRQ(ierr);
47582e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
47592e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
47602e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
47612e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
47622e17dfb7SMatthew G. Knepley   }
47632e17dfb7SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
47642e17dfb7SMatthew G. Knepley     ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, NULL);CHKERRQ(ierr);
47652e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
47662e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
47672e17dfb7SMatthew G. Knepley   }
47682e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
47692e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
47702e17dfb7SMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject) dm), &cVec);CHKERRQ(ierr);
47712e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
47722e17dfb7SMatthew G. Knepley   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
47732e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
47742e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
47752e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec,VECSTANDARD);CHKERRQ(ierr);
47762e17dfb7SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
47772e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec,        &coords2);CHKERRQ(ierr);
47782e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
47792e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
47802e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
47812e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
47822e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
47832e17dfb7SMatthew G. Knepley   }
47842e17dfb7SMatthew G. Knepley   ierr = DMGetWorkArray(dm, 3, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47852e17dfb7SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
47862e17dfb7SMatthew G. Knepley     PetscScalar *cellCoords = NULL;
47872e17dfb7SMatthew G. Knepley     PetscInt     b;
47882e17dfb7SMatthew G. Knepley 
47892e17dfb7SMatthew G. Knepley     ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47902e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
47912e17dfb7SMatthew G. Knepley     for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
47922e17dfb7SMatthew G. Knepley     for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
47932e17dfb7SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47942e17dfb7SMatthew G. Knepley   }
47952e17dfb7SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 3, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47962e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
47972e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec,        &coords2);CHKERRQ(ierr);
47982e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
47992e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
48002e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
48012e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
48022e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48032e17dfb7SMatthew G. Knepley }
48042e17dfb7SMatthew G. Knepley 
48052e17dfb7SMatthew G. Knepley #undef __FUNCT__
4806e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4807e87bb0d3SMatthew G Knepley /*@
4808e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4809e87bb0d3SMatthew G Knepley 
4810e87bb0d3SMatthew G Knepley   Not collective
4811e87bb0d3SMatthew G Knepley 
4812e87bb0d3SMatthew G Knepley   Input Parameters:
4813e87bb0d3SMatthew G Knepley + dm - The DM
4814e87bb0d3SMatthew G Knepley - v - The Vec of points
4815e87bb0d3SMatthew G Knepley 
481661e3bb9bSMatthew G Knepley   Output Parameter:
4817e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4818e87bb0d3SMatthew G Knepley 
4819e87bb0d3SMatthew G Knepley   Level: developer
482061e3bb9bSMatthew G Knepley 
482161e3bb9bSMatthew G Knepley .keywords: point location, mesh
482261e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
482361e3bb9bSMatthew G Knepley @*/
4824e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4825e87bb0d3SMatthew G Knepley {
4826735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4827735aa83eSMatthew G Knepley 
4828e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4829e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4830e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4831e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
483247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4833735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4834735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
483582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
483647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4837e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4838e87bb0d3SMatthew G Knepley }
483914f150ffSMatthew G. Knepley 
484014f150ffSMatthew G. Knepley #undef __FUNCT__
484114f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4842f4d763aaSMatthew G. Knepley /*@
4843f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4844f4d763aaSMatthew G. Knepley 
4845f4d763aaSMatthew G. Knepley   Input Parameter:
4846f4d763aaSMatthew G. Knepley . dm - The original DM
4847f4d763aaSMatthew G. Knepley 
4848f4d763aaSMatthew G. Knepley   Output Parameter:
4849f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4850f4d763aaSMatthew G. Knepley 
4851f4d763aaSMatthew G. Knepley   Level: intermediate
4852f4d763aaSMatthew G. Knepley 
4853f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4854f4d763aaSMatthew G. Knepley @*/
485514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
485614f150ffSMatthew G. Knepley {
4857c26acbdeSMatthew G. Knepley   PetscSection   section;
4858c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
485914f150ffSMatthew G. Knepley   PetscErrorCode ierr;
486014f150ffSMatthew G. Knepley 
486114f150ffSMatthew G. Knepley   PetscFunctionBegin;
486214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
486314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4864c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4865c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4866c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4867c26acbdeSMatthew G. Knepley     *odm = dm;
4868c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4869c26acbdeSMatthew G. Knepley   }
487014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
48710305aff6SMatthew G. Knepley     PetscDS      ds;
4872c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
487314f150ffSMatthew G. Knepley     PetscSF      sf;
487414f150ffSMatthew G. Knepley 
487514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
48760305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
48770305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
487814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
487914f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
488014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
488114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
488215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
488314f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
488414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
488514f150ffSMatthew G. Knepley   }
488614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
488714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
488814f150ffSMatthew G. Knepley }
4889f4d763aaSMatthew G. Knepley 
4890f4d763aaSMatthew G. Knepley #undef __FUNCT__
4891f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4892f4d763aaSMatthew G. Knepley /*@
4893cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4894f4d763aaSMatthew G. Knepley 
4895f4d763aaSMatthew G. Knepley   Input Parameter:
4896f4d763aaSMatthew G. Knepley . dm - The original DM
4897f4d763aaSMatthew G. Knepley 
4898cdb7a50dSMatthew G. Knepley   Output Parameters:
4899cdb7a50dSMatthew G. Knepley + num - The output sequence number
4900cdb7a50dSMatthew G. Knepley - val - The output sequence value
4901f4d763aaSMatthew G. Knepley 
4902f4d763aaSMatthew G. Knepley   Level: intermediate
4903f4d763aaSMatthew G. Knepley 
4904f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4905f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4906f4d763aaSMatthew G. Knepley 
4907f4d763aaSMatthew G. Knepley .seealso: VecView()
4908f4d763aaSMatthew G. Knepley @*/
4909cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4910f4d763aaSMatthew G. Knepley {
4911f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4912f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4913cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4914cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4915f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4916f4d763aaSMatthew G. Knepley }
4917f4d763aaSMatthew G. Knepley 
4918f4d763aaSMatthew G. Knepley #undef __FUNCT__
4919f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4920f4d763aaSMatthew G. Knepley /*@
4921cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4922f4d763aaSMatthew G. Knepley 
4923f4d763aaSMatthew G. Knepley   Input Parameters:
4924f4d763aaSMatthew G. Knepley + dm - The original DM
4925cdb7a50dSMatthew G. Knepley . num - The output sequence number
4926cdb7a50dSMatthew G. Knepley - val - The output sequence value
4927f4d763aaSMatthew G. Knepley 
4928f4d763aaSMatthew G. Knepley   Level: intermediate
4929f4d763aaSMatthew G. Knepley 
4930f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4931f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4932f4d763aaSMatthew G. Knepley 
4933f4d763aaSMatthew G. Knepley .seealso: VecView()
4934f4d763aaSMatthew G. Knepley @*/
4935cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4936f4d763aaSMatthew G. Knepley {
4937f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4938f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4939f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4940cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4941cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4942cdb7a50dSMatthew G. Knepley }
4943cdb7a50dSMatthew G. Knepley 
4944cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4945cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4946cdb7a50dSMatthew G. Knepley /*@C
4947cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4948cdb7a50dSMatthew G. Knepley 
4949cdb7a50dSMatthew G. Knepley   Input Parameters:
4950cdb7a50dSMatthew G. Knepley + dm   - The original DM
4951cdb7a50dSMatthew G. Knepley . name - The sequence name
4952cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4953cdb7a50dSMatthew G. Knepley 
4954cdb7a50dSMatthew G. Knepley   Output Parameter:
4955cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4956cdb7a50dSMatthew G. Knepley 
4957cdb7a50dSMatthew G. Knepley   Level: intermediate
4958cdb7a50dSMatthew G. Knepley 
4959cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4960cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4961cdb7a50dSMatthew G. Knepley 
4962cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4963cdb7a50dSMatthew G. Knepley @*/
4964cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4965cdb7a50dSMatthew G. Knepley {
4966cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4967cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4968cdb7a50dSMatthew G. Knepley 
4969cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4970cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4971cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4972cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4973cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4974cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4975cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4976cdb7a50dSMatthew G. Knepley     PetscScalar value;
4977cdb7a50dSMatthew G. Knepley 
4978cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
49794aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4980cdb7a50dSMatthew G. Knepley #endif
4981cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4982f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4983f4d763aaSMatthew G. Knepley }
49848e4ac7eaSMatthew G. Knepley 
49858e4ac7eaSMatthew G. Knepley #undef __FUNCT__
49868e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
49878e4ac7eaSMatthew G. Knepley /*@
49888e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
49898e4ac7eaSMatthew G. Knepley 
49908e4ac7eaSMatthew G. Knepley   Not collective
49918e4ac7eaSMatthew G. Knepley 
49928e4ac7eaSMatthew G. Knepley   Input Parameter:
49938e4ac7eaSMatthew G. Knepley . dm - The DM
49948e4ac7eaSMatthew G. Knepley 
49958e4ac7eaSMatthew G. Knepley   Output Parameter:
49968e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
49978e4ac7eaSMatthew G. Knepley 
49988e4ac7eaSMatthew G. Knepley   Level: beginner
49998e4ac7eaSMatthew G. Knepley 
50008e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
50018e4ac7eaSMatthew G. Knepley @*/
50028e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
50038e4ac7eaSMatthew G. Knepley {
50048e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
50058e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50068e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
50078e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
50088e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
50098e4ac7eaSMatthew G. Knepley }
50108e4ac7eaSMatthew G. Knepley 
50118e4ac7eaSMatthew G. Knepley #undef __FUNCT__
50128e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
50138e4ac7eaSMatthew G. Knepley /*@
50148e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
50158e4ac7eaSMatthew G. Knepley 
50168e4ac7eaSMatthew G. Knepley   Collective on dm
50178e4ac7eaSMatthew G. Knepley 
50188e4ac7eaSMatthew G. Knepley   Input Parameters:
50198e4ac7eaSMatthew G. Knepley + dm - The DM
50208e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
50218e4ac7eaSMatthew G. Knepley 
50228e4ac7eaSMatthew G. Knepley   Level: beginner
50238e4ac7eaSMatthew G. Knepley 
50248e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
50258e4ac7eaSMatthew G. Knepley @*/
50268e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
50278e4ac7eaSMatthew G. Knepley {
50288e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
50298e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50308e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
50318e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
50328e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
50338e4ac7eaSMatthew G. Knepley }
5034c58f1c22SToby Isaac 
5035c58f1c22SToby Isaac #undef __FUNCT__
5036c58f1c22SToby Isaac #define __FUNCT__
5037c58f1c22SToby Isaac 
5038c58f1c22SToby Isaac #undef __FUNCT__
5039c58f1c22SToby Isaac #define __FUNCT__ "DMCreateLabel"
5040c58f1c22SToby Isaac /*@C
5041c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5042c58f1c22SToby Isaac 
5043c58f1c22SToby Isaac   Not Collective
5044c58f1c22SToby Isaac 
5045c58f1c22SToby Isaac   Input Parameters:
5046c58f1c22SToby Isaac + dm   - The DM object
5047c58f1c22SToby Isaac - name - The label name
5048c58f1c22SToby Isaac 
5049c58f1c22SToby Isaac   Level: intermediate
5050c58f1c22SToby Isaac 
5051c58f1c22SToby Isaac .keywords: mesh
5052c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5053c58f1c22SToby Isaac @*/
5054c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5055c58f1c22SToby Isaac {
5056c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5057c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5058c58f1c22SToby Isaac   PetscErrorCode ierr;
5059c58f1c22SToby Isaac 
5060c58f1c22SToby Isaac   PetscFunctionBegin;
5061c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5062c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5063c58f1c22SToby Isaac   while (next) {
5064c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5065c58f1c22SToby Isaac     if (flg) break;
5066c58f1c22SToby Isaac     next = next->next;
5067c58f1c22SToby Isaac   }
5068c58f1c22SToby Isaac   if (!flg) {
5069c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5070c58f1c22SToby Isaac 
5071c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5072c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5073c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5074c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5075c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5076c58f1c22SToby Isaac   }
5077c58f1c22SToby Isaac   PetscFunctionReturn(0);
5078c58f1c22SToby Isaac }
5079c58f1c22SToby Isaac 
5080c58f1c22SToby Isaac #undef __FUNCT__
5081c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelValue"
5082c58f1c22SToby Isaac /*@C
5083c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5084c58f1c22SToby Isaac 
5085c58f1c22SToby Isaac   Not Collective
5086c58f1c22SToby Isaac 
5087c58f1c22SToby Isaac   Input Parameters:
5088c58f1c22SToby Isaac + dm   - The DM object
5089c58f1c22SToby Isaac . name - The label name
5090c58f1c22SToby Isaac - point - The mesh point
5091c58f1c22SToby Isaac 
5092c58f1c22SToby Isaac   Output Parameter:
5093c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5094c58f1c22SToby Isaac 
5095c58f1c22SToby Isaac   Level: beginner
5096c58f1c22SToby Isaac 
5097c58f1c22SToby Isaac .keywords: mesh
5098c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5099c58f1c22SToby Isaac @*/
5100c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5101c58f1c22SToby Isaac {
5102c58f1c22SToby Isaac   DMLabel        label;
5103c58f1c22SToby Isaac   PetscErrorCode ierr;
5104c58f1c22SToby Isaac 
5105c58f1c22SToby Isaac   PetscFunctionBegin;
5106c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5107c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5108c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5109c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
5110c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5111c58f1c22SToby Isaac   PetscFunctionReturn(0);
5112c58f1c22SToby Isaac }
5113c58f1c22SToby Isaac 
5114c58f1c22SToby Isaac #undef __FUNCT__
5115c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelValue"
5116c58f1c22SToby Isaac /*@C
5117c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5118c58f1c22SToby Isaac 
5119c58f1c22SToby Isaac   Not Collective
5120c58f1c22SToby Isaac 
5121c58f1c22SToby Isaac   Input Parameters:
5122c58f1c22SToby Isaac + dm   - The DM object
5123c58f1c22SToby Isaac . name - The label name
5124c58f1c22SToby Isaac . point - The mesh point
5125c58f1c22SToby Isaac - value - The label value for this point
5126c58f1c22SToby Isaac 
5127c58f1c22SToby Isaac   Output Parameter:
5128c58f1c22SToby Isaac 
5129c58f1c22SToby Isaac   Level: beginner
5130c58f1c22SToby Isaac 
5131c58f1c22SToby Isaac .keywords: mesh
5132c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5133c58f1c22SToby Isaac @*/
5134c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5135c58f1c22SToby Isaac {
5136c58f1c22SToby Isaac   DMLabel        label;
5137c58f1c22SToby Isaac   PetscErrorCode ierr;
5138c58f1c22SToby Isaac 
5139c58f1c22SToby Isaac   PetscFunctionBegin;
5140c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5141c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5142c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5143c58f1c22SToby Isaac   if (!label) {
5144c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5145c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5146c58f1c22SToby Isaac   }
5147c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5148c58f1c22SToby Isaac   PetscFunctionReturn(0);
5149c58f1c22SToby Isaac }
5150c58f1c22SToby Isaac 
5151c58f1c22SToby Isaac #undef __FUNCT__
5152c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelValue"
5153c58f1c22SToby Isaac /*@C
5154c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5155c58f1c22SToby Isaac 
5156c58f1c22SToby Isaac   Not Collective
5157c58f1c22SToby Isaac 
5158c58f1c22SToby Isaac   Input Parameters:
5159c58f1c22SToby Isaac + dm   - The DM object
5160c58f1c22SToby Isaac . name - The label name
5161c58f1c22SToby Isaac . point - The mesh point
5162c58f1c22SToby Isaac - value - The label value for this point
5163c58f1c22SToby Isaac 
5164c58f1c22SToby Isaac   Output Parameter:
5165c58f1c22SToby Isaac 
5166c58f1c22SToby Isaac   Level: beginner
5167c58f1c22SToby Isaac 
5168c58f1c22SToby Isaac .keywords: mesh
5169c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5170c58f1c22SToby Isaac @*/
5171c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5172c58f1c22SToby Isaac {
5173c58f1c22SToby Isaac   DMLabel        label;
5174c58f1c22SToby Isaac   PetscErrorCode ierr;
5175c58f1c22SToby Isaac 
5176c58f1c22SToby Isaac   PetscFunctionBegin;
5177c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5178c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5179c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5180c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5181c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5182c58f1c22SToby Isaac   PetscFunctionReturn(0);
5183c58f1c22SToby Isaac }
5184c58f1c22SToby Isaac 
5185c58f1c22SToby Isaac #undef __FUNCT__
5186c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelSize"
5187c58f1c22SToby Isaac /*@C
5188c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5189c58f1c22SToby Isaac 
5190c58f1c22SToby Isaac   Not Collective
5191c58f1c22SToby Isaac 
5192c58f1c22SToby Isaac   Input Parameters:
5193c58f1c22SToby Isaac + dm   - The DM object
5194c58f1c22SToby Isaac - name - The label name
5195c58f1c22SToby Isaac 
5196c58f1c22SToby Isaac   Output Parameter:
5197c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5198c58f1c22SToby Isaac 
5199c58f1c22SToby Isaac   Level: beginner
5200c58f1c22SToby Isaac 
5201c58f1c22SToby Isaac .keywords: mesh
5202c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5203c58f1c22SToby Isaac @*/
5204c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5205c58f1c22SToby Isaac {
5206c58f1c22SToby Isaac   DMLabel        label;
5207c58f1c22SToby Isaac   PetscErrorCode ierr;
5208c58f1c22SToby Isaac 
5209c58f1c22SToby Isaac   PetscFunctionBegin;
5210c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5211c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5212c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5213c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5214c58f1c22SToby Isaac   *size = 0;
5215c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5216c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5217c58f1c22SToby Isaac   PetscFunctionReturn(0);
5218c58f1c22SToby Isaac }
5219c58f1c22SToby Isaac 
5220c58f1c22SToby Isaac #undef __FUNCT__
5221c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelIdIS"
5222c58f1c22SToby Isaac /*@C
5223c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5224c58f1c22SToby Isaac 
5225c58f1c22SToby Isaac   Not Collective
5226c58f1c22SToby Isaac 
5227c58f1c22SToby Isaac   Input Parameters:
5228c58f1c22SToby Isaac + mesh - The DM object
5229c58f1c22SToby Isaac - name - The label name
5230c58f1c22SToby Isaac 
5231c58f1c22SToby Isaac   Output Parameter:
5232c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5233c58f1c22SToby Isaac 
5234c58f1c22SToby Isaac   Level: beginner
5235c58f1c22SToby Isaac 
5236c58f1c22SToby Isaac .keywords: mesh
5237c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5238c58f1c22SToby Isaac @*/
5239c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5240c58f1c22SToby Isaac {
5241c58f1c22SToby Isaac   DMLabel        label;
5242c58f1c22SToby Isaac   PetscErrorCode ierr;
5243c58f1c22SToby Isaac 
5244c58f1c22SToby Isaac   PetscFunctionBegin;
5245c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5246c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5247c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5248c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5249c58f1c22SToby Isaac   *ids = NULL;
5250c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5251c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5252c58f1c22SToby Isaac   PetscFunctionReturn(0);
5253c58f1c22SToby Isaac }
5254c58f1c22SToby Isaac 
5255c58f1c22SToby Isaac #undef __FUNCT__
5256c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumSize"
5257c58f1c22SToby Isaac /*@C
5258c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5259c58f1c22SToby Isaac 
5260c58f1c22SToby Isaac   Not Collective
5261c58f1c22SToby Isaac 
5262c58f1c22SToby Isaac   Input Parameters:
5263c58f1c22SToby Isaac + dm - The DM object
5264c58f1c22SToby Isaac . name - The label name
5265c58f1c22SToby Isaac - value - The stratum value
5266c58f1c22SToby Isaac 
5267c58f1c22SToby Isaac   Output Parameter:
5268c58f1c22SToby Isaac . size - The stratum size
5269c58f1c22SToby Isaac 
5270c58f1c22SToby Isaac   Level: beginner
5271c58f1c22SToby Isaac 
5272c58f1c22SToby Isaac .keywords: mesh
5273c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5274c58f1c22SToby Isaac @*/
5275c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5276c58f1c22SToby Isaac {
5277c58f1c22SToby Isaac   DMLabel        label;
5278c58f1c22SToby Isaac   PetscErrorCode ierr;
5279c58f1c22SToby Isaac 
5280c58f1c22SToby Isaac   PetscFunctionBegin;
5281c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5282c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5283c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5284c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5285c58f1c22SToby Isaac   *size = 0;
5286c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5287c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5288c58f1c22SToby Isaac   PetscFunctionReturn(0);
5289c58f1c22SToby Isaac }
5290c58f1c22SToby Isaac 
5291c58f1c22SToby Isaac #undef __FUNCT__
5292c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumIS"
5293c58f1c22SToby Isaac /*@C
5294c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5295c58f1c22SToby Isaac 
5296c58f1c22SToby Isaac   Not Collective
5297c58f1c22SToby Isaac 
5298c58f1c22SToby Isaac   Input Parameters:
5299c58f1c22SToby Isaac + dm - The DM object
5300c58f1c22SToby Isaac . name - The label name
5301c58f1c22SToby Isaac - value - The stratum value
5302c58f1c22SToby Isaac 
5303c58f1c22SToby Isaac   Output Parameter:
5304c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5305c58f1c22SToby Isaac 
5306c58f1c22SToby Isaac   Level: beginner
5307c58f1c22SToby Isaac 
5308c58f1c22SToby Isaac .keywords: mesh
5309c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5310c58f1c22SToby Isaac @*/
5311c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5312c58f1c22SToby Isaac {
5313c58f1c22SToby Isaac   DMLabel        label;
5314c58f1c22SToby Isaac   PetscErrorCode ierr;
5315c58f1c22SToby Isaac 
5316c58f1c22SToby Isaac   PetscFunctionBegin;
5317c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5318c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5319c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5320c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5321c58f1c22SToby Isaac   *points = NULL;
5322c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5323c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5324c58f1c22SToby Isaac   PetscFunctionReturn(0);
5325c58f1c22SToby Isaac }
5326c58f1c22SToby Isaac 
5327c58f1c22SToby Isaac #undef __FUNCT__
5328c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelStratum"
5329c58f1c22SToby Isaac /*@C
5330c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5331c58f1c22SToby Isaac 
5332c58f1c22SToby Isaac   Not Collective
5333c58f1c22SToby Isaac 
5334c58f1c22SToby Isaac   Input Parameters:
5335c58f1c22SToby Isaac + dm   - The DM object
5336c58f1c22SToby Isaac . name - The label name
5337c58f1c22SToby Isaac - value - The label value for this point
5338c58f1c22SToby Isaac 
5339c58f1c22SToby Isaac   Output Parameter:
5340c58f1c22SToby Isaac 
5341c58f1c22SToby Isaac   Level: beginner
5342c58f1c22SToby Isaac 
5343c58f1c22SToby Isaac .keywords: mesh
5344c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5345c58f1c22SToby Isaac @*/
5346c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5347c58f1c22SToby Isaac {
5348c58f1c22SToby Isaac   DMLabel        label;
5349c58f1c22SToby Isaac   PetscErrorCode ierr;
5350c58f1c22SToby Isaac 
5351c58f1c22SToby Isaac   PetscFunctionBegin;
5352c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5353c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5354c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5355c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5356c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5357c58f1c22SToby Isaac   PetscFunctionReturn(0);
5358c58f1c22SToby Isaac }
5359c58f1c22SToby Isaac 
5360c58f1c22SToby Isaac #undef __FUNCT__
5361c58f1c22SToby Isaac #define __FUNCT__ "DMGetNumLabels"
5362c58f1c22SToby Isaac /*@
5363c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5364c58f1c22SToby Isaac 
5365c58f1c22SToby Isaac   Not Collective
5366c58f1c22SToby Isaac 
5367c58f1c22SToby Isaac   Input Parameter:
5368c58f1c22SToby Isaac . dm   - The DM object
5369c58f1c22SToby Isaac 
5370c58f1c22SToby Isaac   Output Parameter:
5371c58f1c22SToby Isaac . numLabels - the number of Labels
5372c58f1c22SToby Isaac 
5373c58f1c22SToby Isaac   Level: intermediate
5374c58f1c22SToby Isaac 
5375c58f1c22SToby Isaac .keywords: mesh
5376c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5377c58f1c22SToby Isaac @*/
5378c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5379c58f1c22SToby Isaac {
5380c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5381c58f1c22SToby Isaac   PetscInt  n    = 0;
5382c58f1c22SToby Isaac 
5383c58f1c22SToby Isaac   PetscFunctionBegin;
5384c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5385c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5386c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5387c58f1c22SToby Isaac   *numLabels = n;
5388c58f1c22SToby Isaac   PetscFunctionReturn(0);
5389c58f1c22SToby Isaac }
5390c58f1c22SToby Isaac 
5391c58f1c22SToby Isaac #undef __FUNCT__
5392c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelName"
5393c58f1c22SToby Isaac /*@C
5394c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5395c58f1c22SToby Isaac 
5396c58f1c22SToby Isaac   Not Collective
5397c58f1c22SToby Isaac 
5398c58f1c22SToby Isaac   Input Parameters:
5399c58f1c22SToby Isaac + dm - The DM object
5400c58f1c22SToby Isaac - n  - the label number
5401c58f1c22SToby Isaac 
5402c58f1c22SToby Isaac   Output Parameter:
5403c58f1c22SToby Isaac . name - the label name
5404c58f1c22SToby Isaac 
5405c58f1c22SToby Isaac   Level: intermediate
5406c58f1c22SToby Isaac 
5407c58f1c22SToby Isaac .keywords: mesh
5408c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5409c58f1c22SToby Isaac @*/
5410c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5411c58f1c22SToby Isaac {
5412c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5413c58f1c22SToby Isaac   PetscInt  l    = 0;
5414c58f1c22SToby Isaac 
5415c58f1c22SToby Isaac   PetscFunctionBegin;
5416c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5417c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5418c58f1c22SToby Isaac   while (next) {
5419c58f1c22SToby Isaac     if (l == n) {
5420c58f1c22SToby Isaac       *name = next->label->name;
5421c58f1c22SToby Isaac       PetscFunctionReturn(0);
5422c58f1c22SToby Isaac     }
5423c58f1c22SToby Isaac     ++l;
5424c58f1c22SToby Isaac     next = next->next;
5425c58f1c22SToby Isaac   }
5426c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5427c58f1c22SToby Isaac }
5428c58f1c22SToby Isaac 
5429c58f1c22SToby Isaac #undef __FUNCT__
5430c58f1c22SToby Isaac #define __FUNCT__ "DMHasLabel"
5431c58f1c22SToby Isaac /*@C
5432c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5433c58f1c22SToby Isaac 
5434c58f1c22SToby Isaac   Not Collective
5435c58f1c22SToby Isaac 
5436c58f1c22SToby Isaac   Input Parameters:
5437c58f1c22SToby Isaac + dm   - The DM object
5438c58f1c22SToby Isaac - name - The label name
5439c58f1c22SToby Isaac 
5440c58f1c22SToby Isaac   Output Parameter:
5441c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5442c58f1c22SToby Isaac 
5443c58f1c22SToby Isaac   Level: intermediate
5444c58f1c22SToby Isaac 
5445c58f1c22SToby Isaac .keywords: mesh
5446c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5447c58f1c22SToby Isaac @*/
5448c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5449c58f1c22SToby Isaac {
5450c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5451c58f1c22SToby Isaac   PetscErrorCode ierr;
5452c58f1c22SToby Isaac 
5453c58f1c22SToby Isaac   PetscFunctionBegin;
5454c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5455c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5456c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5457c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5458c58f1c22SToby Isaac   while (next) {
5459c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5460c58f1c22SToby Isaac     if (*hasLabel) break;
5461c58f1c22SToby Isaac     next = next->next;
5462c58f1c22SToby Isaac   }
5463c58f1c22SToby Isaac   PetscFunctionReturn(0);
5464c58f1c22SToby Isaac }
5465c58f1c22SToby Isaac 
5466c58f1c22SToby Isaac #undef __FUNCT__
5467c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabel"
5468c58f1c22SToby Isaac /*@C
5469c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5470c58f1c22SToby Isaac 
5471c58f1c22SToby Isaac   Not Collective
5472c58f1c22SToby Isaac 
5473c58f1c22SToby Isaac   Input Parameters:
5474c58f1c22SToby Isaac + dm   - The DM object
5475c58f1c22SToby Isaac - name - The label name
5476c58f1c22SToby Isaac 
5477c58f1c22SToby Isaac   Output Parameter:
5478c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5479c58f1c22SToby Isaac 
5480c58f1c22SToby Isaac   Level: intermediate
5481c58f1c22SToby Isaac 
5482c58f1c22SToby Isaac .keywords: mesh
5483c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5484c58f1c22SToby Isaac @*/
5485c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5486c58f1c22SToby Isaac {
5487c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5488c58f1c22SToby Isaac   PetscBool      hasLabel;
5489c58f1c22SToby Isaac   PetscErrorCode ierr;
5490c58f1c22SToby Isaac 
5491c58f1c22SToby Isaac   PetscFunctionBegin;
5492c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5493c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5494c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5495c58f1c22SToby Isaac   *label = NULL;
5496c58f1c22SToby Isaac   while (next) {
5497c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5498c58f1c22SToby Isaac     if (hasLabel) {
5499c58f1c22SToby Isaac       *label = next->label;
5500c58f1c22SToby Isaac       break;
5501c58f1c22SToby Isaac     }
5502c58f1c22SToby Isaac     next = next->next;
5503c58f1c22SToby Isaac   }
5504c58f1c22SToby Isaac   PetscFunctionReturn(0);
5505c58f1c22SToby Isaac }
5506c58f1c22SToby Isaac 
5507c58f1c22SToby Isaac #undef __FUNCT__
5508c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelByNum"
5509c58f1c22SToby Isaac /*@C
5510c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5511c58f1c22SToby Isaac 
5512c58f1c22SToby Isaac   Not Collective
5513c58f1c22SToby Isaac 
5514c58f1c22SToby Isaac   Input Parameters:
5515c58f1c22SToby Isaac + dm - The DM object
5516c58f1c22SToby Isaac - n  - the label number
5517c58f1c22SToby Isaac 
5518c58f1c22SToby Isaac   Output Parameter:
5519c58f1c22SToby Isaac . label - the label
5520c58f1c22SToby Isaac 
5521c58f1c22SToby Isaac   Level: intermediate
5522c58f1c22SToby Isaac 
5523c58f1c22SToby Isaac .keywords: mesh
5524c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5525c58f1c22SToby Isaac @*/
5526c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5527c58f1c22SToby Isaac {
5528c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5529c58f1c22SToby Isaac   PetscInt    l    = 0;
5530c58f1c22SToby Isaac 
5531c58f1c22SToby Isaac   PetscFunctionBegin;
5532c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5533c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5534c58f1c22SToby Isaac   while (next) {
5535c58f1c22SToby Isaac     if (l == n) {
5536c58f1c22SToby Isaac       *label = next->label;
5537c58f1c22SToby Isaac       PetscFunctionReturn(0);
5538c58f1c22SToby Isaac     }
5539c58f1c22SToby Isaac     ++l;
5540c58f1c22SToby Isaac     next = next->next;
5541c58f1c22SToby Isaac   }
5542c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5543c58f1c22SToby Isaac }
5544c58f1c22SToby Isaac 
5545c58f1c22SToby Isaac #undef __FUNCT__
5546c58f1c22SToby Isaac #define __FUNCT__ "DMAddLabel"
5547c58f1c22SToby Isaac /*@C
5548c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5549c58f1c22SToby Isaac 
5550c58f1c22SToby Isaac   Not Collective
5551c58f1c22SToby Isaac 
5552c58f1c22SToby Isaac   Input Parameters:
5553c58f1c22SToby Isaac + dm   - The DM object
5554c58f1c22SToby Isaac - label - The DMLabel
5555c58f1c22SToby Isaac 
5556c58f1c22SToby Isaac   Level: developer
5557c58f1c22SToby Isaac 
5558c58f1c22SToby Isaac .keywords: mesh
5559c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5560c58f1c22SToby Isaac @*/
5561c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5562c58f1c22SToby Isaac {
5563c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5564c58f1c22SToby Isaac   PetscBool      hasLabel;
5565c58f1c22SToby Isaac   PetscErrorCode ierr;
5566c58f1c22SToby Isaac 
5567c58f1c22SToby Isaac   PetscFunctionBegin;
5568c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5569c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5570c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5571c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5572c58f1c22SToby Isaac   tmpLabel->label  = label;
5573c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5574c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5575c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5576c58f1c22SToby Isaac   PetscFunctionReturn(0);
5577c58f1c22SToby Isaac }
5578c58f1c22SToby Isaac 
5579c58f1c22SToby Isaac #undef __FUNCT__
5580c58f1c22SToby Isaac #define __FUNCT__ "DMRemoveLabel"
5581c58f1c22SToby Isaac /*@C
5582c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5583c58f1c22SToby Isaac 
5584c58f1c22SToby Isaac   Not Collective
5585c58f1c22SToby Isaac 
5586c58f1c22SToby Isaac   Input Parameters:
5587c58f1c22SToby Isaac + dm   - The DM object
5588c58f1c22SToby Isaac - name - The label name
5589c58f1c22SToby Isaac 
5590c58f1c22SToby Isaac   Output Parameter:
5591c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5592c58f1c22SToby Isaac 
5593c58f1c22SToby Isaac   Level: developer
5594c58f1c22SToby Isaac 
5595c58f1c22SToby Isaac .keywords: mesh
5596c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5597c58f1c22SToby Isaac @*/
5598c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5599c58f1c22SToby Isaac {
5600c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5601c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5602c58f1c22SToby Isaac   PetscBool      hasLabel;
5603c58f1c22SToby Isaac   PetscErrorCode ierr;
5604c58f1c22SToby Isaac 
5605c58f1c22SToby Isaac   PetscFunctionBegin;
5606c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5607c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5608c58f1c22SToby Isaac   *label = NULL;
5609c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5610c58f1c22SToby Isaac   while (next) {
5611c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5612c58f1c22SToby Isaac     if (hasLabel) {
5613c58f1c22SToby Isaac       if (last) last->next       = next->next;
5614c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5615c58f1c22SToby Isaac       next->next = NULL;
5616c58f1c22SToby Isaac       *label     = next->label;
5617c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5618c58f1c22SToby Isaac       if (hasLabel) {
5619c58f1c22SToby Isaac         dm->depthLabel = NULL;
5620c58f1c22SToby Isaac       }
5621c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5622c58f1c22SToby Isaac       break;
5623c58f1c22SToby Isaac     }
5624c58f1c22SToby Isaac     last = next;
5625c58f1c22SToby Isaac     next = next->next;
5626c58f1c22SToby Isaac   }
5627c58f1c22SToby Isaac   PetscFunctionReturn(0);
5628c58f1c22SToby Isaac }
5629c58f1c22SToby Isaac 
5630c58f1c22SToby Isaac #undef __FUNCT__
5631c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelOutput"
5632c58f1c22SToby Isaac /*@C
5633c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5634c58f1c22SToby Isaac 
5635c58f1c22SToby Isaac   Not Collective
5636c58f1c22SToby Isaac 
5637c58f1c22SToby Isaac   Input Parameters:
5638c58f1c22SToby Isaac + dm   - The DM object
5639c58f1c22SToby Isaac - name - The label name
5640c58f1c22SToby Isaac 
5641c58f1c22SToby Isaac   Output Parameter:
5642c58f1c22SToby Isaac . output - The flag for output
5643c58f1c22SToby Isaac 
5644c58f1c22SToby Isaac   Level: developer
5645c58f1c22SToby Isaac 
5646c58f1c22SToby Isaac .keywords: mesh
5647c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5648c58f1c22SToby Isaac @*/
5649c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5650c58f1c22SToby Isaac {
5651c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5652c58f1c22SToby Isaac   PetscErrorCode ierr;
5653c58f1c22SToby Isaac 
5654c58f1c22SToby Isaac   PetscFunctionBegin;
5655c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5656c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5657c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5658c58f1c22SToby Isaac   while (next) {
5659c58f1c22SToby Isaac     PetscBool flg;
5660c58f1c22SToby Isaac 
5661c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5662c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5663c58f1c22SToby Isaac     next = next->next;
5664c58f1c22SToby Isaac   }
5665c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5666c58f1c22SToby Isaac }
5667c58f1c22SToby Isaac 
5668c58f1c22SToby Isaac #undef __FUNCT__
5669c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelOutput"
5670c58f1c22SToby Isaac /*@C
5671c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5672c58f1c22SToby Isaac 
5673c58f1c22SToby Isaac   Not Collective
5674c58f1c22SToby Isaac 
5675c58f1c22SToby Isaac   Input Parameters:
5676c58f1c22SToby Isaac + dm     - The DM object
5677c58f1c22SToby Isaac . name   - The label name
5678c58f1c22SToby Isaac - output - The flag for output
5679c58f1c22SToby Isaac 
5680c58f1c22SToby Isaac   Level: developer
5681c58f1c22SToby Isaac 
5682c58f1c22SToby Isaac .keywords: mesh
5683c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5684c58f1c22SToby Isaac @*/
5685c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5686c58f1c22SToby Isaac {
5687c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5688c58f1c22SToby Isaac   PetscErrorCode ierr;
5689c58f1c22SToby Isaac 
5690c58f1c22SToby Isaac   PetscFunctionBegin;
5691c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5692c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5693c58f1c22SToby Isaac   while (next) {
5694c58f1c22SToby Isaac     PetscBool flg;
5695c58f1c22SToby Isaac 
5696c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5697c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5698c58f1c22SToby Isaac     next = next->next;
5699c58f1c22SToby Isaac   }
5700c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5701c58f1c22SToby Isaac }
5702c58f1c22SToby Isaac 
5703c58f1c22SToby Isaac 
5704c58f1c22SToby Isaac #undef __FUNCT__
5705c58f1c22SToby Isaac #define __FUNCT__ "DMCopyLabels"
5706c58f1c22SToby Isaac /*@
5707c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5708c58f1c22SToby Isaac 
5709c58f1c22SToby Isaac   Collective on DM
5710c58f1c22SToby Isaac 
5711c58f1c22SToby Isaac   Input Parameter:
57122e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5713c58f1c22SToby Isaac 
5714c58f1c22SToby Isaac   Output Parameter:
57152e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5716c58f1c22SToby Isaac 
5717c58f1c22SToby Isaac   Level: intermediate
5718c58f1c22SToby Isaac 
5719c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5720c58f1c22SToby Isaac 
5721c58f1c22SToby Isaac .keywords: mesh
5722c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5723c58f1c22SToby Isaac @*/
5724c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5725c58f1c22SToby Isaac {
5726c58f1c22SToby Isaac   PetscInt       numLabels, l;
5727c58f1c22SToby Isaac   PetscErrorCode ierr;
5728c58f1c22SToby Isaac 
5729c58f1c22SToby Isaac   PetscFunctionBegin;
5730c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5731c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5732c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5733c58f1c22SToby Isaac     DMLabel     label, labelNew;
5734c58f1c22SToby Isaac     const char *name;
5735c58f1c22SToby Isaac     PetscBool   flg;
5736c58f1c22SToby Isaac 
5737c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5738c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5739c58f1c22SToby Isaac     if (flg) continue;
5740c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5741c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5742c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5743c58f1c22SToby Isaac   }
5744c58f1c22SToby Isaac   PetscFunctionReturn(0);
5745c58f1c22SToby Isaac }
5746a8fb8f29SToby Isaac 
5747a8fb8f29SToby Isaac #undef __FUNCT__
5748a8fb8f29SToby Isaac #define __FUNCT__ "DMGetCoarseDM"
5749a8fb8f29SToby Isaac /*@
5750a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5751a8fb8f29SToby Isaac 
5752a8fb8f29SToby Isaac   Input Parameter:
5753a8fb8f29SToby Isaac . dm - The DM object
5754a8fb8f29SToby Isaac 
5755a8fb8f29SToby Isaac   Output Parameter:
5756a8fb8f29SToby Isaac . cdm - The coarse DM
5757a8fb8f29SToby Isaac 
5758a8fb8f29SToby Isaac   Level: intermediate
5759a8fb8f29SToby Isaac 
5760a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5761a8fb8f29SToby Isaac @*/
5762a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5763a8fb8f29SToby Isaac {
5764a8fb8f29SToby Isaac   PetscFunctionBegin;
5765a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5766a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5767a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5768a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5769a8fb8f29SToby Isaac }
5770a8fb8f29SToby Isaac 
5771a8fb8f29SToby Isaac #undef __FUNCT__
5772a8fb8f29SToby Isaac #define __FUNCT__ "DMSetCoarseDM"
5773a8fb8f29SToby Isaac /*@
5774a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5775a8fb8f29SToby Isaac 
5776a8fb8f29SToby Isaac   Input Parameters:
5777a8fb8f29SToby Isaac + dm - The DM object
5778a8fb8f29SToby Isaac - cdm - The coarse DM
5779a8fb8f29SToby Isaac 
5780a8fb8f29SToby Isaac   Level: intermediate
5781a8fb8f29SToby Isaac 
5782a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5783a8fb8f29SToby Isaac @*/
5784a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5785a8fb8f29SToby Isaac {
5786a8fb8f29SToby Isaac   PetscErrorCode ierr;
5787a8fb8f29SToby Isaac 
5788a8fb8f29SToby Isaac   PetscFunctionBegin;
5789a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5790a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5791a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5792a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5793a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5794a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5795a8fb8f29SToby Isaac }
5796a8fb8f29SToby Isaac 
579788bdff64SToby Isaac #undef __FUNCT__
579888bdff64SToby Isaac #define __FUNCT__ "DMGetFineDM"
579988bdff64SToby Isaac /*@
580088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
580188bdff64SToby Isaac 
580288bdff64SToby Isaac   Input Parameter:
580388bdff64SToby Isaac . dm - The DM object
580488bdff64SToby Isaac 
580588bdff64SToby Isaac   Output Parameter:
580688bdff64SToby Isaac . fdm - The fine DM
580788bdff64SToby Isaac 
580888bdff64SToby Isaac   Level: intermediate
580988bdff64SToby Isaac 
581088bdff64SToby Isaac .seealso: DMSetFineDM()
581188bdff64SToby Isaac @*/
581288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
581388bdff64SToby Isaac {
581488bdff64SToby Isaac   PetscFunctionBegin;
581588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
581688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
581788bdff64SToby Isaac   *fdm = dm->fineMesh;
581888bdff64SToby Isaac   PetscFunctionReturn(0);
581988bdff64SToby Isaac }
582088bdff64SToby Isaac 
582188bdff64SToby Isaac #undef __FUNCT__
582288bdff64SToby Isaac #define __FUNCT__ "DMSetFineDM"
582388bdff64SToby Isaac /*@
582488bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
582588bdff64SToby Isaac 
582688bdff64SToby Isaac   Input Parameters:
582788bdff64SToby Isaac + dm - The DM object
582888bdff64SToby Isaac - fdm - The fine DM
582988bdff64SToby Isaac 
583088bdff64SToby Isaac   Level: intermediate
583188bdff64SToby Isaac 
583288bdff64SToby Isaac .seealso: DMGetFineDM()
583388bdff64SToby Isaac @*/
583488bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
583588bdff64SToby Isaac {
583688bdff64SToby Isaac   PetscErrorCode ierr;
583788bdff64SToby Isaac 
583888bdff64SToby Isaac   PetscFunctionBegin;
583988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
584088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
584188bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
584288bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
584388bdff64SToby Isaac   dm->fineMesh = fdm;
584488bdff64SToby Isaac   PetscFunctionReturn(0);
584588bdff64SToby Isaac }
584688bdff64SToby Isaac 
5847a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5848a6ba4734SToby Isaac 
5849a6ba4734SToby Isaac #undef __FUNCT__
5850bcc5ffd9SToby Isaac #define __FUNCT__ "DMBoundaryDuplicate"
5851354557abSToby Isaac PetscErrorCode DMBoundaryDuplicate(DMBoundaryLinkList bd, DMBoundaryLinkList *boundary)
5852a6ba4734SToby Isaac {
5853bcc5ffd9SToby Isaac   DMBoundary     b = bd->next, b2, bold = NULL;
5854a6ba4734SToby Isaac   PetscErrorCode ierr;
5855a6ba4734SToby Isaac 
5856a6ba4734SToby Isaac   PetscFunctionBegin;
5857bcc5ffd9SToby Isaac   ierr = PetscNew(boundary);CHKERRQ(ierr);
5858bcc5ffd9SToby Isaac   (*boundary)->refct = 1;
5859bcc5ffd9SToby Isaac   (*boundary)->next = NULL;
5860a6ba4734SToby Isaac   for (; b; b = b->next, bold = b2) {
5861a6ba4734SToby Isaac     ierr = PetscNew(&b2);CHKERRQ(ierr);
5862a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
5863a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
5864a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
5865a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
5866a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numcomps, &b2->comps);CHKERRQ(ierr);
5867a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->comps, b->comps, b->numcomps*sizeof(PetscInt));CHKERRQ(ierr);
5868a6ba4734SToby Isaac     b2->label     = NULL;
5869a6ba4734SToby Isaac     b2->essential = b->essential;
5870a6ba4734SToby Isaac     b2->field     = b->field;
5871a6ba4734SToby Isaac     b2->numcomps  = b->numcomps;
5872a6ba4734SToby Isaac     b2->func      = b->func;
5873a6ba4734SToby Isaac     b2->numids    = b->numids;
5874a6ba4734SToby Isaac     b2->ctx       = b->ctx;
5875a6ba4734SToby Isaac     b2->next      = NULL;
5876bcc5ffd9SToby Isaac     if (!(*boundary)->next) (*boundary)->next   = b2;
5877a6ba4734SToby Isaac     if (bold)        bold->next = b2;
5878a6ba4734SToby Isaac   }
5879a6ba4734SToby Isaac   PetscFunctionReturn(0);
5880a6ba4734SToby Isaac }
5881a6ba4734SToby Isaac 
5882a6ba4734SToby Isaac #undef __FUNCT__
5883bcc5ffd9SToby Isaac #define __FUNCT__ "DMBoundaryDestroy"
5884bcc5ffd9SToby Isaac PetscErrorCode DMBoundaryDestroy(DMBoundaryLinkList *boundary)
5885a6ba4734SToby Isaac {
5886a6ba4734SToby Isaac   DMBoundary     b, next;
5887a6ba4734SToby Isaac   PetscErrorCode ierr;
5888a6ba4734SToby Isaac 
5889a6ba4734SToby Isaac   PetscFunctionBegin;
5890a6ba4734SToby Isaac   if (!boundary) PetscFunctionReturn(0);
5891bcc5ffd9SToby Isaac   if (--((*boundary)->refct)) {
5892a6ba4734SToby Isaac     *boundary = NULL;
5893bcc5ffd9SToby Isaac     PetscFunctionReturn(0);
5894bcc5ffd9SToby Isaac   }
5895bcc5ffd9SToby Isaac   b = (*boundary)->next;
5896a6ba4734SToby Isaac   for (; b; b = next) {
5897a6ba4734SToby Isaac     next = b->next;
5898a6ba4734SToby Isaac     ierr = PetscFree(b->comps);CHKERRQ(ierr);
5899a6ba4734SToby Isaac     ierr = PetscFree(b->ids);CHKERRQ(ierr);
5900a6ba4734SToby Isaac     ierr = PetscFree(b->name);CHKERRQ(ierr);
5901a6ba4734SToby Isaac     ierr = PetscFree(b->labelname);CHKERRQ(ierr);
5902a6ba4734SToby Isaac     ierr = PetscFree(b);CHKERRQ(ierr);
5903a6ba4734SToby Isaac   }
5904bcc5ffd9SToby Isaac   ierr = PetscFree(*boundary);CHKERRQ(ierr);
5905a6ba4734SToby Isaac   PetscFunctionReturn(0);
5906a6ba4734SToby Isaac }
5907a6ba4734SToby Isaac 
5908a6ba4734SToby Isaac #undef __FUNCT__
5909a6ba4734SToby Isaac #define __FUNCT__ "DMCopyBoundary"
5910a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5911a6ba4734SToby Isaac {
5912a6ba4734SToby Isaac   DMBoundary     b;
5913a6ba4734SToby Isaac   PetscErrorCode ierr;
5914a6ba4734SToby Isaac 
5915a6ba4734SToby Isaac   PetscFunctionBegin;
5916bcc5ffd9SToby Isaac   ierr = DMBoundaryDestroy(&dmNew->boundary);CHKERRQ(ierr);
5917bcc5ffd9SToby Isaac   ierr = DMBoundaryDuplicate(dm->boundary, &dmNew->boundary);CHKERRQ(ierr);
5918bcc5ffd9SToby Isaac   for (b = dmNew->boundary->next; b; b = b->next) {
5919a6ba4734SToby Isaac     if (b->labelname) {
5920a6ba4734SToby Isaac       ierr = DMGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
5921a6ba4734SToby Isaac       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5922a6ba4734SToby Isaac     }
5923a6ba4734SToby Isaac   }
5924a6ba4734SToby Isaac   PetscFunctionReturn(0);
5925a6ba4734SToby Isaac }
5926a6ba4734SToby Isaac 
5927a6ba4734SToby Isaac #undef __FUNCT__
5928a6ba4734SToby Isaac #define __FUNCT__ "DMAddBoundary"
5929a6ba4734SToby Isaac /*@C
5930a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5931a6ba4734SToby Isaac 
5932a6ba4734SToby Isaac   Input Parameters:
5933a6ba4734SToby Isaac + dm          - The mesh object
5934a6ba4734SToby Isaac . isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5935a6ba4734SToby Isaac . name        - The BC name
5936a6ba4734SToby Isaac . labelname   - The label defining constrained points
5937a6ba4734SToby Isaac . field       - The field to constrain
5938a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5939a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5940a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5941a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5942a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5943a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5944a6ba4734SToby Isaac 
5945a6ba4734SToby Isaac   Options Database Keys:
5946a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5947a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5948a6ba4734SToby Isaac 
5949a6ba4734SToby Isaac   Level: developer
5950a6ba4734SToby Isaac 
5951a6ba4734SToby Isaac .seealso: DMGetBoundary()
5952a6ba4734SToby Isaac @*/
5953a6ba4734SToby 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)
5954a6ba4734SToby Isaac {
5955a6ba4734SToby Isaac   DMBoundary     b;
5956a6ba4734SToby Isaac   PetscErrorCode ierr;
5957a6ba4734SToby Isaac 
5958a6ba4734SToby Isaac   PetscFunctionBegin;
5959a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5960a6ba4734SToby Isaac   ierr = PetscNew(&b);CHKERRQ(ierr);
5961a6ba4734SToby Isaac   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
5962a6ba4734SToby Isaac   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
5963a6ba4734SToby Isaac   ierr = PetscMalloc1(numcomps, &b->comps);CHKERRQ(ierr);
5964a6ba4734SToby Isaac   if (numcomps) {ierr = PetscMemcpy(b->comps, comps, numcomps*sizeof(PetscInt));CHKERRQ(ierr);}
5965a6ba4734SToby Isaac   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
5966a6ba4734SToby Isaac   if (numids) {ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);}
5967a6ba4734SToby Isaac   if (b->labelname) {
5968a6ba4734SToby Isaac     ierr = DMGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
5969a6ba4734SToby Isaac     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5970a6ba4734SToby Isaac   }
5971a6ba4734SToby Isaac   b->essential       = isEssential;
5972a6ba4734SToby Isaac   b->field           = field;
5973a6ba4734SToby Isaac   b->numcomps        = numcomps;
5974a6ba4734SToby Isaac   b->func            = bcFunc;
5975a6ba4734SToby Isaac   b->numids          = numids;
5976a6ba4734SToby Isaac   b->ctx             = ctx;
5977bcc5ffd9SToby Isaac   b->next            = dm->boundary->next;
5978bcc5ffd9SToby Isaac   dm->boundary->next = b;
5979a6ba4734SToby Isaac   PetscFunctionReturn(0);
5980a6ba4734SToby Isaac }
5981a6ba4734SToby Isaac 
5982a6ba4734SToby Isaac #undef __FUNCT__
5983a6ba4734SToby Isaac #define __FUNCT__ "DMGetNumBoundary"
5984a6ba4734SToby Isaac /*@
5985a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5986a6ba4734SToby Isaac 
5987a6ba4734SToby Isaac   Input Parameters:
5988a6ba4734SToby Isaac . dm - The mesh object
5989a6ba4734SToby Isaac 
5990a6ba4734SToby Isaac   Output Parameters:
5991a6ba4734SToby Isaac . numBd - The number of BC
5992a6ba4734SToby Isaac 
5993a6ba4734SToby Isaac   Level: intermediate
5994a6ba4734SToby Isaac 
5995a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5996a6ba4734SToby Isaac @*/
5997a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5998a6ba4734SToby Isaac {
5999bcc5ffd9SToby Isaac   DMBoundary b = dm->boundary->next;
6000a6ba4734SToby Isaac 
6001a6ba4734SToby Isaac   PetscFunctionBegin;
6002a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6003a6ba4734SToby Isaac   PetscValidPointer(numBd, 2);
6004a6ba4734SToby Isaac   *numBd = 0;
6005a6ba4734SToby Isaac   while (b) {++(*numBd); b = b->next;}
6006a6ba4734SToby Isaac   PetscFunctionReturn(0);
6007a6ba4734SToby Isaac }
6008a6ba4734SToby Isaac 
6009a6ba4734SToby Isaac #undef __FUNCT__
6010a6ba4734SToby Isaac #define __FUNCT__ "DMGetBoundary"
6011a6ba4734SToby Isaac /*@C
6012a6ba4734SToby Isaac   DMGetBoundary - Add a boundary condition to the model
6013a6ba4734SToby Isaac 
6014a6ba4734SToby Isaac   Input Parameters:
6015a6ba4734SToby Isaac + dm          - The mesh object
6016a6ba4734SToby Isaac - bd          - The BC number
6017a6ba4734SToby Isaac 
6018a6ba4734SToby Isaac   Output Parameters:
6019a6ba4734SToby Isaac + isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
6020a6ba4734SToby Isaac . name        - The BC name
6021a6ba4734SToby Isaac . labelname   - The label defining constrained points
6022a6ba4734SToby Isaac . field       - The field to constrain
6023a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6024a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6025a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6026a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6027a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6028a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6029a6ba4734SToby Isaac 
6030a6ba4734SToby Isaac   Options Database Keys:
6031a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6032a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6033a6ba4734SToby Isaac 
6034a6ba4734SToby Isaac   Level: developer
6035a6ba4734SToby Isaac 
6036a6ba4734SToby Isaac .seealso: DMAddBoundary()
6037a6ba4734SToby Isaac @*/
6038a6ba4734SToby 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)
6039a6ba4734SToby Isaac {
6040bcc5ffd9SToby Isaac   DMBoundary b    = dm->boundary->next;
6041a6ba4734SToby Isaac   PetscInt   n    = 0;
6042a6ba4734SToby Isaac 
6043a6ba4734SToby Isaac   PetscFunctionBegin;
6044a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6045a6ba4734SToby Isaac   while (b) {
6046a6ba4734SToby Isaac     if (n == bd) break;
6047a6ba4734SToby Isaac     b = b->next;
6048a6ba4734SToby Isaac     ++n;
6049a6ba4734SToby Isaac   }
6050a6ba4734SToby Isaac   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
6051a6ba4734SToby Isaac   if (isEssential) {
6052a6ba4734SToby Isaac     PetscValidPointer(isEssential, 3);
6053a6ba4734SToby Isaac     *isEssential = b->essential;
6054a6ba4734SToby Isaac   }
6055a6ba4734SToby Isaac   if (name) {
6056a6ba4734SToby Isaac     PetscValidPointer(name, 4);
6057a6ba4734SToby Isaac     *name = b->name;
6058a6ba4734SToby Isaac   }
6059a6ba4734SToby Isaac   if (labelname) {
6060a6ba4734SToby Isaac     PetscValidPointer(labelname, 5);
6061a6ba4734SToby Isaac     *labelname = b->labelname;
6062a6ba4734SToby Isaac   }
6063a6ba4734SToby Isaac   if (field) {
6064a6ba4734SToby Isaac     PetscValidPointer(field, 6);
6065a6ba4734SToby Isaac     *field = b->field;
6066a6ba4734SToby Isaac   }
6067a6ba4734SToby Isaac   if (numcomps) {
6068a6ba4734SToby Isaac     PetscValidPointer(numcomps, 7);
6069a6ba4734SToby Isaac     *numcomps = b->numcomps;
6070a6ba4734SToby Isaac   }
6071a6ba4734SToby Isaac   if (comps) {
6072a6ba4734SToby Isaac     PetscValidPointer(comps, 8);
6073a6ba4734SToby Isaac     *comps = b->comps;
6074a6ba4734SToby Isaac   }
6075a6ba4734SToby Isaac   if (func) {
6076a6ba4734SToby Isaac     PetscValidPointer(func, 9);
6077a6ba4734SToby Isaac     *func = b->func;
6078a6ba4734SToby Isaac   }
6079a6ba4734SToby Isaac   if (numids) {
6080a6ba4734SToby Isaac     PetscValidPointer(numids, 10);
6081a6ba4734SToby Isaac     *numids = b->numids;
6082a6ba4734SToby Isaac   }
6083a6ba4734SToby Isaac   if (ids) {
6084a6ba4734SToby Isaac     PetscValidPointer(ids, 11);
6085a6ba4734SToby Isaac     *ids = b->ids;
6086a6ba4734SToby Isaac   }
6087a6ba4734SToby Isaac   if (ctx) {
6088a6ba4734SToby Isaac     PetscValidPointer(ctx, 12);
6089a6ba4734SToby Isaac     *ctx = b->ctx;
6090a6ba4734SToby Isaac   }
6091a6ba4734SToby Isaac   PetscFunctionReturn(0);
6092a6ba4734SToby Isaac }
6093a6ba4734SToby Isaac 
6094a6ba4734SToby Isaac #undef __FUNCT__
6095a6ba4734SToby Isaac #define __FUNCT__ "DMIsBoundaryPoint"
6096a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6097a6ba4734SToby Isaac {
6098bcc5ffd9SToby Isaac   DMBoundary     b    = dm->boundary->next;
6099a6ba4734SToby Isaac   PetscErrorCode ierr;
6100a6ba4734SToby Isaac 
6101a6ba4734SToby Isaac   PetscFunctionBegin;
6102a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6103a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6104a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6105a6ba4734SToby Isaac   while (b && !(*isBd)) {
6106a6ba4734SToby Isaac     if (b->label) {
6107a6ba4734SToby Isaac       PetscInt i;
6108a6ba4734SToby Isaac 
6109a6ba4734SToby Isaac       for (i = 0; i < b->numids && !(*isBd); ++i) {
6110a6ba4734SToby Isaac         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
6111a6ba4734SToby Isaac       }
6112a6ba4734SToby Isaac     }
6113a6ba4734SToby Isaac     b = b->next;
6114a6ba4734SToby Isaac   }
6115a6ba4734SToby Isaac   PetscFunctionReturn(0);
6116a6ba4734SToby Isaac }
61174d6f44ffSToby Isaac 
61184d6f44ffSToby Isaac #undef __FUNCT__
61194d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunction"
61204d6f44ffSToby Isaac /*@C
61214d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
61224d6f44ffSToby Isaac 
61234d6f44ffSToby Isaac   Input Parameters:
61244d6f44ffSToby Isaac + dm      - The DM
61250709b2feSToby Isaac . time    - The time
61264d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
61274d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
61284d6f44ffSToby Isaac - mode    - The insertion mode for values
61294d6f44ffSToby Isaac 
61304d6f44ffSToby Isaac   Output Parameter:
61314d6f44ffSToby Isaac . X - vector
61324d6f44ffSToby Isaac 
61334d6f44ffSToby Isaac    Calling sequence of func:
61340709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
61354d6f44ffSToby Isaac 
61364d6f44ffSToby Isaac +  dim - The spatial dimension
61374d6f44ffSToby Isaac .  x   - The coordinates
61384d6f44ffSToby Isaac .  Nf  - The number of fields
61394d6f44ffSToby Isaac .  u   - The output field values
61404d6f44ffSToby Isaac -  ctx - optional user-defined function context
61414d6f44ffSToby Isaac 
61424d6f44ffSToby Isaac   Level: developer
61434d6f44ffSToby Isaac 
61442716604bSToby Isaac .seealso: DMComputeL2Diff()
61454d6f44ffSToby Isaac @*/
61460709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
61474d6f44ffSToby Isaac {
61484d6f44ffSToby Isaac   Vec            localX;
61494d6f44ffSToby Isaac   PetscErrorCode ierr;
61504d6f44ffSToby Isaac 
61514d6f44ffSToby Isaac   PetscFunctionBegin;
61524d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61534d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
61540709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61554d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
61564d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
61574d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
61584d6f44ffSToby Isaac   PetscFunctionReturn(0);
61594d6f44ffSToby Isaac }
61604d6f44ffSToby Isaac 
61614d6f44ffSToby Isaac #undef __FUNCT__
61624d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal"
61630709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
61644d6f44ffSToby Isaac {
61654d6f44ffSToby Isaac   PetscErrorCode ierr;
61664d6f44ffSToby Isaac 
61674d6f44ffSToby Isaac   PetscFunctionBegin;
61684d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61694d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61704d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
61710709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61724d6f44ffSToby Isaac   PetscFunctionReturn(0);
61734d6f44ffSToby Isaac }
61744d6f44ffSToby Isaac 
61754d6f44ffSToby Isaac #undef __FUNCT__
6176bfc4295aSToby Isaac #define __FUNCT__ "DMProjectFieldLocal"
6177bfc4295aSToby Isaac PetscErrorCode DMProjectFieldLocal(DM dm, Vec localU,
6178bfc4295aSToby Isaac                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
6179bfc4295aSToby Isaac                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6180bfc4295aSToby Isaac                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6181bfc4295aSToby Isaac                                                   PetscReal, const PetscReal[], PetscScalar[]),
6182bfc4295aSToby Isaac                                    InsertMode mode, Vec localX)
6183bfc4295aSToby Isaac {
6184bfc4295aSToby Isaac   PetscErrorCode ierr;
6185bfc4295aSToby Isaac 
6186bfc4295aSToby Isaac   PetscFunctionBegin;
6187bfc4295aSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6188bfc4295aSToby Isaac   PetscValidHeaderSpecific(localU,VEC_CLASSID,2);
6189bfc4295aSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
6190bfc4295aSToby Isaac   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
6191bfc4295aSToby Isaac   ierr = (dm->ops->projectfieldlocal) (dm, localU, funcs, mode, localX);CHKERRQ(ierr);
6192bfc4295aSToby Isaac   PetscFunctionReturn(0);
6193bfc4295aSToby Isaac }
6194bfc4295aSToby Isaac 
6195bfc4295aSToby Isaac #undef __FUNCT__
61964d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLabelLocal"
61970709b2feSToby Isaac PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
61984d6f44ffSToby Isaac {
61994d6f44ffSToby Isaac   PetscErrorCode ierr;
62004d6f44ffSToby Isaac 
62014d6f44ffSToby Isaac   PetscFunctionBegin;
62024d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62034d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62044d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
62050709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62064d6f44ffSToby Isaac   PetscFunctionReturn(0);
62074d6f44ffSToby Isaac }
62082716604bSToby Isaac 
62092716604bSToby Isaac #undef __FUNCT__
62102716604bSToby Isaac #define __FUNCT__ "DMComputeL2Diff"
62112716604bSToby Isaac /*@C
62122716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
62132716604bSToby Isaac 
62142716604bSToby Isaac   Input Parameters:
62152716604bSToby Isaac + dm    - The DM
62160709b2feSToby Isaac . time  - The time
62172716604bSToby Isaac . funcs - The functions to evaluate for each field component
62182716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
62192716604bSToby Isaac - X     - The coefficient vector u_h
62202716604bSToby Isaac 
62212716604bSToby Isaac   Output Parameter:
62222716604bSToby Isaac . diff - The diff ||u - u_h||_2
62232716604bSToby Isaac 
62242716604bSToby Isaac   Level: developer
62252716604bSToby Isaac 
62261189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62272716604bSToby Isaac @*/
62280709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
62292716604bSToby Isaac {
62302716604bSToby Isaac   PetscErrorCode ierr;
62312716604bSToby Isaac 
62322716604bSToby Isaac   PetscFunctionBegin;
62332716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6234b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62352716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
62360709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62372716604bSToby Isaac   PetscFunctionReturn(0);
62382716604bSToby Isaac }
6239b698f381SToby Isaac 
6240b698f381SToby Isaac #undef __FUNCT__
6241b698f381SToby Isaac #define __FUNCT__ "DMComputeL2GradientDiff"
6242b698f381SToby Isaac /*@C
6243b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6244b698f381SToby Isaac 
6245b698f381SToby Isaac   Input Parameters:
6246b698f381SToby Isaac + dm    - The DM
6247b698f381SToby Isaac , time  - The time
6248b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6249b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6250b698f381SToby Isaac . X     - The coefficient vector u_h
6251b698f381SToby Isaac - n     - The vector to project along
6252b698f381SToby Isaac 
6253b698f381SToby Isaac   Output Parameter:
6254b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6255b698f381SToby Isaac 
6256b698f381SToby Isaac   Level: developer
6257b698f381SToby Isaac 
6258b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6259b698f381SToby Isaac @*/
6260b698f381SToby Isaac PetscErrorCode DMComputeL2GradientDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
6261b698f381SToby Isaac {
6262b698f381SToby Isaac   PetscErrorCode ierr;
6263b698f381SToby Isaac 
6264b698f381SToby Isaac   PetscFunctionBegin;
6265b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6266b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6267b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6268b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6269b698f381SToby Isaac   PetscFunctionReturn(0);
6270b698f381SToby Isaac }
6271b698f381SToby Isaac 
62722a16baeaSToby Isaac #undef __FUNCT__
62732a16baeaSToby Isaac #define __FUNCT__ "DMComputeL2FieldDiff"
62742a16baeaSToby Isaac /*@C
62752a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
62762a16baeaSToby Isaac 
62772a16baeaSToby Isaac   Input Parameters:
62782a16baeaSToby Isaac + dm    - The DM
62792a16baeaSToby Isaac . time  - The time
62802a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
62812a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
62822a16baeaSToby Isaac - X     - The coefficient vector u_h
62832a16baeaSToby Isaac 
62842a16baeaSToby Isaac   Output Parameter:
62852a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
62862a16baeaSToby Isaac 
62872a16baeaSToby Isaac   Level: developer
62882a16baeaSToby Isaac 
62891189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62902a16baeaSToby Isaac @*/
62911189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
62922a16baeaSToby Isaac {
62932a16baeaSToby Isaac   PetscErrorCode ierr;
62942a16baeaSToby Isaac 
62952a16baeaSToby Isaac   PetscFunctionBegin;
62962a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62972a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62982a16baeaSToby Isaac   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
62992a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63002a16baeaSToby Isaac   PetscFunctionReturn(0);
63012a16baeaSToby Isaac }
63022a16baeaSToby Isaac 
6303