xref: /petsc/src/dm/interface/dm.c (revision 3ad4599a42e9c85bdaf3228acb15d805000068aa)
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;
7*3ad4599aSBarry 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 
1014*3ad4599aSBarry 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__
1031*3ad4599aSBarry Smith #define __FUNCT__ "DMCreateRestriction"
1032*3ad4599aSBarry Smith /*@
1033*3ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
1034*3ad4599aSBarry Smith 
1035*3ad4599aSBarry Smith     Collective on DM
1036*3ad4599aSBarry Smith 
1037*3ad4599aSBarry Smith     Input Parameter:
1038*3ad4599aSBarry Smith +   dm1 - the DM object
1039*3ad4599aSBarry Smith -   dm2 - the second, finer DM object
1040*3ad4599aSBarry Smith 
1041*3ad4599aSBarry Smith     Output Parameter:
1042*3ad4599aSBarry Smith .  mat - the restriction
1043*3ad4599aSBarry Smith 
1044*3ad4599aSBarry Smith 
1045*3ad4599aSBarry Smith     Level: developer
1046*3ad4599aSBarry Smith 
1047*3ad4599aSBarry 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
1048*3ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1049*3ad4599aSBarry Smith 
1050*3ad4599aSBarry Smith 
1051*3ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
1052*3ad4599aSBarry Smith 
1053*3ad4599aSBarry Smith @*/
1054*3ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
1055*3ad4599aSBarry Smith {
1056*3ad4599aSBarry Smith   PetscErrorCode ierr;
1057*3ad4599aSBarry Smith 
1058*3ad4599aSBarry Smith   PetscFunctionBegin;
1059*3ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1060*3ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1061*3ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
1062*3ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
1063*3ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
1064*3ad4599aSBarry Smith   PetscFunctionReturn(0);
1065*3ad4599aSBarry Smith }
1066*3ad4599aSBarry Smith 
1067*3ad4599aSBarry 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);
10966dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
109747c6ae99SBarry Smith   PetscFunctionReturn(0);
109847c6ae99SBarry Smith }
109947c6ae99SBarry Smith 
110047c6ae99SBarry Smith #undef __FUNCT__
1101e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
1102b412c318SBarry Smith /*@
1103b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
110447c6ae99SBarry Smith 
110547c6ae99SBarry Smith     Collective on DM
110647c6ae99SBarry Smith 
110747c6ae99SBarry Smith     Input Parameter:
110847c6ae99SBarry Smith +   dm - the DM object
1109b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
111047c6ae99SBarry Smith 
111147c6ae99SBarry Smith     Output Parameter:
111247c6ae99SBarry Smith .   coloring - the coloring
111347c6ae99SBarry Smith 
111447c6ae99SBarry Smith     Level: developer
111547c6ae99SBarry Smith 
1116b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
111747c6ae99SBarry Smith 
1118aab9d709SJed Brown @*/
1119b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
112047c6ae99SBarry Smith {
112147c6ae99SBarry Smith   PetscErrorCode ierr;
112247c6ae99SBarry Smith 
112347c6ae99SBarry Smith   PetscFunctionBegin;
1124171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1125ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1126b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
112747c6ae99SBarry Smith   PetscFunctionReturn(0);
112847c6ae99SBarry Smith }
112947c6ae99SBarry Smith 
113047c6ae99SBarry Smith #undef __FUNCT__
1131950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1132b412c318SBarry Smith /*@
11338472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith     Collective on DM
113647c6ae99SBarry Smith 
113747c6ae99SBarry Smith     Input Parameter:
1138b412c318SBarry Smith .   dm - the DM object
113947c6ae99SBarry Smith 
114047c6ae99SBarry Smith     Output Parameter:
114147c6ae99SBarry Smith .   mat - the empty Jacobian
114247c6ae99SBarry Smith 
1143073dac72SJed Brown     Level: beginner
114447c6ae99SBarry Smith 
114594013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
114694013140SBarry Smith        do not need to do it yourself.
114794013140SBarry Smith 
114894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1149aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
115094013140SBarry Smith 
115194013140SBarry 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
115294013140SBarry Smith        internally by PETSc.
115394013140SBarry Smith 
115494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1155aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
115694013140SBarry Smith 
1157b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
115847c6ae99SBarry Smith 
1159aab9d709SJed Brown @*/
1160b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
116147c6ae99SBarry Smith {
116247c6ae99SBarry Smith   PetscErrorCode ierr;
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith   PetscFunctionBegin;
1165171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1166607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1167c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1168c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1169b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
117047c6ae99SBarry Smith   PetscFunctionReturn(0);
117147c6ae99SBarry Smith }
117247c6ae99SBarry Smith 
117347c6ae99SBarry Smith #undef __FUNCT__
1174732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1175732e2eb9SMatthew G Knepley /*@
1176950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1177732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1178732e2eb9SMatthew G Knepley 
11798472ad0fSDave May   Logically Collective on DM
1180732e2eb9SMatthew G Knepley 
1181732e2eb9SMatthew G Knepley   Input Parameter:
1182732e2eb9SMatthew G Knepley + dm - the DM
1183732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1184732e2eb9SMatthew G Knepley 
1185732e2eb9SMatthew G Knepley   Level: developer
1186950540a4SJed Brown .seealso DMCreateMatrix()
1187732e2eb9SMatthew G Knepley @*/
1188732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1189732e2eb9SMatthew G Knepley {
1190732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1191732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1192732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1193732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1194732e2eb9SMatthew G Knepley }
1195732e2eb9SMatthew G Knepley 
1196732e2eb9SMatthew G Knepley #undef __FUNCT__
1197a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1198a89ea682SMatthew G Knepley /*@C
1199aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1200a89ea682SMatthew G Knepley 
1201a89ea682SMatthew G Knepley   Not Collective
1202a89ea682SMatthew G Knepley 
1203a89ea682SMatthew G Knepley   Input Parameters:
1204a89ea682SMatthew G Knepley + dm - the DM object
1205aa1993deSMatthew G Knepley . count - The minium size
1206aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1207a89ea682SMatthew G Knepley 
1208a89ea682SMatthew G Knepley   Output Parameter:
1209a89ea682SMatthew G Knepley . array - the work array
1210a89ea682SMatthew G Knepley 
1211a89ea682SMatthew G Knepley   Level: developer
1212a89ea682SMatthew G Knepley 
1213a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1214a89ea682SMatthew G Knepley @*/
1215aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1216a89ea682SMatthew G Knepley {
1217a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1218aa1993deSMatthew G Knepley   DMWorkLink     link;
1219854ce69bSBarry Smith   size_t         dsize;
1220a89ea682SMatthew G Knepley 
1221a89ea682SMatthew G Knepley   PetscFunctionBegin;
1222a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1223aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1224aa1993deSMatthew G Knepley   if (dm->workin) {
1225aa1993deSMatthew G Knepley     link       = dm->workin;
1226aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1227aa1993deSMatthew G Knepley   } else {
1228b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1229a89ea682SMatthew G Knepley   }
1230854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1231854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1232aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1233854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1234854ce69bSBarry Smith     link->bytes = dsize*count;
1235aa1993deSMatthew G Knepley   }
1236aa1993deSMatthew G Knepley   link->next   = dm->workout;
1237aa1993deSMatthew G Knepley   dm->workout  = link;
1238aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1239a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1240a89ea682SMatthew G Knepley }
1241a89ea682SMatthew G Knepley 
1242aa1993deSMatthew G Knepley #undef __FUNCT__
1243aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1244aa1993deSMatthew G Knepley /*@C
1245aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1246aa1993deSMatthew G Knepley 
1247aa1993deSMatthew G Knepley   Not Collective
1248aa1993deSMatthew G Knepley 
1249aa1993deSMatthew G Knepley   Input Parameters:
1250aa1993deSMatthew G Knepley + dm - the DM object
1251aa1993deSMatthew G Knepley . count - The minium size
1252aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1253aa1993deSMatthew G Knepley 
1254aa1993deSMatthew G Knepley   Output Parameter:
1255aa1993deSMatthew G Knepley . array - the work array
1256aa1993deSMatthew G Knepley 
1257aa1993deSMatthew G Knepley   Level: developer
1258aa1993deSMatthew G Knepley 
1259aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1260aa1993deSMatthew G Knepley @*/
1261aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1262aa1993deSMatthew G Knepley {
1263aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1264aa1993deSMatthew G Knepley 
1265aa1993deSMatthew G Knepley   PetscFunctionBegin;
1266aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1267aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1268aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1269aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1270aa1993deSMatthew G Knepley       *p           = link->next;
1271aa1993deSMatthew G Knepley       link->next   = dm->workin;
1272aa1993deSMatthew G Knepley       dm->workin   = link;
12730298fd71SBarry Smith       *(void**)mem = NULL;
1274aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1275aa1993deSMatthew G Knepley     }
1276aa1993deSMatthew G Knepley   }
1277aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1278aa1993deSMatthew G Knepley }
1279e7c4fc90SDmitry Karpeev 
1280e7c4fc90SDmitry Karpeev #undef __FUNCT__
1281435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1282435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1283435a35e8SMatthew G Knepley {
1284435a35e8SMatthew G Knepley   PetscFunctionBegin;
1285435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
128682f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1287435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1288435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1289435a35e8SMatthew G Knepley }
1290435a35e8SMatthew G Knepley 
1291435a35e8SMatthew G Knepley #undef __FUNCT__
12924d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
12934f3b5142SJed Brown /*@C
12944d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12954d343eeaSMatthew G Knepley 
12964d343eeaSMatthew G Knepley   Not collective
12974d343eeaSMatthew G Knepley 
12984d343eeaSMatthew G Knepley   Input Parameter:
12994d343eeaSMatthew G Knepley . dm - the DM object
13004d343eeaSMatthew G Knepley 
13014d343eeaSMatthew G Knepley   Output Parameters:
13020298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13030298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13040298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13054d343eeaSMatthew G Knepley 
13064d343eeaSMatthew G Knepley   Level: intermediate
13074d343eeaSMatthew G Knepley 
130821c9b008SJed Brown   Notes:
130921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
131021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
131121c9b008SJed Brown   PetscFree().
131221c9b008SJed Brown 
13134d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13144d343eeaSMatthew G Knepley @*/
131537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13164d343eeaSMatthew G Knepley {
131737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13184d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13194d343eeaSMatthew G Knepley 
13204d343eeaSMatthew G Knepley   PetscFunctionBegin;
13214d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
132269ca1f37SDmitry Karpeev   if (numFields) {
132369ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
132469ca1f37SDmitry Karpeev     *numFields = 0;
132569ca1f37SDmitry Karpeev   }
132637d0c07bSMatthew G Knepley   if (fieldNames) {
132737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13280298fd71SBarry Smith     *fieldNames = NULL;
132969ca1f37SDmitry Karpeev   }
133069ca1f37SDmitry Karpeev   if (fields) {
133169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13320298fd71SBarry Smith     *fields = NULL;
133369ca1f37SDmitry Karpeev   }
133437d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
133537d0c07bSMatthew G Knepley   if (section) {
133637d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
133737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
133837d0c07bSMatthew G Knepley 
133937d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
134037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1341dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
134237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
134337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
134437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
134537d0c07bSMatthew G Knepley     }
134637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
134737d0c07bSMatthew G Knepley       PetscInt gdof;
134837d0c07bSMatthew G Knepley 
134937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
135037d0c07bSMatthew G Knepley       if (gdof > 0) {
135137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
135237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
135337d0c07bSMatthew G Knepley 
135437d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
135537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
135637d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
135737d0c07bSMatthew G Knepley         }
135837d0c07bSMatthew G Knepley       }
135937d0c07bSMatthew G Knepley     }
136037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1361785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
136237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
136337d0c07bSMatthew G Knepley     }
136437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
136537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
136637d0c07bSMatthew G Knepley 
136737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
136837d0c07bSMatthew G Knepley       if (gdof > 0) {
136937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
137037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
137137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
137237d0c07bSMatthew G Knepley 
137337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
137437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
137537d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
137637d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
137737d0c07bSMatthew G Knepley           }
137837d0c07bSMatthew G Knepley         }
137937d0c07bSMatthew G Knepley       }
138037d0c07bSMatthew G Knepley     }
13818865f1eaSKarl Rupp     if (numFields) *numFields = nF;
138237d0c07bSMatthew G Knepley     if (fieldNames) {
1383785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
138437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
138537d0c07bSMatthew G Knepley         const char *fieldName;
138637d0c07bSMatthew G Knepley 
138737d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
138837d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
138937d0c07bSMatthew G Knepley       }
139037d0c07bSMatthew G Knepley     }
139137d0c07bSMatthew G Knepley     if (fields) {
1392785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
139337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
139482f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
139537d0c07bSMatthew G Knepley       }
139637d0c07bSMatthew G Knepley     }
139737d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13988865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
13998865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
140069ca1f37SDmitry Karpeev   }
14014d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14024d343eeaSMatthew G Knepley }
14034d343eeaSMatthew G Knepley 
140416621825SDmitry Karpeev 
140516621825SDmitry Karpeev #undef __FUNCT__
140616621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
140716621825SDmitry Karpeev /*@C
140816621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
140916621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
141016621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1411e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1412e7c4fc90SDmitry Karpeev 
1413e7c4fc90SDmitry Karpeev   Not collective
1414e7c4fc90SDmitry Karpeev 
1415e7c4fc90SDmitry Karpeev   Input Parameter:
1416e7c4fc90SDmitry Karpeev . dm - the DM object
1417e7c4fc90SDmitry Karpeev 
1418e7c4fc90SDmitry Karpeev   Output Parameters:
14190298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14200298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14210298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14220298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1423e7c4fc90SDmitry Karpeev 
1424e7c4fc90SDmitry Karpeev   Level: intermediate
1425e7c4fc90SDmitry Karpeev 
1426e7c4fc90SDmitry Karpeev   Notes:
1427e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1428e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1429e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1430e7c4fc90SDmitry Karpeev 
1431e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1432e7c4fc90SDmitry Karpeev @*/
143316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1434e7c4fc90SDmitry Karpeev {
1435e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1436e7c4fc90SDmitry Karpeev 
1437e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1438e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14398865f1eaSKarl Rupp   if (len) {
14408865f1eaSKarl Rupp     PetscValidPointer(len,2);
14418865f1eaSKarl Rupp     *len = 0;
14428865f1eaSKarl Rupp   }
14438865f1eaSKarl Rupp   if (namelist) {
14448865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14458865f1eaSKarl Rupp     *namelist = 0;
14468865f1eaSKarl Rupp   }
14478865f1eaSKarl Rupp   if (islist) {
14488865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14498865f1eaSKarl Rupp     *islist = 0;
14508865f1eaSKarl Rupp   }
14518865f1eaSKarl Rupp   if (dmlist) {
14528865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14538865f1eaSKarl Rupp     *dmlist = 0;
14548865f1eaSKarl Rupp   }
1455f3f0edfdSDmitry Karpeev   /*
1456f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1457f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1458f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1459f3f0edfdSDmitry Karpeev    */
1460ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
146116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1462435a35e8SMatthew G Knepley     PetscSection section;
1463435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1464435a35e8SMatthew G Knepley 
1465435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1466435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1467435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1468435a35e8SMatthew G Knepley       *len = numFields;
146903dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
147003dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
147103dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1472435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1473435a35e8SMatthew G Knepley         const char *fieldName;
1474435a35e8SMatthew G Knepley 
147503dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
147603dc3394SMatthew G. Knepley         if (namelist) {
1477435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1478435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1479435a35e8SMatthew G Knepley         }
148003dc3394SMatthew G. Knepley       }
1481435a35e8SMatthew G Knepley     } else {
148269ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1483e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14840298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1485e7c4fc90SDmitry Karpeev     }
14868865f1eaSKarl Rupp   } else {
148716621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
148816621825SDmitry Karpeev   }
148916621825SDmitry Karpeev   PetscFunctionReturn(0);
149016621825SDmitry Karpeev }
149116621825SDmitry Karpeev 
149216621825SDmitry Karpeev #undef __FUNCT__
1493435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1494564cec59SMatthew G. Knepley /*@
1495435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1496435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1497435a35e8SMatthew G Knepley 
1498435a35e8SMatthew G Knepley   Not collective
1499435a35e8SMatthew G Knepley 
1500435a35e8SMatthew G Knepley   Input Parameters:
1501435a35e8SMatthew G Knepley + dm - the DM object
1502435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
15030298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1504435a35e8SMatthew G Knepley 
1505435a35e8SMatthew G Knepley   Output Parameters:
1506435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1507435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1508435a35e8SMatthew G Knepley 
1509435a35e8SMatthew G Knepley   Level: intermediate
1510435a35e8SMatthew G Knepley 
1511435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1512435a35e8SMatthew G Knepley @*/
1513435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1514435a35e8SMatthew G Knepley {
1515435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1516435a35e8SMatthew G Knepley 
1517435a35e8SMatthew G Knepley   PetscFunctionBegin;
1518435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1519435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15208865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15218865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1522435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1523435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
152482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1525435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1526435a35e8SMatthew G Knepley }
1527435a35e8SMatthew G Knepley 
152816621825SDmitry Karpeev 
152916621825SDmitry Karpeev #undef __FUNCT__
153016621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
153116621825SDmitry Karpeev /*@C
15328d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
15338d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
15348d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
15358d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
15368d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
153716621825SDmitry Karpeev 
153816621825SDmitry Karpeev   Not collective
153916621825SDmitry Karpeev 
154016621825SDmitry Karpeev   Input Parameter:
154116621825SDmitry Karpeev . dm - the DM object
154216621825SDmitry Karpeev 
154316621825SDmitry Karpeev   Output Parameters:
15440298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15450298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15460298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15470298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15480298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
154916621825SDmitry Karpeev 
155016621825SDmitry Karpeev   Level: intermediate
155116621825SDmitry Karpeev 
155216621825SDmitry Karpeev   Notes:
155316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
155416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
155516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
155616621825SDmitry Karpeev 
15578d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
155816621825SDmitry Karpeev @*/
15598d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
156016621825SDmitry Karpeev {
156116621825SDmitry Karpeev   PetscErrorCode      ierr;
1562be081cd6SPeter Brune   DMSubDomainHookLink link;
1563be081cd6SPeter Brune   PetscInt            i,l;
156416621825SDmitry Karpeev 
156516621825SDmitry Karpeev   PetscFunctionBegin;
156616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
156714a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
15680298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
15690298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
15700298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
15710298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1572f3f0edfdSDmitry Karpeev   /*
1573f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1574f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1575f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1576f3f0edfdSDmitry Karpeev    */
1577ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
157816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1579be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
158014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
158114a18fd3SPeter Brune     if (dmlist) {
1582be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1583be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1584be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1585be081cd6SPeter Brune         }
1586d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1587e7c4fc90SDmitry Karpeev       }
158814a18fd3SPeter Brune     }
158914a18fd3SPeter Brune     if (len) *len = l;
159014a18fd3SPeter Brune   }
1591e30e807fSPeter Brune   PetscFunctionReturn(0);
1592e30e807fSPeter Brune }
1593e30e807fSPeter Brune 
1594e30e807fSPeter Brune 
1595e30e807fSPeter Brune #undef __FUNCT__
1596e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1597e30e807fSPeter Brune /*@C
1598e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1599e30e807fSPeter Brune 
1600e30e807fSPeter Brune   Not collective
1601e30e807fSPeter Brune 
1602e30e807fSPeter Brune   Input Parameters:
1603e30e807fSPeter Brune + dm - the DM object
1604e30e807fSPeter Brune . n  - the number of subdomain scatters
1605e30e807fSPeter Brune - subdms - the local subdomains
1606e30e807fSPeter Brune 
1607e30e807fSPeter Brune   Output Parameters:
1608e30e807fSPeter Brune + n     - the number of scatters returned
1609e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1610e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1611e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1612e30e807fSPeter Brune 
1613e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1614e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1615e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1616e30e807fSPeter Brune   solution and residual data.
1617e30e807fSPeter Brune 
1618e30e807fSPeter Brune   Level: developer
1619e30e807fSPeter Brune 
1620e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1621e30e807fSPeter Brune @*/
1622e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1623e30e807fSPeter Brune {
1624e30e807fSPeter Brune   PetscErrorCode ierr;
1625e30e807fSPeter Brune 
1626e30e807fSPeter Brune   PetscFunctionBegin;
1627e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1628e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1629e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1630e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
163182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1632e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1633e7c4fc90SDmitry Karpeev }
1634e7c4fc90SDmitry Karpeev 
1635731c8d9eSDmitry Karpeev #undef __FUNCT__
163647c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
163747c6ae99SBarry Smith /*@
163847c6ae99SBarry Smith   DMRefine - Refines a DM object
163947c6ae99SBarry Smith 
164047c6ae99SBarry Smith   Collective on DM
164147c6ae99SBarry Smith 
164247c6ae99SBarry Smith   Input Parameter:
164347c6ae99SBarry Smith + dm   - the DM object
164491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
164547c6ae99SBarry Smith 
164647c6ae99SBarry Smith   Output Parameter:
16470298fd71SBarry Smith . dmf - the refined DM, or NULL
1648ae0a1c52SMatthew G Knepley 
16490298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
165047c6ae99SBarry Smith 
165147c6ae99SBarry Smith   Level: developer
165247c6ae99SBarry Smith 
1653e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
165447c6ae99SBarry Smith @*/
16557087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
165647c6ae99SBarry Smith {
165747c6ae99SBarry Smith   PetscErrorCode   ierr;
1658c833c3b5SJed Brown   DMRefineHookLink link;
165947c6ae99SBarry Smith 
166047c6ae99SBarry Smith   PetscFunctionBegin;
1661732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
166247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
16634057135bSMatthew G Knepley   if (*dmf) {
166443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
16658865f1eaSKarl Rupp 
16668cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
16678865f1eaSKarl Rupp 
1668644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
16690598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1670656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
16718865f1eaSKarl Rupp 
1672e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1673c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
16748865f1eaSKarl Rupp       if (link->refinehook) {
16758865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16768865f1eaSKarl Rupp       }
1677c833c3b5SJed Brown     }
1678c833c3b5SJed Brown   }
1679c833c3b5SJed Brown   PetscFunctionReturn(0);
1680c833c3b5SJed Brown }
1681c833c3b5SJed Brown 
1682c833c3b5SJed Brown #undef __FUNCT__
1683c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1684bb9467b5SJed Brown /*@C
1685c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1686c833c3b5SJed Brown 
1687c833c3b5SJed Brown    Logically Collective
1688c833c3b5SJed Brown 
1689c833c3b5SJed Brown    Input Arguments:
1690c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1691c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1692c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16930298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1694c833c3b5SJed Brown 
1695c833c3b5SJed Brown    Calling sequence of refinehook:
1696c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1697c833c3b5SJed Brown 
1698c833c3b5SJed Brown +  coarse - coarse level DM
1699c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1700c833c3b5SJed Brown -  ctx - optional user-defined function context
1701c833c3b5SJed Brown 
1702c833c3b5SJed Brown    Calling sequence for interphook:
1703c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1704c833c3b5SJed Brown 
1705c833c3b5SJed Brown +  coarse - coarse level DM
1706c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1707c833c3b5SJed Brown .  fine - fine level DM to update
1708c833c3b5SJed Brown -  ctx - optional user-defined function context
1709c833c3b5SJed Brown 
1710c833c3b5SJed Brown    Level: advanced
1711c833c3b5SJed Brown 
1712c833c3b5SJed Brown    Notes:
1713c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1714c833c3b5SJed Brown 
1715c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1716c833c3b5SJed Brown 
1717bb9467b5SJed Brown    This function is currently not available from Fortran.
1718bb9467b5SJed Brown 
1719c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1720c833c3b5SJed Brown @*/
1721c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1722c833c3b5SJed Brown {
1723c833c3b5SJed Brown   PetscErrorCode   ierr;
1724c833c3b5SJed Brown   DMRefineHookLink link,*p;
1725c833c3b5SJed Brown 
1726c833c3b5SJed Brown   PetscFunctionBegin;
1727c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1728c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1729c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1730c833c3b5SJed Brown   link->refinehook = refinehook;
1731c833c3b5SJed Brown   link->interphook = interphook;
1732c833c3b5SJed Brown   link->ctx        = ctx;
17330298fd71SBarry Smith   link->next       = NULL;
1734c833c3b5SJed Brown   *p               = link;
1735c833c3b5SJed Brown   PetscFunctionReturn(0);
1736c833c3b5SJed Brown }
1737c833c3b5SJed Brown 
1738c833c3b5SJed Brown #undef __FUNCT__
1739c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1740c833c3b5SJed Brown /*@
1741c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1742c833c3b5SJed Brown 
1743c833c3b5SJed Brown    Collective if any hooks are
1744c833c3b5SJed Brown 
1745c833c3b5SJed Brown    Input Arguments:
1746c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1747c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1748c833c3b5SJed Brown -  fine - finer DM to update
1749c833c3b5SJed Brown 
1750c833c3b5SJed Brown    Level: developer
1751c833c3b5SJed Brown 
1752c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1753c833c3b5SJed Brown @*/
1754c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1755c833c3b5SJed Brown {
1756c833c3b5SJed Brown   PetscErrorCode   ierr;
1757c833c3b5SJed Brown   DMRefineHookLink link;
1758c833c3b5SJed Brown 
1759c833c3b5SJed Brown   PetscFunctionBegin;
1760c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
17618865f1eaSKarl Rupp     if (link->interphook) {
17628865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
17638865f1eaSKarl Rupp     }
17644057135bSMatthew G Knepley   }
176547c6ae99SBarry Smith   PetscFunctionReturn(0);
176647c6ae99SBarry Smith }
176747c6ae99SBarry Smith 
176847c6ae99SBarry Smith #undef __FUNCT__
1769eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1770eb3f98d2SBarry Smith /*@
1771eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1772eb3f98d2SBarry Smith 
1773eb3f98d2SBarry Smith     Not Collective
1774eb3f98d2SBarry Smith 
1775eb3f98d2SBarry Smith     Input Parameter:
1776eb3f98d2SBarry Smith .   dm - the DM object
1777eb3f98d2SBarry Smith 
1778eb3f98d2SBarry Smith     Output Parameter:
1779eb3f98d2SBarry Smith .   level - number of refinements
1780eb3f98d2SBarry Smith 
1781eb3f98d2SBarry Smith     Level: developer
1782eb3f98d2SBarry Smith 
17836a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1784eb3f98d2SBarry Smith 
1785eb3f98d2SBarry Smith @*/
1786eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1787eb3f98d2SBarry Smith {
1788eb3f98d2SBarry Smith   PetscFunctionBegin;
1789eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1790eb3f98d2SBarry Smith   *level = dm->levelup;
1791eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1792eb3f98d2SBarry Smith }
1793eb3f98d2SBarry Smith 
1794eb3f98d2SBarry Smith #undef __FUNCT__
1795baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1796bb9467b5SJed Brown /*@C
1797baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1798baf369e7SPeter Brune 
1799baf369e7SPeter Brune    Logically Collective
1800baf369e7SPeter Brune 
1801baf369e7SPeter Brune    Input Arguments:
1802baf369e7SPeter Brune +  dm - the DM
1803baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1804baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
18050298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1806baf369e7SPeter Brune 
1807baf369e7SPeter Brune    Calling sequence for beginhook:
1808baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1809baf369e7SPeter Brune 
1810baf369e7SPeter Brune +  dm - global DM
1811baf369e7SPeter Brune .  g - global vector
1812baf369e7SPeter Brune .  mode - mode
1813baf369e7SPeter Brune .  l - local vector
1814baf369e7SPeter Brune -  ctx - optional user-defined function context
1815baf369e7SPeter Brune 
1816baf369e7SPeter Brune 
1817baf369e7SPeter Brune    Calling sequence for endhook:
1818ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1819baf369e7SPeter Brune 
1820baf369e7SPeter Brune +  global - global DM
1821baf369e7SPeter Brune -  ctx - optional user-defined function context
1822baf369e7SPeter Brune 
1823baf369e7SPeter Brune    Level: advanced
1824baf369e7SPeter Brune 
1825baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1826baf369e7SPeter Brune @*/
1827baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1828baf369e7SPeter Brune {
1829baf369e7SPeter Brune   PetscErrorCode          ierr;
1830baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1831baf369e7SPeter Brune 
1832baf369e7SPeter Brune   PetscFunctionBegin;
1833baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1834baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1835baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1836baf369e7SPeter Brune   link->beginhook = beginhook;
1837baf369e7SPeter Brune   link->endhook   = endhook;
1838baf369e7SPeter Brune   link->ctx       = ctx;
18390298fd71SBarry Smith   link->next      = NULL;
1840baf369e7SPeter Brune   *p              = link;
1841baf369e7SPeter Brune   PetscFunctionReturn(0);
1842baf369e7SPeter Brune }
1843baf369e7SPeter Brune 
1844baf369e7SPeter Brune #undef __FUNCT__
18454c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
18464c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
18474c274da1SToby Isaac {
18484c274da1SToby Isaac   Mat cMat;
18494c274da1SToby Isaac   Vec cVec;
18504c274da1SToby Isaac   PetscSection section, cSec;
18514c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18524c274da1SToby Isaac   PetscErrorCode ierr;
18534c274da1SToby Isaac 
18544c274da1SToby Isaac   PetscFunctionBegin;
18554c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18564c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18574c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
18584c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
18597711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
18604c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
18614c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
18624c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
18634c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
18644c274da1SToby Isaac       if (dof) {
18654c274da1SToby Isaac         PetscScalar *vals;
18664c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
18674c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
18684c274da1SToby Isaac       }
18694c274da1SToby Isaac     }
18704c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
18714c274da1SToby Isaac   }
18724c274da1SToby Isaac   PetscFunctionReturn(0);
18734c274da1SToby Isaac }
18744c274da1SToby Isaac 
18754c274da1SToby Isaac #undef __FUNCT__
187647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
187747c6ae99SBarry Smith /*@
187847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
187947c6ae99SBarry Smith 
188047c6ae99SBarry Smith     Neighbor-wise Collective on DM
188147c6ae99SBarry Smith 
188247c6ae99SBarry Smith     Input Parameters:
188347c6ae99SBarry Smith +   dm - the DM object
188447c6ae99SBarry Smith .   g - the global vector
188547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
188647c6ae99SBarry Smith -   l - the local vector
188747c6ae99SBarry Smith 
188847c6ae99SBarry Smith 
188947c6ae99SBarry Smith     Level: beginner
189047c6ae99SBarry Smith 
1891e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
189247c6ae99SBarry Smith 
189347c6ae99SBarry Smith @*/
18947087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
189547c6ae99SBarry Smith {
18967128ae9fSMatthew G Knepley   PetscSF                 sf;
189747c6ae99SBarry Smith   PetscErrorCode          ierr;
1898baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
189947c6ae99SBarry Smith 
190047c6ae99SBarry Smith   PetscFunctionBegin;
1901171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1902baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
19038865f1eaSKarl Rupp     if (link->beginhook) {
19048865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
19058865f1eaSKarl Rupp     }
1906baf369e7SPeter Brune   }
19077128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19087128ae9fSMatthew G Knepley   if (sf) {
1909ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1910ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
19117128ae9fSMatthew G Knepley 
191282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19137128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1914ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19157128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19167128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1917ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19187128ae9fSMatthew G Knepley   } else {
1919843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19207128ae9fSMatthew G Knepley   }
192147c6ae99SBarry Smith   PetscFunctionReturn(0);
192247c6ae99SBarry Smith }
192347c6ae99SBarry Smith 
192447c6ae99SBarry Smith #undef __FUNCT__
192547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
192647c6ae99SBarry Smith /*@
192747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
192847c6ae99SBarry Smith 
192947c6ae99SBarry Smith     Neighbor-wise Collective on DM
193047c6ae99SBarry Smith 
193147c6ae99SBarry Smith     Input Parameters:
193247c6ae99SBarry Smith +   dm - the DM object
193347c6ae99SBarry Smith .   g - the global vector
193447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
193547c6ae99SBarry Smith -   l - the local vector
193647c6ae99SBarry Smith 
193747c6ae99SBarry Smith 
193847c6ae99SBarry Smith     Level: beginner
193947c6ae99SBarry Smith 
1940e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
194147c6ae99SBarry Smith 
194247c6ae99SBarry Smith @*/
19437087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
194447c6ae99SBarry Smith {
19457128ae9fSMatthew G Knepley   PetscSF                 sf;
194647c6ae99SBarry Smith   PetscErrorCode          ierr;
1947ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1948ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1949baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
195047c6ae99SBarry Smith 
195147c6ae99SBarry Smith   PetscFunctionBegin;
1952171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19537128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19547128ae9fSMatthew G Knepley   if (sf) {
195582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19567128ae9fSMatthew G Knepley 
19577128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1958ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19597128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19607128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1961ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19627128ae9fSMatthew G Knepley   } else {
1963843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19647128ae9fSMatthew G Knepley   }
19654c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1966baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1967baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1968baf369e7SPeter Brune   }
196947c6ae99SBarry Smith   PetscFunctionReturn(0);
197047c6ae99SBarry Smith }
197147c6ae99SBarry Smith 
197247c6ae99SBarry Smith #undef __FUNCT__
1973d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1974d4d07f1eSToby Isaac /*@C
1975d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1976d4d07f1eSToby Isaac 
1977d4d07f1eSToby Isaac    Logically Collective
1978d4d07f1eSToby Isaac 
1979d4d07f1eSToby Isaac    Input Arguments:
1980d4d07f1eSToby Isaac +  dm - the DM
1981d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1982d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1983d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1984d4d07f1eSToby Isaac 
1985d4d07f1eSToby Isaac    Calling sequence for beginhook:
1986d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1987d4d07f1eSToby Isaac 
1988d4d07f1eSToby Isaac +  dm - global DM
1989d4d07f1eSToby Isaac .  l - local vector
1990d4d07f1eSToby Isaac .  mode - mode
1991d4d07f1eSToby Isaac .  g - global vector
1992d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1993d4d07f1eSToby Isaac 
1994d4d07f1eSToby Isaac 
1995d4d07f1eSToby Isaac    Calling sequence for endhook:
1996d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1997d4d07f1eSToby Isaac 
1998d4d07f1eSToby Isaac +  global - global DM
1999d4d07f1eSToby Isaac .  l - local vector
2000d4d07f1eSToby Isaac .  mode - mode
2001d4d07f1eSToby Isaac .  g - global vector
2002d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2003d4d07f1eSToby Isaac 
2004d4d07f1eSToby Isaac    Level: advanced
2005d4d07f1eSToby Isaac 
2006d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2007d4d07f1eSToby Isaac @*/
2008d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2009d4d07f1eSToby Isaac {
2010d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2011d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2012d4d07f1eSToby Isaac 
2013d4d07f1eSToby Isaac   PetscFunctionBegin;
2014d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2015d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2016d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
2017d4d07f1eSToby Isaac   link->beginhook = beginhook;
2018d4d07f1eSToby Isaac   link->endhook   = endhook;
2019d4d07f1eSToby Isaac   link->ctx       = ctx;
2020d4d07f1eSToby Isaac   link->next      = NULL;
2021d4d07f1eSToby Isaac   *p              = link;
2022d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2023d4d07f1eSToby Isaac }
2024d4d07f1eSToby Isaac 
2025d4d07f1eSToby Isaac #undef __FUNCT__
20264c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
20274c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
20284c274da1SToby Isaac {
20294c274da1SToby Isaac   Mat cMat;
20304c274da1SToby Isaac   Vec cVec;
20314c274da1SToby Isaac   PetscSection section, cSec;
20324c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20334c274da1SToby Isaac   PetscErrorCode ierr;
20344c274da1SToby Isaac 
20354c274da1SToby Isaac   PetscFunctionBegin;
20364c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20374c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20384c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
20394c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
20407711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20414c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20424c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20434c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20444c274da1SToby Isaac       if (dof) {
20454c274da1SToby Isaac         PetscInt d;
20464c274da1SToby Isaac         PetscScalar *vals;
20474c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
20484c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
20494c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
20504c274da1SToby Isaac          * we just extracted */
20514c274da1SToby Isaac         for (d = 0; d < dof; d++) {
20524c274da1SToby Isaac           vals[d] = 0.;
20534c274da1SToby Isaac         }
20544c274da1SToby Isaac       }
20554c274da1SToby Isaac     }
20564c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
20574c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
20584c274da1SToby Isaac   }
20594c274da1SToby Isaac   PetscFunctionReturn(0);
20604c274da1SToby Isaac }
20614c274da1SToby Isaac 
20624c274da1SToby Isaac #undef __FUNCT__
20639a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
206447c6ae99SBarry Smith /*@
20659a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
20669a42bb27SBarry Smith 
20679a42bb27SBarry Smith     Neighbor-wise Collective on DM
20689a42bb27SBarry Smith 
20699a42bb27SBarry Smith     Input Parameters:
20709a42bb27SBarry Smith +   dm - the DM object
2071f6813fd5SJed Brown .   l - the local vector
20721eb28f2eSBarry 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.
20731eb28f2eSBarry Smith -   g - the global vector
20749a42bb27SBarry Smith 
2075d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
207684330215SMatthew 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.
20779a42bb27SBarry Smith 
20789a42bb27SBarry Smith     Level: beginner
20799a42bb27SBarry Smith 
2080e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
20819a42bb27SBarry Smith 
20829a42bb27SBarry Smith @*/
20837087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
20849a42bb27SBarry Smith {
20857128ae9fSMatthew G Knepley   PetscSF                 sf;
208684330215SMatthew G. Knepley   PetscSection            s, gs;
2087d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2088ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2089ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
209084330215SMatthew G. Knepley   PetscBool               isInsert;
209184330215SMatthew G. Knepley   PetscErrorCode          ierr;
20929a42bb27SBarry Smith 
20939a42bb27SBarry Smith   PetscFunctionBegin;
2094171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2095d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2096d4d07f1eSToby Isaac     if (link->beginhook) {
2097d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2098d4d07f1eSToby Isaac     }
2099d4d07f1eSToby Isaac   }
21004c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
21017128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
210284330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21037128ae9fSMatthew G Knepley   switch (mode) {
21047128ae9fSMatthew G Knepley   case INSERT_VALUES:
21057128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
210684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21077128ae9fSMatthew G Knepley   case ADD_VALUES:
21087128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
210984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21107128ae9fSMatthew G Knepley   default:
211182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21127128ae9fSMatthew G Knepley   }
211384330215SMatthew G. Knepley   if (sf && !isInsert) {
2114ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21157128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2116a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2117ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
211884330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
211984330215SMatthew G. Knepley   } else if (s && isInsert) {
212084330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
212184330215SMatthew G. Knepley 
212284330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
212384330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
212484330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2125ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
212684330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
212784330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2128b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
212984330215SMatthew G. Knepley 
213084330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
213103442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
213284330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2133b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
213484330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
213584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2136b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
213703442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2138b3b16f48SMatthew 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);
2139b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2140b3b16f48SMatthew G. Knepley       if (!gcdof) {
214184330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2142b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2143b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
214484330215SMatthew G. Knepley         const PetscInt *cdofs;
214584330215SMatthew G. Knepley         PetscInt        cind = 0;
214684330215SMatthew G. Knepley 
214784330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2148b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
214984330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2150b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
215184330215SMatthew G. Knepley         }
2152b3b16f48SMatthew 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);
215384330215SMatthew G. Knepley     }
2154ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21557128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
21567128ae9fSMatthew G Knepley   } else {
2157843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21587128ae9fSMatthew G Knepley   }
21599a42bb27SBarry Smith   PetscFunctionReturn(0);
21609a42bb27SBarry Smith }
21619a42bb27SBarry Smith 
21629a42bb27SBarry Smith #undef __FUNCT__
21639a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
21649a42bb27SBarry Smith /*@
21659a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
216647c6ae99SBarry Smith 
216747c6ae99SBarry Smith     Neighbor-wise Collective on DM
216847c6ae99SBarry Smith 
216947c6ae99SBarry Smith     Input Parameters:
217047c6ae99SBarry Smith +   dm - the DM object
2171f6813fd5SJed Brown .   l - the local vector
217247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2173f6813fd5SJed Brown -   g - the global vector
217447c6ae99SBarry Smith 
217547c6ae99SBarry Smith 
217647c6ae99SBarry Smith     Level: beginner
217747c6ae99SBarry Smith 
2178e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
217947c6ae99SBarry Smith 
218047c6ae99SBarry Smith @*/
21817087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
218247c6ae99SBarry Smith {
21837128ae9fSMatthew G Knepley   PetscSF                 sf;
218484330215SMatthew G. Knepley   PetscSection            s;
2185d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
218684330215SMatthew G. Knepley   PetscBool               isInsert;
218784330215SMatthew G. Knepley   PetscErrorCode          ierr;
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith   PetscFunctionBegin;
2190171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21917128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
219284330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21937128ae9fSMatthew G Knepley   switch (mode) {
21947128ae9fSMatthew G Knepley   case INSERT_VALUES:
21957128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
219684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21977128ae9fSMatthew G Knepley   case ADD_VALUES:
21987128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
219984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22007128ae9fSMatthew G Knepley   default:
220182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22027128ae9fSMatthew G Knepley   }
220384330215SMatthew G. Knepley   if (sf && !isInsert) {
2204ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2205ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
220684330215SMatthew G. Knepley 
2207ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22087128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2209a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2210ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22117128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
221284330215SMatthew G. Knepley   } else if (s && isInsert) {
22137128ae9fSMatthew G Knepley   } else {
2214843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22157128ae9fSMatthew G Knepley   }
2216d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2217d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2218d4d07f1eSToby Isaac   }
221947c6ae99SBarry Smith   PetscFunctionReturn(0);
222047c6ae99SBarry Smith }
222147c6ae99SBarry Smith 
222247c6ae99SBarry Smith #undef __FUNCT__
2223f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2224f089877aSRichard Tran Mills /*@
2225bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2226bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2227d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2228f089877aSRichard Tran Mills 
2229bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2230f089877aSRichard Tran Mills 
2231f089877aSRichard Tran Mills    Input Parameters:
2232f089877aSRichard Tran Mills +  dm - the DM object
2233bc0a1609SRichard Tran Mills .  g - the original local vector
2234bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2235f089877aSRichard Tran Mills 
2236bc0a1609SRichard Tran Mills    Output Parameter:
2237bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2238f089877aSRichard Tran Mills 
2239f089877aSRichard Tran Mills    Level: intermediate
2240f089877aSRichard Tran Mills 
2241bc0a1609SRichard Tran Mills    Notes:
2242bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2243bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2244bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2245bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2246bc0a1609SRichard Tran Mills 
2247bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2248bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2249f089877aSRichard Tran Mills 
2250f089877aSRichard Tran Mills @*/
2251f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2252f089877aSRichard Tran Mills {
2253f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2254f089877aSRichard Tran Mills 
2255f089877aSRichard Tran Mills   PetscFunctionBegin;
2256f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2257f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2258f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2259f089877aSRichard Tran Mills }
2260f089877aSRichard Tran Mills 
2261f089877aSRichard Tran Mills #undef __FUNCT__
2262f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2263f089877aSRichard Tran Mills /*@
2264bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2265bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2266d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2267f089877aSRichard Tran Mills 
2268bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2269f089877aSRichard Tran Mills 
2270f089877aSRichard Tran Mills    Input Parameters:
2271bc0a1609SRichard Tran Mills +  da - the DM object
2272bc0a1609SRichard Tran Mills .  g - the original local vector
2273bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2274f089877aSRichard Tran Mills 
2275bc0a1609SRichard Tran Mills    Output Parameter:
2276bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2277f089877aSRichard Tran Mills 
2278f089877aSRichard Tran Mills    Level: intermediate
2279f089877aSRichard Tran Mills 
2280bc0a1609SRichard Tran Mills    Notes:
2281bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2282bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2283bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2284bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2285bc0a1609SRichard Tran Mills 
2286bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2287bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2288f089877aSRichard Tran Mills 
2289f089877aSRichard Tran Mills @*/
2290f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2291f089877aSRichard Tran Mills {
2292f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2293f089877aSRichard Tran Mills 
2294f089877aSRichard Tran Mills   PetscFunctionBegin;
2295f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2296f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2297f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2298f089877aSRichard Tran Mills }
2299f089877aSRichard Tran Mills 
2300f089877aSRichard Tran Mills 
2301f089877aSRichard Tran Mills #undef __FUNCT__
230247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
230347c6ae99SBarry Smith /*@
230447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
230547c6ae99SBarry Smith 
230647c6ae99SBarry Smith     Collective on DM
230747c6ae99SBarry Smith 
230847c6ae99SBarry Smith     Input Parameter:
230947c6ae99SBarry Smith +   dm - the DM object
231091d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
231147c6ae99SBarry Smith 
231247c6ae99SBarry Smith     Output Parameter:
231347c6ae99SBarry Smith .   dmc - the coarsened DM
231447c6ae99SBarry Smith 
231547c6ae99SBarry Smith     Level: developer
231647c6ae99SBarry Smith 
2317e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
231847c6ae99SBarry Smith 
231947c6ae99SBarry Smith @*/
23207087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
232147c6ae99SBarry Smith {
232247c6ae99SBarry Smith   PetscErrorCode    ierr;
2323b17ce1afSJed Brown   DMCoarsenHookLink link;
232447c6ae99SBarry Smith 
232547c6ae99SBarry Smith   PetscFunctionBegin;
2326171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
232747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
232847c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
23298892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2330a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
233143842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
23328cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2333644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
23340598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2335656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2336e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2337b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2338b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2339b17ce1afSJed Brown   }
234047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2341b17ce1afSJed Brown   PetscFunctionReturn(0);
2342b17ce1afSJed Brown }
2343b17ce1afSJed Brown 
2344b17ce1afSJed Brown #undef __FUNCT__
2345b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2346bb9467b5SJed Brown /*@C
2347b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2348b17ce1afSJed Brown 
2349b17ce1afSJed Brown    Logically Collective
2350b17ce1afSJed Brown 
2351b17ce1afSJed Brown    Input Arguments:
2352b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2353b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2354b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
23550298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2356b17ce1afSJed Brown 
2357b17ce1afSJed Brown    Calling sequence of coarsenhook:
2358b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2359b17ce1afSJed Brown 
2360b17ce1afSJed Brown +  fine - fine level DM
2361b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2362b17ce1afSJed Brown -  ctx - optional user-defined function context
2363b17ce1afSJed Brown 
2364b17ce1afSJed Brown    Calling sequence for restricthook:
2365c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2366b17ce1afSJed Brown 
2367b17ce1afSJed Brown +  fine - fine level DM
2368b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2369c833c3b5SJed Brown .  rscale - scaling vector for restriction
2370c833c3b5SJed Brown .  inject - matrix restricting by injection
2371b17ce1afSJed Brown .  coarse - coarse level DM to update
2372b17ce1afSJed Brown -  ctx - optional user-defined function context
2373b17ce1afSJed Brown 
2374b17ce1afSJed Brown    Level: advanced
2375b17ce1afSJed Brown 
2376b17ce1afSJed Brown    Notes:
2377b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2378b17ce1afSJed Brown 
2379b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2380b17ce1afSJed Brown 
2381b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2382b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2383b17ce1afSJed Brown 
2384bb9467b5SJed Brown    This function is currently not available from Fortran.
2385bb9467b5SJed Brown 
2386c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2387b17ce1afSJed Brown @*/
2388b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2389b17ce1afSJed Brown {
2390b17ce1afSJed Brown   PetscErrorCode    ierr;
2391b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2392b17ce1afSJed Brown 
2393b17ce1afSJed Brown   PetscFunctionBegin;
2394b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
23956bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2396b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2397b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2398b17ce1afSJed Brown   link->restricthook = restricthook;
2399b17ce1afSJed Brown   link->ctx          = ctx;
24000298fd71SBarry Smith   link->next         = NULL;
2401b17ce1afSJed Brown   *p                 = link;
2402b17ce1afSJed Brown   PetscFunctionReturn(0);
2403b17ce1afSJed Brown }
2404b17ce1afSJed Brown 
2405b17ce1afSJed Brown #undef __FUNCT__
2406b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2407b17ce1afSJed Brown /*@
2408b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2409b17ce1afSJed Brown 
2410b17ce1afSJed Brown    Collective if any hooks are
2411b17ce1afSJed Brown 
2412b17ce1afSJed Brown    Input Arguments:
2413b17ce1afSJed Brown +  fine - finer DM to use as a base
2414b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2415b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2416b17ce1afSJed Brown -  coarse - coarer DM to update
2417b17ce1afSJed Brown 
2418b17ce1afSJed Brown    Level: developer
2419b17ce1afSJed Brown 
2420b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2421b17ce1afSJed Brown @*/
2422b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2423b17ce1afSJed Brown {
2424b17ce1afSJed Brown   PetscErrorCode    ierr;
2425b17ce1afSJed Brown   DMCoarsenHookLink link;
2426b17ce1afSJed Brown 
2427b17ce1afSJed Brown   PetscFunctionBegin;
2428b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
24298865f1eaSKarl Rupp     if (link->restricthook) {
24308865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
24318865f1eaSKarl Rupp     }
2432b17ce1afSJed Brown   }
243347c6ae99SBarry Smith   PetscFunctionReturn(0);
243447c6ae99SBarry Smith }
243547c6ae99SBarry Smith 
243647c6ae99SBarry Smith #undef __FUNCT__
2437be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2438bb9467b5SJed Brown /*@C
2439be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
24405dbd56e3SPeter Brune 
24415dbd56e3SPeter Brune    Logically Collective
24425dbd56e3SPeter Brune 
24435dbd56e3SPeter Brune    Input Arguments:
24445dbd56e3SPeter Brune +  global - global DM
2445ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
24465dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
24470298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
24485dbd56e3SPeter Brune 
2449ec4806b8SPeter Brune 
2450ec4806b8SPeter Brune    Calling sequence for ddhook:
2451ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2452ec4806b8SPeter Brune 
2453ec4806b8SPeter Brune +  global - global DM
2454ec4806b8SPeter Brune .  block  - block DM
2455ec4806b8SPeter Brune -  ctx - optional user-defined function context
2456ec4806b8SPeter Brune 
24575dbd56e3SPeter Brune    Calling sequence for restricthook:
2458ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
24595dbd56e3SPeter Brune 
24605dbd56e3SPeter Brune +  global - global DM
24615dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
24625dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2463ec4806b8SPeter Brune .  block  - block DM
24645dbd56e3SPeter Brune -  ctx - optional user-defined function context
24655dbd56e3SPeter Brune 
24665dbd56e3SPeter Brune    Level: advanced
24675dbd56e3SPeter Brune 
24685dbd56e3SPeter Brune    Notes:
2469ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
24705dbd56e3SPeter Brune 
24715dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
24725dbd56e3SPeter Brune 
24735dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2474ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
24755dbd56e3SPeter Brune 
2476bb9467b5SJed Brown    This function is currently not available from Fortran.
2477bb9467b5SJed Brown 
24785dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
24795dbd56e3SPeter Brune @*/
2480be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
24815dbd56e3SPeter Brune {
24825dbd56e3SPeter Brune   PetscErrorCode      ierr;
2483be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
24845dbd56e3SPeter Brune 
24855dbd56e3SPeter Brune   PetscFunctionBegin;
24865dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2487be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2488be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
24895dbd56e3SPeter Brune   link->restricthook = restricthook;
2490be081cd6SPeter Brune   link->ddhook       = ddhook;
24915dbd56e3SPeter Brune   link->ctx          = ctx;
24920298fd71SBarry Smith   link->next         = NULL;
24935dbd56e3SPeter Brune   *p                 = link;
24945dbd56e3SPeter Brune   PetscFunctionReturn(0);
24955dbd56e3SPeter Brune }
24965dbd56e3SPeter Brune 
24975dbd56e3SPeter Brune #undef __FUNCT__
2498be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
24995dbd56e3SPeter Brune /*@
2500be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
25015dbd56e3SPeter Brune 
25025dbd56e3SPeter Brune    Collective if any hooks are
25035dbd56e3SPeter Brune 
25045dbd56e3SPeter Brune    Input Arguments:
25055dbd56e3SPeter Brune +  fine - finer DM to use as a base
2506be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2507be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
25085dbd56e3SPeter Brune -  coarse - coarer DM to update
25095dbd56e3SPeter Brune 
25105dbd56e3SPeter Brune    Level: developer
25115dbd56e3SPeter Brune 
25125dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
25135dbd56e3SPeter Brune @*/
2514be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
25155dbd56e3SPeter Brune {
25165dbd56e3SPeter Brune   PetscErrorCode      ierr;
2517be081cd6SPeter Brune   DMSubDomainHookLink link;
25185dbd56e3SPeter Brune 
25195dbd56e3SPeter Brune   PetscFunctionBegin;
2520be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
25218865f1eaSKarl Rupp     if (link->restricthook) {
25228865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
25238865f1eaSKarl Rupp     }
25245dbd56e3SPeter Brune   }
25255dbd56e3SPeter Brune   PetscFunctionReturn(0);
25265dbd56e3SPeter Brune }
25275dbd56e3SPeter Brune 
25285dbd56e3SPeter Brune #undef __FUNCT__
25295fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
25305fe1f584SPeter Brune /*@
25316a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
25325fe1f584SPeter Brune 
25335fe1f584SPeter Brune     Not Collective
25345fe1f584SPeter Brune 
25355fe1f584SPeter Brune     Input Parameter:
25365fe1f584SPeter Brune .   dm - the DM object
25375fe1f584SPeter Brune 
25385fe1f584SPeter Brune     Output Parameter:
25396a7d9d85SPeter Brune .   level - number of coarsenings
25405fe1f584SPeter Brune 
25415fe1f584SPeter Brune     Level: developer
25425fe1f584SPeter Brune 
25436a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
25445fe1f584SPeter Brune 
25455fe1f584SPeter Brune @*/
25465fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
25475fe1f584SPeter Brune {
25485fe1f584SPeter Brune   PetscFunctionBegin;
25495fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25505fe1f584SPeter Brune   *level = dm->leveldown;
25515fe1f584SPeter Brune   PetscFunctionReturn(0);
25525fe1f584SPeter Brune }
25535fe1f584SPeter Brune 
25545fe1f584SPeter Brune 
25555fe1f584SPeter Brune 
25565fe1f584SPeter Brune #undef __FUNCT__
255747c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
255847c6ae99SBarry Smith /*@C
255947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
256047c6ae99SBarry Smith 
256147c6ae99SBarry Smith     Collective on DM
256247c6ae99SBarry Smith 
256347c6ae99SBarry Smith     Input Parameter:
256447c6ae99SBarry Smith +   dm - the DM object
256547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
256647c6ae99SBarry Smith 
256747c6ae99SBarry Smith     Output Parameter:
256847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
256947c6ae99SBarry Smith 
257047c6ae99SBarry Smith     Level: developer
257147c6ae99SBarry Smith 
2572e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
257347c6ae99SBarry Smith 
257447c6ae99SBarry Smith @*/
25757087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
257647c6ae99SBarry Smith {
257747c6ae99SBarry Smith   PetscErrorCode ierr;
257847c6ae99SBarry Smith 
257947c6ae99SBarry Smith   PetscFunctionBegin;
2580171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2581ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
258247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
258347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
258447c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
258547c6ae99SBarry Smith   } else if (dm->ops->refine) {
258647c6ae99SBarry Smith     PetscInt i;
258747c6ae99SBarry Smith 
2588ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
258947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2590ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
259147c6ae99SBarry Smith     }
2592ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
259347c6ae99SBarry Smith   PetscFunctionReturn(0);
259447c6ae99SBarry Smith }
259547c6ae99SBarry Smith 
259647c6ae99SBarry Smith #undef __FUNCT__
259747c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
259847c6ae99SBarry Smith /*@C
259947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
260047c6ae99SBarry Smith 
260147c6ae99SBarry Smith     Collective on DM
260247c6ae99SBarry Smith 
260347c6ae99SBarry Smith     Input Parameter:
260447c6ae99SBarry Smith +   dm - the DM object
260547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
260647c6ae99SBarry Smith 
260747c6ae99SBarry Smith     Output Parameter:
260847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
260947c6ae99SBarry Smith 
261047c6ae99SBarry Smith     Level: developer
261147c6ae99SBarry Smith 
2612e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
261347c6ae99SBarry Smith 
261447c6ae99SBarry Smith @*/
26157087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
261647c6ae99SBarry Smith {
261747c6ae99SBarry Smith   PetscErrorCode ierr;
261847c6ae99SBarry Smith 
261947c6ae99SBarry Smith   PetscFunctionBegin;
2620171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2621ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
262247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
262347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
262447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
262547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
262647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
262747c6ae99SBarry Smith     PetscInt i;
262847c6ae99SBarry Smith 
2629ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
263047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2631ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
263247c6ae99SBarry Smith     }
2633ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
263447c6ae99SBarry Smith   PetscFunctionReturn(0);
263547c6ae99SBarry Smith }
263647c6ae99SBarry Smith 
263747c6ae99SBarry Smith #undef __FUNCT__
2638e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
263947c6ae99SBarry Smith /*@
2640e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
264147c6ae99SBarry Smith    grids associated with two DMs.
264247c6ae99SBarry Smith 
264347c6ae99SBarry Smith    Collective on DM
264447c6ae99SBarry Smith 
264547c6ae99SBarry Smith    Input Parameters:
264647c6ae99SBarry Smith +  dmc - the coarse grid DM
264747c6ae99SBarry Smith -  dmf - the fine grid DM
264847c6ae99SBarry Smith 
264947c6ae99SBarry Smith    Output Parameters:
265047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
265147c6ae99SBarry Smith 
265247c6ae99SBarry Smith    Level: intermediate
265347c6ae99SBarry Smith 
265447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
265547c6ae99SBarry Smith 
2656e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
265747c6ae99SBarry Smith @*/
2658e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
265947c6ae99SBarry Smith {
266047c6ae99SBarry Smith   PetscErrorCode ierr;
266147c6ae99SBarry Smith 
266247c6ae99SBarry Smith   PetscFunctionBegin;
2663171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2664171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
266547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
266647c6ae99SBarry Smith   PetscFunctionReturn(0);
266747c6ae99SBarry Smith }
266847c6ae99SBarry Smith 
266947c6ae99SBarry Smith #undef __FUNCT__
26701a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
26711a266240SBarry Smith /*@C
26721a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
26731a266240SBarry Smith 
26741a266240SBarry Smith     Not Collective
26751a266240SBarry Smith 
26761a266240SBarry Smith     Input Parameters:
26771a266240SBarry Smith +   dm - the DM object
26781a266240SBarry Smith -   destroy - the destroy function
26791a266240SBarry Smith 
26801a266240SBarry Smith     Level: intermediate
26811a266240SBarry Smith 
2682e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
26831a266240SBarry Smith 
2684f07f9ceaSJed Brown @*/
26851a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
26861a266240SBarry Smith {
26871a266240SBarry Smith   PetscFunctionBegin;
2688171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26891a266240SBarry Smith   dm->ctxdestroy = destroy;
26901a266240SBarry Smith   PetscFunctionReturn(0);
26911a266240SBarry Smith }
26921a266240SBarry Smith 
26931a266240SBarry Smith #undef __FUNCT__
26941b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2695b07ff414SBarry Smith /*@
26961b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
269747c6ae99SBarry Smith 
269847c6ae99SBarry Smith     Not Collective
269947c6ae99SBarry Smith 
270047c6ae99SBarry Smith     Input Parameters:
270147c6ae99SBarry Smith +   dm - the DM object
270247c6ae99SBarry Smith -   ctx - the user context
270347c6ae99SBarry Smith 
270447c6ae99SBarry Smith     Level: intermediate
270547c6ae99SBarry Smith 
2706e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
270747c6ae99SBarry Smith 
270847c6ae99SBarry Smith @*/
27091b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
271047c6ae99SBarry Smith {
271147c6ae99SBarry Smith   PetscFunctionBegin;
2712171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
271347c6ae99SBarry Smith   dm->ctx = ctx;
271447c6ae99SBarry Smith   PetscFunctionReturn(0);
271547c6ae99SBarry Smith }
271647c6ae99SBarry Smith 
271747c6ae99SBarry Smith #undef __FUNCT__
27181b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
271947c6ae99SBarry Smith /*@
27201b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
272147c6ae99SBarry Smith 
272247c6ae99SBarry Smith     Not Collective
272347c6ae99SBarry Smith 
272447c6ae99SBarry Smith     Input Parameter:
272547c6ae99SBarry Smith .   dm - the DM object
272647c6ae99SBarry Smith 
272747c6ae99SBarry Smith     Output Parameter:
272847c6ae99SBarry Smith .   ctx - the user context
272947c6ae99SBarry Smith 
273047c6ae99SBarry Smith     Level: intermediate
273147c6ae99SBarry Smith 
2732e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
273347c6ae99SBarry Smith 
273447c6ae99SBarry Smith @*/
27351b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
273647c6ae99SBarry Smith {
273747c6ae99SBarry Smith   PetscFunctionBegin;
2738171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27391b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
274047c6ae99SBarry Smith   PetscFunctionReturn(0);
274147c6ae99SBarry Smith }
274247c6ae99SBarry Smith 
274347c6ae99SBarry Smith #undef __FUNCT__
274408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
274508da532bSDmitry Karpeev /*@C
2746df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
274708da532bSDmitry Karpeev 
274808da532bSDmitry Karpeev     Logically Collective on DM
274908da532bSDmitry Karpeev 
275008da532bSDmitry Karpeev     Input Parameter:
275108da532bSDmitry Karpeev +   dm - the DM object
27520298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
275308da532bSDmitry Karpeev 
275408da532bSDmitry Karpeev     Level: intermediate
275508da532bSDmitry Karpeev 
2756835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
275708da532bSDmitry Karpeev          DMSetJacobian()
275808da532bSDmitry Karpeev 
275908da532bSDmitry Karpeev @*/
276008da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
276108da532bSDmitry Karpeev {
276208da532bSDmitry Karpeev   PetscFunctionBegin;
276308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
276408da532bSDmitry Karpeev   PetscFunctionReturn(0);
276508da532bSDmitry Karpeev }
276608da532bSDmitry Karpeev 
276708da532bSDmitry Karpeev #undef __FUNCT__
276808da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
276908da532bSDmitry Karpeev /*@
277008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
277108da532bSDmitry Karpeev 
277208da532bSDmitry Karpeev     Not Collective
277308da532bSDmitry Karpeev 
277408da532bSDmitry Karpeev     Input Parameter:
277508da532bSDmitry Karpeev .   dm - the DM object to destroy
277608da532bSDmitry Karpeev 
277708da532bSDmitry Karpeev     Output Parameter:
277808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
277908da532bSDmitry Karpeev 
278008da532bSDmitry Karpeev     Level: developer
278108da532bSDmitry Karpeev 
278274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
278308da532bSDmitry Karpeev 
278408da532bSDmitry Karpeev @*/
278508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
278608da532bSDmitry Karpeev {
278708da532bSDmitry Karpeev   PetscFunctionBegin;
278808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
278908da532bSDmitry Karpeev   PetscFunctionReturn(0);
279008da532bSDmitry Karpeev }
279108da532bSDmitry Karpeev 
279208da532bSDmitry Karpeev #undef __FUNCT__
279308da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
279408da532bSDmitry Karpeev /*@C
279508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
279608da532bSDmitry Karpeev 
279708da532bSDmitry Karpeev     Logically Collective on DM
279808da532bSDmitry Karpeev 
279908da532bSDmitry Karpeev     Input Parameters:
2800907376e6SBarry Smith .   dm - the DM object
280108da532bSDmitry Karpeev 
280208da532bSDmitry Karpeev     Output parameters:
280308da532bSDmitry Karpeev +   xl - lower bound
280408da532bSDmitry Karpeev -   xu - upper bound
280508da532bSDmitry Karpeev 
2806907376e6SBarry Smith     Level: advanced
2807907376e6SBarry Smith 
2808907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
280908da532bSDmitry Karpeev 
281074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
281108da532bSDmitry Karpeev 
281208da532bSDmitry Karpeev @*/
281308da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
281408da532bSDmitry Karpeev {
281508da532bSDmitry Karpeev   PetscErrorCode ierr;
28165fd66863SKarl Rupp 
281708da532bSDmitry Karpeev   PetscFunctionBegin;
281808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
281908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
282008da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
282108da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
28228865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
282308da532bSDmitry Karpeev   PetscFunctionReturn(0);
282408da532bSDmitry Karpeev }
282508da532bSDmitry Karpeev 
282608da532bSDmitry Karpeev #undef __FUNCT__
2827b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2828b0ae01b7SPeter Brune /*@
2829b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2830b0ae01b7SPeter Brune 
2831b0ae01b7SPeter Brune     Not Collective
2832b0ae01b7SPeter Brune 
2833b0ae01b7SPeter Brune     Input Parameter:
2834b0ae01b7SPeter Brune .   dm - the DM object
2835b0ae01b7SPeter Brune 
2836b0ae01b7SPeter Brune     Output Parameter:
2837b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2838b0ae01b7SPeter Brune 
2839b0ae01b7SPeter Brune     Level: developer
2840b0ae01b7SPeter Brune 
2841b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2842b0ae01b7SPeter Brune 
2843b0ae01b7SPeter Brune @*/
2844b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2845b0ae01b7SPeter Brune {
2846b0ae01b7SPeter Brune   PetscFunctionBegin;
2847b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2848b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2849b0ae01b7SPeter Brune }
2850b0ae01b7SPeter Brune 
2851b0ae01b7SPeter Brune #undef __FUNCT__
2852*3ad4599aSBarry Smith #define __FUNCT__ "DMHasCreateRestriction"
2853*3ad4599aSBarry Smith /*@
2854*3ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
2855*3ad4599aSBarry Smith 
2856*3ad4599aSBarry Smith     Not Collective
2857*3ad4599aSBarry Smith 
2858*3ad4599aSBarry Smith     Input Parameter:
2859*3ad4599aSBarry Smith .   dm - the DM object
2860*3ad4599aSBarry Smith 
2861*3ad4599aSBarry Smith     Output Parameter:
2862*3ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
2863*3ad4599aSBarry Smith 
2864*3ad4599aSBarry Smith     Level: developer
2865*3ad4599aSBarry Smith 
2866*3ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
2867*3ad4599aSBarry Smith 
2868*3ad4599aSBarry Smith @*/
2869*3ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
2870*3ad4599aSBarry Smith {
2871*3ad4599aSBarry Smith   PetscFunctionBegin;
2872*3ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
2873*3ad4599aSBarry Smith   PetscFunctionReturn(0);
2874*3ad4599aSBarry Smith }
2875*3ad4599aSBarry Smith 
2876*3ad4599aSBarry Smith #undef  __FUNCT__
287708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2878748fac09SDmitry Karpeev /*@C
287908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
288008da532bSDmitry Karpeev 
288108da532bSDmitry Karpeev     Collective on DM
288208da532bSDmitry Karpeev 
288308da532bSDmitry Karpeev     Input Parameter:
288408da532bSDmitry Karpeev +   dm - the DM object
28850298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
288608da532bSDmitry Karpeev 
288708da532bSDmitry Karpeev     Level: developer
288808da532bSDmitry Karpeev 
288974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
289008da532bSDmitry Karpeev 
289108da532bSDmitry Karpeev @*/
289208da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
289308da532bSDmitry Karpeev {
289408da532bSDmitry Karpeev   PetscErrorCode ierr;
28955fd66863SKarl Rupp 
289608da532bSDmitry Karpeev   PetscFunctionBegin;
289708da532bSDmitry Karpeev   if (x) {
289808da532bSDmitry Karpeev     if (!dm->x) {
289908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
290008da532bSDmitry Karpeev     }
290108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
29028865f1eaSKarl Rupp   } else if (dm->x) {
290308da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
290408da532bSDmitry Karpeev   }
290508da532bSDmitry Karpeev   PetscFunctionReturn(0);
290608da532bSDmitry Karpeev }
290708da532bSDmitry Karpeev 
29080298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2909264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2910264ace61SBarry Smith 
2911264ace61SBarry Smith #undef __FUNCT__
2912264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2913264ace61SBarry Smith /*@C
2914264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2915264ace61SBarry Smith 
2916264ace61SBarry Smith   Collective on DM
2917264ace61SBarry Smith 
2918264ace61SBarry Smith   Input Parameters:
2919264ace61SBarry Smith + dm     - The DM object
2920264ace61SBarry Smith - method - The name of the DM type
2921264ace61SBarry Smith 
2922264ace61SBarry Smith   Options Database Key:
2923264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2924264ace61SBarry Smith 
2925264ace61SBarry Smith   Notes:
2926e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2927264ace61SBarry Smith 
2928264ace61SBarry Smith   Level: intermediate
2929264ace61SBarry Smith 
2930264ace61SBarry Smith .keywords: DM, set, type
2931264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2932264ace61SBarry Smith @*/
293319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2934264ace61SBarry Smith {
2935264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2936264ace61SBarry Smith   PetscBool      match;
2937264ace61SBarry Smith   PetscErrorCode ierr;
2938264ace61SBarry Smith 
2939264ace61SBarry Smith   PetscFunctionBegin;
2940264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2941251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2942264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2943264ace61SBarry Smith 
29440f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
29451c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2946ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2947264ace61SBarry Smith 
2948264ace61SBarry Smith   if (dm->ops->destroy) {
2949264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
29500298fd71SBarry Smith     dm->ops->destroy = NULL;
2951264ace61SBarry Smith   }
2952264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2953264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2954264ace61SBarry Smith   PetscFunctionReturn(0);
2955264ace61SBarry Smith }
2956264ace61SBarry Smith 
2957264ace61SBarry Smith #undef __FUNCT__
2958264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2959264ace61SBarry Smith /*@C
2960264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2961264ace61SBarry Smith 
2962264ace61SBarry Smith   Not Collective
2963264ace61SBarry Smith 
2964264ace61SBarry Smith   Input Parameter:
2965264ace61SBarry Smith . dm  - The DM
2966264ace61SBarry Smith 
2967264ace61SBarry Smith   Output Parameter:
2968264ace61SBarry Smith . type - The DM type name
2969264ace61SBarry Smith 
2970264ace61SBarry Smith   Level: intermediate
2971264ace61SBarry Smith 
2972264ace61SBarry Smith .keywords: DM, get, type, name
2973264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2974264ace61SBarry Smith @*/
297519fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2976264ace61SBarry Smith {
2977264ace61SBarry Smith   PetscErrorCode ierr;
2978264ace61SBarry Smith 
2979264ace61SBarry Smith   PetscFunctionBegin;
2980264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2981c959eef4SJed Brown   PetscValidPointer(type,2);
2982607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2983264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2984264ace61SBarry Smith   PetscFunctionReturn(0);
2985264ace61SBarry Smith }
2986264ace61SBarry Smith 
298767a56275SMatthew G Knepley #undef __FUNCT__
298867a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
298967a56275SMatthew G Knepley /*@C
299067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
299167a56275SMatthew G Knepley 
299267a56275SMatthew G Knepley   Collective on DM
299367a56275SMatthew G Knepley 
299467a56275SMatthew G Knepley   Input Parameters:
299567a56275SMatthew G Knepley + dm - the DM
299667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
299767a56275SMatthew G Knepley 
299867a56275SMatthew G Knepley   Output Parameter:
299967a56275SMatthew G Knepley . M - pointer to new DM
300067a56275SMatthew G Knepley 
300167a56275SMatthew G Knepley   Notes:
300267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
300367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
300467a56275SMatthew G Knepley   of the input DM.
300567a56275SMatthew G Knepley 
300667a56275SMatthew G Knepley   Level: intermediate
300767a56275SMatthew G Knepley 
300867a56275SMatthew G Knepley .seealso: DMCreate()
300967a56275SMatthew G Knepley @*/
301019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
301167a56275SMatthew G Knepley {
301267a56275SMatthew G Knepley   DM             B;
301367a56275SMatthew G Knepley   char           convname[256];
3014c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
301567a56275SMatthew G Knepley   PetscErrorCode ierr;
301667a56275SMatthew G Knepley 
301767a56275SMatthew G Knepley   PetscFunctionBegin;
301867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
301967a56275SMatthew G Knepley   PetscValidType(dm,1);
302067a56275SMatthew G Knepley   PetscValidPointer(M,3);
3021251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3022c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3023c067b6caSMatthew G. Knepley   if (sametype) {
3024c067b6caSMatthew G. Knepley     *M   = dm;
3025c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3026c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3027c067b6caSMatthew G. Knepley   } else {
30280298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
302967a56275SMatthew G Knepley 
303067a56275SMatthew G Knepley     /*
303167a56275SMatthew G Knepley        Order of precedence:
303267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
303367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
303467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
303567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
303667a56275SMatthew G Knepley        5) Use a really basic converter.
303767a56275SMatthew G Knepley     */
303867a56275SMatthew G Knepley 
303967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
304067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
304167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
304267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
304367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
304467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
30450005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
304667a56275SMatthew G Knepley     if (conv) goto foundconv;
304767a56275SMatthew G Knepley 
304867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
304982f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
305067a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
305167a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
305267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
305367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
305467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
305567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
30560005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
305767a56275SMatthew G Knepley     if (conv) {
3058fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
305967a56275SMatthew G Knepley       goto foundconv;
306067a56275SMatthew G Knepley     }
306167a56275SMatthew G Knepley 
306267a56275SMatthew G Knepley #if 0
306367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
306467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3065fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
306667a56275SMatthew G Knepley     if (conv) goto foundconv;
306767a56275SMatthew G Knepley 
306867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
306967a56275SMatthew G Knepley     if (dm->ops->convert) {
307067a56275SMatthew G Knepley       conv = dm->ops->convert;
307167a56275SMatthew G Knepley     }
307267a56275SMatthew G Knepley     if (conv) goto foundconv;
307367a56275SMatthew G Knepley #endif
307467a56275SMatthew G Knepley 
307567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
307682f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
307767a56275SMatthew G Knepley 
307867a56275SMatthew G Knepley foundconv:
307967a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
308067a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
308112fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
308212fa691eSMatthew G. Knepley     if (dm->maxCell) {
308312fa691eSMatthew G. Knepley       const PetscReal *maxCell, *L;
308412fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
308512fa691eSMatthew G. Knepley       ierr = DMGetPeriodicity(dm, &maxCell, &L, &bd);CHKERRQ(ierr);
308612fa691eSMatthew G. Knepley       ierr = DMSetPeriodicity(*M,  maxCell,  L,  bd);CHKERRQ(ierr);
308712fa691eSMatthew G. Knepley     }
308867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
308967a56275SMatthew G Knepley   }
309067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
309167a56275SMatthew G Knepley   PetscFunctionReturn(0);
309267a56275SMatthew G Knepley }
3093264ace61SBarry Smith 
3094264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3095264ace61SBarry Smith 
3096264ace61SBarry Smith #undef __FUNCT__
3097264ace61SBarry Smith #define __FUNCT__ "DMRegister"
3098264ace61SBarry Smith /*@C
30991c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
31001c84c290SBarry Smith 
31011c84c290SBarry Smith   Not Collective
31021c84c290SBarry Smith 
31031c84c290SBarry Smith   Input Parameters:
31041c84c290SBarry Smith + name        - The name of a new user-defined creation routine
31051c84c290SBarry Smith - create_func - The creation routine itself
31061c84c290SBarry Smith 
31071c84c290SBarry Smith   Notes:
31081c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
31091c84c290SBarry Smith 
31101c84c290SBarry Smith 
31111c84c290SBarry Smith   Sample usage:
31121c84c290SBarry Smith .vb
3113bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
31141c84c290SBarry Smith .ve
31151c84c290SBarry Smith 
31161c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
31171c84c290SBarry Smith .vb
31181c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
31191c84c290SBarry Smith     DMSetType(DM,"my_da");
31201c84c290SBarry Smith .ve
31211c84c290SBarry Smith    or at runtime via the option
31221c84c290SBarry Smith .vb
31231c84c290SBarry Smith     -da_type my_da
31241c84c290SBarry Smith .ve
3125264ace61SBarry Smith 
3126264ace61SBarry Smith   Level: advanced
31271c84c290SBarry Smith 
31281c84c290SBarry Smith .keywords: DM, register
3129bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
31301c84c290SBarry Smith 
3131264ace61SBarry Smith @*/
3132bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3133264ace61SBarry Smith {
3134264ace61SBarry Smith   PetscErrorCode ierr;
3135264ace61SBarry Smith 
3136264ace61SBarry Smith   PetscFunctionBegin;
3137a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3138264ace61SBarry Smith   PetscFunctionReturn(0);
3139264ace61SBarry Smith }
3140264ace61SBarry Smith 
3141b859378eSBarry Smith #undef __FUNCT__
3142b859378eSBarry Smith #define __FUNCT__ "DMLoad"
3143b859378eSBarry Smith /*@C
314455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3145b859378eSBarry Smith 
3146b859378eSBarry Smith   Collective on PetscViewer
3147b859378eSBarry Smith 
3148b859378eSBarry Smith   Input Parameters:
3149b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3150b859378eSBarry Smith            some related function before a call to DMLoad().
3151b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3152b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3153b859378eSBarry Smith 
3154b859378eSBarry Smith    Level: intermediate
3155b859378eSBarry Smith 
3156b859378eSBarry Smith   Notes:
315755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3158b859378eSBarry Smith 
3159b859378eSBarry Smith   Notes for advanced users:
3160b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3161b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3162b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3163b859378eSBarry Smith   format is
3164b859378eSBarry Smith .vb
3165b859378eSBarry Smith      has not yet been determined
3166b859378eSBarry Smith .ve
3167b859378eSBarry Smith 
3168b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3169b859378eSBarry Smith @*/
3170b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3171b859378eSBarry Smith {
31729331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3173b859378eSBarry Smith   PetscErrorCode ierr;
3174b859378eSBarry Smith 
3175b859378eSBarry Smith   PetscFunctionBegin;
3176b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3177b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
317832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
31799331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
31809331c7a4SMatthew G. Knepley   if (isbinary) {
31819331c7a4SMatthew G. Knepley     PetscInt classid;
31829331c7a4SMatthew G. Knepley     char     type[256];
3183b859378eSBarry Smith 
3184060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
31859200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3186060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
318732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
31889331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
31899331c7a4SMatthew G. Knepley   } else if (ishdf5) {
31909331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
31919331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3192b859378eSBarry Smith   PetscFunctionReturn(0);
3193b859378eSBarry Smith }
3194b859378eSBarry Smith 
31957da65231SMatthew G Knepley /******************************** FEM Support **********************************/
31967da65231SMatthew G Knepley 
31977da65231SMatthew G Knepley #undef __FUNCT__
31987da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3199a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3200a6dfd86eSKarl Rupp {
32011d47ebbbSSatish Balay   PetscInt       f;
32021b30c384SMatthew G Knepley   PetscErrorCode ierr;
32031b30c384SMatthew G Knepley 
32047da65231SMatthew G Knepley   PetscFunctionBegin;
320574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32061d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
320757622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
32087da65231SMatthew G Knepley   }
32097da65231SMatthew G Knepley   PetscFunctionReturn(0);
32107da65231SMatthew G Knepley }
32117da65231SMatthew G Knepley 
32127da65231SMatthew G Knepley #undef __FUNCT__
32137da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3214a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3215a6dfd86eSKarl Rupp {
32161b30c384SMatthew G Knepley   PetscInt       f, g;
32177da65231SMatthew G Knepley   PetscErrorCode ierr;
32187da65231SMatthew G Knepley 
32197da65231SMatthew G Knepley   PetscFunctionBegin;
322074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32211d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
322274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
32231d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3224e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
32257da65231SMatthew G Knepley     }
322674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
32277da65231SMatthew G Knepley   }
32287da65231SMatthew G Knepley   PetscFunctionReturn(0);
32297da65231SMatthew G Knepley }
3230e7c4fc90SDmitry Karpeev 
3231970e74d5SMatthew G Knepley #undef __FUNCT__
3232e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
32336113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3234e759306cSMatthew G. Knepley {
3235e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3236e759306cSMatthew G. Knepley   PetscInt       p;
3237e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3238e759306cSMatthew G. Knepley 
3239e759306cSMatthew G. Knepley   PetscFunctionBegin;
3240e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3241e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3242e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3243e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3244e759306cSMatthew G. Knepley     if (p == rank) {
3245e759306cSMatthew G. Knepley       Vec x;
3246e759306cSMatthew G. Knepley 
3247e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3248e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
32496113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3250e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3251e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3252e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3253e759306cSMatthew G. Knepley     }
3254e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3255e759306cSMatthew G. Knepley   }
3256e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3257e759306cSMatthew G. Knepley }
3258e759306cSMatthew G. Knepley 
3259e759306cSMatthew G. Knepley #undef __FUNCT__
326088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
326188ed4aceSMatthew G Knepley /*@
326288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
326388ed4aceSMatthew G Knepley 
326488ed4aceSMatthew G Knepley   Input Parameter:
326588ed4aceSMatthew G Knepley . dm - The DM
326688ed4aceSMatthew G Knepley 
326788ed4aceSMatthew G Knepley   Output Parameter:
326888ed4aceSMatthew G Knepley . section - The PetscSection
326988ed4aceSMatthew G Knepley 
327088ed4aceSMatthew G Knepley   Level: intermediate
327188ed4aceSMatthew G Knepley 
327288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
327388ed4aceSMatthew G Knepley 
327488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
327588ed4aceSMatthew G Knepley @*/
32760adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
32770adebc6cSBarry Smith {
3278fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3279fd59a867SMatthew G. Knepley 
328088ed4aceSMatthew G Knepley   PetscFunctionBegin;
328188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
328288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
32832f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
32842f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3285ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
32862f0f8703SMatthew G. Knepley   }
328788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
328888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
328988ed4aceSMatthew G Knepley }
329088ed4aceSMatthew G Knepley 
329188ed4aceSMatthew G Knepley #undef __FUNCT__
329288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
329388ed4aceSMatthew G Knepley /*@
329488ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
329588ed4aceSMatthew G Knepley 
329688ed4aceSMatthew G Knepley   Input Parameters:
329788ed4aceSMatthew G Knepley + dm - The DM
329888ed4aceSMatthew G Knepley - section - The PetscSection
329988ed4aceSMatthew G Knepley 
330088ed4aceSMatthew G Knepley   Level: intermediate
330188ed4aceSMatthew G Knepley 
330288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
330388ed4aceSMatthew G Knepley 
330488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
330588ed4aceSMatthew G Knepley @*/
33060adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
33070adebc6cSBarry Smith {
3308c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3309af122d2aSMatthew G Knepley   PetscInt       f;
331088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
331188ed4aceSMatthew G Knepley 
331288ed4aceSMatthew G Knepley   PetscFunctionBegin;
331388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3314c473ab19SMatthew G. Knepley   if (section) {
33151d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33161d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3317c473ab19SMatthew G. Knepley   }
331888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
331988ed4aceSMatthew G Knepley   dm->defaultSection = section;
3320c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3321af122d2aSMatthew G Knepley   if (numFields) {
3322af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3323af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
33240f21e855SMatthew G. Knepley       PetscObject disc;
3325af122d2aSMatthew G Knepley       const char *name;
3326af122d2aSMatthew G Knepley 
3327af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
33280f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
33290f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3330af122d2aSMatthew G Knepley     }
3331af122d2aSMatthew G Knepley   }
33321d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
33331d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
333488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
333588ed4aceSMatthew G Knepley }
333688ed4aceSMatthew G Knepley 
333788ed4aceSMatthew G Knepley #undef __FUNCT__
33389435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
33399435951eSToby Isaac /*@
33409435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
33419435951eSToby Isaac 
3342e228b242SToby Isaac   not collective
3343e228b242SToby Isaac 
33449435951eSToby Isaac   Input Parameter:
33459435951eSToby Isaac . dm - The DM
33469435951eSToby Isaac 
33479435951eSToby Isaac   Output Parameter:
33489435951eSToby 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.
33499435951eSToby 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.
33509435951eSToby Isaac 
33519435951eSToby Isaac   Level: advanced
33529435951eSToby Isaac 
33539435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
33549435951eSToby Isaac 
33559435951eSToby Isaac .seealso: DMSetDefaultConstraints()
33569435951eSToby Isaac @*/
33579435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
33589435951eSToby Isaac {
33599435951eSToby Isaac   PetscErrorCode ierr;
33609435951eSToby Isaac 
33619435951eSToby Isaac   PetscFunctionBegin;
33629435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33639435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
336445a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
336545a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
33669435951eSToby Isaac   PetscFunctionReturn(0);
33679435951eSToby Isaac }
33689435951eSToby Isaac 
33699435951eSToby Isaac #undef __FUNCT__
33709435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
33719435951eSToby Isaac /*@
33729435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
33739435951eSToby Isaac 
33749435951eSToby 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().
33759435951eSToby Isaac 
33769435951eSToby 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.
33779435951eSToby Isaac 
3378e228b242SToby Isaac   collective on dm
3379e228b242SToby Isaac 
33809435951eSToby Isaac   Input Parameters:
33819435951eSToby Isaac + dm - The DM
3382e228b242SToby 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).
3383e228b242SToby 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).
33849435951eSToby Isaac 
33859435951eSToby Isaac   Level: advanced
33869435951eSToby Isaac 
33879435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
33889435951eSToby Isaac 
33899435951eSToby Isaac .seealso: DMGetDefaultConstraints()
33909435951eSToby Isaac @*/
33919435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
33929435951eSToby Isaac {
3393e228b242SToby Isaac   PetscMPIInt result;
33949435951eSToby Isaac   PetscErrorCode ierr;
33959435951eSToby Isaac 
33969435951eSToby Isaac   PetscFunctionBegin;
33979435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3398e228b242SToby Isaac   if (section) {
3399e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3400e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3401e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3402e228b242SToby Isaac   }
3403e228b242SToby Isaac   if (mat) {
3404e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3405e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3406e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3407e228b242SToby Isaac   }
34089435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
34099435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
34109435951eSToby Isaac   dm->defaultConstraintSection = section;
34119435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
34129435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
34139435951eSToby Isaac   dm->defaultConstraintMat = mat;
34149435951eSToby Isaac   PetscFunctionReturn(0);
34159435951eSToby Isaac }
34169435951eSToby Isaac 
3417f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
34189435951eSToby Isaac #undef __FUNCT__
3419284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3420507e4973SMatthew G. Knepley /*
3421507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3422507e4973SMatthew G. Knepley 
3423507e4973SMatthew G. Knepley   Input Parameters:
3424507e4973SMatthew G. Knepley + dm - The DM
3425507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3426507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3427507e4973SMatthew G. Knepley 
3428507e4973SMatthew G. Knepley   Level: intermediate
3429507e4973SMatthew G. Knepley 
3430507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3431507e4973SMatthew G. Knepley */
3432f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3433507e4973SMatthew G. Knepley {
3434507e4973SMatthew G. Knepley   MPI_Comm        comm;
3435507e4973SMatthew G. Knepley   PetscLayout     layout;
3436507e4973SMatthew G. Knepley   const PetscInt *ranges;
3437507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3438507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3439507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3440507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3441507e4973SMatthew G. Knepley 
3442507e4973SMatthew G. Knepley   PetscFunctionBegin;
3443507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3444507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3445507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3446507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3447507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3448507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3449507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3450507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3451507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3452507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3453507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3454507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3455f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3456507e4973SMatthew G. Knepley 
3457507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3458507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3459507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3460507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3461507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3462507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3463507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3464507e4973SMatthew 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;}
3465507e4973SMatthew 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;}
3466507e4973SMatthew G. Knepley     if (gdof < 0) {
3467507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3468507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3469507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3470507e4973SMatthew G. Knepley 
3471507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3472507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3473507e4973SMatthew 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;}
3474507e4973SMatthew G. Knepley       }
3475507e4973SMatthew G. Knepley     }
3476507e4973SMatthew G. Knepley   }
3477507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3478507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3479b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3480507e4973SMatthew G. Knepley   if (!gvalid) {
3481507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3482507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3483507e4973SMatthew G. Knepley   }
3484507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3485507e4973SMatthew G. Knepley }
3486f741bcd2SMatthew G. Knepley #endif
3487507e4973SMatthew G. Knepley 
3488507e4973SMatthew G. Knepley #undef __FUNCT__
348988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
349088ed4aceSMatthew G Knepley /*@
349188ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
349288ed4aceSMatthew G Knepley 
34938b1ab98fSJed Brown   Collective on DM
34948b1ab98fSJed Brown 
349588ed4aceSMatthew G Knepley   Input Parameter:
349688ed4aceSMatthew G Knepley . dm - The DM
349788ed4aceSMatthew G Knepley 
349888ed4aceSMatthew G Knepley   Output Parameter:
349988ed4aceSMatthew G Knepley . section - The PetscSection
350088ed4aceSMatthew G Knepley 
350188ed4aceSMatthew G Knepley   Level: intermediate
350288ed4aceSMatthew G Knepley 
350388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
350488ed4aceSMatthew G Knepley 
350588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
350688ed4aceSMatthew G Knepley @*/
35070adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
35080adebc6cSBarry Smith {
350988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
351088ed4aceSMatthew G Knepley 
351188ed4aceSMatthew G Knepley   PetscFunctionBegin;
351288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
351388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
351488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3515fd59a867SMatthew G. Knepley     PetscSection s;
3516fd59a867SMatthew G. Knepley 
3517fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3518fd59a867SMatthew 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");
3519fd59a867SMatthew 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");
352015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3521cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3522ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3523685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
352488ed4aceSMatthew G Knepley   }
352588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
352688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
352788ed4aceSMatthew G Knepley }
352888ed4aceSMatthew G Knepley 
352988ed4aceSMatthew G Knepley #undef __FUNCT__
3530b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3531b21d0597SMatthew G Knepley /*@
3532b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3533b21d0597SMatthew G Knepley 
3534b21d0597SMatthew G Knepley   Input Parameters:
3535b21d0597SMatthew G Knepley + dm - The DM
35365080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3537b21d0597SMatthew G Knepley 
3538b21d0597SMatthew G Knepley   Level: intermediate
3539b21d0597SMatthew G Knepley 
3540b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3541b21d0597SMatthew G Knepley 
3542b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3543b21d0597SMatthew G Knepley @*/
35440adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
35450adebc6cSBarry Smith {
3546b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3547b21d0597SMatthew G Knepley 
3548b21d0597SMatthew G Knepley   PetscFunctionBegin;
3549b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35505080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
35511d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3552b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3553b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3554507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3555f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3556507e4973SMatthew G. Knepley #endif
3557b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3558b21d0597SMatthew G Knepley }
3559b21d0597SMatthew G Knepley 
3560b21d0597SMatthew G Knepley #undef __FUNCT__
356188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
356288ed4aceSMatthew G Knepley /*@
356388ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
356488ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
356588ed4aceSMatthew G Knepley 
356688ed4aceSMatthew G Knepley   Input Parameter:
356788ed4aceSMatthew G Knepley . dm - The DM
356888ed4aceSMatthew G Knepley 
356988ed4aceSMatthew G Knepley   Output Parameter:
357088ed4aceSMatthew G Knepley . sf - The PetscSF
357188ed4aceSMatthew G Knepley 
357288ed4aceSMatthew G Knepley   Level: intermediate
357388ed4aceSMatthew G Knepley 
357488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
357588ed4aceSMatthew G Knepley 
357688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
357788ed4aceSMatthew G Knepley @*/
35780adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
35790adebc6cSBarry Smith {
358088ed4aceSMatthew G Knepley   PetscInt       nroots;
358188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
358288ed4aceSMatthew G Knepley 
358388ed4aceSMatthew G Knepley   PetscFunctionBegin;
358488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
358588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
35860298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
358788ed4aceSMatthew G Knepley   if (nroots < 0) {
358888ed4aceSMatthew G Knepley     PetscSection section, gSection;
358988ed4aceSMatthew G Knepley 
359088ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
359131ea6d37SMatthew G Knepley     if (section) {
359288ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
359388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
359431ea6d37SMatthew G Knepley     } else {
35950298fd71SBarry Smith       *sf = NULL;
359631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
359731ea6d37SMatthew G Knepley     }
359888ed4aceSMatthew G Knepley   }
359988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
360088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
360188ed4aceSMatthew G Knepley }
360288ed4aceSMatthew G Knepley 
360388ed4aceSMatthew G Knepley #undef __FUNCT__
360488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
360588ed4aceSMatthew G Knepley /*@
360688ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
360788ed4aceSMatthew G Knepley 
360888ed4aceSMatthew G Knepley   Input Parameters:
360988ed4aceSMatthew G Knepley + dm - The DM
361088ed4aceSMatthew G Knepley - sf - The PetscSF
361188ed4aceSMatthew G Knepley 
361288ed4aceSMatthew G Knepley   Level: intermediate
361388ed4aceSMatthew G Knepley 
361488ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
361588ed4aceSMatthew G Knepley 
361688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
361788ed4aceSMatthew G Knepley @*/
36180adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
36190adebc6cSBarry Smith {
362088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
362188ed4aceSMatthew G Knepley 
362288ed4aceSMatthew G Knepley   PetscFunctionBegin;
362388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
362488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
362588ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
362688ed4aceSMatthew G Knepley   dm->defaultSF = sf;
362788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
362888ed4aceSMatthew G Knepley }
362988ed4aceSMatthew G Knepley 
363088ed4aceSMatthew G Knepley #undef __FUNCT__
363188ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
363288ed4aceSMatthew G Knepley /*@C
363388ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
363488ed4aceSMatthew G Knepley   describing the data layout.
363588ed4aceSMatthew G Knepley 
363688ed4aceSMatthew G Knepley   Input Parameters:
363788ed4aceSMatthew G Knepley + dm - The DM
363888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
363988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
364088ed4aceSMatthew G Knepley 
364188ed4aceSMatthew G Knepley   Level: intermediate
364288ed4aceSMatthew G Knepley 
364388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
364488ed4aceSMatthew G Knepley @*/
364588ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
364688ed4aceSMatthew G Knepley {
364782f516ccSBarry Smith   MPI_Comm       comm;
364888ed4aceSMatthew G Knepley   PetscLayout    layout;
364988ed4aceSMatthew G Knepley   const PetscInt *ranges;
365088ed4aceSMatthew G Knepley   PetscInt       *local;
365188ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3652ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
365388ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
365488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
365588ed4aceSMatthew G Knepley 
365688ed4aceSMatthew G Knepley   PetscFunctionBegin;
365782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
365888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
365988ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
366088ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
366188ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
366288ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
366388ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
366488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
366588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
366688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
366788ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3668ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
36696636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
367088ed4aceSMatthew G Knepley 
36716636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
36726636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3673235fbf56SMatthew 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));
36746636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
367588ed4aceSMatthew G Knepley   }
3676785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3677785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
367888ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
36791f588964SMatthew G Knepley     const PetscInt *cind;
36806636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
368188ed4aceSMatthew G Knepley 
368288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
368388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
368488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
368588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
368688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
36876636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
368888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
36896636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
36906636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
36916636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3692057b4bcdSMatthew 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);
36936636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
36946636e97aSMatthew G Knepley     }
369588ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
369688ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
369788ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
369888ed4aceSMatthew G Knepley     }
369988ed4aceSMatthew G Knepley     if (gdof < 0) {
37006636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
370188ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
370288ed4aceSMatthew G Knepley 
370305376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
370431d3f06eSJed Brown         if (r < 0) r = -(r+2);
370505376888SMatthew 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);
370688ed4aceSMatthew G Knepley         remote[l].rank  = r;
370788ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
370888ed4aceSMatthew G Knepley       }
370988ed4aceSMatthew G Knepley     } else {
37106636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
371188ed4aceSMatthew G Knepley         remote[l].rank  = rank;
371288ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
371388ed4aceSMatthew G Knepley       }
371488ed4aceSMatthew G Knepley     }
371588ed4aceSMatthew G Knepley   }
37166636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
371788ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
371888ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
371988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
372088ed4aceSMatthew G Knepley }
3721af122d2aSMatthew G Knepley 
3722af122d2aSMatthew G Knepley #undef __FUNCT__
3723b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3724b21d0597SMatthew G Knepley /*@
3725b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3726b21d0597SMatthew G Knepley 
3727b21d0597SMatthew G Knepley   Input Parameter:
3728b21d0597SMatthew G Knepley . dm - The DM
3729b21d0597SMatthew G Knepley 
3730b21d0597SMatthew G Knepley   Output Parameter:
3731b21d0597SMatthew G Knepley . sf - The PetscSF
3732b21d0597SMatthew G Knepley 
3733b21d0597SMatthew G Knepley   Level: intermediate
3734b21d0597SMatthew G Knepley 
3735b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3736b21d0597SMatthew G Knepley 
3737057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3738b21d0597SMatthew G Knepley @*/
37390adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
37400adebc6cSBarry Smith {
3741b21d0597SMatthew G Knepley   PetscFunctionBegin;
3742b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3743b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3744b21d0597SMatthew G Knepley   *sf = dm->sf;
3745b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3746b21d0597SMatthew G Knepley }
3747b21d0597SMatthew G Knepley 
3748b21d0597SMatthew G Knepley #undef __FUNCT__
3749057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3750057b4bcdSMatthew G Knepley /*@
3751057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3752057b4bcdSMatthew G Knepley 
3753057b4bcdSMatthew G Knepley   Input Parameters:
3754057b4bcdSMatthew G Knepley + dm - The DM
3755057b4bcdSMatthew G Knepley - sf - The PetscSF
3756057b4bcdSMatthew G Knepley 
3757057b4bcdSMatthew G Knepley   Level: intermediate
3758057b4bcdSMatthew G Knepley 
3759057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3760057b4bcdSMatthew G Knepley @*/
37610adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
37620adebc6cSBarry Smith {
3763057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3764057b4bcdSMatthew G Knepley 
3765057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3766057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3767057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3768057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3769057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3770057b4bcdSMatthew G Knepley   dm->sf = sf;
3771057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3772057b4bcdSMatthew G Knepley }
3773057b4bcdSMatthew G Knepley 
3774057b4bcdSMatthew G Knepley #undef __FUNCT__
37752764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
37762764a2aaSMatthew G. Knepley /*@
37772764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
37782764a2aaSMatthew G. Knepley 
37792764a2aaSMatthew G. Knepley   Input Parameter:
37802764a2aaSMatthew G. Knepley . dm - The DM
37812764a2aaSMatthew G. Knepley 
37822764a2aaSMatthew G. Knepley   Output Parameter:
37832764a2aaSMatthew G. Knepley . prob - The PetscDS
37842764a2aaSMatthew G. Knepley 
37852764a2aaSMatthew G. Knepley   Level: developer
37862764a2aaSMatthew G. Knepley 
37872764a2aaSMatthew G. Knepley .seealso: DMSetDS()
37882764a2aaSMatthew G. Knepley @*/
37892764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3790af122d2aSMatthew G Knepley {
3791af122d2aSMatthew G Knepley   PetscFunctionBegin;
3792af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37930f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
37940f21e855SMatthew G. Knepley   *prob = dm->prob;
37950f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
37960f21e855SMatthew G. Knepley }
37970f21e855SMatthew G. Knepley 
37980f21e855SMatthew G. Knepley #undef __FUNCT__
37992764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
38002764a2aaSMatthew G. Knepley /*@
38012764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
38022764a2aaSMatthew G. Knepley 
38032764a2aaSMatthew G. Knepley   Input Parameters:
38042764a2aaSMatthew G. Knepley + dm - The DM
38052764a2aaSMatthew G. Knepley - prob - The PetscDS
38062764a2aaSMatthew G. Knepley 
38072764a2aaSMatthew G. Knepley   Level: developer
38082764a2aaSMatthew G. Knepley 
38092764a2aaSMatthew G. Knepley .seealso: DMGetDS()
38102764a2aaSMatthew G. Knepley @*/
38112764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
38120f21e855SMatthew G. Knepley {
38130f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38140f21e855SMatthew G. Knepley 
38150f21e855SMatthew G. Knepley   PetscFunctionBegin;
38160f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38172764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
38182764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
38190f21e855SMatthew G. Knepley   dm->prob = prob;
38200f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
38210f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38220f21e855SMatthew G. Knepley }
38230f21e855SMatthew G. Knepley 
38240f21e855SMatthew G. Knepley #undef __FUNCT__
38250f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
38260f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
38270f21e855SMatthew G. Knepley {
38280f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38290f21e855SMatthew G. Knepley 
38300f21e855SMatthew G. Knepley   PetscFunctionBegin;
38310f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38322764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3833af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3834af122d2aSMatthew G Knepley }
3835af122d2aSMatthew G Knepley 
3836af122d2aSMatthew G Knepley #undef __FUNCT__
3837af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3838af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3839af122d2aSMatthew G Knepley {
38400f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3841af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3842af122d2aSMatthew G Knepley 
3843af122d2aSMatthew G Knepley   PetscFunctionBegin;
3844af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38452764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
38460f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
38470f21e855SMatthew G. Knepley     PetscContainer obj;
38480f21e855SMatthew G. Knepley 
38490f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
38502764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
38510f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3852af122d2aSMatthew G Knepley   }
3853af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3854af122d2aSMatthew G Knepley }
3855af122d2aSMatthew G Knepley 
3856af122d2aSMatthew G Knepley #undef __FUNCT__
3857af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3858c1929be8SMatthew G. Knepley /*@
3859c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3860c1929be8SMatthew G. Knepley 
3861c1929be8SMatthew G. Knepley   Not collective
3862c1929be8SMatthew G. Knepley 
3863c1929be8SMatthew G. Knepley   Input Parameters:
3864c1929be8SMatthew G. Knepley + dm - The DM
3865c1929be8SMatthew G. Knepley - f  - The field number
3866c1929be8SMatthew G. Knepley 
3867c1929be8SMatthew G. Knepley   Output Parameter:
3868c1929be8SMatthew G. Knepley . field - The discretization object
3869c1929be8SMatthew G. Knepley 
3870c1929be8SMatthew G. Knepley   Level: developer
3871c1929be8SMatthew G. Knepley 
3872c1929be8SMatthew G. Knepley .seealso: DMSetField()
3873c1929be8SMatthew G. Knepley @*/
3874af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3875af122d2aSMatthew G Knepley {
38760f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38770f21e855SMatthew G. Knepley 
3878af122d2aSMatthew G Knepley   PetscFunctionBegin;
3879af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38802764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3881decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3882decb47aaSMatthew G. Knepley }
3883decb47aaSMatthew G. Knepley 
3884decb47aaSMatthew G. Knepley #undef __FUNCT__
3885decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3886c1929be8SMatthew G. Knepley /*@
3887c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3888c1929be8SMatthew G. Knepley 
3889c1929be8SMatthew G. Knepley   Logically collective on DM
3890c1929be8SMatthew G. Knepley 
3891c1929be8SMatthew G. Knepley   Input Parameters:
3892c1929be8SMatthew G. Knepley + dm - The DM
3893c1929be8SMatthew G. Knepley . f  - The field number
3894c1929be8SMatthew G. Knepley - field - The discretization object
3895c1929be8SMatthew G. Knepley 
3896c1929be8SMatthew G. Knepley   Level: developer
3897c1929be8SMatthew G. Knepley 
3898c1929be8SMatthew G. Knepley .seealso: DMGetField()
3899c1929be8SMatthew G. Knepley @*/
3900decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3901decb47aaSMatthew G. Knepley {
3902decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3903decb47aaSMatthew G. Knepley 
3904decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3905decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39062764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3907af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3908af122d2aSMatthew G Knepley }
39096636e97aSMatthew G Knepley 
39106636e97aSMatthew G Knepley #undef __FUNCT__
3911b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3912b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3913b64e0483SPeter Brune {
3914b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3915b64e0483SPeter Brune   PetscErrorCode ierr;
3916b64e0483SPeter Brune   Vec coords,ccoords;
39176dbf9973SLawrence Mitchell   Mat inject;
3918b64e0483SPeter Brune   PetscFunctionBegin;
3919b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3920b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3921b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3922b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3923b64e0483SPeter Brune   if (coords && !ccoords) {
3924b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
39256dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
39262adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
39276dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3928b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3929b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3930b64e0483SPeter Brune   }
3931b64e0483SPeter Brune   PetscFunctionReturn(0);
3932b64e0483SPeter Brune }
3933b64e0483SPeter Brune 
3934b64e0483SPeter Brune #undef __FUNCT__
393503dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
393603dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
393703dadc2fSPeter Brune {
393803dadc2fSPeter Brune   DM dm_coord,subdm_coord;
393903dadc2fSPeter Brune   PetscErrorCode ierr;
394003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
394103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
394203dadc2fSPeter Brune   PetscFunctionBegin;
394303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
394403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
394503dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
394603dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
394703dadc2fSPeter Brune   if (coords && !ccoords) {
394803dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
394903dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
395024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
395103dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
395203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
395303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
395403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
395503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
395603dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
395703dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
395803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
395903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
396003dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
396103dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
396203dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
396303dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
396403dadc2fSPeter Brune   }
396503dadc2fSPeter Brune   PetscFunctionReturn(0);
396603dadc2fSPeter Brune }
396703dadc2fSPeter Brune 
396803dadc2fSPeter Brune #undef __FUNCT__
3969c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3970c73cfb54SMatthew G. Knepley /*@
3971c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3972c73cfb54SMatthew G. Knepley 
3973c73cfb54SMatthew G. Knepley   Not collective
3974c73cfb54SMatthew G. Knepley 
3975c73cfb54SMatthew G. Knepley   Input Parameter:
3976c73cfb54SMatthew G. Knepley . dm - The DM
3977c73cfb54SMatthew G. Knepley 
3978c73cfb54SMatthew G. Knepley   Output Parameter:
3979c73cfb54SMatthew G. Knepley . dim - The topological dimension
3980c73cfb54SMatthew G. Knepley 
3981c73cfb54SMatthew G. Knepley   Level: beginner
3982c73cfb54SMatthew G. Knepley 
3983c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3984c73cfb54SMatthew G. Knepley @*/
3985c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3986c73cfb54SMatthew G. Knepley {
3987c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3988c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3989c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3990c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3991c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3992c73cfb54SMatthew G. Knepley }
3993c73cfb54SMatthew G. Knepley 
3994c73cfb54SMatthew G. Knepley #undef __FUNCT__
3995c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3996c73cfb54SMatthew G. Knepley /*@
3997c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3998c73cfb54SMatthew G. Knepley 
3999c73cfb54SMatthew G. Knepley   Collective on dm
4000c73cfb54SMatthew G. Knepley 
4001c73cfb54SMatthew G. Knepley   Input Parameters:
4002c73cfb54SMatthew G. Knepley + dm - The DM
4003c73cfb54SMatthew G. Knepley - dim - The topological dimension
4004c73cfb54SMatthew G. Knepley 
4005c73cfb54SMatthew G. Knepley   Level: beginner
4006c73cfb54SMatthew G. Knepley 
4007c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4008c73cfb54SMatthew G. Knepley @*/
4009c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4010c73cfb54SMatthew G. Knepley {
4011c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4012c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4013c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4014c73cfb54SMatthew G. Knepley   dm->dim = dim;
4015c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4016c73cfb54SMatthew G. Knepley }
4017c73cfb54SMatthew G. Knepley 
4018793f3fe5SMatthew G. Knepley #undef __FUNCT__
4019793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
4020793f3fe5SMatthew G. Knepley /*@
4021793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4022793f3fe5SMatthew G. Knepley 
4023793f3fe5SMatthew G. Knepley   Collective on DM
4024793f3fe5SMatthew G. Knepley 
4025793f3fe5SMatthew G. Knepley   Input Parameters:
4026793f3fe5SMatthew G. Knepley + dm - the DM
4027793f3fe5SMatthew G. Knepley - dim - the dimension
4028793f3fe5SMatthew G. Knepley 
4029793f3fe5SMatthew G. Knepley   Output Parameters:
4030793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4031793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4032793f3fe5SMatthew G. Knepley 
4033793f3fe5SMatthew G. Knepley   Note:
4034793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4035793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4036793f3fe5SMatthew G. Knepley   then the interval is empty.
4037793f3fe5SMatthew G. Knepley 
4038793f3fe5SMatthew G. Knepley   Level: intermediate
4039793f3fe5SMatthew G. Knepley 
4040793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4041793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4042793f3fe5SMatthew G. Knepley @*/
4043793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4044793f3fe5SMatthew G. Knepley {
4045793f3fe5SMatthew G. Knepley   PetscInt       d;
4046793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4047793f3fe5SMatthew G. Knepley 
4048793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4049793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4050793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4051793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4052793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4053793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4054793f3fe5SMatthew G. Knepley }
4055793f3fe5SMatthew G. Knepley 
4056793f3fe5SMatthew G. Knepley #undef __FUNCT__
40576636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
40586636e97aSMatthew G Knepley /*@
40596636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
40606636e97aSMatthew G Knepley 
40616636e97aSMatthew G Knepley   Collective on DM
40626636e97aSMatthew G Knepley 
40636636e97aSMatthew G Knepley   Input Parameters:
40646636e97aSMatthew G Knepley + dm - the DM
40656636e97aSMatthew G Knepley - c - coordinate vector
40666636e97aSMatthew G Knepley 
40676636e97aSMatthew G Knepley   Note:
40686636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
40696636e97aSMatthew G Knepley 
40706636e97aSMatthew G Knepley   Level: intermediate
40716636e97aSMatthew G Knepley 
40726636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40736636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
40746636e97aSMatthew G Knepley @*/
40756636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
40766636e97aSMatthew G Knepley {
40776636e97aSMatthew G Knepley   PetscErrorCode ierr;
40786636e97aSMatthew G Knepley 
40796636e97aSMatthew G Knepley   PetscFunctionBegin;
40806636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40816636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
40826636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
40836636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
40846636e97aSMatthew G Knepley   dm->coordinates = c;
40856636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4086b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
408703dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
40886636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40896636e97aSMatthew G Knepley }
40906636e97aSMatthew G Knepley 
40916636e97aSMatthew G Knepley #undef __FUNCT__
40926636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
40936636e97aSMatthew G Knepley /*@
40946636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
40956636e97aSMatthew G Knepley 
40966636e97aSMatthew G Knepley   Collective on DM
40976636e97aSMatthew G Knepley 
40986636e97aSMatthew G Knepley    Input Parameters:
40996636e97aSMatthew G Knepley +  dm - the DM
41006636e97aSMatthew G Knepley -  c - coordinate vector
41016636e97aSMatthew G Knepley 
41026636e97aSMatthew G Knepley   Note:
41036636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
41046636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
41056636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
41066636e97aSMatthew G Knepley 
41076636e97aSMatthew G Knepley   Level: intermediate
41086636e97aSMatthew G Knepley 
41096636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41106636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
41116636e97aSMatthew G Knepley @*/
41126636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
41136636e97aSMatthew G Knepley {
41146636e97aSMatthew G Knepley   PetscErrorCode ierr;
41156636e97aSMatthew G Knepley 
41166636e97aSMatthew G Knepley   PetscFunctionBegin;
41176636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41186636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41196636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41206636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
41218865f1eaSKarl Rupp 
41226636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
41238865f1eaSKarl Rupp 
41246636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41256636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41266636e97aSMatthew G Knepley }
41276636e97aSMatthew G Knepley 
41286636e97aSMatthew G Knepley #undef __FUNCT__
41296636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
41306636e97aSMatthew G Knepley /*@
41316636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
41326636e97aSMatthew G Knepley 
41336636e97aSMatthew G Knepley   Not Collective
41346636e97aSMatthew G Knepley 
41356636e97aSMatthew G Knepley   Input Parameter:
41366636e97aSMatthew G Knepley . dm - the DM
41376636e97aSMatthew G Knepley 
41386636e97aSMatthew G Knepley   Output Parameter:
41396636e97aSMatthew G Knepley . c - global coordinate vector
41406636e97aSMatthew G Knepley 
41416636e97aSMatthew G Knepley   Note:
41426636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
41436636e97aSMatthew G Knepley 
41446636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
41456636e97aSMatthew G Knepley 
41466636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
41476636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
41486636e97aSMatthew G Knepley 
41496636e97aSMatthew G Knepley   Level: intermediate
41506636e97aSMatthew G Knepley 
41516636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41526636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
41536636e97aSMatthew G Knepley @*/
41546636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
41556636e97aSMatthew G Knepley {
41566636e97aSMatthew G Knepley   PetscErrorCode ierr;
41576636e97aSMatthew G Knepley 
41586636e97aSMatthew G Knepley   PetscFunctionBegin;
41596636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41606636e97aSMatthew G Knepley   PetscValidPointer(c,2);
41611f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
41620298fd71SBarry Smith     DM cdm = NULL;
41636636e97aSMatthew G Knepley 
41646636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
41656636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
41666636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
41676636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
41686636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
41696636e97aSMatthew G Knepley   }
41706636e97aSMatthew G Knepley   *c = dm->coordinates;
41716636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41726636e97aSMatthew G Knepley }
41736636e97aSMatthew G Knepley 
41746636e97aSMatthew G Knepley #undef __FUNCT__
41756636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
41766636e97aSMatthew G Knepley /*@
41776636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
41786636e97aSMatthew G Knepley 
41796636e97aSMatthew G Knepley   Collective on DM
41806636e97aSMatthew G Knepley 
41816636e97aSMatthew G Knepley   Input Parameter:
41826636e97aSMatthew G Knepley . dm - the DM
41836636e97aSMatthew G Knepley 
41846636e97aSMatthew G Knepley   Output Parameter:
41856636e97aSMatthew G Knepley . c - coordinate vector
41866636e97aSMatthew G Knepley 
41876636e97aSMatthew G Knepley   Note:
41886636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
41896636e97aSMatthew G Knepley 
41906636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
41916636e97aSMatthew G Knepley 
41926636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
41936636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
41946636e97aSMatthew G Knepley 
41956636e97aSMatthew G Knepley   Level: intermediate
41966636e97aSMatthew G Knepley 
41976636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41986636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
41996636e97aSMatthew G Knepley @*/
42006636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
42016636e97aSMatthew G Knepley {
42026636e97aSMatthew G Knepley   PetscErrorCode ierr;
42036636e97aSMatthew G Knepley 
42046636e97aSMatthew G Knepley   PetscFunctionBegin;
42056636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42066636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42071f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
42080298fd71SBarry Smith     DM cdm = NULL;
42096636e97aSMatthew G Knepley 
42106636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42116636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
42126636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
42136636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42146636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42156636e97aSMatthew G Knepley   }
42166636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
42176636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42186636e97aSMatthew G Knepley }
42196636e97aSMatthew G Knepley 
42206636e97aSMatthew G Knepley #undef __FUNCT__
42216636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
42226636e97aSMatthew G Knepley /*@
42231cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
42246636e97aSMatthew G Knepley 
42256636e97aSMatthew G Knepley   Collective on DM
42266636e97aSMatthew G Knepley 
42276636e97aSMatthew G Knepley   Input Parameter:
42286636e97aSMatthew G Knepley . dm - the DM
42296636e97aSMatthew G Knepley 
42306636e97aSMatthew G Knepley   Output Parameter:
42316636e97aSMatthew G Knepley . cdm - coordinate DM
42326636e97aSMatthew G Knepley 
42336636e97aSMatthew G Knepley   Level: intermediate
42346636e97aSMatthew G Knepley 
42356636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42361cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
42376636e97aSMatthew G Knepley @*/
42386636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
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(cdm,2);
42456636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
424682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
42476636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
42486636e97aSMatthew G Knepley   }
42496636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
42506636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42516636e97aSMatthew G Knepley }
4252e87bb0d3SMatthew G Knepley 
4253e87bb0d3SMatthew G Knepley #undef __FUNCT__
42541cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
42551cfe2091SMatthew G. Knepley /*@
42561cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
42571cfe2091SMatthew G. Knepley 
42581cfe2091SMatthew G. Knepley   Logically Collective on DM
42591cfe2091SMatthew G. Knepley 
42601cfe2091SMatthew G. Knepley   Input Parameters:
42611cfe2091SMatthew G. Knepley + dm - the DM
42621cfe2091SMatthew G. Knepley - cdm - coordinate DM
42631cfe2091SMatthew G. Knepley 
42641cfe2091SMatthew G. Knepley   Level: intermediate
42651cfe2091SMatthew G. Knepley 
42661cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42671cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
42681cfe2091SMatthew G. Knepley @*/
42691cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
42701cfe2091SMatthew G. Knepley {
42711cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
42721cfe2091SMatthew G. Knepley 
42731cfe2091SMatthew G. Knepley   PetscFunctionBegin;
42741cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42751cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
42761cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
42771cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
42781cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
42791cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
42801cfe2091SMatthew G. Knepley }
42811cfe2091SMatthew G. Knepley 
42821cfe2091SMatthew G. Knepley #undef __FUNCT__
428346e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
428446e270d4SMatthew G. Knepley /*@
428546e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
428646e270d4SMatthew G. Knepley 
428746e270d4SMatthew G. Knepley   Not Collective
428846e270d4SMatthew G. Knepley 
428946e270d4SMatthew G. Knepley   Input Parameter:
429046e270d4SMatthew G. Knepley . dm - The DM object
429146e270d4SMatthew G. Knepley 
429246e270d4SMatthew G. Knepley   Output Parameter:
429346e270d4SMatthew G. Knepley . dim - The embedding dimension
429446e270d4SMatthew G. Knepley 
429546e270d4SMatthew G. Knepley   Level: intermediate
429646e270d4SMatthew G. Knepley 
429746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
429846e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
429946e270d4SMatthew G. Knepley @*/
430046e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
430146e270d4SMatthew G. Knepley {
430246e270d4SMatthew G. Knepley   PetscFunctionBegin;
430346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
430446e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
43059a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
43069a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
43079a9a41abSToby Isaac   }
430846e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
430946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
431046e270d4SMatthew G. Knepley }
431146e270d4SMatthew G. Knepley 
431246e270d4SMatthew G. Knepley #undef __FUNCT__
431346e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
431446e270d4SMatthew G. Knepley /*@
431546e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
431646e270d4SMatthew G. Knepley 
431746e270d4SMatthew G. Knepley   Not Collective
431846e270d4SMatthew G. Knepley 
431946e270d4SMatthew G. Knepley   Input Parameters:
432046e270d4SMatthew G. Knepley + dm  - The DM object
432146e270d4SMatthew G. Knepley - dim - The embedding dimension
432246e270d4SMatthew G. Knepley 
432346e270d4SMatthew G. Knepley   Level: intermediate
432446e270d4SMatthew G. Knepley 
432546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
432646e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
432746e270d4SMatthew G. Knepley @*/
432846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
432946e270d4SMatthew G. Knepley {
433046e270d4SMatthew G. Knepley   PetscFunctionBegin;
433146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
433246e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
433346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
433446e270d4SMatthew G. Knepley }
433546e270d4SMatthew G. Knepley 
433646e270d4SMatthew G. Knepley #undef __FUNCT__
4337e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4338e8abe2deSMatthew G. Knepley /*@
4339e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4340e8abe2deSMatthew G. Knepley 
4341e8abe2deSMatthew G. Knepley   Not Collective
4342e8abe2deSMatthew G. Knepley 
4343e8abe2deSMatthew G. Knepley   Input Parameter:
4344e8abe2deSMatthew G. Knepley . dm - The DM object
4345e8abe2deSMatthew G. Knepley 
4346e8abe2deSMatthew G. Knepley   Output Parameter:
4347e8abe2deSMatthew G. Knepley . section - The PetscSection object
4348e8abe2deSMatthew G. Knepley 
4349e8abe2deSMatthew G. Knepley   Level: intermediate
4350e8abe2deSMatthew G. Knepley 
4351e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4352e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4353e8abe2deSMatthew G. Knepley @*/
4354e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4355e8abe2deSMatthew G. Knepley {
4356e8abe2deSMatthew G. Knepley   DM             cdm;
4357e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4358e8abe2deSMatthew G. Knepley 
4359e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4360e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4361e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4362e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4363e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4364e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4365e8abe2deSMatthew G. Knepley }
4366e8abe2deSMatthew G. Knepley 
4367e8abe2deSMatthew G. Knepley #undef __FUNCT__
4368e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4369e8abe2deSMatthew G. Knepley /*@
4370e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4371e8abe2deSMatthew G. Knepley 
4372e8abe2deSMatthew G. Knepley   Not Collective
4373e8abe2deSMatthew G. Knepley 
4374e8abe2deSMatthew G. Knepley   Input Parameters:
4375e8abe2deSMatthew G. Knepley + dm      - The DM object
437646e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4377e8abe2deSMatthew G. Knepley - section - The PetscSection object
4378e8abe2deSMatthew G. Knepley 
4379e8abe2deSMatthew G. Knepley   Level: intermediate
4380e8abe2deSMatthew G. Knepley 
4381e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4382e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4383e8abe2deSMatthew G. Knepley @*/
438446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4385e8abe2deSMatthew G. Knepley {
4386e8abe2deSMatthew G. Knepley   DM             cdm;
4387e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4388e8abe2deSMatthew G. Knepley 
4389e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4390e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
439146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4392e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4393e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
439446e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
439546e270d4SMatthew G. Knepley     PetscInt d = dim;
439646e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
439746e270d4SMatthew G. Knepley 
439846e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
439946e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
440046e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
440146e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
440246e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
440346e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
440446e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
440546e270d4SMatthew G. Knepley     }
440646e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
440746e270d4SMatthew G. Knepley   }
4408e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4409e8abe2deSMatthew G. Knepley }
4410e8abe2deSMatthew G. Knepley 
4411e8abe2deSMatthew G. Knepley #undef __FUNCT__
4412c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
44135dc8c3f7SMatthew G. Knepley /*@C
44145dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
44155dc8c3f7SMatthew G. Knepley 
44165dc8c3f7SMatthew G. Knepley   Input Parameters:
44175dc8c3f7SMatthew G. Knepley + dm      - The DM object
44185dc8c3f7SMatthew 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
44195dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44205dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44215dc8c3f7SMatthew G. Knepley 
44225dc8c3f7SMatthew G. Knepley   Level: developer
44235dc8c3f7SMatthew G. Knepley 
44245dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44255dc8c3f7SMatthew G. Knepley @*/
44265dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4427c6b900c6SMatthew G. Knepley {
4428c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4429c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4430c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4431c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
44325dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4433c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4434c6b900c6SMatthew G. Knepley }
4435c6b900c6SMatthew G. Knepley 
4436c6b900c6SMatthew G. Knepley #undef __FUNCT__
4437c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
44385dc8c3f7SMatthew G. Knepley /*@C
44395dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
44405dc8c3f7SMatthew G. Knepley 
44415dc8c3f7SMatthew G. Knepley   Input Parameters:
44425dc8c3f7SMatthew G. Knepley + dm      - The DM object
44435dc8c3f7SMatthew 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
44445dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44455dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44465dc8c3f7SMatthew G. Knepley 
44475dc8c3f7SMatthew G. Knepley   Level: developer
44485dc8c3f7SMatthew G. Knepley 
44495dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44505dc8c3f7SMatthew G. Knepley @*/
44515dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4452c6b900c6SMatthew G. Knepley {
4453c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4454c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4455c6b900c6SMatthew G. Knepley 
4456c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4457c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44585dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
44595dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
44605dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44615dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
44625dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4463c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4464c6b900c6SMatthew G. Knepley }
4465c6b900c6SMatthew G. Knepley 
4466c6b900c6SMatthew G. Knepley #undef __FUNCT__
44672e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinate"
44682e17dfb7SMatthew G. Knepley /*@
44692e17dfb7SMatthew 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.
44702e17dfb7SMatthew G. Knepley 
44712e17dfb7SMatthew G. Knepley   Input Parameters:
44722e17dfb7SMatthew G. Knepley + dm     - The DM
44732e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
44742e17dfb7SMatthew G. Knepley 
44752e17dfb7SMatthew G. Knepley   Output Parameter:
44762e17dfb7SMatthew G. Knepley . out - The localized coordinate point
44772e17dfb7SMatthew G. Knepley 
44782e17dfb7SMatthew G. Knepley   Level: developer
44792e17dfb7SMatthew G. Knepley 
44802e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
44812e17dfb7SMatthew G. Knepley @*/
44822e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscScalar out[])
44832e17dfb7SMatthew G. Knepley {
44842e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
44852e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
44862e17dfb7SMatthew G. Knepley 
44872e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
44882e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
44892e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
44902e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
44912e17dfb7SMatthew G. Knepley   } else {
44922e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
44932e17dfb7SMatthew G. Knepley       out[d] = in[d] - dm->L[d]*floor(PetscRealPart(in[d])/dm->L[d]);
44942e17dfb7SMatthew G. Knepley     }
44952e17dfb7SMatthew G. Knepley   }
44962e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
44972e17dfb7SMatthew G. Knepley }
44982e17dfb7SMatthew G. Knepley 
44992e17dfb7SMatthew G. Knepley #undef __FUNCT__
45002e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinate_Internal"
45012e17dfb7SMatthew G. Knepley /*
45022e17dfb7SMatthew 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.
45032e17dfb7SMatthew G. Knepley 
45042e17dfb7SMatthew G. Knepley   Input Parameters:
45052e17dfb7SMatthew G. Knepley + dm     - The DM
45062e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45072e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45082e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
45092e17dfb7SMatthew G. Knepley 
45102e17dfb7SMatthew G. Knepley   Output Parameter:
45112e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45122e17dfb7SMatthew G. Knepley 
45132e17dfb7SMatthew G. Knepley   Level: developer
45142e17dfb7SMatthew G. Knepley 
45152e17dfb7SMatthew 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
45162e17dfb7SMatthew G. Knepley 
45172e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45182e17dfb7SMatthew G. Knepley */
45192e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45202e17dfb7SMatthew G. Knepley {
45212e17dfb7SMatthew G. Knepley   PetscInt d;
45222e17dfb7SMatthew G. Knepley 
45232e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45242e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45252e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45262e17dfb7SMatthew G. Knepley   } else {
45272e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45282e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
45292e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
45302e17dfb7SMatthew G. Knepley       } else {
45312e17dfb7SMatthew G. Knepley         out[d] = in[d];
45322e17dfb7SMatthew G. Knepley       }
45332e17dfb7SMatthew G. Knepley     }
45342e17dfb7SMatthew G. Knepley   }
45352e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45362e17dfb7SMatthew G. Knepley }
45372e17dfb7SMatthew G. Knepley #undef __FUNCT__
45382e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinateReal_Internal"
45392e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
45402e17dfb7SMatthew G. Knepley {
45412e17dfb7SMatthew G. Knepley   PetscInt d;
45422e17dfb7SMatthew G. Knepley 
45432e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45442e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45452e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45462e17dfb7SMatthew G. Knepley   } else {
45472e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45482e17dfb7SMatthew G. Knepley       if (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d]) {
45492e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
45502e17dfb7SMatthew G. Knepley       } else {
45512e17dfb7SMatthew G. Knepley         out[d] = in[d];
45522e17dfb7SMatthew G. Knepley       }
45532e17dfb7SMatthew G. Knepley     }
45542e17dfb7SMatthew G. Knepley   }
45552e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45562e17dfb7SMatthew G. Knepley }
45572e17dfb7SMatthew G. Knepley 
45582e17dfb7SMatthew G. Knepley #undef __FUNCT__
45592e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeAddCoordinate_Internal"
45602e17dfb7SMatthew G. Knepley /*
45612e17dfb7SMatthew 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.
45622e17dfb7SMatthew G. Knepley 
45632e17dfb7SMatthew G. Knepley   Input Parameters:
45642e17dfb7SMatthew G. Knepley + dm     - The DM
45652e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45662e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45672e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
45682e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
45692e17dfb7SMatthew G. Knepley 
45702e17dfb7SMatthew G. Knepley   Output Parameter:
45712e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
45722e17dfb7SMatthew G. Knepley 
45732e17dfb7SMatthew G. Knepley   Level: developer
45742e17dfb7SMatthew G. Knepley 
45752e17dfb7SMatthew 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
45762e17dfb7SMatthew G. Knepley 
45772e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
45782e17dfb7SMatthew G. Knepley */
45792e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45802e17dfb7SMatthew G. Knepley {
45812e17dfb7SMatthew G. Knepley   PetscInt d;
45822e17dfb7SMatthew G. Knepley 
45832e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45842e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45852e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
45862e17dfb7SMatthew G. Knepley   } else {
45872e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45882e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
45892e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
45902e17dfb7SMatthew G. Knepley       } else {
45912e17dfb7SMatthew G. Knepley         out[d] += in[d];
45922e17dfb7SMatthew G. Knepley       }
45932e17dfb7SMatthew G. Knepley     }
45942e17dfb7SMatthew G. Knepley   }
45952e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45962e17dfb7SMatthew G. Knepley }
45972e17dfb7SMatthew G. Knepley 
45982e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetDepthStratum(DM, PetscInt, PetscInt *, PetscInt *);
45992e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetHeightStratum(DM, PetscInt, PetscInt *, PetscInt *);
46002e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecGetClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
46012e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecRestoreClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
46022e17dfb7SMatthew G. Knepley 
46032e17dfb7SMatthew G. Knepley #undef __FUNCT__
460436447a5eSToby Isaac #define __FUNCT__ "DMGetCoordinatesLocalized"
460536447a5eSToby Isaac /*@
460636447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
460736447a5eSToby Isaac 
460836447a5eSToby Isaac   Input Parameter:
460936447a5eSToby Isaac . dm - The DM
461036447a5eSToby Isaac 
461136447a5eSToby Isaac   Output Parameter:
461236447a5eSToby Isaac   areLocalized - True if localized
461336447a5eSToby Isaac 
461436447a5eSToby Isaac   Level: developer
461536447a5eSToby Isaac 
461636447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
461736447a5eSToby Isaac @*/
461836447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
461936447a5eSToby Isaac {
462036447a5eSToby Isaac   DM             cdm;
462136447a5eSToby Isaac   PetscSection   coordSection;
462236447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
462336447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
462436447a5eSToby Isaac   PetscErrorCode ierr;
462536447a5eSToby Isaac 
462636447a5eSToby Isaac   PetscFunctionBegin;
462736447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46288b09590cSToby Isaac   if (!dm->maxCell) {
46298b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
46308b09590cSToby Isaac     PetscFunctionReturn(0);
46318b09590cSToby Isaac   }
463236447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
463336447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
463436447a5eSToby Isaac   {
463536447a5eSToby Isaac     PetscBool isplex;
463636447a5eSToby Isaac 
463736447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
463836447a5eSToby Isaac     if (isplex) {
463936447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
464036447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
464136447a5eSToby Isaac   }
464236447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
464336447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
464436447a5eSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
464536447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
464636447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
464736447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
464836447a5eSToby Isaac       break;
464936447a5eSToby Isaac     }
465036447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
465136447a5eSToby Isaac     if (!dof) {
465236447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
465336447a5eSToby Isaac       break;
465436447a5eSToby Isaac     }
465536447a5eSToby Isaac   }
465636447a5eSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
465736447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
465836447a5eSToby Isaac   PetscFunctionReturn(0);
465936447a5eSToby Isaac }
466036447a5eSToby Isaac 
466136447a5eSToby Isaac 
466236447a5eSToby Isaac #undef __FUNCT__
46632e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinates"
46642e17dfb7SMatthew G. Knepley /*@
46652e17dfb7SMatthew G. Knepley   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for each cell
46662e17dfb7SMatthew G. Knepley 
46672e17dfb7SMatthew G. Knepley   Input Parameter:
46682e17dfb7SMatthew G. Knepley . dm - The DM
46692e17dfb7SMatthew G. Knepley 
46702e17dfb7SMatthew G. Knepley   Level: developer
46712e17dfb7SMatthew G. Knepley 
46722e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
46732e17dfb7SMatthew G. Knepley @*/
46742e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
46752e17dfb7SMatthew G. Knepley {
46762e17dfb7SMatthew G. Knepley   DM             cdm;
46772e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
46782e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
46792e17dfb7SMatthew G. Knepley   PetscScalar   *coords, *coords2, *anchor;
4680e0ae35bbSToby Isaac   PetscInt       Nc, cStart, cEnd, c, vStart, vEnd, v, sStart, sEnd, dof, d, off, off2, bs, coordSize;
4681e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
46822e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
46832e17dfb7SMatthew G. Knepley 
46842e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46852e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46862e17dfb7SMatthew G. Knepley   if (!dm->maxCell) PetscFunctionReturn(0);
46872e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
46882e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
46892e17dfb7SMatthew G. Knepley   {
46902e17dfb7SMatthew G. Knepley     PetscBool isplex;
46912e17dfb7SMatthew G. Knepley 
46922e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
46932e17dfb7SMatthew G. Knepley     if (isplex) {
46942e17dfb7SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
46952e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
46962e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
46972e17dfb7SMatthew G. Knepley   }
46982e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
46992e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
4700e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
4701e0ae35bbSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
4702e0ae35bbSToby Isaac   for (c = cStart; c < cEnd; ++c) {
4703e0ae35bbSToby Isaac     if (c < sStart || c >= sEnd) {
4704e0ae35bbSToby Isaac       alreadyLocalized = PETSC_FALSE;
4705e0ae35bbSToby Isaac       break;
4706e0ae35bbSToby Isaac     }
4707e0ae35bbSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
4708e0ae35bbSToby Isaac     if (!dof) {
4709e0ae35bbSToby Isaac       alreadyLocalized = PETSC_FALSE;
4710e0ae35bbSToby Isaac       break;
4711e0ae35bbSToby Isaac     }
4712e0ae35bbSToby Isaac   }
4713e0ae35bbSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
4714e0ae35bbSToby Isaac   if (alreadyLocalizedGlobal) PetscFunctionReturn(0);
47152e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
47162e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
47172e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
47182e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
47192e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetChart(cSection, cStart, vEnd);CHKERRQ(ierr);
47202e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
47212e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
47222e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
47232e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
47242e17dfb7SMatthew G. Knepley   }
47252e17dfb7SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
47262e17dfb7SMatthew G. Knepley     ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, NULL);CHKERRQ(ierr);
47272e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
47282e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
47292e17dfb7SMatthew G. Knepley   }
47302e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
47312e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
47322e17dfb7SMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject) dm), &cVec);CHKERRQ(ierr);
47332e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
47342e17dfb7SMatthew G. Knepley   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
47352e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
47362e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
47372e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec,VECSTANDARD);CHKERRQ(ierr);
47382e17dfb7SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
47392e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec,        &coords2);CHKERRQ(ierr);
47402e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
47412e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
47422e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
47432e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
47442e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
47452e17dfb7SMatthew G. Knepley   }
47462e17dfb7SMatthew G. Knepley   ierr = DMGetWorkArray(dm, 3, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47472e17dfb7SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
47482e17dfb7SMatthew G. Knepley     PetscScalar *cellCoords = NULL;
47492e17dfb7SMatthew G. Knepley     PetscInt     b;
47502e17dfb7SMatthew G. Knepley 
47512e17dfb7SMatthew G. Knepley     ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47522e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
47532e17dfb7SMatthew G. Knepley     for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
47542e17dfb7SMatthew G. Knepley     for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
47552e17dfb7SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47562e17dfb7SMatthew G. Knepley   }
47572e17dfb7SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 3, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47582e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
47592e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec,        &coords2);CHKERRQ(ierr);
47602e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
47612e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
47622e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
47632e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
47642e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47652e17dfb7SMatthew G. Knepley }
47662e17dfb7SMatthew G. Knepley 
47672e17dfb7SMatthew G. Knepley #undef __FUNCT__
4768e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4769e87bb0d3SMatthew G Knepley /*@
4770e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4771e87bb0d3SMatthew G Knepley 
4772e87bb0d3SMatthew G Knepley   Not collective
4773e87bb0d3SMatthew G Knepley 
4774e87bb0d3SMatthew G Knepley   Input Parameters:
4775e87bb0d3SMatthew G Knepley + dm - The DM
4776e87bb0d3SMatthew G Knepley - v - The Vec of points
4777e87bb0d3SMatthew G Knepley 
477861e3bb9bSMatthew G Knepley   Output Parameter:
4779e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4780e87bb0d3SMatthew G Knepley 
4781e87bb0d3SMatthew G Knepley   Level: developer
478261e3bb9bSMatthew G Knepley 
478361e3bb9bSMatthew G Knepley .keywords: point location, mesh
478461e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
478561e3bb9bSMatthew G Knepley @*/
4786e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4787e87bb0d3SMatthew G Knepley {
4788735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4789735aa83eSMatthew G Knepley 
4790e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4791e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4792e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4793e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
479447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4795735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4796735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
479782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
479847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4799e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4800e87bb0d3SMatthew G Knepley }
480114f150ffSMatthew G. Knepley 
480214f150ffSMatthew G. Knepley #undef __FUNCT__
480314f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4804f4d763aaSMatthew G. Knepley /*@
4805f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4806f4d763aaSMatthew G. Knepley 
4807f4d763aaSMatthew G. Knepley   Input Parameter:
4808f4d763aaSMatthew G. Knepley . dm - The original DM
4809f4d763aaSMatthew G. Knepley 
4810f4d763aaSMatthew G. Knepley   Output Parameter:
4811f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4812f4d763aaSMatthew G. Knepley 
4813f4d763aaSMatthew G. Knepley   Level: intermediate
4814f4d763aaSMatthew G. Knepley 
4815f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4816f4d763aaSMatthew G. Knepley @*/
481714f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
481814f150ffSMatthew G. Knepley {
4819c26acbdeSMatthew G. Knepley   PetscSection   section;
4820c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
482114f150ffSMatthew G. Knepley   PetscErrorCode ierr;
482214f150ffSMatthew G. Knepley 
482314f150ffSMatthew G. Knepley   PetscFunctionBegin;
482414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
482514f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4826c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4827c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4828c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4829c26acbdeSMatthew G. Knepley     *odm = dm;
4830c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4831c26acbdeSMatthew G. Knepley   }
483214f150ffSMatthew G. Knepley   if (!dm->dmBC) {
48330305aff6SMatthew G. Knepley     PetscDS      ds;
4834c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
483514f150ffSMatthew G. Knepley     PetscSF      sf;
483614f150ffSMatthew G. Knepley 
483714f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
48380305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
48390305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
484014f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
484114f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
484214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
484314f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
484415b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
484514f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
484614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
484714f150ffSMatthew G. Knepley   }
484814f150ffSMatthew G. Knepley   *odm = dm->dmBC;
484914f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
485014f150ffSMatthew G. Knepley }
4851f4d763aaSMatthew G. Knepley 
4852f4d763aaSMatthew G. Knepley #undef __FUNCT__
4853f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4854f4d763aaSMatthew G. Knepley /*@
4855cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4856f4d763aaSMatthew G. Knepley 
4857f4d763aaSMatthew G. Knepley   Input Parameter:
4858f4d763aaSMatthew G. Knepley . dm - The original DM
4859f4d763aaSMatthew G. Knepley 
4860cdb7a50dSMatthew G. Knepley   Output Parameters:
4861cdb7a50dSMatthew G. Knepley + num - The output sequence number
4862cdb7a50dSMatthew G. Knepley - val - The output sequence value
4863f4d763aaSMatthew G. Knepley 
4864f4d763aaSMatthew G. Knepley   Level: intermediate
4865f4d763aaSMatthew G. Knepley 
4866f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4867f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4868f4d763aaSMatthew G. Knepley 
4869f4d763aaSMatthew G. Knepley .seealso: VecView()
4870f4d763aaSMatthew G. Knepley @*/
4871cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4872f4d763aaSMatthew G. Knepley {
4873f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4874f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4875cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4876cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4877f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4878f4d763aaSMatthew G. Knepley }
4879f4d763aaSMatthew G. Knepley 
4880f4d763aaSMatthew G. Knepley #undef __FUNCT__
4881f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4882f4d763aaSMatthew G. Knepley /*@
4883cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4884f4d763aaSMatthew G. Knepley 
4885f4d763aaSMatthew G. Knepley   Input Parameters:
4886f4d763aaSMatthew G. Knepley + dm - The original DM
4887cdb7a50dSMatthew G. Knepley . num - The output sequence number
4888cdb7a50dSMatthew G. Knepley - val - The output sequence value
4889f4d763aaSMatthew G. Knepley 
4890f4d763aaSMatthew G. Knepley   Level: intermediate
4891f4d763aaSMatthew G. Knepley 
4892f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4893f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4894f4d763aaSMatthew G. Knepley 
4895f4d763aaSMatthew G. Knepley .seealso: VecView()
4896f4d763aaSMatthew G. Knepley @*/
4897cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4898f4d763aaSMatthew G. Knepley {
4899f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4900f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4901f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4902cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4903cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4904cdb7a50dSMatthew G. Knepley }
4905cdb7a50dSMatthew G. Knepley 
4906cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4907cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4908cdb7a50dSMatthew G. Knepley /*@C
4909cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4910cdb7a50dSMatthew G. Knepley 
4911cdb7a50dSMatthew G. Knepley   Input Parameters:
4912cdb7a50dSMatthew G. Knepley + dm   - The original DM
4913cdb7a50dSMatthew G. Knepley . name - The sequence name
4914cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4915cdb7a50dSMatthew G. Knepley 
4916cdb7a50dSMatthew G. Knepley   Output Parameter:
4917cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4918cdb7a50dSMatthew G. Knepley 
4919cdb7a50dSMatthew G. Knepley   Level: intermediate
4920cdb7a50dSMatthew G. Knepley 
4921cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4922cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4923cdb7a50dSMatthew G. Knepley 
4924cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4925cdb7a50dSMatthew G. Knepley @*/
4926cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4927cdb7a50dSMatthew G. Knepley {
4928cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4929cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4930cdb7a50dSMatthew G. Knepley 
4931cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4932cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4933cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4934cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4935cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4936cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4937cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4938cdb7a50dSMatthew G. Knepley     PetscScalar value;
4939cdb7a50dSMatthew G. Knepley 
4940cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
49414aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4942cdb7a50dSMatthew G. Knepley #endif
4943cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4944f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4945f4d763aaSMatthew G. Knepley }
49468e4ac7eaSMatthew G. Knepley 
49478e4ac7eaSMatthew G. Knepley #undef __FUNCT__
49488e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
49498e4ac7eaSMatthew G. Knepley /*@
49508e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
49518e4ac7eaSMatthew G. Knepley 
49528e4ac7eaSMatthew G. Knepley   Not collective
49538e4ac7eaSMatthew G. Knepley 
49548e4ac7eaSMatthew G. Knepley   Input Parameter:
49558e4ac7eaSMatthew G. Knepley . dm - The DM
49568e4ac7eaSMatthew G. Knepley 
49578e4ac7eaSMatthew G. Knepley   Output Parameter:
49588e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
49598e4ac7eaSMatthew G. Knepley 
49608e4ac7eaSMatthew G. Knepley   Level: beginner
49618e4ac7eaSMatthew G. Knepley 
49628e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
49638e4ac7eaSMatthew G. Knepley @*/
49648e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
49658e4ac7eaSMatthew G. Knepley {
49668e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
49678e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49688e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
49698e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
49708e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
49718e4ac7eaSMatthew G. Knepley }
49728e4ac7eaSMatthew G. Knepley 
49738e4ac7eaSMatthew G. Knepley #undef __FUNCT__
49748e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
49758e4ac7eaSMatthew G. Knepley /*@
49768e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
49778e4ac7eaSMatthew G. Knepley 
49788e4ac7eaSMatthew G. Knepley   Collective on dm
49798e4ac7eaSMatthew G. Knepley 
49808e4ac7eaSMatthew G. Knepley   Input Parameters:
49818e4ac7eaSMatthew G. Knepley + dm - The DM
49828e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
49838e4ac7eaSMatthew G. Knepley 
49848e4ac7eaSMatthew G. Knepley   Level: beginner
49858e4ac7eaSMatthew G. Knepley 
49868e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
49878e4ac7eaSMatthew G. Knepley @*/
49888e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
49898e4ac7eaSMatthew G. Knepley {
49908e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
49918e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49928e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
49938e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
49948e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
49958e4ac7eaSMatthew G. Knepley }
4996c58f1c22SToby Isaac 
4997c58f1c22SToby Isaac #undef __FUNCT__
4998c58f1c22SToby Isaac #define __FUNCT__
4999c58f1c22SToby Isaac 
5000c58f1c22SToby Isaac #undef __FUNCT__
5001c58f1c22SToby Isaac #define __FUNCT__ "DMCreateLabel"
5002c58f1c22SToby Isaac /*@C
5003c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5004c58f1c22SToby Isaac 
5005c58f1c22SToby Isaac   Not Collective
5006c58f1c22SToby Isaac 
5007c58f1c22SToby Isaac   Input Parameters:
5008c58f1c22SToby Isaac + dm   - The DM object
5009c58f1c22SToby Isaac - name - The label name
5010c58f1c22SToby Isaac 
5011c58f1c22SToby Isaac   Level: intermediate
5012c58f1c22SToby Isaac 
5013c58f1c22SToby Isaac .keywords: mesh
5014c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5015c58f1c22SToby Isaac @*/
5016c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5017c58f1c22SToby Isaac {
5018c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5019c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5020c58f1c22SToby Isaac   PetscErrorCode ierr;
5021c58f1c22SToby Isaac 
5022c58f1c22SToby Isaac   PetscFunctionBegin;
5023c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5024c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5025c58f1c22SToby Isaac   while (next) {
5026c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5027c58f1c22SToby Isaac     if (flg) break;
5028c58f1c22SToby Isaac     next = next->next;
5029c58f1c22SToby Isaac   }
5030c58f1c22SToby Isaac   if (!flg) {
5031c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5032c58f1c22SToby Isaac 
5033c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5034c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5035c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5036c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5037c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5038c58f1c22SToby Isaac   }
5039c58f1c22SToby Isaac   PetscFunctionReturn(0);
5040c58f1c22SToby Isaac }
5041c58f1c22SToby Isaac 
5042c58f1c22SToby Isaac #undef __FUNCT__
5043c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelValue"
5044c58f1c22SToby Isaac /*@C
5045c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5046c58f1c22SToby Isaac 
5047c58f1c22SToby Isaac   Not Collective
5048c58f1c22SToby Isaac 
5049c58f1c22SToby Isaac   Input Parameters:
5050c58f1c22SToby Isaac + dm   - The DM object
5051c58f1c22SToby Isaac . name - The label name
5052c58f1c22SToby Isaac - point - The mesh point
5053c58f1c22SToby Isaac 
5054c58f1c22SToby Isaac   Output Parameter:
5055c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5056c58f1c22SToby Isaac 
5057c58f1c22SToby Isaac   Level: beginner
5058c58f1c22SToby Isaac 
5059c58f1c22SToby Isaac .keywords: mesh
5060c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5061c58f1c22SToby Isaac @*/
5062c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5063c58f1c22SToby Isaac {
5064c58f1c22SToby Isaac   DMLabel        label;
5065c58f1c22SToby Isaac   PetscErrorCode ierr;
5066c58f1c22SToby Isaac 
5067c58f1c22SToby Isaac   PetscFunctionBegin;
5068c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5069c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5070c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5071c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
5072c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5073c58f1c22SToby Isaac   PetscFunctionReturn(0);
5074c58f1c22SToby Isaac }
5075c58f1c22SToby Isaac 
5076c58f1c22SToby Isaac #undef __FUNCT__
5077c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelValue"
5078c58f1c22SToby Isaac /*@C
5079c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5080c58f1c22SToby Isaac 
5081c58f1c22SToby Isaac   Not Collective
5082c58f1c22SToby Isaac 
5083c58f1c22SToby Isaac   Input Parameters:
5084c58f1c22SToby Isaac + dm   - The DM object
5085c58f1c22SToby Isaac . name - The label name
5086c58f1c22SToby Isaac . point - The mesh point
5087c58f1c22SToby Isaac - value - The label value for this point
5088c58f1c22SToby Isaac 
5089c58f1c22SToby Isaac   Output Parameter:
5090c58f1c22SToby Isaac 
5091c58f1c22SToby Isaac   Level: beginner
5092c58f1c22SToby Isaac 
5093c58f1c22SToby Isaac .keywords: mesh
5094c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5095c58f1c22SToby Isaac @*/
5096c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5097c58f1c22SToby Isaac {
5098c58f1c22SToby Isaac   DMLabel        label;
5099c58f1c22SToby Isaac   PetscErrorCode ierr;
5100c58f1c22SToby Isaac 
5101c58f1c22SToby Isaac   PetscFunctionBegin;
5102c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5103c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5104c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5105c58f1c22SToby Isaac   if (!label) {
5106c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5107c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5108c58f1c22SToby Isaac   }
5109c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5110c58f1c22SToby Isaac   PetscFunctionReturn(0);
5111c58f1c22SToby Isaac }
5112c58f1c22SToby Isaac 
5113c58f1c22SToby Isaac #undef __FUNCT__
5114c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelValue"
5115c58f1c22SToby Isaac /*@C
5116c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5117c58f1c22SToby Isaac 
5118c58f1c22SToby Isaac   Not Collective
5119c58f1c22SToby Isaac 
5120c58f1c22SToby Isaac   Input Parameters:
5121c58f1c22SToby Isaac + dm   - The DM object
5122c58f1c22SToby Isaac . name - The label name
5123c58f1c22SToby Isaac . point - The mesh point
5124c58f1c22SToby Isaac - value - The label value for this point
5125c58f1c22SToby Isaac 
5126c58f1c22SToby Isaac   Output Parameter:
5127c58f1c22SToby Isaac 
5128c58f1c22SToby Isaac   Level: beginner
5129c58f1c22SToby Isaac 
5130c58f1c22SToby Isaac .keywords: mesh
5131c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5132c58f1c22SToby Isaac @*/
5133c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5134c58f1c22SToby Isaac {
5135c58f1c22SToby Isaac   DMLabel        label;
5136c58f1c22SToby Isaac   PetscErrorCode ierr;
5137c58f1c22SToby Isaac 
5138c58f1c22SToby Isaac   PetscFunctionBegin;
5139c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5140c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5141c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5142c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5143c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5144c58f1c22SToby Isaac   PetscFunctionReturn(0);
5145c58f1c22SToby Isaac }
5146c58f1c22SToby Isaac 
5147c58f1c22SToby Isaac #undef __FUNCT__
5148c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelSize"
5149c58f1c22SToby Isaac /*@C
5150c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5151c58f1c22SToby Isaac 
5152c58f1c22SToby Isaac   Not Collective
5153c58f1c22SToby Isaac 
5154c58f1c22SToby Isaac   Input Parameters:
5155c58f1c22SToby Isaac + dm   - The DM object
5156c58f1c22SToby Isaac - name - The label name
5157c58f1c22SToby Isaac 
5158c58f1c22SToby Isaac   Output Parameter:
5159c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5160c58f1c22SToby Isaac 
5161c58f1c22SToby Isaac   Level: beginner
5162c58f1c22SToby Isaac 
5163c58f1c22SToby Isaac .keywords: mesh
5164c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5165c58f1c22SToby Isaac @*/
5166c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5167c58f1c22SToby Isaac {
5168c58f1c22SToby Isaac   DMLabel        label;
5169c58f1c22SToby Isaac   PetscErrorCode ierr;
5170c58f1c22SToby Isaac 
5171c58f1c22SToby Isaac   PetscFunctionBegin;
5172c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5173c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5174c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5175c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5176c58f1c22SToby Isaac   *size = 0;
5177c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5178c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5179c58f1c22SToby Isaac   PetscFunctionReturn(0);
5180c58f1c22SToby Isaac }
5181c58f1c22SToby Isaac 
5182c58f1c22SToby Isaac #undef __FUNCT__
5183c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelIdIS"
5184c58f1c22SToby Isaac /*@C
5185c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5186c58f1c22SToby Isaac 
5187c58f1c22SToby Isaac   Not Collective
5188c58f1c22SToby Isaac 
5189c58f1c22SToby Isaac   Input Parameters:
5190c58f1c22SToby Isaac + mesh - The DM object
5191c58f1c22SToby Isaac - name - The label name
5192c58f1c22SToby Isaac 
5193c58f1c22SToby Isaac   Output Parameter:
5194c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5195c58f1c22SToby Isaac 
5196c58f1c22SToby Isaac   Level: beginner
5197c58f1c22SToby Isaac 
5198c58f1c22SToby Isaac .keywords: mesh
5199c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5200c58f1c22SToby Isaac @*/
5201c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5202c58f1c22SToby Isaac {
5203c58f1c22SToby Isaac   DMLabel        label;
5204c58f1c22SToby Isaac   PetscErrorCode ierr;
5205c58f1c22SToby Isaac 
5206c58f1c22SToby Isaac   PetscFunctionBegin;
5207c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5208c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5209c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5210c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5211c58f1c22SToby Isaac   *ids = NULL;
5212c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5213c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5214c58f1c22SToby Isaac   PetscFunctionReturn(0);
5215c58f1c22SToby Isaac }
5216c58f1c22SToby Isaac 
5217c58f1c22SToby Isaac #undef __FUNCT__
5218c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumSize"
5219c58f1c22SToby Isaac /*@C
5220c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5221c58f1c22SToby Isaac 
5222c58f1c22SToby Isaac   Not Collective
5223c58f1c22SToby Isaac 
5224c58f1c22SToby Isaac   Input Parameters:
5225c58f1c22SToby Isaac + dm - The DM object
5226c58f1c22SToby Isaac . name - The label name
5227c58f1c22SToby Isaac - value - The stratum value
5228c58f1c22SToby Isaac 
5229c58f1c22SToby Isaac   Output Parameter:
5230c58f1c22SToby Isaac . size - The stratum size
5231c58f1c22SToby Isaac 
5232c58f1c22SToby Isaac   Level: beginner
5233c58f1c22SToby Isaac 
5234c58f1c22SToby Isaac .keywords: mesh
5235c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5236c58f1c22SToby Isaac @*/
5237c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5238c58f1c22SToby Isaac {
5239c58f1c22SToby Isaac   DMLabel        label;
5240c58f1c22SToby Isaac   PetscErrorCode ierr;
5241c58f1c22SToby Isaac 
5242c58f1c22SToby Isaac   PetscFunctionBegin;
5243c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5244c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5245c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5246c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5247c58f1c22SToby Isaac   *size = 0;
5248c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5249c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5250c58f1c22SToby Isaac   PetscFunctionReturn(0);
5251c58f1c22SToby Isaac }
5252c58f1c22SToby Isaac 
5253c58f1c22SToby Isaac #undef __FUNCT__
5254c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumIS"
5255c58f1c22SToby Isaac /*@C
5256c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5257c58f1c22SToby Isaac 
5258c58f1c22SToby Isaac   Not Collective
5259c58f1c22SToby Isaac 
5260c58f1c22SToby Isaac   Input Parameters:
5261c58f1c22SToby Isaac + dm - The DM object
5262c58f1c22SToby Isaac . name - The label name
5263c58f1c22SToby Isaac - value - The stratum value
5264c58f1c22SToby Isaac 
5265c58f1c22SToby Isaac   Output Parameter:
5266c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5267c58f1c22SToby Isaac 
5268c58f1c22SToby Isaac   Level: beginner
5269c58f1c22SToby Isaac 
5270c58f1c22SToby Isaac .keywords: mesh
5271c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5272c58f1c22SToby Isaac @*/
5273c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5274c58f1c22SToby Isaac {
5275c58f1c22SToby Isaac   DMLabel        label;
5276c58f1c22SToby Isaac   PetscErrorCode ierr;
5277c58f1c22SToby Isaac 
5278c58f1c22SToby Isaac   PetscFunctionBegin;
5279c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5280c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5281c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5282c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5283c58f1c22SToby Isaac   *points = NULL;
5284c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5285c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5286c58f1c22SToby Isaac   PetscFunctionReturn(0);
5287c58f1c22SToby Isaac }
5288c58f1c22SToby Isaac 
5289c58f1c22SToby Isaac #undef __FUNCT__
5290c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelStratum"
5291c58f1c22SToby Isaac /*@C
5292c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5293c58f1c22SToby Isaac 
5294c58f1c22SToby Isaac   Not Collective
5295c58f1c22SToby Isaac 
5296c58f1c22SToby Isaac   Input Parameters:
5297c58f1c22SToby Isaac + dm   - The DM object
5298c58f1c22SToby Isaac . name - The label name
5299c58f1c22SToby Isaac - value - The label value for this point
5300c58f1c22SToby Isaac 
5301c58f1c22SToby Isaac   Output Parameter:
5302c58f1c22SToby Isaac 
5303c58f1c22SToby Isaac   Level: beginner
5304c58f1c22SToby Isaac 
5305c58f1c22SToby Isaac .keywords: mesh
5306c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5307c58f1c22SToby Isaac @*/
5308c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5309c58f1c22SToby Isaac {
5310c58f1c22SToby Isaac   DMLabel        label;
5311c58f1c22SToby Isaac   PetscErrorCode ierr;
5312c58f1c22SToby Isaac 
5313c58f1c22SToby Isaac   PetscFunctionBegin;
5314c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5315c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5316c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5317c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5318c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5319c58f1c22SToby Isaac   PetscFunctionReturn(0);
5320c58f1c22SToby Isaac }
5321c58f1c22SToby Isaac 
5322c58f1c22SToby Isaac #undef __FUNCT__
5323c58f1c22SToby Isaac #define __FUNCT__ "DMGetNumLabels"
5324c58f1c22SToby Isaac /*@
5325c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5326c58f1c22SToby Isaac 
5327c58f1c22SToby Isaac   Not Collective
5328c58f1c22SToby Isaac 
5329c58f1c22SToby Isaac   Input Parameter:
5330c58f1c22SToby Isaac . dm   - The DM object
5331c58f1c22SToby Isaac 
5332c58f1c22SToby Isaac   Output Parameter:
5333c58f1c22SToby Isaac . numLabels - the number of Labels
5334c58f1c22SToby Isaac 
5335c58f1c22SToby Isaac   Level: intermediate
5336c58f1c22SToby Isaac 
5337c58f1c22SToby Isaac .keywords: mesh
5338c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5339c58f1c22SToby Isaac @*/
5340c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5341c58f1c22SToby Isaac {
5342c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5343c58f1c22SToby Isaac   PetscInt  n    = 0;
5344c58f1c22SToby Isaac 
5345c58f1c22SToby Isaac   PetscFunctionBegin;
5346c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5347c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5348c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5349c58f1c22SToby Isaac   *numLabels = n;
5350c58f1c22SToby Isaac   PetscFunctionReturn(0);
5351c58f1c22SToby Isaac }
5352c58f1c22SToby Isaac 
5353c58f1c22SToby Isaac #undef __FUNCT__
5354c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelName"
5355c58f1c22SToby Isaac /*@C
5356c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5357c58f1c22SToby Isaac 
5358c58f1c22SToby Isaac   Not Collective
5359c58f1c22SToby Isaac 
5360c58f1c22SToby Isaac   Input Parameters:
5361c58f1c22SToby Isaac + dm - The DM object
5362c58f1c22SToby Isaac - n  - the label number
5363c58f1c22SToby Isaac 
5364c58f1c22SToby Isaac   Output Parameter:
5365c58f1c22SToby Isaac . name - the label name
5366c58f1c22SToby Isaac 
5367c58f1c22SToby Isaac   Level: intermediate
5368c58f1c22SToby Isaac 
5369c58f1c22SToby Isaac .keywords: mesh
5370c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5371c58f1c22SToby Isaac @*/
5372c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5373c58f1c22SToby Isaac {
5374c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5375c58f1c22SToby Isaac   PetscInt  l    = 0;
5376c58f1c22SToby Isaac 
5377c58f1c22SToby Isaac   PetscFunctionBegin;
5378c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5379c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5380c58f1c22SToby Isaac   while (next) {
5381c58f1c22SToby Isaac     if (l == n) {
5382c58f1c22SToby Isaac       *name = next->label->name;
5383c58f1c22SToby Isaac       PetscFunctionReturn(0);
5384c58f1c22SToby Isaac     }
5385c58f1c22SToby Isaac     ++l;
5386c58f1c22SToby Isaac     next = next->next;
5387c58f1c22SToby Isaac   }
5388c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5389c58f1c22SToby Isaac }
5390c58f1c22SToby Isaac 
5391c58f1c22SToby Isaac #undef __FUNCT__
5392c58f1c22SToby Isaac #define __FUNCT__ "DMHasLabel"
5393c58f1c22SToby Isaac /*@C
5394c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5395c58f1c22SToby Isaac 
5396c58f1c22SToby Isaac   Not Collective
5397c58f1c22SToby Isaac 
5398c58f1c22SToby Isaac   Input Parameters:
5399c58f1c22SToby Isaac + dm   - The DM object
5400c58f1c22SToby Isaac - name - The label name
5401c58f1c22SToby Isaac 
5402c58f1c22SToby Isaac   Output Parameter:
5403c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5404c58f1c22SToby Isaac 
5405c58f1c22SToby Isaac   Level: intermediate
5406c58f1c22SToby Isaac 
5407c58f1c22SToby Isaac .keywords: mesh
5408c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5409c58f1c22SToby Isaac @*/
5410c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5411c58f1c22SToby Isaac {
5412c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5413c58f1c22SToby Isaac   PetscErrorCode ierr;
5414c58f1c22SToby Isaac 
5415c58f1c22SToby Isaac   PetscFunctionBegin;
5416c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5417c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5418c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5419c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5420c58f1c22SToby Isaac   while (next) {
5421c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5422c58f1c22SToby Isaac     if (*hasLabel) break;
5423c58f1c22SToby Isaac     next = next->next;
5424c58f1c22SToby Isaac   }
5425c58f1c22SToby Isaac   PetscFunctionReturn(0);
5426c58f1c22SToby Isaac }
5427c58f1c22SToby Isaac 
5428c58f1c22SToby Isaac #undef __FUNCT__
5429c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabel"
5430c58f1c22SToby Isaac /*@C
5431c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5432c58f1c22SToby Isaac 
5433c58f1c22SToby Isaac   Not Collective
5434c58f1c22SToby Isaac 
5435c58f1c22SToby Isaac   Input Parameters:
5436c58f1c22SToby Isaac + dm   - The DM object
5437c58f1c22SToby Isaac - name - The label name
5438c58f1c22SToby Isaac 
5439c58f1c22SToby Isaac   Output Parameter:
5440c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5441c58f1c22SToby Isaac 
5442c58f1c22SToby Isaac   Level: intermediate
5443c58f1c22SToby Isaac 
5444c58f1c22SToby Isaac .keywords: mesh
5445c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5446c58f1c22SToby Isaac @*/
5447c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5448c58f1c22SToby Isaac {
5449c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5450c58f1c22SToby Isaac   PetscBool      hasLabel;
5451c58f1c22SToby Isaac   PetscErrorCode ierr;
5452c58f1c22SToby Isaac 
5453c58f1c22SToby Isaac   PetscFunctionBegin;
5454c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5455c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5456c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5457c58f1c22SToby Isaac   *label = NULL;
5458c58f1c22SToby Isaac   while (next) {
5459c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5460c58f1c22SToby Isaac     if (hasLabel) {
5461c58f1c22SToby Isaac       *label = next->label;
5462c58f1c22SToby Isaac       break;
5463c58f1c22SToby Isaac     }
5464c58f1c22SToby Isaac     next = next->next;
5465c58f1c22SToby Isaac   }
5466c58f1c22SToby Isaac   PetscFunctionReturn(0);
5467c58f1c22SToby Isaac }
5468c58f1c22SToby Isaac 
5469c58f1c22SToby Isaac #undef __FUNCT__
5470c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelByNum"
5471c58f1c22SToby Isaac /*@C
5472c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5473c58f1c22SToby Isaac 
5474c58f1c22SToby Isaac   Not Collective
5475c58f1c22SToby Isaac 
5476c58f1c22SToby Isaac   Input Parameters:
5477c58f1c22SToby Isaac + dm - The DM object
5478c58f1c22SToby Isaac - n  - the label number
5479c58f1c22SToby Isaac 
5480c58f1c22SToby Isaac   Output Parameter:
5481c58f1c22SToby Isaac . label - the label
5482c58f1c22SToby Isaac 
5483c58f1c22SToby Isaac   Level: intermediate
5484c58f1c22SToby Isaac 
5485c58f1c22SToby Isaac .keywords: mesh
5486c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5487c58f1c22SToby Isaac @*/
5488c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5489c58f1c22SToby Isaac {
5490c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5491c58f1c22SToby Isaac   PetscInt    l    = 0;
5492c58f1c22SToby Isaac 
5493c58f1c22SToby Isaac   PetscFunctionBegin;
5494c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5495c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5496c58f1c22SToby Isaac   while (next) {
5497c58f1c22SToby Isaac     if (l == n) {
5498c58f1c22SToby Isaac       *label = next->label;
5499c58f1c22SToby Isaac       PetscFunctionReturn(0);
5500c58f1c22SToby Isaac     }
5501c58f1c22SToby Isaac     ++l;
5502c58f1c22SToby Isaac     next = next->next;
5503c58f1c22SToby Isaac   }
5504c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5505c58f1c22SToby Isaac }
5506c58f1c22SToby Isaac 
5507c58f1c22SToby Isaac #undef __FUNCT__
5508c58f1c22SToby Isaac #define __FUNCT__ "DMAddLabel"
5509c58f1c22SToby Isaac /*@C
5510c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5511c58f1c22SToby Isaac 
5512c58f1c22SToby Isaac   Not Collective
5513c58f1c22SToby Isaac 
5514c58f1c22SToby Isaac   Input Parameters:
5515c58f1c22SToby Isaac + dm   - The DM object
5516c58f1c22SToby Isaac - label - The DMLabel
5517c58f1c22SToby Isaac 
5518c58f1c22SToby Isaac   Level: developer
5519c58f1c22SToby Isaac 
5520c58f1c22SToby Isaac .keywords: mesh
5521c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5522c58f1c22SToby Isaac @*/
5523c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5524c58f1c22SToby Isaac {
5525c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5526c58f1c22SToby Isaac   PetscBool      hasLabel;
5527c58f1c22SToby Isaac   PetscErrorCode ierr;
5528c58f1c22SToby Isaac 
5529c58f1c22SToby Isaac   PetscFunctionBegin;
5530c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5531c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5532c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5533c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5534c58f1c22SToby Isaac   tmpLabel->label  = label;
5535c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5536c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5537c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5538c58f1c22SToby Isaac   PetscFunctionReturn(0);
5539c58f1c22SToby Isaac }
5540c58f1c22SToby Isaac 
5541c58f1c22SToby Isaac #undef __FUNCT__
5542c58f1c22SToby Isaac #define __FUNCT__ "DMRemoveLabel"
5543c58f1c22SToby Isaac /*@C
5544c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5545c58f1c22SToby Isaac 
5546c58f1c22SToby Isaac   Not Collective
5547c58f1c22SToby Isaac 
5548c58f1c22SToby Isaac   Input Parameters:
5549c58f1c22SToby Isaac + dm   - The DM object
5550c58f1c22SToby Isaac - name - The label name
5551c58f1c22SToby Isaac 
5552c58f1c22SToby Isaac   Output Parameter:
5553c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5554c58f1c22SToby Isaac 
5555c58f1c22SToby Isaac   Level: developer
5556c58f1c22SToby Isaac 
5557c58f1c22SToby Isaac .keywords: mesh
5558c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5559c58f1c22SToby Isaac @*/
5560c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5561c58f1c22SToby Isaac {
5562c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5563c58f1c22SToby Isaac   DMLabelLink    last = NULL;
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, name, &hasLabel);CHKERRQ(ierr);
5570c58f1c22SToby Isaac   *label = NULL;
5571c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5572c58f1c22SToby Isaac   while (next) {
5573c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5574c58f1c22SToby Isaac     if (hasLabel) {
5575c58f1c22SToby Isaac       if (last) last->next       = next->next;
5576c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5577c58f1c22SToby Isaac       next->next = NULL;
5578c58f1c22SToby Isaac       *label     = next->label;
5579c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5580c58f1c22SToby Isaac       if (hasLabel) {
5581c58f1c22SToby Isaac         dm->depthLabel = NULL;
5582c58f1c22SToby Isaac       }
5583c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5584c58f1c22SToby Isaac       break;
5585c58f1c22SToby Isaac     }
5586c58f1c22SToby Isaac     last = next;
5587c58f1c22SToby Isaac     next = next->next;
5588c58f1c22SToby Isaac   }
5589c58f1c22SToby Isaac   PetscFunctionReturn(0);
5590c58f1c22SToby Isaac }
5591c58f1c22SToby Isaac 
5592c58f1c22SToby Isaac #undef __FUNCT__
5593c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelOutput"
5594c58f1c22SToby Isaac /*@C
5595c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5596c58f1c22SToby Isaac 
5597c58f1c22SToby Isaac   Not Collective
5598c58f1c22SToby Isaac 
5599c58f1c22SToby Isaac   Input Parameters:
5600c58f1c22SToby Isaac + dm   - The DM object
5601c58f1c22SToby Isaac - name - The label name
5602c58f1c22SToby Isaac 
5603c58f1c22SToby Isaac   Output Parameter:
5604c58f1c22SToby Isaac . output - The flag for output
5605c58f1c22SToby Isaac 
5606c58f1c22SToby Isaac   Level: developer
5607c58f1c22SToby Isaac 
5608c58f1c22SToby Isaac .keywords: mesh
5609c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5610c58f1c22SToby Isaac @*/
5611c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5612c58f1c22SToby Isaac {
5613c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5614c58f1c22SToby Isaac   PetscErrorCode ierr;
5615c58f1c22SToby Isaac 
5616c58f1c22SToby Isaac   PetscFunctionBegin;
5617c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5618c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5619c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5620c58f1c22SToby Isaac   while (next) {
5621c58f1c22SToby Isaac     PetscBool flg;
5622c58f1c22SToby Isaac 
5623c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5624c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5625c58f1c22SToby Isaac     next = next->next;
5626c58f1c22SToby Isaac   }
5627c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5628c58f1c22SToby Isaac }
5629c58f1c22SToby Isaac 
5630c58f1c22SToby Isaac #undef __FUNCT__
5631c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelOutput"
5632c58f1c22SToby Isaac /*@C
5633c58f1c22SToby Isaac   DMSetLabelOutput - Set 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 - output - The flag for output
5641c58f1c22SToby Isaac 
5642c58f1c22SToby Isaac   Level: developer
5643c58f1c22SToby Isaac 
5644c58f1c22SToby Isaac .keywords: mesh
5645c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5646c58f1c22SToby Isaac @*/
5647c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5648c58f1c22SToby Isaac {
5649c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5650c58f1c22SToby Isaac   PetscErrorCode ierr;
5651c58f1c22SToby Isaac 
5652c58f1c22SToby Isaac   PetscFunctionBegin;
5653c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5654c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5655c58f1c22SToby Isaac   while (next) {
5656c58f1c22SToby Isaac     PetscBool flg;
5657c58f1c22SToby Isaac 
5658c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5659c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5660c58f1c22SToby Isaac     next = next->next;
5661c58f1c22SToby Isaac   }
5662c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5663c58f1c22SToby Isaac }
5664c58f1c22SToby Isaac 
5665c58f1c22SToby Isaac 
5666c58f1c22SToby Isaac #undef __FUNCT__
5667c58f1c22SToby Isaac #define __FUNCT__ "DMCopyLabels"
5668c58f1c22SToby Isaac /*@
5669c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5670c58f1c22SToby Isaac 
5671c58f1c22SToby Isaac   Collective on DM
5672c58f1c22SToby Isaac 
5673c58f1c22SToby Isaac   Input Parameter:
56742e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5675c58f1c22SToby Isaac 
5676c58f1c22SToby Isaac   Output Parameter:
56772e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5678c58f1c22SToby Isaac 
5679c58f1c22SToby Isaac   Level: intermediate
5680c58f1c22SToby Isaac 
5681c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5682c58f1c22SToby Isaac 
5683c58f1c22SToby Isaac .keywords: mesh
5684c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5685c58f1c22SToby Isaac @*/
5686c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5687c58f1c22SToby Isaac {
5688c58f1c22SToby Isaac   PetscInt       numLabels, l;
5689c58f1c22SToby Isaac   PetscErrorCode ierr;
5690c58f1c22SToby Isaac 
5691c58f1c22SToby Isaac   PetscFunctionBegin;
5692c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5693c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5694c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5695c58f1c22SToby Isaac     DMLabel     label, labelNew;
5696c58f1c22SToby Isaac     const char *name;
5697c58f1c22SToby Isaac     PetscBool   flg;
5698c58f1c22SToby Isaac 
5699c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5700c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5701c58f1c22SToby Isaac     if (flg) continue;
5702c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5703c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5704c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5705c58f1c22SToby Isaac   }
5706c58f1c22SToby Isaac   PetscFunctionReturn(0);
5707c58f1c22SToby Isaac }
5708a8fb8f29SToby Isaac 
5709a8fb8f29SToby Isaac #undef __FUNCT__
5710a8fb8f29SToby Isaac #define __FUNCT__ "DMGetCoarseDM"
5711a8fb8f29SToby Isaac /*@
5712a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5713a8fb8f29SToby Isaac 
5714a8fb8f29SToby Isaac   Input Parameter:
5715a8fb8f29SToby Isaac . dm - The DM object
5716a8fb8f29SToby Isaac 
5717a8fb8f29SToby Isaac   Output Parameter:
5718a8fb8f29SToby Isaac . cdm - The coarse DM
5719a8fb8f29SToby Isaac 
5720a8fb8f29SToby Isaac   Level: intermediate
5721a8fb8f29SToby Isaac 
5722a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5723a8fb8f29SToby Isaac @*/
5724a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5725a8fb8f29SToby Isaac {
5726a8fb8f29SToby Isaac   PetscFunctionBegin;
5727a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5728a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5729a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5730a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5731a8fb8f29SToby Isaac }
5732a8fb8f29SToby Isaac 
5733a8fb8f29SToby Isaac #undef __FUNCT__
5734a8fb8f29SToby Isaac #define __FUNCT__ "DMSetCoarseDM"
5735a8fb8f29SToby Isaac /*@
5736a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5737a8fb8f29SToby Isaac 
5738a8fb8f29SToby Isaac   Input Parameters:
5739a8fb8f29SToby Isaac + dm - The DM object
5740a8fb8f29SToby Isaac - cdm - The coarse DM
5741a8fb8f29SToby Isaac 
5742a8fb8f29SToby Isaac   Level: intermediate
5743a8fb8f29SToby Isaac 
5744a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5745a8fb8f29SToby Isaac @*/
5746a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5747a8fb8f29SToby Isaac {
5748a8fb8f29SToby Isaac   PetscErrorCode ierr;
5749a8fb8f29SToby Isaac 
5750a8fb8f29SToby Isaac   PetscFunctionBegin;
5751a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5752a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5753a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5754a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5755a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5756a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5757a8fb8f29SToby Isaac }
5758a8fb8f29SToby Isaac 
575988bdff64SToby Isaac #undef __FUNCT__
576088bdff64SToby Isaac #define __FUNCT__ "DMGetFineDM"
576188bdff64SToby Isaac /*@
576288bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
576388bdff64SToby Isaac 
576488bdff64SToby Isaac   Input Parameter:
576588bdff64SToby Isaac . dm - The DM object
576688bdff64SToby Isaac 
576788bdff64SToby Isaac   Output Parameter:
576888bdff64SToby Isaac . fdm - The fine DM
576988bdff64SToby Isaac 
577088bdff64SToby Isaac   Level: intermediate
577188bdff64SToby Isaac 
577288bdff64SToby Isaac .seealso: DMSetFineDM()
577388bdff64SToby Isaac @*/
577488bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
577588bdff64SToby Isaac {
577688bdff64SToby Isaac   PetscFunctionBegin;
577788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
577888bdff64SToby Isaac   PetscValidPointer(fdm, 2);
577988bdff64SToby Isaac   *fdm = dm->fineMesh;
578088bdff64SToby Isaac   PetscFunctionReturn(0);
578188bdff64SToby Isaac }
578288bdff64SToby Isaac 
578388bdff64SToby Isaac #undef __FUNCT__
578488bdff64SToby Isaac #define __FUNCT__ "DMSetFineDM"
578588bdff64SToby Isaac /*@
578688bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
578788bdff64SToby Isaac 
578888bdff64SToby Isaac   Input Parameters:
578988bdff64SToby Isaac + dm - The DM object
579088bdff64SToby Isaac - fdm - The fine DM
579188bdff64SToby Isaac 
579288bdff64SToby Isaac   Level: intermediate
579388bdff64SToby Isaac 
579488bdff64SToby Isaac .seealso: DMGetFineDM()
579588bdff64SToby Isaac @*/
579688bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
579788bdff64SToby Isaac {
579888bdff64SToby Isaac   PetscErrorCode ierr;
579988bdff64SToby Isaac 
580088bdff64SToby Isaac   PetscFunctionBegin;
580188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
580288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
580388bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
580488bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
580588bdff64SToby Isaac   dm->fineMesh = fdm;
580688bdff64SToby Isaac   PetscFunctionReturn(0);
580788bdff64SToby Isaac }
580888bdff64SToby Isaac 
5809a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5810a6ba4734SToby Isaac 
5811a6ba4734SToby Isaac #undef __FUNCT__
5812bcc5ffd9SToby Isaac #define __FUNCT__ "DMBoundaryDuplicate"
5813354557abSToby Isaac PetscErrorCode DMBoundaryDuplicate(DMBoundaryLinkList bd, DMBoundaryLinkList *boundary)
5814a6ba4734SToby Isaac {
5815bcc5ffd9SToby Isaac   DMBoundary     b = bd->next, b2, bold = NULL;
5816a6ba4734SToby Isaac   PetscErrorCode ierr;
5817a6ba4734SToby Isaac 
5818a6ba4734SToby Isaac   PetscFunctionBegin;
5819bcc5ffd9SToby Isaac   ierr = PetscNew(boundary);CHKERRQ(ierr);
5820bcc5ffd9SToby Isaac   (*boundary)->refct = 1;
5821bcc5ffd9SToby Isaac   (*boundary)->next = NULL;
5822a6ba4734SToby Isaac   for (; b; b = b->next, bold = b2) {
5823a6ba4734SToby Isaac     ierr = PetscNew(&b2);CHKERRQ(ierr);
5824a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
5825a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
5826a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
5827a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
5828a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numcomps, &b2->comps);CHKERRQ(ierr);
5829a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->comps, b->comps, b->numcomps*sizeof(PetscInt));CHKERRQ(ierr);
5830a6ba4734SToby Isaac     b2->label     = NULL;
5831a6ba4734SToby Isaac     b2->essential = b->essential;
5832a6ba4734SToby Isaac     b2->field     = b->field;
5833a6ba4734SToby Isaac     b2->numcomps  = b->numcomps;
5834a6ba4734SToby Isaac     b2->func      = b->func;
5835a6ba4734SToby Isaac     b2->numids    = b->numids;
5836a6ba4734SToby Isaac     b2->ctx       = b->ctx;
5837a6ba4734SToby Isaac     b2->next      = NULL;
5838bcc5ffd9SToby Isaac     if (!(*boundary)->next) (*boundary)->next   = b2;
5839a6ba4734SToby Isaac     if (bold)        bold->next = b2;
5840a6ba4734SToby Isaac   }
5841a6ba4734SToby Isaac   PetscFunctionReturn(0);
5842a6ba4734SToby Isaac }
5843a6ba4734SToby Isaac 
5844a6ba4734SToby Isaac #undef __FUNCT__
5845bcc5ffd9SToby Isaac #define __FUNCT__ "DMBoundaryDestroy"
5846bcc5ffd9SToby Isaac PetscErrorCode DMBoundaryDestroy(DMBoundaryLinkList *boundary)
5847a6ba4734SToby Isaac {
5848a6ba4734SToby Isaac   DMBoundary     b, next;
5849a6ba4734SToby Isaac   PetscErrorCode ierr;
5850a6ba4734SToby Isaac 
5851a6ba4734SToby Isaac   PetscFunctionBegin;
5852a6ba4734SToby Isaac   if (!boundary) PetscFunctionReturn(0);
5853bcc5ffd9SToby Isaac   if (--((*boundary)->refct)) {
5854a6ba4734SToby Isaac     *boundary = NULL;
5855bcc5ffd9SToby Isaac     PetscFunctionReturn(0);
5856bcc5ffd9SToby Isaac   }
5857bcc5ffd9SToby Isaac   b = (*boundary)->next;
5858a6ba4734SToby Isaac   for (; b; b = next) {
5859a6ba4734SToby Isaac     next = b->next;
5860a6ba4734SToby Isaac     ierr = PetscFree(b->comps);CHKERRQ(ierr);
5861a6ba4734SToby Isaac     ierr = PetscFree(b->ids);CHKERRQ(ierr);
5862a6ba4734SToby Isaac     ierr = PetscFree(b->name);CHKERRQ(ierr);
5863a6ba4734SToby Isaac     ierr = PetscFree(b->labelname);CHKERRQ(ierr);
5864a6ba4734SToby Isaac     ierr = PetscFree(b);CHKERRQ(ierr);
5865a6ba4734SToby Isaac   }
5866bcc5ffd9SToby Isaac   ierr = PetscFree(*boundary);CHKERRQ(ierr);
5867a6ba4734SToby Isaac   PetscFunctionReturn(0);
5868a6ba4734SToby Isaac }
5869a6ba4734SToby Isaac 
5870a6ba4734SToby Isaac #undef __FUNCT__
5871a6ba4734SToby Isaac #define __FUNCT__ "DMCopyBoundary"
5872a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5873a6ba4734SToby Isaac {
5874a6ba4734SToby Isaac   DMBoundary     b;
5875a6ba4734SToby Isaac   PetscErrorCode ierr;
5876a6ba4734SToby Isaac 
5877a6ba4734SToby Isaac   PetscFunctionBegin;
5878bcc5ffd9SToby Isaac   ierr = DMBoundaryDestroy(&dmNew->boundary);CHKERRQ(ierr);
5879bcc5ffd9SToby Isaac   ierr = DMBoundaryDuplicate(dm->boundary, &dmNew->boundary);CHKERRQ(ierr);
5880bcc5ffd9SToby Isaac   for (b = dmNew->boundary->next; b; b = b->next) {
5881a6ba4734SToby Isaac     if (b->labelname) {
5882a6ba4734SToby Isaac       ierr = DMGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
5883a6ba4734SToby Isaac       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5884a6ba4734SToby Isaac     }
5885a6ba4734SToby Isaac   }
5886a6ba4734SToby Isaac   PetscFunctionReturn(0);
5887a6ba4734SToby Isaac }
5888a6ba4734SToby Isaac 
5889a6ba4734SToby Isaac #undef __FUNCT__
5890a6ba4734SToby Isaac #define __FUNCT__ "DMAddBoundary"
5891a6ba4734SToby Isaac /*@C
5892a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5893a6ba4734SToby Isaac 
5894a6ba4734SToby Isaac   Input Parameters:
5895a6ba4734SToby Isaac + dm          - The mesh object
5896a6ba4734SToby Isaac . isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5897a6ba4734SToby Isaac . name        - The BC name
5898a6ba4734SToby Isaac . labelname   - The label defining constrained points
5899a6ba4734SToby Isaac . field       - The field to constrain
5900a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5901a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5902a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5903a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5904a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5905a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5906a6ba4734SToby Isaac 
5907a6ba4734SToby Isaac   Options Database Keys:
5908a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5909a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5910a6ba4734SToby Isaac 
5911a6ba4734SToby Isaac   Level: developer
5912a6ba4734SToby Isaac 
5913a6ba4734SToby Isaac .seealso: DMGetBoundary()
5914a6ba4734SToby Isaac @*/
5915a6ba4734SToby 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)
5916a6ba4734SToby Isaac {
5917a6ba4734SToby Isaac   DMBoundary     b;
5918a6ba4734SToby Isaac   PetscErrorCode ierr;
5919a6ba4734SToby Isaac 
5920a6ba4734SToby Isaac   PetscFunctionBegin;
5921a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5922a6ba4734SToby Isaac   ierr = PetscNew(&b);CHKERRQ(ierr);
5923a6ba4734SToby Isaac   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
5924a6ba4734SToby Isaac   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
5925a6ba4734SToby Isaac   ierr = PetscMalloc1(numcomps, &b->comps);CHKERRQ(ierr);
5926a6ba4734SToby Isaac   if (numcomps) {ierr = PetscMemcpy(b->comps, comps, numcomps*sizeof(PetscInt));CHKERRQ(ierr);}
5927a6ba4734SToby Isaac   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
5928a6ba4734SToby Isaac   if (numids) {ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);}
5929a6ba4734SToby Isaac   if (b->labelname) {
5930a6ba4734SToby Isaac     ierr = DMGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
5931a6ba4734SToby Isaac     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5932a6ba4734SToby Isaac   }
5933a6ba4734SToby Isaac   b->essential       = isEssential;
5934a6ba4734SToby Isaac   b->field           = field;
5935a6ba4734SToby Isaac   b->numcomps        = numcomps;
5936a6ba4734SToby Isaac   b->func            = bcFunc;
5937a6ba4734SToby Isaac   b->numids          = numids;
5938a6ba4734SToby Isaac   b->ctx             = ctx;
5939bcc5ffd9SToby Isaac   b->next            = dm->boundary->next;
5940bcc5ffd9SToby Isaac   dm->boundary->next = b;
5941a6ba4734SToby Isaac   PetscFunctionReturn(0);
5942a6ba4734SToby Isaac }
5943a6ba4734SToby Isaac 
5944a6ba4734SToby Isaac #undef __FUNCT__
5945a6ba4734SToby Isaac #define __FUNCT__ "DMGetNumBoundary"
5946a6ba4734SToby Isaac /*@
5947a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5948a6ba4734SToby Isaac 
5949a6ba4734SToby Isaac   Input Parameters:
5950a6ba4734SToby Isaac . dm - The mesh object
5951a6ba4734SToby Isaac 
5952a6ba4734SToby Isaac   Output Parameters:
5953a6ba4734SToby Isaac . numBd - The number of BC
5954a6ba4734SToby Isaac 
5955a6ba4734SToby Isaac   Level: intermediate
5956a6ba4734SToby Isaac 
5957a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5958a6ba4734SToby Isaac @*/
5959a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5960a6ba4734SToby Isaac {
5961bcc5ffd9SToby Isaac   DMBoundary b = dm->boundary->next;
5962a6ba4734SToby Isaac 
5963a6ba4734SToby Isaac   PetscFunctionBegin;
5964a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5965a6ba4734SToby Isaac   PetscValidPointer(numBd, 2);
5966a6ba4734SToby Isaac   *numBd = 0;
5967a6ba4734SToby Isaac   while (b) {++(*numBd); b = b->next;}
5968a6ba4734SToby Isaac   PetscFunctionReturn(0);
5969a6ba4734SToby Isaac }
5970a6ba4734SToby Isaac 
5971a6ba4734SToby Isaac #undef __FUNCT__
5972a6ba4734SToby Isaac #define __FUNCT__ "DMGetBoundary"
5973a6ba4734SToby Isaac /*@C
5974a6ba4734SToby Isaac   DMGetBoundary - Add a boundary condition to the model
5975a6ba4734SToby Isaac 
5976a6ba4734SToby Isaac   Input Parameters:
5977a6ba4734SToby Isaac + dm          - The mesh object
5978a6ba4734SToby Isaac - bd          - The BC number
5979a6ba4734SToby Isaac 
5980a6ba4734SToby Isaac   Output Parameters:
5981a6ba4734SToby Isaac + isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5982a6ba4734SToby Isaac . name        - The BC name
5983a6ba4734SToby Isaac . labelname   - The label defining constrained points
5984a6ba4734SToby Isaac . field       - The field to constrain
5985a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5986a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5987a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5988a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5989a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5990a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5991a6ba4734SToby Isaac 
5992a6ba4734SToby Isaac   Options Database Keys:
5993a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5994a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5995a6ba4734SToby Isaac 
5996a6ba4734SToby Isaac   Level: developer
5997a6ba4734SToby Isaac 
5998a6ba4734SToby Isaac .seealso: DMAddBoundary()
5999a6ba4734SToby Isaac @*/
6000a6ba4734SToby 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)
6001a6ba4734SToby Isaac {
6002bcc5ffd9SToby Isaac   DMBoundary b    = dm->boundary->next;
6003a6ba4734SToby Isaac   PetscInt   n    = 0;
6004a6ba4734SToby Isaac 
6005a6ba4734SToby Isaac   PetscFunctionBegin;
6006a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6007a6ba4734SToby Isaac   while (b) {
6008a6ba4734SToby Isaac     if (n == bd) break;
6009a6ba4734SToby Isaac     b = b->next;
6010a6ba4734SToby Isaac     ++n;
6011a6ba4734SToby Isaac   }
6012a6ba4734SToby Isaac   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
6013a6ba4734SToby Isaac   if (isEssential) {
6014a6ba4734SToby Isaac     PetscValidPointer(isEssential, 3);
6015a6ba4734SToby Isaac     *isEssential = b->essential;
6016a6ba4734SToby Isaac   }
6017a6ba4734SToby Isaac   if (name) {
6018a6ba4734SToby Isaac     PetscValidPointer(name, 4);
6019a6ba4734SToby Isaac     *name = b->name;
6020a6ba4734SToby Isaac   }
6021a6ba4734SToby Isaac   if (labelname) {
6022a6ba4734SToby Isaac     PetscValidPointer(labelname, 5);
6023a6ba4734SToby Isaac     *labelname = b->labelname;
6024a6ba4734SToby Isaac   }
6025a6ba4734SToby Isaac   if (field) {
6026a6ba4734SToby Isaac     PetscValidPointer(field, 6);
6027a6ba4734SToby Isaac     *field = b->field;
6028a6ba4734SToby Isaac   }
6029a6ba4734SToby Isaac   if (numcomps) {
6030a6ba4734SToby Isaac     PetscValidPointer(numcomps, 7);
6031a6ba4734SToby Isaac     *numcomps = b->numcomps;
6032a6ba4734SToby Isaac   }
6033a6ba4734SToby Isaac   if (comps) {
6034a6ba4734SToby Isaac     PetscValidPointer(comps, 8);
6035a6ba4734SToby Isaac     *comps = b->comps;
6036a6ba4734SToby Isaac   }
6037a6ba4734SToby Isaac   if (func) {
6038a6ba4734SToby Isaac     PetscValidPointer(func, 9);
6039a6ba4734SToby Isaac     *func = b->func;
6040a6ba4734SToby Isaac   }
6041a6ba4734SToby Isaac   if (numids) {
6042a6ba4734SToby Isaac     PetscValidPointer(numids, 10);
6043a6ba4734SToby Isaac     *numids = b->numids;
6044a6ba4734SToby Isaac   }
6045a6ba4734SToby Isaac   if (ids) {
6046a6ba4734SToby Isaac     PetscValidPointer(ids, 11);
6047a6ba4734SToby Isaac     *ids = b->ids;
6048a6ba4734SToby Isaac   }
6049a6ba4734SToby Isaac   if (ctx) {
6050a6ba4734SToby Isaac     PetscValidPointer(ctx, 12);
6051a6ba4734SToby Isaac     *ctx = b->ctx;
6052a6ba4734SToby Isaac   }
6053a6ba4734SToby Isaac   PetscFunctionReturn(0);
6054a6ba4734SToby Isaac }
6055a6ba4734SToby Isaac 
6056a6ba4734SToby Isaac #undef __FUNCT__
6057a6ba4734SToby Isaac #define __FUNCT__ "DMIsBoundaryPoint"
6058a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6059a6ba4734SToby Isaac {
6060bcc5ffd9SToby Isaac   DMBoundary     b    = dm->boundary->next;
6061a6ba4734SToby Isaac   PetscErrorCode ierr;
6062a6ba4734SToby Isaac 
6063a6ba4734SToby Isaac   PetscFunctionBegin;
6064a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6065a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6066a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6067a6ba4734SToby Isaac   while (b && !(*isBd)) {
6068a6ba4734SToby Isaac     if (b->label) {
6069a6ba4734SToby Isaac       PetscInt i;
6070a6ba4734SToby Isaac 
6071a6ba4734SToby Isaac       for (i = 0; i < b->numids && !(*isBd); ++i) {
6072a6ba4734SToby Isaac         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
6073a6ba4734SToby Isaac       }
6074a6ba4734SToby Isaac     }
6075a6ba4734SToby Isaac     b = b->next;
6076a6ba4734SToby Isaac   }
6077a6ba4734SToby Isaac   PetscFunctionReturn(0);
6078a6ba4734SToby Isaac }
60794d6f44ffSToby Isaac 
60804d6f44ffSToby Isaac #undef __FUNCT__
60814d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunction"
60824d6f44ffSToby Isaac /*@C
60834d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
60844d6f44ffSToby Isaac 
60854d6f44ffSToby Isaac   Input Parameters:
60864d6f44ffSToby Isaac + dm      - The DM
60870709b2feSToby Isaac . time    - The time
60884d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
60894d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
60904d6f44ffSToby Isaac - mode    - The insertion mode for values
60914d6f44ffSToby Isaac 
60924d6f44ffSToby Isaac   Output Parameter:
60934d6f44ffSToby Isaac . X - vector
60944d6f44ffSToby Isaac 
60954d6f44ffSToby Isaac    Calling sequence of func:
60960709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
60974d6f44ffSToby Isaac 
60984d6f44ffSToby Isaac +  dim - The spatial dimension
60994d6f44ffSToby Isaac .  x   - The coordinates
61004d6f44ffSToby Isaac .  Nf  - The number of fields
61014d6f44ffSToby Isaac .  u   - The output field values
61024d6f44ffSToby Isaac -  ctx - optional user-defined function context
61034d6f44ffSToby Isaac 
61044d6f44ffSToby Isaac   Level: developer
61054d6f44ffSToby Isaac 
61062716604bSToby Isaac .seealso: DMComputeL2Diff()
61074d6f44ffSToby Isaac @*/
61080709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
61094d6f44ffSToby Isaac {
61104d6f44ffSToby Isaac   Vec            localX;
61114d6f44ffSToby Isaac   PetscErrorCode ierr;
61124d6f44ffSToby Isaac 
61134d6f44ffSToby Isaac   PetscFunctionBegin;
61144d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61154d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
61160709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61174d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
61184d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
61194d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
61204d6f44ffSToby Isaac   PetscFunctionReturn(0);
61214d6f44ffSToby Isaac }
61224d6f44ffSToby Isaac 
61234d6f44ffSToby Isaac #undef __FUNCT__
61244d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal"
61250709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
61264d6f44ffSToby Isaac {
61274d6f44ffSToby Isaac   PetscErrorCode ierr;
61284d6f44ffSToby Isaac 
61294d6f44ffSToby Isaac   PetscFunctionBegin;
61304d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61314d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61324d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
61330709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61344d6f44ffSToby Isaac   PetscFunctionReturn(0);
61354d6f44ffSToby Isaac }
61364d6f44ffSToby Isaac 
61374d6f44ffSToby Isaac #undef __FUNCT__
6138bfc4295aSToby Isaac #define __FUNCT__ "DMProjectFieldLocal"
6139bfc4295aSToby Isaac PetscErrorCode DMProjectFieldLocal(DM dm, Vec localU,
6140bfc4295aSToby Isaac                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
6141bfc4295aSToby Isaac                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6142bfc4295aSToby Isaac                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6143bfc4295aSToby Isaac                                                   PetscReal, const PetscReal[], PetscScalar[]),
6144bfc4295aSToby Isaac                                    InsertMode mode, Vec localX)
6145bfc4295aSToby Isaac {
6146bfc4295aSToby Isaac   PetscErrorCode ierr;
6147bfc4295aSToby Isaac 
6148bfc4295aSToby Isaac   PetscFunctionBegin;
6149bfc4295aSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6150bfc4295aSToby Isaac   PetscValidHeaderSpecific(localU,VEC_CLASSID,2);
6151bfc4295aSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
6152bfc4295aSToby Isaac   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
6153bfc4295aSToby Isaac   ierr = (dm->ops->projectfieldlocal) (dm, localU, funcs, mode, localX);CHKERRQ(ierr);
6154bfc4295aSToby Isaac   PetscFunctionReturn(0);
6155bfc4295aSToby Isaac }
6156bfc4295aSToby Isaac 
6157bfc4295aSToby Isaac #undef __FUNCT__
61584d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLabelLocal"
61590709b2feSToby 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)
61604d6f44ffSToby Isaac {
61614d6f44ffSToby Isaac   PetscErrorCode ierr;
61624d6f44ffSToby Isaac 
61634d6f44ffSToby Isaac   PetscFunctionBegin;
61644d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61654d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61664d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
61670709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61684d6f44ffSToby Isaac   PetscFunctionReturn(0);
61694d6f44ffSToby Isaac }
61702716604bSToby Isaac 
61712716604bSToby Isaac #undef __FUNCT__
61722716604bSToby Isaac #define __FUNCT__ "DMComputeL2Diff"
61732716604bSToby Isaac /*@C
61742716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
61752716604bSToby Isaac 
61762716604bSToby Isaac   Input Parameters:
61772716604bSToby Isaac + dm    - The DM
61780709b2feSToby Isaac . time  - The time
61792716604bSToby Isaac . funcs - The functions to evaluate for each field component
61802716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
61812716604bSToby Isaac - X     - The coefficient vector u_h
61822716604bSToby Isaac 
61832716604bSToby Isaac   Output Parameter:
61842716604bSToby Isaac . diff - The diff ||u - u_h||_2
61852716604bSToby Isaac 
61862716604bSToby Isaac   Level: developer
61872716604bSToby Isaac 
61881189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
61892716604bSToby Isaac @*/
61900709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
61912716604bSToby Isaac {
61922716604bSToby Isaac   PetscErrorCode ierr;
61932716604bSToby Isaac 
61942716604bSToby Isaac   PetscFunctionBegin;
61952716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6196b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
61972716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
61980709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
61992716604bSToby Isaac   PetscFunctionReturn(0);
62002716604bSToby Isaac }
6201b698f381SToby Isaac 
6202b698f381SToby Isaac #undef __FUNCT__
6203b698f381SToby Isaac #define __FUNCT__ "DMComputeL2GradientDiff"
6204b698f381SToby Isaac /*@C
6205b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6206b698f381SToby Isaac 
6207b698f381SToby Isaac   Input Parameters:
6208b698f381SToby Isaac + dm    - The DM
6209b698f381SToby Isaac , time  - The time
6210b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6211b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6212b698f381SToby Isaac . X     - The coefficient vector u_h
6213b698f381SToby Isaac - n     - The vector to project along
6214b698f381SToby Isaac 
6215b698f381SToby Isaac   Output Parameter:
6216b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6217b698f381SToby Isaac 
6218b698f381SToby Isaac   Level: developer
6219b698f381SToby Isaac 
6220b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6221b698f381SToby Isaac @*/
6222b698f381SToby 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)
6223b698f381SToby Isaac {
6224b698f381SToby Isaac   PetscErrorCode ierr;
6225b698f381SToby Isaac 
6226b698f381SToby Isaac   PetscFunctionBegin;
6227b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6228b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6229b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6230b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6231b698f381SToby Isaac   PetscFunctionReturn(0);
6232b698f381SToby Isaac }
6233b698f381SToby Isaac 
62342a16baeaSToby Isaac #undef __FUNCT__
62352a16baeaSToby Isaac #define __FUNCT__ "DMComputeL2FieldDiff"
62362a16baeaSToby Isaac /*@C
62372a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
62382a16baeaSToby Isaac 
62392a16baeaSToby Isaac   Input Parameters:
62402a16baeaSToby Isaac + dm    - The DM
62412a16baeaSToby Isaac . time  - The time
62422a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
62432a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
62442a16baeaSToby Isaac - X     - The coefficient vector u_h
62452a16baeaSToby Isaac 
62462a16baeaSToby Isaac   Output Parameter:
62472a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
62482a16baeaSToby Isaac 
62492a16baeaSToby Isaac   Level: developer
62502a16baeaSToby Isaac 
62511189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62522a16baeaSToby Isaac @*/
62531189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
62542a16baeaSToby Isaac {
62552a16baeaSToby Isaac   PetscErrorCode ierr;
62562a16baeaSToby Isaac 
62572a16baeaSToby Isaac   PetscFunctionBegin;
62582a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62592a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62602a16baeaSToby Isaac   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
62612a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62622a16baeaSToby Isaac   PetscFunctionReturn(0);
62632a16baeaSToby Isaac }
62642a16baeaSToby Isaac 
6265