xref: /petsc/src/dm/interface/dm.c (revision bfc4295a1bbdd76fb598197f2f5f63544b171f7b)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
30c312b8eSJed Brown #include <petscsf.h>
42764a2aaSMatthew G. Knepley #include <petscds.h>
547c6ae99SBarry Smith 
6732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
7cea54e9dSPatrick Farrell PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_CreateInterpolation;
867a56275SMatthew G Knepley 
9bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
10bff4a2f0SMatthew G. Knepley 
1147c6ae99SBarry Smith #undef __FUNCT__
12a4121054SBarry Smith #define __FUNCT__ "DMCreate"
13a4121054SBarry Smith /*@
14de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
15a4121054SBarry Smith 
16a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
17a4121054SBarry Smith    error when you try to use the vector.
18a4121054SBarry Smith 
19a4121054SBarry Smith   Collective on MPI_Comm
20a4121054SBarry Smith 
21a4121054SBarry Smith   Input Parameter:
22a4121054SBarry Smith . comm - The communicator for the DM object
23a4121054SBarry Smith 
24a4121054SBarry Smith   Output Parameter:
25a4121054SBarry Smith . dm - The DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Level: beginner
28a4121054SBarry Smith 
298472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
30a4121054SBarry Smith @*/
317087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
32a4121054SBarry Smith {
33a4121054SBarry Smith   DM             v;
34a4121054SBarry Smith   PetscErrorCode ierr;
35a4121054SBarry Smith 
36a4121054SBarry Smith   PetscFunctionBegin;
371411c6eeSJed Brown   PetscValidPointer(dm,2);
380298fd71SBarry Smith   *dm = NULL;
398b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
42607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
43a4121054SBarry Smith 
4473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
45e7c4fc90SDmitry Karpeev 
460298fd71SBarry Smith   v->ltogmap                  = NULL;
471411c6eeSJed Brown   v->bs                       = 1;
48171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
4988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
51c58f1c22SToby Isaac   v->labels                   = NULL;
52c58f1c22SToby Isaac   v->depthLabel               = NULL;
530298fd71SBarry Smith   v->defaultSection           = NULL;
540298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
55fba222abSToby Isaac   v->defaultConstraintSection = NULL;
56fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
57c6b900c6SMatthew G. Knepley   v->L                        = NULL;
58c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
595dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
609a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
61435a35e8SMatthew G Knepley   {
62435a35e8SMatthew G Knepley     PetscInt i;
63435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
640298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
65435a35e8SMatthew G Knepley     }
66435a35e8SMatthew G Knepley   }
672764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6814f150ffSMatthew G. Knepley   v->dmBC = NULL;
69a8fb8f29SToby Isaac   v->coarseMesh = NULL;
70f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
71cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
72c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
73b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
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   }
120cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
12138221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
12238221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
12338221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
12438221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
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);
134be4c1c3eSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
135be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
136be4c1c3eSMatthew G. Knepley     }
137be4c1c3eSMatthew G. Knepley   }
13838221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   if (coords) {
14038221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   } else {
14238221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14338221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14438221697SMatthew G. Knepley   }
145c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
146c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1475dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1485dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1495dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
150c6b900c6SMatthew G. Knepley   }
15138221697SMatthew G. Knepley   PetscFunctionReturn(0);
15238221697SMatthew G. Knepley }
15338221697SMatthew G. Knepley 
15438221697SMatthew G. Knepley #undef __FUNCT__
1559a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1569a42bb27SBarry Smith /*@C
157564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1589a42bb27SBarry Smith 
1598472ad0fSDave May    Logically Collective on DM
1609a42bb27SBarry Smith 
1619a42bb27SBarry Smith    Input Parameter:
1629a42bb27SBarry Smith +  da - initial distributed array
1638154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1649a42bb27SBarry Smith 
1659a42bb27SBarry Smith    Options Database:
166dd85299cSBarry Smith .   -dm_vec_type ctype
1679a42bb27SBarry Smith 
1689a42bb27SBarry Smith    Level: intermediate
1699a42bb27SBarry Smith 
1708472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1719a42bb27SBarry Smith @*/
17219fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1739a42bb27SBarry Smith {
1749a42bb27SBarry Smith   PetscErrorCode ierr;
1759a42bb27SBarry Smith 
1769a42bb27SBarry Smith   PetscFunctionBegin;
1779a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1789a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
17919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1809a42bb27SBarry Smith   PetscFunctionReturn(0);
1819a42bb27SBarry Smith }
1829a42bb27SBarry Smith 
1839a42bb27SBarry Smith #undef __FUNCT__
184c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
185c0dedaeaSBarry Smith /*@C
186c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
187c0dedaeaSBarry Smith 
1888472ad0fSDave May    Logically Collective on DM
189c0dedaeaSBarry Smith 
190c0dedaeaSBarry Smith    Input Parameter:
191c0dedaeaSBarry Smith .  da - initial distributed array
192c0dedaeaSBarry Smith 
193c0dedaeaSBarry Smith    Output Parameter:
194c0dedaeaSBarry Smith .  ctype - the vector type
195c0dedaeaSBarry Smith 
196c0dedaeaSBarry Smith    Level: intermediate
197c0dedaeaSBarry Smith 
1988472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
199c0dedaeaSBarry Smith @*/
200c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
201c0dedaeaSBarry Smith {
202c0dedaeaSBarry Smith   PetscFunctionBegin;
203c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
204c0dedaeaSBarry Smith   *ctype = da->vectype;
205c0dedaeaSBarry Smith   PetscFunctionReturn(0);
206c0dedaeaSBarry Smith }
207c0dedaeaSBarry Smith 
208c0dedaeaSBarry Smith #undef __FUNCT__
2095f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
2105f1ad066SMatthew G Knepley /*@
21134f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2125f1ad066SMatthew G Knepley 
2135f1ad066SMatthew G Knepley   Not collective
2145f1ad066SMatthew G Knepley 
2155f1ad066SMatthew G Knepley   Input Parameter:
2165f1ad066SMatthew G Knepley . v - The Vec
2175f1ad066SMatthew G Knepley 
2185f1ad066SMatthew G Knepley   Output Parameter:
2195f1ad066SMatthew G Knepley . dm - The DM
2205f1ad066SMatthew G Knepley 
2215f1ad066SMatthew G Knepley   Level: intermediate
2225f1ad066SMatthew G Knepley 
2235f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2245f1ad066SMatthew G Knepley @*/
2255f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2265f1ad066SMatthew G Knepley {
2275f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2285f1ad066SMatthew G Knepley 
2295f1ad066SMatthew G Knepley   PetscFunctionBegin;
2305f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2315f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2325f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2335f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2345f1ad066SMatthew G Knepley }
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley #undef __FUNCT__
2375f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2385f1ad066SMatthew G Knepley /*@
239d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2405f1ad066SMatthew G Knepley 
2415f1ad066SMatthew G Knepley   Not collective
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   Input Parameters:
2445f1ad066SMatthew G Knepley + v - The Vec
2455f1ad066SMatthew G Knepley - dm - The DM
2465f1ad066SMatthew G Knepley 
247d9805387SMatthew 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.
248d9805387SMatthew G. Knepley 
2495f1ad066SMatthew G Knepley   Level: intermediate
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2525f1ad066SMatthew G Knepley @*/
2535f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2545f1ad066SMatthew G Knepley {
2555f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley   PetscFunctionBegin;
2585f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
259d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2605f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2615f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2625f1ad066SMatthew G Knepley }
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley #undef __FUNCT__
265521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
266521d9a4cSLisandro Dalcin /*@C
267521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
268521d9a4cSLisandro Dalcin 
269521d9a4cSLisandro Dalcin    Logically Collective on DM
270521d9a4cSLisandro Dalcin 
271521d9a4cSLisandro Dalcin    Input Parameter:
272521d9a4cSLisandro Dalcin +  dm - the DM context
273521d9a4cSLisandro Dalcin .  ctype - the matrix type
274521d9a4cSLisandro Dalcin 
275521d9a4cSLisandro Dalcin    Options Database:
276521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
277521d9a4cSLisandro Dalcin 
278521d9a4cSLisandro Dalcin    Level: intermediate
279521d9a4cSLisandro Dalcin 
280c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
281521d9a4cSLisandro Dalcin @*/
28219fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
283521d9a4cSLisandro Dalcin {
284521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
28588f0584fSBarry Smith 
286521d9a4cSLisandro Dalcin   PetscFunctionBegin;
287521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
288521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
28919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
290521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
291521d9a4cSLisandro Dalcin }
292521d9a4cSLisandro Dalcin 
293521d9a4cSLisandro Dalcin #undef __FUNCT__
294c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
295c0dedaeaSBarry Smith /*@C
296c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
297c0dedaeaSBarry Smith 
298c0dedaeaSBarry Smith    Logically Collective on DM
299c0dedaeaSBarry Smith 
300c0dedaeaSBarry Smith    Input Parameter:
301c0dedaeaSBarry Smith .  dm - the DM context
302c0dedaeaSBarry Smith 
303c0dedaeaSBarry Smith    Output Parameter:
304c0dedaeaSBarry Smith .  ctype - the matrix type
305c0dedaeaSBarry Smith 
306c0dedaeaSBarry Smith    Options Database:
307c0dedaeaSBarry Smith .   -dm_mat_type ctype
308c0dedaeaSBarry Smith 
309c0dedaeaSBarry Smith    Level: intermediate
310c0dedaeaSBarry Smith 
311c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
312c0dedaeaSBarry Smith @*/
313c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
314c0dedaeaSBarry Smith {
315c0dedaeaSBarry Smith   PetscFunctionBegin;
316c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
317c0dedaeaSBarry Smith   *ctype = dm->mattype;
318c0dedaeaSBarry Smith   PetscFunctionReturn(0);
319c0dedaeaSBarry Smith }
320c0dedaeaSBarry Smith 
321c0dedaeaSBarry Smith #undef __FUNCT__
322c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
323c688c046SMatthew G Knepley /*@
32434f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
325c688c046SMatthew G Knepley 
326c688c046SMatthew G Knepley   Not collective
327c688c046SMatthew G Knepley 
328c688c046SMatthew G Knepley   Input Parameter:
329c688c046SMatthew G Knepley . A - The Mat
330c688c046SMatthew G Knepley 
331c688c046SMatthew G Knepley   Output Parameter:
332c688c046SMatthew G Knepley . dm - The DM
333c688c046SMatthew G Knepley 
334c688c046SMatthew G Knepley   Level: intermediate
335c688c046SMatthew G Knepley 
336c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
337c688c046SMatthew G Knepley @*/
338c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
339c688c046SMatthew G Knepley {
340c688c046SMatthew G Knepley   PetscErrorCode ierr;
341c688c046SMatthew G Knepley 
342c688c046SMatthew G Knepley   PetscFunctionBegin;
343c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
344c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
345c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
346c688c046SMatthew G Knepley   PetscFunctionReturn(0);
347c688c046SMatthew G Knepley }
348c688c046SMatthew G Knepley 
349c688c046SMatthew G Knepley #undef __FUNCT__
350c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
351c688c046SMatthew G Knepley /*@
352c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
353c688c046SMatthew G Knepley 
354c688c046SMatthew G Knepley   Not collective
355c688c046SMatthew G Knepley 
356c688c046SMatthew G Knepley   Input Parameters:
357c688c046SMatthew G Knepley + A - The Mat
358c688c046SMatthew G Knepley - dm - The DM
359c688c046SMatthew G Knepley 
360c688c046SMatthew G Knepley   Level: intermediate
361c688c046SMatthew G Knepley 
362c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
363c688c046SMatthew G Knepley @*/
364c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
365c688c046SMatthew G Knepley {
366c688c046SMatthew G Knepley   PetscErrorCode ierr;
367c688c046SMatthew G Knepley 
368c688c046SMatthew G Knepley   PetscFunctionBegin;
369c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3708865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
371c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
372c688c046SMatthew G Knepley   PetscFunctionReturn(0);
373c688c046SMatthew G Knepley }
374c688c046SMatthew G Knepley 
375c688c046SMatthew G Knepley #undef __FUNCT__
3769a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3779a42bb27SBarry Smith /*@C
3789a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3796757b960SDave May    DM options in the database.
3809a42bb27SBarry Smith 
3818353ddbbSDave May    Logically Collective on DM
3829a42bb27SBarry Smith 
3839a42bb27SBarry Smith    Input Parameter:
3848353ddbbSDave May +  da - the DM context
3859a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3869a42bb27SBarry Smith 
3879a42bb27SBarry Smith    Notes:
3889a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3899a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3909a42bb27SBarry Smith 
3919a42bb27SBarry Smith    Level: advanced
3929a42bb27SBarry Smith 
3938353ddbbSDave May .keywords: DM, set, options, prefix, database
3949a42bb27SBarry Smith 
3959a42bb27SBarry Smith .seealso: DMSetFromOptions()
3969a42bb27SBarry Smith @*/
3977087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3989a42bb27SBarry Smith {
3999a42bb27SBarry Smith   PetscErrorCode ierr;
4009a42bb27SBarry Smith 
4019a42bb27SBarry Smith   PetscFunctionBegin;
4029a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4039a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
404691be533SLawrence Mitchell   if (dm->sf) {
405691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
406691be533SLawrence Mitchell   }
407691be533SLawrence Mitchell   if (dm->defaultSF) {
408691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
409691be533SLawrence Mitchell   }
4109a42bb27SBarry Smith   PetscFunctionReturn(0);
4119a42bb27SBarry Smith }
4129a42bb27SBarry Smith 
4139a42bb27SBarry Smith #undef __FUNCT__
41431697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
41531697293SDave May /*@C
41631697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
41731697293SDave May    DM options in the database.
41831697293SDave May 
41931697293SDave May    Logically Collective on DM
42031697293SDave May 
42131697293SDave May    Input Parameters:
42231697293SDave May +  dm - the DM context
42331697293SDave May -  prefix - the prefix string to prepend to all DM option requests
42431697293SDave May 
42531697293SDave May    Notes:
42631697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
42731697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
42831697293SDave May 
42931697293SDave May    Level: advanced
43031697293SDave May 
43131697293SDave May .keywords: DM, append, options, prefix, database
43231697293SDave May 
43331697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
43431697293SDave May @*/
43531697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
43631697293SDave May {
43731697293SDave May   PetscErrorCode ierr;
43831697293SDave May 
43931697293SDave May   PetscFunctionBegin;
44031697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44131697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
44231697293SDave May   PetscFunctionReturn(0);
44331697293SDave May }
44431697293SDave May 
44531697293SDave May #undef __FUNCT__
44631697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
44731697293SDave May /*@C
44831697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
44931697293SDave May    DM options in the database.
45031697293SDave May 
45131697293SDave May    Not Collective
45231697293SDave May 
45331697293SDave May    Input Parameters:
45431697293SDave May .  dm - the DM context
45531697293SDave May 
45631697293SDave May    Output Parameters:
45731697293SDave May .  prefix - pointer to the prefix string used is returned
45831697293SDave May 
45931697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
46031697293SDave May    sufficient length to hold the prefix.
46131697293SDave May 
46231697293SDave May    Level: advanced
46331697293SDave May 
46431697293SDave May .keywords: DM, set, options, prefix, database
46531697293SDave May 
46631697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
46731697293SDave May @*/
46831697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
46931697293SDave May {
47031697293SDave May   PetscErrorCode ierr;
47131697293SDave May 
47231697293SDave May   PetscFunctionBegin;
47331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47431697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
47531697293SDave May   PetscFunctionReturn(0);
47631697293SDave May }
47731697293SDave May 
47831697293SDave May #undef __FUNCT__
47988bdff64SToby Isaac #define __FUNCT__ "DMCountNonCyclicReferences"
48088bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
48188bdff64SToby Isaac {
48288bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
48388bdff64SToby Isaac   DMNamedVecLink nlink;
48488bdff64SToby Isaac   PetscErrorCode ierr;
48588bdff64SToby Isaac 
48688bdff64SToby Isaac   PetscFunctionBegin;
48788bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
48888bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
48988bdff64SToby Isaac     if (dm->localin[i])  refct--;
49088bdff64SToby Isaac     if (dm->globalin[i]) refct--;
49188bdff64SToby Isaac   }
49288bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
49388bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
49488bdff64SToby Isaac   if (dm->x) {
49588bdff64SToby Isaac     DM obj;
49688bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
49788bdff64SToby Isaac     if (obj == dm) refct--;
49888bdff64SToby Isaac   }
49988bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
50088bdff64SToby Isaac     refct--;
50188bdff64SToby Isaac     if (recurseCoarse) {
50288bdff64SToby Isaac       PetscInt coarseCount;
50388bdff64SToby Isaac 
50488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
50588bdff64SToby Isaac       refct += coarseCount;
50688bdff64SToby Isaac     }
50788bdff64SToby Isaac   }
50888bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
50988bdff64SToby Isaac     refct--;
51088bdff64SToby Isaac     if (recurseFine) {
51188bdff64SToby Isaac       PetscInt fineCount;
51288bdff64SToby Isaac 
51388bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
51488bdff64SToby Isaac       refct += fineCount;
51588bdff64SToby Isaac     }
51688bdff64SToby Isaac   }
51788bdff64SToby Isaac   *ncrefct = refct;
51888bdff64SToby Isaac   PetscFunctionReturn(0);
51988bdff64SToby Isaac }
52088bdff64SToby Isaac 
521bcc5ffd9SToby Isaac PetscErrorCode DMBoundaryDestroy(DMBoundaryLinkList *boundary);
522a6ba4734SToby Isaac 
52388bdff64SToby Isaac #undef __FUNCT__
524354557abSToby Isaac #define __FUNCT__ "DMDestroyLabelLinkList"
525354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
526354557abSToby Isaac {
527354557abSToby Isaac   PetscErrorCode ierr;
528354557abSToby Isaac 
529354557abSToby Isaac   PetscFunctionBegin;
530354557abSToby Isaac   if (!--(dm->labels->refct)) {
531354557abSToby Isaac     DMLabelLink next = dm->labels->next;
532354557abSToby Isaac 
533354557abSToby Isaac     /* destroy the labels */
534354557abSToby Isaac     while (next) {
535354557abSToby Isaac       DMLabelLink tmp = next->next;
536354557abSToby Isaac 
537354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
538354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
539354557abSToby Isaac       next = tmp;
540354557abSToby Isaac     }
541354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
542354557abSToby Isaac   }
543354557abSToby Isaac   PetscFunctionReturn(0);
544354557abSToby Isaac }
545354557abSToby Isaac 
546354557abSToby Isaac #undef __FUNCT__
54747c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
54847c6ae99SBarry Smith /*@
5498472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
55047c6ae99SBarry Smith 
55147c6ae99SBarry Smith     Collective on DM
55247c6ae99SBarry Smith 
55347c6ae99SBarry Smith     Input Parameter:
55447c6ae99SBarry Smith .   dm - the DM object to destroy
55547c6ae99SBarry Smith 
55647c6ae99SBarry Smith     Level: developer
55747c6ae99SBarry Smith 
558e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith @*/
561fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
56247c6ae99SBarry Smith {
56388bdff64SToby Isaac   PetscInt       i, cnt;
564dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
56547c6ae99SBarry Smith   PetscErrorCode ierr;
56647c6ae99SBarry Smith 
56747c6ae99SBarry Smith   PetscFunctionBegin;
5686bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5696bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
57087e657c6SBarry Smith 
57188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
57288bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
57388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
57488bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
575732e2eb9SMatthew G Knepley   /*
576732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
577732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
578732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
579732e2eb9SMatthew G Knepley   */
5806bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5816bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
582732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5836bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5846bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
585732e2eb9SMatthew G Knepley   }
586f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5870298fd71SBarry Smith   (*dm)->namedglobal = NULL;
588f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5892348bcf4SPeter Brune     nnext = nlink->next;
5902348bcf4SPeter 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);
5912348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5922348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5932348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5942348bcf4SPeter Brune   }
595f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5960298fd71SBarry Smith   (*dm)->namedlocal = NULL;
597f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
598f490541aSPeter Brune     nnext = nlink->next;
599f490541aSPeter 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);
600f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
601f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
602f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
603f490541aSPeter Brune   }
6042348bcf4SPeter Brune 
605b17ce1afSJed Brown   /* Destroy the list of hooks */
606c833c3b5SJed Brown   {
607c833c3b5SJed Brown     DMCoarsenHookLink link,next;
608b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
609b17ce1afSJed Brown       next = link->next;
610b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
611b17ce1afSJed Brown     }
6120298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
613c833c3b5SJed Brown   }
614c833c3b5SJed Brown   {
615c833c3b5SJed Brown     DMRefineHookLink link,next;
616c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
617c833c3b5SJed Brown       next = link->next;
618c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
619c833c3b5SJed Brown     }
6200298fd71SBarry Smith     (*dm)->refinehook = NULL;
621c833c3b5SJed Brown   }
622be081cd6SPeter Brune   {
623be081cd6SPeter Brune     DMSubDomainHookLink link,next;
624be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
625be081cd6SPeter Brune       next = link->next;
626be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
627be081cd6SPeter Brune     }
6280298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
629be081cd6SPeter Brune   }
630baf369e7SPeter Brune   {
631baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
632baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
633baf369e7SPeter Brune       next = link->next;
634baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
635baf369e7SPeter Brune     }
6360298fd71SBarry Smith     (*dm)->gtolhook = NULL;
637baf369e7SPeter Brune   }
638d4d07f1eSToby Isaac   {
639d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
640d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
641d4d07f1eSToby Isaac       next = link->next;
642d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
643d4d07f1eSToby Isaac     }
644d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
645d4d07f1eSToby Isaac   }
646aa1993deSMatthew G Knepley   /* Destroy the work arrays */
647aa1993deSMatthew G Knepley   {
648aa1993deSMatthew G Knepley     DMWorkLink link,next;
649aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
650aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
651aa1993deSMatthew G Knepley       next = link->next;
652aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
653aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
654aa1993deSMatthew G Knepley     }
6550298fd71SBarry Smith     (*dm)->workin = NULL;
656aa1993deSMatthew G Knepley   }
657c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
658c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
659c58f1c22SToby Isaac 
660c58f1c22SToby Isaac     /* destroy the labels */
661c58f1c22SToby Isaac     while (next) {
662c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
663c58f1c22SToby Isaac 
664c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
665c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
666c58f1c22SToby Isaac       next = tmp;
667c58f1c22SToby Isaac     }
668c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
669c58f1c22SToby Isaac   }
670bcc5ffd9SToby Isaac   ierr = DMBoundaryDestroy(&(*dm)->boundary);CHKERRQ(ierr);
671b17ce1afSJed Brown 
67252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
67352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
67452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
67552536dc3SBarry Smith 
6761a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6771a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6781a266240SBarry Smith   }
67987e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
68071cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6814dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6826bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6836bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
684073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
68588ed4aceSMatthew G Knepley 
68688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
68788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6888b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
689fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
690fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
69188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
69288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6938e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
694af122d2aSMatthew G Knepley 
69588bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
69688bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
69788bdff64SToby Isaac   }
698a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
69988bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
70088bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
70188bdff64SToby Isaac   }
70288bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
7036636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7046636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7056636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7065dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
7076636e97aSMatthew G Knepley 
7082764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
70914f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
710e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
711e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
712732e2eb9SMatthew G Knepley 
7136bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
714435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
715732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
71647c6ae99SBarry Smith   PetscFunctionReturn(0);
71747c6ae99SBarry Smith }
71847c6ae99SBarry Smith 
71947c6ae99SBarry Smith #undef __FUNCT__
720d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
721d7bf68aeSBarry Smith /*@
722d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
723d7bf68aeSBarry Smith 
724d7bf68aeSBarry Smith     Collective on DM
725d7bf68aeSBarry Smith 
726d7bf68aeSBarry Smith     Input Parameter:
727d7bf68aeSBarry Smith .   dm - the DM object to setup
728d7bf68aeSBarry Smith 
729d7bf68aeSBarry Smith     Level: developer
730d7bf68aeSBarry Smith 
731e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
732d7bf68aeSBarry Smith 
733d7bf68aeSBarry Smith @*/
7347087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
735d7bf68aeSBarry Smith {
736d7bf68aeSBarry Smith   PetscErrorCode ierr;
737d7bf68aeSBarry Smith 
738d7bf68aeSBarry Smith   PetscFunctionBegin;
739171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7408387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
741d7bf68aeSBarry Smith   if (dm->ops->setup) {
742d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
743d7bf68aeSBarry Smith   }
7448387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
745d7bf68aeSBarry Smith   PetscFunctionReturn(0);
746d7bf68aeSBarry Smith }
747d7bf68aeSBarry Smith 
748d7bf68aeSBarry Smith #undef __FUNCT__
749d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
750d7bf68aeSBarry Smith /*@
751d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
752d7bf68aeSBarry Smith 
753d7bf68aeSBarry Smith     Collective on DM
754d7bf68aeSBarry Smith 
755d7bf68aeSBarry Smith     Input Parameter:
756d7bf68aeSBarry Smith .   dm - the DM object to set options for
757d7bf68aeSBarry Smith 
758732e2eb9SMatthew G Knepley     Options Database:
7594164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7604164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7614164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7624164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
763732e2eb9SMatthew G Knepley 
764d7bf68aeSBarry Smith     Level: developer
765d7bf68aeSBarry Smith 
766e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
767d7bf68aeSBarry Smith 
768d7bf68aeSBarry Smith @*/
7697087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
770d7bf68aeSBarry Smith {
7717781c08eSBarry Smith   char           typeName[256];
772ca266f36SBarry Smith   PetscBool      flg;
773d7bf68aeSBarry Smith   PetscErrorCode ierr;
774d7bf68aeSBarry Smith 
775d7bf68aeSBarry Smith   PetscFunctionBegin;
776171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
777691be533SLawrence Mitchell   if (dm->sf) {
778691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
779691be533SLawrence Mitchell   }
780691be533SLawrence Mitchell   if (dm->defaultSF) {
781691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
782691be533SLawrence Mitchell   }
7833194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7840298fd71SBarry 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);
785a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
786f9ba7244SBarry Smith   if (flg) {
787f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
788f9ba7244SBarry Smith   }
789a264d7a6SBarry 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);
790073dac72SJed Brown   if (flg) {
791521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
792073dac72SJed Brown   }
7930298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
794f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
795e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
796f9ba7244SBarry Smith   }
797f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7980633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
79982fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
800d7bf68aeSBarry Smith   PetscFunctionReturn(0);
801d7bf68aeSBarry Smith }
802d7bf68aeSBarry Smith 
803d7bf68aeSBarry Smith #undef __FUNCT__
80447c6ae99SBarry Smith #define __FUNCT__ "DMView"
805fc9bc008SSatish Balay /*@C
806224748a4SBarry Smith     DMView - Views a DM
80747c6ae99SBarry Smith 
80847c6ae99SBarry Smith     Collective on DM
80947c6ae99SBarry Smith 
81047c6ae99SBarry Smith     Input Parameter:
81147c6ae99SBarry Smith +   dm - the DM object to view
81247c6ae99SBarry Smith -   v - the viewer
81347c6ae99SBarry Smith 
814224748a4SBarry Smith     Level: beginner
81547c6ae99SBarry Smith 
816e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
81747c6ae99SBarry Smith 
81847c6ae99SBarry Smith @*/
8197087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
82047c6ae99SBarry Smith {
82147c6ae99SBarry Smith   PetscErrorCode ierr;
82232c0f0efSBarry Smith   PetscBool      isbinary;
82347c6ae99SBarry Smith 
82447c6ae99SBarry Smith   PetscFunctionBegin;
825171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8263014e516SBarry Smith   if (!v) {
827ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8283014e516SBarry Smith   }
82998c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
83032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
83132c0f0efSBarry Smith   if (isbinary) {
83255849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
83332c0f0efSBarry Smith     char     type[256];
83432c0f0efSBarry Smith 
83532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
83632c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
83732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
83832c0f0efSBarry Smith   }
8390c010503SBarry Smith   if (dm->ops->view) {
8400c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
84147c6ae99SBarry Smith   }
84247c6ae99SBarry Smith   PetscFunctionReturn(0);
84347c6ae99SBarry Smith }
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith #undef __FUNCT__
84647c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
84747c6ae99SBarry Smith /*@
8488472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith     Collective on DM
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith     Input Parameter:
85347c6ae99SBarry Smith .   dm - the DM object
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith     Output Parameter:
85647c6ae99SBarry Smith .   vec - the global vector
85747c6ae99SBarry Smith 
858073dac72SJed Brown     Level: beginner
85947c6ae99SBarry Smith 
860e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
86147c6ae99SBarry Smith 
86247c6ae99SBarry Smith @*/
8637087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
86447c6ae99SBarry Smith {
86547c6ae99SBarry Smith   PetscErrorCode ierr;
86647c6ae99SBarry Smith 
86747c6ae99SBarry Smith   PetscFunctionBegin;
868171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86947c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
87047c6ae99SBarry Smith   PetscFunctionReturn(0);
87147c6ae99SBarry Smith }
87247c6ae99SBarry Smith 
87347c6ae99SBarry Smith #undef __FUNCT__
87447c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
87547c6ae99SBarry Smith /*@
8768472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
87747c6ae99SBarry Smith 
87847c6ae99SBarry Smith     Not Collective
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Input Parameter:
88147c6ae99SBarry Smith .   dm - the DM object
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith     Output Parameter:
88447c6ae99SBarry Smith .   vec - the local vector
88547c6ae99SBarry Smith 
886073dac72SJed Brown     Level: beginner
88747c6ae99SBarry Smith 
888e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith @*/
8917087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
89247c6ae99SBarry Smith {
89347c6ae99SBarry Smith   PetscErrorCode ierr;
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith   PetscFunctionBegin;
896171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
89847c6ae99SBarry Smith   PetscFunctionReturn(0);
89947c6ae99SBarry Smith }
90047c6ae99SBarry Smith 
90147c6ae99SBarry Smith #undef __FUNCT__
9021411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
9031411c6eeSJed Brown /*@
9041411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9051411c6eeSJed Brown 
9061411c6eeSJed Brown    Collective on DM
9071411c6eeSJed Brown 
9081411c6eeSJed Brown    Input Parameter:
9091411c6eeSJed Brown .  dm - the DM that provides the mapping
9101411c6eeSJed Brown 
9111411c6eeSJed Brown    Output Parameter:
9121411c6eeSJed Brown .  ltog - the mapping
9131411c6eeSJed Brown 
9141411c6eeSJed Brown    Level: intermediate
9151411c6eeSJed Brown 
9161411c6eeSJed Brown    Notes:
9171411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9181411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9191411c6eeSJed Brown 
920fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9211411c6eeSJed Brown @*/
9227087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9231411c6eeSJed Brown {
9241411c6eeSJed Brown   PetscErrorCode ierr;
9251411c6eeSJed Brown 
9261411c6eeSJed Brown   PetscFunctionBegin;
9271411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9281411c6eeSJed Brown   PetscValidPointer(ltog,2);
9291411c6eeSJed Brown   if (!dm->ltogmap) {
93037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
93137d0c07bSMatthew G Knepley 
93237d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
93337d0c07bSMatthew G Knepley     if (section) {
93437d0c07bSMatthew G Knepley       PetscInt *ltog;
93537d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
93637d0c07bSMatthew G Knepley 
93737d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
93837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
93937d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
940785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
94137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
94237d0c07bSMatthew G Knepley         PetscInt dof, off, c;
94337d0c07bSMatthew G Knepley 
94437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
94537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
94637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
94737d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
94837d0c07bSMatthew G Knepley           ltog[l] = off+c;
94937d0c07bSMatthew G Knepley         }
95037d0c07bSMatthew G Knepley       }
951f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9523bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
95337d0c07bSMatthew G Knepley     } else {
954184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
955184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9561411c6eeSJed Brown     }
95737d0c07bSMatthew G Knepley   }
9581411c6eeSJed Brown   *ltog = dm->ltogmap;
9591411c6eeSJed Brown   PetscFunctionReturn(0);
9601411c6eeSJed Brown }
9611411c6eeSJed Brown 
9621411c6eeSJed Brown #undef __FUNCT__
9631411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
9641411c6eeSJed Brown /*@
9651411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9661411c6eeSJed Brown 
9671411c6eeSJed Brown    Not Collective
9681411c6eeSJed Brown 
9691411c6eeSJed Brown    Input Parameter:
9701411c6eeSJed Brown .  dm - the DM with block structure
9711411c6eeSJed Brown 
9721411c6eeSJed Brown    Output Parameter:
9731411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9741411c6eeSJed Brown 
9751411c6eeSJed Brown    Level: intermediate
9761411c6eeSJed Brown 
977fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9781411c6eeSJed Brown @*/
9797087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9801411c6eeSJed Brown {
9811411c6eeSJed Brown   PetscFunctionBegin;
9821411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9831411c6eeSJed Brown   PetscValidPointer(bs,2);
9841411c6eeSJed 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");
9851411c6eeSJed Brown   *bs = dm->bs;
9861411c6eeSJed Brown   PetscFunctionReturn(0);
9871411c6eeSJed Brown }
9881411c6eeSJed Brown 
9891411c6eeSJed Brown #undef __FUNCT__
990e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
99147c6ae99SBarry Smith /*@
9928472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith     Collective on DM
99547c6ae99SBarry Smith 
99647c6ae99SBarry Smith     Input Parameter:
99747c6ae99SBarry Smith +   dm1 - the DM object
99847c6ae99SBarry Smith -   dm2 - the second, finer DM object
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Output Parameter:
100147c6ae99SBarry Smith +  mat - the interpolation
100247c6ae99SBarry Smith -  vec - the scaling (optional)
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith     Level: developer
100547c6ae99SBarry Smith 
100685afcc9aSBarry 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
100785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1008d52bd9f3SBarry Smith 
10091f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1010d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
101185afcc9aSBarry Smith 
101285afcc9aSBarry Smith 
1013e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
101447c6ae99SBarry Smith 
101547c6ae99SBarry Smith @*/
1016e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
101747c6ae99SBarry Smith {
101847c6ae99SBarry Smith   PetscErrorCode ierr;
101947c6ae99SBarry Smith 
102047c6ae99SBarry Smith   PetscFunctionBegin;
1021171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1022171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1023cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
102425296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1025cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
102647c6ae99SBarry Smith   PetscFunctionReturn(0);
102747c6ae99SBarry Smith }
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith #undef __FUNCT__
1030e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
103147c6ae99SBarry Smith /*@
10328472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith     Collective on DM
103547c6ae99SBarry Smith 
103647c6ae99SBarry Smith     Input Parameter:
103747c6ae99SBarry Smith +   dm1 - the DM object
103847c6ae99SBarry Smith -   dm2 - the second, finer DM object
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith     Output Parameter:
10416dbf9973SLawrence Mitchell .   mat - the injection
104247c6ae99SBarry Smith 
104347c6ae99SBarry Smith     Level: developer
104447c6ae99SBarry Smith 
104585afcc9aSBarry 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
104685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
104785afcc9aSBarry Smith 
1048e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
104947c6ae99SBarry Smith 
105047c6ae99SBarry Smith @*/
10516dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
105247c6ae99SBarry Smith {
105347c6ae99SBarry Smith   PetscErrorCode ierr;
105447c6ae99SBarry Smith 
105547c6ae99SBarry Smith   PetscFunctionBegin;
1056171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1057171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10586dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
105947c6ae99SBarry Smith   PetscFunctionReturn(0);
106047c6ae99SBarry Smith }
106147c6ae99SBarry Smith 
106247c6ae99SBarry Smith #undef __FUNCT__
1063e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
1064b412c318SBarry Smith /*@
1065b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
106647c6ae99SBarry Smith 
106747c6ae99SBarry Smith     Collective on DM
106847c6ae99SBarry Smith 
106947c6ae99SBarry Smith     Input Parameter:
107047c6ae99SBarry Smith +   dm - the DM object
1071b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith     Output Parameter:
107447c6ae99SBarry Smith .   coloring - the coloring
107547c6ae99SBarry Smith 
107647c6ae99SBarry Smith     Level: developer
107747c6ae99SBarry Smith 
1078b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
107947c6ae99SBarry Smith 
1080aab9d709SJed Brown @*/
1081b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
108247c6ae99SBarry Smith {
108347c6ae99SBarry Smith   PetscErrorCode ierr;
108447c6ae99SBarry Smith 
108547c6ae99SBarry Smith   PetscFunctionBegin;
1086171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1087ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1088b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
108947c6ae99SBarry Smith   PetscFunctionReturn(0);
109047c6ae99SBarry Smith }
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith #undef __FUNCT__
1093950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1094b412c318SBarry Smith /*@
10958472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith     Collective on DM
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith     Input Parameter:
1100b412c318SBarry Smith .   dm - the DM object
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith     Output Parameter:
110347c6ae99SBarry Smith .   mat - the empty Jacobian
110447c6ae99SBarry Smith 
1105073dac72SJed Brown     Level: beginner
110647c6ae99SBarry Smith 
110794013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
110894013140SBarry Smith        do not need to do it yourself.
110994013140SBarry Smith 
111094013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1111aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
111294013140SBarry Smith 
111394013140SBarry 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
111494013140SBarry Smith        internally by PETSc.
111594013140SBarry Smith 
111694013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1117aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
111894013140SBarry Smith 
1119b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
112047c6ae99SBarry Smith 
1121aab9d709SJed Brown @*/
1122b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
112347c6ae99SBarry Smith {
112447c6ae99SBarry Smith   PetscErrorCode ierr;
112547c6ae99SBarry Smith 
112647c6ae99SBarry Smith   PetscFunctionBegin;
1127171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1128607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1129c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1130c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1131b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
113247c6ae99SBarry Smith   PetscFunctionReturn(0);
113347c6ae99SBarry Smith }
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith #undef __FUNCT__
1136732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1137732e2eb9SMatthew G Knepley /*@
1138950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1139732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1140732e2eb9SMatthew G Knepley 
11418472ad0fSDave May   Logically Collective on DM
1142732e2eb9SMatthew G Knepley 
1143732e2eb9SMatthew G Knepley   Input Parameter:
1144732e2eb9SMatthew G Knepley + dm - the DM
1145732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1146732e2eb9SMatthew G Knepley 
1147732e2eb9SMatthew G Knepley   Level: developer
1148950540a4SJed Brown .seealso DMCreateMatrix()
1149732e2eb9SMatthew G Knepley @*/
1150732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1151732e2eb9SMatthew G Knepley {
1152732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1153732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1154732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1155732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1156732e2eb9SMatthew G Knepley }
1157732e2eb9SMatthew G Knepley 
1158732e2eb9SMatthew G Knepley #undef __FUNCT__
1159a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1160a89ea682SMatthew G Knepley /*@C
1161aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1162a89ea682SMatthew G Knepley 
1163a89ea682SMatthew G Knepley   Not Collective
1164a89ea682SMatthew G Knepley 
1165a89ea682SMatthew G Knepley   Input Parameters:
1166a89ea682SMatthew G Knepley + dm - the DM object
1167aa1993deSMatthew G Knepley . count - The minium size
1168aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1169a89ea682SMatthew G Knepley 
1170a89ea682SMatthew G Knepley   Output Parameter:
1171a89ea682SMatthew G Knepley . array - the work array
1172a89ea682SMatthew G Knepley 
1173a89ea682SMatthew G Knepley   Level: developer
1174a89ea682SMatthew G Knepley 
1175a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1176a89ea682SMatthew G Knepley @*/
1177aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1178a89ea682SMatthew G Knepley {
1179a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1180aa1993deSMatthew G Knepley   DMWorkLink     link;
1181854ce69bSBarry Smith   size_t         dsize;
1182a89ea682SMatthew G Knepley 
1183a89ea682SMatthew G Knepley   PetscFunctionBegin;
1184a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1185aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1186aa1993deSMatthew G Knepley   if (dm->workin) {
1187aa1993deSMatthew G Knepley     link       = dm->workin;
1188aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1189aa1993deSMatthew G Knepley   } else {
1190b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1191a89ea682SMatthew G Knepley   }
1192854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1193854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1194aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1195854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1196854ce69bSBarry Smith     link->bytes = dsize*count;
1197aa1993deSMatthew G Knepley   }
1198aa1993deSMatthew G Knepley   link->next   = dm->workout;
1199aa1993deSMatthew G Knepley   dm->workout  = link;
1200aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1201a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1202a89ea682SMatthew G Knepley }
1203a89ea682SMatthew G Knepley 
1204aa1993deSMatthew G Knepley #undef __FUNCT__
1205aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1206aa1993deSMatthew G Knepley /*@C
1207aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1208aa1993deSMatthew G Knepley 
1209aa1993deSMatthew G Knepley   Not Collective
1210aa1993deSMatthew G Knepley 
1211aa1993deSMatthew G Knepley   Input Parameters:
1212aa1993deSMatthew G Knepley + dm - the DM object
1213aa1993deSMatthew G Knepley . count - The minium size
1214aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1215aa1993deSMatthew G Knepley 
1216aa1993deSMatthew G Knepley   Output Parameter:
1217aa1993deSMatthew G Knepley . array - the work array
1218aa1993deSMatthew G Knepley 
1219aa1993deSMatthew G Knepley   Level: developer
1220aa1993deSMatthew G Knepley 
1221aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1222aa1993deSMatthew G Knepley @*/
1223aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1224aa1993deSMatthew G Knepley {
1225aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1226aa1993deSMatthew G Knepley 
1227aa1993deSMatthew G Knepley   PetscFunctionBegin;
1228aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1229aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1230aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1231aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1232aa1993deSMatthew G Knepley       *p           = link->next;
1233aa1993deSMatthew G Knepley       link->next   = dm->workin;
1234aa1993deSMatthew G Knepley       dm->workin   = link;
12350298fd71SBarry Smith       *(void**)mem = NULL;
1236aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1237aa1993deSMatthew G Knepley     }
1238aa1993deSMatthew G Knepley   }
1239aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1240aa1993deSMatthew G Knepley }
1241e7c4fc90SDmitry Karpeev 
1242e7c4fc90SDmitry Karpeev #undef __FUNCT__
1243435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1244435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1245435a35e8SMatthew G Knepley {
1246435a35e8SMatthew G Knepley   PetscFunctionBegin;
1247435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
124882f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1249435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1250435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1251435a35e8SMatthew G Knepley }
1252435a35e8SMatthew G Knepley 
1253435a35e8SMatthew G Knepley #undef __FUNCT__
12544d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
12554f3b5142SJed Brown /*@C
12564d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12574d343eeaSMatthew G Knepley 
12584d343eeaSMatthew G Knepley   Not collective
12594d343eeaSMatthew G Knepley 
12604d343eeaSMatthew G Knepley   Input Parameter:
12614d343eeaSMatthew G Knepley . dm - the DM object
12624d343eeaSMatthew G Knepley 
12634d343eeaSMatthew G Knepley   Output Parameters:
12640298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
12650298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
12660298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
12674d343eeaSMatthew G Knepley 
12684d343eeaSMatthew G Knepley   Level: intermediate
12694d343eeaSMatthew G Knepley 
127021c9b008SJed Brown   Notes:
127121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
127221c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
127321c9b008SJed Brown   PetscFree().
127421c9b008SJed Brown 
12754d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
12764d343eeaSMatthew G Knepley @*/
127737d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
12784d343eeaSMatthew G Knepley {
127937d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
12804d343eeaSMatthew G Knepley   PetscErrorCode ierr;
12814d343eeaSMatthew G Knepley 
12824d343eeaSMatthew G Knepley   PetscFunctionBegin;
12834d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
128469ca1f37SDmitry Karpeev   if (numFields) {
128569ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
128669ca1f37SDmitry Karpeev     *numFields = 0;
128769ca1f37SDmitry Karpeev   }
128837d0c07bSMatthew G Knepley   if (fieldNames) {
128937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
12900298fd71SBarry Smith     *fieldNames = NULL;
129169ca1f37SDmitry Karpeev   }
129269ca1f37SDmitry Karpeev   if (fields) {
129369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
12940298fd71SBarry Smith     *fields = NULL;
129569ca1f37SDmitry Karpeev   }
129637d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
129737d0c07bSMatthew G Knepley   if (section) {
129837d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
129937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
130037d0c07bSMatthew G Knepley 
130137d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
130237d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1303dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
130437d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
130537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
130637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
130737d0c07bSMatthew G Knepley     }
130837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
130937d0c07bSMatthew G Knepley       PetscInt gdof;
131037d0c07bSMatthew G Knepley 
131137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
131237d0c07bSMatthew G Knepley       if (gdof > 0) {
131337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
131437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
131537d0c07bSMatthew G Knepley 
131637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
131737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
131837d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
131937d0c07bSMatthew G Knepley         }
132037d0c07bSMatthew G Knepley       }
132137d0c07bSMatthew G Knepley     }
132237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1323785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
132437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
132537d0c07bSMatthew G Knepley     }
132637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
132737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
132837d0c07bSMatthew G Knepley 
132937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
133037d0c07bSMatthew G Knepley       if (gdof > 0) {
133137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
133237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
133337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
133437d0c07bSMatthew G Knepley 
133537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
133637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
133737d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
133837d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
133937d0c07bSMatthew G Knepley           }
134037d0c07bSMatthew G Knepley         }
134137d0c07bSMatthew G Knepley       }
134237d0c07bSMatthew G Knepley     }
13438865f1eaSKarl Rupp     if (numFields) *numFields = nF;
134437d0c07bSMatthew G Knepley     if (fieldNames) {
1345785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
134637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
134737d0c07bSMatthew G Knepley         const char *fieldName;
134837d0c07bSMatthew G Knepley 
134937d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
135037d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
135137d0c07bSMatthew G Knepley       }
135237d0c07bSMatthew G Knepley     }
135337d0c07bSMatthew G Knepley     if (fields) {
1354785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
135537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
135682f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
135737d0c07bSMatthew G Knepley       }
135837d0c07bSMatthew G Knepley     }
135937d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13608865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
13618865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
136269ca1f37SDmitry Karpeev   }
13634d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
13644d343eeaSMatthew G Knepley }
13654d343eeaSMatthew G Knepley 
136616621825SDmitry Karpeev 
136716621825SDmitry Karpeev #undef __FUNCT__
136816621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
136916621825SDmitry Karpeev /*@C
137016621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
137116621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
137216621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1373e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1374e7c4fc90SDmitry Karpeev 
1375e7c4fc90SDmitry Karpeev   Not collective
1376e7c4fc90SDmitry Karpeev 
1377e7c4fc90SDmitry Karpeev   Input Parameter:
1378e7c4fc90SDmitry Karpeev . dm - the DM object
1379e7c4fc90SDmitry Karpeev 
1380e7c4fc90SDmitry Karpeev   Output Parameters:
13810298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
13820298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
13830298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
13840298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1385e7c4fc90SDmitry Karpeev 
1386e7c4fc90SDmitry Karpeev   Level: intermediate
1387e7c4fc90SDmitry Karpeev 
1388e7c4fc90SDmitry Karpeev   Notes:
1389e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1390e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1391e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1392e7c4fc90SDmitry Karpeev 
1393e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1394e7c4fc90SDmitry Karpeev @*/
139516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1396e7c4fc90SDmitry Karpeev {
1397e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1398e7c4fc90SDmitry Karpeev 
1399e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1400e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14018865f1eaSKarl Rupp   if (len) {
14028865f1eaSKarl Rupp     PetscValidPointer(len,2);
14038865f1eaSKarl Rupp     *len = 0;
14048865f1eaSKarl Rupp   }
14058865f1eaSKarl Rupp   if (namelist) {
14068865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14078865f1eaSKarl Rupp     *namelist = 0;
14088865f1eaSKarl Rupp   }
14098865f1eaSKarl Rupp   if (islist) {
14108865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14118865f1eaSKarl Rupp     *islist = 0;
14128865f1eaSKarl Rupp   }
14138865f1eaSKarl Rupp   if (dmlist) {
14148865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14158865f1eaSKarl Rupp     *dmlist = 0;
14168865f1eaSKarl Rupp   }
1417f3f0edfdSDmitry Karpeev   /*
1418f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1419f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1420f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1421f3f0edfdSDmitry Karpeev    */
1422ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
142316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1424435a35e8SMatthew G Knepley     PetscSection section;
1425435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1426435a35e8SMatthew G Knepley 
1427435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1428435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1429435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1430435a35e8SMatthew G Knepley       *len = numFields;
143103dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
143203dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
143303dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1434435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1435435a35e8SMatthew G Knepley         const char *fieldName;
1436435a35e8SMatthew G Knepley 
143703dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
143803dc3394SMatthew G. Knepley         if (namelist) {
1439435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1440435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1441435a35e8SMatthew G Knepley         }
144203dc3394SMatthew G. Knepley       }
1443435a35e8SMatthew G Knepley     } else {
144469ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1445e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14460298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1447e7c4fc90SDmitry Karpeev     }
14488865f1eaSKarl Rupp   } else {
144916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
145016621825SDmitry Karpeev   }
145116621825SDmitry Karpeev   PetscFunctionReturn(0);
145216621825SDmitry Karpeev }
145316621825SDmitry Karpeev 
145416621825SDmitry Karpeev #undef __FUNCT__
1455435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1456564cec59SMatthew G. Knepley /*@
1457435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1458435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1459435a35e8SMatthew G Knepley 
1460435a35e8SMatthew G Knepley   Not collective
1461435a35e8SMatthew G Knepley 
1462435a35e8SMatthew G Knepley   Input Parameters:
1463435a35e8SMatthew G Knepley + dm - the DM object
1464435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
14650298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1466435a35e8SMatthew G Knepley 
1467435a35e8SMatthew G Knepley   Output Parameters:
1468435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1469435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1470435a35e8SMatthew G Knepley 
1471435a35e8SMatthew G Knepley   Level: intermediate
1472435a35e8SMatthew G Knepley 
1473435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1474435a35e8SMatthew G Knepley @*/
1475435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1476435a35e8SMatthew G Knepley {
1477435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1478435a35e8SMatthew G Knepley 
1479435a35e8SMatthew G Knepley   PetscFunctionBegin;
1480435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1481435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
14828865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
14838865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1484435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1485435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
148682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1487435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1488435a35e8SMatthew G Knepley }
1489435a35e8SMatthew G Knepley 
149016621825SDmitry Karpeev 
149116621825SDmitry Karpeev #undef __FUNCT__
149216621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
149316621825SDmitry Karpeev /*@C
14948d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
14958d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
14968d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
14978d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
14988d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
149916621825SDmitry Karpeev 
150016621825SDmitry Karpeev   Not collective
150116621825SDmitry Karpeev 
150216621825SDmitry Karpeev   Input Parameter:
150316621825SDmitry Karpeev . dm - the DM object
150416621825SDmitry Karpeev 
150516621825SDmitry Karpeev   Output Parameters:
15060298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15070298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15080298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15090298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15100298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
151116621825SDmitry Karpeev 
151216621825SDmitry Karpeev   Level: intermediate
151316621825SDmitry Karpeev 
151416621825SDmitry Karpeev   Notes:
151516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
151616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
151716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
151816621825SDmitry Karpeev 
15198d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
152016621825SDmitry Karpeev @*/
15218d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
152216621825SDmitry Karpeev {
152316621825SDmitry Karpeev   PetscErrorCode      ierr;
1524be081cd6SPeter Brune   DMSubDomainHookLink link;
1525be081cd6SPeter Brune   PetscInt            i,l;
152616621825SDmitry Karpeev 
152716621825SDmitry Karpeev   PetscFunctionBegin;
152816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
152914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
15300298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
15310298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
15320298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
15330298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1534f3f0edfdSDmitry Karpeev   /*
1535f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1536f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1537f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1538f3f0edfdSDmitry Karpeev    */
1539ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
154016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1541be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
154214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
154314a18fd3SPeter Brune     if (dmlist) {
1544be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1545be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1546be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1547be081cd6SPeter Brune         }
1548d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1549e7c4fc90SDmitry Karpeev       }
155014a18fd3SPeter Brune     }
155114a18fd3SPeter Brune     if (len) *len = l;
155214a18fd3SPeter Brune   }
1553e30e807fSPeter Brune   PetscFunctionReturn(0);
1554e30e807fSPeter Brune }
1555e30e807fSPeter Brune 
1556e30e807fSPeter Brune 
1557e30e807fSPeter Brune #undef __FUNCT__
1558e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1559e30e807fSPeter Brune /*@C
1560e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1561e30e807fSPeter Brune 
1562e30e807fSPeter Brune   Not collective
1563e30e807fSPeter Brune 
1564e30e807fSPeter Brune   Input Parameters:
1565e30e807fSPeter Brune + dm - the DM object
1566e30e807fSPeter Brune . n  - the number of subdomain scatters
1567e30e807fSPeter Brune - subdms - the local subdomains
1568e30e807fSPeter Brune 
1569e30e807fSPeter Brune   Output Parameters:
1570e30e807fSPeter Brune + n     - the number of scatters returned
1571e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1572e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1573e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1574e30e807fSPeter Brune 
1575e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1576e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1577e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1578e30e807fSPeter Brune   solution and residual data.
1579e30e807fSPeter Brune 
1580e30e807fSPeter Brune   Level: developer
1581e30e807fSPeter Brune 
1582e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1583e30e807fSPeter Brune @*/
1584e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1585e30e807fSPeter Brune {
1586e30e807fSPeter Brune   PetscErrorCode ierr;
1587e30e807fSPeter Brune 
1588e30e807fSPeter Brune   PetscFunctionBegin;
1589e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1590e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1591e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1592e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
159382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1594e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1595e7c4fc90SDmitry Karpeev }
1596e7c4fc90SDmitry Karpeev 
1597731c8d9eSDmitry Karpeev #undef __FUNCT__
159847c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
159947c6ae99SBarry Smith /*@
160047c6ae99SBarry Smith   DMRefine - Refines a DM object
160147c6ae99SBarry Smith 
160247c6ae99SBarry Smith   Collective on DM
160347c6ae99SBarry Smith 
160447c6ae99SBarry Smith   Input Parameter:
160547c6ae99SBarry Smith + dm   - the DM object
160691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
160747c6ae99SBarry Smith 
160847c6ae99SBarry Smith   Output Parameter:
16090298fd71SBarry Smith . dmf - the refined DM, or NULL
1610ae0a1c52SMatthew G Knepley 
16110298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
161247c6ae99SBarry Smith 
161347c6ae99SBarry Smith   Level: developer
161447c6ae99SBarry Smith 
1615e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
161647c6ae99SBarry Smith @*/
16177087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
161847c6ae99SBarry Smith {
161947c6ae99SBarry Smith   PetscErrorCode   ierr;
1620c833c3b5SJed Brown   DMRefineHookLink link;
162147c6ae99SBarry Smith 
162247c6ae99SBarry Smith   PetscFunctionBegin;
1623732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
162447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
16254057135bSMatthew G Knepley   if (*dmf) {
162643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
16278865f1eaSKarl Rupp 
16288cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
16298865f1eaSKarl Rupp 
1630644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
16310598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1632656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
16338865f1eaSKarl Rupp 
1634e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1635c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
16368865f1eaSKarl Rupp       if (link->refinehook) {
16378865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16388865f1eaSKarl Rupp       }
1639c833c3b5SJed Brown     }
1640c833c3b5SJed Brown   }
1641c833c3b5SJed Brown   PetscFunctionReturn(0);
1642c833c3b5SJed Brown }
1643c833c3b5SJed Brown 
1644c833c3b5SJed Brown #undef __FUNCT__
1645c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1646bb9467b5SJed Brown /*@C
1647c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1648c833c3b5SJed Brown 
1649c833c3b5SJed Brown    Logically Collective
1650c833c3b5SJed Brown 
1651c833c3b5SJed Brown    Input Arguments:
1652c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1653c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1654c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16550298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1656c833c3b5SJed Brown 
1657c833c3b5SJed Brown    Calling sequence of refinehook:
1658c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1659c833c3b5SJed Brown 
1660c833c3b5SJed Brown +  coarse - coarse level DM
1661c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1662c833c3b5SJed Brown -  ctx - optional user-defined function context
1663c833c3b5SJed Brown 
1664c833c3b5SJed Brown    Calling sequence for interphook:
1665c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1666c833c3b5SJed Brown 
1667c833c3b5SJed Brown +  coarse - coarse level DM
1668c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1669c833c3b5SJed Brown .  fine - fine level DM to update
1670c833c3b5SJed Brown -  ctx - optional user-defined function context
1671c833c3b5SJed Brown 
1672c833c3b5SJed Brown    Level: advanced
1673c833c3b5SJed Brown 
1674c833c3b5SJed Brown    Notes:
1675c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1676c833c3b5SJed Brown 
1677c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1678c833c3b5SJed Brown 
1679bb9467b5SJed Brown    This function is currently not available from Fortran.
1680bb9467b5SJed Brown 
1681c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1682c833c3b5SJed Brown @*/
1683c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1684c833c3b5SJed Brown {
1685c833c3b5SJed Brown   PetscErrorCode   ierr;
1686c833c3b5SJed Brown   DMRefineHookLink link,*p;
1687c833c3b5SJed Brown 
1688c833c3b5SJed Brown   PetscFunctionBegin;
1689c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1690c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1691c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1692c833c3b5SJed Brown   link->refinehook = refinehook;
1693c833c3b5SJed Brown   link->interphook = interphook;
1694c833c3b5SJed Brown   link->ctx        = ctx;
16950298fd71SBarry Smith   link->next       = NULL;
1696c833c3b5SJed Brown   *p               = link;
1697c833c3b5SJed Brown   PetscFunctionReturn(0);
1698c833c3b5SJed Brown }
1699c833c3b5SJed Brown 
1700c833c3b5SJed Brown #undef __FUNCT__
1701c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1702c833c3b5SJed Brown /*@
1703c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1704c833c3b5SJed Brown 
1705c833c3b5SJed Brown    Collective if any hooks are
1706c833c3b5SJed Brown 
1707c833c3b5SJed Brown    Input Arguments:
1708c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1709c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1710c833c3b5SJed Brown -  fine - finer DM to update
1711c833c3b5SJed Brown 
1712c833c3b5SJed Brown    Level: developer
1713c833c3b5SJed Brown 
1714c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1715c833c3b5SJed Brown @*/
1716c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1717c833c3b5SJed Brown {
1718c833c3b5SJed Brown   PetscErrorCode   ierr;
1719c833c3b5SJed Brown   DMRefineHookLink link;
1720c833c3b5SJed Brown 
1721c833c3b5SJed Brown   PetscFunctionBegin;
1722c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
17238865f1eaSKarl Rupp     if (link->interphook) {
17248865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
17258865f1eaSKarl Rupp     }
17264057135bSMatthew G Knepley   }
172747c6ae99SBarry Smith   PetscFunctionReturn(0);
172847c6ae99SBarry Smith }
172947c6ae99SBarry Smith 
173047c6ae99SBarry Smith #undef __FUNCT__
1731eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1732eb3f98d2SBarry Smith /*@
1733eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1734eb3f98d2SBarry Smith 
1735eb3f98d2SBarry Smith     Not Collective
1736eb3f98d2SBarry Smith 
1737eb3f98d2SBarry Smith     Input Parameter:
1738eb3f98d2SBarry Smith .   dm - the DM object
1739eb3f98d2SBarry Smith 
1740eb3f98d2SBarry Smith     Output Parameter:
1741eb3f98d2SBarry Smith .   level - number of refinements
1742eb3f98d2SBarry Smith 
1743eb3f98d2SBarry Smith     Level: developer
1744eb3f98d2SBarry Smith 
17456a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1746eb3f98d2SBarry Smith 
1747eb3f98d2SBarry Smith @*/
1748eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1749eb3f98d2SBarry Smith {
1750eb3f98d2SBarry Smith   PetscFunctionBegin;
1751eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1752eb3f98d2SBarry Smith   *level = dm->levelup;
1753eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1754eb3f98d2SBarry Smith }
1755eb3f98d2SBarry Smith 
1756eb3f98d2SBarry Smith #undef __FUNCT__
1757baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1758bb9467b5SJed Brown /*@C
1759baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1760baf369e7SPeter Brune 
1761baf369e7SPeter Brune    Logically Collective
1762baf369e7SPeter Brune 
1763baf369e7SPeter Brune    Input Arguments:
1764baf369e7SPeter Brune +  dm - the DM
1765baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1766baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
17670298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1768baf369e7SPeter Brune 
1769baf369e7SPeter Brune    Calling sequence for beginhook:
1770baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1771baf369e7SPeter Brune 
1772baf369e7SPeter Brune +  dm - global DM
1773baf369e7SPeter Brune .  g - global vector
1774baf369e7SPeter Brune .  mode - mode
1775baf369e7SPeter Brune .  l - local vector
1776baf369e7SPeter Brune -  ctx - optional user-defined function context
1777baf369e7SPeter Brune 
1778baf369e7SPeter Brune 
1779baf369e7SPeter Brune    Calling sequence for endhook:
1780ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1781baf369e7SPeter Brune 
1782baf369e7SPeter Brune +  global - global DM
1783baf369e7SPeter Brune -  ctx - optional user-defined function context
1784baf369e7SPeter Brune 
1785baf369e7SPeter Brune    Level: advanced
1786baf369e7SPeter Brune 
1787baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1788baf369e7SPeter Brune @*/
1789baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1790baf369e7SPeter Brune {
1791baf369e7SPeter Brune   PetscErrorCode          ierr;
1792baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1793baf369e7SPeter Brune 
1794baf369e7SPeter Brune   PetscFunctionBegin;
1795baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1796baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1797baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1798baf369e7SPeter Brune   link->beginhook = beginhook;
1799baf369e7SPeter Brune   link->endhook   = endhook;
1800baf369e7SPeter Brune   link->ctx       = ctx;
18010298fd71SBarry Smith   link->next      = NULL;
1802baf369e7SPeter Brune   *p              = link;
1803baf369e7SPeter Brune   PetscFunctionReturn(0);
1804baf369e7SPeter Brune }
1805baf369e7SPeter Brune 
1806baf369e7SPeter Brune #undef __FUNCT__
18074c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
18084c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
18094c274da1SToby Isaac {
18104c274da1SToby Isaac   Mat cMat;
18114c274da1SToby Isaac   Vec cVec;
18124c274da1SToby Isaac   PetscSection section, cSec;
18134c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18144c274da1SToby Isaac   PetscErrorCode ierr;
18154c274da1SToby Isaac 
18164c274da1SToby Isaac   PetscFunctionBegin;
18174c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18184c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18194c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
18204c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
18217711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
18224c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
18234c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
18244c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
18254c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
18264c274da1SToby Isaac       if (dof) {
18274c274da1SToby Isaac         PetscScalar *vals;
18284c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
18294c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
18304c274da1SToby Isaac       }
18314c274da1SToby Isaac     }
18324c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
18334c274da1SToby Isaac   }
18344c274da1SToby Isaac   PetscFunctionReturn(0);
18354c274da1SToby Isaac }
18364c274da1SToby Isaac 
18374c274da1SToby Isaac #undef __FUNCT__
183847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
183947c6ae99SBarry Smith /*@
184047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
184147c6ae99SBarry Smith 
184247c6ae99SBarry Smith     Neighbor-wise Collective on DM
184347c6ae99SBarry Smith 
184447c6ae99SBarry Smith     Input Parameters:
184547c6ae99SBarry Smith +   dm - the DM object
184647c6ae99SBarry Smith .   g - the global vector
184747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
184847c6ae99SBarry Smith -   l - the local vector
184947c6ae99SBarry Smith 
185047c6ae99SBarry Smith 
185147c6ae99SBarry Smith     Level: beginner
185247c6ae99SBarry Smith 
1853e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
185447c6ae99SBarry Smith 
185547c6ae99SBarry Smith @*/
18567087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
185747c6ae99SBarry Smith {
18587128ae9fSMatthew G Knepley   PetscSF                 sf;
185947c6ae99SBarry Smith   PetscErrorCode          ierr;
1860baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
186147c6ae99SBarry Smith 
186247c6ae99SBarry Smith   PetscFunctionBegin;
1863171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1864baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
18658865f1eaSKarl Rupp     if (link->beginhook) {
18668865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
18678865f1eaSKarl Rupp     }
1868baf369e7SPeter Brune   }
18697128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18707128ae9fSMatthew G Knepley   if (sf) {
1871ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1872ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
18737128ae9fSMatthew G Knepley 
187482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18757128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1876ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18777128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18787128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1879ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18807128ae9fSMatthew G Knepley   } else {
1881843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18827128ae9fSMatthew G Knepley   }
188347c6ae99SBarry Smith   PetscFunctionReturn(0);
188447c6ae99SBarry Smith }
188547c6ae99SBarry Smith 
188647c6ae99SBarry Smith #undef __FUNCT__
188747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
188847c6ae99SBarry Smith /*@
188947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
189047c6ae99SBarry Smith 
189147c6ae99SBarry Smith     Neighbor-wise Collective on DM
189247c6ae99SBarry Smith 
189347c6ae99SBarry Smith     Input Parameters:
189447c6ae99SBarry Smith +   dm - the DM object
189547c6ae99SBarry Smith .   g - the global vector
189647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
189747c6ae99SBarry Smith -   l - the local vector
189847c6ae99SBarry Smith 
189947c6ae99SBarry Smith 
190047c6ae99SBarry Smith     Level: beginner
190147c6ae99SBarry Smith 
1902e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
190347c6ae99SBarry Smith 
190447c6ae99SBarry Smith @*/
19057087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
190647c6ae99SBarry Smith {
19077128ae9fSMatthew G Knepley   PetscSF                 sf;
190847c6ae99SBarry Smith   PetscErrorCode          ierr;
1909ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1910ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1911baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
191247c6ae99SBarry Smith 
191347c6ae99SBarry Smith   PetscFunctionBegin;
1914171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19157128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19167128ae9fSMatthew G Knepley   if (sf) {
191782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19187128ae9fSMatthew G Knepley 
19197128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1920ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19217128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19227128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1923ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19247128ae9fSMatthew G Knepley   } else {
1925843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19267128ae9fSMatthew G Knepley   }
19274c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1928baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1929baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1930baf369e7SPeter Brune   }
193147c6ae99SBarry Smith   PetscFunctionReturn(0);
193247c6ae99SBarry Smith }
193347c6ae99SBarry Smith 
193447c6ae99SBarry Smith #undef __FUNCT__
1935d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1936d4d07f1eSToby Isaac /*@C
1937d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1938d4d07f1eSToby Isaac 
1939d4d07f1eSToby Isaac    Logically Collective
1940d4d07f1eSToby Isaac 
1941d4d07f1eSToby Isaac    Input Arguments:
1942d4d07f1eSToby Isaac +  dm - the DM
1943d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1944d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1945d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1946d4d07f1eSToby Isaac 
1947d4d07f1eSToby Isaac    Calling sequence for beginhook:
1948d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1949d4d07f1eSToby Isaac 
1950d4d07f1eSToby Isaac +  dm - global DM
1951d4d07f1eSToby Isaac .  l - local vector
1952d4d07f1eSToby Isaac .  mode - mode
1953d4d07f1eSToby Isaac .  g - global vector
1954d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1955d4d07f1eSToby Isaac 
1956d4d07f1eSToby Isaac 
1957d4d07f1eSToby Isaac    Calling sequence for endhook:
1958d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1959d4d07f1eSToby Isaac 
1960d4d07f1eSToby Isaac +  global - global DM
1961d4d07f1eSToby Isaac .  l - local vector
1962d4d07f1eSToby Isaac .  mode - mode
1963d4d07f1eSToby Isaac .  g - global vector
1964d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1965d4d07f1eSToby Isaac 
1966d4d07f1eSToby Isaac    Level: advanced
1967d4d07f1eSToby Isaac 
1968d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1969d4d07f1eSToby Isaac @*/
1970d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1971d4d07f1eSToby Isaac {
1972d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1973d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1974d4d07f1eSToby Isaac 
1975d4d07f1eSToby Isaac   PetscFunctionBegin;
1976d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1977d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1978d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1979d4d07f1eSToby Isaac   link->beginhook = beginhook;
1980d4d07f1eSToby Isaac   link->endhook   = endhook;
1981d4d07f1eSToby Isaac   link->ctx       = ctx;
1982d4d07f1eSToby Isaac   link->next      = NULL;
1983d4d07f1eSToby Isaac   *p              = link;
1984d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1985d4d07f1eSToby Isaac }
1986d4d07f1eSToby Isaac 
1987d4d07f1eSToby Isaac #undef __FUNCT__
19884c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
19894c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
19904c274da1SToby Isaac {
19914c274da1SToby Isaac   Mat cMat;
19924c274da1SToby Isaac   Vec cVec;
19934c274da1SToby Isaac   PetscSection section, cSec;
19944c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19954c274da1SToby Isaac   PetscErrorCode ierr;
19964c274da1SToby Isaac 
19974c274da1SToby Isaac   PetscFunctionBegin;
19984c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19994c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20004c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
20014c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
20027711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20034c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20044c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20054c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20064c274da1SToby Isaac       if (dof) {
20074c274da1SToby Isaac         PetscInt d;
20084c274da1SToby Isaac         PetscScalar *vals;
20094c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
20104c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
20114c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
20124c274da1SToby Isaac          * we just extracted */
20134c274da1SToby Isaac         for (d = 0; d < dof; d++) {
20144c274da1SToby Isaac           vals[d] = 0.;
20154c274da1SToby Isaac         }
20164c274da1SToby Isaac       }
20174c274da1SToby Isaac     }
20184c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
20194c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
20204c274da1SToby Isaac   }
20214c274da1SToby Isaac   PetscFunctionReturn(0);
20224c274da1SToby Isaac }
20234c274da1SToby Isaac 
20244c274da1SToby Isaac #undef __FUNCT__
20259a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
202647c6ae99SBarry Smith /*@
20279a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
20289a42bb27SBarry Smith 
20299a42bb27SBarry Smith     Neighbor-wise Collective on DM
20309a42bb27SBarry Smith 
20319a42bb27SBarry Smith     Input Parameters:
20329a42bb27SBarry Smith +   dm - the DM object
2033f6813fd5SJed Brown .   l - the local vector
20341eb28f2eSBarry 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.
20351eb28f2eSBarry Smith -   g - the global vector
20369a42bb27SBarry Smith 
2037d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
203884330215SMatthew 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.
20399a42bb27SBarry Smith 
20409a42bb27SBarry Smith     Level: beginner
20419a42bb27SBarry Smith 
2042e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
20439a42bb27SBarry Smith 
20449a42bb27SBarry Smith @*/
20457087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
20469a42bb27SBarry Smith {
20477128ae9fSMatthew G Knepley   PetscSF                 sf;
204884330215SMatthew G. Knepley   PetscSection            s, gs;
2049d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2050ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2051ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
205284330215SMatthew G. Knepley   PetscBool               isInsert;
205384330215SMatthew G. Knepley   PetscErrorCode          ierr;
20549a42bb27SBarry Smith 
20559a42bb27SBarry Smith   PetscFunctionBegin;
2056171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2057d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2058d4d07f1eSToby Isaac     if (link->beginhook) {
2059d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2060d4d07f1eSToby Isaac     }
2061d4d07f1eSToby Isaac   }
20624c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
20637128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
206484330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20657128ae9fSMatthew G Knepley   switch (mode) {
20667128ae9fSMatthew G Knepley   case INSERT_VALUES:
20677128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
206884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20697128ae9fSMatthew G Knepley   case ADD_VALUES:
20707128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
207184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20727128ae9fSMatthew G Knepley   default:
207382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20747128ae9fSMatthew G Knepley   }
207584330215SMatthew G. Knepley   if (sf && !isInsert) {
2076ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20777128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2078a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2079ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
208084330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
208184330215SMatthew G. Knepley   } else if (s && isInsert) {
208284330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
208384330215SMatthew G. Knepley 
208484330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
208584330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
208684330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2087ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
208884330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
208984330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2090b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
209184330215SMatthew G. Knepley 
209284330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
209303442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
209484330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2095b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
209684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
209784330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2098b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
209903442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2100b3b16f48SMatthew 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);
2101b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2102b3b16f48SMatthew G. Knepley       if (!gcdof) {
210384330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2104b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2105b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
210684330215SMatthew G. Knepley         const PetscInt *cdofs;
210784330215SMatthew G. Knepley         PetscInt        cind = 0;
210884330215SMatthew G. Knepley 
210984330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2110b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
211184330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2112b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
211384330215SMatthew G. Knepley         }
2114b3b16f48SMatthew 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);
211584330215SMatthew G. Knepley     }
2116ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21177128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
21187128ae9fSMatthew G Knepley   } else {
2119843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21207128ae9fSMatthew G Knepley   }
21219a42bb27SBarry Smith   PetscFunctionReturn(0);
21229a42bb27SBarry Smith }
21239a42bb27SBarry Smith 
21249a42bb27SBarry Smith #undef __FUNCT__
21259a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
21269a42bb27SBarry Smith /*@
21279a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
212847c6ae99SBarry Smith 
212947c6ae99SBarry Smith     Neighbor-wise Collective on DM
213047c6ae99SBarry Smith 
213147c6ae99SBarry Smith     Input Parameters:
213247c6ae99SBarry Smith +   dm - the DM object
2133f6813fd5SJed Brown .   l - the local vector
213447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2135f6813fd5SJed Brown -   g - the global vector
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith 
213847c6ae99SBarry Smith     Level: beginner
213947c6ae99SBarry Smith 
2140e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
214147c6ae99SBarry Smith 
214247c6ae99SBarry Smith @*/
21437087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
214447c6ae99SBarry Smith {
21457128ae9fSMatthew G Knepley   PetscSF                 sf;
214684330215SMatthew G. Knepley   PetscSection            s;
2147d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
214884330215SMatthew G. Knepley   PetscBool               isInsert;
214984330215SMatthew G. Knepley   PetscErrorCode          ierr;
215047c6ae99SBarry Smith 
215147c6ae99SBarry Smith   PetscFunctionBegin;
2152171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21537128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
215484330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21557128ae9fSMatthew G Knepley   switch (mode) {
21567128ae9fSMatthew G Knepley   case INSERT_VALUES:
21577128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
215884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21597128ae9fSMatthew G Knepley   case ADD_VALUES:
21607128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
216184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21627128ae9fSMatthew G Knepley   default:
216382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21647128ae9fSMatthew G Knepley   }
216584330215SMatthew G. Knepley   if (sf && !isInsert) {
2166ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2167ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
216884330215SMatthew G. Knepley 
2169ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21707128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2171a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2172ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21737128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
217484330215SMatthew G. Knepley   } else if (s && isInsert) {
21757128ae9fSMatthew G Knepley   } else {
2176843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21777128ae9fSMatthew G Knepley   }
2178d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2179d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2180d4d07f1eSToby Isaac   }
218147c6ae99SBarry Smith   PetscFunctionReturn(0);
218247c6ae99SBarry Smith }
218347c6ae99SBarry Smith 
218447c6ae99SBarry Smith #undef __FUNCT__
2185f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2186f089877aSRichard Tran Mills /*@
2187bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2188bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2189d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2190f089877aSRichard Tran Mills 
2191bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2192f089877aSRichard Tran Mills 
2193f089877aSRichard Tran Mills    Input Parameters:
2194f089877aSRichard Tran Mills +  dm - the DM object
2195bc0a1609SRichard Tran Mills .  g - the original local vector
2196bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2197f089877aSRichard Tran Mills 
2198bc0a1609SRichard Tran Mills    Output Parameter:
2199bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2200f089877aSRichard Tran Mills 
2201f089877aSRichard Tran Mills    Level: intermediate
2202f089877aSRichard Tran Mills 
2203bc0a1609SRichard Tran Mills    Notes:
2204bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2205bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2206bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2207bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2208bc0a1609SRichard Tran Mills 
2209bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2210bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2211f089877aSRichard Tran Mills 
2212f089877aSRichard Tran Mills @*/
2213f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2214f089877aSRichard Tran Mills {
2215f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2216f089877aSRichard Tran Mills 
2217f089877aSRichard Tran Mills   PetscFunctionBegin;
2218f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2219f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2220f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2221f089877aSRichard Tran Mills }
2222f089877aSRichard Tran Mills 
2223f089877aSRichard Tran Mills #undef __FUNCT__
2224f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2225f089877aSRichard Tran Mills /*@
2226bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2227bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2228d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2229f089877aSRichard Tran Mills 
2230bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2231f089877aSRichard Tran Mills 
2232f089877aSRichard Tran Mills    Input Parameters:
2233bc0a1609SRichard Tran Mills +  da - the DM object
2234bc0a1609SRichard Tran Mills .  g - the original local vector
2235bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2236f089877aSRichard Tran Mills 
2237bc0a1609SRichard Tran Mills    Output Parameter:
2238bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2239f089877aSRichard Tran Mills 
2240f089877aSRichard Tran Mills    Level: intermediate
2241f089877aSRichard Tran Mills 
2242bc0a1609SRichard Tran Mills    Notes:
2243bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2244bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2245bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2246bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2247bc0a1609SRichard Tran Mills 
2248bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2249bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2250f089877aSRichard Tran Mills 
2251f089877aSRichard Tran Mills @*/
2252f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2253f089877aSRichard Tran Mills {
2254f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2255f089877aSRichard Tran Mills 
2256f089877aSRichard Tran Mills   PetscFunctionBegin;
2257f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2258f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2259f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2260f089877aSRichard Tran Mills }
2261f089877aSRichard Tran Mills 
2262f089877aSRichard Tran Mills 
2263f089877aSRichard Tran Mills #undef __FUNCT__
226447c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
226547c6ae99SBarry Smith /*@
226647c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
226747c6ae99SBarry Smith 
226847c6ae99SBarry Smith     Collective on DM
226947c6ae99SBarry Smith 
227047c6ae99SBarry Smith     Input Parameter:
227147c6ae99SBarry Smith +   dm - the DM object
227291d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
227347c6ae99SBarry Smith 
227447c6ae99SBarry Smith     Output Parameter:
227547c6ae99SBarry Smith .   dmc - the coarsened DM
227647c6ae99SBarry Smith 
227747c6ae99SBarry Smith     Level: developer
227847c6ae99SBarry Smith 
2279e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
228047c6ae99SBarry Smith 
228147c6ae99SBarry Smith @*/
22827087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
228347c6ae99SBarry Smith {
228447c6ae99SBarry Smith   PetscErrorCode    ierr;
2285b17ce1afSJed Brown   DMCoarsenHookLink link;
228647c6ae99SBarry Smith 
228747c6ae99SBarry Smith   PetscFunctionBegin;
2288171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
228947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
229047c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
22918892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2292a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
229343842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
22948cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2295644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
22960598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2297656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2298e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2299b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2300b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2301b17ce1afSJed Brown   }
230247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2303b17ce1afSJed Brown   PetscFunctionReturn(0);
2304b17ce1afSJed Brown }
2305b17ce1afSJed Brown 
2306b17ce1afSJed Brown #undef __FUNCT__
2307b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2308bb9467b5SJed Brown /*@C
2309b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2310b17ce1afSJed Brown 
2311b17ce1afSJed Brown    Logically Collective
2312b17ce1afSJed Brown 
2313b17ce1afSJed Brown    Input Arguments:
2314b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2315b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2316b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
23170298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2318b17ce1afSJed Brown 
2319b17ce1afSJed Brown    Calling sequence of coarsenhook:
2320b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2321b17ce1afSJed Brown 
2322b17ce1afSJed Brown +  fine - fine level DM
2323b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2324b17ce1afSJed Brown -  ctx - optional user-defined function context
2325b17ce1afSJed Brown 
2326b17ce1afSJed Brown    Calling sequence for restricthook:
2327c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2328b17ce1afSJed Brown 
2329b17ce1afSJed Brown +  fine - fine level DM
2330b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2331c833c3b5SJed Brown .  rscale - scaling vector for restriction
2332c833c3b5SJed Brown .  inject - matrix restricting by injection
2333b17ce1afSJed Brown .  coarse - coarse level DM to update
2334b17ce1afSJed Brown -  ctx - optional user-defined function context
2335b17ce1afSJed Brown 
2336b17ce1afSJed Brown    Level: advanced
2337b17ce1afSJed Brown 
2338b17ce1afSJed Brown    Notes:
2339b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2340b17ce1afSJed Brown 
2341b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2342b17ce1afSJed Brown 
2343b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2344b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2345b17ce1afSJed Brown 
2346bb9467b5SJed Brown    This function is currently not available from Fortran.
2347bb9467b5SJed Brown 
2348c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2349b17ce1afSJed Brown @*/
2350b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2351b17ce1afSJed Brown {
2352b17ce1afSJed Brown   PetscErrorCode    ierr;
2353b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2354b17ce1afSJed Brown 
2355b17ce1afSJed Brown   PetscFunctionBegin;
2356b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
23576bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2358b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2359b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2360b17ce1afSJed Brown   link->restricthook = restricthook;
2361b17ce1afSJed Brown   link->ctx          = ctx;
23620298fd71SBarry Smith   link->next         = NULL;
2363b17ce1afSJed Brown   *p                 = link;
2364b17ce1afSJed Brown   PetscFunctionReturn(0);
2365b17ce1afSJed Brown }
2366b17ce1afSJed Brown 
2367b17ce1afSJed Brown #undef __FUNCT__
2368b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2369b17ce1afSJed Brown /*@
2370b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2371b17ce1afSJed Brown 
2372b17ce1afSJed Brown    Collective if any hooks are
2373b17ce1afSJed Brown 
2374b17ce1afSJed Brown    Input Arguments:
2375b17ce1afSJed Brown +  fine - finer DM to use as a base
2376b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2377b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2378b17ce1afSJed Brown -  coarse - coarer DM to update
2379b17ce1afSJed Brown 
2380b17ce1afSJed Brown    Level: developer
2381b17ce1afSJed Brown 
2382b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2383b17ce1afSJed Brown @*/
2384b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2385b17ce1afSJed Brown {
2386b17ce1afSJed Brown   PetscErrorCode    ierr;
2387b17ce1afSJed Brown   DMCoarsenHookLink link;
2388b17ce1afSJed Brown 
2389b17ce1afSJed Brown   PetscFunctionBegin;
2390b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
23918865f1eaSKarl Rupp     if (link->restricthook) {
23928865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
23938865f1eaSKarl Rupp     }
2394b17ce1afSJed Brown   }
239547c6ae99SBarry Smith   PetscFunctionReturn(0);
239647c6ae99SBarry Smith }
239747c6ae99SBarry Smith 
239847c6ae99SBarry Smith #undef __FUNCT__
2399be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2400bb9467b5SJed Brown /*@C
2401be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
24025dbd56e3SPeter Brune 
24035dbd56e3SPeter Brune    Logically Collective
24045dbd56e3SPeter Brune 
24055dbd56e3SPeter Brune    Input Arguments:
24065dbd56e3SPeter Brune +  global - global DM
2407ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
24085dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
24090298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
24105dbd56e3SPeter Brune 
2411ec4806b8SPeter Brune 
2412ec4806b8SPeter Brune    Calling sequence for ddhook:
2413ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2414ec4806b8SPeter Brune 
2415ec4806b8SPeter Brune +  global - global DM
2416ec4806b8SPeter Brune .  block  - block DM
2417ec4806b8SPeter Brune -  ctx - optional user-defined function context
2418ec4806b8SPeter Brune 
24195dbd56e3SPeter Brune    Calling sequence for restricthook:
2420ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
24215dbd56e3SPeter Brune 
24225dbd56e3SPeter Brune +  global - global DM
24235dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
24245dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2425ec4806b8SPeter Brune .  block  - block DM
24265dbd56e3SPeter Brune -  ctx - optional user-defined function context
24275dbd56e3SPeter Brune 
24285dbd56e3SPeter Brune    Level: advanced
24295dbd56e3SPeter Brune 
24305dbd56e3SPeter Brune    Notes:
2431ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
24325dbd56e3SPeter Brune 
24335dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
24345dbd56e3SPeter Brune 
24355dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2436ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
24375dbd56e3SPeter Brune 
2438bb9467b5SJed Brown    This function is currently not available from Fortran.
2439bb9467b5SJed Brown 
24405dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
24415dbd56e3SPeter Brune @*/
2442be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
24435dbd56e3SPeter Brune {
24445dbd56e3SPeter Brune   PetscErrorCode      ierr;
2445be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
24465dbd56e3SPeter Brune 
24475dbd56e3SPeter Brune   PetscFunctionBegin;
24485dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2449be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2450be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
24515dbd56e3SPeter Brune   link->restricthook = restricthook;
2452be081cd6SPeter Brune   link->ddhook       = ddhook;
24535dbd56e3SPeter Brune   link->ctx          = ctx;
24540298fd71SBarry Smith   link->next         = NULL;
24555dbd56e3SPeter Brune   *p                 = link;
24565dbd56e3SPeter Brune   PetscFunctionReturn(0);
24575dbd56e3SPeter Brune }
24585dbd56e3SPeter Brune 
24595dbd56e3SPeter Brune #undef __FUNCT__
2460be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
24615dbd56e3SPeter Brune /*@
2462be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
24635dbd56e3SPeter Brune 
24645dbd56e3SPeter Brune    Collective if any hooks are
24655dbd56e3SPeter Brune 
24665dbd56e3SPeter Brune    Input Arguments:
24675dbd56e3SPeter Brune +  fine - finer DM to use as a base
2468be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2469be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
24705dbd56e3SPeter Brune -  coarse - coarer DM to update
24715dbd56e3SPeter Brune 
24725dbd56e3SPeter Brune    Level: developer
24735dbd56e3SPeter Brune 
24745dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
24755dbd56e3SPeter Brune @*/
2476be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
24775dbd56e3SPeter Brune {
24785dbd56e3SPeter Brune   PetscErrorCode      ierr;
2479be081cd6SPeter Brune   DMSubDomainHookLink link;
24805dbd56e3SPeter Brune 
24815dbd56e3SPeter Brune   PetscFunctionBegin;
2482be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
24838865f1eaSKarl Rupp     if (link->restricthook) {
24848865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
24858865f1eaSKarl Rupp     }
24865dbd56e3SPeter Brune   }
24875dbd56e3SPeter Brune   PetscFunctionReturn(0);
24885dbd56e3SPeter Brune }
24895dbd56e3SPeter Brune 
24905dbd56e3SPeter Brune #undef __FUNCT__
24915fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
24925fe1f584SPeter Brune /*@
24936a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
24945fe1f584SPeter Brune 
24955fe1f584SPeter Brune     Not Collective
24965fe1f584SPeter Brune 
24975fe1f584SPeter Brune     Input Parameter:
24985fe1f584SPeter Brune .   dm - the DM object
24995fe1f584SPeter Brune 
25005fe1f584SPeter Brune     Output Parameter:
25016a7d9d85SPeter Brune .   level - number of coarsenings
25025fe1f584SPeter Brune 
25035fe1f584SPeter Brune     Level: developer
25045fe1f584SPeter Brune 
25056a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
25065fe1f584SPeter Brune 
25075fe1f584SPeter Brune @*/
25085fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
25095fe1f584SPeter Brune {
25105fe1f584SPeter Brune   PetscFunctionBegin;
25115fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25125fe1f584SPeter Brune   *level = dm->leveldown;
25135fe1f584SPeter Brune   PetscFunctionReturn(0);
25145fe1f584SPeter Brune }
25155fe1f584SPeter Brune 
25165fe1f584SPeter Brune 
25175fe1f584SPeter Brune 
25185fe1f584SPeter Brune #undef __FUNCT__
251947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
252047c6ae99SBarry Smith /*@C
252147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
252247c6ae99SBarry Smith 
252347c6ae99SBarry Smith     Collective on DM
252447c6ae99SBarry Smith 
252547c6ae99SBarry Smith     Input Parameter:
252647c6ae99SBarry Smith +   dm - the DM object
252747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
252847c6ae99SBarry Smith 
252947c6ae99SBarry Smith     Output Parameter:
253047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
253147c6ae99SBarry Smith 
253247c6ae99SBarry Smith     Level: developer
253347c6ae99SBarry Smith 
2534e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
253547c6ae99SBarry Smith 
253647c6ae99SBarry Smith @*/
25377087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
253847c6ae99SBarry Smith {
253947c6ae99SBarry Smith   PetscErrorCode ierr;
254047c6ae99SBarry Smith 
254147c6ae99SBarry Smith   PetscFunctionBegin;
2542171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2543ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
254447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
254547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
254647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
254747c6ae99SBarry Smith   } else if (dm->ops->refine) {
254847c6ae99SBarry Smith     PetscInt i;
254947c6ae99SBarry Smith 
2550ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
255147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2552ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
255347c6ae99SBarry Smith     }
2554ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
255547c6ae99SBarry Smith   PetscFunctionReturn(0);
255647c6ae99SBarry Smith }
255747c6ae99SBarry Smith 
255847c6ae99SBarry Smith #undef __FUNCT__
255947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
256047c6ae99SBarry Smith /*@C
256147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
256247c6ae99SBarry Smith 
256347c6ae99SBarry Smith     Collective on DM
256447c6ae99SBarry Smith 
256547c6ae99SBarry Smith     Input Parameter:
256647c6ae99SBarry Smith +   dm - the DM object
256747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
256847c6ae99SBarry Smith 
256947c6ae99SBarry Smith     Output Parameter:
257047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
257147c6ae99SBarry Smith 
257247c6ae99SBarry Smith     Level: developer
257347c6ae99SBarry Smith 
2574e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
257547c6ae99SBarry Smith 
257647c6ae99SBarry Smith @*/
25777087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
257847c6ae99SBarry Smith {
257947c6ae99SBarry Smith   PetscErrorCode ierr;
258047c6ae99SBarry Smith 
258147c6ae99SBarry Smith   PetscFunctionBegin;
2582171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2583ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
258447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
258547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
258647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
258747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
258847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
258947c6ae99SBarry Smith     PetscInt i;
259047c6ae99SBarry Smith 
2591ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
259247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2593ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
259447c6ae99SBarry Smith     }
2595ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
259647c6ae99SBarry Smith   PetscFunctionReturn(0);
259747c6ae99SBarry Smith }
259847c6ae99SBarry Smith 
259947c6ae99SBarry Smith #undef __FUNCT__
2600e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
260147c6ae99SBarry Smith /*@
2602e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
260347c6ae99SBarry Smith    grids associated with two DMs.
260447c6ae99SBarry Smith 
260547c6ae99SBarry Smith    Collective on DM
260647c6ae99SBarry Smith 
260747c6ae99SBarry Smith    Input Parameters:
260847c6ae99SBarry Smith +  dmc - the coarse grid DM
260947c6ae99SBarry Smith -  dmf - the fine grid DM
261047c6ae99SBarry Smith 
261147c6ae99SBarry Smith    Output Parameters:
261247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
261347c6ae99SBarry Smith 
261447c6ae99SBarry Smith    Level: intermediate
261547c6ae99SBarry Smith 
261647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
261747c6ae99SBarry Smith 
2618e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
261947c6ae99SBarry Smith @*/
2620e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
262147c6ae99SBarry Smith {
262247c6ae99SBarry Smith   PetscErrorCode ierr;
262347c6ae99SBarry Smith 
262447c6ae99SBarry Smith   PetscFunctionBegin;
2625171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2626171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
262747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
262847c6ae99SBarry Smith   PetscFunctionReturn(0);
262947c6ae99SBarry Smith }
263047c6ae99SBarry Smith 
263147c6ae99SBarry Smith #undef __FUNCT__
26321a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
26331a266240SBarry Smith /*@C
26341a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
26351a266240SBarry Smith 
26361a266240SBarry Smith     Not Collective
26371a266240SBarry Smith 
26381a266240SBarry Smith     Input Parameters:
26391a266240SBarry Smith +   dm - the DM object
26401a266240SBarry Smith -   destroy - the destroy function
26411a266240SBarry Smith 
26421a266240SBarry Smith     Level: intermediate
26431a266240SBarry Smith 
2644e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
26451a266240SBarry Smith 
2646f07f9ceaSJed Brown @*/
26471a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
26481a266240SBarry Smith {
26491a266240SBarry Smith   PetscFunctionBegin;
2650171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26511a266240SBarry Smith   dm->ctxdestroy = destroy;
26521a266240SBarry Smith   PetscFunctionReturn(0);
26531a266240SBarry Smith }
26541a266240SBarry Smith 
26551a266240SBarry Smith #undef __FUNCT__
26561b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2657b07ff414SBarry Smith /*@
26581b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
265947c6ae99SBarry Smith 
266047c6ae99SBarry Smith     Not Collective
266147c6ae99SBarry Smith 
266247c6ae99SBarry Smith     Input Parameters:
266347c6ae99SBarry Smith +   dm - the DM object
266447c6ae99SBarry Smith -   ctx - the user context
266547c6ae99SBarry Smith 
266647c6ae99SBarry Smith     Level: intermediate
266747c6ae99SBarry Smith 
2668e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
266947c6ae99SBarry Smith 
267047c6ae99SBarry Smith @*/
26711b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
267247c6ae99SBarry Smith {
267347c6ae99SBarry Smith   PetscFunctionBegin;
2674171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
267547c6ae99SBarry Smith   dm->ctx = ctx;
267647c6ae99SBarry Smith   PetscFunctionReturn(0);
267747c6ae99SBarry Smith }
267847c6ae99SBarry Smith 
267947c6ae99SBarry Smith #undef __FUNCT__
26801b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
268147c6ae99SBarry Smith /*@
26821b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
268347c6ae99SBarry Smith 
268447c6ae99SBarry Smith     Not Collective
268547c6ae99SBarry Smith 
268647c6ae99SBarry Smith     Input Parameter:
268747c6ae99SBarry Smith .   dm - the DM object
268847c6ae99SBarry Smith 
268947c6ae99SBarry Smith     Output Parameter:
269047c6ae99SBarry Smith .   ctx - the user context
269147c6ae99SBarry Smith 
269247c6ae99SBarry Smith     Level: intermediate
269347c6ae99SBarry Smith 
2694e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
269547c6ae99SBarry Smith 
269647c6ae99SBarry Smith @*/
26971b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
269847c6ae99SBarry Smith {
269947c6ae99SBarry Smith   PetscFunctionBegin;
2700171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27011b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
270247c6ae99SBarry Smith   PetscFunctionReturn(0);
270347c6ae99SBarry Smith }
270447c6ae99SBarry Smith 
270547c6ae99SBarry Smith #undef __FUNCT__
270608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
270708da532bSDmitry Karpeev /*@C
2708df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
270908da532bSDmitry Karpeev 
271008da532bSDmitry Karpeev     Logically Collective on DM
271108da532bSDmitry Karpeev 
271208da532bSDmitry Karpeev     Input Parameter:
271308da532bSDmitry Karpeev +   dm - the DM object
27140298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
271508da532bSDmitry Karpeev 
271608da532bSDmitry Karpeev     Level: intermediate
271708da532bSDmitry Karpeev 
2718835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
271908da532bSDmitry Karpeev          DMSetJacobian()
272008da532bSDmitry Karpeev 
272108da532bSDmitry Karpeev @*/
272208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
272308da532bSDmitry Karpeev {
272408da532bSDmitry Karpeev   PetscFunctionBegin;
272508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
272608da532bSDmitry Karpeev   PetscFunctionReturn(0);
272708da532bSDmitry Karpeev }
272808da532bSDmitry Karpeev 
272908da532bSDmitry Karpeev #undef __FUNCT__
273008da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
273108da532bSDmitry Karpeev /*@
273208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
273308da532bSDmitry Karpeev 
273408da532bSDmitry Karpeev     Not Collective
273508da532bSDmitry Karpeev 
273608da532bSDmitry Karpeev     Input Parameter:
273708da532bSDmitry Karpeev .   dm - the DM object to destroy
273808da532bSDmitry Karpeev 
273908da532bSDmitry Karpeev     Output Parameter:
274008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
274108da532bSDmitry Karpeev 
274208da532bSDmitry Karpeev     Level: developer
274308da532bSDmitry Karpeev 
274474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
274508da532bSDmitry Karpeev 
274608da532bSDmitry Karpeev @*/
274708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
274808da532bSDmitry Karpeev {
274908da532bSDmitry Karpeev   PetscFunctionBegin;
275008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
275108da532bSDmitry Karpeev   PetscFunctionReturn(0);
275208da532bSDmitry Karpeev }
275308da532bSDmitry Karpeev 
275408da532bSDmitry Karpeev #undef __FUNCT__
275508da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
275608da532bSDmitry Karpeev /*@C
275708da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
275808da532bSDmitry Karpeev 
275908da532bSDmitry Karpeev     Logically Collective on DM
276008da532bSDmitry Karpeev 
276108da532bSDmitry Karpeev     Input Parameters:
2762907376e6SBarry Smith .   dm - the DM object
276308da532bSDmitry Karpeev 
276408da532bSDmitry Karpeev     Output parameters:
276508da532bSDmitry Karpeev +   xl - lower bound
276608da532bSDmitry Karpeev -   xu - upper bound
276708da532bSDmitry Karpeev 
2768907376e6SBarry Smith     Level: advanced
2769907376e6SBarry Smith 
2770907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
277108da532bSDmitry Karpeev 
277274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
277308da532bSDmitry Karpeev 
277408da532bSDmitry Karpeev @*/
277508da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
277608da532bSDmitry Karpeev {
277708da532bSDmitry Karpeev   PetscErrorCode ierr;
27785fd66863SKarl Rupp 
277908da532bSDmitry Karpeev   PetscFunctionBegin;
278008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
278108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
278208da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
278308da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
27848865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
278508da532bSDmitry Karpeev   PetscFunctionReturn(0);
278608da532bSDmitry Karpeev }
278708da532bSDmitry Karpeev 
278808da532bSDmitry Karpeev #undef __FUNCT__
2789b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2790b0ae01b7SPeter Brune /*@
2791b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2792b0ae01b7SPeter Brune 
2793b0ae01b7SPeter Brune     Not Collective
2794b0ae01b7SPeter Brune 
2795b0ae01b7SPeter Brune     Input Parameter:
2796b0ae01b7SPeter Brune .   dm - the DM object
2797b0ae01b7SPeter Brune 
2798b0ae01b7SPeter Brune     Output Parameter:
2799b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2800b0ae01b7SPeter Brune 
2801b0ae01b7SPeter Brune     Level: developer
2802b0ae01b7SPeter Brune 
2803b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2804b0ae01b7SPeter Brune 
2805b0ae01b7SPeter Brune @*/
2806b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2807b0ae01b7SPeter Brune {
2808b0ae01b7SPeter Brune   PetscFunctionBegin;
2809b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2810b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2811b0ae01b7SPeter Brune }
2812b0ae01b7SPeter Brune 
2813b0ae01b7SPeter Brune #undef  __FUNCT__
281408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2815748fac09SDmitry Karpeev /*@C
281608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
281708da532bSDmitry Karpeev 
281808da532bSDmitry Karpeev     Collective on DM
281908da532bSDmitry Karpeev 
282008da532bSDmitry Karpeev     Input Parameter:
282108da532bSDmitry Karpeev +   dm - the DM object
28220298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
282308da532bSDmitry Karpeev 
282408da532bSDmitry Karpeev     Level: developer
282508da532bSDmitry Karpeev 
282674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
282708da532bSDmitry Karpeev 
282808da532bSDmitry Karpeev @*/
282908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
283008da532bSDmitry Karpeev {
283108da532bSDmitry Karpeev   PetscErrorCode ierr;
28325fd66863SKarl Rupp 
283308da532bSDmitry Karpeev   PetscFunctionBegin;
283408da532bSDmitry Karpeev   if (x) {
283508da532bSDmitry Karpeev     if (!dm->x) {
283608da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
283708da532bSDmitry Karpeev     }
283808da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
28398865f1eaSKarl Rupp   } else if (dm->x) {
284008da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
284108da532bSDmitry Karpeev   }
284208da532bSDmitry Karpeev   PetscFunctionReturn(0);
284308da532bSDmitry Karpeev }
284408da532bSDmitry Karpeev 
28450298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2846264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2847264ace61SBarry Smith 
2848264ace61SBarry Smith #undef __FUNCT__
2849264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2850264ace61SBarry Smith /*@C
2851264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2852264ace61SBarry Smith 
2853264ace61SBarry Smith   Collective on DM
2854264ace61SBarry Smith 
2855264ace61SBarry Smith   Input Parameters:
2856264ace61SBarry Smith + dm     - The DM object
2857264ace61SBarry Smith - method - The name of the DM type
2858264ace61SBarry Smith 
2859264ace61SBarry Smith   Options Database Key:
2860264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2861264ace61SBarry Smith 
2862264ace61SBarry Smith   Notes:
2863e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2864264ace61SBarry Smith 
2865264ace61SBarry Smith   Level: intermediate
2866264ace61SBarry Smith 
2867264ace61SBarry Smith .keywords: DM, set, type
2868264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2869264ace61SBarry Smith @*/
287019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2871264ace61SBarry Smith {
2872264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2873264ace61SBarry Smith   PetscBool      match;
2874264ace61SBarry Smith   PetscErrorCode ierr;
2875264ace61SBarry Smith 
2876264ace61SBarry Smith   PetscFunctionBegin;
2877264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2878251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2879264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2880264ace61SBarry Smith 
28810f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
28821c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2883ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2884264ace61SBarry Smith 
2885264ace61SBarry Smith   if (dm->ops->destroy) {
2886264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
28870298fd71SBarry Smith     dm->ops->destroy = NULL;
2888264ace61SBarry Smith   }
2889264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2890264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2891264ace61SBarry Smith   PetscFunctionReturn(0);
2892264ace61SBarry Smith }
2893264ace61SBarry Smith 
2894264ace61SBarry Smith #undef __FUNCT__
2895264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2896264ace61SBarry Smith /*@C
2897264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2898264ace61SBarry Smith 
2899264ace61SBarry Smith   Not Collective
2900264ace61SBarry Smith 
2901264ace61SBarry Smith   Input Parameter:
2902264ace61SBarry Smith . dm  - The DM
2903264ace61SBarry Smith 
2904264ace61SBarry Smith   Output Parameter:
2905264ace61SBarry Smith . type - The DM type name
2906264ace61SBarry Smith 
2907264ace61SBarry Smith   Level: intermediate
2908264ace61SBarry Smith 
2909264ace61SBarry Smith .keywords: DM, get, type, name
2910264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2911264ace61SBarry Smith @*/
291219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2913264ace61SBarry Smith {
2914264ace61SBarry Smith   PetscErrorCode ierr;
2915264ace61SBarry Smith 
2916264ace61SBarry Smith   PetscFunctionBegin;
2917264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2918c959eef4SJed Brown   PetscValidPointer(type,2);
2919607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2920264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2921264ace61SBarry Smith   PetscFunctionReturn(0);
2922264ace61SBarry Smith }
2923264ace61SBarry Smith 
292467a56275SMatthew G Knepley #undef __FUNCT__
292567a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
292667a56275SMatthew G Knepley /*@C
292767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
292867a56275SMatthew G Knepley 
292967a56275SMatthew G Knepley   Collective on DM
293067a56275SMatthew G Knepley 
293167a56275SMatthew G Knepley   Input Parameters:
293267a56275SMatthew G Knepley + dm - the DM
293367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
293467a56275SMatthew G Knepley 
293567a56275SMatthew G Knepley   Output Parameter:
293667a56275SMatthew G Knepley . M - pointer to new DM
293767a56275SMatthew G Knepley 
293867a56275SMatthew G Knepley   Notes:
293967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
294067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
294167a56275SMatthew G Knepley   of the input DM.
294267a56275SMatthew G Knepley 
294367a56275SMatthew G Knepley   Level: intermediate
294467a56275SMatthew G Knepley 
294567a56275SMatthew G Knepley .seealso: DMCreate()
294667a56275SMatthew G Knepley @*/
294719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
294867a56275SMatthew G Knepley {
294967a56275SMatthew G Knepley   DM             B;
295067a56275SMatthew G Knepley   char           convname[256];
295167a56275SMatthew G Knepley   PetscBool      sametype, issame;
295267a56275SMatthew G Knepley   PetscErrorCode ierr;
295367a56275SMatthew G Knepley 
295467a56275SMatthew G Knepley   PetscFunctionBegin;
295567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
295667a56275SMatthew G Knepley   PetscValidType(dm,1);
295767a56275SMatthew G Knepley   PetscValidPointer(M,3);
2958251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
295967a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
296067a56275SMatthew G Knepley   {
29610298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
296267a56275SMatthew G Knepley 
296367a56275SMatthew G Knepley     /*
296467a56275SMatthew G Knepley        Order of precedence:
296567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
296667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
296767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
296867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
296967a56275SMatthew G Knepley        5) Use a really basic converter.
297067a56275SMatthew G Knepley     */
297167a56275SMatthew G Knepley 
297267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
297367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
297467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
297567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
297667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
297767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29780005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
297967a56275SMatthew G Knepley     if (conv) goto foundconv;
298067a56275SMatthew G Knepley 
298167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
298282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
298367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
298467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
298567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
298667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
298767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
298867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29890005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
299067a56275SMatthew G Knepley     if (conv) {
2991fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
299267a56275SMatthew G Knepley       goto foundconv;
299367a56275SMatthew G Knepley     }
299467a56275SMatthew G Knepley 
299567a56275SMatthew G Knepley #if 0
299667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
299767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2998fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
299967a56275SMatthew G Knepley     if (conv) goto foundconv;
300067a56275SMatthew G Knepley 
300167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
300267a56275SMatthew G Knepley     if (dm->ops->convert) {
300367a56275SMatthew G Knepley       conv = dm->ops->convert;
300467a56275SMatthew G Knepley     }
300567a56275SMatthew G Knepley     if (conv) goto foundconv;
300667a56275SMatthew G Knepley #endif
300767a56275SMatthew G Knepley 
300867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
300982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
301067a56275SMatthew G Knepley 
301167a56275SMatthew G Knepley foundconv:
301267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
301367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
301467a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
301567a56275SMatthew G Knepley   }
301667a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
301767a56275SMatthew G Knepley   PetscFunctionReturn(0);
301867a56275SMatthew G Knepley }
3019264ace61SBarry Smith 
3020264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3021264ace61SBarry Smith 
3022264ace61SBarry Smith #undef __FUNCT__
3023264ace61SBarry Smith #define __FUNCT__ "DMRegister"
3024264ace61SBarry Smith /*@C
30251c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
30261c84c290SBarry Smith 
30271c84c290SBarry Smith   Not Collective
30281c84c290SBarry Smith 
30291c84c290SBarry Smith   Input Parameters:
30301c84c290SBarry Smith + name        - The name of a new user-defined creation routine
30311c84c290SBarry Smith - create_func - The creation routine itself
30321c84c290SBarry Smith 
30331c84c290SBarry Smith   Notes:
30341c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
30351c84c290SBarry Smith 
30361c84c290SBarry Smith 
30371c84c290SBarry Smith   Sample usage:
30381c84c290SBarry Smith .vb
3039bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
30401c84c290SBarry Smith .ve
30411c84c290SBarry Smith 
30421c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
30431c84c290SBarry Smith .vb
30441c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
30451c84c290SBarry Smith     DMSetType(DM,"my_da");
30461c84c290SBarry Smith .ve
30471c84c290SBarry Smith    or at runtime via the option
30481c84c290SBarry Smith .vb
30491c84c290SBarry Smith     -da_type my_da
30501c84c290SBarry Smith .ve
3051264ace61SBarry Smith 
3052264ace61SBarry Smith   Level: advanced
30531c84c290SBarry Smith 
30541c84c290SBarry Smith .keywords: DM, register
3055bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
30561c84c290SBarry Smith 
3057264ace61SBarry Smith @*/
3058bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3059264ace61SBarry Smith {
3060264ace61SBarry Smith   PetscErrorCode ierr;
3061264ace61SBarry Smith 
3062264ace61SBarry Smith   PetscFunctionBegin;
3063a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3064264ace61SBarry Smith   PetscFunctionReturn(0);
3065264ace61SBarry Smith }
3066264ace61SBarry Smith 
3067b859378eSBarry Smith #undef __FUNCT__
3068b859378eSBarry Smith #define __FUNCT__ "DMLoad"
3069b859378eSBarry Smith /*@C
307055849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3071b859378eSBarry Smith 
3072b859378eSBarry Smith   Collective on PetscViewer
3073b859378eSBarry Smith 
3074b859378eSBarry Smith   Input Parameters:
3075b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3076b859378eSBarry Smith            some related function before a call to DMLoad().
3077b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3078b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3079b859378eSBarry Smith 
3080b859378eSBarry Smith    Level: intermediate
3081b859378eSBarry Smith 
3082b859378eSBarry Smith   Notes:
308355849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3084b859378eSBarry Smith 
3085b859378eSBarry Smith   Notes for advanced users:
3086b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3087b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3088b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3089b859378eSBarry Smith   format is
3090b859378eSBarry Smith .vb
3091b859378eSBarry Smith      has not yet been determined
3092b859378eSBarry Smith .ve
3093b859378eSBarry Smith 
3094b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3095b859378eSBarry Smith @*/
3096b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3097b859378eSBarry Smith {
30989331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3099b859378eSBarry Smith   PetscErrorCode ierr;
3100b859378eSBarry Smith 
3101b859378eSBarry Smith   PetscFunctionBegin;
3102b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3103b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
310432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
31059331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
31069331c7a4SMatthew G. Knepley   if (isbinary) {
31079331c7a4SMatthew G. Knepley     PetscInt classid;
31089331c7a4SMatthew G. Knepley     char     type[256];
3109b859378eSBarry Smith 
3110060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
31119200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3112060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
311332c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
31149331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
31159331c7a4SMatthew G. Knepley   } else if (ishdf5) {
31169331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
31179331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3118b859378eSBarry Smith   PetscFunctionReturn(0);
3119b859378eSBarry Smith }
3120b859378eSBarry Smith 
31217da65231SMatthew G Knepley /******************************** FEM Support **********************************/
31227da65231SMatthew G Knepley 
31237da65231SMatthew G Knepley #undef __FUNCT__
31247da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3125a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3126a6dfd86eSKarl Rupp {
31271d47ebbbSSatish Balay   PetscInt       f;
31281b30c384SMatthew G Knepley   PetscErrorCode ierr;
31291b30c384SMatthew G Knepley 
31307da65231SMatthew G Knepley   PetscFunctionBegin;
313174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
31321d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
313357622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
31347da65231SMatthew G Knepley   }
31357da65231SMatthew G Knepley   PetscFunctionReturn(0);
31367da65231SMatthew G Knepley }
31377da65231SMatthew G Knepley 
31387da65231SMatthew G Knepley #undef __FUNCT__
31397da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3140a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3141a6dfd86eSKarl Rupp {
31421b30c384SMatthew G Knepley   PetscInt       f, g;
31437da65231SMatthew G Knepley   PetscErrorCode ierr;
31447da65231SMatthew G Knepley 
31457da65231SMatthew G Knepley   PetscFunctionBegin;
314674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
31471d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
314874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
31491d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3150e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
31517da65231SMatthew G Knepley     }
315274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
31537da65231SMatthew G Knepley   }
31547da65231SMatthew G Knepley   PetscFunctionReturn(0);
31557da65231SMatthew G Knepley }
3156e7c4fc90SDmitry Karpeev 
3157970e74d5SMatthew G Knepley #undef __FUNCT__
3158e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
31596113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3160e759306cSMatthew G. Knepley {
3161e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3162e759306cSMatthew G. Knepley   PetscInt       p;
3163e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3164e759306cSMatthew G. Knepley 
3165e759306cSMatthew G. Knepley   PetscFunctionBegin;
3166e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3167e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3168e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3169e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3170e759306cSMatthew G. Knepley     if (p == rank) {
3171e759306cSMatthew G. Knepley       Vec x;
3172e759306cSMatthew G. Knepley 
3173e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3174e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
31756113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3176e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3177e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3178e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3179e759306cSMatthew G. Knepley     }
3180e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3181e759306cSMatthew G. Knepley   }
3182e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3183e759306cSMatthew G. Knepley }
3184e759306cSMatthew G. Knepley 
3185e759306cSMatthew G. Knepley #undef __FUNCT__
318688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
318788ed4aceSMatthew G Knepley /*@
318888ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
318988ed4aceSMatthew G Knepley 
319088ed4aceSMatthew G Knepley   Input Parameter:
319188ed4aceSMatthew G Knepley . dm - The DM
319288ed4aceSMatthew G Knepley 
319388ed4aceSMatthew G Knepley   Output Parameter:
319488ed4aceSMatthew G Knepley . section - The PetscSection
319588ed4aceSMatthew G Knepley 
319688ed4aceSMatthew G Knepley   Level: intermediate
319788ed4aceSMatthew G Knepley 
319888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
319988ed4aceSMatthew G Knepley 
320088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
320188ed4aceSMatthew G Knepley @*/
32020adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
32030adebc6cSBarry Smith {
3204fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3205fd59a867SMatthew G. Knepley 
320688ed4aceSMatthew G Knepley   PetscFunctionBegin;
320788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
320888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
32092f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
32102f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3211ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
32122f0f8703SMatthew G. Knepley   }
321388ed4aceSMatthew G Knepley   *section = dm->defaultSection;
321488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
321588ed4aceSMatthew G Knepley }
321688ed4aceSMatthew G Knepley 
321788ed4aceSMatthew G Knepley #undef __FUNCT__
321888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
321988ed4aceSMatthew G Knepley /*@
322088ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
322188ed4aceSMatthew G Knepley 
322288ed4aceSMatthew G Knepley   Input Parameters:
322388ed4aceSMatthew G Knepley + dm - The DM
322488ed4aceSMatthew G Knepley - section - The PetscSection
322588ed4aceSMatthew G Knepley 
322688ed4aceSMatthew G Knepley   Level: intermediate
322788ed4aceSMatthew G Knepley 
322888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
322988ed4aceSMatthew G Knepley 
323088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
323188ed4aceSMatthew G Knepley @*/
32320adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
32330adebc6cSBarry Smith {
3234c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3235af122d2aSMatthew G Knepley   PetscInt       f;
323688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
323788ed4aceSMatthew G Knepley 
323888ed4aceSMatthew G Knepley   PetscFunctionBegin;
323988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3240c473ab19SMatthew G. Knepley   if (section) {
32411d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
32421d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3243c473ab19SMatthew G. Knepley   }
324488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
324588ed4aceSMatthew G Knepley   dm->defaultSection = section;
3246c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3247af122d2aSMatthew G Knepley   if (numFields) {
3248af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3249af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
32500f21e855SMatthew G. Knepley       PetscObject disc;
3251af122d2aSMatthew G Knepley       const char *name;
3252af122d2aSMatthew G Knepley 
3253af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
32540f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
32550f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3256af122d2aSMatthew G Knepley     }
3257af122d2aSMatthew G Knepley   }
32581d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
32591d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
326088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
326188ed4aceSMatthew G Knepley }
326288ed4aceSMatthew G Knepley 
326388ed4aceSMatthew G Knepley #undef __FUNCT__
32649435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
32659435951eSToby Isaac /*@
32669435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
32679435951eSToby Isaac 
3268e228b242SToby Isaac   not collective
3269e228b242SToby Isaac 
32709435951eSToby Isaac   Input Parameter:
32719435951eSToby Isaac . dm - The DM
32729435951eSToby Isaac 
32739435951eSToby Isaac   Output Parameter:
32749435951eSToby 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.
32759435951eSToby 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.
32769435951eSToby Isaac 
32779435951eSToby Isaac   Level: advanced
32789435951eSToby Isaac 
32799435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
32809435951eSToby Isaac 
32819435951eSToby Isaac .seealso: DMSetDefaultConstraints()
32829435951eSToby Isaac @*/
32839435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
32849435951eSToby Isaac {
32859435951eSToby Isaac   PetscErrorCode ierr;
32869435951eSToby Isaac 
32879435951eSToby Isaac   PetscFunctionBegin;
32889435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32899435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
329045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
329145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
32929435951eSToby Isaac   PetscFunctionReturn(0);
32939435951eSToby Isaac }
32949435951eSToby Isaac 
32959435951eSToby Isaac #undef __FUNCT__
32969435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
32979435951eSToby Isaac /*@
32989435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
32999435951eSToby Isaac 
33009435951eSToby 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().
33019435951eSToby Isaac 
33029435951eSToby 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.
33039435951eSToby Isaac 
3304e228b242SToby Isaac   collective on dm
3305e228b242SToby Isaac 
33069435951eSToby Isaac   Input Parameters:
33079435951eSToby Isaac + dm - The DM
3308e228b242SToby 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).
3309e228b242SToby 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).
33109435951eSToby Isaac 
33119435951eSToby Isaac   Level: advanced
33129435951eSToby Isaac 
33139435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
33149435951eSToby Isaac 
33159435951eSToby Isaac .seealso: DMGetDefaultConstraints()
33169435951eSToby Isaac @*/
33179435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
33189435951eSToby Isaac {
3319e228b242SToby Isaac   PetscMPIInt result;
33209435951eSToby Isaac   PetscErrorCode ierr;
33219435951eSToby Isaac 
33229435951eSToby Isaac   PetscFunctionBegin;
33239435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3324e228b242SToby Isaac   if (section) {
3325e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3326e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3327e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3328e228b242SToby Isaac   }
3329e228b242SToby Isaac   if (mat) {
3330e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3331e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3332e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3333e228b242SToby Isaac   }
33349435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
33359435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
33369435951eSToby Isaac   dm->defaultConstraintSection = section;
33379435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
33389435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
33399435951eSToby Isaac   dm->defaultConstraintMat = mat;
33409435951eSToby Isaac   PetscFunctionReturn(0);
33419435951eSToby Isaac }
33429435951eSToby Isaac 
3343f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
33449435951eSToby Isaac #undef __FUNCT__
3345284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3346507e4973SMatthew G. Knepley /*
3347507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3348507e4973SMatthew G. Knepley 
3349507e4973SMatthew G. Knepley   Input Parameters:
3350507e4973SMatthew G. Knepley + dm - The DM
3351507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3352507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3353507e4973SMatthew G. Knepley 
3354507e4973SMatthew G. Knepley   Level: intermediate
3355507e4973SMatthew G. Knepley 
3356507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3357507e4973SMatthew G. Knepley */
3358f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3359507e4973SMatthew G. Knepley {
3360507e4973SMatthew G. Knepley   MPI_Comm        comm;
3361507e4973SMatthew G. Knepley   PetscLayout     layout;
3362507e4973SMatthew G. Knepley   const PetscInt *ranges;
3363507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3364507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3365507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3366507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3367507e4973SMatthew G. Knepley 
3368507e4973SMatthew G. Knepley   PetscFunctionBegin;
3369507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3370507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3371507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3372507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3373507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3374507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3375507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3376507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3377507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3378507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3379507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3380507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3381f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3382507e4973SMatthew G. Knepley 
3383507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3384507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3385507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3386507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3387507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3388507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3389507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3390507e4973SMatthew 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;}
3391507e4973SMatthew 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;}
3392507e4973SMatthew G. Knepley     if (gdof < 0) {
3393507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3394507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3395507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3396507e4973SMatthew G. Knepley 
3397507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3398507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3399507e4973SMatthew 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;}
3400507e4973SMatthew G. Knepley       }
3401507e4973SMatthew G. Knepley     }
3402507e4973SMatthew G. Knepley   }
3403507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3404507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3405b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3406507e4973SMatthew G. Knepley   if (!gvalid) {
3407507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3408507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3409507e4973SMatthew G. Knepley   }
3410507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3411507e4973SMatthew G. Knepley }
3412f741bcd2SMatthew G. Knepley #endif
3413507e4973SMatthew G. Knepley 
3414507e4973SMatthew G. Knepley #undef __FUNCT__
341588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
341688ed4aceSMatthew G Knepley /*@
341788ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
341888ed4aceSMatthew G Knepley 
34198b1ab98fSJed Brown   Collective on DM
34208b1ab98fSJed Brown 
342188ed4aceSMatthew G Knepley   Input Parameter:
342288ed4aceSMatthew G Knepley . dm - The DM
342388ed4aceSMatthew G Knepley 
342488ed4aceSMatthew G Knepley   Output Parameter:
342588ed4aceSMatthew G Knepley . section - The PetscSection
342688ed4aceSMatthew G Knepley 
342788ed4aceSMatthew G Knepley   Level: intermediate
342888ed4aceSMatthew G Knepley 
342988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
343088ed4aceSMatthew G Knepley 
343188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
343288ed4aceSMatthew G Knepley @*/
34330adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
34340adebc6cSBarry Smith {
343588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
343688ed4aceSMatthew G Knepley 
343788ed4aceSMatthew G Knepley   PetscFunctionBegin;
343888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
343988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
344088ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3441fd59a867SMatthew G. Knepley     PetscSection s;
3442fd59a867SMatthew G. Knepley 
3443fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3444fd59a867SMatthew 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");
3445fd59a867SMatthew 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");
344615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3447cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3448ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3449685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
345088ed4aceSMatthew G Knepley   }
345188ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
345288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
345388ed4aceSMatthew G Knepley }
345488ed4aceSMatthew G Knepley 
345588ed4aceSMatthew G Knepley #undef __FUNCT__
3456b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3457b21d0597SMatthew G Knepley /*@
3458b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3459b21d0597SMatthew G Knepley 
3460b21d0597SMatthew G Knepley   Input Parameters:
3461b21d0597SMatthew G Knepley + dm - The DM
34625080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3463b21d0597SMatthew G Knepley 
3464b21d0597SMatthew G Knepley   Level: intermediate
3465b21d0597SMatthew G Knepley 
3466b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3467b21d0597SMatthew G Knepley 
3468b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3469b21d0597SMatthew G Knepley @*/
34700adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
34710adebc6cSBarry Smith {
3472b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3473b21d0597SMatthew G Knepley 
3474b21d0597SMatthew G Knepley   PetscFunctionBegin;
3475b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34765080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34771d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3478b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3479b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3480507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3481f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3482507e4973SMatthew G. Knepley #endif
3483b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3484b21d0597SMatthew G Knepley }
3485b21d0597SMatthew G Knepley 
3486b21d0597SMatthew G Knepley #undef __FUNCT__
348788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
348888ed4aceSMatthew G Knepley /*@
348988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
349088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
349188ed4aceSMatthew G Knepley 
349288ed4aceSMatthew G Knepley   Input Parameter:
349388ed4aceSMatthew G Knepley . dm - The DM
349488ed4aceSMatthew G Knepley 
349588ed4aceSMatthew G Knepley   Output Parameter:
349688ed4aceSMatthew G Knepley . sf - The PetscSF
349788ed4aceSMatthew G Knepley 
349888ed4aceSMatthew G Knepley   Level: intermediate
349988ed4aceSMatthew G Knepley 
350088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
350188ed4aceSMatthew G Knepley 
350288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
350388ed4aceSMatthew G Knepley @*/
35040adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
35050adebc6cSBarry Smith {
350688ed4aceSMatthew G Knepley   PetscInt       nroots;
350788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
350888ed4aceSMatthew G Knepley 
350988ed4aceSMatthew G Knepley   PetscFunctionBegin;
351088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
351188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
35120298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
351388ed4aceSMatthew G Knepley   if (nroots < 0) {
351488ed4aceSMatthew G Knepley     PetscSection section, gSection;
351588ed4aceSMatthew G Knepley 
351688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
351731ea6d37SMatthew G Knepley     if (section) {
351888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
351988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
352031ea6d37SMatthew G Knepley     } else {
35210298fd71SBarry Smith       *sf = NULL;
352231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
352331ea6d37SMatthew G Knepley     }
352488ed4aceSMatthew G Knepley   }
352588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
352688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
352788ed4aceSMatthew G Knepley }
352888ed4aceSMatthew G Knepley 
352988ed4aceSMatthew G Knepley #undef __FUNCT__
353088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
353188ed4aceSMatthew G Knepley /*@
353288ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
353388ed4aceSMatthew G Knepley 
353488ed4aceSMatthew G Knepley   Input Parameters:
353588ed4aceSMatthew G Knepley + dm - The DM
353688ed4aceSMatthew G Knepley - sf - The PetscSF
353788ed4aceSMatthew G Knepley 
353888ed4aceSMatthew G Knepley   Level: intermediate
353988ed4aceSMatthew G Knepley 
354088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
354188ed4aceSMatthew G Knepley 
354288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
354388ed4aceSMatthew G Knepley @*/
35440adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
35450adebc6cSBarry Smith {
354688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
354788ed4aceSMatthew G Knepley 
354888ed4aceSMatthew G Knepley   PetscFunctionBegin;
354988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
355088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
355188ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
355288ed4aceSMatthew G Knepley   dm->defaultSF = sf;
355388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
355488ed4aceSMatthew G Knepley }
355588ed4aceSMatthew G Knepley 
355688ed4aceSMatthew G Knepley #undef __FUNCT__
355788ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
355888ed4aceSMatthew G Knepley /*@C
355988ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
356088ed4aceSMatthew G Knepley   describing the data layout.
356188ed4aceSMatthew G Knepley 
356288ed4aceSMatthew G Knepley   Input Parameters:
356388ed4aceSMatthew G Knepley + dm - The DM
356488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
356588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
356688ed4aceSMatthew G Knepley 
356788ed4aceSMatthew G Knepley   Level: intermediate
356888ed4aceSMatthew G Knepley 
356988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
357088ed4aceSMatthew G Knepley @*/
357188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
357288ed4aceSMatthew G Knepley {
357382f516ccSBarry Smith   MPI_Comm       comm;
357488ed4aceSMatthew G Knepley   PetscLayout    layout;
357588ed4aceSMatthew G Knepley   const PetscInt *ranges;
357688ed4aceSMatthew G Knepley   PetscInt       *local;
357788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3578ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
357988ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
358088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
358188ed4aceSMatthew G Knepley 
358288ed4aceSMatthew G Knepley   PetscFunctionBegin;
358382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
358488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
358588ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
358688ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
358788ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
358888ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
358988ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
359088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
359188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
359288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
359388ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3594ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35956636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
359688ed4aceSMatthew G Knepley 
35976636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35986636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3599235fbf56SMatthew 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));
36006636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
360188ed4aceSMatthew G Knepley   }
3602785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3603785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
360488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
36051f588964SMatthew G Knepley     const PetscInt *cind;
36066636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
360788ed4aceSMatthew G Knepley 
360888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
360988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
361088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
361188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
361288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
36136636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
361488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
36156636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
36166636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
36176636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3618057b4bcdSMatthew 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);
36196636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
36206636e97aSMatthew G Knepley     }
362188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
362288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
362388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
362488ed4aceSMatthew G Knepley     }
362588ed4aceSMatthew G Knepley     if (gdof < 0) {
36266636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
362788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
362888ed4aceSMatthew G Knepley 
362905376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
363031d3f06eSJed Brown         if (r < 0) r = -(r+2);
363105376888SMatthew 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);
363288ed4aceSMatthew G Knepley         remote[l].rank  = r;
363388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
363488ed4aceSMatthew G Knepley       }
363588ed4aceSMatthew G Knepley     } else {
36366636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
363788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
363888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
363988ed4aceSMatthew G Knepley       }
364088ed4aceSMatthew G Knepley     }
364188ed4aceSMatthew G Knepley   }
36426636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
364388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
364488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
364588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
364688ed4aceSMatthew G Knepley }
3647af122d2aSMatthew G Knepley 
3648af122d2aSMatthew G Knepley #undef __FUNCT__
3649b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3650b21d0597SMatthew G Knepley /*@
3651b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3652b21d0597SMatthew G Knepley 
3653b21d0597SMatthew G Knepley   Input Parameter:
3654b21d0597SMatthew G Knepley . dm - The DM
3655b21d0597SMatthew G Knepley 
3656b21d0597SMatthew G Knepley   Output Parameter:
3657b21d0597SMatthew G Knepley . sf - The PetscSF
3658b21d0597SMatthew G Knepley 
3659b21d0597SMatthew G Knepley   Level: intermediate
3660b21d0597SMatthew G Knepley 
3661b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3662b21d0597SMatthew G Knepley 
3663057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3664b21d0597SMatthew G Knepley @*/
36650adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
36660adebc6cSBarry Smith {
3667b21d0597SMatthew G Knepley   PetscFunctionBegin;
3668b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3669b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3670b21d0597SMatthew G Knepley   *sf = dm->sf;
3671b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3672b21d0597SMatthew G Knepley }
3673b21d0597SMatthew G Knepley 
3674b21d0597SMatthew G Knepley #undef __FUNCT__
3675057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3676057b4bcdSMatthew G Knepley /*@
3677057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3678057b4bcdSMatthew G Knepley 
3679057b4bcdSMatthew G Knepley   Input Parameters:
3680057b4bcdSMatthew G Knepley + dm - The DM
3681057b4bcdSMatthew G Knepley - sf - The PetscSF
3682057b4bcdSMatthew G Knepley 
3683057b4bcdSMatthew G Knepley   Level: intermediate
3684057b4bcdSMatthew G Knepley 
3685057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3686057b4bcdSMatthew G Knepley @*/
36870adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
36880adebc6cSBarry Smith {
3689057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3690057b4bcdSMatthew G Knepley 
3691057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3692057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3693057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3694057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3695057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3696057b4bcdSMatthew G Knepley   dm->sf = sf;
3697057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3698057b4bcdSMatthew G Knepley }
3699057b4bcdSMatthew G Knepley 
3700057b4bcdSMatthew G Knepley #undef __FUNCT__
37012764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
37022764a2aaSMatthew G. Knepley /*@
37032764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
37042764a2aaSMatthew G. Knepley 
37052764a2aaSMatthew G. Knepley   Input Parameter:
37062764a2aaSMatthew G. Knepley . dm - The DM
37072764a2aaSMatthew G. Knepley 
37082764a2aaSMatthew G. Knepley   Output Parameter:
37092764a2aaSMatthew G. Knepley . prob - The PetscDS
37102764a2aaSMatthew G. Knepley 
37112764a2aaSMatthew G. Knepley   Level: developer
37122764a2aaSMatthew G. Knepley 
37132764a2aaSMatthew G. Knepley .seealso: DMSetDS()
37142764a2aaSMatthew G. Knepley @*/
37152764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3716af122d2aSMatthew G Knepley {
3717af122d2aSMatthew G Knepley   PetscFunctionBegin;
3718af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37190f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
37200f21e855SMatthew G. Knepley   *prob = dm->prob;
37210f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
37220f21e855SMatthew G. Knepley }
37230f21e855SMatthew G. Knepley 
37240f21e855SMatthew G. Knepley #undef __FUNCT__
37252764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
37262764a2aaSMatthew G. Knepley /*@
37272764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
37282764a2aaSMatthew G. Knepley 
37292764a2aaSMatthew G. Knepley   Input Parameters:
37302764a2aaSMatthew G. Knepley + dm - The DM
37312764a2aaSMatthew G. Knepley - prob - The PetscDS
37322764a2aaSMatthew G. Knepley 
37332764a2aaSMatthew G. Knepley   Level: developer
37342764a2aaSMatthew G. Knepley 
37352764a2aaSMatthew G. Knepley .seealso: DMGetDS()
37362764a2aaSMatthew G. Knepley @*/
37372764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
37380f21e855SMatthew G. Knepley {
37390f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37400f21e855SMatthew G. Knepley 
37410f21e855SMatthew G. Knepley   PetscFunctionBegin;
37420f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37432764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
37442764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
37450f21e855SMatthew G. Knepley   dm->prob = prob;
37460f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
37470f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
37480f21e855SMatthew G. Knepley }
37490f21e855SMatthew G. Knepley 
37500f21e855SMatthew G. Knepley #undef __FUNCT__
37510f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
37520f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
37530f21e855SMatthew G. Knepley {
37540f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37550f21e855SMatthew G. Knepley 
37560f21e855SMatthew G. Knepley   PetscFunctionBegin;
37570f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37582764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3759af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3760af122d2aSMatthew G Knepley }
3761af122d2aSMatthew G Knepley 
3762af122d2aSMatthew G Knepley #undef __FUNCT__
3763af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3764af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3765af122d2aSMatthew G Knepley {
37660f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3767af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3768af122d2aSMatthew G Knepley 
3769af122d2aSMatthew G Knepley   PetscFunctionBegin;
3770af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37712764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
37720f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
37730f21e855SMatthew G. Knepley     PetscContainer obj;
37740f21e855SMatthew G. Knepley 
37750f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
37762764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
37770f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3778af122d2aSMatthew G Knepley   }
3779af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3780af122d2aSMatthew G Knepley }
3781af122d2aSMatthew G Knepley 
3782af122d2aSMatthew G Knepley #undef __FUNCT__
3783af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3784c1929be8SMatthew G. Knepley /*@
3785c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3786c1929be8SMatthew G. Knepley 
3787c1929be8SMatthew G. Knepley   Not collective
3788c1929be8SMatthew G. Knepley 
3789c1929be8SMatthew G. Knepley   Input Parameters:
3790c1929be8SMatthew G. Knepley + dm - The DM
3791c1929be8SMatthew G. Knepley - f  - The field number
3792c1929be8SMatthew G. Knepley 
3793c1929be8SMatthew G. Knepley   Output Parameter:
3794c1929be8SMatthew G. Knepley . field - The discretization object
3795c1929be8SMatthew G. Knepley 
3796c1929be8SMatthew G. Knepley   Level: developer
3797c1929be8SMatthew G. Knepley 
3798c1929be8SMatthew G. Knepley .seealso: DMSetField()
3799c1929be8SMatthew G. Knepley @*/
3800af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3801af122d2aSMatthew G Knepley {
38020f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38030f21e855SMatthew G. Knepley 
3804af122d2aSMatthew G Knepley   PetscFunctionBegin;
3805af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38062764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3807decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3808decb47aaSMatthew G. Knepley }
3809decb47aaSMatthew G. Knepley 
3810decb47aaSMatthew G. Knepley #undef __FUNCT__
3811decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3812c1929be8SMatthew G. Knepley /*@
3813c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3814c1929be8SMatthew G. Knepley 
3815c1929be8SMatthew G. Knepley   Logically collective on DM
3816c1929be8SMatthew G. Knepley 
3817c1929be8SMatthew G. Knepley   Input Parameters:
3818c1929be8SMatthew G. Knepley + dm - The DM
3819c1929be8SMatthew G. Knepley . f  - The field number
3820c1929be8SMatthew G. Knepley - field - The discretization object
3821c1929be8SMatthew G. Knepley 
3822c1929be8SMatthew G. Knepley   Level: developer
3823c1929be8SMatthew G. Knepley 
3824c1929be8SMatthew G. Knepley .seealso: DMGetField()
3825c1929be8SMatthew G. Knepley @*/
3826decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3827decb47aaSMatthew G. Knepley {
3828decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3829decb47aaSMatthew G. Knepley 
3830decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3831decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38322764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3833af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3834af122d2aSMatthew G Knepley }
38356636e97aSMatthew G Knepley 
38366636e97aSMatthew G Knepley #undef __FUNCT__
3837b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3838b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3839b64e0483SPeter Brune {
3840b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3841b64e0483SPeter Brune   PetscErrorCode ierr;
3842b64e0483SPeter Brune   Vec coords,ccoords;
38436dbf9973SLawrence Mitchell   Mat inject;
3844b64e0483SPeter Brune   PetscFunctionBegin;
3845b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3846b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3847b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3848b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3849b64e0483SPeter Brune   if (coords && !ccoords) {
3850b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
38516dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
38522adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
38536dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3854b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3855b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3856b64e0483SPeter Brune   }
3857b64e0483SPeter Brune   PetscFunctionReturn(0);
3858b64e0483SPeter Brune }
3859b64e0483SPeter Brune 
3860b64e0483SPeter Brune #undef __FUNCT__
386103dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
386203dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
386303dadc2fSPeter Brune {
386403dadc2fSPeter Brune   DM dm_coord,subdm_coord;
386503dadc2fSPeter Brune   PetscErrorCode ierr;
386603dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
386703dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
386803dadc2fSPeter Brune   PetscFunctionBegin;
386903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
387003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
387103dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
387203dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
387303dadc2fSPeter Brune   if (coords && !ccoords) {
387403dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
387503dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
387624640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
387703dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
387803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
387903dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
388003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
388103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
388203dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
388303dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
388403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
388503dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
388603dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
388703dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
388803dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
388903dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
389003dadc2fSPeter Brune   }
389103dadc2fSPeter Brune   PetscFunctionReturn(0);
389203dadc2fSPeter Brune }
389303dadc2fSPeter Brune 
389403dadc2fSPeter Brune #undef __FUNCT__
3895c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3896c73cfb54SMatthew G. Knepley /*@
3897c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3898c73cfb54SMatthew G. Knepley 
3899c73cfb54SMatthew G. Knepley   Not collective
3900c73cfb54SMatthew G. Knepley 
3901c73cfb54SMatthew G. Knepley   Input Parameter:
3902c73cfb54SMatthew G. Knepley . dm - The DM
3903c73cfb54SMatthew G. Knepley 
3904c73cfb54SMatthew G. Knepley   Output Parameter:
3905c73cfb54SMatthew G. Knepley . dim - The topological dimension
3906c73cfb54SMatthew G. Knepley 
3907c73cfb54SMatthew G. Knepley   Level: beginner
3908c73cfb54SMatthew G. Knepley 
3909c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3910c73cfb54SMatthew G. Knepley @*/
3911c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3912c73cfb54SMatthew G. Knepley {
3913c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3914c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3915c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3916c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3917c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3918c73cfb54SMatthew G. Knepley }
3919c73cfb54SMatthew G. Knepley 
3920c73cfb54SMatthew G. Knepley #undef __FUNCT__
3921c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3922c73cfb54SMatthew G. Knepley /*@
3923c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3924c73cfb54SMatthew G. Knepley 
3925c73cfb54SMatthew G. Knepley   Collective on dm
3926c73cfb54SMatthew G. Knepley 
3927c73cfb54SMatthew G. Knepley   Input Parameters:
3928c73cfb54SMatthew G. Knepley + dm - The DM
3929c73cfb54SMatthew G. Knepley - dim - The topological dimension
3930c73cfb54SMatthew G. Knepley 
3931c73cfb54SMatthew G. Knepley   Level: beginner
3932c73cfb54SMatthew G. Knepley 
3933c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3934c73cfb54SMatthew G. Knepley @*/
3935c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3936c73cfb54SMatthew G. Knepley {
3937c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3938c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3939c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3940c73cfb54SMatthew G. Knepley   dm->dim = dim;
3941c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3942c73cfb54SMatthew G. Knepley }
3943c73cfb54SMatthew G. Knepley 
3944793f3fe5SMatthew G. Knepley #undef __FUNCT__
3945793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3946793f3fe5SMatthew G. Knepley /*@
3947793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3948793f3fe5SMatthew G. Knepley 
3949793f3fe5SMatthew G. Knepley   Collective on DM
3950793f3fe5SMatthew G. Knepley 
3951793f3fe5SMatthew G. Knepley   Input Parameters:
3952793f3fe5SMatthew G. Knepley + dm - the DM
3953793f3fe5SMatthew G. Knepley - dim - the dimension
3954793f3fe5SMatthew G. Knepley 
3955793f3fe5SMatthew G. Knepley   Output Parameters:
3956793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3957793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3958793f3fe5SMatthew G. Knepley 
3959793f3fe5SMatthew G. Knepley   Note:
3960793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3961793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3962793f3fe5SMatthew G. Knepley   then the interval is empty.
3963793f3fe5SMatthew G. Knepley 
3964793f3fe5SMatthew G. Knepley   Level: intermediate
3965793f3fe5SMatthew G. Knepley 
3966793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3967793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3968793f3fe5SMatthew G. Knepley @*/
3969793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3970793f3fe5SMatthew G. Knepley {
3971793f3fe5SMatthew G. Knepley   PetscInt       d;
3972793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3973793f3fe5SMatthew G. Knepley 
3974793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3975793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3976793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3977793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3978793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3979793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3980793f3fe5SMatthew G. Knepley }
3981793f3fe5SMatthew G. Knepley 
3982793f3fe5SMatthew G. Knepley #undef __FUNCT__
39836636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
39846636e97aSMatthew G Knepley /*@
39856636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
39866636e97aSMatthew G Knepley 
39876636e97aSMatthew G Knepley   Collective on DM
39886636e97aSMatthew G Knepley 
39896636e97aSMatthew G Knepley   Input Parameters:
39906636e97aSMatthew G Knepley + dm - the DM
39916636e97aSMatthew G Knepley - c - coordinate vector
39926636e97aSMatthew G Knepley 
39936636e97aSMatthew G Knepley   Note:
39946636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
39956636e97aSMatthew G Knepley 
39966636e97aSMatthew G Knepley   Level: intermediate
39976636e97aSMatthew G Knepley 
39986636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39996636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
40006636e97aSMatthew G Knepley @*/
40016636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
40026636e97aSMatthew G Knepley {
40036636e97aSMatthew G Knepley   PetscErrorCode ierr;
40046636e97aSMatthew G Knepley 
40056636e97aSMatthew G Knepley   PetscFunctionBegin;
40066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40076636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
40086636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
40096636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
40106636e97aSMatthew G Knepley   dm->coordinates = c;
40116636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4012b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
401303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
40146636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40156636e97aSMatthew G Knepley }
40166636e97aSMatthew G Knepley 
40176636e97aSMatthew G Knepley #undef __FUNCT__
40186636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
40196636e97aSMatthew G Knepley /*@
40206636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
40216636e97aSMatthew G Knepley 
40226636e97aSMatthew G Knepley   Collective on DM
40236636e97aSMatthew G Knepley 
40246636e97aSMatthew G Knepley    Input Parameters:
40256636e97aSMatthew G Knepley +  dm - the DM
40266636e97aSMatthew G Knepley -  c - coordinate vector
40276636e97aSMatthew G Knepley 
40286636e97aSMatthew G Knepley   Note:
40296636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
40306636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
40316636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
40326636e97aSMatthew G Knepley 
40336636e97aSMatthew G Knepley   Level: intermediate
40346636e97aSMatthew G Knepley 
40356636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40366636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
40376636e97aSMatthew G Knepley @*/
40386636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
40396636e97aSMatthew G Knepley {
40406636e97aSMatthew G Knepley   PetscErrorCode ierr;
40416636e97aSMatthew G Knepley 
40426636e97aSMatthew G Knepley   PetscFunctionBegin;
40436636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40446636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
40456636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
40466636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
40478865f1eaSKarl Rupp 
40486636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
40498865f1eaSKarl Rupp 
40506636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
40516636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40526636e97aSMatthew G Knepley }
40536636e97aSMatthew G Knepley 
40546636e97aSMatthew G Knepley #undef __FUNCT__
40556636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
40566636e97aSMatthew G Knepley /*@
40576636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
40586636e97aSMatthew G Knepley 
40596636e97aSMatthew G Knepley   Not Collective
40606636e97aSMatthew G Knepley 
40616636e97aSMatthew G Knepley   Input Parameter:
40626636e97aSMatthew G Knepley . dm - the DM
40636636e97aSMatthew G Knepley 
40646636e97aSMatthew G Knepley   Output Parameter:
40656636e97aSMatthew G Knepley . c - global coordinate vector
40666636e97aSMatthew G Knepley 
40676636e97aSMatthew G Knepley   Note:
40686636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40696636e97aSMatthew G Knepley 
40706636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
40716636e97aSMatthew G Knepley 
40726636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40736636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40746636e97aSMatthew G Knepley 
40756636e97aSMatthew G Knepley   Level: intermediate
40766636e97aSMatthew G Knepley 
40776636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40786636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
40796636e97aSMatthew G Knepley @*/
40806636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
40816636e97aSMatthew G Knepley {
40826636e97aSMatthew G Knepley   PetscErrorCode ierr;
40836636e97aSMatthew G Knepley 
40846636e97aSMatthew G Knepley   PetscFunctionBegin;
40856636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40866636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40871f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
40880298fd71SBarry Smith     DM cdm = NULL;
40896636e97aSMatthew G Knepley 
40906636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40916636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
40926636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
40936636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40946636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40956636e97aSMatthew G Knepley   }
40966636e97aSMatthew G Knepley   *c = dm->coordinates;
40976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40986636e97aSMatthew G Knepley }
40996636e97aSMatthew G Knepley 
41006636e97aSMatthew G Knepley #undef __FUNCT__
41016636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
41026636e97aSMatthew G Knepley /*@
41036636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
41046636e97aSMatthew G Knepley 
41056636e97aSMatthew G Knepley   Collective on DM
41066636e97aSMatthew G Knepley 
41076636e97aSMatthew G Knepley   Input Parameter:
41086636e97aSMatthew G Knepley . dm - the DM
41096636e97aSMatthew G Knepley 
41106636e97aSMatthew G Knepley   Output Parameter:
41116636e97aSMatthew G Knepley . c - coordinate vector
41126636e97aSMatthew G Knepley 
41136636e97aSMatthew G Knepley   Note:
41146636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
41156636e97aSMatthew G Knepley 
41166636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
41176636e97aSMatthew G Knepley 
41186636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
41196636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
41206636e97aSMatthew G Knepley 
41216636e97aSMatthew G Knepley   Level: intermediate
41226636e97aSMatthew G Knepley 
41236636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41246636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
41256636e97aSMatthew G Knepley @*/
41266636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
41276636e97aSMatthew G Knepley {
41286636e97aSMatthew G Knepley   PetscErrorCode ierr;
41296636e97aSMatthew G Knepley 
41306636e97aSMatthew G Knepley   PetscFunctionBegin;
41316636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41326636e97aSMatthew G Knepley   PetscValidPointer(c,2);
41331f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
41340298fd71SBarry Smith     DM cdm = NULL;
41356636e97aSMatthew G Knepley 
41366636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
41376636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
41386636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
41396636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41406636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41416636e97aSMatthew G Knepley   }
41426636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
41436636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41446636e97aSMatthew G Knepley }
41456636e97aSMatthew G Knepley 
41466636e97aSMatthew G Knepley #undef __FUNCT__
41476636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
41486636e97aSMatthew G Knepley /*@
41491cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
41506636e97aSMatthew G Knepley 
41516636e97aSMatthew G Knepley   Collective on DM
41526636e97aSMatthew G Knepley 
41536636e97aSMatthew G Knepley   Input Parameter:
41546636e97aSMatthew G Knepley . dm - the DM
41556636e97aSMatthew G Knepley 
41566636e97aSMatthew G Knepley   Output Parameter:
41576636e97aSMatthew G Knepley . cdm - coordinate DM
41586636e97aSMatthew G Knepley 
41596636e97aSMatthew G Knepley   Level: intermediate
41606636e97aSMatthew G Knepley 
41616636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41621cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41636636e97aSMatthew G Knepley @*/
41646636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
41656636e97aSMatthew G Knepley {
41666636e97aSMatthew G Knepley   PetscErrorCode ierr;
41676636e97aSMatthew G Knepley 
41686636e97aSMatthew G Knepley   PetscFunctionBegin;
41696636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41706636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
41716636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
417282f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
41736636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
41746636e97aSMatthew G Knepley   }
41756636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
41766636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41776636e97aSMatthew G Knepley }
4178e87bb0d3SMatthew G Knepley 
4179e87bb0d3SMatthew G Knepley #undef __FUNCT__
41801cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
41811cfe2091SMatthew G. Knepley /*@
41821cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
41831cfe2091SMatthew G. Knepley 
41841cfe2091SMatthew G. Knepley   Logically Collective on DM
41851cfe2091SMatthew G. Knepley 
41861cfe2091SMatthew G. Knepley   Input Parameters:
41871cfe2091SMatthew G. Knepley + dm - the DM
41881cfe2091SMatthew G. Knepley - cdm - coordinate DM
41891cfe2091SMatthew G. Knepley 
41901cfe2091SMatthew G. Knepley   Level: intermediate
41911cfe2091SMatthew G. Knepley 
41921cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41931cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41941cfe2091SMatthew G. Knepley @*/
41951cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
41961cfe2091SMatthew G. Knepley {
41971cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
41981cfe2091SMatthew G. Knepley 
41991cfe2091SMatthew G. Knepley   PetscFunctionBegin;
42001cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42011cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
42021cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
42031cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
42041cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
42051cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
42061cfe2091SMatthew G. Knepley }
42071cfe2091SMatthew G. Knepley 
42081cfe2091SMatthew G. Knepley #undef __FUNCT__
420946e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
421046e270d4SMatthew G. Knepley /*@
421146e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
421246e270d4SMatthew G. Knepley 
421346e270d4SMatthew G. Knepley   Not Collective
421446e270d4SMatthew G. Knepley 
421546e270d4SMatthew G. Knepley   Input Parameter:
421646e270d4SMatthew G. Knepley . dm - The DM object
421746e270d4SMatthew G. Knepley 
421846e270d4SMatthew G. Knepley   Output Parameter:
421946e270d4SMatthew G. Knepley . dim - The embedding dimension
422046e270d4SMatthew G. Knepley 
422146e270d4SMatthew G. Knepley   Level: intermediate
422246e270d4SMatthew G. Knepley 
422346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
422446e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
422546e270d4SMatthew G. Knepley @*/
422646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
422746e270d4SMatthew G. Knepley {
422846e270d4SMatthew G. Knepley   PetscFunctionBegin;
422946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
423046e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
42319a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
42329a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
42339a9a41abSToby Isaac   }
423446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
423546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
423646e270d4SMatthew G. Knepley }
423746e270d4SMatthew G. Knepley 
423846e270d4SMatthew G. Knepley #undef __FUNCT__
423946e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
424046e270d4SMatthew G. Knepley /*@
424146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
424246e270d4SMatthew G. Knepley 
424346e270d4SMatthew G. Knepley   Not Collective
424446e270d4SMatthew G. Knepley 
424546e270d4SMatthew G. Knepley   Input Parameters:
424646e270d4SMatthew G. Knepley + dm  - The DM object
424746e270d4SMatthew G. Knepley - dim - The embedding dimension
424846e270d4SMatthew G. Knepley 
424946e270d4SMatthew G. Knepley   Level: intermediate
425046e270d4SMatthew G. Knepley 
425146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
425246e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
425346e270d4SMatthew G. Knepley @*/
425446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
425546e270d4SMatthew G. Knepley {
425646e270d4SMatthew G. Knepley   PetscFunctionBegin;
425746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
425846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
425946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
426046e270d4SMatthew G. Knepley }
426146e270d4SMatthew G. Knepley 
426246e270d4SMatthew G. Knepley #undef __FUNCT__
4263e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4264e8abe2deSMatthew G. Knepley /*@
4265e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4266e8abe2deSMatthew G. Knepley 
4267e8abe2deSMatthew G. Knepley   Not Collective
4268e8abe2deSMatthew G. Knepley 
4269e8abe2deSMatthew G. Knepley   Input Parameter:
4270e8abe2deSMatthew G. Knepley . dm - The DM object
4271e8abe2deSMatthew G. Knepley 
4272e8abe2deSMatthew G. Knepley   Output Parameter:
4273e8abe2deSMatthew G. Knepley . section - The PetscSection object
4274e8abe2deSMatthew G. Knepley 
4275e8abe2deSMatthew G. Knepley   Level: intermediate
4276e8abe2deSMatthew G. Knepley 
4277e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4278e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4279e8abe2deSMatthew G. Knepley @*/
4280e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4281e8abe2deSMatthew G. Knepley {
4282e8abe2deSMatthew G. Knepley   DM             cdm;
4283e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4284e8abe2deSMatthew G. Knepley 
4285e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4286e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4287e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4288e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4289e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4290e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4291e8abe2deSMatthew G. Knepley }
4292e8abe2deSMatthew G. Knepley 
4293e8abe2deSMatthew G. Knepley #undef __FUNCT__
4294e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4295e8abe2deSMatthew G. Knepley /*@
4296e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4297e8abe2deSMatthew G. Knepley 
4298e8abe2deSMatthew G. Knepley   Not Collective
4299e8abe2deSMatthew G. Knepley 
4300e8abe2deSMatthew G. Knepley   Input Parameters:
4301e8abe2deSMatthew G. Knepley + dm      - The DM object
430246e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4303e8abe2deSMatthew G. Knepley - section - The PetscSection object
4304e8abe2deSMatthew G. Knepley 
4305e8abe2deSMatthew G. Knepley   Level: intermediate
4306e8abe2deSMatthew G. Knepley 
4307e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4308e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4309e8abe2deSMatthew G. Knepley @*/
431046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4311e8abe2deSMatthew G. Knepley {
4312e8abe2deSMatthew G. Knepley   DM             cdm;
4313e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4314e8abe2deSMatthew G. Knepley 
4315e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4316e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
431746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4318e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4319e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
432046e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
432146e270d4SMatthew G. Knepley     PetscInt d = dim;
432246e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
432346e270d4SMatthew G. Knepley 
432446e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
432546e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
432646e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
432746e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
432846e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
432946e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
433046e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
433146e270d4SMatthew G. Knepley     }
433246e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
433346e270d4SMatthew G. Knepley   }
4334e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4335e8abe2deSMatthew G. Knepley }
4336e8abe2deSMatthew G. Knepley 
4337e8abe2deSMatthew G. Knepley #undef __FUNCT__
4338c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
43395dc8c3f7SMatthew G. Knepley /*@C
43405dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
43415dc8c3f7SMatthew G. Knepley 
43425dc8c3f7SMatthew G. Knepley   Input Parameters:
43435dc8c3f7SMatthew G. Knepley + dm      - The DM object
43445dc8c3f7SMatthew 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
43455dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43465dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43475dc8c3f7SMatthew G. Knepley 
43485dc8c3f7SMatthew G. Knepley   Level: developer
43495dc8c3f7SMatthew G. Knepley 
43505dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43515dc8c3f7SMatthew G. Knepley @*/
43525dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4353c6b900c6SMatthew G. Knepley {
4354c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4355c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4356c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4357c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
43585dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4359c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4360c6b900c6SMatthew G. Knepley }
4361c6b900c6SMatthew G. Knepley 
4362c6b900c6SMatthew G. Knepley #undef __FUNCT__
4363c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
43645dc8c3f7SMatthew G. Knepley /*@C
43655dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
43665dc8c3f7SMatthew G. Knepley 
43675dc8c3f7SMatthew G. Knepley   Input Parameters:
43685dc8c3f7SMatthew G. Knepley + dm      - The DM object
43695dc8c3f7SMatthew 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
43705dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43715dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43725dc8c3f7SMatthew G. Knepley 
43735dc8c3f7SMatthew G. Knepley   Level: developer
43745dc8c3f7SMatthew G. Knepley 
43755dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43765dc8c3f7SMatthew G. Knepley @*/
43775dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4378c6b900c6SMatthew G. Knepley {
4379c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4380c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4381c6b900c6SMatthew G. Knepley 
4382c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4383c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43845dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
43855dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
43865dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
43875dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
43885dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4389c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4390c6b900c6SMatthew G. Knepley }
4391c6b900c6SMatthew G. Knepley 
4392c6b900c6SMatthew G. Knepley #undef __FUNCT__
43932e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinate"
43942e17dfb7SMatthew G. Knepley /*@
43952e17dfb7SMatthew 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.
43962e17dfb7SMatthew G. Knepley 
43972e17dfb7SMatthew G. Knepley   Input Parameters:
43982e17dfb7SMatthew G. Knepley + dm     - The DM
43992e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
44002e17dfb7SMatthew G. Knepley 
44012e17dfb7SMatthew G. Knepley   Output Parameter:
44022e17dfb7SMatthew G. Knepley . out - The localized coordinate point
44032e17dfb7SMatthew G. Knepley 
44042e17dfb7SMatthew G. Knepley   Level: developer
44052e17dfb7SMatthew G. Knepley 
44062e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
44072e17dfb7SMatthew G. Knepley @*/
44082e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscScalar out[])
44092e17dfb7SMatthew G. Knepley {
44102e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
44112e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
44122e17dfb7SMatthew G. Knepley 
44132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
44142e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
44152e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
44162e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
44172e17dfb7SMatthew G. Knepley   } else {
44182e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
44192e17dfb7SMatthew G. Knepley       out[d] = in[d] - dm->L[d]*floor(PetscRealPart(in[d])/dm->L[d]);
44202e17dfb7SMatthew G. Knepley     }
44212e17dfb7SMatthew G. Knepley   }
44222e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
44232e17dfb7SMatthew G. Knepley }
44242e17dfb7SMatthew G. Knepley 
44252e17dfb7SMatthew G. Knepley #undef __FUNCT__
44262e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinate_Internal"
44272e17dfb7SMatthew G. Knepley /*
44282e17dfb7SMatthew 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.
44292e17dfb7SMatthew G. Knepley 
44302e17dfb7SMatthew G. Knepley   Input Parameters:
44312e17dfb7SMatthew G. Knepley + dm     - The DM
44322e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
44332e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
44342e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
44352e17dfb7SMatthew G. Knepley 
44362e17dfb7SMatthew G. Knepley   Output Parameter:
44372e17dfb7SMatthew G. Knepley . out - The localized coordinate point
44382e17dfb7SMatthew G. Knepley 
44392e17dfb7SMatthew G. Knepley   Level: developer
44402e17dfb7SMatthew G. Knepley 
44412e17dfb7SMatthew 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
44422e17dfb7SMatthew G. Knepley 
44432e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
44442e17dfb7SMatthew G. Knepley */
44452e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
44462e17dfb7SMatthew G. Knepley {
44472e17dfb7SMatthew G. Knepley   PetscInt d;
44482e17dfb7SMatthew G. Knepley 
44492e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
44502e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
44512e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
44522e17dfb7SMatthew G. Knepley   } else {
44532e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
44542e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
44552e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
44562e17dfb7SMatthew G. Knepley       } else {
44572e17dfb7SMatthew G. Knepley         out[d] = in[d];
44582e17dfb7SMatthew G. Knepley       }
44592e17dfb7SMatthew G. Knepley     }
44602e17dfb7SMatthew G. Knepley   }
44612e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
44622e17dfb7SMatthew G. Knepley }
44632e17dfb7SMatthew G. Knepley #undef __FUNCT__
44642e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinateReal_Internal"
44652e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
44662e17dfb7SMatthew G. Knepley {
44672e17dfb7SMatthew G. Knepley   PetscInt d;
44682e17dfb7SMatthew G. Knepley 
44692e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
44702e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
44712e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
44722e17dfb7SMatthew G. Knepley   } else {
44732e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
44742e17dfb7SMatthew G. Knepley       if (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d]) {
44752e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
44762e17dfb7SMatthew G. Knepley       } else {
44772e17dfb7SMatthew G. Knepley         out[d] = in[d];
44782e17dfb7SMatthew G. Knepley       }
44792e17dfb7SMatthew G. Knepley     }
44802e17dfb7SMatthew G. Knepley   }
44812e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
44822e17dfb7SMatthew G. Knepley }
44832e17dfb7SMatthew G. Knepley 
44842e17dfb7SMatthew G. Knepley #undef __FUNCT__
44852e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeAddCoordinate_Internal"
44862e17dfb7SMatthew G. Knepley /*
44872e17dfb7SMatthew 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.
44882e17dfb7SMatthew G. Knepley 
44892e17dfb7SMatthew G. Knepley   Input Parameters:
44902e17dfb7SMatthew G. Knepley + dm     - The DM
44912e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
44922e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
44932e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
44942e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
44952e17dfb7SMatthew G. Knepley 
44962e17dfb7SMatthew G. Knepley   Output Parameter:
44972e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
44982e17dfb7SMatthew G. Knepley 
44992e17dfb7SMatthew G. Knepley   Level: developer
45002e17dfb7SMatthew G. Knepley 
45012e17dfb7SMatthew 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
45022e17dfb7SMatthew G. Knepley 
45032e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
45042e17dfb7SMatthew G. Knepley */
45052e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45062e17dfb7SMatthew G. Knepley {
45072e17dfb7SMatthew G. Knepley   PetscInt d;
45082e17dfb7SMatthew G. Knepley 
45092e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45102e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45112e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
45122e17dfb7SMatthew G. Knepley   } else {
45132e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45142e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
45152e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
45162e17dfb7SMatthew G. Knepley       } else {
45172e17dfb7SMatthew G. Knepley         out[d] += in[d];
45182e17dfb7SMatthew G. Knepley       }
45192e17dfb7SMatthew G. Knepley     }
45202e17dfb7SMatthew G. Knepley   }
45212e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45222e17dfb7SMatthew G. Knepley }
45232e17dfb7SMatthew G. Knepley 
45242e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetDepthStratum(DM, PetscInt, PetscInt *, PetscInt *);
45252e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetHeightStratum(DM, PetscInt, PetscInt *, PetscInt *);
45262e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecGetClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
45272e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecRestoreClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
45282e17dfb7SMatthew G. Knepley 
45292e17dfb7SMatthew G. Knepley #undef __FUNCT__
45302e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinates"
45312e17dfb7SMatthew G. Knepley /*@
45322e17dfb7SMatthew G. Knepley   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for each cell
45332e17dfb7SMatthew G. Knepley 
45342e17dfb7SMatthew G. Knepley   Input Parameter:
45352e17dfb7SMatthew G. Knepley . dm - The DM
45362e17dfb7SMatthew G. Knepley 
45372e17dfb7SMatthew G. Knepley   Level: developer
45382e17dfb7SMatthew G. Knepley 
45392e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
45402e17dfb7SMatthew G. Knepley @*/
45412e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
45422e17dfb7SMatthew G. Knepley {
45432e17dfb7SMatthew G. Knepley   DM             cdm;
45442e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
45452e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
45462e17dfb7SMatthew G. Knepley   PetscScalar   *coords, *coords2, *anchor;
45472e17dfb7SMatthew G. Knepley   PetscInt       Nc, cStart, cEnd, c, vStart, vEnd, v, dof, d, off, off2, bs, coordSize;
45482e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
45492e17dfb7SMatthew G. Knepley 
45502e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45512e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45522e17dfb7SMatthew G. Knepley   if (!dm->maxCell) PetscFunctionReturn(0);
45532e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
45542e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
45552e17dfb7SMatthew G. Knepley   {
45562e17dfb7SMatthew G. Knepley     PetscBool isplex;
45572e17dfb7SMatthew G. Knepley 
45582e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
45592e17dfb7SMatthew G. Knepley     if (isplex) {
45602e17dfb7SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
45612e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
45622e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
45632e17dfb7SMatthew G. Knepley   }
45642e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
45652e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
45662e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
45672e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
45682e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
45692e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
45702e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetChart(cSection, cStart, vEnd);CHKERRQ(ierr);
45712e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
45722e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
45732e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
45742e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
45752e17dfb7SMatthew G. Knepley   }
45762e17dfb7SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
45772e17dfb7SMatthew G. Knepley     ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, NULL);CHKERRQ(ierr);
45782e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
45792e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
45802e17dfb7SMatthew G. Knepley   }
45812e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
45822e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
45832e17dfb7SMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject) dm), &cVec);CHKERRQ(ierr);
45842e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
45852e17dfb7SMatthew G. Knepley   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
45862e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
45872e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
45882e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec,VECSTANDARD);CHKERRQ(ierr);
45892e17dfb7SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
45902e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec,        &coords2);CHKERRQ(ierr);
45912e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
45922e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
45932e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
45942e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
45952e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
45962e17dfb7SMatthew G. Knepley   }
45972e17dfb7SMatthew G. Knepley   ierr = DMGetWorkArray(dm, 3, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
45982e17dfb7SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
45992e17dfb7SMatthew G. Knepley     PetscScalar *cellCoords = NULL;
46002e17dfb7SMatthew G. Knepley     PetscInt     b;
46012e17dfb7SMatthew G. Knepley 
46022e17dfb7SMatthew G. Knepley     ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
46032e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
46042e17dfb7SMatthew G. Knepley     for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
46052e17dfb7SMatthew G. Knepley     for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
46062e17dfb7SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
46072e17dfb7SMatthew G. Knepley   }
46082e17dfb7SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 3, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
46092e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
46102e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec,        &coords2);CHKERRQ(ierr);
46112e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
46122e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
46132e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
46142e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
46152e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46162e17dfb7SMatthew G. Knepley }
46172e17dfb7SMatthew G. Knepley 
46182e17dfb7SMatthew G. Knepley #undef __FUNCT__
4619e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4620e87bb0d3SMatthew G Knepley /*@
4621e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4622e87bb0d3SMatthew G Knepley 
4623e87bb0d3SMatthew G Knepley   Not collective
4624e87bb0d3SMatthew G Knepley 
4625e87bb0d3SMatthew G Knepley   Input Parameters:
4626e87bb0d3SMatthew G Knepley + dm - The DM
4627e87bb0d3SMatthew G Knepley - v - The Vec of points
4628e87bb0d3SMatthew G Knepley 
462961e3bb9bSMatthew G Knepley   Output Parameter:
4630e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4631e87bb0d3SMatthew G Knepley 
4632e87bb0d3SMatthew G Knepley   Level: developer
463361e3bb9bSMatthew G Knepley 
463461e3bb9bSMatthew G Knepley .keywords: point location, mesh
463561e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
463661e3bb9bSMatthew G Knepley @*/
4637e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4638e87bb0d3SMatthew G Knepley {
4639735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4640735aa83eSMatthew G Knepley 
4641e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4642e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4643e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4644e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
464547a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4646735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4647735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
464882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
464947a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4650e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4651e87bb0d3SMatthew G Knepley }
465214f150ffSMatthew G. Knepley 
465314f150ffSMatthew G. Knepley #undef __FUNCT__
465414f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4655f4d763aaSMatthew G. Knepley /*@
4656f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4657f4d763aaSMatthew G. Knepley 
4658f4d763aaSMatthew G. Knepley   Input Parameter:
4659f4d763aaSMatthew G. Knepley . dm - The original DM
4660f4d763aaSMatthew G. Knepley 
4661f4d763aaSMatthew G. Knepley   Output Parameter:
4662f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4663f4d763aaSMatthew G. Knepley 
4664f4d763aaSMatthew G. Knepley   Level: intermediate
4665f4d763aaSMatthew G. Knepley 
4666f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4667f4d763aaSMatthew G. Knepley @*/
466814f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
466914f150ffSMatthew G. Knepley {
4670c26acbdeSMatthew G. Knepley   PetscSection   section;
4671c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
467214f150ffSMatthew G. Knepley   PetscErrorCode ierr;
467314f150ffSMatthew G. Knepley 
467414f150ffSMatthew G. Knepley   PetscFunctionBegin;
467514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
467614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4677c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4678c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4679c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4680c26acbdeSMatthew G. Knepley     *odm = dm;
4681c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4682c26acbdeSMatthew G. Knepley   }
468314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4684c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
468514f150ffSMatthew G. Knepley     PetscSF      sf;
468614f150ffSMatthew G. Knepley 
468714f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
468814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
468914f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
469014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
469114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
469215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
469314f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
469414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
469514f150ffSMatthew G. Knepley   }
469614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
469714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
469814f150ffSMatthew G. Knepley }
4699f4d763aaSMatthew G. Knepley 
4700f4d763aaSMatthew G. Knepley #undef __FUNCT__
4701f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4702f4d763aaSMatthew G. Knepley /*@
4703cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4704f4d763aaSMatthew G. Knepley 
4705f4d763aaSMatthew G. Knepley   Input Parameter:
4706f4d763aaSMatthew G. Knepley . dm - The original DM
4707f4d763aaSMatthew G. Knepley 
4708cdb7a50dSMatthew G. Knepley   Output Parameters:
4709cdb7a50dSMatthew G. Knepley + num - The output sequence number
4710cdb7a50dSMatthew G. Knepley - val - The output sequence value
4711f4d763aaSMatthew G. Knepley 
4712f4d763aaSMatthew G. Knepley   Level: intermediate
4713f4d763aaSMatthew G. Knepley 
4714f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4715f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4716f4d763aaSMatthew G. Knepley 
4717f4d763aaSMatthew G. Knepley .seealso: VecView()
4718f4d763aaSMatthew G. Knepley @*/
4719cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4720f4d763aaSMatthew G. Knepley {
4721f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4722f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4723cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4724cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4725f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4726f4d763aaSMatthew G. Knepley }
4727f4d763aaSMatthew G. Knepley 
4728f4d763aaSMatthew G. Knepley #undef __FUNCT__
4729f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4730f4d763aaSMatthew G. Knepley /*@
4731cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4732f4d763aaSMatthew G. Knepley 
4733f4d763aaSMatthew G. Knepley   Input Parameters:
4734f4d763aaSMatthew G. Knepley + dm - The original DM
4735cdb7a50dSMatthew G. Knepley . num - The output sequence number
4736cdb7a50dSMatthew G. Knepley - val - The output sequence value
4737f4d763aaSMatthew G. Knepley 
4738f4d763aaSMatthew G. Knepley   Level: intermediate
4739f4d763aaSMatthew G. Knepley 
4740f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4741f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4742f4d763aaSMatthew G. Knepley 
4743f4d763aaSMatthew G. Knepley .seealso: VecView()
4744f4d763aaSMatthew G. Knepley @*/
4745cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4746f4d763aaSMatthew G. Knepley {
4747f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4748f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4749f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4750cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4751cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4752cdb7a50dSMatthew G. Knepley }
4753cdb7a50dSMatthew G. Knepley 
4754cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4755cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4756cdb7a50dSMatthew G. Knepley /*@C
4757cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4758cdb7a50dSMatthew G. Knepley 
4759cdb7a50dSMatthew G. Knepley   Input Parameters:
4760cdb7a50dSMatthew G. Knepley + dm   - The original DM
4761cdb7a50dSMatthew G. Knepley . name - The sequence name
4762cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4763cdb7a50dSMatthew G. Knepley 
4764cdb7a50dSMatthew G. Knepley   Output Parameter:
4765cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4766cdb7a50dSMatthew G. Knepley 
4767cdb7a50dSMatthew G. Knepley   Level: intermediate
4768cdb7a50dSMatthew G. Knepley 
4769cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4770cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4771cdb7a50dSMatthew G. Knepley 
4772cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4773cdb7a50dSMatthew G. Knepley @*/
4774cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4775cdb7a50dSMatthew G. Knepley {
4776cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4777cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4778cdb7a50dSMatthew G. Knepley 
4779cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4780cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4781cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4782cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4783cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4784cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4785cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4786cdb7a50dSMatthew G. Knepley     PetscScalar value;
4787cdb7a50dSMatthew G. Knepley 
4788cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
47894aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4790cdb7a50dSMatthew G. Knepley #endif
4791cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4792f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4793f4d763aaSMatthew G. Knepley }
47948e4ac7eaSMatthew G. Knepley 
47958e4ac7eaSMatthew G. Knepley #undef __FUNCT__
47968e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
47978e4ac7eaSMatthew G. Knepley /*@
47988e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
47998e4ac7eaSMatthew G. Knepley 
48008e4ac7eaSMatthew G. Knepley   Not collective
48018e4ac7eaSMatthew G. Knepley 
48028e4ac7eaSMatthew G. Knepley   Input Parameter:
48038e4ac7eaSMatthew G. Knepley . dm - The DM
48048e4ac7eaSMatthew G. Knepley 
48058e4ac7eaSMatthew G. Knepley   Output Parameter:
48068e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
48078e4ac7eaSMatthew G. Knepley 
48088e4ac7eaSMatthew G. Knepley   Level: beginner
48098e4ac7eaSMatthew G. Knepley 
48108e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
48118e4ac7eaSMatthew G. Knepley @*/
48128e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
48138e4ac7eaSMatthew G. Knepley {
48148e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
48158e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48168e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
48178e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
48188e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
48198e4ac7eaSMatthew G. Knepley }
48208e4ac7eaSMatthew G. Knepley 
48218e4ac7eaSMatthew G. Knepley #undef __FUNCT__
48228e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
48238e4ac7eaSMatthew G. Knepley /*@
48248e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
48258e4ac7eaSMatthew G. Knepley 
48268e4ac7eaSMatthew G. Knepley   Collective on dm
48278e4ac7eaSMatthew G. Knepley 
48288e4ac7eaSMatthew G. Knepley   Input Parameters:
48298e4ac7eaSMatthew G. Knepley + dm - The DM
48308e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
48318e4ac7eaSMatthew G. Knepley 
48328e4ac7eaSMatthew G. Knepley   Level: beginner
48338e4ac7eaSMatthew G. Knepley 
48348e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
48358e4ac7eaSMatthew G. Knepley @*/
48368e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
48378e4ac7eaSMatthew G. Knepley {
48388e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
48398e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48408e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
48418e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
48428e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
48438e4ac7eaSMatthew G. Knepley }
4844c58f1c22SToby Isaac 
4845c58f1c22SToby Isaac #undef __FUNCT__
4846c58f1c22SToby Isaac #define __FUNCT__
4847c58f1c22SToby Isaac 
4848c58f1c22SToby Isaac #undef __FUNCT__
4849c58f1c22SToby Isaac #define __FUNCT__ "DMCreateLabel"
4850c58f1c22SToby Isaac /*@C
4851c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
4852c58f1c22SToby Isaac 
4853c58f1c22SToby Isaac   Not Collective
4854c58f1c22SToby Isaac 
4855c58f1c22SToby Isaac   Input Parameters:
4856c58f1c22SToby Isaac + dm   - The DM object
4857c58f1c22SToby Isaac - name - The label name
4858c58f1c22SToby Isaac 
4859c58f1c22SToby Isaac   Level: intermediate
4860c58f1c22SToby Isaac 
4861c58f1c22SToby Isaac .keywords: mesh
4862c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4863c58f1c22SToby Isaac @*/
4864c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
4865c58f1c22SToby Isaac {
4866c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
4867c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
4868c58f1c22SToby Isaac   PetscErrorCode ierr;
4869c58f1c22SToby Isaac 
4870c58f1c22SToby Isaac   PetscFunctionBegin;
4871c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4872c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4873c58f1c22SToby Isaac   while (next) {
4874c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
4875c58f1c22SToby Isaac     if (flg) break;
4876c58f1c22SToby Isaac     next = next->next;
4877c58f1c22SToby Isaac   }
4878c58f1c22SToby Isaac   if (!flg) {
4879c58f1c22SToby Isaac     DMLabelLink tmpLabel;
4880c58f1c22SToby Isaac 
4881c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
4882c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
4883c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
4884c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
4885c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
4886c58f1c22SToby Isaac   }
4887c58f1c22SToby Isaac   PetscFunctionReturn(0);
4888c58f1c22SToby Isaac }
4889c58f1c22SToby Isaac 
4890c58f1c22SToby Isaac #undef __FUNCT__
4891c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelValue"
4892c58f1c22SToby Isaac /*@C
4893c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
4894c58f1c22SToby Isaac 
4895c58f1c22SToby Isaac   Not Collective
4896c58f1c22SToby Isaac 
4897c58f1c22SToby Isaac   Input Parameters:
4898c58f1c22SToby Isaac + dm   - The DM object
4899c58f1c22SToby Isaac . name - The label name
4900c58f1c22SToby Isaac - point - The mesh point
4901c58f1c22SToby Isaac 
4902c58f1c22SToby Isaac   Output Parameter:
4903c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
4904c58f1c22SToby Isaac 
4905c58f1c22SToby Isaac   Level: beginner
4906c58f1c22SToby Isaac 
4907c58f1c22SToby Isaac .keywords: mesh
4908c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
4909c58f1c22SToby Isaac @*/
4910c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
4911c58f1c22SToby Isaac {
4912c58f1c22SToby Isaac   DMLabel        label;
4913c58f1c22SToby Isaac   PetscErrorCode ierr;
4914c58f1c22SToby Isaac 
4915c58f1c22SToby Isaac   PetscFunctionBegin;
4916c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4917c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4918c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4919c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
4920c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
4921c58f1c22SToby Isaac   PetscFunctionReturn(0);
4922c58f1c22SToby Isaac }
4923c58f1c22SToby Isaac 
4924c58f1c22SToby Isaac #undef __FUNCT__
4925c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelValue"
4926c58f1c22SToby Isaac /*@C
4927c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
4928c58f1c22SToby Isaac 
4929c58f1c22SToby Isaac   Not Collective
4930c58f1c22SToby Isaac 
4931c58f1c22SToby Isaac   Input Parameters:
4932c58f1c22SToby Isaac + dm   - The DM object
4933c58f1c22SToby Isaac . name - The label name
4934c58f1c22SToby Isaac . point - The mesh point
4935c58f1c22SToby Isaac - value - The label value for this point
4936c58f1c22SToby Isaac 
4937c58f1c22SToby Isaac   Output Parameter:
4938c58f1c22SToby Isaac 
4939c58f1c22SToby Isaac   Level: beginner
4940c58f1c22SToby Isaac 
4941c58f1c22SToby Isaac .keywords: mesh
4942c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
4943c58f1c22SToby Isaac @*/
4944c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4945c58f1c22SToby Isaac {
4946c58f1c22SToby Isaac   DMLabel        label;
4947c58f1c22SToby Isaac   PetscErrorCode ierr;
4948c58f1c22SToby Isaac 
4949c58f1c22SToby Isaac   PetscFunctionBegin;
4950c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4951c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4952c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4953c58f1c22SToby Isaac   if (!label) {
4954c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
4955c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4956c58f1c22SToby Isaac   }
4957c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
4958c58f1c22SToby Isaac   PetscFunctionReturn(0);
4959c58f1c22SToby Isaac }
4960c58f1c22SToby Isaac 
4961c58f1c22SToby Isaac #undef __FUNCT__
4962c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelValue"
4963c58f1c22SToby Isaac /*@C
4964c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
4965c58f1c22SToby Isaac 
4966c58f1c22SToby Isaac   Not Collective
4967c58f1c22SToby Isaac 
4968c58f1c22SToby Isaac   Input Parameters:
4969c58f1c22SToby Isaac + dm   - The DM object
4970c58f1c22SToby Isaac . name - The label name
4971c58f1c22SToby Isaac . point - The mesh point
4972c58f1c22SToby Isaac - value - The label value for this point
4973c58f1c22SToby Isaac 
4974c58f1c22SToby Isaac   Output Parameter:
4975c58f1c22SToby Isaac 
4976c58f1c22SToby Isaac   Level: beginner
4977c58f1c22SToby Isaac 
4978c58f1c22SToby Isaac .keywords: mesh
4979c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
4980c58f1c22SToby Isaac @*/
4981c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4982c58f1c22SToby Isaac {
4983c58f1c22SToby Isaac   DMLabel        label;
4984c58f1c22SToby Isaac   PetscErrorCode ierr;
4985c58f1c22SToby Isaac 
4986c58f1c22SToby Isaac   PetscFunctionBegin;
4987c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4988c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4989c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4990c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4991c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
4992c58f1c22SToby Isaac   PetscFunctionReturn(0);
4993c58f1c22SToby Isaac }
4994c58f1c22SToby Isaac 
4995c58f1c22SToby Isaac #undef __FUNCT__
4996c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelSize"
4997c58f1c22SToby Isaac /*@C
4998c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
4999c58f1c22SToby Isaac 
5000c58f1c22SToby Isaac   Not Collective
5001c58f1c22SToby Isaac 
5002c58f1c22SToby Isaac   Input Parameters:
5003c58f1c22SToby Isaac + dm   - The DM object
5004c58f1c22SToby Isaac - name - The label name
5005c58f1c22SToby Isaac 
5006c58f1c22SToby Isaac   Output Parameter:
5007c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5008c58f1c22SToby Isaac 
5009c58f1c22SToby Isaac   Level: beginner
5010c58f1c22SToby Isaac 
5011c58f1c22SToby Isaac .keywords: mesh
5012c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5013c58f1c22SToby Isaac @*/
5014c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5015c58f1c22SToby Isaac {
5016c58f1c22SToby Isaac   DMLabel        label;
5017c58f1c22SToby Isaac   PetscErrorCode ierr;
5018c58f1c22SToby Isaac 
5019c58f1c22SToby Isaac   PetscFunctionBegin;
5020c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5021c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5022c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5023c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5024c58f1c22SToby Isaac   *size = 0;
5025c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5026c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5027c58f1c22SToby Isaac   PetscFunctionReturn(0);
5028c58f1c22SToby Isaac }
5029c58f1c22SToby Isaac 
5030c58f1c22SToby Isaac #undef __FUNCT__
5031c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelIdIS"
5032c58f1c22SToby Isaac /*@C
5033c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5034c58f1c22SToby Isaac 
5035c58f1c22SToby Isaac   Not Collective
5036c58f1c22SToby Isaac 
5037c58f1c22SToby Isaac   Input Parameters:
5038c58f1c22SToby Isaac + mesh - The DM object
5039c58f1c22SToby Isaac - name - The label name
5040c58f1c22SToby Isaac 
5041c58f1c22SToby Isaac   Output Parameter:
5042c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5043c58f1c22SToby Isaac 
5044c58f1c22SToby Isaac   Level: beginner
5045c58f1c22SToby Isaac 
5046c58f1c22SToby Isaac .keywords: mesh
5047c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5048c58f1c22SToby Isaac @*/
5049c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5050c58f1c22SToby Isaac {
5051c58f1c22SToby Isaac   DMLabel        label;
5052c58f1c22SToby Isaac   PetscErrorCode ierr;
5053c58f1c22SToby Isaac 
5054c58f1c22SToby Isaac   PetscFunctionBegin;
5055c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5056c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5057c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5058c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5059c58f1c22SToby Isaac   *ids = NULL;
5060c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5061c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5062c58f1c22SToby Isaac   PetscFunctionReturn(0);
5063c58f1c22SToby Isaac }
5064c58f1c22SToby Isaac 
5065c58f1c22SToby Isaac #undef __FUNCT__
5066c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumSize"
5067c58f1c22SToby Isaac /*@C
5068c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5069c58f1c22SToby Isaac 
5070c58f1c22SToby Isaac   Not Collective
5071c58f1c22SToby Isaac 
5072c58f1c22SToby Isaac   Input Parameters:
5073c58f1c22SToby Isaac + dm - The DM object
5074c58f1c22SToby Isaac . name - The label name
5075c58f1c22SToby Isaac - value - The stratum value
5076c58f1c22SToby Isaac 
5077c58f1c22SToby Isaac   Output Parameter:
5078c58f1c22SToby Isaac . size - The stratum size
5079c58f1c22SToby Isaac 
5080c58f1c22SToby Isaac   Level: beginner
5081c58f1c22SToby Isaac 
5082c58f1c22SToby Isaac .keywords: mesh
5083c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5084c58f1c22SToby Isaac @*/
5085c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5086c58f1c22SToby Isaac {
5087c58f1c22SToby Isaac   DMLabel        label;
5088c58f1c22SToby Isaac   PetscErrorCode ierr;
5089c58f1c22SToby Isaac 
5090c58f1c22SToby Isaac   PetscFunctionBegin;
5091c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5092c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5093c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5094c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5095c58f1c22SToby Isaac   *size = 0;
5096c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5097c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5098c58f1c22SToby Isaac   PetscFunctionReturn(0);
5099c58f1c22SToby Isaac }
5100c58f1c22SToby Isaac 
5101c58f1c22SToby Isaac #undef __FUNCT__
5102c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumIS"
5103c58f1c22SToby Isaac /*@C
5104c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5105c58f1c22SToby Isaac 
5106c58f1c22SToby Isaac   Not Collective
5107c58f1c22SToby Isaac 
5108c58f1c22SToby Isaac   Input Parameters:
5109c58f1c22SToby Isaac + dm - The DM object
5110c58f1c22SToby Isaac . name - The label name
5111c58f1c22SToby Isaac - value - The stratum value
5112c58f1c22SToby Isaac 
5113c58f1c22SToby Isaac   Output Parameter:
5114c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5115c58f1c22SToby Isaac 
5116c58f1c22SToby Isaac   Level: beginner
5117c58f1c22SToby Isaac 
5118c58f1c22SToby Isaac .keywords: mesh
5119c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5120c58f1c22SToby Isaac @*/
5121c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5122c58f1c22SToby Isaac {
5123c58f1c22SToby Isaac   DMLabel        label;
5124c58f1c22SToby Isaac   PetscErrorCode ierr;
5125c58f1c22SToby Isaac 
5126c58f1c22SToby Isaac   PetscFunctionBegin;
5127c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5128c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5129c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5130c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5131c58f1c22SToby Isaac   *points = NULL;
5132c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5133c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5134c58f1c22SToby Isaac   PetscFunctionReturn(0);
5135c58f1c22SToby Isaac }
5136c58f1c22SToby Isaac 
5137c58f1c22SToby Isaac #undef __FUNCT__
5138c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelStratum"
5139c58f1c22SToby Isaac /*@C
5140c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5141c58f1c22SToby Isaac 
5142c58f1c22SToby Isaac   Not Collective
5143c58f1c22SToby Isaac 
5144c58f1c22SToby Isaac   Input Parameters:
5145c58f1c22SToby Isaac + dm   - The DM object
5146c58f1c22SToby Isaac . name - The label name
5147c58f1c22SToby Isaac - value - The label value for this point
5148c58f1c22SToby Isaac 
5149c58f1c22SToby Isaac   Output Parameter:
5150c58f1c22SToby Isaac 
5151c58f1c22SToby Isaac   Level: beginner
5152c58f1c22SToby Isaac 
5153c58f1c22SToby Isaac .keywords: mesh
5154c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5155c58f1c22SToby Isaac @*/
5156c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5157c58f1c22SToby Isaac {
5158c58f1c22SToby Isaac   DMLabel        label;
5159c58f1c22SToby Isaac   PetscErrorCode ierr;
5160c58f1c22SToby Isaac 
5161c58f1c22SToby Isaac   PetscFunctionBegin;
5162c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5163c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5164c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5165c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5166c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5167c58f1c22SToby Isaac   PetscFunctionReturn(0);
5168c58f1c22SToby Isaac }
5169c58f1c22SToby Isaac 
5170c58f1c22SToby Isaac #undef __FUNCT__
5171c58f1c22SToby Isaac #define __FUNCT__ "DMGetNumLabels"
5172c58f1c22SToby Isaac /*@
5173c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5174c58f1c22SToby Isaac 
5175c58f1c22SToby Isaac   Not Collective
5176c58f1c22SToby Isaac 
5177c58f1c22SToby Isaac   Input Parameter:
5178c58f1c22SToby Isaac . dm   - The DM object
5179c58f1c22SToby Isaac 
5180c58f1c22SToby Isaac   Output Parameter:
5181c58f1c22SToby Isaac . numLabels - the number of Labels
5182c58f1c22SToby Isaac 
5183c58f1c22SToby Isaac   Level: intermediate
5184c58f1c22SToby Isaac 
5185c58f1c22SToby Isaac .keywords: mesh
5186c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5187c58f1c22SToby Isaac @*/
5188c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5189c58f1c22SToby Isaac {
5190c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5191c58f1c22SToby Isaac   PetscInt  n    = 0;
5192c58f1c22SToby Isaac 
5193c58f1c22SToby Isaac   PetscFunctionBegin;
5194c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5195c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5196c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5197c58f1c22SToby Isaac   *numLabels = n;
5198c58f1c22SToby Isaac   PetscFunctionReturn(0);
5199c58f1c22SToby Isaac }
5200c58f1c22SToby Isaac 
5201c58f1c22SToby Isaac #undef __FUNCT__
5202c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelName"
5203c58f1c22SToby Isaac /*@C
5204c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5205c58f1c22SToby Isaac 
5206c58f1c22SToby Isaac   Not Collective
5207c58f1c22SToby Isaac 
5208c58f1c22SToby Isaac   Input Parameters:
5209c58f1c22SToby Isaac + dm - The DM object
5210c58f1c22SToby Isaac - n  - the label number
5211c58f1c22SToby Isaac 
5212c58f1c22SToby Isaac   Output Parameter:
5213c58f1c22SToby Isaac . name - the label name
5214c58f1c22SToby Isaac 
5215c58f1c22SToby Isaac   Level: intermediate
5216c58f1c22SToby Isaac 
5217c58f1c22SToby Isaac .keywords: mesh
5218c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5219c58f1c22SToby Isaac @*/
5220c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5221c58f1c22SToby Isaac {
5222c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5223c58f1c22SToby Isaac   PetscInt  l    = 0;
5224c58f1c22SToby Isaac 
5225c58f1c22SToby Isaac   PetscFunctionBegin;
5226c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5227c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5228c58f1c22SToby Isaac   while (next) {
5229c58f1c22SToby Isaac     if (l == n) {
5230c58f1c22SToby Isaac       *name = next->label->name;
5231c58f1c22SToby Isaac       PetscFunctionReturn(0);
5232c58f1c22SToby Isaac     }
5233c58f1c22SToby Isaac     ++l;
5234c58f1c22SToby Isaac     next = next->next;
5235c58f1c22SToby Isaac   }
5236c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5237c58f1c22SToby Isaac }
5238c58f1c22SToby Isaac 
5239c58f1c22SToby Isaac #undef __FUNCT__
5240c58f1c22SToby Isaac #define __FUNCT__ "DMHasLabel"
5241c58f1c22SToby Isaac /*@C
5242c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5243c58f1c22SToby Isaac 
5244c58f1c22SToby Isaac   Not Collective
5245c58f1c22SToby Isaac 
5246c58f1c22SToby Isaac   Input Parameters:
5247c58f1c22SToby Isaac + dm   - The DM object
5248c58f1c22SToby Isaac - name - The label name
5249c58f1c22SToby Isaac 
5250c58f1c22SToby Isaac   Output Parameter:
5251c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5252c58f1c22SToby Isaac 
5253c58f1c22SToby Isaac   Level: intermediate
5254c58f1c22SToby Isaac 
5255c58f1c22SToby Isaac .keywords: mesh
5256c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5257c58f1c22SToby Isaac @*/
5258c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5259c58f1c22SToby Isaac {
5260c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5261c58f1c22SToby Isaac   PetscErrorCode ierr;
5262c58f1c22SToby Isaac 
5263c58f1c22SToby Isaac   PetscFunctionBegin;
5264c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5265c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5266c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5267c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5268c58f1c22SToby Isaac   while (next) {
5269c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5270c58f1c22SToby Isaac     if (*hasLabel) break;
5271c58f1c22SToby Isaac     next = next->next;
5272c58f1c22SToby Isaac   }
5273c58f1c22SToby Isaac   PetscFunctionReturn(0);
5274c58f1c22SToby Isaac }
5275c58f1c22SToby Isaac 
5276c58f1c22SToby Isaac #undef __FUNCT__
5277c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabel"
5278c58f1c22SToby Isaac /*@C
5279c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5280c58f1c22SToby Isaac 
5281c58f1c22SToby Isaac   Not Collective
5282c58f1c22SToby Isaac 
5283c58f1c22SToby Isaac   Input Parameters:
5284c58f1c22SToby Isaac + dm   - The DM object
5285c58f1c22SToby Isaac - name - The label name
5286c58f1c22SToby Isaac 
5287c58f1c22SToby Isaac   Output Parameter:
5288c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5289c58f1c22SToby Isaac 
5290c58f1c22SToby Isaac   Level: intermediate
5291c58f1c22SToby Isaac 
5292c58f1c22SToby Isaac .keywords: mesh
5293c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5294c58f1c22SToby Isaac @*/
5295c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5296c58f1c22SToby Isaac {
5297c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5298c58f1c22SToby Isaac   PetscBool      hasLabel;
5299c58f1c22SToby Isaac   PetscErrorCode ierr;
5300c58f1c22SToby Isaac 
5301c58f1c22SToby Isaac   PetscFunctionBegin;
5302c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5303c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5304c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5305c58f1c22SToby Isaac   *label = NULL;
5306c58f1c22SToby Isaac   while (next) {
5307c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5308c58f1c22SToby Isaac     if (hasLabel) {
5309c58f1c22SToby Isaac       *label = next->label;
5310c58f1c22SToby Isaac       break;
5311c58f1c22SToby Isaac     }
5312c58f1c22SToby Isaac     next = next->next;
5313c58f1c22SToby Isaac   }
5314c58f1c22SToby Isaac   PetscFunctionReturn(0);
5315c58f1c22SToby Isaac }
5316c58f1c22SToby Isaac 
5317c58f1c22SToby Isaac #undef __FUNCT__
5318c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelByNum"
5319c58f1c22SToby Isaac /*@C
5320c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5321c58f1c22SToby Isaac 
5322c58f1c22SToby Isaac   Not Collective
5323c58f1c22SToby Isaac 
5324c58f1c22SToby Isaac   Input Parameters:
5325c58f1c22SToby Isaac + dm - The DM object
5326c58f1c22SToby Isaac - n  - the label number
5327c58f1c22SToby Isaac 
5328c58f1c22SToby Isaac   Output Parameter:
5329c58f1c22SToby Isaac . label - the label
5330c58f1c22SToby Isaac 
5331c58f1c22SToby Isaac   Level: intermediate
5332c58f1c22SToby Isaac 
5333c58f1c22SToby Isaac .keywords: mesh
5334c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5335c58f1c22SToby Isaac @*/
5336c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5337c58f1c22SToby Isaac {
5338c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5339c58f1c22SToby Isaac   PetscInt    l    = 0;
5340c58f1c22SToby Isaac 
5341c58f1c22SToby Isaac   PetscFunctionBegin;
5342c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5343c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5344c58f1c22SToby Isaac   while (next) {
5345c58f1c22SToby Isaac     if (l == n) {
5346c58f1c22SToby Isaac       *label = next->label;
5347c58f1c22SToby Isaac       PetscFunctionReturn(0);
5348c58f1c22SToby Isaac     }
5349c58f1c22SToby Isaac     ++l;
5350c58f1c22SToby Isaac     next = next->next;
5351c58f1c22SToby Isaac   }
5352c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5353c58f1c22SToby Isaac }
5354c58f1c22SToby Isaac 
5355c58f1c22SToby Isaac #undef __FUNCT__
5356c58f1c22SToby Isaac #define __FUNCT__ "DMAddLabel"
5357c58f1c22SToby Isaac /*@C
5358c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5359c58f1c22SToby Isaac 
5360c58f1c22SToby Isaac   Not Collective
5361c58f1c22SToby Isaac 
5362c58f1c22SToby Isaac   Input Parameters:
5363c58f1c22SToby Isaac + dm   - The DM object
5364c58f1c22SToby Isaac - label - The DMLabel
5365c58f1c22SToby Isaac 
5366c58f1c22SToby Isaac   Level: developer
5367c58f1c22SToby Isaac 
5368c58f1c22SToby Isaac .keywords: mesh
5369c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5370c58f1c22SToby Isaac @*/
5371c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5372c58f1c22SToby Isaac {
5373c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5374c58f1c22SToby Isaac   PetscBool      hasLabel;
5375c58f1c22SToby Isaac   PetscErrorCode ierr;
5376c58f1c22SToby Isaac 
5377c58f1c22SToby Isaac   PetscFunctionBegin;
5378c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5379c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5380c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5381c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5382c58f1c22SToby Isaac   tmpLabel->label  = label;
5383c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5384c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5385c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5386c58f1c22SToby Isaac   PetscFunctionReturn(0);
5387c58f1c22SToby Isaac }
5388c58f1c22SToby Isaac 
5389c58f1c22SToby Isaac #undef __FUNCT__
5390c58f1c22SToby Isaac #define __FUNCT__ "DMRemoveLabel"
5391c58f1c22SToby Isaac /*@C
5392c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5393c58f1c22SToby Isaac 
5394c58f1c22SToby Isaac   Not Collective
5395c58f1c22SToby Isaac 
5396c58f1c22SToby Isaac   Input Parameters:
5397c58f1c22SToby Isaac + dm   - The DM object
5398c58f1c22SToby Isaac - name - The label name
5399c58f1c22SToby Isaac 
5400c58f1c22SToby Isaac   Output Parameter:
5401c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5402c58f1c22SToby Isaac 
5403c58f1c22SToby Isaac   Level: developer
5404c58f1c22SToby Isaac 
5405c58f1c22SToby Isaac .keywords: mesh
5406c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5407c58f1c22SToby Isaac @*/
5408c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5409c58f1c22SToby Isaac {
5410c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5411c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5412c58f1c22SToby Isaac   PetscBool      hasLabel;
5413c58f1c22SToby Isaac   PetscErrorCode ierr;
5414c58f1c22SToby Isaac 
5415c58f1c22SToby Isaac   PetscFunctionBegin;
5416c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5417c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5418c58f1c22SToby Isaac   *label = NULL;
5419c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5420c58f1c22SToby Isaac   while (next) {
5421c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5422c58f1c22SToby Isaac     if (hasLabel) {
5423c58f1c22SToby Isaac       if (last) last->next       = next->next;
5424c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5425c58f1c22SToby Isaac       next->next = NULL;
5426c58f1c22SToby Isaac       *label     = next->label;
5427c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5428c58f1c22SToby Isaac       if (hasLabel) {
5429c58f1c22SToby Isaac         dm->depthLabel = NULL;
5430c58f1c22SToby Isaac       }
5431c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5432c58f1c22SToby Isaac       break;
5433c58f1c22SToby Isaac     }
5434c58f1c22SToby Isaac     last = next;
5435c58f1c22SToby Isaac     next = next->next;
5436c58f1c22SToby Isaac   }
5437c58f1c22SToby Isaac   PetscFunctionReturn(0);
5438c58f1c22SToby Isaac }
5439c58f1c22SToby Isaac 
5440c58f1c22SToby Isaac #undef __FUNCT__
5441c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelOutput"
5442c58f1c22SToby Isaac /*@C
5443c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5444c58f1c22SToby Isaac 
5445c58f1c22SToby Isaac   Not Collective
5446c58f1c22SToby Isaac 
5447c58f1c22SToby Isaac   Input Parameters:
5448c58f1c22SToby Isaac + dm   - The DM object
5449c58f1c22SToby Isaac - name - The label name
5450c58f1c22SToby Isaac 
5451c58f1c22SToby Isaac   Output Parameter:
5452c58f1c22SToby Isaac . output - The flag for output
5453c58f1c22SToby Isaac 
5454c58f1c22SToby Isaac   Level: developer
5455c58f1c22SToby Isaac 
5456c58f1c22SToby Isaac .keywords: mesh
5457c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5458c58f1c22SToby Isaac @*/
5459c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5460c58f1c22SToby Isaac {
5461c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5462c58f1c22SToby Isaac   PetscErrorCode ierr;
5463c58f1c22SToby Isaac 
5464c58f1c22SToby Isaac   PetscFunctionBegin;
5465c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5466c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5467c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5468c58f1c22SToby Isaac   while (next) {
5469c58f1c22SToby Isaac     PetscBool flg;
5470c58f1c22SToby Isaac 
5471c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5472c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5473c58f1c22SToby Isaac     next = next->next;
5474c58f1c22SToby Isaac   }
5475c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5476c58f1c22SToby Isaac }
5477c58f1c22SToby Isaac 
5478c58f1c22SToby Isaac #undef __FUNCT__
5479c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelOutput"
5480c58f1c22SToby Isaac /*@C
5481c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5482c58f1c22SToby Isaac 
5483c58f1c22SToby Isaac   Not Collective
5484c58f1c22SToby Isaac 
5485c58f1c22SToby Isaac   Input Parameters:
5486c58f1c22SToby Isaac + dm     - The DM object
5487c58f1c22SToby Isaac . name   - The label name
5488c58f1c22SToby Isaac - output - The flag for output
5489c58f1c22SToby Isaac 
5490c58f1c22SToby Isaac   Level: developer
5491c58f1c22SToby Isaac 
5492c58f1c22SToby Isaac .keywords: mesh
5493c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5494c58f1c22SToby Isaac @*/
5495c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5496c58f1c22SToby Isaac {
5497c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5498c58f1c22SToby Isaac   PetscErrorCode ierr;
5499c58f1c22SToby Isaac 
5500c58f1c22SToby Isaac   PetscFunctionBegin;
5501c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5502c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5503c58f1c22SToby Isaac   while (next) {
5504c58f1c22SToby Isaac     PetscBool flg;
5505c58f1c22SToby Isaac 
5506c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5507c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5508c58f1c22SToby Isaac     next = next->next;
5509c58f1c22SToby Isaac   }
5510c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5511c58f1c22SToby Isaac }
5512c58f1c22SToby Isaac 
5513c58f1c22SToby Isaac 
5514c58f1c22SToby Isaac #undef __FUNCT__
5515c58f1c22SToby Isaac #define __FUNCT__ "DMCopyLabels"
5516c58f1c22SToby Isaac /*@
5517c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5518c58f1c22SToby Isaac 
5519c58f1c22SToby Isaac   Collective on DM
5520c58f1c22SToby Isaac 
5521c58f1c22SToby Isaac   Input Parameter:
55222e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5523c58f1c22SToby Isaac 
5524c58f1c22SToby Isaac   Output Parameter:
55252e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5526c58f1c22SToby Isaac 
5527c58f1c22SToby Isaac   Level: intermediate
5528c58f1c22SToby Isaac 
5529c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5530c58f1c22SToby Isaac 
5531c58f1c22SToby Isaac .keywords: mesh
5532c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5533c58f1c22SToby Isaac @*/
5534c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5535c58f1c22SToby Isaac {
5536c58f1c22SToby Isaac   PetscInt       numLabels, l;
5537c58f1c22SToby Isaac   PetscErrorCode ierr;
5538c58f1c22SToby Isaac 
5539c58f1c22SToby Isaac   PetscFunctionBegin;
5540c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5541c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5542c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5543c58f1c22SToby Isaac     DMLabel     label, labelNew;
5544c58f1c22SToby Isaac     const char *name;
5545c58f1c22SToby Isaac     PetscBool   flg;
5546c58f1c22SToby Isaac 
5547c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5548c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5549c58f1c22SToby Isaac     if (flg) continue;
5550c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5551c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5552c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5553c58f1c22SToby Isaac   }
5554c58f1c22SToby Isaac   PetscFunctionReturn(0);
5555c58f1c22SToby Isaac }
5556a8fb8f29SToby Isaac 
5557a8fb8f29SToby Isaac #undef __FUNCT__
5558a8fb8f29SToby Isaac #define __FUNCT__ "DMGetCoarseDM"
5559a8fb8f29SToby Isaac /*@
5560a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5561a8fb8f29SToby Isaac 
5562a8fb8f29SToby Isaac   Input Parameter:
5563a8fb8f29SToby Isaac . dm - The DM object
5564a8fb8f29SToby Isaac 
5565a8fb8f29SToby Isaac   Output Parameter:
5566a8fb8f29SToby Isaac . cdm - The coarse DM
5567a8fb8f29SToby Isaac 
5568a8fb8f29SToby Isaac   Level: intermediate
5569a8fb8f29SToby Isaac 
5570a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5571a8fb8f29SToby Isaac @*/
5572a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5573a8fb8f29SToby Isaac {
5574a8fb8f29SToby Isaac   PetscFunctionBegin;
5575a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5576a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5577a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5578a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5579a8fb8f29SToby Isaac }
5580a8fb8f29SToby Isaac 
5581a8fb8f29SToby Isaac #undef __FUNCT__
5582a8fb8f29SToby Isaac #define __FUNCT__ "DMSetCoarseDM"
5583a8fb8f29SToby Isaac /*@
5584a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5585a8fb8f29SToby Isaac 
5586a8fb8f29SToby Isaac   Input Parameters:
5587a8fb8f29SToby Isaac + dm - The DM object
5588a8fb8f29SToby Isaac - cdm - The coarse DM
5589a8fb8f29SToby Isaac 
5590a8fb8f29SToby Isaac   Level: intermediate
5591a8fb8f29SToby Isaac 
5592a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5593a8fb8f29SToby Isaac @*/
5594a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5595a8fb8f29SToby Isaac {
5596a8fb8f29SToby Isaac   PetscErrorCode ierr;
5597a8fb8f29SToby Isaac 
5598a8fb8f29SToby Isaac   PetscFunctionBegin;
5599a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5600a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5601a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5602a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5603a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5604a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5605a8fb8f29SToby Isaac }
5606a8fb8f29SToby Isaac 
560788bdff64SToby Isaac #undef __FUNCT__
560888bdff64SToby Isaac #define __FUNCT__ "DMGetFineDM"
560988bdff64SToby Isaac /*@
561088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
561188bdff64SToby Isaac 
561288bdff64SToby Isaac   Input Parameter:
561388bdff64SToby Isaac . dm - The DM object
561488bdff64SToby Isaac 
561588bdff64SToby Isaac   Output Parameter:
561688bdff64SToby Isaac . fdm - The fine DM
561788bdff64SToby Isaac 
561888bdff64SToby Isaac   Level: intermediate
561988bdff64SToby Isaac 
562088bdff64SToby Isaac .seealso: DMSetFineDM()
562188bdff64SToby Isaac @*/
562288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
562388bdff64SToby Isaac {
562488bdff64SToby Isaac   PetscFunctionBegin;
562588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
562688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
562788bdff64SToby Isaac   *fdm = dm->fineMesh;
562888bdff64SToby Isaac   PetscFunctionReturn(0);
562988bdff64SToby Isaac }
563088bdff64SToby Isaac 
563188bdff64SToby Isaac #undef __FUNCT__
563288bdff64SToby Isaac #define __FUNCT__ "DMSetFineDM"
563388bdff64SToby Isaac /*@
563488bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
563588bdff64SToby Isaac 
563688bdff64SToby Isaac   Input Parameters:
563788bdff64SToby Isaac + dm - The DM object
563888bdff64SToby Isaac - fdm - The fine DM
563988bdff64SToby Isaac 
564088bdff64SToby Isaac   Level: intermediate
564188bdff64SToby Isaac 
564288bdff64SToby Isaac .seealso: DMGetFineDM()
564388bdff64SToby Isaac @*/
564488bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
564588bdff64SToby Isaac {
564688bdff64SToby Isaac   PetscErrorCode ierr;
564788bdff64SToby Isaac 
564888bdff64SToby Isaac   PetscFunctionBegin;
564988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
565088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
565188bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
565288bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
565388bdff64SToby Isaac   dm->fineMesh = fdm;
565488bdff64SToby Isaac   PetscFunctionReturn(0);
565588bdff64SToby Isaac }
565688bdff64SToby Isaac 
5657a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5658a6ba4734SToby Isaac 
5659a6ba4734SToby Isaac #undef __FUNCT__
5660bcc5ffd9SToby Isaac #define __FUNCT__ "DMBoundaryDuplicate"
5661354557abSToby Isaac PetscErrorCode DMBoundaryDuplicate(DMBoundaryLinkList bd, DMBoundaryLinkList *boundary)
5662a6ba4734SToby Isaac {
5663bcc5ffd9SToby Isaac   DMBoundary     b = bd->next, b2, bold = NULL;
5664a6ba4734SToby Isaac   PetscErrorCode ierr;
5665a6ba4734SToby Isaac 
5666a6ba4734SToby Isaac   PetscFunctionBegin;
5667bcc5ffd9SToby Isaac   ierr = PetscNew(boundary);CHKERRQ(ierr);
5668bcc5ffd9SToby Isaac   (*boundary)->refct = 1;
5669bcc5ffd9SToby Isaac   (*boundary)->next = NULL;
5670a6ba4734SToby Isaac   for (; b; b = b->next, bold = b2) {
5671a6ba4734SToby Isaac     ierr = PetscNew(&b2);CHKERRQ(ierr);
5672a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
5673a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
5674a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
5675a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
5676a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numcomps, &b2->comps);CHKERRQ(ierr);
5677a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->comps, b->comps, b->numcomps*sizeof(PetscInt));CHKERRQ(ierr);
5678a6ba4734SToby Isaac     b2->label     = NULL;
5679a6ba4734SToby Isaac     b2->essential = b->essential;
5680a6ba4734SToby Isaac     b2->field     = b->field;
5681a6ba4734SToby Isaac     b2->numcomps  = b->numcomps;
5682a6ba4734SToby Isaac     b2->func      = b->func;
5683a6ba4734SToby Isaac     b2->numids    = b->numids;
5684a6ba4734SToby Isaac     b2->ctx       = b->ctx;
5685a6ba4734SToby Isaac     b2->next      = NULL;
5686bcc5ffd9SToby Isaac     if (!(*boundary)->next) (*boundary)->next   = b2;
5687a6ba4734SToby Isaac     if (bold)        bold->next = b2;
5688a6ba4734SToby Isaac   }
5689a6ba4734SToby Isaac   PetscFunctionReturn(0);
5690a6ba4734SToby Isaac }
5691a6ba4734SToby Isaac 
5692a6ba4734SToby Isaac #undef __FUNCT__
5693bcc5ffd9SToby Isaac #define __FUNCT__ "DMBoundaryDestroy"
5694bcc5ffd9SToby Isaac PetscErrorCode DMBoundaryDestroy(DMBoundaryLinkList *boundary)
5695a6ba4734SToby Isaac {
5696a6ba4734SToby Isaac   DMBoundary     b, next;
5697a6ba4734SToby Isaac   PetscErrorCode ierr;
5698a6ba4734SToby Isaac 
5699a6ba4734SToby Isaac   PetscFunctionBegin;
5700a6ba4734SToby Isaac   if (!boundary) PetscFunctionReturn(0);
5701bcc5ffd9SToby Isaac   if (--((*boundary)->refct)) {
5702a6ba4734SToby Isaac     *boundary = NULL;
5703bcc5ffd9SToby Isaac     PetscFunctionReturn(0);
5704bcc5ffd9SToby Isaac   }
5705bcc5ffd9SToby Isaac   b = (*boundary)->next;
5706a6ba4734SToby Isaac   for (; b; b = next) {
5707a6ba4734SToby Isaac     next = b->next;
5708a6ba4734SToby Isaac     ierr = PetscFree(b->comps);CHKERRQ(ierr);
5709a6ba4734SToby Isaac     ierr = PetscFree(b->ids);CHKERRQ(ierr);
5710a6ba4734SToby Isaac     ierr = PetscFree(b->name);CHKERRQ(ierr);
5711a6ba4734SToby Isaac     ierr = PetscFree(b->labelname);CHKERRQ(ierr);
5712a6ba4734SToby Isaac     ierr = PetscFree(b);CHKERRQ(ierr);
5713a6ba4734SToby Isaac   }
5714bcc5ffd9SToby Isaac   ierr = PetscFree(*boundary);CHKERRQ(ierr);
5715a6ba4734SToby Isaac   PetscFunctionReturn(0);
5716a6ba4734SToby Isaac }
5717a6ba4734SToby Isaac 
5718a6ba4734SToby Isaac #undef __FUNCT__
5719a6ba4734SToby Isaac #define __FUNCT__ "DMCopyBoundary"
5720a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5721a6ba4734SToby Isaac {
5722a6ba4734SToby Isaac   DMBoundary     b;
5723a6ba4734SToby Isaac   PetscErrorCode ierr;
5724a6ba4734SToby Isaac 
5725a6ba4734SToby Isaac   PetscFunctionBegin;
5726bcc5ffd9SToby Isaac   ierr = DMBoundaryDestroy(&dmNew->boundary);CHKERRQ(ierr);
5727bcc5ffd9SToby Isaac   ierr = DMBoundaryDuplicate(dm->boundary, &dmNew->boundary);CHKERRQ(ierr);
5728bcc5ffd9SToby Isaac   for (b = dmNew->boundary->next; b; b = b->next) {
5729a6ba4734SToby Isaac     if (b->labelname) {
5730a6ba4734SToby Isaac       ierr = DMGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
5731a6ba4734SToby Isaac       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5732a6ba4734SToby Isaac     }
5733a6ba4734SToby Isaac   }
5734a6ba4734SToby Isaac   PetscFunctionReturn(0);
5735a6ba4734SToby Isaac }
5736a6ba4734SToby Isaac 
5737a6ba4734SToby Isaac #undef __FUNCT__
5738a6ba4734SToby Isaac #define __FUNCT__ "DMAddBoundary"
5739a6ba4734SToby Isaac /*@C
5740a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5741a6ba4734SToby Isaac 
5742a6ba4734SToby Isaac   Input Parameters:
5743a6ba4734SToby Isaac + dm          - The mesh object
5744a6ba4734SToby Isaac . isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5745a6ba4734SToby Isaac . name        - The BC name
5746a6ba4734SToby Isaac . labelname   - The label defining constrained points
5747a6ba4734SToby Isaac . field       - The field to constrain
5748a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5749a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5750a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5751a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5752a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5753a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5754a6ba4734SToby Isaac 
5755a6ba4734SToby Isaac   Options Database Keys:
5756a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5757a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5758a6ba4734SToby Isaac 
5759a6ba4734SToby Isaac   Level: developer
5760a6ba4734SToby Isaac 
5761a6ba4734SToby Isaac .seealso: DMGetBoundary()
5762a6ba4734SToby Isaac @*/
5763a6ba4734SToby 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)
5764a6ba4734SToby Isaac {
5765a6ba4734SToby Isaac   DMBoundary     b;
5766a6ba4734SToby Isaac   PetscErrorCode ierr;
5767a6ba4734SToby Isaac 
5768a6ba4734SToby Isaac   PetscFunctionBegin;
5769a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5770a6ba4734SToby Isaac   ierr = PetscNew(&b);CHKERRQ(ierr);
5771a6ba4734SToby Isaac   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
5772a6ba4734SToby Isaac   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
5773a6ba4734SToby Isaac   ierr = PetscMalloc1(numcomps, &b->comps);CHKERRQ(ierr);
5774a6ba4734SToby Isaac   if (numcomps) {ierr = PetscMemcpy(b->comps, comps, numcomps*sizeof(PetscInt));CHKERRQ(ierr);}
5775a6ba4734SToby Isaac   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
5776a6ba4734SToby Isaac   if (numids) {ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);}
5777a6ba4734SToby Isaac   if (b->labelname) {
5778a6ba4734SToby Isaac     ierr = DMGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
5779a6ba4734SToby Isaac     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5780a6ba4734SToby Isaac   }
5781a6ba4734SToby Isaac   b->essential       = isEssential;
5782a6ba4734SToby Isaac   b->field           = field;
5783a6ba4734SToby Isaac   b->numcomps        = numcomps;
5784a6ba4734SToby Isaac   b->func            = bcFunc;
5785a6ba4734SToby Isaac   b->numids          = numids;
5786a6ba4734SToby Isaac   b->ctx             = ctx;
5787bcc5ffd9SToby Isaac   b->next            = dm->boundary->next;
5788bcc5ffd9SToby Isaac   dm->boundary->next = b;
5789a6ba4734SToby Isaac   PetscFunctionReturn(0);
5790a6ba4734SToby Isaac }
5791a6ba4734SToby Isaac 
5792a6ba4734SToby Isaac #undef __FUNCT__
5793a6ba4734SToby Isaac #define __FUNCT__ "DMGetNumBoundary"
5794a6ba4734SToby Isaac /*@
5795a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5796a6ba4734SToby Isaac 
5797a6ba4734SToby Isaac   Input Parameters:
5798a6ba4734SToby Isaac . dm - The mesh object
5799a6ba4734SToby Isaac 
5800a6ba4734SToby Isaac   Output Parameters:
5801a6ba4734SToby Isaac . numBd - The number of BC
5802a6ba4734SToby Isaac 
5803a6ba4734SToby Isaac   Level: intermediate
5804a6ba4734SToby Isaac 
5805a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5806a6ba4734SToby Isaac @*/
5807a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5808a6ba4734SToby Isaac {
5809bcc5ffd9SToby Isaac   DMBoundary b = dm->boundary->next;
5810a6ba4734SToby Isaac 
5811a6ba4734SToby Isaac   PetscFunctionBegin;
5812a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5813a6ba4734SToby Isaac   PetscValidPointer(numBd, 2);
5814a6ba4734SToby Isaac   *numBd = 0;
5815a6ba4734SToby Isaac   while (b) {++(*numBd); b = b->next;}
5816a6ba4734SToby Isaac   PetscFunctionReturn(0);
5817a6ba4734SToby Isaac }
5818a6ba4734SToby Isaac 
5819a6ba4734SToby Isaac #undef __FUNCT__
5820a6ba4734SToby Isaac #define __FUNCT__ "DMGetBoundary"
5821a6ba4734SToby Isaac /*@C
5822a6ba4734SToby Isaac   DMGetBoundary - Add a boundary condition to the model
5823a6ba4734SToby Isaac 
5824a6ba4734SToby Isaac   Input Parameters:
5825a6ba4734SToby Isaac + dm          - The mesh object
5826a6ba4734SToby Isaac - bd          - The BC number
5827a6ba4734SToby Isaac 
5828a6ba4734SToby Isaac   Output Parameters:
5829a6ba4734SToby Isaac + isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5830a6ba4734SToby Isaac . name        - The BC name
5831a6ba4734SToby Isaac . labelname   - The label defining constrained points
5832a6ba4734SToby Isaac . field       - The field to constrain
5833a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5834a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5835a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5836a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5837a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5838a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5839a6ba4734SToby Isaac 
5840a6ba4734SToby Isaac   Options Database Keys:
5841a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5842a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5843a6ba4734SToby Isaac 
5844a6ba4734SToby Isaac   Level: developer
5845a6ba4734SToby Isaac 
5846a6ba4734SToby Isaac .seealso: DMAddBoundary()
5847a6ba4734SToby Isaac @*/
5848a6ba4734SToby 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)
5849a6ba4734SToby Isaac {
5850bcc5ffd9SToby Isaac   DMBoundary b    = dm->boundary->next;
5851a6ba4734SToby Isaac   PetscInt   n    = 0;
5852a6ba4734SToby Isaac 
5853a6ba4734SToby Isaac   PetscFunctionBegin;
5854a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5855a6ba4734SToby Isaac   while (b) {
5856a6ba4734SToby Isaac     if (n == bd) break;
5857a6ba4734SToby Isaac     b = b->next;
5858a6ba4734SToby Isaac     ++n;
5859a6ba4734SToby Isaac   }
5860a6ba4734SToby Isaac   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
5861a6ba4734SToby Isaac   if (isEssential) {
5862a6ba4734SToby Isaac     PetscValidPointer(isEssential, 3);
5863a6ba4734SToby Isaac     *isEssential = b->essential;
5864a6ba4734SToby Isaac   }
5865a6ba4734SToby Isaac   if (name) {
5866a6ba4734SToby Isaac     PetscValidPointer(name, 4);
5867a6ba4734SToby Isaac     *name = b->name;
5868a6ba4734SToby Isaac   }
5869a6ba4734SToby Isaac   if (labelname) {
5870a6ba4734SToby Isaac     PetscValidPointer(labelname, 5);
5871a6ba4734SToby Isaac     *labelname = b->labelname;
5872a6ba4734SToby Isaac   }
5873a6ba4734SToby Isaac   if (field) {
5874a6ba4734SToby Isaac     PetscValidPointer(field, 6);
5875a6ba4734SToby Isaac     *field = b->field;
5876a6ba4734SToby Isaac   }
5877a6ba4734SToby Isaac   if (numcomps) {
5878a6ba4734SToby Isaac     PetscValidPointer(numcomps, 7);
5879a6ba4734SToby Isaac     *numcomps = b->numcomps;
5880a6ba4734SToby Isaac   }
5881a6ba4734SToby Isaac   if (comps) {
5882a6ba4734SToby Isaac     PetscValidPointer(comps, 8);
5883a6ba4734SToby Isaac     *comps = b->comps;
5884a6ba4734SToby Isaac   }
5885a6ba4734SToby Isaac   if (func) {
5886a6ba4734SToby Isaac     PetscValidPointer(func, 9);
5887a6ba4734SToby Isaac     *func = b->func;
5888a6ba4734SToby Isaac   }
5889a6ba4734SToby Isaac   if (numids) {
5890a6ba4734SToby Isaac     PetscValidPointer(numids, 10);
5891a6ba4734SToby Isaac     *numids = b->numids;
5892a6ba4734SToby Isaac   }
5893a6ba4734SToby Isaac   if (ids) {
5894a6ba4734SToby Isaac     PetscValidPointer(ids, 11);
5895a6ba4734SToby Isaac     *ids = b->ids;
5896a6ba4734SToby Isaac   }
5897a6ba4734SToby Isaac   if (ctx) {
5898a6ba4734SToby Isaac     PetscValidPointer(ctx, 12);
5899a6ba4734SToby Isaac     *ctx = b->ctx;
5900a6ba4734SToby Isaac   }
5901a6ba4734SToby Isaac   PetscFunctionReturn(0);
5902a6ba4734SToby Isaac }
5903a6ba4734SToby Isaac 
5904a6ba4734SToby Isaac #undef __FUNCT__
5905a6ba4734SToby Isaac #define __FUNCT__ "DMIsBoundaryPoint"
5906a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
5907a6ba4734SToby Isaac {
5908bcc5ffd9SToby Isaac   DMBoundary     b    = dm->boundary->next;
5909a6ba4734SToby Isaac   PetscErrorCode ierr;
5910a6ba4734SToby Isaac 
5911a6ba4734SToby Isaac   PetscFunctionBegin;
5912a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5913a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
5914a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
5915a6ba4734SToby Isaac   while (b && !(*isBd)) {
5916a6ba4734SToby Isaac     if (b->label) {
5917a6ba4734SToby Isaac       PetscInt i;
5918a6ba4734SToby Isaac 
5919a6ba4734SToby Isaac       for (i = 0; i < b->numids && !(*isBd); ++i) {
5920a6ba4734SToby Isaac         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
5921a6ba4734SToby Isaac       }
5922a6ba4734SToby Isaac     }
5923a6ba4734SToby Isaac     b = b->next;
5924a6ba4734SToby Isaac   }
5925a6ba4734SToby Isaac   PetscFunctionReturn(0);
5926a6ba4734SToby Isaac }
59274d6f44ffSToby Isaac 
59284d6f44ffSToby Isaac #undef __FUNCT__
59294d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunction"
59304d6f44ffSToby Isaac /*@C
59314d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
59324d6f44ffSToby Isaac 
59334d6f44ffSToby Isaac   Input Parameters:
59344d6f44ffSToby Isaac + dm      - The DM
59350709b2feSToby Isaac . time    - The time
59364d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
59374d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
59384d6f44ffSToby Isaac - mode    - The insertion mode for values
59394d6f44ffSToby Isaac 
59404d6f44ffSToby Isaac   Output Parameter:
59414d6f44ffSToby Isaac . X - vector
59424d6f44ffSToby Isaac 
59434d6f44ffSToby Isaac    Calling sequence of func:
59440709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
59454d6f44ffSToby Isaac 
59464d6f44ffSToby Isaac +  dim - The spatial dimension
59474d6f44ffSToby Isaac .  x   - The coordinates
59484d6f44ffSToby Isaac .  Nf  - The number of fields
59494d6f44ffSToby Isaac .  u   - The output field values
59504d6f44ffSToby Isaac -  ctx - optional user-defined function context
59514d6f44ffSToby Isaac 
59524d6f44ffSToby Isaac   Level: developer
59534d6f44ffSToby Isaac 
59542716604bSToby Isaac .seealso: DMComputeL2Diff()
59554d6f44ffSToby Isaac @*/
59560709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
59574d6f44ffSToby Isaac {
59584d6f44ffSToby Isaac   Vec            localX;
59594d6f44ffSToby Isaac   PetscErrorCode ierr;
59604d6f44ffSToby Isaac 
59614d6f44ffSToby Isaac   PetscFunctionBegin;
59624d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59634d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
59640709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
59654d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
59664d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
59674d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
59684d6f44ffSToby Isaac   PetscFunctionReturn(0);
59694d6f44ffSToby Isaac }
59704d6f44ffSToby Isaac 
59714d6f44ffSToby Isaac #undef __FUNCT__
59724d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal"
59730709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
59744d6f44ffSToby Isaac {
59754d6f44ffSToby Isaac   PetscErrorCode ierr;
59764d6f44ffSToby Isaac 
59774d6f44ffSToby Isaac   PetscFunctionBegin;
59784d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59794d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
59804d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
59810709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
59824d6f44ffSToby Isaac   PetscFunctionReturn(0);
59834d6f44ffSToby Isaac }
59844d6f44ffSToby Isaac 
59854d6f44ffSToby Isaac #undef __FUNCT__
5986*bfc4295aSToby Isaac #define __FUNCT__ "DMProjectFieldLocal"
5987*bfc4295aSToby Isaac PetscErrorCode DMProjectFieldLocal(DM dm, Vec localU,
5988*bfc4295aSToby Isaac                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
5989*bfc4295aSToby Isaac                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
5990*bfc4295aSToby Isaac                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
5991*bfc4295aSToby Isaac                                                   PetscReal, const PetscReal[], PetscScalar[]),
5992*bfc4295aSToby Isaac                                    InsertMode mode, Vec localX)
5993*bfc4295aSToby Isaac {
5994*bfc4295aSToby Isaac   PetscErrorCode ierr;
5995*bfc4295aSToby Isaac 
5996*bfc4295aSToby Isaac   PetscFunctionBegin;
5997*bfc4295aSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5998*bfc4295aSToby Isaac   PetscValidHeaderSpecific(localU,VEC_CLASSID,2);
5999*bfc4295aSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
6000*bfc4295aSToby Isaac   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
6001*bfc4295aSToby Isaac   ierr = (dm->ops->projectfieldlocal) (dm, localU, funcs, mode, localX);CHKERRQ(ierr);
6002*bfc4295aSToby Isaac   PetscFunctionReturn(0);
6003*bfc4295aSToby Isaac }
6004*bfc4295aSToby Isaac 
6005*bfc4295aSToby Isaac #undef __FUNCT__
60064d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLabelLocal"
60070709b2feSToby 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)
60084d6f44ffSToby Isaac {
60094d6f44ffSToby Isaac   PetscErrorCode ierr;
60104d6f44ffSToby Isaac 
60114d6f44ffSToby Isaac   PetscFunctionBegin;
60124d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60134d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
60144d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
60150709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, funcs, ctxs, mode, localX);CHKERRQ(ierr);
60164d6f44ffSToby Isaac   PetscFunctionReturn(0);
60174d6f44ffSToby Isaac }
60182716604bSToby Isaac 
60192716604bSToby Isaac #undef __FUNCT__
60202716604bSToby Isaac #define __FUNCT__ "DMComputeL2Diff"
60212716604bSToby Isaac /*@C
60222716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
60232716604bSToby Isaac 
60242716604bSToby Isaac   Input Parameters:
60252716604bSToby Isaac + dm    - The DM
60260709b2feSToby Isaac . time  - The time
60272716604bSToby Isaac . funcs - The functions to evaluate for each field component
60282716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
60292716604bSToby Isaac - X     - The coefficient vector u_h
60302716604bSToby Isaac 
60312716604bSToby Isaac   Output Parameter:
60322716604bSToby Isaac . diff - The diff ||u - u_h||_2
60332716604bSToby Isaac 
60342716604bSToby Isaac   Level: developer
60352716604bSToby Isaac 
6036b698f381SToby Isaac .seealso: DMProjectFunction(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
60372716604bSToby Isaac @*/
60380709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
60392716604bSToby Isaac {
60402716604bSToby Isaac   PetscErrorCode ierr;
60412716604bSToby Isaac 
60422716604bSToby Isaac   PetscFunctionBegin;
60432716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6044b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
60452716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
60460709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
60472716604bSToby Isaac   PetscFunctionReturn(0);
60482716604bSToby Isaac }
6049b698f381SToby Isaac 
6050b698f381SToby Isaac #undef __FUNCT__
6051b698f381SToby Isaac #define __FUNCT__ "DMComputeL2GradientDiff"
6052b698f381SToby Isaac /*@C
6053b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6054b698f381SToby Isaac 
6055b698f381SToby Isaac   Input Parameters:
6056b698f381SToby Isaac + dm    - The DM
6057b698f381SToby Isaac , time  - The time
6058b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6059b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6060b698f381SToby Isaac . X     - The coefficient vector u_h
6061b698f381SToby Isaac - n     - The vector to project along
6062b698f381SToby Isaac 
6063b698f381SToby Isaac   Output Parameter:
6064b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6065b698f381SToby Isaac 
6066b698f381SToby Isaac   Level: developer
6067b698f381SToby Isaac 
6068b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6069b698f381SToby Isaac @*/
6070b698f381SToby 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)
6071b698f381SToby Isaac {
6072b698f381SToby Isaac   PetscErrorCode ierr;
6073b698f381SToby Isaac 
6074b698f381SToby Isaac   PetscFunctionBegin;
6075b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6076b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6077b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6078b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6079b698f381SToby Isaac   PetscFunctionReturn(0);
6080b698f381SToby Isaac }
6081b698f381SToby Isaac 
6082