xref: /petsc/src/dm/interface/dm.c (revision 4d6f44ff4594d45700d1dfd21f7232893717f324)
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);
74c58f1c22SToby Isaac   ierr = PetscCalloc1(1,&(v->labels));CHKERRQ(ierr);
75c58f1c22SToby Isaac   v->labels->refct = 1;
761411c6eeSJed Brown   *dm = v;
77a4121054SBarry Smith   PetscFunctionReturn(0);
78a4121054SBarry Smith }
79a4121054SBarry Smith 
80a4121054SBarry Smith #undef __FUNCT__
8138221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
8238221697SMatthew G. Knepley /*@
8338221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
8438221697SMatthew G. Knepley 
8538221697SMatthew G. Knepley   Collective on MPI_Comm
8638221697SMatthew G. Knepley 
8738221697SMatthew G. Knepley   Input Parameter:
8838221697SMatthew G. Knepley . dm - The original DM object
8938221697SMatthew G. Knepley 
9038221697SMatthew G. Knepley   Output Parameter:
9138221697SMatthew G. Knepley . newdm  - The new DM object
9238221697SMatthew G. Knepley 
9338221697SMatthew G. Knepley   Level: beginner
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley .keywords: DM, topology, create
9638221697SMatthew G. Knepley @*/
9738221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9838221697SMatthew G. Knepley {
9938221697SMatthew G. Knepley   PetscSF        sf;
10038221697SMatthew G. Knepley   Vec            coords;
10138221697SMatthew G. Knepley   void          *ctx;
1021de53e9aSMatthew G. Knepley   PetscInt       dim;
10338221697SMatthew G. Knepley   PetscErrorCode ierr;
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley   PetscFunctionBegin;
10638221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10738221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10838221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
109c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
110c58f1c22SToby Isaac   dm->labels->refct++;
111c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
112c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1131de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1141de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
11538221697SMatthew G. Knepley   if (dm->ops->clone) {
11638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
11738221697SMatthew G. Knepley   }
118cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
11938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
12038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
12138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
12238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
12338221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
12438221697SMatthew G. Knepley   if (coords) {
12538221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
12638221697SMatthew G. Knepley   } else {
12738221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
12838221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
12938221697SMatthew G. Knepley   }
130c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
131c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1325dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1335dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1345dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
135c6b900c6SMatthew G. Knepley   }
13638221697SMatthew G. Knepley   PetscFunctionReturn(0);
13738221697SMatthew G. Knepley }
13838221697SMatthew G. Knepley 
13938221697SMatthew G. Knepley #undef __FUNCT__
1409a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1419a42bb27SBarry Smith /*@C
142564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1439a42bb27SBarry Smith 
1448472ad0fSDave May    Logically Collective on DM
1459a42bb27SBarry Smith 
1469a42bb27SBarry Smith    Input Parameter:
1479a42bb27SBarry Smith +  da - initial distributed array
1488154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1499a42bb27SBarry Smith 
1509a42bb27SBarry Smith    Options Database:
151dd85299cSBarry Smith .   -dm_vec_type ctype
1529a42bb27SBarry Smith 
1539a42bb27SBarry Smith    Level: intermediate
1549a42bb27SBarry Smith 
1558472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1569a42bb27SBarry Smith @*/
15719fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1589a42bb27SBarry Smith {
1599a42bb27SBarry Smith   PetscErrorCode ierr;
1609a42bb27SBarry Smith 
1619a42bb27SBarry Smith   PetscFunctionBegin;
1629a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1639a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
16419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1659a42bb27SBarry Smith   PetscFunctionReturn(0);
1669a42bb27SBarry Smith }
1679a42bb27SBarry Smith 
1689a42bb27SBarry Smith #undef __FUNCT__
169c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
170c0dedaeaSBarry Smith /*@C
171c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
172c0dedaeaSBarry Smith 
1738472ad0fSDave May    Logically Collective on DM
174c0dedaeaSBarry Smith 
175c0dedaeaSBarry Smith    Input Parameter:
176c0dedaeaSBarry Smith .  da - initial distributed array
177c0dedaeaSBarry Smith 
178c0dedaeaSBarry Smith    Output Parameter:
179c0dedaeaSBarry Smith .  ctype - the vector type
180c0dedaeaSBarry Smith 
181c0dedaeaSBarry Smith    Level: intermediate
182c0dedaeaSBarry Smith 
1838472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
184c0dedaeaSBarry Smith @*/
185c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
186c0dedaeaSBarry Smith {
187c0dedaeaSBarry Smith   PetscFunctionBegin;
188c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
189c0dedaeaSBarry Smith   *ctype = da->vectype;
190c0dedaeaSBarry Smith   PetscFunctionReturn(0);
191c0dedaeaSBarry Smith }
192c0dedaeaSBarry Smith 
193c0dedaeaSBarry Smith #undef __FUNCT__
1945f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1955f1ad066SMatthew G Knepley /*@
19634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1975f1ad066SMatthew G Knepley 
1985f1ad066SMatthew G Knepley   Not collective
1995f1ad066SMatthew G Knepley 
2005f1ad066SMatthew G Knepley   Input Parameter:
2015f1ad066SMatthew G Knepley . v - The Vec
2025f1ad066SMatthew G Knepley 
2035f1ad066SMatthew G Knepley   Output Parameter:
2045f1ad066SMatthew G Knepley . dm - The DM
2055f1ad066SMatthew G Knepley 
2065f1ad066SMatthew G Knepley   Level: intermediate
2075f1ad066SMatthew G Knepley 
2085f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2095f1ad066SMatthew G Knepley @*/
2105f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2115f1ad066SMatthew G Knepley {
2125f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2135f1ad066SMatthew G Knepley 
2145f1ad066SMatthew G Knepley   PetscFunctionBegin;
2155f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2165f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2175f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2185f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2195f1ad066SMatthew G Knepley }
2205f1ad066SMatthew G Knepley 
2215f1ad066SMatthew G Knepley #undef __FUNCT__
2225f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2235f1ad066SMatthew G Knepley /*@
224d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2255f1ad066SMatthew G Knepley 
2265f1ad066SMatthew G Knepley   Not collective
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Input Parameters:
2295f1ad066SMatthew G Knepley + v - The Vec
2305f1ad066SMatthew G Knepley - dm - The DM
2315f1ad066SMatthew G Knepley 
232d9805387SMatthew G. Knepley   Note: This is NOT the same as DMCreateGlobalVector() since it does not change the view methods or perform other customization, but merely sets the DM member.
233d9805387SMatthew G. Knepley 
2345f1ad066SMatthew G Knepley   Level: intermediate
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2375f1ad066SMatthew G Knepley @*/
2385f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2395f1ad066SMatthew G Knepley {
2405f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2415f1ad066SMatthew G Knepley 
2425f1ad066SMatthew G Knepley   PetscFunctionBegin;
2435f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
244d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2455f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2465f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2475f1ad066SMatthew G Knepley }
2485f1ad066SMatthew G Knepley 
2495f1ad066SMatthew G Knepley #undef __FUNCT__
250521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
251521d9a4cSLisandro Dalcin /*@C
252521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
253521d9a4cSLisandro Dalcin 
254521d9a4cSLisandro Dalcin    Logically Collective on DM
255521d9a4cSLisandro Dalcin 
256521d9a4cSLisandro Dalcin    Input Parameter:
257521d9a4cSLisandro Dalcin +  dm - the DM context
258521d9a4cSLisandro Dalcin .  ctype - the matrix type
259521d9a4cSLisandro Dalcin 
260521d9a4cSLisandro Dalcin    Options Database:
261521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
262521d9a4cSLisandro Dalcin 
263521d9a4cSLisandro Dalcin    Level: intermediate
264521d9a4cSLisandro Dalcin 
265c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
266521d9a4cSLisandro Dalcin @*/
26719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
268521d9a4cSLisandro Dalcin {
269521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27088f0584fSBarry Smith 
271521d9a4cSLisandro Dalcin   PetscFunctionBegin;
272521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
273521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
27419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
275521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
276521d9a4cSLisandro Dalcin }
277521d9a4cSLisandro Dalcin 
278521d9a4cSLisandro Dalcin #undef __FUNCT__
279c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
280c0dedaeaSBarry Smith /*@C
281c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
282c0dedaeaSBarry Smith 
283c0dedaeaSBarry Smith    Logically Collective on DM
284c0dedaeaSBarry Smith 
285c0dedaeaSBarry Smith    Input Parameter:
286c0dedaeaSBarry Smith .  dm - the DM context
287c0dedaeaSBarry Smith 
288c0dedaeaSBarry Smith    Output Parameter:
289c0dedaeaSBarry Smith .  ctype - the matrix type
290c0dedaeaSBarry Smith 
291c0dedaeaSBarry Smith    Options Database:
292c0dedaeaSBarry Smith .   -dm_mat_type ctype
293c0dedaeaSBarry Smith 
294c0dedaeaSBarry Smith    Level: intermediate
295c0dedaeaSBarry Smith 
296c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
297c0dedaeaSBarry Smith @*/
298c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
299c0dedaeaSBarry Smith {
300c0dedaeaSBarry Smith   PetscFunctionBegin;
301c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
302c0dedaeaSBarry Smith   *ctype = dm->mattype;
303c0dedaeaSBarry Smith   PetscFunctionReturn(0);
304c0dedaeaSBarry Smith }
305c0dedaeaSBarry Smith 
306c0dedaeaSBarry Smith #undef __FUNCT__
307c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
308c688c046SMatthew G Knepley /*@
30934f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
310c688c046SMatthew G Knepley 
311c688c046SMatthew G Knepley   Not collective
312c688c046SMatthew G Knepley 
313c688c046SMatthew G Knepley   Input Parameter:
314c688c046SMatthew G Knepley . A - The Mat
315c688c046SMatthew G Knepley 
316c688c046SMatthew G Knepley   Output Parameter:
317c688c046SMatthew G Knepley . dm - The DM
318c688c046SMatthew G Knepley 
319c688c046SMatthew G Knepley   Level: intermediate
320c688c046SMatthew G Knepley 
321c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
322c688c046SMatthew G Knepley @*/
323c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
324c688c046SMatthew G Knepley {
325c688c046SMatthew G Knepley   PetscErrorCode ierr;
326c688c046SMatthew G Knepley 
327c688c046SMatthew G Knepley   PetscFunctionBegin;
328c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
329c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
330c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
331c688c046SMatthew G Knepley   PetscFunctionReturn(0);
332c688c046SMatthew G Knepley }
333c688c046SMatthew G Knepley 
334c688c046SMatthew G Knepley #undef __FUNCT__
335c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
336c688c046SMatthew G Knepley /*@
337c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
338c688c046SMatthew G Knepley 
339c688c046SMatthew G Knepley   Not collective
340c688c046SMatthew G Knepley 
341c688c046SMatthew G Knepley   Input Parameters:
342c688c046SMatthew G Knepley + A - The Mat
343c688c046SMatthew G Knepley - dm - The DM
344c688c046SMatthew G Knepley 
345c688c046SMatthew G Knepley   Level: intermediate
346c688c046SMatthew G Knepley 
347c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
348c688c046SMatthew G Knepley @*/
349c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
350c688c046SMatthew G Knepley {
351c688c046SMatthew G Knepley   PetscErrorCode ierr;
352c688c046SMatthew G Knepley 
353c688c046SMatthew G Knepley   PetscFunctionBegin;
354c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3558865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
356c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
357c688c046SMatthew G Knepley   PetscFunctionReturn(0);
358c688c046SMatthew G Knepley }
359c688c046SMatthew G Knepley 
360c688c046SMatthew G Knepley #undef __FUNCT__
3619a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3629a42bb27SBarry Smith /*@C
3639a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3646757b960SDave May    DM options in the database.
3659a42bb27SBarry Smith 
3668353ddbbSDave May    Logically Collective on DM
3679a42bb27SBarry Smith 
3689a42bb27SBarry Smith    Input Parameter:
3698353ddbbSDave May +  da - the DM context
3709a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3719a42bb27SBarry Smith 
3729a42bb27SBarry Smith    Notes:
3739a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3749a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3759a42bb27SBarry Smith 
3769a42bb27SBarry Smith    Level: advanced
3779a42bb27SBarry Smith 
3788353ddbbSDave May .keywords: DM, set, options, prefix, database
3799a42bb27SBarry Smith 
3809a42bb27SBarry Smith .seealso: DMSetFromOptions()
3819a42bb27SBarry Smith @*/
3827087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3839a42bb27SBarry Smith {
3849a42bb27SBarry Smith   PetscErrorCode ierr;
3859a42bb27SBarry Smith 
3869a42bb27SBarry Smith   PetscFunctionBegin;
3879a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3889a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
389691be533SLawrence Mitchell   if (dm->sf) {
390691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
391691be533SLawrence Mitchell   }
392691be533SLawrence Mitchell   if (dm->defaultSF) {
393691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
394691be533SLawrence Mitchell   }
3959a42bb27SBarry Smith   PetscFunctionReturn(0);
3969a42bb27SBarry Smith }
3979a42bb27SBarry Smith 
3989a42bb27SBarry Smith #undef __FUNCT__
39931697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
40031697293SDave May /*@C
40131697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
40231697293SDave May    DM options in the database.
40331697293SDave May 
40431697293SDave May    Logically Collective on DM
40531697293SDave May 
40631697293SDave May    Input Parameters:
40731697293SDave May +  dm - the DM context
40831697293SDave May -  prefix - the prefix string to prepend to all DM option requests
40931697293SDave May 
41031697293SDave May    Notes:
41131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
41231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
41331697293SDave May 
41431697293SDave May    Level: advanced
41531697293SDave May 
41631697293SDave May .keywords: DM, append, options, prefix, database
41731697293SDave May 
41831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
41931697293SDave May @*/
42031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
42131697293SDave May {
42231697293SDave May   PetscErrorCode ierr;
42331697293SDave May 
42431697293SDave May   PetscFunctionBegin;
42531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
42731697293SDave May   PetscFunctionReturn(0);
42831697293SDave May }
42931697293SDave May 
43031697293SDave May #undef __FUNCT__
43131697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
43231697293SDave May /*@C
43331697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
43431697293SDave May    DM options in the database.
43531697293SDave May 
43631697293SDave May    Not Collective
43731697293SDave May 
43831697293SDave May    Input Parameters:
43931697293SDave May .  dm - the DM context
44031697293SDave May 
44131697293SDave May    Output Parameters:
44231697293SDave May .  prefix - pointer to the prefix string used is returned
44331697293SDave May 
44431697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
44531697293SDave May    sufficient length to hold the prefix.
44631697293SDave May 
44731697293SDave May    Level: advanced
44831697293SDave May 
44931697293SDave May .keywords: DM, set, options, prefix, database
45031697293SDave May 
45131697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
45231697293SDave May @*/
45331697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
45431697293SDave May {
45531697293SDave May   PetscErrorCode ierr;
45631697293SDave May 
45731697293SDave May   PetscFunctionBegin;
45831697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45931697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
46031697293SDave May   PetscFunctionReturn(0);
46131697293SDave May }
46231697293SDave May 
46331697293SDave May #undef __FUNCT__
46488bdff64SToby Isaac #define __FUNCT__ "DMCountNonCyclicReferences"
46588bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
46688bdff64SToby Isaac {
46788bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
46888bdff64SToby Isaac   DMNamedVecLink nlink;
46988bdff64SToby Isaac   PetscErrorCode ierr;
47088bdff64SToby Isaac 
47188bdff64SToby Isaac   PetscFunctionBegin;
47288bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
47388bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
47488bdff64SToby Isaac     if (dm->localin[i])  refct--;
47588bdff64SToby Isaac     if (dm->globalin[i]) refct--;
47688bdff64SToby Isaac   }
47788bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
47888bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
47988bdff64SToby Isaac   if (dm->x) {
48088bdff64SToby Isaac     DM obj;
48188bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
48288bdff64SToby Isaac     if (obj == dm) refct--;
48388bdff64SToby Isaac   }
48488bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
48588bdff64SToby Isaac     refct--;
48688bdff64SToby Isaac     if (recurseCoarse) {
48788bdff64SToby Isaac       PetscInt coarseCount;
48888bdff64SToby Isaac 
48988bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
49088bdff64SToby Isaac       refct += coarseCount;
49188bdff64SToby Isaac     }
49288bdff64SToby Isaac   }
49388bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
49488bdff64SToby Isaac     refct--;
49588bdff64SToby Isaac     if (recurseFine) {
49688bdff64SToby Isaac       PetscInt fineCount;
49788bdff64SToby Isaac 
49888bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
49988bdff64SToby Isaac       refct += fineCount;
50088bdff64SToby Isaac     }
50188bdff64SToby Isaac   }
50288bdff64SToby Isaac   *ncrefct = refct;
50388bdff64SToby Isaac   PetscFunctionReturn(0);
50488bdff64SToby Isaac }
50588bdff64SToby Isaac 
506a6ba4734SToby Isaac static PetscErrorCode BoundaryDestroy(DMBoundary *boundary);
507a6ba4734SToby Isaac 
50888bdff64SToby Isaac #undef __FUNCT__
50947c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
51047c6ae99SBarry Smith /*@
5118472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
51247c6ae99SBarry Smith 
51347c6ae99SBarry Smith     Collective on DM
51447c6ae99SBarry Smith 
51547c6ae99SBarry Smith     Input Parameter:
51647c6ae99SBarry Smith .   dm - the DM object to destroy
51747c6ae99SBarry Smith 
51847c6ae99SBarry Smith     Level: developer
51947c6ae99SBarry Smith 
520e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
52147c6ae99SBarry Smith 
52247c6ae99SBarry Smith @*/
523fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
52447c6ae99SBarry Smith {
52588bdff64SToby Isaac   PetscInt       i, cnt;
526dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
52747c6ae99SBarry Smith   PetscErrorCode ierr;
52847c6ae99SBarry Smith 
52947c6ae99SBarry Smith   PetscFunctionBegin;
5306bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5316bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
53287e657c6SBarry Smith 
53388bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
53488bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
53588bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
53688bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
537732e2eb9SMatthew G Knepley   /*
538732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
539732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
540732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
541732e2eb9SMatthew G Knepley   */
5426bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5436bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
544732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5456bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5466bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
547732e2eb9SMatthew G Knepley   }
548f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5490298fd71SBarry Smith   (*dm)->namedglobal = NULL;
550f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5512348bcf4SPeter Brune     nnext = nlink->next;
5522348bcf4SPeter 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);
5532348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5542348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5552348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5562348bcf4SPeter Brune   }
557f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5580298fd71SBarry Smith   (*dm)->namedlocal = NULL;
559f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
560f490541aSPeter Brune     nnext = nlink->next;
561f490541aSPeter 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);
562f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
563f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
564f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
565f490541aSPeter Brune   }
5662348bcf4SPeter Brune 
567b17ce1afSJed Brown   /* Destroy the list of hooks */
568c833c3b5SJed Brown   {
569c833c3b5SJed Brown     DMCoarsenHookLink link,next;
570b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
571b17ce1afSJed Brown       next = link->next;
572b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
573b17ce1afSJed Brown     }
5740298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
575c833c3b5SJed Brown   }
576c833c3b5SJed Brown   {
577c833c3b5SJed Brown     DMRefineHookLink link,next;
578c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
579c833c3b5SJed Brown       next = link->next;
580c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
581c833c3b5SJed Brown     }
5820298fd71SBarry Smith     (*dm)->refinehook = NULL;
583c833c3b5SJed Brown   }
584be081cd6SPeter Brune   {
585be081cd6SPeter Brune     DMSubDomainHookLink link,next;
586be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
587be081cd6SPeter Brune       next = link->next;
588be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
589be081cd6SPeter Brune     }
5900298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
591be081cd6SPeter Brune   }
592baf369e7SPeter Brune   {
593baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
594baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
595baf369e7SPeter Brune       next = link->next;
596baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
597baf369e7SPeter Brune     }
5980298fd71SBarry Smith     (*dm)->gtolhook = NULL;
599baf369e7SPeter Brune   }
600d4d07f1eSToby Isaac   {
601d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
602d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
603d4d07f1eSToby Isaac       next = link->next;
604d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
605d4d07f1eSToby Isaac     }
606d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
607d4d07f1eSToby Isaac   }
608aa1993deSMatthew G Knepley   /* Destroy the work arrays */
609aa1993deSMatthew G Knepley   {
610aa1993deSMatthew G Knepley     DMWorkLink link,next;
611aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
612aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
613aa1993deSMatthew G Knepley       next = link->next;
614aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
615aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
616aa1993deSMatthew G Knepley     }
6170298fd71SBarry Smith     (*dm)->workin = NULL;
618aa1993deSMatthew G Knepley   }
619c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
620c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
621c58f1c22SToby Isaac 
622c58f1c22SToby Isaac     /* destroy the labels */
623c58f1c22SToby Isaac     while (next) {
624c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
625c58f1c22SToby Isaac 
626c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
627c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
628c58f1c22SToby Isaac       next = tmp;
629c58f1c22SToby Isaac     }
630c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
631c58f1c22SToby Isaac   }
632a6ba4734SToby Isaac   ierr = BoundaryDestroy(&(*dm)->boundary);CHKERRQ(ierr);
633b17ce1afSJed Brown 
63452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
63552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
63652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
63752536dc3SBarry Smith 
6381a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6391a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6401a266240SBarry Smith   }
64187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
64271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6434dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6446bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6456bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
646073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
64788ed4aceSMatthew G Knepley 
64888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
64988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6508b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
651fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
652fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
65388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
65488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6558e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
656af122d2aSMatthew G Knepley 
65788bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
65888bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
65988bdff64SToby Isaac   }
660a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
66188bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
66288bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
66388bdff64SToby Isaac   }
66488bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
6656636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6666636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6676636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6685dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6696636e97aSMatthew G Knepley 
6702764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
67114f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
672e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
673e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
674732e2eb9SMatthew G Knepley 
6756bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
676435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
677732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
67847c6ae99SBarry Smith   PetscFunctionReturn(0);
67947c6ae99SBarry Smith }
68047c6ae99SBarry Smith 
68147c6ae99SBarry Smith #undef __FUNCT__
682d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
683d7bf68aeSBarry Smith /*@
684d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
685d7bf68aeSBarry Smith 
686d7bf68aeSBarry Smith     Collective on DM
687d7bf68aeSBarry Smith 
688d7bf68aeSBarry Smith     Input Parameter:
689d7bf68aeSBarry Smith .   dm - the DM object to setup
690d7bf68aeSBarry Smith 
691d7bf68aeSBarry Smith     Level: developer
692d7bf68aeSBarry Smith 
693e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
694d7bf68aeSBarry Smith 
695d7bf68aeSBarry Smith @*/
6967087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
697d7bf68aeSBarry Smith {
698d7bf68aeSBarry Smith   PetscErrorCode ierr;
699d7bf68aeSBarry Smith 
700d7bf68aeSBarry Smith   PetscFunctionBegin;
701171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7028387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
703d7bf68aeSBarry Smith   if (dm->ops->setup) {
704d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
705d7bf68aeSBarry Smith   }
7068387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
707d7bf68aeSBarry Smith   PetscFunctionReturn(0);
708d7bf68aeSBarry Smith }
709d7bf68aeSBarry Smith 
710d7bf68aeSBarry Smith #undef __FUNCT__
711d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
712d7bf68aeSBarry Smith /*@
713d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
714d7bf68aeSBarry Smith 
715d7bf68aeSBarry Smith     Collective on DM
716d7bf68aeSBarry Smith 
717d7bf68aeSBarry Smith     Input Parameter:
718d7bf68aeSBarry Smith .   dm - the DM object to set options for
719d7bf68aeSBarry Smith 
720732e2eb9SMatthew G Knepley     Options Database:
7214164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7224164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7234164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7244164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
725732e2eb9SMatthew G Knepley 
726d7bf68aeSBarry Smith     Level: developer
727d7bf68aeSBarry Smith 
728e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
729d7bf68aeSBarry Smith 
730d7bf68aeSBarry Smith @*/
7317087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
732d7bf68aeSBarry Smith {
7337781c08eSBarry Smith   char           typeName[256];
734ca266f36SBarry Smith   PetscBool      flg;
735d7bf68aeSBarry Smith   PetscErrorCode ierr;
736d7bf68aeSBarry Smith 
737d7bf68aeSBarry Smith   PetscFunctionBegin;
738171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
739691be533SLawrence Mitchell   if (dm->sf) {
740691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
741691be533SLawrence Mitchell   }
742691be533SLawrence Mitchell   if (dm->defaultSF) {
743691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
744691be533SLawrence Mitchell   }
7453194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7460298fd71SBarry 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);
747a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
748f9ba7244SBarry Smith   if (flg) {
749f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
750f9ba7244SBarry Smith   }
751a264d7a6SBarry 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);
752073dac72SJed Brown   if (flg) {
753521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
754073dac72SJed Brown   }
7550298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
756f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
757e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
758f9ba7244SBarry Smith   }
759f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
760f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
76182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
762d7bf68aeSBarry Smith   PetscFunctionReturn(0);
763d7bf68aeSBarry Smith }
764d7bf68aeSBarry Smith 
765d7bf68aeSBarry Smith #undef __FUNCT__
76647c6ae99SBarry Smith #define __FUNCT__ "DMView"
767fc9bc008SSatish Balay /*@C
768224748a4SBarry Smith     DMView - Views a DM
76947c6ae99SBarry Smith 
77047c6ae99SBarry Smith     Collective on DM
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith     Input Parameter:
77347c6ae99SBarry Smith +   dm - the DM object to view
77447c6ae99SBarry Smith -   v - the viewer
77547c6ae99SBarry Smith 
776224748a4SBarry Smith     Level: beginner
77747c6ae99SBarry Smith 
778e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
77947c6ae99SBarry Smith 
78047c6ae99SBarry Smith @*/
7817087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
78247c6ae99SBarry Smith {
78347c6ae99SBarry Smith   PetscErrorCode ierr;
78432c0f0efSBarry Smith   PetscBool      isbinary;
78547c6ae99SBarry Smith 
78647c6ae99SBarry Smith   PetscFunctionBegin;
787171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7883014e516SBarry Smith   if (!v) {
789ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
7903014e516SBarry Smith   }
79198c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
79232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
79332c0f0efSBarry Smith   if (isbinary) {
79455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
79532c0f0efSBarry Smith     char     type[256];
79632c0f0efSBarry Smith 
79732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
79832c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
79932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
80032c0f0efSBarry Smith   }
8010c010503SBarry Smith   if (dm->ops->view) {
8020c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
80347c6ae99SBarry Smith   }
80447c6ae99SBarry Smith   PetscFunctionReturn(0);
80547c6ae99SBarry Smith }
80647c6ae99SBarry Smith 
80747c6ae99SBarry Smith #undef __FUNCT__
80847c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
80947c6ae99SBarry Smith /*@
8108472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
81147c6ae99SBarry Smith 
81247c6ae99SBarry Smith     Collective on DM
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith     Input Parameter:
81547c6ae99SBarry Smith .   dm - the DM object
81647c6ae99SBarry Smith 
81747c6ae99SBarry Smith     Output Parameter:
81847c6ae99SBarry Smith .   vec - the global vector
81947c6ae99SBarry Smith 
820073dac72SJed Brown     Level: beginner
82147c6ae99SBarry Smith 
822e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
82347c6ae99SBarry Smith 
82447c6ae99SBarry Smith @*/
8257087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
82647c6ae99SBarry Smith {
82747c6ae99SBarry Smith   PetscErrorCode ierr;
82847c6ae99SBarry Smith 
82947c6ae99SBarry Smith   PetscFunctionBegin;
830171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
83247c6ae99SBarry Smith   PetscFunctionReturn(0);
83347c6ae99SBarry Smith }
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith #undef __FUNCT__
83647c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
83747c6ae99SBarry Smith /*@
8388472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
83947c6ae99SBarry Smith 
84047c6ae99SBarry Smith     Not Collective
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith     Input Parameter:
84347c6ae99SBarry Smith .   dm - the DM object
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith     Output Parameter:
84647c6ae99SBarry Smith .   vec - the local vector
84747c6ae99SBarry Smith 
848073dac72SJed Brown     Level: beginner
84947c6ae99SBarry Smith 
850e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith @*/
8537087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
85447c6ae99SBarry Smith {
85547c6ae99SBarry Smith   PetscErrorCode ierr;
85647c6ae99SBarry Smith 
85747c6ae99SBarry Smith   PetscFunctionBegin;
858171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85947c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
86047c6ae99SBarry Smith   PetscFunctionReturn(0);
86147c6ae99SBarry Smith }
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith #undef __FUNCT__
8641411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
8651411c6eeSJed Brown /*@
8661411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8671411c6eeSJed Brown 
8681411c6eeSJed Brown    Collective on DM
8691411c6eeSJed Brown 
8701411c6eeSJed Brown    Input Parameter:
8711411c6eeSJed Brown .  dm - the DM that provides the mapping
8721411c6eeSJed Brown 
8731411c6eeSJed Brown    Output Parameter:
8741411c6eeSJed Brown .  ltog - the mapping
8751411c6eeSJed Brown 
8761411c6eeSJed Brown    Level: intermediate
8771411c6eeSJed Brown 
8781411c6eeSJed Brown    Notes:
8791411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8801411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8811411c6eeSJed Brown 
882fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8831411c6eeSJed Brown @*/
8847087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8851411c6eeSJed Brown {
8861411c6eeSJed Brown   PetscErrorCode ierr;
8871411c6eeSJed Brown 
8881411c6eeSJed Brown   PetscFunctionBegin;
8891411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8901411c6eeSJed Brown   PetscValidPointer(ltog,2);
8911411c6eeSJed Brown   if (!dm->ltogmap) {
89237d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
89337d0c07bSMatthew G Knepley 
89437d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
89537d0c07bSMatthew G Knepley     if (section) {
89637d0c07bSMatthew G Knepley       PetscInt *ltog;
89737d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
89837d0c07bSMatthew G Knepley 
89937d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
90037d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
90137d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
902785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
90337d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
90437d0c07bSMatthew G Knepley         PetscInt dof, off, c;
90537d0c07bSMatthew G Knepley 
90637d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
90737d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
90837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
90937d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
91037d0c07bSMatthew G Knepley           ltog[l] = off+c;
91137d0c07bSMatthew G Knepley         }
91237d0c07bSMatthew G Knepley       }
913f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9143bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
91537d0c07bSMatthew G Knepley     } else {
916184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
917184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9181411c6eeSJed Brown     }
91937d0c07bSMatthew G Knepley   }
9201411c6eeSJed Brown   *ltog = dm->ltogmap;
9211411c6eeSJed Brown   PetscFunctionReturn(0);
9221411c6eeSJed Brown }
9231411c6eeSJed Brown 
9241411c6eeSJed Brown #undef __FUNCT__
9251411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
9261411c6eeSJed Brown /*@
9271411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9281411c6eeSJed Brown 
9291411c6eeSJed Brown    Not Collective
9301411c6eeSJed Brown 
9311411c6eeSJed Brown    Input Parameter:
9321411c6eeSJed Brown .  dm - the DM with block structure
9331411c6eeSJed Brown 
9341411c6eeSJed Brown    Output Parameter:
9351411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9361411c6eeSJed Brown 
9371411c6eeSJed Brown    Level: intermediate
9381411c6eeSJed Brown 
939fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9401411c6eeSJed Brown @*/
9417087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9421411c6eeSJed Brown {
9431411c6eeSJed Brown   PetscFunctionBegin;
9441411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9451411c6eeSJed Brown   PetscValidPointer(bs,2);
9461411c6eeSJed 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");
9471411c6eeSJed Brown   *bs = dm->bs;
9481411c6eeSJed Brown   PetscFunctionReturn(0);
9491411c6eeSJed Brown }
9501411c6eeSJed Brown 
9511411c6eeSJed Brown #undef __FUNCT__
952e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
95347c6ae99SBarry Smith /*@
9548472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
95547c6ae99SBarry Smith 
95647c6ae99SBarry Smith     Collective on DM
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith     Input Parameter:
95947c6ae99SBarry Smith +   dm1 - the DM object
96047c6ae99SBarry Smith -   dm2 - the second, finer DM object
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith     Output Parameter:
96347c6ae99SBarry Smith +  mat - the interpolation
96447c6ae99SBarry Smith -  vec - the scaling (optional)
96547c6ae99SBarry Smith 
96647c6ae99SBarry Smith     Level: developer
96747c6ae99SBarry Smith 
96885afcc9aSBarry 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
96985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
970d52bd9f3SBarry Smith 
9711f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
972d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
97385afcc9aSBarry Smith 
97485afcc9aSBarry Smith 
975e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith @*/
978e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
97947c6ae99SBarry Smith {
98047c6ae99SBarry Smith   PetscErrorCode ierr;
98147c6ae99SBarry Smith 
98247c6ae99SBarry Smith   PetscFunctionBegin;
983171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
984171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
985cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
98625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
987cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
98847c6ae99SBarry Smith   PetscFunctionReturn(0);
98947c6ae99SBarry Smith }
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith #undef __FUNCT__
992e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
99347c6ae99SBarry Smith /*@
9948472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
99547c6ae99SBarry Smith 
99647c6ae99SBarry Smith     Collective on DM
99747c6ae99SBarry Smith 
99847c6ae99SBarry Smith     Input Parameter:
99947c6ae99SBarry Smith +   dm1 - the DM object
100047c6ae99SBarry Smith -   dm2 - the second, finer DM object
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith     Output Parameter:
10036dbf9973SLawrence Mitchell .   mat - the injection
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith     Level: developer
100647c6ae99SBarry Smith 
100785afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
100885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
100985afcc9aSBarry Smith 
1010e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
101147c6ae99SBarry Smith 
101247c6ae99SBarry Smith @*/
10136dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
101447c6ae99SBarry Smith {
101547c6ae99SBarry Smith   PetscErrorCode ierr;
101647c6ae99SBarry Smith 
101747c6ae99SBarry Smith   PetscFunctionBegin;
1018171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1019171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10206dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
102147c6ae99SBarry Smith   PetscFunctionReturn(0);
102247c6ae99SBarry Smith }
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith #undef __FUNCT__
1025e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
1026b412c318SBarry Smith /*@
1027b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith     Collective on DM
103047c6ae99SBarry Smith 
103147c6ae99SBarry Smith     Input Parameter:
103247c6ae99SBarry Smith +   dm - the DM object
1033b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith     Output Parameter:
103647c6ae99SBarry Smith .   coloring - the coloring
103747c6ae99SBarry Smith 
103847c6ae99SBarry Smith     Level: developer
103947c6ae99SBarry Smith 
1040b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
104147c6ae99SBarry Smith 
1042aab9d709SJed Brown @*/
1043b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
104447c6ae99SBarry Smith {
104547c6ae99SBarry Smith   PetscErrorCode ierr;
104647c6ae99SBarry Smith 
104747c6ae99SBarry Smith   PetscFunctionBegin;
1048171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1049ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1050b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
105147c6ae99SBarry Smith   PetscFunctionReturn(0);
105247c6ae99SBarry Smith }
105347c6ae99SBarry Smith 
105447c6ae99SBarry Smith #undef __FUNCT__
1055950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1056b412c318SBarry Smith /*@
10578472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
105847c6ae99SBarry Smith 
105947c6ae99SBarry Smith     Collective on DM
106047c6ae99SBarry Smith 
106147c6ae99SBarry Smith     Input Parameter:
1062b412c318SBarry Smith .   dm - the DM object
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith     Output Parameter:
106547c6ae99SBarry Smith .   mat - the empty Jacobian
106647c6ae99SBarry Smith 
1067073dac72SJed Brown     Level: beginner
106847c6ae99SBarry Smith 
106994013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
107094013140SBarry Smith        do not need to do it yourself.
107194013140SBarry Smith 
107294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1073aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
107494013140SBarry Smith 
107594013140SBarry 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
107694013140SBarry Smith        internally by PETSc.
107794013140SBarry Smith 
107894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1079aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
108094013140SBarry Smith 
1081b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
108247c6ae99SBarry Smith 
1083aab9d709SJed Brown @*/
1084b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
108547c6ae99SBarry Smith {
108647c6ae99SBarry Smith   PetscErrorCode ierr;
108747c6ae99SBarry Smith 
108847c6ae99SBarry Smith   PetscFunctionBegin;
1089171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1090607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1091c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1092c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1093b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
109447c6ae99SBarry Smith   PetscFunctionReturn(0);
109547c6ae99SBarry Smith }
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith #undef __FUNCT__
1098732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1099732e2eb9SMatthew G Knepley /*@
1100950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1101732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1102732e2eb9SMatthew G Knepley 
11038472ad0fSDave May   Logically Collective on DM
1104732e2eb9SMatthew G Knepley 
1105732e2eb9SMatthew G Knepley   Input Parameter:
1106732e2eb9SMatthew G Knepley + dm - the DM
1107732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1108732e2eb9SMatthew G Knepley 
1109732e2eb9SMatthew G Knepley   Level: developer
1110950540a4SJed Brown .seealso DMCreateMatrix()
1111732e2eb9SMatthew G Knepley @*/
1112732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1113732e2eb9SMatthew G Knepley {
1114732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1115732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1116732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1117732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1118732e2eb9SMatthew G Knepley }
1119732e2eb9SMatthew G Knepley 
1120732e2eb9SMatthew G Knepley #undef __FUNCT__
1121a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1122a89ea682SMatthew G Knepley /*@C
1123aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1124a89ea682SMatthew G Knepley 
1125a89ea682SMatthew G Knepley   Not Collective
1126a89ea682SMatthew G Knepley 
1127a89ea682SMatthew G Knepley   Input Parameters:
1128a89ea682SMatthew G Knepley + dm - the DM object
1129aa1993deSMatthew G Knepley . count - The minium size
1130aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1131a89ea682SMatthew G Knepley 
1132a89ea682SMatthew G Knepley   Output Parameter:
1133a89ea682SMatthew G Knepley . array - the work array
1134a89ea682SMatthew G Knepley 
1135a89ea682SMatthew G Knepley   Level: developer
1136a89ea682SMatthew G Knepley 
1137a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1138a89ea682SMatthew G Knepley @*/
1139aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1140a89ea682SMatthew G Knepley {
1141a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1142aa1993deSMatthew G Knepley   DMWorkLink     link;
1143854ce69bSBarry Smith   size_t         dsize;
1144a89ea682SMatthew G Knepley 
1145a89ea682SMatthew G Knepley   PetscFunctionBegin;
1146a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1147aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1148aa1993deSMatthew G Knepley   if (dm->workin) {
1149aa1993deSMatthew G Knepley     link       = dm->workin;
1150aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1151aa1993deSMatthew G Knepley   } else {
1152b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1153a89ea682SMatthew G Knepley   }
1154854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1155854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1156aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1157854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1158854ce69bSBarry Smith     link->bytes = dsize*count;
1159aa1993deSMatthew G Knepley   }
1160aa1993deSMatthew G Knepley   link->next   = dm->workout;
1161aa1993deSMatthew G Knepley   dm->workout  = link;
1162aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1163a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1164a89ea682SMatthew G Knepley }
1165a89ea682SMatthew G Knepley 
1166aa1993deSMatthew G Knepley #undef __FUNCT__
1167aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1168aa1993deSMatthew G Knepley /*@C
1169aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1170aa1993deSMatthew G Knepley 
1171aa1993deSMatthew G Knepley   Not Collective
1172aa1993deSMatthew G Knepley 
1173aa1993deSMatthew G Knepley   Input Parameters:
1174aa1993deSMatthew G Knepley + dm - the DM object
1175aa1993deSMatthew G Knepley . count - The minium size
1176aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1177aa1993deSMatthew G Knepley 
1178aa1993deSMatthew G Knepley   Output Parameter:
1179aa1993deSMatthew G Knepley . array - the work array
1180aa1993deSMatthew G Knepley 
1181aa1993deSMatthew G Knepley   Level: developer
1182aa1993deSMatthew G Knepley 
1183aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1184aa1993deSMatthew G Knepley @*/
1185aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1186aa1993deSMatthew G Knepley {
1187aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1188aa1993deSMatthew G Knepley 
1189aa1993deSMatthew G Knepley   PetscFunctionBegin;
1190aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1191aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1192aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1193aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1194aa1993deSMatthew G Knepley       *p           = link->next;
1195aa1993deSMatthew G Knepley       link->next   = dm->workin;
1196aa1993deSMatthew G Knepley       dm->workin   = link;
11970298fd71SBarry Smith       *(void**)mem = NULL;
1198aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1199aa1993deSMatthew G Knepley     }
1200aa1993deSMatthew G Knepley   }
1201aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1202aa1993deSMatthew G Knepley }
1203e7c4fc90SDmitry Karpeev 
1204e7c4fc90SDmitry Karpeev #undef __FUNCT__
1205435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1206435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1207435a35e8SMatthew G Knepley {
1208435a35e8SMatthew G Knepley   PetscFunctionBegin;
1209435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
121082f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1211435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1212435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1213435a35e8SMatthew G Knepley }
1214435a35e8SMatthew G Knepley 
1215435a35e8SMatthew G Knepley #undef __FUNCT__
12164d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
12174f3b5142SJed Brown /*@C
12184d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12194d343eeaSMatthew G Knepley 
12204d343eeaSMatthew G Knepley   Not collective
12214d343eeaSMatthew G Knepley 
12224d343eeaSMatthew G Knepley   Input Parameter:
12234d343eeaSMatthew G Knepley . dm - the DM object
12244d343eeaSMatthew G Knepley 
12254d343eeaSMatthew G Knepley   Output Parameters:
12260298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
12270298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
12280298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
12294d343eeaSMatthew G Knepley 
12304d343eeaSMatthew G Knepley   Level: intermediate
12314d343eeaSMatthew G Knepley 
123221c9b008SJed Brown   Notes:
123321c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
123421c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
123521c9b008SJed Brown   PetscFree().
123621c9b008SJed Brown 
12374d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
12384d343eeaSMatthew G Knepley @*/
123937d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
12404d343eeaSMatthew G Knepley {
124137d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
12424d343eeaSMatthew G Knepley   PetscErrorCode ierr;
12434d343eeaSMatthew G Knepley 
12444d343eeaSMatthew G Knepley   PetscFunctionBegin;
12454d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
124669ca1f37SDmitry Karpeev   if (numFields) {
124769ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
124869ca1f37SDmitry Karpeev     *numFields = 0;
124969ca1f37SDmitry Karpeev   }
125037d0c07bSMatthew G Knepley   if (fieldNames) {
125137d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
12520298fd71SBarry Smith     *fieldNames = NULL;
125369ca1f37SDmitry Karpeev   }
125469ca1f37SDmitry Karpeev   if (fields) {
125569ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
12560298fd71SBarry Smith     *fields = NULL;
125769ca1f37SDmitry Karpeev   }
125837d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
125937d0c07bSMatthew G Knepley   if (section) {
126037d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
126137d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
126237d0c07bSMatthew G Knepley 
126337d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
126437d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1265dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
126637d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
126737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
126837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
126937d0c07bSMatthew G Knepley     }
127037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
127137d0c07bSMatthew G Knepley       PetscInt gdof;
127237d0c07bSMatthew G Knepley 
127337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
127437d0c07bSMatthew G Knepley       if (gdof > 0) {
127537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
127637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
127737d0c07bSMatthew G Knepley 
127837d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
127937d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
128037d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
128137d0c07bSMatthew G Knepley         }
128237d0c07bSMatthew G Knepley       }
128337d0c07bSMatthew G Knepley     }
128437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1285785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
128637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
128737d0c07bSMatthew G Knepley     }
128837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
128937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
129037d0c07bSMatthew G Knepley 
129137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
129237d0c07bSMatthew G Knepley       if (gdof > 0) {
129337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
129437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
129537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
129637d0c07bSMatthew G Knepley 
129737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
129837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
129937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
130037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
130137d0c07bSMatthew G Knepley           }
130237d0c07bSMatthew G Knepley         }
130337d0c07bSMatthew G Knepley       }
130437d0c07bSMatthew G Knepley     }
13058865f1eaSKarl Rupp     if (numFields) *numFields = nF;
130637d0c07bSMatthew G Knepley     if (fieldNames) {
1307785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
130837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
130937d0c07bSMatthew G Knepley         const char *fieldName;
131037d0c07bSMatthew G Knepley 
131137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
131237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
131337d0c07bSMatthew G Knepley       }
131437d0c07bSMatthew G Knepley     }
131537d0c07bSMatthew G Knepley     if (fields) {
1316785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
131737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
131882f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
131937d0c07bSMatthew G Knepley       }
132037d0c07bSMatthew G Knepley     }
132137d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13228865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
13238865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
132469ca1f37SDmitry Karpeev   }
13254d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
13264d343eeaSMatthew G Knepley }
13274d343eeaSMatthew G Knepley 
132816621825SDmitry Karpeev 
132916621825SDmitry Karpeev #undef __FUNCT__
133016621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
133116621825SDmitry Karpeev /*@C
133216621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
133316621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
133416621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1335e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1336e7c4fc90SDmitry Karpeev 
1337e7c4fc90SDmitry Karpeev   Not collective
1338e7c4fc90SDmitry Karpeev 
1339e7c4fc90SDmitry Karpeev   Input Parameter:
1340e7c4fc90SDmitry Karpeev . dm - the DM object
1341e7c4fc90SDmitry Karpeev 
1342e7c4fc90SDmitry Karpeev   Output Parameters:
13430298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
13440298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
13450298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
13460298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1347e7c4fc90SDmitry Karpeev 
1348e7c4fc90SDmitry Karpeev   Level: intermediate
1349e7c4fc90SDmitry Karpeev 
1350e7c4fc90SDmitry Karpeev   Notes:
1351e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1352e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1353e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1354e7c4fc90SDmitry Karpeev 
1355e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1356e7c4fc90SDmitry Karpeev @*/
135716621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1358e7c4fc90SDmitry Karpeev {
1359e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1360e7c4fc90SDmitry Karpeev 
1361e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1362e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13638865f1eaSKarl Rupp   if (len) {
13648865f1eaSKarl Rupp     PetscValidPointer(len,2);
13658865f1eaSKarl Rupp     *len = 0;
13668865f1eaSKarl Rupp   }
13678865f1eaSKarl Rupp   if (namelist) {
13688865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
13698865f1eaSKarl Rupp     *namelist = 0;
13708865f1eaSKarl Rupp   }
13718865f1eaSKarl Rupp   if (islist) {
13728865f1eaSKarl Rupp     PetscValidPointer(islist,4);
13738865f1eaSKarl Rupp     *islist = 0;
13748865f1eaSKarl Rupp   }
13758865f1eaSKarl Rupp   if (dmlist) {
13768865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
13778865f1eaSKarl Rupp     *dmlist = 0;
13788865f1eaSKarl Rupp   }
1379f3f0edfdSDmitry Karpeev   /*
1380f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1381f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1382f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1383f3f0edfdSDmitry Karpeev    */
1384ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
138516621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1386435a35e8SMatthew G Knepley     PetscSection section;
1387435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1388435a35e8SMatthew G Knepley 
1389435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1390435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1391435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1392435a35e8SMatthew G Knepley       *len = numFields;
139303dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
139403dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
139503dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1396435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1397435a35e8SMatthew G Knepley         const char *fieldName;
1398435a35e8SMatthew G Knepley 
139903dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
140003dc3394SMatthew G. Knepley         if (namelist) {
1401435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1402435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1403435a35e8SMatthew G Knepley         }
140403dc3394SMatthew G. Knepley       }
1405435a35e8SMatthew G Knepley     } else {
140669ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1407e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14080298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1409e7c4fc90SDmitry Karpeev     }
14108865f1eaSKarl Rupp   } else {
141116621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
141216621825SDmitry Karpeev   }
141316621825SDmitry Karpeev   PetscFunctionReturn(0);
141416621825SDmitry Karpeev }
141516621825SDmitry Karpeev 
141616621825SDmitry Karpeev #undef __FUNCT__
1417435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1418564cec59SMatthew G. Knepley /*@
1419435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1420435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1421435a35e8SMatthew G Knepley 
1422435a35e8SMatthew G Knepley   Not collective
1423435a35e8SMatthew G Knepley 
1424435a35e8SMatthew G Knepley   Input Parameters:
1425435a35e8SMatthew G Knepley + dm - the DM object
1426435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
14270298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1428435a35e8SMatthew G Knepley 
1429435a35e8SMatthew G Knepley   Output Parameters:
1430435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1431435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1432435a35e8SMatthew G Knepley 
1433435a35e8SMatthew G Knepley   Level: intermediate
1434435a35e8SMatthew G Knepley 
1435435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1436435a35e8SMatthew G Knepley @*/
1437435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1438435a35e8SMatthew G Knepley {
1439435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1440435a35e8SMatthew G Knepley 
1441435a35e8SMatthew G Knepley   PetscFunctionBegin;
1442435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1443435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
14448865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
14458865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1446435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1447435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
144882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1449435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1450435a35e8SMatthew G Knepley }
1451435a35e8SMatthew G Knepley 
145216621825SDmitry Karpeev 
145316621825SDmitry Karpeev #undef __FUNCT__
145416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
145516621825SDmitry Karpeev /*@C
14568d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
14578d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
14588d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
14598d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
14608d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
146116621825SDmitry Karpeev 
146216621825SDmitry Karpeev   Not collective
146316621825SDmitry Karpeev 
146416621825SDmitry Karpeev   Input Parameter:
146516621825SDmitry Karpeev . dm - the DM object
146616621825SDmitry Karpeev 
146716621825SDmitry Karpeev   Output Parameters:
14680298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
14690298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
14700298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
14710298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
14720298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
147316621825SDmitry Karpeev 
147416621825SDmitry Karpeev   Level: intermediate
147516621825SDmitry Karpeev 
147616621825SDmitry Karpeev   Notes:
147716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
147816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
147916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
148016621825SDmitry Karpeev 
14818d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
148216621825SDmitry Karpeev @*/
14838d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
148416621825SDmitry Karpeev {
148516621825SDmitry Karpeev   PetscErrorCode      ierr;
1486be081cd6SPeter Brune   DMSubDomainHookLink link;
1487be081cd6SPeter Brune   PetscInt            i,l;
148816621825SDmitry Karpeev 
148916621825SDmitry Karpeev   PetscFunctionBegin;
149016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
149114a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14920298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14930298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14940298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14950298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1496f3f0edfdSDmitry Karpeev   /*
1497f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1498f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1499f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1500f3f0edfdSDmitry Karpeev    */
1501ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
150216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1503be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
150414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
150514a18fd3SPeter Brune     if (dmlist) {
1506be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1507be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1508be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1509be081cd6SPeter Brune         }
1510d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1511e7c4fc90SDmitry Karpeev       }
151214a18fd3SPeter Brune     }
151314a18fd3SPeter Brune     if (len) *len = l;
151414a18fd3SPeter Brune   }
1515e30e807fSPeter Brune   PetscFunctionReturn(0);
1516e30e807fSPeter Brune }
1517e30e807fSPeter Brune 
1518e30e807fSPeter Brune 
1519e30e807fSPeter Brune #undef __FUNCT__
1520e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1521e30e807fSPeter Brune /*@C
1522e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1523e30e807fSPeter Brune 
1524e30e807fSPeter Brune   Not collective
1525e30e807fSPeter Brune 
1526e30e807fSPeter Brune   Input Parameters:
1527e30e807fSPeter Brune + dm - the DM object
1528e30e807fSPeter Brune . n  - the number of subdomain scatters
1529e30e807fSPeter Brune - subdms - the local subdomains
1530e30e807fSPeter Brune 
1531e30e807fSPeter Brune   Output Parameters:
1532e30e807fSPeter Brune + n     - the number of scatters returned
1533e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1534e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1535e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1536e30e807fSPeter Brune 
1537e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1538e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1539e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1540e30e807fSPeter Brune   solution and residual data.
1541e30e807fSPeter Brune 
1542e30e807fSPeter Brune   Level: developer
1543e30e807fSPeter Brune 
1544e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1545e30e807fSPeter Brune @*/
1546e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1547e30e807fSPeter Brune {
1548e30e807fSPeter Brune   PetscErrorCode ierr;
1549e30e807fSPeter Brune 
1550e30e807fSPeter Brune   PetscFunctionBegin;
1551e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1552e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1553e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1554e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
155582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1556e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1557e7c4fc90SDmitry Karpeev }
1558e7c4fc90SDmitry Karpeev 
1559731c8d9eSDmitry Karpeev #undef __FUNCT__
156047c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
156147c6ae99SBarry Smith /*@
156247c6ae99SBarry Smith   DMRefine - Refines a DM object
156347c6ae99SBarry Smith 
156447c6ae99SBarry Smith   Collective on DM
156547c6ae99SBarry Smith 
156647c6ae99SBarry Smith   Input Parameter:
156747c6ae99SBarry Smith + dm   - the DM object
156891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
156947c6ae99SBarry Smith 
157047c6ae99SBarry Smith   Output Parameter:
15710298fd71SBarry Smith . dmf - the refined DM, or NULL
1572ae0a1c52SMatthew G Knepley 
15730298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
157447c6ae99SBarry Smith 
157547c6ae99SBarry Smith   Level: developer
157647c6ae99SBarry Smith 
1577e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
157847c6ae99SBarry Smith @*/
15797087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
158047c6ae99SBarry Smith {
158147c6ae99SBarry Smith   PetscErrorCode   ierr;
1582c833c3b5SJed Brown   DMRefineHookLink link;
158347c6ae99SBarry Smith 
158447c6ae99SBarry Smith   PetscFunctionBegin;
1585732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
158647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15874057135bSMatthew G Knepley   if (*dmf) {
158843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15898865f1eaSKarl Rupp 
15908cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15918865f1eaSKarl Rupp 
1592644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15930598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1594656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15958865f1eaSKarl Rupp 
1596e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1597c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
15988865f1eaSKarl Rupp       if (link->refinehook) {
15998865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16008865f1eaSKarl Rupp       }
1601c833c3b5SJed Brown     }
1602c833c3b5SJed Brown   }
1603c833c3b5SJed Brown   PetscFunctionReturn(0);
1604c833c3b5SJed Brown }
1605c833c3b5SJed Brown 
1606c833c3b5SJed Brown #undef __FUNCT__
1607c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1608bb9467b5SJed Brown /*@C
1609c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1610c833c3b5SJed Brown 
1611c833c3b5SJed Brown    Logically Collective
1612c833c3b5SJed Brown 
1613c833c3b5SJed Brown    Input Arguments:
1614c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1615c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1616c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16170298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1618c833c3b5SJed Brown 
1619c833c3b5SJed Brown    Calling sequence of refinehook:
1620c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1621c833c3b5SJed Brown 
1622c833c3b5SJed Brown +  coarse - coarse level DM
1623c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1624c833c3b5SJed Brown -  ctx - optional user-defined function context
1625c833c3b5SJed Brown 
1626c833c3b5SJed Brown    Calling sequence for interphook:
1627c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1628c833c3b5SJed Brown 
1629c833c3b5SJed Brown +  coarse - coarse level DM
1630c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1631c833c3b5SJed Brown .  fine - fine level DM to update
1632c833c3b5SJed Brown -  ctx - optional user-defined function context
1633c833c3b5SJed Brown 
1634c833c3b5SJed Brown    Level: advanced
1635c833c3b5SJed Brown 
1636c833c3b5SJed Brown    Notes:
1637c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1638c833c3b5SJed Brown 
1639c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1640c833c3b5SJed Brown 
1641bb9467b5SJed Brown    This function is currently not available from Fortran.
1642bb9467b5SJed Brown 
1643c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1644c833c3b5SJed Brown @*/
1645c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1646c833c3b5SJed Brown {
1647c833c3b5SJed Brown   PetscErrorCode   ierr;
1648c833c3b5SJed Brown   DMRefineHookLink link,*p;
1649c833c3b5SJed Brown 
1650c833c3b5SJed Brown   PetscFunctionBegin;
1651c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1652c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1653c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1654c833c3b5SJed Brown   link->refinehook = refinehook;
1655c833c3b5SJed Brown   link->interphook = interphook;
1656c833c3b5SJed Brown   link->ctx        = ctx;
16570298fd71SBarry Smith   link->next       = NULL;
1658c833c3b5SJed Brown   *p               = link;
1659c833c3b5SJed Brown   PetscFunctionReturn(0);
1660c833c3b5SJed Brown }
1661c833c3b5SJed Brown 
1662c833c3b5SJed Brown #undef __FUNCT__
1663c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1664c833c3b5SJed Brown /*@
1665c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1666c833c3b5SJed Brown 
1667c833c3b5SJed Brown    Collective if any hooks are
1668c833c3b5SJed Brown 
1669c833c3b5SJed Brown    Input Arguments:
1670c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1671c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1672c833c3b5SJed Brown -  fine - finer DM to update
1673c833c3b5SJed Brown 
1674c833c3b5SJed Brown    Level: developer
1675c833c3b5SJed Brown 
1676c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1677c833c3b5SJed Brown @*/
1678c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1679c833c3b5SJed Brown {
1680c833c3b5SJed Brown   PetscErrorCode   ierr;
1681c833c3b5SJed Brown   DMRefineHookLink link;
1682c833c3b5SJed Brown 
1683c833c3b5SJed Brown   PetscFunctionBegin;
1684c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16858865f1eaSKarl Rupp     if (link->interphook) {
16868865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16878865f1eaSKarl Rupp     }
16884057135bSMatthew G Knepley   }
168947c6ae99SBarry Smith   PetscFunctionReturn(0);
169047c6ae99SBarry Smith }
169147c6ae99SBarry Smith 
169247c6ae99SBarry Smith #undef __FUNCT__
1693eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1694eb3f98d2SBarry Smith /*@
1695eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1696eb3f98d2SBarry Smith 
1697eb3f98d2SBarry Smith     Not Collective
1698eb3f98d2SBarry Smith 
1699eb3f98d2SBarry Smith     Input Parameter:
1700eb3f98d2SBarry Smith .   dm - the DM object
1701eb3f98d2SBarry Smith 
1702eb3f98d2SBarry Smith     Output Parameter:
1703eb3f98d2SBarry Smith .   level - number of refinements
1704eb3f98d2SBarry Smith 
1705eb3f98d2SBarry Smith     Level: developer
1706eb3f98d2SBarry Smith 
17076a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1708eb3f98d2SBarry Smith 
1709eb3f98d2SBarry Smith @*/
1710eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1711eb3f98d2SBarry Smith {
1712eb3f98d2SBarry Smith   PetscFunctionBegin;
1713eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1714eb3f98d2SBarry Smith   *level = dm->levelup;
1715eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1716eb3f98d2SBarry Smith }
1717eb3f98d2SBarry Smith 
1718eb3f98d2SBarry Smith #undef __FUNCT__
1719baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1720bb9467b5SJed Brown /*@C
1721baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1722baf369e7SPeter Brune 
1723baf369e7SPeter Brune    Logically Collective
1724baf369e7SPeter Brune 
1725baf369e7SPeter Brune    Input Arguments:
1726baf369e7SPeter Brune +  dm - the DM
1727baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1728baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
17290298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1730baf369e7SPeter Brune 
1731baf369e7SPeter Brune    Calling sequence for beginhook:
1732baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1733baf369e7SPeter Brune 
1734baf369e7SPeter Brune +  dm - global DM
1735baf369e7SPeter Brune .  g - global vector
1736baf369e7SPeter Brune .  mode - mode
1737baf369e7SPeter Brune .  l - local vector
1738baf369e7SPeter Brune -  ctx - optional user-defined function context
1739baf369e7SPeter Brune 
1740baf369e7SPeter Brune 
1741baf369e7SPeter Brune    Calling sequence for endhook:
1742ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1743baf369e7SPeter Brune 
1744baf369e7SPeter Brune +  global - global DM
1745baf369e7SPeter Brune -  ctx - optional user-defined function context
1746baf369e7SPeter Brune 
1747baf369e7SPeter Brune    Level: advanced
1748baf369e7SPeter Brune 
1749baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1750baf369e7SPeter Brune @*/
1751baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1752baf369e7SPeter Brune {
1753baf369e7SPeter Brune   PetscErrorCode          ierr;
1754baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1755baf369e7SPeter Brune 
1756baf369e7SPeter Brune   PetscFunctionBegin;
1757baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1758baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1759baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1760baf369e7SPeter Brune   link->beginhook = beginhook;
1761baf369e7SPeter Brune   link->endhook   = endhook;
1762baf369e7SPeter Brune   link->ctx       = ctx;
17630298fd71SBarry Smith   link->next      = NULL;
1764baf369e7SPeter Brune   *p              = link;
1765baf369e7SPeter Brune   PetscFunctionReturn(0);
1766baf369e7SPeter Brune }
1767baf369e7SPeter Brune 
1768baf369e7SPeter Brune #undef __FUNCT__
17694c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
17704c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
17714c274da1SToby Isaac {
17724c274da1SToby Isaac   Mat cMat;
17734c274da1SToby Isaac   Vec cVec;
17744c274da1SToby Isaac   PetscSection section, cSec;
17754c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
17764c274da1SToby Isaac   PetscErrorCode ierr;
17774c274da1SToby Isaac 
17784c274da1SToby Isaac   PetscFunctionBegin;
17794c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17804c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
17814c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
17824c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
17837711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
17844c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
17854c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
17864c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
17874c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
17884c274da1SToby Isaac       if (dof) {
17894c274da1SToby Isaac         PetscScalar *vals;
17904c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
17914c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
17924c274da1SToby Isaac       }
17934c274da1SToby Isaac     }
17944c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
17954c274da1SToby Isaac   }
17964c274da1SToby Isaac   PetscFunctionReturn(0);
17974c274da1SToby Isaac }
17984c274da1SToby Isaac 
17994c274da1SToby Isaac #undef __FUNCT__
180047c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
180147c6ae99SBarry Smith /*@
180247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
180347c6ae99SBarry Smith 
180447c6ae99SBarry Smith     Neighbor-wise Collective on DM
180547c6ae99SBarry Smith 
180647c6ae99SBarry Smith     Input Parameters:
180747c6ae99SBarry Smith +   dm - the DM object
180847c6ae99SBarry Smith .   g - the global vector
180947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
181047c6ae99SBarry Smith -   l - the local vector
181147c6ae99SBarry Smith 
181247c6ae99SBarry Smith 
181347c6ae99SBarry Smith     Level: beginner
181447c6ae99SBarry Smith 
1815e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
181647c6ae99SBarry Smith 
181747c6ae99SBarry Smith @*/
18187087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
181947c6ae99SBarry Smith {
18207128ae9fSMatthew G Knepley   PetscSF                 sf;
182147c6ae99SBarry Smith   PetscErrorCode          ierr;
1822baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
182347c6ae99SBarry Smith 
182447c6ae99SBarry Smith   PetscFunctionBegin;
1825171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1826baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
18278865f1eaSKarl Rupp     if (link->beginhook) {
18288865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
18298865f1eaSKarl Rupp     }
1830baf369e7SPeter Brune   }
18317128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18327128ae9fSMatthew G Knepley   if (sf) {
1833ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1834ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
18357128ae9fSMatthew G Knepley 
183682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18377128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1838ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18397128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18407128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1841ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18427128ae9fSMatthew G Knepley   } else {
1843843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18447128ae9fSMatthew G Knepley   }
184547c6ae99SBarry Smith   PetscFunctionReturn(0);
184647c6ae99SBarry Smith }
184747c6ae99SBarry Smith 
184847c6ae99SBarry Smith #undef __FUNCT__
184947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
185047c6ae99SBarry Smith /*@
185147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
185247c6ae99SBarry Smith 
185347c6ae99SBarry Smith     Neighbor-wise Collective on DM
185447c6ae99SBarry Smith 
185547c6ae99SBarry Smith     Input Parameters:
185647c6ae99SBarry Smith +   dm - the DM object
185747c6ae99SBarry Smith .   g - the global vector
185847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
185947c6ae99SBarry Smith -   l - the local vector
186047c6ae99SBarry Smith 
186147c6ae99SBarry Smith 
186247c6ae99SBarry Smith     Level: beginner
186347c6ae99SBarry Smith 
1864e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
186547c6ae99SBarry Smith 
186647c6ae99SBarry Smith @*/
18677087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
186847c6ae99SBarry Smith {
18697128ae9fSMatthew G Knepley   PetscSF                 sf;
187047c6ae99SBarry Smith   PetscErrorCode          ierr;
1871ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1872ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1873baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
187447c6ae99SBarry Smith 
187547c6ae99SBarry Smith   PetscFunctionBegin;
1876171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18777128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18787128ae9fSMatthew G Knepley   if (sf) {
187982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18807128ae9fSMatthew G Knepley 
18817128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1882ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18837128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18847128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1885ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18867128ae9fSMatthew G Knepley   } else {
1887843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18887128ae9fSMatthew G Knepley   }
18894c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1890baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1891baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1892baf369e7SPeter Brune   }
189347c6ae99SBarry Smith   PetscFunctionReturn(0);
189447c6ae99SBarry Smith }
189547c6ae99SBarry Smith 
189647c6ae99SBarry Smith #undef __FUNCT__
1897d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1898d4d07f1eSToby Isaac /*@C
1899d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1900d4d07f1eSToby Isaac 
1901d4d07f1eSToby Isaac    Logically Collective
1902d4d07f1eSToby Isaac 
1903d4d07f1eSToby Isaac    Input Arguments:
1904d4d07f1eSToby Isaac +  dm - the DM
1905d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1906d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1907d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1908d4d07f1eSToby Isaac 
1909d4d07f1eSToby Isaac    Calling sequence for beginhook:
1910d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1911d4d07f1eSToby Isaac 
1912d4d07f1eSToby Isaac +  dm - global DM
1913d4d07f1eSToby Isaac .  l - local vector
1914d4d07f1eSToby Isaac .  mode - mode
1915d4d07f1eSToby Isaac .  g - global vector
1916d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1917d4d07f1eSToby Isaac 
1918d4d07f1eSToby Isaac 
1919d4d07f1eSToby Isaac    Calling sequence for endhook:
1920d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1921d4d07f1eSToby Isaac 
1922d4d07f1eSToby Isaac +  global - global DM
1923d4d07f1eSToby Isaac .  l - local vector
1924d4d07f1eSToby Isaac .  mode - mode
1925d4d07f1eSToby Isaac .  g - global vector
1926d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1927d4d07f1eSToby Isaac 
1928d4d07f1eSToby Isaac    Level: advanced
1929d4d07f1eSToby Isaac 
1930d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1931d4d07f1eSToby Isaac @*/
1932d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1933d4d07f1eSToby Isaac {
1934d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1935d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1936d4d07f1eSToby Isaac 
1937d4d07f1eSToby Isaac   PetscFunctionBegin;
1938d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1939d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1940d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1941d4d07f1eSToby Isaac   link->beginhook = beginhook;
1942d4d07f1eSToby Isaac   link->endhook   = endhook;
1943d4d07f1eSToby Isaac   link->ctx       = ctx;
1944d4d07f1eSToby Isaac   link->next      = NULL;
1945d4d07f1eSToby Isaac   *p              = link;
1946d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1947d4d07f1eSToby Isaac }
1948d4d07f1eSToby Isaac 
1949d4d07f1eSToby Isaac #undef __FUNCT__
19504c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
19514c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
19524c274da1SToby Isaac {
19534c274da1SToby Isaac   Mat cMat;
19544c274da1SToby Isaac   Vec cVec;
19554c274da1SToby Isaac   PetscSection section, cSec;
19564c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19574c274da1SToby Isaac   PetscErrorCode ierr;
19584c274da1SToby Isaac 
19594c274da1SToby Isaac   PetscFunctionBegin;
19604c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19614c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19624c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
19634c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19647711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19654c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19664c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19674c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19684c274da1SToby Isaac       if (dof) {
19694c274da1SToby Isaac         PetscInt d;
19704c274da1SToby Isaac         PetscScalar *vals;
19714c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
19724c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
19734c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
19744c274da1SToby Isaac          * we just extracted */
19754c274da1SToby Isaac         for (d = 0; d < dof; d++) {
19764c274da1SToby Isaac           vals[d] = 0.;
19774c274da1SToby Isaac         }
19784c274da1SToby Isaac       }
19794c274da1SToby Isaac     }
19804c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
19814c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19824c274da1SToby Isaac   }
19834c274da1SToby Isaac   PetscFunctionReturn(0);
19844c274da1SToby Isaac }
19854c274da1SToby Isaac 
19864c274da1SToby Isaac #undef __FUNCT__
19879a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
198847c6ae99SBarry Smith /*@
19899a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
19909a42bb27SBarry Smith 
19919a42bb27SBarry Smith     Neighbor-wise Collective on DM
19929a42bb27SBarry Smith 
19939a42bb27SBarry Smith     Input Parameters:
19949a42bb27SBarry Smith +   dm - the DM object
1995f6813fd5SJed Brown .   l - the local vector
19961eb28f2eSBarry 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.
19971eb28f2eSBarry Smith -   g - the global vector
19989a42bb27SBarry Smith 
1999d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
200084330215SMatthew 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.
20019a42bb27SBarry Smith 
20029a42bb27SBarry Smith     Level: beginner
20039a42bb27SBarry Smith 
2004e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
20059a42bb27SBarry Smith 
20069a42bb27SBarry Smith @*/
20077087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
20089a42bb27SBarry Smith {
20097128ae9fSMatthew G Knepley   PetscSF                 sf;
201084330215SMatthew G. Knepley   PetscSection            s, gs;
2011d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2012ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2013ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
201484330215SMatthew G. Knepley   PetscBool               isInsert;
201584330215SMatthew G. Knepley   PetscErrorCode          ierr;
20169a42bb27SBarry Smith 
20179a42bb27SBarry Smith   PetscFunctionBegin;
2018171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2019d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2020d4d07f1eSToby Isaac     if (link->beginhook) {
2021d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2022d4d07f1eSToby Isaac     }
2023d4d07f1eSToby Isaac   }
20244c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
20257128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
202684330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20277128ae9fSMatthew G Knepley   switch (mode) {
20287128ae9fSMatthew G Knepley   case INSERT_VALUES:
20297128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
203084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20317128ae9fSMatthew G Knepley   case ADD_VALUES:
20327128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
203384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20347128ae9fSMatthew G Knepley   default:
203582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20367128ae9fSMatthew G Knepley   }
203784330215SMatthew G. Knepley   if (sf && !isInsert) {
2038ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20397128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2040a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2041ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
204284330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
204384330215SMatthew G. Knepley   } else if (s && isInsert) {
204484330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
204584330215SMatthew G. Knepley 
204684330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
204784330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
204884330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2049ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
205084330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
205184330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2052b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
205384330215SMatthew G. Knepley 
205484330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
205503442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
205684330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2057b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
205884330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
205984330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2060b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
206103442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2062b3b16f48SMatthew 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);
2063b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2064b3b16f48SMatthew G. Knepley       if (!gcdof) {
206584330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2066b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2067b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
206884330215SMatthew G. Knepley         const PetscInt *cdofs;
206984330215SMatthew G. Knepley         PetscInt        cind = 0;
207084330215SMatthew G. Knepley 
207184330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2072b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
207384330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2074b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
207584330215SMatthew G. Knepley         }
2076b3b16f48SMatthew 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);
207784330215SMatthew G. Knepley     }
2078ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20797128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
20807128ae9fSMatthew G Knepley   } else {
2081843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20827128ae9fSMatthew G Knepley   }
20839a42bb27SBarry Smith   PetscFunctionReturn(0);
20849a42bb27SBarry Smith }
20859a42bb27SBarry Smith 
20869a42bb27SBarry Smith #undef __FUNCT__
20879a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
20889a42bb27SBarry Smith /*@
20899a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
209047c6ae99SBarry Smith 
209147c6ae99SBarry Smith     Neighbor-wise Collective on DM
209247c6ae99SBarry Smith 
209347c6ae99SBarry Smith     Input Parameters:
209447c6ae99SBarry Smith +   dm - the DM object
2095f6813fd5SJed Brown .   l - the local vector
209647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2097f6813fd5SJed Brown -   g - the global vector
209847c6ae99SBarry Smith 
209947c6ae99SBarry Smith 
210047c6ae99SBarry Smith     Level: beginner
210147c6ae99SBarry Smith 
2102e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
210347c6ae99SBarry Smith 
210447c6ae99SBarry Smith @*/
21057087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
210647c6ae99SBarry Smith {
21077128ae9fSMatthew G Knepley   PetscSF                 sf;
210884330215SMatthew G. Knepley   PetscSection            s;
2109d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
211084330215SMatthew G. Knepley   PetscBool               isInsert;
211184330215SMatthew G. Knepley   PetscErrorCode          ierr;
211247c6ae99SBarry Smith 
211347c6ae99SBarry Smith   PetscFunctionBegin;
2114171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21157128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
211684330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21177128ae9fSMatthew G Knepley   switch (mode) {
21187128ae9fSMatthew G Knepley   case INSERT_VALUES:
21197128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
212084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21217128ae9fSMatthew G Knepley   case ADD_VALUES:
21227128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
212384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21247128ae9fSMatthew G Knepley   default:
212582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21267128ae9fSMatthew G Knepley   }
212784330215SMatthew G. Knepley   if (sf && !isInsert) {
2128ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2129ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
213084330215SMatthew G. Knepley 
2131ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21327128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2133a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2134ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21357128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
213684330215SMatthew G. Knepley   } else if (s && isInsert) {
21377128ae9fSMatthew G Knepley   } else {
2138843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21397128ae9fSMatthew G Knepley   }
2140d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2141d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2142d4d07f1eSToby Isaac   }
214347c6ae99SBarry Smith   PetscFunctionReturn(0);
214447c6ae99SBarry Smith }
214547c6ae99SBarry Smith 
214647c6ae99SBarry Smith #undef __FUNCT__
2147f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2148f089877aSRichard Tran Mills /*@
2149bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2150bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2151d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2152f089877aSRichard Tran Mills 
2153bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2154f089877aSRichard Tran Mills 
2155f089877aSRichard Tran Mills    Input Parameters:
2156f089877aSRichard Tran Mills +  dm - the DM object
2157bc0a1609SRichard Tran Mills .  g - the original local vector
2158bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2159f089877aSRichard Tran Mills 
2160bc0a1609SRichard Tran Mills    Output Parameter:
2161bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2162f089877aSRichard Tran Mills 
2163f089877aSRichard Tran Mills    Level: intermediate
2164f089877aSRichard Tran Mills 
2165bc0a1609SRichard Tran Mills    Notes:
2166bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2167bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2168bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2169bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2170bc0a1609SRichard Tran Mills 
2171bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2172bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2173f089877aSRichard Tran Mills 
2174f089877aSRichard Tran Mills @*/
2175f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2176f089877aSRichard Tran Mills {
2177f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2178f089877aSRichard Tran Mills 
2179f089877aSRichard Tran Mills   PetscFunctionBegin;
2180f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2181f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2182f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2183f089877aSRichard Tran Mills }
2184f089877aSRichard Tran Mills 
2185f089877aSRichard Tran Mills #undef __FUNCT__
2186f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2187f089877aSRichard Tran Mills /*@
2188bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2189bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2190d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2191f089877aSRichard Tran Mills 
2192bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2193f089877aSRichard Tran Mills 
2194f089877aSRichard Tran Mills    Input Parameters:
2195bc0a1609SRichard Tran Mills +  da - the DM object
2196bc0a1609SRichard Tran Mills .  g - the original local vector
2197bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2198f089877aSRichard Tran Mills 
2199bc0a1609SRichard Tran Mills    Output Parameter:
2200bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2201f089877aSRichard Tran Mills 
2202f089877aSRichard Tran Mills    Level: intermediate
2203f089877aSRichard Tran Mills 
2204bc0a1609SRichard Tran Mills    Notes:
2205bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2206bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2207bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2208bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2209bc0a1609SRichard Tran Mills 
2210bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2211bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2212f089877aSRichard Tran Mills 
2213f089877aSRichard Tran Mills @*/
2214f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2215f089877aSRichard Tran Mills {
2216f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2217f089877aSRichard Tran Mills 
2218f089877aSRichard Tran Mills   PetscFunctionBegin;
2219f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2220f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2221f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2222f089877aSRichard Tran Mills }
2223f089877aSRichard Tran Mills 
2224f089877aSRichard Tran Mills 
2225f089877aSRichard Tran Mills #undef __FUNCT__
222647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
222747c6ae99SBarry Smith /*@
222847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
222947c6ae99SBarry Smith 
223047c6ae99SBarry Smith     Collective on DM
223147c6ae99SBarry Smith 
223247c6ae99SBarry Smith     Input Parameter:
223347c6ae99SBarry Smith +   dm - the DM object
223491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
223547c6ae99SBarry Smith 
223647c6ae99SBarry Smith     Output Parameter:
223747c6ae99SBarry Smith .   dmc - the coarsened DM
223847c6ae99SBarry Smith 
223947c6ae99SBarry Smith     Level: developer
224047c6ae99SBarry Smith 
2241e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
224247c6ae99SBarry Smith 
224347c6ae99SBarry Smith @*/
22447087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
224547c6ae99SBarry Smith {
224647c6ae99SBarry Smith   PetscErrorCode    ierr;
2247b17ce1afSJed Brown   DMCoarsenHookLink link;
224847c6ae99SBarry Smith 
224947c6ae99SBarry Smith   PetscFunctionBegin;
2250171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
225147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
225247c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
22538892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2254a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
225543842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
22568cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2257644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
22580598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2259656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2260e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2261b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2262b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2263b17ce1afSJed Brown   }
226447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2265b17ce1afSJed Brown   PetscFunctionReturn(0);
2266b17ce1afSJed Brown }
2267b17ce1afSJed Brown 
2268b17ce1afSJed Brown #undef __FUNCT__
2269b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2270bb9467b5SJed Brown /*@C
2271b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2272b17ce1afSJed Brown 
2273b17ce1afSJed Brown    Logically Collective
2274b17ce1afSJed Brown 
2275b17ce1afSJed Brown    Input Arguments:
2276b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2277b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2278b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
22790298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2280b17ce1afSJed Brown 
2281b17ce1afSJed Brown    Calling sequence of coarsenhook:
2282b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2283b17ce1afSJed Brown 
2284b17ce1afSJed Brown +  fine - fine level DM
2285b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2286b17ce1afSJed Brown -  ctx - optional user-defined function context
2287b17ce1afSJed Brown 
2288b17ce1afSJed Brown    Calling sequence for restricthook:
2289c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2290b17ce1afSJed Brown 
2291b17ce1afSJed Brown +  fine - fine level DM
2292b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2293c833c3b5SJed Brown .  rscale - scaling vector for restriction
2294c833c3b5SJed Brown .  inject - matrix restricting by injection
2295b17ce1afSJed Brown .  coarse - coarse level DM to update
2296b17ce1afSJed Brown -  ctx - optional user-defined function context
2297b17ce1afSJed Brown 
2298b17ce1afSJed Brown    Level: advanced
2299b17ce1afSJed Brown 
2300b17ce1afSJed Brown    Notes:
2301b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2302b17ce1afSJed Brown 
2303b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2304b17ce1afSJed Brown 
2305b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2306b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2307b17ce1afSJed Brown 
2308bb9467b5SJed Brown    This function is currently not available from Fortran.
2309bb9467b5SJed Brown 
2310c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2311b17ce1afSJed Brown @*/
2312b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2313b17ce1afSJed Brown {
2314b17ce1afSJed Brown   PetscErrorCode    ierr;
2315b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2316b17ce1afSJed Brown 
2317b17ce1afSJed Brown   PetscFunctionBegin;
2318b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
23196bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2320b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2321b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2322b17ce1afSJed Brown   link->restricthook = restricthook;
2323b17ce1afSJed Brown   link->ctx          = ctx;
23240298fd71SBarry Smith   link->next         = NULL;
2325b17ce1afSJed Brown   *p                 = link;
2326b17ce1afSJed Brown   PetscFunctionReturn(0);
2327b17ce1afSJed Brown }
2328b17ce1afSJed Brown 
2329b17ce1afSJed Brown #undef __FUNCT__
2330b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2331b17ce1afSJed Brown /*@
2332b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2333b17ce1afSJed Brown 
2334b17ce1afSJed Brown    Collective if any hooks are
2335b17ce1afSJed Brown 
2336b17ce1afSJed Brown    Input Arguments:
2337b17ce1afSJed Brown +  fine - finer DM to use as a base
2338b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2339b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2340b17ce1afSJed Brown -  coarse - coarer DM to update
2341b17ce1afSJed Brown 
2342b17ce1afSJed Brown    Level: developer
2343b17ce1afSJed Brown 
2344b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2345b17ce1afSJed Brown @*/
2346b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2347b17ce1afSJed Brown {
2348b17ce1afSJed Brown   PetscErrorCode    ierr;
2349b17ce1afSJed Brown   DMCoarsenHookLink link;
2350b17ce1afSJed Brown 
2351b17ce1afSJed Brown   PetscFunctionBegin;
2352b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
23538865f1eaSKarl Rupp     if (link->restricthook) {
23548865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
23558865f1eaSKarl Rupp     }
2356b17ce1afSJed Brown   }
235747c6ae99SBarry Smith   PetscFunctionReturn(0);
235847c6ae99SBarry Smith }
235947c6ae99SBarry Smith 
236047c6ae99SBarry Smith #undef __FUNCT__
2361be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2362bb9467b5SJed Brown /*@C
2363be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
23645dbd56e3SPeter Brune 
23655dbd56e3SPeter Brune    Logically Collective
23665dbd56e3SPeter Brune 
23675dbd56e3SPeter Brune    Input Arguments:
23685dbd56e3SPeter Brune +  global - global DM
2369ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
23705dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
23710298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23725dbd56e3SPeter Brune 
2373ec4806b8SPeter Brune 
2374ec4806b8SPeter Brune    Calling sequence for ddhook:
2375ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2376ec4806b8SPeter Brune 
2377ec4806b8SPeter Brune +  global - global DM
2378ec4806b8SPeter Brune .  block  - block DM
2379ec4806b8SPeter Brune -  ctx - optional user-defined function context
2380ec4806b8SPeter Brune 
23815dbd56e3SPeter Brune    Calling sequence for restricthook:
2382ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
23835dbd56e3SPeter Brune 
23845dbd56e3SPeter Brune +  global - global DM
23855dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
23865dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2387ec4806b8SPeter Brune .  block  - block DM
23885dbd56e3SPeter Brune -  ctx - optional user-defined function context
23895dbd56e3SPeter Brune 
23905dbd56e3SPeter Brune    Level: advanced
23915dbd56e3SPeter Brune 
23925dbd56e3SPeter Brune    Notes:
2393ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
23945dbd56e3SPeter Brune 
23955dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
23965dbd56e3SPeter Brune 
23975dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2398ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
23995dbd56e3SPeter Brune 
2400bb9467b5SJed Brown    This function is currently not available from Fortran.
2401bb9467b5SJed Brown 
24025dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
24035dbd56e3SPeter Brune @*/
2404be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
24055dbd56e3SPeter Brune {
24065dbd56e3SPeter Brune   PetscErrorCode      ierr;
2407be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
24085dbd56e3SPeter Brune 
24095dbd56e3SPeter Brune   PetscFunctionBegin;
24105dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2411be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2412be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
24135dbd56e3SPeter Brune   link->restricthook = restricthook;
2414be081cd6SPeter Brune   link->ddhook       = ddhook;
24155dbd56e3SPeter Brune   link->ctx          = ctx;
24160298fd71SBarry Smith   link->next         = NULL;
24175dbd56e3SPeter Brune   *p                 = link;
24185dbd56e3SPeter Brune   PetscFunctionReturn(0);
24195dbd56e3SPeter Brune }
24205dbd56e3SPeter Brune 
24215dbd56e3SPeter Brune #undef __FUNCT__
2422be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
24235dbd56e3SPeter Brune /*@
2424be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
24255dbd56e3SPeter Brune 
24265dbd56e3SPeter Brune    Collective if any hooks are
24275dbd56e3SPeter Brune 
24285dbd56e3SPeter Brune    Input Arguments:
24295dbd56e3SPeter Brune +  fine - finer DM to use as a base
2430be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2431be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
24325dbd56e3SPeter Brune -  coarse - coarer DM to update
24335dbd56e3SPeter Brune 
24345dbd56e3SPeter Brune    Level: developer
24355dbd56e3SPeter Brune 
24365dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
24375dbd56e3SPeter Brune @*/
2438be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
24395dbd56e3SPeter Brune {
24405dbd56e3SPeter Brune   PetscErrorCode      ierr;
2441be081cd6SPeter Brune   DMSubDomainHookLink link;
24425dbd56e3SPeter Brune 
24435dbd56e3SPeter Brune   PetscFunctionBegin;
2444be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
24458865f1eaSKarl Rupp     if (link->restricthook) {
24468865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
24478865f1eaSKarl Rupp     }
24485dbd56e3SPeter Brune   }
24495dbd56e3SPeter Brune   PetscFunctionReturn(0);
24505dbd56e3SPeter Brune }
24515dbd56e3SPeter Brune 
24525dbd56e3SPeter Brune #undef __FUNCT__
24535fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
24545fe1f584SPeter Brune /*@
24556a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
24565fe1f584SPeter Brune 
24575fe1f584SPeter Brune     Not Collective
24585fe1f584SPeter Brune 
24595fe1f584SPeter Brune     Input Parameter:
24605fe1f584SPeter Brune .   dm - the DM object
24615fe1f584SPeter Brune 
24625fe1f584SPeter Brune     Output Parameter:
24636a7d9d85SPeter Brune .   level - number of coarsenings
24645fe1f584SPeter Brune 
24655fe1f584SPeter Brune     Level: developer
24665fe1f584SPeter Brune 
24676a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
24685fe1f584SPeter Brune 
24695fe1f584SPeter Brune @*/
24705fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
24715fe1f584SPeter Brune {
24725fe1f584SPeter Brune   PetscFunctionBegin;
24735fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24745fe1f584SPeter Brune   *level = dm->leveldown;
24755fe1f584SPeter Brune   PetscFunctionReturn(0);
24765fe1f584SPeter Brune }
24775fe1f584SPeter Brune 
24785fe1f584SPeter Brune 
24795fe1f584SPeter Brune 
24805fe1f584SPeter Brune #undef __FUNCT__
248147c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
248247c6ae99SBarry Smith /*@C
248347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
248447c6ae99SBarry Smith 
248547c6ae99SBarry Smith     Collective on DM
248647c6ae99SBarry Smith 
248747c6ae99SBarry Smith     Input Parameter:
248847c6ae99SBarry Smith +   dm - the DM object
248947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
249047c6ae99SBarry Smith 
249147c6ae99SBarry Smith     Output Parameter:
249247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
249347c6ae99SBarry Smith 
249447c6ae99SBarry Smith     Level: developer
249547c6ae99SBarry Smith 
2496e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
249747c6ae99SBarry Smith 
249847c6ae99SBarry Smith @*/
24997087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
250047c6ae99SBarry Smith {
250147c6ae99SBarry Smith   PetscErrorCode ierr;
250247c6ae99SBarry Smith 
250347c6ae99SBarry Smith   PetscFunctionBegin;
2504171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2505ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
250647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
250747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
250847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
250947c6ae99SBarry Smith   } else if (dm->ops->refine) {
251047c6ae99SBarry Smith     PetscInt i;
251147c6ae99SBarry Smith 
2512ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
251347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2514ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
251547c6ae99SBarry Smith     }
2516ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
251747c6ae99SBarry Smith   PetscFunctionReturn(0);
251847c6ae99SBarry Smith }
251947c6ae99SBarry Smith 
252047c6ae99SBarry Smith #undef __FUNCT__
252147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
252247c6ae99SBarry Smith /*@C
252347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
252447c6ae99SBarry Smith 
252547c6ae99SBarry Smith     Collective on DM
252647c6ae99SBarry Smith 
252747c6ae99SBarry Smith     Input Parameter:
252847c6ae99SBarry Smith +   dm - the DM object
252947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
253047c6ae99SBarry Smith 
253147c6ae99SBarry Smith     Output Parameter:
253247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
253347c6ae99SBarry Smith 
253447c6ae99SBarry Smith     Level: developer
253547c6ae99SBarry Smith 
2536e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
253747c6ae99SBarry Smith 
253847c6ae99SBarry Smith @*/
25397087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
254047c6ae99SBarry Smith {
254147c6ae99SBarry Smith   PetscErrorCode ierr;
254247c6ae99SBarry Smith 
254347c6ae99SBarry Smith   PetscFunctionBegin;
2544171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2545ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
254647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
254747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
254847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
254947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
255047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
255147c6ae99SBarry Smith     PetscInt i;
255247c6ae99SBarry Smith 
2553ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
255447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2555ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
255647c6ae99SBarry Smith     }
2557ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
255847c6ae99SBarry Smith   PetscFunctionReturn(0);
255947c6ae99SBarry Smith }
256047c6ae99SBarry Smith 
256147c6ae99SBarry Smith #undef __FUNCT__
2562e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
256347c6ae99SBarry Smith /*@
2564e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
256547c6ae99SBarry Smith    grids associated with two DMs.
256647c6ae99SBarry Smith 
256747c6ae99SBarry Smith    Collective on DM
256847c6ae99SBarry Smith 
256947c6ae99SBarry Smith    Input Parameters:
257047c6ae99SBarry Smith +  dmc - the coarse grid DM
257147c6ae99SBarry Smith -  dmf - the fine grid DM
257247c6ae99SBarry Smith 
257347c6ae99SBarry Smith    Output Parameters:
257447c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
257547c6ae99SBarry Smith 
257647c6ae99SBarry Smith    Level: intermediate
257747c6ae99SBarry Smith 
257847c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
257947c6ae99SBarry Smith 
2580e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
258147c6ae99SBarry Smith @*/
2582e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
258347c6ae99SBarry Smith {
258447c6ae99SBarry Smith   PetscErrorCode ierr;
258547c6ae99SBarry Smith 
258647c6ae99SBarry Smith   PetscFunctionBegin;
2587171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2588171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
258947c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
259047c6ae99SBarry Smith   PetscFunctionReturn(0);
259147c6ae99SBarry Smith }
259247c6ae99SBarry Smith 
259347c6ae99SBarry Smith #undef __FUNCT__
25941a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
25951a266240SBarry Smith /*@C
25961a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
25971a266240SBarry Smith 
25981a266240SBarry Smith     Not Collective
25991a266240SBarry Smith 
26001a266240SBarry Smith     Input Parameters:
26011a266240SBarry Smith +   dm - the DM object
26021a266240SBarry Smith -   destroy - the destroy function
26031a266240SBarry Smith 
26041a266240SBarry Smith     Level: intermediate
26051a266240SBarry Smith 
2606e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
26071a266240SBarry Smith 
2608f07f9ceaSJed Brown @*/
26091a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
26101a266240SBarry Smith {
26111a266240SBarry Smith   PetscFunctionBegin;
2612171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26131a266240SBarry Smith   dm->ctxdestroy = destroy;
26141a266240SBarry Smith   PetscFunctionReturn(0);
26151a266240SBarry Smith }
26161a266240SBarry Smith 
26171a266240SBarry Smith #undef __FUNCT__
26181b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2619b07ff414SBarry Smith /*@
26201b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
262147c6ae99SBarry Smith 
262247c6ae99SBarry Smith     Not Collective
262347c6ae99SBarry Smith 
262447c6ae99SBarry Smith     Input Parameters:
262547c6ae99SBarry Smith +   dm - the DM object
262647c6ae99SBarry Smith -   ctx - the user context
262747c6ae99SBarry Smith 
262847c6ae99SBarry Smith     Level: intermediate
262947c6ae99SBarry Smith 
2630e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
263147c6ae99SBarry Smith 
263247c6ae99SBarry Smith @*/
26331b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
263447c6ae99SBarry Smith {
263547c6ae99SBarry Smith   PetscFunctionBegin;
2636171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
263747c6ae99SBarry Smith   dm->ctx = ctx;
263847c6ae99SBarry Smith   PetscFunctionReturn(0);
263947c6ae99SBarry Smith }
264047c6ae99SBarry Smith 
264147c6ae99SBarry Smith #undef __FUNCT__
26421b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
264347c6ae99SBarry Smith /*@
26441b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
264547c6ae99SBarry Smith 
264647c6ae99SBarry Smith     Not Collective
264747c6ae99SBarry Smith 
264847c6ae99SBarry Smith     Input Parameter:
264947c6ae99SBarry Smith .   dm - the DM object
265047c6ae99SBarry Smith 
265147c6ae99SBarry Smith     Output Parameter:
265247c6ae99SBarry Smith .   ctx - the user context
265347c6ae99SBarry Smith 
265447c6ae99SBarry Smith     Level: intermediate
265547c6ae99SBarry Smith 
2656e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
265747c6ae99SBarry Smith 
265847c6ae99SBarry Smith @*/
26591b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
266047c6ae99SBarry Smith {
266147c6ae99SBarry Smith   PetscFunctionBegin;
2662171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26631b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
266447c6ae99SBarry Smith   PetscFunctionReturn(0);
266547c6ae99SBarry Smith }
266647c6ae99SBarry Smith 
266747c6ae99SBarry Smith #undef __FUNCT__
266808da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
266908da532bSDmitry Karpeev /*@C
2670df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
267108da532bSDmitry Karpeev 
267208da532bSDmitry Karpeev     Logically Collective on DM
267308da532bSDmitry Karpeev 
267408da532bSDmitry Karpeev     Input Parameter:
267508da532bSDmitry Karpeev +   dm - the DM object
26760298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
267708da532bSDmitry Karpeev 
267808da532bSDmitry Karpeev     Level: intermediate
267908da532bSDmitry Karpeev 
2680835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
268108da532bSDmitry Karpeev          DMSetJacobian()
268208da532bSDmitry Karpeev 
268308da532bSDmitry Karpeev @*/
268408da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
268508da532bSDmitry Karpeev {
268608da532bSDmitry Karpeev   PetscFunctionBegin;
268708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
268808da532bSDmitry Karpeev   PetscFunctionReturn(0);
268908da532bSDmitry Karpeev }
269008da532bSDmitry Karpeev 
269108da532bSDmitry Karpeev #undef __FUNCT__
269208da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
269308da532bSDmitry Karpeev /*@
269408da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
269508da532bSDmitry Karpeev 
269608da532bSDmitry Karpeev     Not Collective
269708da532bSDmitry Karpeev 
269808da532bSDmitry Karpeev     Input Parameter:
269908da532bSDmitry Karpeev .   dm - the DM object to destroy
270008da532bSDmitry Karpeev 
270108da532bSDmitry Karpeev     Output Parameter:
270208da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
270308da532bSDmitry Karpeev 
270408da532bSDmitry Karpeev     Level: developer
270508da532bSDmitry Karpeev 
270674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
270708da532bSDmitry Karpeev 
270808da532bSDmitry Karpeev @*/
270908da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
271008da532bSDmitry Karpeev {
271108da532bSDmitry Karpeev   PetscFunctionBegin;
271208da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
271308da532bSDmitry Karpeev   PetscFunctionReturn(0);
271408da532bSDmitry Karpeev }
271508da532bSDmitry Karpeev 
271608da532bSDmitry Karpeev #undef __FUNCT__
271708da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
271808da532bSDmitry Karpeev /*@C
271908da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
272008da532bSDmitry Karpeev 
272108da532bSDmitry Karpeev     Logically Collective on DM
272208da532bSDmitry Karpeev 
272308da532bSDmitry Karpeev     Input Parameters:
2724907376e6SBarry Smith .   dm - the DM object
272508da532bSDmitry Karpeev 
272608da532bSDmitry Karpeev     Output parameters:
272708da532bSDmitry Karpeev +   xl - lower bound
272808da532bSDmitry Karpeev -   xu - upper bound
272908da532bSDmitry Karpeev 
2730907376e6SBarry Smith     Level: advanced
2731907376e6SBarry Smith 
2732907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
273308da532bSDmitry Karpeev 
273474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
273508da532bSDmitry Karpeev 
273608da532bSDmitry Karpeev @*/
273708da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
273808da532bSDmitry Karpeev {
273908da532bSDmitry Karpeev   PetscErrorCode ierr;
27405fd66863SKarl Rupp 
274108da532bSDmitry Karpeev   PetscFunctionBegin;
274208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
274308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
274408da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
274508da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
27468865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
274708da532bSDmitry Karpeev   PetscFunctionReturn(0);
274808da532bSDmitry Karpeev }
274908da532bSDmitry Karpeev 
275008da532bSDmitry Karpeev #undef __FUNCT__
2751b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2752b0ae01b7SPeter Brune /*@
2753b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2754b0ae01b7SPeter Brune 
2755b0ae01b7SPeter Brune     Not Collective
2756b0ae01b7SPeter Brune 
2757b0ae01b7SPeter Brune     Input Parameter:
2758b0ae01b7SPeter Brune .   dm - the DM object
2759b0ae01b7SPeter Brune 
2760b0ae01b7SPeter Brune     Output Parameter:
2761b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2762b0ae01b7SPeter Brune 
2763b0ae01b7SPeter Brune     Level: developer
2764b0ae01b7SPeter Brune 
2765b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2766b0ae01b7SPeter Brune 
2767b0ae01b7SPeter Brune @*/
2768b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2769b0ae01b7SPeter Brune {
2770b0ae01b7SPeter Brune   PetscFunctionBegin;
2771b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2772b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2773b0ae01b7SPeter Brune }
2774b0ae01b7SPeter Brune 
2775b0ae01b7SPeter Brune #undef  __FUNCT__
277608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2777748fac09SDmitry Karpeev /*@C
277808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
277908da532bSDmitry Karpeev 
278008da532bSDmitry Karpeev     Collective on DM
278108da532bSDmitry Karpeev 
278208da532bSDmitry Karpeev     Input Parameter:
278308da532bSDmitry Karpeev +   dm - the DM object
27840298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
278508da532bSDmitry Karpeev 
278608da532bSDmitry Karpeev     Level: developer
278708da532bSDmitry Karpeev 
278874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
278908da532bSDmitry Karpeev 
279008da532bSDmitry Karpeev @*/
279108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
279208da532bSDmitry Karpeev {
279308da532bSDmitry Karpeev   PetscErrorCode ierr;
27945fd66863SKarl Rupp 
279508da532bSDmitry Karpeev   PetscFunctionBegin;
279608da532bSDmitry Karpeev   if (x) {
279708da532bSDmitry Karpeev     if (!dm->x) {
279808da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
279908da532bSDmitry Karpeev     }
280008da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
28018865f1eaSKarl Rupp   } else if (dm->x) {
280208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
280308da532bSDmitry Karpeev   }
280408da532bSDmitry Karpeev   PetscFunctionReturn(0);
280508da532bSDmitry Karpeev }
280608da532bSDmitry Karpeev 
28070298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2808264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2809264ace61SBarry Smith 
2810264ace61SBarry Smith #undef __FUNCT__
2811264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2812264ace61SBarry Smith /*@C
2813264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2814264ace61SBarry Smith 
2815264ace61SBarry Smith   Collective on DM
2816264ace61SBarry Smith 
2817264ace61SBarry Smith   Input Parameters:
2818264ace61SBarry Smith + dm     - The DM object
2819264ace61SBarry Smith - method - The name of the DM type
2820264ace61SBarry Smith 
2821264ace61SBarry Smith   Options Database Key:
2822264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2823264ace61SBarry Smith 
2824264ace61SBarry Smith   Notes:
2825e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2826264ace61SBarry Smith 
2827264ace61SBarry Smith   Level: intermediate
2828264ace61SBarry Smith 
2829264ace61SBarry Smith .keywords: DM, set, type
2830264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2831264ace61SBarry Smith @*/
283219fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2833264ace61SBarry Smith {
2834264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2835264ace61SBarry Smith   PetscBool      match;
2836264ace61SBarry Smith   PetscErrorCode ierr;
2837264ace61SBarry Smith 
2838264ace61SBarry Smith   PetscFunctionBegin;
2839264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2840251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2841264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2842264ace61SBarry Smith 
28430f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
28441c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2845ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2846264ace61SBarry Smith 
2847264ace61SBarry Smith   if (dm->ops->destroy) {
2848264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
28490298fd71SBarry Smith     dm->ops->destroy = NULL;
2850264ace61SBarry Smith   }
2851264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2852264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2853264ace61SBarry Smith   PetscFunctionReturn(0);
2854264ace61SBarry Smith }
2855264ace61SBarry Smith 
2856264ace61SBarry Smith #undef __FUNCT__
2857264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2858264ace61SBarry Smith /*@C
2859264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2860264ace61SBarry Smith 
2861264ace61SBarry Smith   Not Collective
2862264ace61SBarry Smith 
2863264ace61SBarry Smith   Input Parameter:
2864264ace61SBarry Smith . dm  - The DM
2865264ace61SBarry Smith 
2866264ace61SBarry Smith   Output Parameter:
2867264ace61SBarry Smith . type - The DM type name
2868264ace61SBarry Smith 
2869264ace61SBarry Smith   Level: intermediate
2870264ace61SBarry Smith 
2871264ace61SBarry Smith .keywords: DM, get, type, name
2872264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2873264ace61SBarry Smith @*/
287419fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2875264ace61SBarry Smith {
2876264ace61SBarry Smith   PetscErrorCode ierr;
2877264ace61SBarry Smith 
2878264ace61SBarry Smith   PetscFunctionBegin;
2879264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2880c959eef4SJed Brown   PetscValidPointer(type,2);
2881607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2882264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2883264ace61SBarry Smith   PetscFunctionReturn(0);
2884264ace61SBarry Smith }
2885264ace61SBarry Smith 
288667a56275SMatthew G Knepley #undef __FUNCT__
288767a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
288867a56275SMatthew G Knepley /*@C
288967a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
289067a56275SMatthew G Knepley 
289167a56275SMatthew G Knepley   Collective on DM
289267a56275SMatthew G Knepley 
289367a56275SMatthew G Knepley   Input Parameters:
289467a56275SMatthew G Knepley + dm - the DM
289567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
289667a56275SMatthew G Knepley 
289767a56275SMatthew G Knepley   Output Parameter:
289867a56275SMatthew G Knepley . M - pointer to new DM
289967a56275SMatthew G Knepley 
290067a56275SMatthew G Knepley   Notes:
290167a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
290267a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
290367a56275SMatthew G Knepley   of the input DM.
290467a56275SMatthew G Knepley 
290567a56275SMatthew G Knepley   Level: intermediate
290667a56275SMatthew G Knepley 
290767a56275SMatthew G Knepley .seealso: DMCreate()
290867a56275SMatthew G Knepley @*/
290919fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
291067a56275SMatthew G Knepley {
291167a56275SMatthew G Knepley   DM             B;
291267a56275SMatthew G Knepley   char           convname[256];
291367a56275SMatthew G Knepley   PetscBool      sametype, issame;
291467a56275SMatthew G Knepley   PetscErrorCode ierr;
291567a56275SMatthew G Knepley 
291667a56275SMatthew G Knepley   PetscFunctionBegin;
291767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
291867a56275SMatthew G Knepley   PetscValidType(dm,1);
291967a56275SMatthew G Knepley   PetscValidPointer(M,3);
2920251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
292167a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
292267a56275SMatthew G Knepley   {
29230298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
292467a56275SMatthew G Knepley 
292567a56275SMatthew G Knepley     /*
292667a56275SMatthew G Knepley        Order of precedence:
292767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
292867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
292967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
293067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
293167a56275SMatthew G Knepley        5) Use a really basic converter.
293267a56275SMatthew G Knepley     */
293367a56275SMatthew G Knepley 
293467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
293567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
293667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
293767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
293867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
293967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29400005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
294167a56275SMatthew G Knepley     if (conv) goto foundconv;
294267a56275SMatthew G Knepley 
294367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
294482f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
294567a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
294667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
294767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
294867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
294967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
295067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29510005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
295267a56275SMatthew G Knepley     if (conv) {
2953fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
295467a56275SMatthew G Knepley       goto foundconv;
295567a56275SMatthew G Knepley     }
295667a56275SMatthew G Knepley 
295767a56275SMatthew G Knepley #if 0
295867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
295967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2960fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
296167a56275SMatthew G Knepley     if (conv) goto foundconv;
296267a56275SMatthew G Knepley 
296367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
296467a56275SMatthew G Knepley     if (dm->ops->convert) {
296567a56275SMatthew G Knepley       conv = dm->ops->convert;
296667a56275SMatthew G Knepley     }
296767a56275SMatthew G Knepley     if (conv) goto foundconv;
296867a56275SMatthew G Knepley #endif
296967a56275SMatthew G Knepley 
297067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
297182f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
297267a56275SMatthew G Knepley 
297367a56275SMatthew G Knepley foundconv:
297467a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
297567a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
297667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
297767a56275SMatthew G Knepley   }
297867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
297967a56275SMatthew G Knepley   PetscFunctionReturn(0);
298067a56275SMatthew G Knepley }
2981264ace61SBarry Smith 
2982264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2983264ace61SBarry Smith 
2984264ace61SBarry Smith #undef __FUNCT__
2985264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2986264ace61SBarry Smith /*@C
29871c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
29881c84c290SBarry Smith 
29891c84c290SBarry Smith   Not Collective
29901c84c290SBarry Smith 
29911c84c290SBarry Smith   Input Parameters:
29921c84c290SBarry Smith + name        - The name of a new user-defined creation routine
29931c84c290SBarry Smith - create_func - The creation routine itself
29941c84c290SBarry Smith 
29951c84c290SBarry Smith   Notes:
29961c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
29971c84c290SBarry Smith 
29981c84c290SBarry Smith 
29991c84c290SBarry Smith   Sample usage:
30001c84c290SBarry Smith .vb
3001bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
30021c84c290SBarry Smith .ve
30031c84c290SBarry Smith 
30041c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
30051c84c290SBarry Smith .vb
30061c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
30071c84c290SBarry Smith     DMSetType(DM,"my_da");
30081c84c290SBarry Smith .ve
30091c84c290SBarry Smith    or at runtime via the option
30101c84c290SBarry Smith .vb
30111c84c290SBarry Smith     -da_type my_da
30121c84c290SBarry Smith .ve
3013264ace61SBarry Smith 
3014264ace61SBarry Smith   Level: advanced
30151c84c290SBarry Smith 
30161c84c290SBarry Smith .keywords: DM, register
3017bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
30181c84c290SBarry Smith 
3019264ace61SBarry Smith @*/
3020bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3021264ace61SBarry Smith {
3022264ace61SBarry Smith   PetscErrorCode ierr;
3023264ace61SBarry Smith 
3024264ace61SBarry Smith   PetscFunctionBegin;
3025a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3026264ace61SBarry Smith   PetscFunctionReturn(0);
3027264ace61SBarry Smith }
3028264ace61SBarry Smith 
3029b859378eSBarry Smith #undef __FUNCT__
3030b859378eSBarry Smith #define __FUNCT__ "DMLoad"
3031b859378eSBarry Smith /*@C
303255849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3033b859378eSBarry Smith 
3034b859378eSBarry Smith   Collective on PetscViewer
3035b859378eSBarry Smith 
3036b859378eSBarry Smith   Input Parameters:
3037b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3038b859378eSBarry Smith            some related function before a call to DMLoad().
3039b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3040b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3041b859378eSBarry Smith 
3042b859378eSBarry Smith    Level: intermediate
3043b859378eSBarry Smith 
3044b859378eSBarry Smith   Notes:
304555849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3046b859378eSBarry Smith 
3047b859378eSBarry Smith   Notes for advanced users:
3048b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3049b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3050b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3051b859378eSBarry Smith   format is
3052b859378eSBarry Smith .vb
3053b859378eSBarry Smith      has not yet been determined
3054b859378eSBarry Smith .ve
3055b859378eSBarry Smith 
3056b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3057b859378eSBarry Smith @*/
3058b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3059b859378eSBarry Smith {
30609331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3061b859378eSBarry Smith   PetscErrorCode ierr;
3062b859378eSBarry Smith 
3063b859378eSBarry Smith   PetscFunctionBegin;
3064b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3065b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
306632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
30679331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
30689331c7a4SMatthew G. Knepley   if (isbinary) {
30699331c7a4SMatthew G. Knepley     PetscInt classid;
30709331c7a4SMatthew G. Knepley     char     type[256];
3071b859378eSBarry Smith 
3072060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
30739200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3074060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
307532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
30769331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30779331c7a4SMatthew G. Knepley   } else if (ishdf5) {
30789331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30799331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3080b859378eSBarry Smith   PetscFunctionReturn(0);
3081b859378eSBarry Smith }
3082b859378eSBarry Smith 
30837da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30847da65231SMatthew G Knepley 
30857da65231SMatthew G Knepley #undef __FUNCT__
30867da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3087a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3088a6dfd86eSKarl Rupp {
30891d47ebbbSSatish Balay   PetscInt       f;
30901b30c384SMatthew G Knepley   PetscErrorCode ierr;
30911b30c384SMatthew G Knepley 
30927da65231SMatthew G Knepley   PetscFunctionBegin;
309374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30941d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
309557622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
30967da65231SMatthew G Knepley   }
30977da65231SMatthew G Knepley   PetscFunctionReturn(0);
30987da65231SMatthew G Knepley }
30997da65231SMatthew G Knepley 
31007da65231SMatthew G Knepley #undef __FUNCT__
31017da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3102a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3103a6dfd86eSKarl Rupp {
31041b30c384SMatthew G Knepley   PetscInt       f, g;
31057da65231SMatthew G Knepley   PetscErrorCode ierr;
31067da65231SMatthew G Knepley 
31077da65231SMatthew G Knepley   PetscFunctionBegin;
310874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
31091d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
311074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
31111d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3112e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
31137da65231SMatthew G Knepley     }
311474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
31157da65231SMatthew G Knepley   }
31167da65231SMatthew G Knepley   PetscFunctionReturn(0);
31177da65231SMatthew G Knepley }
3118e7c4fc90SDmitry Karpeev 
3119970e74d5SMatthew G Knepley #undef __FUNCT__
3120e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
31216113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3122e759306cSMatthew G. Knepley {
3123e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3124e759306cSMatthew G. Knepley   PetscInt       p;
3125e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3126e759306cSMatthew G. Knepley 
3127e759306cSMatthew G. Knepley   PetscFunctionBegin;
3128e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3129e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3130e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3131e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3132e759306cSMatthew G. Knepley     if (p == rank) {
3133e759306cSMatthew G. Knepley       Vec x;
3134e759306cSMatthew G. Knepley 
3135e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3136e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
31376113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3138e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3139e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3140e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3141e759306cSMatthew G. Knepley     }
3142e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3143e759306cSMatthew G. Knepley   }
3144e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3145e759306cSMatthew G. Knepley }
3146e759306cSMatthew G. Knepley 
3147e759306cSMatthew G. Knepley #undef __FUNCT__
314888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
314988ed4aceSMatthew G Knepley /*@
315088ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
315188ed4aceSMatthew G Knepley 
315288ed4aceSMatthew G Knepley   Input Parameter:
315388ed4aceSMatthew G Knepley . dm - The DM
315488ed4aceSMatthew G Knepley 
315588ed4aceSMatthew G Knepley   Output Parameter:
315688ed4aceSMatthew G Knepley . section - The PetscSection
315788ed4aceSMatthew G Knepley 
315888ed4aceSMatthew G Knepley   Level: intermediate
315988ed4aceSMatthew G Knepley 
316088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
316188ed4aceSMatthew G Knepley 
316288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
316388ed4aceSMatthew G Knepley @*/
31640adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
31650adebc6cSBarry Smith {
3166fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3167fd59a867SMatthew G. Knepley 
316888ed4aceSMatthew G Knepley   PetscFunctionBegin;
316988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
317088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
31712f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
31722f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3173685405a1SBarry Smith     ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);
31742f0f8703SMatthew G. Knepley   }
317588ed4aceSMatthew G Knepley   *section = dm->defaultSection;
317688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
317788ed4aceSMatthew G Knepley }
317888ed4aceSMatthew G Knepley 
317988ed4aceSMatthew G Knepley #undef __FUNCT__
318088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
318188ed4aceSMatthew G Knepley /*@
318288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
318388ed4aceSMatthew G Knepley 
318488ed4aceSMatthew G Knepley   Input Parameters:
318588ed4aceSMatthew G Knepley + dm - The DM
318688ed4aceSMatthew G Knepley - section - The PetscSection
318788ed4aceSMatthew G Knepley 
318888ed4aceSMatthew G Knepley   Level: intermediate
318988ed4aceSMatthew G Knepley 
319088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
319188ed4aceSMatthew G Knepley 
319288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
319388ed4aceSMatthew G Knepley @*/
31940adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
31950adebc6cSBarry Smith {
3196c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3197af122d2aSMatthew G Knepley   PetscInt       f;
319888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
319988ed4aceSMatthew G Knepley 
320088ed4aceSMatthew G Knepley   PetscFunctionBegin;
320188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3202c473ab19SMatthew G. Knepley   if (section) {
32031d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
32041d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3205c473ab19SMatthew G. Knepley   }
320688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
320788ed4aceSMatthew G Knepley   dm->defaultSection = section;
3208c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3209af122d2aSMatthew G Knepley   if (numFields) {
3210af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3211af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
32120f21e855SMatthew G. Knepley       PetscObject disc;
3213af122d2aSMatthew G Knepley       const char *name;
3214af122d2aSMatthew G Knepley 
3215af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
32160f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
32170f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3218af122d2aSMatthew G Knepley     }
3219af122d2aSMatthew G Knepley   }
32201d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
32211d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
322288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
322388ed4aceSMatthew G Knepley }
322488ed4aceSMatthew G Knepley 
322588ed4aceSMatthew G Knepley #undef __FUNCT__
32269435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
32279435951eSToby Isaac /*@
32289435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
32299435951eSToby Isaac 
3230e228b242SToby Isaac   not collective
3231e228b242SToby Isaac 
32329435951eSToby Isaac   Input Parameter:
32339435951eSToby Isaac . dm - The DM
32349435951eSToby Isaac 
32359435951eSToby Isaac   Output Parameter:
32369435951eSToby 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.
32379435951eSToby 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.
32389435951eSToby Isaac 
32399435951eSToby Isaac   Level: advanced
32409435951eSToby Isaac 
32419435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
32429435951eSToby Isaac 
32439435951eSToby Isaac .seealso: DMSetDefaultConstraints()
32449435951eSToby Isaac @*/
32459435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
32469435951eSToby Isaac {
32479435951eSToby Isaac   PetscErrorCode ierr;
32489435951eSToby Isaac 
32499435951eSToby Isaac   PetscFunctionBegin;
32509435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32519435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
325245a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
325345a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
32549435951eSToby Isaac   PetscFunctionReturn(0);
32559435951eSToby Isaac }
32569435951eSToby Isaac 
32579435951eSToby Isaac #undef __FUNCT__
32589435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
32599435951eSToby Isaac /*@
32609435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
32619435951eSToby Isaac 
32629435951eSToby 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().
32639435951eSToby Isaac 
32649435951eSToby 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.
32659435951eSToby Isaac 
3266e228b242SToby Isaac   collective on dm
3267e228b242SToby Isaac 
32689435951eSToby Isaac   Input Parameters:
32699435951eSToby Isaac + dm - The DM
3270e228b242SToby 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).
3271e228b242SToby 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).
32729435951eSToby Isaac 
32739435951eSToby Isaac   Level: advanced
32749435951eSToby Isaac 
32759435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32769435951eSToby Isaac 
32779435951eSToby Isaac .seealso: DMGetDefaultConstraints()
32789435951eSToby Isaac @*/
32799435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
32809435951eSToby Isaac {
3281e228b242SToby Isaac   PetscMPIInt result;
32829435951eSToby Isaac   PetscErrorCode ierr;
32839435951eSToby Isaac 
32849435951eSToby Isaac   PetscFunctionBegin;
32859435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3286e228b242SToby Isaac   if (section) {
3287e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3288e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3289e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3290e228b242SToby Isaac   }
3291e228b242SToby Isaac   if (mat) {
3292e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3293e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3294e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3295e228b242SToby Isaac   }
32969435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
32979435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
32989435951eSToby Isaac   dm->defaultConstraintSection = section;
32999435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
33009435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
33019435951eSToby Isaac   dm->defaultConstraintMat = mat;
33029435951eSToby Isaac   PetscFunctionReturn(0);
33039435951eSToby Isaac }
33049435951eSToby Isaac 
3305f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
33069435951eSToby Isaac #undef __FUNCT__
3307284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3308507e4973SMatthew G. Knepley /*
3309507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3310507e4973SMatthew G. Knepley 
3311507e4973SMatthew G. Knepley   Input Parameters:
3312507e4973SMatthew G. Knepley + dm - The DM
3313507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3314507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3315507e4973SMatthew G. Knepley 
3316507e4973SMatthew G. Knepley   Level: intermediate
3317507e4973SMatthew G. Knepley 
3318507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3319507e4973SMatthew G. Knepley */
3320f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3321507e4973SMatthew G. Knepley {
3322507e4973SMatthew G. Knepley   MPI_Comm        comm;
3323507e4973SMatthew G. Knepley   PetscLayout     layout;
3324507e4973SMatthew G. Knepley   const PetscInt *ranges;
3325507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3326507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3327507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3328507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3329507e4973SMatthew G. Knepley 
3330507e4973SMatthew G. Knepley   PetscFunctionBegin;
3331507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3332507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3333507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3334507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3335507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3336507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3337507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3338507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3339507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3340507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3341507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3342507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3343f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3344507e4973SMatthew G. Knepley 
3345507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3346507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3347507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3348507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3349507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3350507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3351507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3352507e4973SMatthew 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;}
3353507e4973SMatthew 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;}
3354507e4973SMatthew G. Knepley     if (gdof < 0) {
3355507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3356507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3357507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3358507e4973SMatthew G. Knepley 
3359507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3360507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3361507e4973SMatthew 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;}
3362507e4973SMatthew G. Knepley       }
3363507e4973SMatthew G. Knepley     }
3364507e4973SMatthew G. Knepley   }
3365507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3366507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3367b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3368507e4973SMatthew G. Knepley   if (!gvalid) {
3369507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3370507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3371507e4973SMatthew G. Knepley   }
3372507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3373507e4973SMatthew G. Knepley }
3374f741bcd2SMatthew G. Knepley #endif
3375507e4973SMatthew G. Knepley 
3376507e4973SMatthew G. Knepley #undef __FUNCT__
337788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
337888ed4aceSMatthew G Knepley /*@
337988ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
338088ed4aceSMatthew G Knepley 
33818b1ab98fSJed Brown   Collective on DM
33828b1ab98fSJed Brown 
338388ed4aceSMatthew G Knepley   Input Parameter:
338488ed4aceSMatthew G Knepley . dm - The DM
338588ed4aceSMatthew G Knepley 
338688ed4aceSMatthew G Knepley   Output Parameter:
338788ed4aceSMatthew G Knepley . section - The PetscSection
338888ed4aceSMatthew G Knepley 
338988ed4aceSMatthew G Knepley   Level: intermediate
339088ed4aceSMatthew G Knepley 
339188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
339288ed4aceSMatthew G Knepley 
339388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
339488ed4aceSMatthew G Knepley @*/
33950adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
33960adebc6cSBarry Smith {
339788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
339888ed4aceSMatthew G Knepley 
339988ed4aceSMatthew G Knepley   PetscFunctionBegin;
340088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
340188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
340288ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3403fd59a867SMatthew G. Knepley     PetscSection s;
3404fd59a867SMatthew G. Knepley 
3405fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3406fd59a867SMatthew 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");
3407fd59a867SMatthew 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");
340815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3409cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3410ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3411685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
341288ed4aceSMatthew G Knepley   }
341388ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
341488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
341588ed4aceSMatthew G Knepley }
341688ed4aceSMatthew G Knepley 
341788ed4aceSMatthew G Knepley #undef __FUNCT__
3418b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3419b21d0597SMatthew G Knepley /*@
3420b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3421b21d0597SMatthew G Knepley 
3422b21d0597SMatthew G Knepley   Input Parameters:
3423b21d0597SMatthew G Knepley + dm - The DM
34245080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3425b21d0597SMatthew G Knepley 
3426b21d0597SMatthew G Knepley   Level: intermediate
3427b21d0597SMatthew G Knepley 
3428b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3429b21d0597SMatthew G Knepley 
3430b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3431b21d0597SMatthew G Knepley @*/
34320adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
34330adebc6cSBarry Smith {
3434b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3435b21d0597SMatthew G Knepley 
3436b21d0597SMatthew G Knepley   PetscFunctionBegin;
3437b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34385080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34391d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3440b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3441b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3442507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3443f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3444507e4973SMatthew G. Knepley #endif
3445b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3446b21d0597SMatthew G Knepley }
3447b21d0597SMatthew G Knepley 
3448b21d0597SMatthew G Knepley #undef __FUNCT__
344988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
345088ed4aceSMatthew G Knepley /*@
345188ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
345288ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
345388ed4aceSMatthew G Knepley 
345488ed4aceSMatthew G Knepley   Input Parameter:
345588ed4aceSMatthew G Knepley . dm - The DM
345688ed4aceSMatthew G Knepley 
345788ed4aceSMatthew G Knepley   Output Parameter:
345888ed4aceSMatthew G Knepley . sf - The PetscSF
345988ed4aceSMatthew G Knepley 
346088ed4aceSMatthew G Knepley   Level: intermediate
346188ed4aceSMatthew G Knepley 
346288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
346388ed4aceSMatthew G Knepley 
346488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
346588ed4aceSMatthew G Knepley @*/
34660adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
34670adebc6cSBarry Smith {
346888ed4aceSMatthew G Knepley   PetscInt       nroots;
346988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
347088ed4aceSMatthew G Knepley 
347188ed4aceSMatthew G Knepley   PetscFunctionBegin;
347288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
347388ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34740298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
347588ed4aceSMatthew G Knepley   if (nroots < 0) {
347688ed4aceSMatthew G Knepley     PetscSection section, gSection;
347788ed4aceSMatthew G Knepley 
347888ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
347931ea6d37SMatthew G Knepley     if (section) {
348088ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
348188ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
348231ea6d37SMatthew G Knepley     } else {
34830298fd71SBarry Smith       *sf = NULL;
348431ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
348531ea6d37SMatthew G Knepley     }
348688ed4aceSMatthew G Knepley   }
348788ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
348888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
348988ed4aceSMatthew G Knepley }
349088ed4aceSMatthew G Knepley 
349188ed4aceSMatthew G Knepley #undef __FUNCT__
349288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
349388ed4aceSMatthew G Knepley /*@
349488ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
349588ed4aceSMatthew G Knepley 
349688ed4aceSMatthew G Knepley   Input Parameters:
349788ed4aceSMatthew G Knepley + dm - The DM
349888ed4aceSMatthew G Knepley - sf - The PetscSF
349988ed4aceSMatthew G Knepley 
350088ed4aceSMatthew G Knepley   Level: intermediate
350188ed4aceSMatthew G Knepley 
350288ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
350388ed4aceSMatthew G Knepley 
350488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
350588ed4aceSMatthew G Knepley @*/
35060adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
35070adebc6cSBarry Smith {
350888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
350988ed4aceSMatthew G Knepley 
351088ed4aceSMatthew G Knepley   PetscFunctionBegin;
351188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
351288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
351388ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
351488ed4aceSMatthew G Knepley   dm->defaultSF = sf;
351588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
351688ed4aceSMatthew G Knepley }
351788ed4aceSMatthew G Knepley 
351888ed4aceSMatthew G Knepley #undef __FUNCT__
351988ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
352088ed4aceSMatthew G Knepley /*@C
352188ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
352288ed4aceSMatthew G Knepley   describing the data layout.
352388ed4aceSMatthew G Knepley 
352488ed4aceSMatthew G Knepley   Input Parameters:
352588ed4aceSMatthew G Knepley + dm - The DM
352688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
352788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
352888ed4aceSMatthew G Knepley 
352988ed4aceSMatthew G Knepley   Level: intermediate
353088ed4aceSMatthew G Knepley 
353188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
353288ed4aceSMatthew G Knepley @*/
353388ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
353488ed4aceSMatthew G Knepley {
353582f516ccSBarry Smith   MPI_Comm       comm;
353688ed4aceSMatthew G Knepley   PetscLayout    layout;
353788ed4aceSMatthew G Knepley   const PetscInt *ranges;
353888ed4aceSMatthew G Knepley   PetscInt       *local;
353988ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3540ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
354188ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
354288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
354388ed4aceSMatthew G Knepley 
354488ed4aceSMatthew G Knepley   PetscFunctionBegin;
354582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
354688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
354788ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
354888ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
354988ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
355088ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
355188ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
355288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
355388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
355488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
355588ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3556ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35576636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
355888ed4aceSMatthew G Knepley 
35596636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35606636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3561235fbf56SMatthew 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));
35626636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
356388ed4aceSMatthew G Knepley   }
3564785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3565785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
356688ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
35671f588964SMatthew G Knepley     const PetscInt *cind;
35686636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
356988ed4aceSMatthew G Knepley 
357088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
357188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
357288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
357388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
357488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35756636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
357688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35776636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35786636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35796636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3580057b4bcdSMatthew 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);
35816636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35826636e97aSMatthew G Knepley     }
358388ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
358488ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
358588ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
358688ed4aceSMatthew G Knepley     }
358788ed4aceSMatthew G Knepley     if (gdof < 0) {
35886636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
358988ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
359088ed4aceSMatthew G Knepley 
359105376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
359231d3f06eSJed Brown         if (r < 0) r = -(r+2);
359305376888SMatthew 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);
359488ed4aceSMatthew G Knepley         remote[l].rank  = r;
359588ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
359688ed4aceSMatthew G Knepley       }
359788ed4aceSMatthew G Knepley     } else {
35986636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
359988ed4aceSMatthew G Knepley         remote[l].rank  = rank;
360088ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
360188ed4aceSMatthew G Knepley       }
360288ed4aceSMatthew G Knepley     }
360388ed4aceSMatthew G Knepley   }
36046636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
360588ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
360688ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
360788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
360888ed4aceSMatthew G Knepley }
3609af122d2aSMatthew G Knepley 
3610af122d2aSMatthew G Knepley #undef __FUNCT__
3611b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3612b21d0597SMatthew G Knepley /*@
3613b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3614b21d0597SMatthew G Knepley 
3615b21d0597SMatthew G Knepley   Input Parameter:
3616b21d0597SMatthew G Knepley . dm - The DM
3617b21d0597SMatthew G Knepley 
3618b21d0597SMatthew G Knepley   Output Parameter:
3619b21d0597SMatthew G Knepley . sf - The PetscSF
3620b21d0597SMatthew G Knepley 
3621b21d0597SMatthew G Knepley   Level: intermediate
3622b21d0597SMatthew G Knepley 
3623b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3624b21d0597SMatthew G Knepley 
3625057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3626b21d0597SMatthew G Knepley @*/
36270adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
36280adebc6cSBarry Smith {
3629b21d0597SMatthew G Knepley   PetscFunctionBegin;
3630b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3631b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3632b21d0597SMatthew G Knepley   *sf = dm->sf;
3633b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3634b21d0597SMatthew G Knepley }
3635b21d0597SMatthew G Knepley 
3636b21d0597SMatthew G Knepley #undef __FUNCT__
3637057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3638057b4bcdSMatthew G Knepley /*@
3639057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3640057b4bcdSMatthew G Knepley 
3641057b4bcdSMatthew G Knepley   Input Parameters:
3642057b4bcdSMatthew G Knepley + dm - The DM
3643057b4bcdSMatthew G Knepley - sf - The PetscSF
3644057b4bcdSMatthew G Knepley 
3645057b4bcdSMatthew G Knepley   Level: intermediate
3646057b4bcdSMatthew G Knepley 
3647057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3648057b4bcdSMatthew G Knepley @*/
36490adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
36500adebc6cSBarry Smith {
3651057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3652057b4bcdSMatthew G Knepley 
3653057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3654057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3655057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3656057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3657057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3658057b4bcdSMatthew G Knepley   dm->sf = sf;
3659057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3660057b4bcdSMatthew G Knepley }
3661057b4bcdSMatthew G Knepley 
3662057b4bcdSMatthew G Knepley #undef __FUNCT__
36632764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
36642764a2aaSMatthew G. Knepley /*@
36652764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
36662764a2aaSMatthew G. Knepley 
36672764a2aaSMatthew G. Knepley   Input Parameter:
36682764a2aaSMatthew G. Knepley . dm - The DM
36692764a2aaSMatthew G. Knepley 
36702764a2aaSMatthew G. Knepley   Output Parameter:
36712764a2aaSMatthew G. Knepley . prob - The PetscDS
36722764a2aaSMatthew G. Knepley 
36732764a2aaSMatthew G. Knepley   Level: developer
36742764a2aaSMatthew G. Knepley 
36752764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36762764a2aaSMatthew G. Knepley @*/
36772764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3678af122d2aSMatthew G Knepley {
3679af122d2aSMatthew G Knepley   PetscFunctionBegin;
3680af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36810f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36820f21e855SMatthew G. Knepley   *prob = dm->prob;
36830f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36840f21e855SMatthew G. Knepley }
36850f21e855SMatthew G. Knepley 
36860f21e855SMatthew G. Knepley #undef __FUNCT__
36872764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
36882764a2aaSMatthew G. Knepley /*@
36892764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36902764a2aaSMatthew G. Knepley 
36912764a2aaSMatthew G. Knepley   Input Parameters:
36922764a2aaSMatthew G. Knepley + dm - The DM
36932764a2aaSMatthew G. Knepley - prob - The PetscDS
36942764a2aaSMatthew G. Knepley 
36952764a2aaSMatthew G. Knepley   Level: developer
36962764a2aaSMatthew G. Knepley 
36972764a2aaSMatthew G. Knepley .seealso: DMGetDS()
36982764a2aaSMatthew G. Knepley @*/
36992764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
37000f21e855SMatthew G. Knepley {
37010f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37020f21e855SMatthew G. Knepley 
37030f21e855SMatthew G. Knepley   PetscFunctionBegin;
37040f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37052764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
37062764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
37070f21e855SMatthew G. Knepley   dm->prob = prob;
37080f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
37090f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
37100f21e855SMatthew G. Knepley }
37110f21e855SMatthew G. Knepley 
37120f21e855SMatthew G. Knepley #undef __FUNCT__
37130f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
37140f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
37150f21e855SMatthew G. Knepley {
37160f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37170f21e855SMatthew G. Knepley 
37180f21e855SMatthew G. Knepley   PetscFunctionBegin;
37190f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37202764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3721af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3722af122d2aSMatthew G Knepley }
3723af122d2aSMatthew G Knepley 
3724af122d2aSMatthew G Knepley #undef __FUNCT__
3725af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3726af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3727af122d2aSMatthew G Knepley {
37280f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3729af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3730af122d2aSMatthew G Knepley 
3731af122d2aSMatthew G Knepley   PetscFunctionBegin;
3732af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37332764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
37340f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
37350f21e855SMatthew G. Knepley     PetscContainer obj;
37360f21e855SMatthew G. Knepley 
37370f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
37382764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
37390f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3740af122d2aSMatthew G Knepley   }
3741af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3742af122d2aSMatthew G Knepley }
3743af122d2aSMatthew G Knepley 
3744af122d2aSMatthew G Knepley #undef __FUNCT__
3745af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3746c1929be8SMatthew G. Knepley /*@
3747c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3748c1929be8SMatthew G. Knepley 
3749c1929be8SMatthew G. Knepley   Not collective
3750c1929be8SMatthew G. Knepley 
3751c1929be8SMatthew G. Knepley   Input Parameters:
3752c1929be8SMatthew G. Knepley + dm - The DM
3753c1929be8SMatthew G. Knepley - f  - The field number
3754c1929be8SMatthew G. Knepley 
3755c1929be8SMatthew G. Knepley   Output Parameter:
3756c1929be8SMatthew G. Knepley . field - The discretization object
3757c1929be8SMatthew G. Knepley 
3758c1929be8SMatthew G. Knepley   Level: developer
3759c1929be8SMatthew G. Knepley 
3760c1929be8SMatthew G. Knepley .seealso: DMSetField()
3761c1929be8SMatthew G. Knepley @*/
3762af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3763af122d2aSMatthew G Knepley {
37640f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37650f21e855SMatthew G. Knepley 
3766af122d2aSMatthew G Knepley   PetscFunctionBegin;
3767af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37682764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3769decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3770decb47aaSMatthew G. Knepley }
3771decb47aaSMatthew G. Knepley 
3772decb47aaSMatthew G. Knepley #undef __FUNCT__
3773decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3774c1929be8SMatthew G. Knepley /*@
3775c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3776c1929be8SMatthew G. Knepley 
3777c1929be8SMatthew G. Knepley   Logically collective on DM
3778c1929be8SMatthew G. Knepley 
3779c1929be8SMatthew G. Knepley   Input Parameters:
3780c1929be8SMatthew G. Knepley + dm - The DM
3781c1929be8SMatthew G. Knepley . f  - The field number
3782c1929be8SMatthew G. Knepley - field - The discretization object
3783c1929be8SMatthew G. Knepley 
3784c1929be8SMatthew G. Knepley   Level: developer
3785c1929be8SMatthew G. Knepley 
3786c1929be8SMatthew G. Knepley .seealso: DMGetField()
3787c1929be8SMatthew G. Knepley @*/
3788decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3789decb47aaSMatthew G. Knepley {
3790decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3791decb47aaSMatthew G. Knepley 
3792decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3793decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37942764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3795af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3796af122d2aSMatthew G Knepley }
37976636e97aSMatthew G Knepley 
37986636e97aSMatthew G Knepley #undef __FUNCT__
3799b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3800b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3801b64e0483SPeter Brune {
3802b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3803b64e0483SPeter Brune   PetscErrorCode ierr;
3804b64e0483SPeter Brune   Vec coords,ccoords;
38056dbf9973SLawrence Mitchell   Mat inject;
3806b64e0483SPeter Brune   PetscFunctionBegin;
3807b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3808b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3809b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3810b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3811b64e0483SPeter Brune   if (coords && !ccoords) {
3812b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
38136dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
38142adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
38156dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3816b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3817b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3818b64e0483SPeter Brune   }
3819b64e0483SPeter Brune   PetscFunctionReturn(0);
3820b64e0483SPeter Brune }
3821b64e0483SPeter Brune 
3822b64e0483SPeter Brune #undef __FUNCT__
382303dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
382403dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
382503dadc2fSPeter Brune {
382603dadc2fSPeter Brune   DM dm_coord,subdm_coord;
382703dadc2fSPeter Brune   PetscErrorCode ierr;
382803dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
382903dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
383003dadc2fSPeter Brune   PetscFunctionBegin;
383103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
383203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
383303dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
383403dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
383503dadc2fSPeter Brune   if (coords && !ccoords) {
383603dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
383703dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
383824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
383903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
384003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
384403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
384503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
384603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
384703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
384803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
384903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
385003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
385103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
385203dadc2fSPeter Brune   }
385303dadc2fSPeter Brune   PetscFunctionReturn(0);
385403dadc2fSPeter Brune }
385503dadc2fSPeter Brune 
385603dadc2fSPeter Brune #undef __FUNCT__
3857c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3858c73cfb54SMatthew G. Knepley /*@
3859c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3860c73cfb54SMatthew G. Knepley 
3861c73cfb54SMatthew G. Knepley   Not collective
3862c73cfb54SMatthew G. Knepley 
3863c73cfb54SMatthew G. Knepley   Input Parameter:
3864c73cfb54SMatthew G. Knepley . dm - The DM
3865c73cfb54SMatthew G. Knepley 
3866c73cfb54SMatthew G. Knepley   Output Parameter:
3867c73cfb54SMatthew G. Knepley . dim - The topological dimension
3868c73cfb54SMatthew G. Knepley 
3869c73cfb54SMatthew G. Knepley   Level: beginner
3870c73cfb54SMatthew G. Knepley 
3871c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3872c73cfb54SMatthew G. Knepley @*/
3873c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3874c73cfb54SMatthew G. Knepley {
3875c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3876c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3877c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3878c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3879c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3880c73cfb54SMatthew G. Knepley }
3881c73cfb54SMatthew G. Knepley 
3882c73cfb54SMatthew G. Knepley #undef __FUNCT__
3883c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3884c73cfb54SMatthew G. Knepley /*@
3885c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3886c73cfb54SMatthew G. Knepley 
3887c73cfb54SMatthew G. Knepley   Collective on dm
3888c73cfb54SMatthew G. Knepley 
3889c73cfb54SMatthew G. Knepley   Input Parameters:
3890c73cfb54SMatthew G. Knepley + dm - The DM
3891c73cfb54SMatthew G. Knepley - dim - The topological dimension
3892c73cfb54SMatthew G. Knepley 
3893c73cfb54SMatthew G. Knepley   Level: beginner
3894c73cfb54SMatthew G. Knepley 
3895c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3896c73cfb54SMatthew G. Knepley @*/
3897c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3898c73cfb54SMatthew G. Knepley {
3899c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3900c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3901c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3902c73cfb54SMatthew G. Knepley   dm->dim = dim;
3903c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3904c73cfb54SMatthew G. Knepley }
3905c73cfb54SMatthew G. Knepley 
3906793f3fe5SMatthew G. Knepley #undef __FUNCT__
3907793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3908793f3fe5SMatthew G. Knepley /*@
3909793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3910793f3fe5SMatthew G. Knepley 
3911793f3fe5SMatthew G. Knepley   Collective on DM
3912793f3fe5SMatthew G. Knepley 
3913793f3fe5SMatthew G. Knepley   Input Parameters:
3914793f3fe5SMatthew G. Knepley + dm - the DM
3915793f3fe5SMatthew G. Knepley - dim - the dimension
3916793f3fe5SMatthew G. Knepley 
3917793f3fe5SMatthew G. Knepley   Output Parameters:
3918793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3919793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3920793f3fe5SMatthew G. Knepley 
3921793f3fe5SMatthew G. Knepley   Note:
3922793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3923793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3924793f3fe5SMatthew G. Knepley   then the interval is empty.
3925793f3fe5SMatthew G. Knepley 
3926793f3fe5SMatthew G. Knepley   Level: intermediate
3927793f3fe5SMatthew G. Knepley 
3928793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3929793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3930793f3fe5SMatthew G. Knepley @*/
3931793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3932793f3fe5SMatthew G. Knepley {
3933793f3fe5SMatthew G. Knepley   PetscInt       d;
3934793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3935793f3fe5SMatthew G. Knepley 
3936793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3937793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3938793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3939793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3940793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3941793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3942793f3fe5SMatthew G. Knepley }
3943793f3fe5SMatthew G. Knepley 
3944793f3fe5SMatthew G. Knepley #undef __FUNCT__
39456636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
39466636e97aSMatthew G Knepley /*@
39476636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
39486636e97aSMatthew G Knepley 
39496636e97aSMatthew G Knepley   Collective on DM
39506636e97aSMatthew G Knepley 
39516636e97aSMatthew G Knepley   Input Parameters:
39526636e97aSMatthew G Knepley + dm - the DM
39536636e97aSMatthew G Knepley - c - coordinate vector
39546636e97aSMatthew G Knepley 
39556636e97aSMatthew G Knepley   Note:
39566636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
39576636e97aSMatthew G Knepley 
39586636e97aSMatthew G Knepley   Level: intermediate
39596636e97aSMatthew G Knepley 
39606636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39616636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
39626636e97aSMatthew G Knepley @*/
39636636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
39646636e97aSMatthew G Knepley {
39656636e97aSMatthew G Knepley   PetscErrorCode ierr;
39666636e97aSMatthew G Knepley 
39676636e97aSMatthew G Knepley   PetscFunctionBegin;
39686636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39696636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39706636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39716636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39726636e97aSMatthew G Knepley   dm->coordinates = c;
39736636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3974b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
397503dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39766636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39776636e97aSMatthew G Knepley }
39786636e97aSMatthew G Knepley 
39796636e97aSMatthew G Knepley #undef __FUNCT__
39806636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
39816636e97aSMatthew G Knepley /*@
39826636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39836636e97aSMatthew G Knepley 
39846636e97aSMatthew G Knepley   Collective on DM
39856636e97aSMatthew G Knepley 
39866636e97aSMatthew G Knepley    Input Parameters:
39876636e97aSMatthew G Knepley +  dm - the DM
39886636e97aSMatthew G Knepley -  c - coordinate vector
39896636e97aSMatthew G Knepley 
39906636e97aSMatthew G Knepley   Note:
39916636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39926636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39936636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39946636e97aSMatthew G Knepley 
39956636e97aSMatthew G Knepley   Level: intermediate
39966636e97aSMatthew G Knepley 
39976636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39986636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
39996636e97aSMatthew G Knepley @*/
40006636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
40016636e97aSMatthew G Knepley {
40026636e97aSMatthew G Knepley   PetscErrorCode ierr;
40036636e97aSMatthew G Knepley 
40046636e97aSMatthew G Knepley   PetscFunctionBegin;
40056636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
40076636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
40086636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
40098865f1eaSKarl Rupp 
40106636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
40118865f1eaSKarl Rupp 
40126636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
40136636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40146636e97aSMatthew G Knepley }
40156636e97aSMatthew G Knepley 
40166636e97aSMatthew G Knepley #undef __FUNCT__
40176636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
40186636e97aSMatthew G Knepley /*@
40196636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
40206636e97aSMatthew G Knepley 
40216636e97aSMatthew G Knepley   Not Collective
40226636e97aSMatthew G Knepley 
40236636e97aSMatthew G Knepley   Input Parameter:
40246636e97aSMatthew G Knepley . dm - the DM
40256636e97aSMatthew G Knepley 
40266636e97aSMatthew G Knepley   Output Parameter:
40276636e97aSMatthew G Knepley . c - global coordinate vector
40286636e97aSMatthew G Knepley 
40296636e97aSMatthew G Knepley   Note:
40306636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40316636e97aSMatthew G Knepley 
40326636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
40336636e97aSMatthew G Knepley 
40346636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40356636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40366636e97aSMatthew G Knepley 
40376636e97aSMatthew G Knepley   Level: intermediate
40386636e97aSMatthew G Knepley 
40396636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40406636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
40416636e97aSMatthew G Knepley @*/
40426636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
40436636e97aSMatthew G Knepley {
40446636e97aSMatthew G Knepley   PetscErrorCode ierr;
40456636e97aSMatthew G Knepley 
40466636e97aSMatthew G Knepley   PetscFunctionBegin;
40476636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40486636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40491f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
40500298fd71SBarry Smith     DM cdm = NULL;
40516636e97aSMatthew G Knepley 
40526636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40536636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
40546636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
40556636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40566636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40576636e97aSMatthew G Knepley   }
40586636e97aSMatthew G Knepley   *c = dm->coordinates;
40596636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40606636e97aSMatthew G Knepley }
40616636e97aSMatthew G Knepley 
40626636e97aSMatthew G Knepley #undef __FUNCT__
40636636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
40646636e97aSMatthew G Knepley /*@
40656636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
40666636e97aSMatthew G Knepley 
40676636e97aSMatthew G Knepley   Collective on DM
40686636e97aSMatthew G Knepley 
40696636e97aSMatthew G Knepley   Input Parameter:
40706636e97aSMatthew G Knepley . dm - the DM
40716636e97aSMatthew G Knepley 
40726636e97aSMatthew G Knepley   Output Parameter:
40736636e97aSMatthew G Knepley . c - coordinate vector
40746636e97aSMatthew G Knepley 
40756636e97aSMatthew G Knepley   Note:
40766636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40776636e97aSMatthew G Knepley 
40786636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40796636e97aSMatthew G Knepley 
40806636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40816636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40826636e97aSMatthew G Knepley 
40836636e97aSMatthew G Knepley   Level: intermediate
40846636e97aSMatthew G Knepley 
40856636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40866636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40876636e97aSMatthew G Knepley @*/
40886636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40896636e97aSMatthew G Knepley {
40906636e97aSMatthew G Knepley   PetscErrorCode ierr;
40916636e97aSMatthew G Knepley 
40926636e97aSMatthew G Knepley   PetscFunctionBegin;
40936636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40946636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40951f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40960298fd71SBarry Smith     DM cdm = NULL;
40976636e97aSMatthew G Knepley 
40986636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40996636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
41006636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
41016636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41026636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41036636e97aSMatthew G Knepley   }
41046636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
41056636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41066636e97aSMatthew G Knepley }
41076636e97aSMatthew G Knepley 
41086636e97aSMatthew G Knepley #undef __FUNCT__
41096636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
41106636e97aSMatthew G Knepley /*@
41111cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
41126636e97aSMatthew G Knepley 
41136636e97aSMatthew G Knepley   Collective on DM
41146636e97aSMatthew G Knepley 
41156636e97aSMatthew G Knepley   Input Parameter:
41166636e97aSMatthew G Knepley . dm - the DM
41176636e97aSMatthew G Knepley 
41186636e97aSMatthew G Knepley   Output Parameter:
41196636e97aSMatthew G Knepley . cdm - coordinate DM
41206636e97aSMatthew G Knepley 
41216636e97aSMatthew G Knepley   Level: intermediate
41226636e97aSMatthew G Knepley 
41236636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41241cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41256636e97aSMatthew G Knepley @*/
41266636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
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(cdm,2);
41336636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
413482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
41356636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
41366636e97aSMatthew G Knepley   }
41376636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
41386636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41396636e97aSMatthew G Knepley }
4140e87bb0d3SMatthew G Knepley 
4141e87bb0d3SMatthew G Knepley #undef __FUNCT__
41421cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
41431cfe2091SMatthew G. Knepley /*@
41441cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
41451cfe2091SMatthew G. Knepley 
41461cfe2091SMatthew G. Knepley   Logically Collective on DM
41471cfe2091SMatthew G. Knepley 
41481cfe2091SMatthew G. Knepley   Input Parameters:
41491cfe2091SMatthew G. Knepley + dm - the DM
41501cfe2091SMatthew G. Knepley - cdm - coordinate DM
41511cfe2091SMatthew G. Knepley 
41521cfe2091SMatthew G. Knepley   Level: intermediate
41531cfe2091SMatthew G. Knepley 
41541cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41551cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41561cfe2091SMatthew G. Knepley @*/
41571cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
41581cfe2091SMatthew G. Knepley {
41591cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
41601cfe2091SMatthew G. Knepley 
41611cfe2091SMatthew G. Knepley   PetscFunctionBegin;
41621cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41631cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
41641cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
41651cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
41661cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
41671cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
41681cfe2091SMatthew G. Knepley }
41691cfe2091SMatthew G. Knepley 
41701cfe2091SMatthew G. Knepley #undef __FUNCT__
417146e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
417246e270d4SMatthew G. Knepley /*@
417346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
417446e270d4SMatthew G. Knepley 
417546e270d4SMatthew G. Knepley   Not Collective
417646e270d4SMatthew G. Knepley 
417746e270d4SMatthew G. Knepley   Input Parameter:
417846e270d4SMatthew G. Knepley . dm - The DM object
417946e270d4SMatthew G. Knepley 
418046e270d4SMatthew G. Knepley   Output Parameter:
418146e270d4SMatthew G. Knepley . dim - The embedding dimension
418246e270d4SMatthew G. Knepley 
418346e270d4SMatthew G. Knepley   Level: intermediate
418446e270d4SMatthew G. Knepley 
418546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
418646e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
418746e270d4SMatthew G. Knepley @*/
418846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
418946e270d4SMatthew G. Knepley {
419046e270d4SMatthew G. Knepley   PetscFunctionBegin;
419146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
419246e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41939a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41949a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41959a9a41abSToby Isaac   }
419646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
419746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
419846e270d4SMatthew G. Knepley }
419946e270d4SMatthew G. Knepley 
420046e270d4SMatthew G. Knepley #undef __FUNCT__
420146e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
420246e270d4SMatthew G. Knepley /*@
420346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
420446e270d4SMatthew G. Knepley 
420546e270d4SMatthew G. Knepley   Not Collective
420646e270d4SMatthew G. Knepley 
420746e270d4SMatthew G. Knepley   Input Parameters:
420846e270d4SMatthew G. Knepley + dm  - The DM object
420946e270d4SMatthew G. Knepley - dim - The embedding dimension
421046e270d4SMatthew G. Knepley 
421146e270d4SMatthew G. Knepley   Level: intermediate
421246e270d4SMatthew G. Knepley 
421346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
421446e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
421546e270d4SMatthew G. Knepley @*/
421646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
421746e270d4SMatthew G. Knepley {
421846e270d4SMatthew G. Knepley   PetscFunctionBegin;
421946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
422046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
422146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
422246e270d4SMatthew G. Knepley }
422346e270d4SMatthew G. Knepley 
422446e270d4SMatthew G. Knepley #undef __FUNCT__
4225e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4226e8abe2deSMatthew G. Knepley /*@
4227e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4228e8abe2deSMatthew G. Knepley 
4229e8abe2deSMatthew G. Knepley   Not Collective
4230e8abe2deSMatthew G. Knepley 
4231e8abe2deSMatthew G. Knepley   Input Parameter:
4232e8abe2deSMatthew G. Knepley . dm - The DM object
4233e8abe2deSMatthew G. Knepley 
4234e8abe2deSMatthew G. Knepley   Output Parameter:
4235e8abe2deSMatthew G. Knepley . section - The PetscSection object
4236e8abe2deSMatthew G. Knepley 
4237e8abe2deSMatthew G. Knepley   Level: intermediate
4238e8abe2deSMatthew G. Knepley 
4239e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4240e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4241e8abe2deSMatthew G. Knepley @*/
4242e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4243e8abe2deSMatthew G. Knepley {
4244e8abe2deSMatthew G. Knepley   DM             cdm;
4245e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4246e8abe2deSMatthew G. Knepley 
4247e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4248e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4249e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4250e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4251e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4252e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4253e8abe2deSMatthew G. Knepley }
4254e8abe2deSMatthew G. Knepley 
4255e8abe2deSMatthew G. Knepley #undef __FUNCT__
4256e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4257e8abe2deSMatthew G. Knepley /*@
4258e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4259e8abe2deSMatthew G. Knepley 
4260e8abe2deSMatthew G. Knepley   Not Collective
4261e8abe2deSMatthew G. Knepley 
4262e8abe2deSMatthew G. Knepley   Input Parameters:
4263e8abe2deSMatthew G. Knepley + dm      - The DM object
426446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4265e8abe2deSMatthew G. Knepley - section - The PetscSection object
4266e8abe2deSMatthew G. Knepley 
4267e8abe2deSMatthew G. Knepley   Level: intermediate
4268e8abe2deSMatthew G. Knepley 
4269e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4270e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4271e8abe2deSMatthew G. Knepley @*/
427246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4273e8abe2deSMatthew G. Knepley {
4274e8abe2deSMatthew G. Knepley   DM             cdm;
4275e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4276e8abe2deSMatthew G. Knepley 
4277e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4278e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
427946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4280e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4281e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
428246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
428346e270d4SMatthew G. Knepley     PetscInt d = dim;
428446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
428546e270d4SMatthew G. Knepley 
428646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
428746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
428846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
428946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
429046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
429146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
429246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
429346e270d4SMatthew G. Knepley     }
429446e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
429546e270d4SMatthew G. Knepley   }
4296e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4297e8abe2deSMatthew G. Knepley }
4298e8abe2deSMatthew G. Knepley 
4299e8abe2deSMatthew G. Knepley #undef __FUNCT__
4300c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
43015dc8c3f7SMatthew G. Knepley /*@C
43025dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
43035dc8c3f7SMatthew G. Knepley 
43045dc8c3f7SMatthew G. Knepley   Input Parameters:
43055dc8c3f7SMatthew G. Knepley + dm      - The DM object
43065dc8c3f7SMatthew 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
43075dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43085dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43095dc8c3f7SMatthew G. Knepley 
43105dc8c3f7SMatthew G. Knepley   Level: developer
43115dc8c3f7SMatthew G. Knepley 
43125dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43135dc8c3f7SMatthew G. Knepley @*/
43145dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4315c6b900c6SMatthew G. Knepley {
4316c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4317c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4318c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4319c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
43205dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4321c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4322c6b900c6SMatthew G. Knepley }
4323c6b900c6SMatthew G. Knepley 
4324c6b900c6SMatthew G. Knepley #undef __FUNCT__
4325c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
43265dc8c3f7SMatthew G. Knepley /*@C
43275dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
43285dc8c3f7SMatthew G. Knepley 
43295dc8c3f7SMatthew G. Knepley   Input Parameters:
43305dc8c3f7SMatthew G. Knepley + dm      - The DM object
43315dc8c3f7SMatthew 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
43325dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43335dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43345dc8c3f7SMatthew G. Knepley 
43355dc8c3f7SMatthew G. Knepley   Level: developer
43365dc8c3f7SMatthew G. Knepley 
43375dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43385dc8c3f7SMatthew G. Knepley @*/
43395dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4340c6b900c6SMatthew G. Knepley {
4341c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4342c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4343c6b900c6SMatthew G. Knepley 
4344c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4345c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43465dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
43475dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
43485dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
43495dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
43505dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4351c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4352c6b900c6SMatthew G. Knepley }
4353c6b900c6SMatthew G. Knepley 
4354c6b900c6SMatthew G. Knepley #undef __FUNCT__
4355e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4356e87bb0d3SMatthew G Knepley /*@
4357e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4358e87bb0d3SMatthew G Knepley 
4359e87bb0d3SMatthew G Knepley   Not collective
4360e87bb0d3SMatthew G Knepley 
4361e87bb0d3SMatthew G Knepley   Input Parameters:
4362e87bb0d3SMatthew G Knepley + dm - The DM
4363e87bb0d3SMatthew G Knepley - v - The Vec of points
4364e87bb0d3SMatthew G Knepley 
436561e3bb9bSMatthew G Knepley   Output Parameter:
4366e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4367e87bb0d3SMatthew G Knepley 
4368e87bb0d3SMatthew G Knepley   Level: developer
436961e3bb9bSMatthew G Knepley 
437061e3bb9bSMatthew G Knepley .keywords: point location, mesh
437161e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
437261e3bb9bSMatthew G Knepley @*/
4373e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4374e87bb0d3SMatthew G Knepley {
4375735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4376735aa83eSMatthew G Knepley 
4377e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4378e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4379e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4380e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
438147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4382735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4383735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
438482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
438547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4386e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4387e87bb0d3SMatthew G Knepley }
438814f150ffSMatthew G. Knepley 
438914f150ffSMatthew G. Knepley #undef __FUNCT__
439014f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4391f4d763aaSMatthew G. Knepley /*@
4392f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4393f4d763aaSMatthew G. Knepley 
4394f4d763aaSMatthew G. Knepley   Input Parameter:
4395f4d763aaSMatthew G. Knepley . dm - The original DM
4396f4d763aaSMatthew G. Knepley 
4397f4d763aaSMatthew G. Knepley   Output Parameter:
4398f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4399f4d763aaSMatthew G. Knepley 
4400f4d763aaSMatthew G. Knepley   Level: intermediate
4401f4d763aaSMatthew G. Knepley 
4402f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4403f4d763aaSMatthew G. Knepley @*/
440414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
440514f150ffSMatthew G. Knepley {
4406c26acbdeSMatthew G. Knepley   PetscSection   section;
4407c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
440814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
440914f150ffSMatthew G. Knepley 
441014f150ffSMatthew G. Knepley   PetscFunctionBegin;
441114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
441214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4413c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4414c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4415c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4416c26acbdeSMatthew G. Knepley     *odm = dm;
4417c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4418c26acbdeSMatthew G. Knepley   }
441914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4420c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
442114f150ffSMatthew G. Knepley     PetscSF      sf;
442214f150ffSMatthew G. Knepley 
442314f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
442414f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
442514f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
442614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
442714f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
442815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
442914f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
443014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
443114f150ffSMatthew G. Knepley   }
443214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
443314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
443414f150ffSMatthew G. Knepley }
4435f4d763aaSMatthew G. Knepley 
4436f4d763aaSMatthew G. Knepley #undef __FUNCT__
4437f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4438f4d763aaSMatthew G. Knepley /*@
4439cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4440f4d763aaSMatthew G. Knepley 
4441f4d763aaSMatthew G. Knepley   Input Parameter:
4442f4d763aaSMatthew G. Knepley . dm - The original DM
4443f4d763aaSMatthew G. Knepley 
4444cdb7a50dSMatthew G. Knepley   Output Parameters:
4445cdb7a50dSMatthew G. Knepley + num - The output sequence number
4446cdb7a50dSMatthew G. Knepley - val - The output sequence value
4447f4d763aaSMatthew G. Knepley 
4448f4d763aaSMatthew G. Knepley   Level: intermediate
4449f4d763aaSMatthew G. Knepley 
4450f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4451f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4452f4d763aaSMatthew G. Knepley 
4453f4d763aaSMatthew G. Knepley .seealso: VecView()
4454f4d763aaSMatthew G. Knepley @*/
4455cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4456f4d763aaSMatthew G. Knepley {
4457f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4458f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4459cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4460cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4461f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4462f4d763aaSMatthew G. Knepley }
4463f4d763aaSMatthew G. Knepley 
4464f4d763aaSMatthew G. Knepley #undef __FUNCT__
4465f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4466f4d763aaSMatthew G. Knepley /*@
4467cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4468f4d763aaSMatthew G. Knepley 
4469f4d763aaSMatthew G. Knepley   Input Parameters:
4470f4d763aaSMatthew G. Knepley + dm - The original DM
4471cdb7a50dSMatthew G. Knepley . num - The output sequence number
4472cdb7a50dSMatthew G. Knepley - val - The output sequence value
4473f4d763aaSMatthew G. Knepley 
4474f4d763aaSMatthew G. Knepley   Level: intermediate
4475f4d763aaSMatthew G. Knepley 
4476f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4477f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4478f4d763aaSMatthew G. Knepley 
4479f4d763aaSMatthew G. Knepley .seealso: VecView()
4480f4d763aaSMatthew G. Knepley @*/
4481cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4482f4d763aaSMatthew G. Knepley {
4483f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4484f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4485f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4486cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4487cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4488cdb7a50dSMatthew G. Knepley }
4489cdb7a50dSMatthew G. Knepley 
4490cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4491cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4492cdb7a50dSMatthew G. Knepley /*@C
4493cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4494cdb7a50dSMatthew G. Knepley 
4495cdb7a50dSMatthew G. Knepley   Input Parameters:
4496cdb7a50dSMatthew G. Knepley + dm   - The original DM
4497cdb7a50dSMatthew G. Knepley . name - The sequence name
4498cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4499cdb7a50dSMatthew G. Knepley 
4500cdb7a50dSMatthew G. Knepley   Output Parameter:
4501cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4502cdb7a50dSMatthew G. Knepley 
4503cdb7a50dSMatthew G. Knepley   Level: intermediate
4504cdb7a50dSMatthew G. Knepley 
4505cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4506cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4507cdb7a50dSMatthew G. Knepley 
4508cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4509cdb7a50dSMatthew G. Knepley @*/
4510cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4511cdb7a50dSMatthew G. Knepley {
4512cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4513cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4514cdb7a50dSMatthew G. Knepley 
4515cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4516cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4517cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4518cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4519cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4520cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4521cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4522cdb7a50dSMatthew G. Knepley     PetscScalar value;
4523cdb7a50dSMatthew G. Knepley 
4524cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
45254aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4526cdb7a50dSMatthew G. Knepley #endif
4527cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4528f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4529f4d763aaSMatthew G. Knepley }
45308e4ac7eaSMatthew G. Knepley 
45318e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45328e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
45338e4ac7eaSMatthew G. Knepley /*@
45348e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
45358e4ac7eaSMatthew G. Knepley 
45368e4ac7eaSMatthew G. Knepley   Not collective
45378e4ac7eaSMatthew G. Knepley 
45388e4ac7eaSMatthew G. Knepley   Input Parameter:
45398e4ac7eaSMatthew G. Knepley . dm - The DM
45408e4ac7eaSMatthew G. Knepley 
45418e4ac7eaSMatthew G. Knepley   Output Parameter:
45428e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
45438e4ac7eaSMatthew G. Knepley 
45448e4ac7eaSMatthew G. Knepley   Level: beginner
45458e4ac7eaSMatthew G. Knepley 
45468e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
45478e4ac7eaSMatthew G. Knepley @*/
45488e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
45498e4ac7eaSMatthew G. Knepley {
45508e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45518e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45528e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
45538e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
45548e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45558e4ac7eaSMatthew G. Knepley }
45568e4ac7eaSMatthew G. Knepley 
45578e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45588e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
45598e4ac7eaSMatthew G. Knepley /*@
45608e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
45618e4ac7eaSMatthew G. Knepley 
45628e4ac7eaSMatthew G. Knepley   Collective on dm
45638e4ac7eaSMatthew G. Knepley 
45648e4ac7eaSMatthew G. Knepley   Input Parameters:
45658e4ac7eaSMatthew G. Knepley + dm - The DM
45668e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
45678e4ac7eaSMatthew G. Knepley 
45688e4ac7eaSMatthew G. Knepley   Level: beginner
45698e4ac7eaSMatthew G. Knepley 
45708e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
45718e4ac7eaSMatthew G. Knepley @*/
45728e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
45738e4ac7eaSMatthew G. Knepley {
45748e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45758e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45768e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
45778e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
45788e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45798e4ac7eaSMatthew G. Knepley }
4580c58f1c22SToby Isaac 
4581c58f1c22SToby Isaac #undef __FUNCT__
4582c58f1c22SToby Isaac #define __FUNCT__
4583c58f1c22SToby Isaac 
4584c58f1c22SToby Isaac #undef __FUNCT__
4585c58f1c22SToby Isaac #define __FUNCT__ "DMCreateLabel"
4586c58f1c22SToby Isaac /*@C
4587c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
4588c58f1c22SToby Isaac 
4589c58f1c22SToby Isaac   Not Collective
4590c58f1c22SToby Isaac 
4591c58f1c22SToby Isaac   Input Parameters:
4592c58f1c22SToby Isaac + dm   - The DM object
4593c58f1c22SToby Isaac - name - The label name
4594c58f1c22SToby Isaac 
4595c58f1c22SToby Isaac   Level: intermediate
4596c58f1c22SToby Isaac 
4597c58f1c22SToby Isaac .keywords: mesh
4598c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4599c58f1c22SToby Isaac @*/
4600c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
4601c58f1c22SToby Isaac {
4602c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
4603c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
4604c58f1c22SToby Isaac   PetscErrorCode ierr;
4605c58f1c22SToby Isaac 
4606c58f1c22SToby Isaac   PetscFunctionBegin;
4607c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4608c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4609c58f1c22SToby Isaac   while (next) {
4610c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
4611c58f1c22SToby Isaac     if (flg) break;
4612c58f1c22SToby Isaac     next = next->next;
4613c58f1c22SToby Isaac   }
4614c58f1c22SToby Isaac   if (!flg) {
4615c58f1c22SToby Isaac     DMLabelLink tmpLabel;
4616c58f1c22SToby Isaac 
4617c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
4618c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
4619c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
4620c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
4621c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
4622c58f1c22SToby Isaac   }
4623c58f1c22SToby Isaac   PetscFunctionReturn(0);
4624c58f1c22SToby Isaac }
4625c58f1c22SToby Isaac 
4626c58f1c22SToby Isaac #undef __FUNCT__
4627c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelValue"
4628c58f1c22SToby Isaac /*@C
4629c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
4630c58f1c22SToby Isaac 
4631c58f1c22SToby Isaac   Not Collective
4632c58f1c22SToby Isaac 
4633c58f1c22SToby Isaac   Input Parameters:
4634c58f1c22SToby Isaac + dm   - The DM object
4635c58f1c22SToby Isaac . name - The label name
4636c58f1c22SToby Isaac - point - The mesh point
4637c58f1c22SToby Isaac 
4638c58f1c22SToby Isaac   Output Parameter:
4639c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
4640c58f1c22SToby Isaac 
4641c58f1c22SToby Isaac   Level: beginner
4642c58f1c22SToby Isaac 
4643c58f1c22SToby Isaac .keywords: mesh
4644c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
4645c58f1c22SToby Isaac @*/
4646c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
4647c58f1c22SToby Isaac {
4648c58f1c22SToby Isaac   DMLabel        label;
4649c58f1c22SToby Isaac   PetscErrorCode ierr;
4650c58f1c22SToby Isaac 
4651c58f1c22SToby Isaac   PetscFunctionBegin;
4652c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4653c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4654c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4655c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
4656c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
4657c58f1c22SToby Isaac   PetscFunctionReturn(0);
4658c58f1c22SToby Isaac }
4659c58f1c22SToby Isaac 
4660c58f1c22SToby Isaac #undef __FUNCT__
4661c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelValue"
4662c58f1c22SToby Isaac /*@C
4663c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
4664c58f1c22SToby Isaac 
4665c58f1c22SToby Isaac   Not Collective
4666c58f1c22SToby Isaac 
4667c58f1c22SToby Isaac   Input Parameters:
4668c58f1c22SToby Isaac + dm   - The DM object
4669c58f1c22SToby Isaac . name - The label name
4670c58f1c22SToby Isaac . point - The mesh point
4671c58f1c22SToby Isaac - value - The label value for this point
4672c58f1c22SToby Isaac 
4673c58f1c22SToby Isaac   Output Parameter:
4674c58f1c22SToby Isaac 
4675c58f1c22SToby Isaac   Level: beginner
4676c58f1c22SToby Isaac 
4677c58f1c22SToby Isaac .keywords: mesh
4678c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
4679c58f1c22SToby Isaac @*/
4680c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4681c58f1c22SToby Isaac {
4682c58f1c22SToby Isaac   DMLabel        label;
4683c58f1c22SToby Isaac   PetscErrorCode ierr;
4684c58f1c22SToby Isaac 
4685c58f1c22SToby Isaac   PetscFunctionBegin;
4686c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4687c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4688c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4689c58f1c22SToby Isaac   if (!label) {
4690c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
4691c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4692c58f1c22SToby Isaac   }
4693c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
4694c58f1c22SToby Isaac   PetscFunctionReturn(0);
4695c58f1c22SToby Isaac }
4696c58f1c22SToby Isaac 
4697c58f1c22SToby Isaac #undef __FUNCT__
4698c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelValue"
4699c58f1c22SToby Isaac /*@C
4700c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
4701c58f1c22SToby Isaac 
4702c58f1c22SToby Isaac   Not Collective
4703c58f1c22SToby Isaac 
4704c58f1c22SToby Isaac   Input Parameters:
4705c58f1c22SToby Isaac + dm   - The DM object
4706c58f1c22SToby Isaac . name - The label name
4707c58f1c22SToby Isaac . point - The mesh point
4708c58f1c22SToby Isaac - value - The label value for this point
4709c58f1c22SToby Isaac 
4710c58f1c22SToby Isaac   Output Parameter:
4711c58f1c22SToby Isaac 
4712c58f1c22SToby Isaac   Level: beginner
4713c58f1c22SToby Isaac 
4714c58f1c22SToby Isaac .keywords: mesh
4715c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
4716c58f1c22SToby Isaac @*/
4717c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
4718c58f1c22SToby Isaac {
4719c58f1c22SToby Isaac   DMLabel        label;
4720c58f1c22SToby Isaac   PetscErrorCode ierr;
4721c58f1c22SToby Isaac 
4722c58f1c22SToby Isaac   PetscFunctionBegin;
4723c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4724c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4725c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4726c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4727c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
4728c58f1c22SToby Isaac   PetscFunctionReturn(0);
4729c58f1c22SToby Isaac }
4730c58f1c22SToby Isaac 
4731c58f1c22SToby Isaac #undef __FUNCT__
4732c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelSize"
4733c58f1c22SToby Isaac /*@C
4734c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
4735c58f1c22SToby Isaac 
4736c58f1c22SToby Isaac   Not Collective
4737c58f1c22SToby Isaac 
4738c58f1c22SToby Isaac   Input Parameters:
4739c58f1c22SToby Isaac + dm   - The DM object
4740c58f1c22SToby Isaac - name - The label name
4741c58f1c22SToby Isaac 
4742c58f1c22SToby Isaac   Output Parameter:
4743c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
4744c58f1c22SToby Isaac 
4745c58f1c22SToby Isaac   Level: beginner
4746c58f1c22SToby Isaac 
4747c58f1c22SToby Isaac .keywords: mesh
4748c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
4749c58f1c22SToby Isaac @*/
4750c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
4751c58f1c22SToby Isaac {
4752c58f1c22SToby Isaac   DMLabel        label;
4753c58f1c22SToby Isaac   PetscErrorCode ierr;
4754c58f1c22SToby Isaac 
4755c58f1c22SToby Isaac   PetscFunctionBegin;
4756c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4757c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4758c58f1c22SToby Isaac   PetscValidPointer(size, 3);
4759c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4760c58f1c22SToby Isaac   *size = 0;
4761c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4762c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
4763c58f1c22SToby Isaac   PetscFunctionReturn(0);
4764c58f1c22SToby Isaac }
4765c58f1c22SToby Isaac 
4766c58f1c22SToby Isaac #undef __FUNCT__
4767c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelIdIS"
4768c58f1c22SToby Isaac /*@C
4769c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
4770c58f1c22SToby Isaac 
4771c58f1c22SToby Isaac   Not Collective
4772c58f1c22SToby Isaac 
4773c58f1c22SToby Isaac   Input Parameters:
4774c58f1c22SToby Isaac + mesh - The DM object
4775c58f1c22SToby Isaac - name - The label name
4776c58f1c22SToby Isaac 
4777c58f1c22SToby Isaac   Output Parameter:
4778c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
4779c58f1c22SToby Isaac 
4780c58f1c22SToby Isaac   Level: beginner
4781c58f1c22SToby Isaac 
4782c58f1c22SToby Isaac .keywords: mesh
4783c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
4784c58f1c22SToby Isaac @*/
4785c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
4786c58f1c22SToby Isaac {
4787c58f1c22SToby Isaac   DMLabel        label;
4788c58f1c22SToby Isaac   PetscErrorCode ierr;
4789c58f1c22SToby Isaac 
4790c58f1c22SToby Isaac   PetscFunctionBegin;
4791c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4792c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4793c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
4794c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4795c58f1c22SToby Isaac   *ids = NULL;
4796c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4797c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
4798c58f1c22SToby Isaac   PetscFunctionReturn(0);
4799c58f1c22SToby Isaac }
4800c58f1c22SToby Isaac 
4801c58f1c22SToby Isaac #undef __FUNCT__
4802c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumSize"
4803c58f1c22SToby Isaac /*@C
4804c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
4805c58f1c22SToby Isaac 
4806c58f1c22SToby Isaac   Not Collective
4807c58f1c22SToby Isaac 
4808c58f1c22SToby Isaac   Input Parameters:
4809c58f1c22SToby Isaac + dm - The DM object
4810c58f1c22SToby Isaac . name - The label name
4811c58f1c22SToby Isaac - value - The stratum value
4812c58f1c22SToby Isaac 
4813c58f1c22SToby Isaac   Output Parameter:
4814c58f1c22SToby Isaac . size - The stratum size
4815c58f1c22SToby Isaac 
4816c58f1c22SToby Isaac   Level: beginner
4817c58f1c22SToby Isaac 
4818c58f1c22SToby Isaac .keywords: mesh
4819c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
4820c58f1c22SToby Isaac @*/
4821c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
4822c58f1c22SToby Isaac {
4823c58f1c22SToby Isaac   DMLabel        label;
4824c58f1c22SToby Isaac   PetscErrorCode ierr;
4825c58f1c22SToby Isaac 
4826c58f1c22SToby Isaac   PetscFunctionBegin;
4827c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4828c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4829c58f1c22SToby Isaac   PetscValidPointer(size, 4);
4830c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4831c58f1c22SToby Isaac   *size = 0;
4832c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4833c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
4834c58f1c22SToby Isaac   PetscFunctionReturn(0);
4835c58f1c22SToby Isaac }
4836c58f1c22SToby Isaac 
4837c58f1c22SToby Isaac #undef __FUNCT__
4838c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumIS"
4839c58f1c22SToby Isaac /*@C
4840c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
4841c58f1c22SToby Isaac 
4842c58f1c22SToby Isaac   Not Collective
4843c58f1c22SToby Isaac 
4844c58f1c22SToby Isaac   Input Parameters:
4845c58f1c22SToby Isaac + dm - The DM object
4846c58f1c22SToby Isaac . name - The label name
4847c58f1c22SToby Isaac - value - The stratum value
4848c58f1c22SToby Isaac 
4849c58f1c22SToby Isaac   Output Parameter:
4850c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
4851c58f1c22SToby Isaac 
4852c58f1c22SToby Isaac   Level: beginner
4853c58f1c22SToby Isaac 
4854c58f1c22SToby Isaac .keywords: mesh
4855c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
4856c58f1c22SToby Isaac @*/
4857c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
4858c58f1c22SToby Isaac {
4859c58f1c22SToby Isaac   DMLabel        label;
4860c58f1c22SToby Isaac   PetscErrorCode ierr;
4861c58f1c22SToby Isaac 
4862c58f1c22SToby Isaac   PetscFunctionBegin;
4863c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4864c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4865c58f1c22SToby Isaac   PetscValidPointer(points, 4);
4866c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4867c58f1c22SToby Isaac   *points = NULL;
4868c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4869c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
4870c58f1c22SToby Isaac   PetscFunctionReturn(0);
4871c58f1c22SToby Isaac }
4872c58f1c22SToby Isaac 
4873c58f1c22SToby Isaac #undef __FUNCT__
4874c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelStratum"
4875c58f1c22SToby Isaac /*@C
4876c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
4877c58f1c22SToby Isaac 
4878c58f1c22SToby Isaac   Not Collective
4879c58f1c22SToby Isaac 
4880c58f1c22SToby Isaac   Input Parameters:
4881c58f1c22SToby Isaac + dm   - The DM object
4882c58f1c22SToby Isaac . name - The label name
4883c58f1c22SToby Isaac - value - The label value for this point
4884c58f1c22SToby Isaac 
4885c58f1c22SToby Isaac   Output Parameter:
4886c58f1c22SToby Isaac 
4887c58f1c22SToby Isaac   Level: beginner
4888c58f1c22SToby Isaac 
4889c58f1c22SToby Isaac .keywords: mesh
4890c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
4891c58f1c22SToby Isaac @*/
4892c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
4893c58f1c22SToby Isaac {
4894c58f1c22SToby Isaac   DMLabel        label;
4895c58f1c22SToby Isaac   PetscErrorCode ierr;
4896c58f1c22SToby Isaac 
4897c58f1c22SToby Isaac   PetscFunctionBegin;
4898c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4899c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4900c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4901c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
4902c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
4903c58f1c22SToby Isaac   PetscFunctionReturn(0);
4904c58f1c22SToby Isaac }
4905c58f1c22SToby Isaac 
4906c58f1c22SToby Isaac #undef __FUNCT__
4907c58f1c22SToby Isaac #define __FUNCT__ "DMGetNumLabels"
4908c58f1c22SToby Isaac /*@
4909c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
4910c58f1c22SToby Isaac 
4911c58f1c22SToby Isaac   Not Collective
4912c58f1c22SToby Isaac 
4913c58f1c22SToby Isaac   Input Parameter:
4914c58f1c22SToby Isaac . dm   - The DM object
4915c58f1c22SToby Isaac 
4916c58f1c22SToby Isaac   Output Parameter:
4917c58f1c22SToby Isaac . numLabels - the number of Labels
4918c58f1c22SToby Isaac 
4919c58f1c22SToby Isaac   Level: intermediate
4920c58f1c22SToby Isaac 
4921c58f1c22SToby Isaac .keywords: mesh
4922c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4923c58f1c22SToby Isaac @*/
4924c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
4925c58f1c22SToby Isaac {
4926c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
4927c58f1c22SToby Isaac   PetscInt  n    = 0;
4928c58f1c22SToby Isaac 
4929c58f1c22SToby Isaac   PetscFunctionBegin;
4930c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4931c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
4932c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
4933c58f1c22SToby Isaac   *numLabels = n;
4934c58f1c22SToby Isaac   PetscFunctionReturn(0);
4935c58f1c22SToby Isaac }
4936c58f1c22SToby Isaac 
4937c58f1c22SToby Isaac #undef __FUNCT__
4938c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelName"
4939c58f1c22SToby Isaac /*@C
4940c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
4941c58f1c22SToby Isaac 
4942c58f1c22SToby Isaac   Not Collective
4943c58f1c22SToby Isaac 
4944c58f1c22SToby Isaac   Input Parameters:
4945c58f1c22SToby Isaac + dm - The DM object
4946c58f1c22SToby Isaac - n  - the label number
4947c58f1c22SToby Isaac 
4948c58f1c22SToby Isaac   Output Parameter:
4949c58f1c22SToby Isaac . name - the label name
4950c58f1c22SToby Isaac 
4951c58f1c22SToby Isaac   Level: intermediate
4952c58f1c22SToby Isaac 
4953c58f1c22SToby Isaac .keywords: mesh
4954c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4955c58f1c22SToby Isaac @*/
4956c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
4957c58f1c22SToby Isaac {
4958c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
4959c58f1c22SToby Isaac   PetscInt  l    = 0;
4960c58f1c22SToby Isaac 
4961c58f1c22SToby Isaac   PetscFunctionBegin;
4962c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4963c58f1c22SToby Isaac   PetscValidPointer(name, 3);
4964c58f1c22SToby Isaac   while (next) {
4965c58f1c22SToby Isaac     if (l == n) {
4966c58f1c22SToby Isaac       *name = next->label->name;
4967c58f1c22SToby Isaac       PetscFunctionReturn(0);
4968c58f1c22SToby Isaac     }
4969c58f1c22SToby Isaac     ++l;
4970c58f1c22SToby Isaac     next = next->next;
4971c58f1c22SToby Isaac   }
4972c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
4973c58f1c22SToby Isaac }
4974c58f1c22SToby Isaac 
4975c58f1c22SToby Isaac #undef __FUNCT__
4976c58f1c22SToby Isaac #define __FUNCT__ "DMHasLabel"
4977c58f1c22SToby Isaac /*@C
4978c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
4979c58f1c22SToby Isaac 
4980c58f1c22SToby Isaac   Not Collective
4981c58f1c22SToby Isaac 
4982c58f1c22SToby Isaac   Input Parameters:
4983c58f1c22SToby Isaac + dm   - The DM object
4984c58f1c22SToby Isaac - name - The label name
4985c58f1c22SToby Isaac 
4986c58f1c22SToby Isaac   Output Parameter:
4987c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
4988c58f1c22SToby Isaac 
4989c58f1c22SToby Isaac   Level: intermediate
4990c58f1c22SToby Isaac 
4991c58f1c22SToby Isaac .keywords: mesh
4992c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4993c58f1c22SToby Isaac @*/
4994c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
4995c58f1c22SToby Isaac {
4996c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
4997c58f1c22SToby Isaac   PetscErrorCode ierr;
4998c58f1c22SToby Isaac 
4999c58f1c22SToby Isaac   PetscFunctionBegin;
5000c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5001c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5002c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5003c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5004c58f1c22SToby Isaac   while (next) {
5005c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5006c58f1c22SToby Isaac     if (*hasLabel) break;
5007c58f1c22SToby Isaac     next = next->next;
5008c58f1c22SToby Isaac   }
5009c58f1c22SToby Isaac   PetscFunctionReturn(0);
5010c58f1c22SToby Isaac }
5011c58f1c22SToby Isaac 
5012c58f1c22SToby Isaac #undef __FUNCT__
5013c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabel"
5014c58f1c22SToby Isaac /*@C
5015c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5016c58f1c22SToby Isaac 
5017c58f1c22SToby Isaac   Not Collective
5018c58f1c22SToby Isaac 
5019c58f1c22SToby Isaac   Input Parameters:
5020c58f1c22SToby Isaac + dm   - The DM object
5021c58f1c22SToby Isaac - name - The label name
5022c58f1c22SToby Isaac 
5023c58f1c22SToby Isaac   Output Parameter:
5024c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5025c58f1c22SToby Isaac 
5026c58f1c22SToby Isaac   Level: intermediate
5027c58f1c22SToby Isaac 
5028c58f1c22SToby Isaac .keywords: mesh
5029c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5030c58f1c22SToby Isaac @*/
5031c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5032c58f1c22SToby Isaac {
5033c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5034c58f1c22SToby Isaac   PetscBool      hasLabel;
5035c58f1c22SToby Isaac   PetscErrorCode ierr;
5036c58f1c22SToby Isaac 
5037c58f1c22SToby Isaac   PetscFunctionBegin;
5038c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5039c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5040c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5041c58f1c22SToby Isaac   *label = NULL;
5042c58f1c22SToby Isaac   while (next) {
5043c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5044c58f1c22SToby Isaac     if (hasLabel) {
5045c58f1c22SToby Isaac       *label = next->label;
5046c58f1c22SToby Isaac       break;
5047c58f1c22SToby Isaac     }
5048c58f1c22SToby Isaac     next = next->next;
5049c58f1c22SToby Isaac   }
5050c58f1c22SToby Isaac   PetscFunctionReturn(0);
5051c58f1c22SToby Isaac }
5052c58f1c22SToby Isaac 
5053c58f1c22SToby Isaac #undef __FUNCT__
5054c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelByNum"
5055c58f1c22SToby Isaac /*@C
5056c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5057c58f1c22SToby Isaac 
5058c58f1c22SToby Isaac   Not Collective
5059c58f1c22SToby Isaac 
5060c58f1c22SToby Isaac   Input Parameters:
5061c58f1c22SToby Isaac + dm - The DM object
5062c58f1c22SToby Isaac - n  - the label number
5063c58f1c22SToby Isaac 
5064c58f1c22SToby Isaac   Output Parameter:
5065c58f1c22SToby Isaac . label - the label
5066c58f1c22SToby Isaac 
5067c58f1c22SToby Isaac   Level: intermediate
5068c58f1c22SToby Isaac 
5069c58f1c22SToby Isaac .keywords: mesh
5070c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5071c58f1c22SToby Isaac @*/
5072c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5073c58f1c22SToby Isaac {
5074c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5075c58f1c22SToby Isaac   PetscInt    l    = 0;
5076c58f1c22SToby Isaac 
5077c58f1c22SToby Isaac   PetscFunctionBegin;
5078c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5079c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5080c58f1c22SToby Isaac   while (next) {
5081c58f1c22SToby Isaac     if (l == n) {
5082c58f1c22SToby Isaac       *label = next->label;
5083c58f1c22SToby Isaac       PetscFunctionReturn(0);
5084c58f1c22SToby Isaac     }
5085c58f1c22SToby Isaac     ++l;
5086c58f1c22SToby Isaac     next = next->next;
5087c58f1c22SToby Isaac   }
5088c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5089c58f1c22SToby Isaac }
5090c58f1c22SToby Isaac 
5091c58f1c22SToby Isaac #undef __FUNCT__
5092c58f1c22SToby Isaac #define __FUNCT__ "DMAddLabel"
5093c58f1c22SToby Isaac /*@C
5094c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5095c58f1c22SToby Isaac 
5096c58f1c22SToby Isaac   Not Collective
5097c58f1c22SToby Isaac 
5098c58f1c22SToby Isaac   Input Parameters:
5099c58f1c22SToby Isaac + dm   - The DM object
5100c58f1c22SToby Isaac - label - The DMLabel
5101c58f1c22SToby Isaac 
5102c58f1c22SToby Isaac   Level: developer
5103c58f1c22SToby Isaac 
5104c58f1c22SToby Isaac .keywords: mesh
5105c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5106c58f1c22SToby Isaac @*/
5107c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5108c58f1c22SToby Isaac {
5109c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5110c58f1c22SToby Isaac   PetscBool      hasLabel;
5111c58f1c22SToby Isaac   PetscErrorCode ierr;
5112c58f1c22SToby Isaac 
5113c58f1c22SToby Isaac   PetscFunctionBegin;
5114c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5115c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5116c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5117c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5118c58f1c22SToby Isaac   tmpLabel->label  = label;
5119c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5120c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5121c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5122c58f1c22SToby Isaac   PetscFunctionReturn(0);
5123c58f1c22SToby Isaac }
5124c58f1c22SToby Isaac 
5125c58f1c22SToby Isaac #undef __FUNCT__
5126c58f1c22SToby Isaac #define __FUNCT__ "DMRemoveLabel"
5127c58f1c22SToby Isaac /*@C
5128c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5129c58f1c22SToby Isaac 
5130c58f1c22SToby Isaac   Not Collective
5131c58f1c22SToby Isaac 
5132c58f1c22SToby Isaac   Input Parameters:
5133c58f1c22SToby Isaac + dm   - The DM object
5134c58f1c22SToby Isaac - name - The label name
5135c58f1c22SToby Isaac 
5136c58f1c22SToby Isaac   Output Parameter:
5137c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5138c58f1c22SToby Isaac 
5139c58f1c22SToby Isaac   Level: developer
5140c58f1c22SToby Isaac 
5141c58f1c22SToby Isaac .keywords: mesh
5142c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5143c58f1c22SToby Isaac @*/
5144c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5145c58f1c22SToby Isaac {
5146c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5147c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5148c58f1c22SToby Isaac   PetscBool      hasLabel;
5149c58f1c22SToby Isaac   PetscErrorCode ierr;
5150c58f1c22SToby Isaac 
5151c58f1c22SToby Isaac   PetscFunctionBegin;
5152c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5153c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5154c58f1c22SToby Isaac   *label = NULL;
5155c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5156c58f1c22SToby Isaac   while (next) {
5157c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5158c58f1c22SToby Isaac     if (hasLabel) {
5159c58f1c22SToby Isaac       if (last) last->next       = next->next;
5160c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5161c58f1c22SToby Isaac       next->next = NULL;
5162c58f1c22SToby Isaac       *label     = next->label;
5163c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5164c58f1c22SToby Isaac       if (hasLabel) {
5165c58f1c22SToby Isaac         dm->depthLabel = NULL;
5166c58f1c22SToby Isaac       }
5167c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5168c58f1c22SToby Isaac       break;
5169c58f1c22SToby Isaac     }
5170c58f1c22SToby Isaac     last = next;
5171c58f1c22SToby Isaac     next = next->next;
5172c58f1c22SToby Isaac   }
5173c58f1c22SToby Isaac   PetscFunctionReturn(0);
5174c58f1c22SToby Isaac }
5175c58f1c22SToby Isaac 
5176c58f1c22SToby Isaac #undef __FUNCT__
5177c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelOutput"
5178c58f1c22SToby Isaac /*@C
5179c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5180c58f1c22SToby Isaac 
5181c58f1c22SToby Isaac   Not Collective
5182c58f1c22SToby Isaac 
5183c58f1c22SToby Isaac   Input Parameters:
5184c58f1c22SToby Isaac + dm   - The DM object
5185c58f1c22SToby Isaac - name - The label name
5186c58f1c22SToby Isaac 
5187c58f1c22SToby Isaac   Output Parameter:
5188c58f1c22SToby Isaac . output - The flag for output
5189c58f1c22SToby Isaac 
5190c58f1c22SToby Isaac   Level: developer
5191c58f1c22SToby Isaac 
5192c58f1c22SToby Isaac .keywords: mesh
5193c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5194c58f1c22SToby Isaac @*/
5195c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5196c58f1c22SToby Isaac {
5197c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5198c58f1c22SToby Isaac   PetscErrorCode ierr;
5199c58f1c22SToby Isaac 
5200c58f1c22SToby Isaac   PetscFunctionBegin;
5201c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5202c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5203c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5204c58f1c22SToby Isaac   while (next) {
5205c58f1c22SToby Isaac     PetscBool flg;
5206c58f1c22SToby Isaac 
5207c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5208c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5209c58f1c22SToby Isaac     next = next->next;
5210c58f1c22SToby Isaac   }
5211c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5212c58f1c22SToby Isaac }
5213c58f1c22SToby Isaac 
5214c58f1c22SToby Isaac #undef __FUNCT__
5215c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelOutput"
5216c58f1c22SToby Isaac /*@C
5217c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5218c58f1c22SToby Isaac 
5219c58f1c22SToby Isaac   Not Collective
5220c58f1c22SToby Isaac 
5221c58f1c22SToby Isaac   Input Parameters:
5222c58f1c22SToby Isaac + dm     - The DM object
5223c58f1c22SToby Isaac . name   - The label name
5224c58f1c22SToby Isaac - output - The flag for output
5225c58f1c22SToby Isaac 
5226c58f1c22SToby Isaac   Level: developer
5227c58f1c22SToby Isaac 
5228c58f1c22SToby Isaac .keywords: mesh
5229c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5230c58f1c22SToby Isaac @*/
5231c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5232c58f1c22SToby Isaac {
5233c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5234c58f1c22SToby Isaac   PetscErrorCode ierr;
5235c58f1c22SToby Isaac 
5236c58f1c22SToby Isaac   PetscFunctionBegin;
5237c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5238c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5239c58f1c22SToby Isaac   while (next) {
5240c58f1c22SToby Isaac     PetscBool flg;
5241c58f1c22SToby Isaac 
5242c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5243c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5244c58f1c22SToby Isaac     next = next->next;
5245c58f1c22SToby Isaac   }
5246c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5247c58f1c22SToby Isaac }
5248c58f1c22SToby Isaac 
5249c58f1c22SToby Isaac 
5250c58f1c22SToby Isaac #undef __FUNCT__
5251c58f1c22SToby Isaac #define __FUNCT__ "DMCopyLabels"
5252c58f1c22SToby Isaac /*@
5253c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5254c58f1c22SToby Isaac 
5255c58f1c22SToby Isaac   Collective on DM
5256c58f1c22SToby Isaac 
5257c58f1c22SToby Isaac   Input Parameter:
5258c58f1c22SToby Isaac . dmA - The DMPlex object with initial labels
5259c58f1c22SToby Isaac 
5260c58f1c22SToby Isaac   Output Parameter:
5261c58f1c22SToby Isaac . dmB - The DMPlex object with copied labels
5262c58f1c22SToby Isaac 
5263c58f1c22SToby Isaac   Level: intermediate
5264c58f1c22SToby Isaac 
5265c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5266c58f1c22SToby Isaac 
5267c58f1c22SToby Isaac .keywords: mesh
5268c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5269c58f1c22SToby Isaac @*/
5270c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5271c58f1c22SToby Isaac {
5272c58f1c22SToby Isaac   PetscInt       numLabels, l;
5273c58f1c22SToby Isaac   PetscErrorCode ierr;
5274c58f1c22SToby Isaac 
5275c58f1c22SToby Isaac   PetscFunctionBegin;
5276c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5277c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5278c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5279c58f1c22SToby Isaac     DMLabel     label, labelNew;
5280c58f1c22SToby Isaac     const char *name;
5281c58f1c22SToby Isaac     PetscBool   flg;
5282c58f1c22SToby Isaac 
5283c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5284c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5285c58f1c22SToby Isaac     if (flg) continue;
5286c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5287c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5288c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5289c58f1c22SToby Isaac   }
5290c58f1c22SToby Isaac   PetscFunctionReturn(0);
5291c58f1c22SToby Isaac }
5292a8fb8f29SToby Isaac 
5293a8fb8f29SToby Isaac #undef __FUNCT__
5294a8fb8f29SToby Isaac #define __FUNCT__ "DMGetCoarseDM"
5295a8fb8f29SToby Isaac /*@
5296a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5297a8fb8f29SToby Isaac 
5298a8fb8f29SToby Isaac   Input Parameter:
5299a8fb8f29SToby Isaac . dm - The DM object
5300a8fb8f29SToby Isaac 
5301a8fb8f29SToby Isaac   Output Parameter:
5302a8fb8f29SToby Isaac . cdm - The coarse DM
5303a8fb8f29SToby Isaac 
5304a8fb8f29SToby Isaac   Level: intermediate
5305a8fb8f29SToby Isaac 
5306a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5307a8fb8f29SToby Isaac @*/
5308a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5309a8fb8f29SToby Isaac {
5310a8fb8f29SToby Isaac   PetscFunctionBegin;
5311a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5312a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5313a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5314a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5315a8fb8f29SToby Isaac }
5316a8fb8f29SToby Isaac 
5317a8fb8f29SToby Isaac #undef __FUNCT__
5318a8fb8f29SToby Isaac #define __FUNCT__ "DMSetCoarseDM"
5319a8fb8f29SToby Isaac /*@
5320a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5321a8fb8f29SToby Isaac 
5322a8fb8f29SToby Isaac   Input Parameters:
5323a8fb8f29SToby Isaac + dm - The DM object
5324a8fb8f29SToby Isaac - cdm - The coarse DM
5325a8fb8f29SToby Isaac 
5326a8fb8f29SToby Isaac   Level: intermediate
5327a8fb8f29SToby Isaac 
5328a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5329a8fb8f29SToby Isaac @*/
5330a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5331a8fb8f29SToby Isaac {
5332a8fb8f29SToby Isaac   PetscErrorCode ierr;
5333a8fb8f29SToby Isaac 
5334a8fb8f29SToby Isaac   PetscFunctionBegin;
5335a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5336a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5337a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5338a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5339a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5340a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5341a8fb8f29SToby Isaac }
5342a8fb8f29SToby Isaac 
534388bdff64SToby Isaac #undef __FUNCT__
534488bdff64SToby Isaac #define __FUNCT__ "DMGetFineDM"
534588bdff64SToby Isaac /*@
534688bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
534788bdff64SToby Isaac 
534888bdff64SToby Isaac   Input Parameter:
534988bdff64SToby Isaac . dm - The DM object
535088bdff64SToby Isaac 
535188bdff64SToby Isaac   Output Parameter:
535288bdff64SToby Isaac . fdm - The fine DM
535388bdff64SToby Isaac 
535488bdff64SToby Isaac   Level: intermediate
535588bdff64SToby Isaac 
535688bdff64SToby Isaac .seealso: DMSetFineDM()
535788bdff64SToby Isaac @*/
535888bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
535988bdff64SToby Isaac {
536088bdff64SToby Isaac   PetscFunctionBegin;
536188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
536288bdff64SToby Isaac   PetscValidPointer(fdm, 2);
536388bdff64SToby Isaac   *fdm = dm->fineMesh;
536488bdff64SToby Isaac   PetscFunctionReturn(0);
536588bdff64SToby Isaac }
536688bdff64SToby Isaac 
536788bdff64SToby Isaac #undef __FUNCT__
536888bdff64SToby Isaac #define __FUNCT__ "DMSetFineDM"
536988bdff64SToby Isaac /*@
537088bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
537188bdff64SToby Isaac 
537288bdff64SToby Isaac   Input Parameters:
537388bdff64SToby Isaac + dm - The DM object
537488bdff64SToby Isaac - fdm - The fine DM
537588bdff64SToby Isaac 
537688bdff64SToby Isaac   Level: intermediate
537788bdff64SToby Isaac 
537888bdff64SToby Isaac .seealso: DMGetFineDM()
537988bdff64SToby Isaac @*/
538088bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
538188bdff64SToby Isaac {
538288bdff64SToby Isaac   PetscErrorCode ierr;
538388bdff64SToby Isaac 
538488bdff64SToby Isaac   PetscFunctionBegin;
538588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
538688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
538788bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
538888bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
538988bdff64SToby Isaac   dm->fineMesh = fdm;
539088bdff64SToby Isaac   PetscFunctionReturn(0);
539188bdff64SToby Isaac }
539288bdff64SToby Isaac 
5393a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5394a6ba4734SToby Isaac 
5395a6ba4734SToby Isaac #undef __FUNCT__
5396a6ba4734SToby Isaac #define __FUNCT__ "BoundaryDuplicate"
5397a6ba4734SToby Isaac static PetscErrorCode BoundaryDuplicate(DMBoundary bd, DMBoundary *boundary)
5398a6ba4734SToby Isaac {
5399a6ba4734SToby Isaac   DMBoundary     b = bd, b2, bold = NULL;
5400a6ba4734SToby Isaac   PetscErrorCode ierr;
5401a6ba4734SToby Isaac 
5402a6ba4734SToby Isaac   PetscFunctionBegin;
5403a6ba4734SToby Isaac   *boundary = NULL;
5404a6ba4734SToby Isaac   for (; b; b = b->next, bold = b2) {
5405a6ba4734SToby Isaac     ierr = PetscNew(&b2);CHKERRQ(ierr);
5406a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
5407a6ba4734SToby Isaac     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
5408a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
5409a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
5410a6ba4734SToby Isaac     ierr = PetscMalloc1(b->numcomps, &b2->comps);CHKERRQ(ierr);
5411a6ba4734SToby Isaac     ierr = PetscMemcpy(b2->comps, b->comps, b->numcomps*sizeof(PetscInt));CHKERRQ(ierr);
5412a6ba4734SToby Isaac     b2->label     = NULL;
5413a6ba4734SToby Isaac     b2->essential = b->essential;
5414a6ba4734SToby Isaac     b2->field     = b->field;
5415a6ba4734SToby Isaac     b2->numcomps  = b->numcomps;
5416a6ba4734SToby Isaac     b2->func      = b->func;
5417a6ba4734SToby Isaac     b2->numids    = b->numids;
5418a6ba4734SToby Isaac     b2->ctx       = b->ctx;
5419a6ba4734SToby Isaac     b2->next      = NULL;
5420a6ba4734SToby Isaac     if (!*boundary) *boundary   = b2;
5421a6ba4734SToby Isaac     if (bold)        bold->next = b2;
5422a6ba4734SToby Isaac   }
5423a6ba4734SToby Isaac   PetscFunctionReturn(0);
5424a6ba4734SToby Isaac }
5425a6ba4734SToby Isaac 
5426a6ba4734SToby Isaac #undef __FUNCT__
5427a6ba4734SToby Isaac #define __FUNCT__ "BoundaryDestroy"
5428a6ba4734SToby Isaac static PetscErrorCode BoundaryDestroy(DMBoundary *boundary)
5429a6ba4734SToby Isaac {
5430a6ba4734SToby Isaac   DMBoundary     b, next;
5431a6ba4734SToby Isaac   PetscErrorCode ierr;
5432a6ba4734SToby Isaac 
5433a6ba4734SToby Isaac   PetscFunctionBegin;
5434a6ba4734SToby Isaac   if (!boundary) PetscFunctionReturn(0);
5435a6ba4734SToby Isaac   b = *boundary;
5436a6ba4734SToby Isaac   *boundary = NULL;
5437a6ba4734SToby Isaac   for (; b; b = next) {
5438a6ba4734SToby Isaac     next = b->next;
5439a6ba4734SToby Isaac     ierr = PetscFree(b->comps);CHKERRQ(ierr);
5440a6ba4734SToby Isaac     ierr = PetscFree(b->ids);CHKERRQ(ierr);
5441a6ba4734SToby Isaac     ierr = PetscFree(b->name);CHKERRQ(ierr);
5442a6ba4734SToby Isaac     ierr = PetscFree(b->labelname);CHKERRQ(ierr);
5443a6ba4734SToby Isaac     ierr = PetscFree(b);CHKERRQ(ierr);
5444a6ba4734SToby Isaac   }
5445a6ba4734SToby Isaac   PetscFunctionReturn(0);
5446a6ba4734SToby Isaac }
5447a6ba4734SToby Isaac 
5448a6ba4734SToby Isaac #undef __FUNCT__
5449a6ba4734SToby Isaac #define __FUNCT__ "DMCopyBoundary"
5450a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5451a6ba4734SToby Isaac {
5452a6ba4734SToby Isaac   DMBoundary     b;
5453a6ba4734SToby Isaac   PetscErrorCode ierr;
5454a6ba4734SToby Isaac 
5455a6ba4734SToby Isaac   PetscFunctionBegin;
5456a6ba4734SToby Isaac   ierr = BoundaryDuplicate(dm->boundary, &dmNew->boundary);CHKERRQ(ierr);
5457a6ba4734SToby Isaac   for (b = dmNew->boundary; b; b = b->next) {
5458a6ba4734SToby Isaac     if (b->labelname) {
5459a6ba4734SToby Isaac       ierr = DMGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
5460a6ba4734SToby Isaac       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5461a6ba4734SToby Isaac     }
5462a6ba4734SToby Isaac   }
5463a6ba4734SToby Isaac   PetscFunctionReturn(0);
5464a6ba4734SToby Isaac }
5465a6ba4734SToby Isaac 
5466a6ba4734SToby Isaac #undef __FUNCT__
5467a6ba4734SToby Isaac #define __FUNCT__ "DMAddBoundary"
5468a6ba4734SToby Isaac /*@C
5469a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5470a6ba4734SToby Isaac 
5471a6ba4734SToby Isaac   Input Parameters:
5472a6ba4734SToby Isaac + dm          - The mesh object
5473a6ba4734SToby Isaac . isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5474a6ba4734SToby Isaac . name        - The BC name
5475a6ba4734SToby Isaac . labelname   - The label defining constrained points
5476a6ba4734SToby Isaac . field       - The field to constrain
5477a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5478a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5479a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5480a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5481a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5482a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5483a6ba4734SToby Isaac 
5484a6ba4734SToby Isaac   Options Database Keys:
5485a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5486a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5487a6ba4734SToby Isaac 
5488a6ba4734SToby Isaac   Level: developer
5489a6ba4734SToby Isaac 
5490a6ba4734SToby Isaac .seealso: DMGetBoundary()
5491a6ba4734SToby Isaac @*/
5492a6ba4734SToby 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)
5493a6ba4734SToby Isaac {
5494a6ba4734SToby Isaac   DMBoundary     b;
5495a6ba4734SToby Isaac   PetscErrorCode ierr;
5496a6ba4734SToby Isaac 
5497a6ba4734SToby Isaac   PetscFunctionBegin;
5498a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5499a6ba4734SToby Isaac   ierr = PetscNew(&b);CHKERRQ(ierr);
5500a6ba4734SToby Isaac   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
5501a6ba4734SToby Isaac   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
5502a6ba4734SToby Isaac   ierr = PetscMalloc1(numcomps, &b->comps);CHKERRQ(ierr);
5503a6ba4734SToby Isaac   if (numcomps) {ierr = PetscMemcpy(b->comps, comps, numcomps*sizeof(PetscInt));CHKERRQ(ierr);}
5504a6ba4734SToby Isaac   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
5505a6ba4734SToby Isaac   if (numids) {ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);}
5506a6ba4734SToby Isaac   if (b->labelname) {
5507a6ba4734SToby Isaac     ierr = DMGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
5508a6ba4734SToby Isaac     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
5509a6ba4734SToby Isaac   }
5510a6ba4734SToby Isaac   b->essential   = isEssential;
5511a6ba4734SToby Isaac   b->field       = field;
5512a6ba4734SToby Isaac   b->numcomps    = numcomps;
5513a6ba4734SToby Isaac   b->func        = bcFunc;
5514a6ba4734SToby Isaac   b->numids      = numids;
5515a6ba4734SToby Isaac   b->ctx         = ctx;
5516a6ba4734SToby Isaac   b->next        = dm->boundary;
5517a6ba4734SToby Isaac   dm->boundary   = b;
5518a6ba4734SToby Isaac   PetscFunctionReturn(0);
5519a6ba4734SToby Isaac }
5520a6ba4734SToby Isaac 
5521a6ba4734SToby Isaac #undef __FUNCT__
5522a6ba4734SToby Isaac #define __FUNCT__ "DMGetNumBoundary"
5523a6ba4734SToby Isaac /*@
5524a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5525a6ba4734SToby Isaac 
5526a6ba4734SToby Isaac   Input Parameters:
5527a6ba4734SToby Isaac . dm - The mesh object
5528a6ba4734SToby Isaac 
5529a6ba4734SToby Isaac   Output Parameters:
5530a6ba4734SToby Isaac . numBd - The number of BC
5531a6ba4734SToby Isaac 
5532a6ba4734SToby Isaac   Level: intermediate
5533a6ba4734SToby Isaac 
5534a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5535a6ba4734SToby Isaac @*/
5536a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5537a6ba4734SToby Isaac {
5538a6ba4734SToby Isaac   DMBoundary b = dm->boundary;
5539a6ba4734SToby Isaac 
5540a6ba4734SToby Isaac   PetscFunctionBegin;
5541a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5542a6ba4734SToby Isaac   PetscValidPointer(numBd, 2);
5543a6ba4734SToby Isaac   *numBd = 0;
5544a6ba4734SToby Isaac   while (b) {++(*numBd); b = b->next;}
5545a6ba4734SToby Isaac   PetscFunctionReturn(0);
5546a6ba4734SToby Isaac }
5547a6ba4734SToby Isaac 
5548a6ba4734SToby Isaac #undef __FUNCT__
5549a6ba4734SToby Isaac #define __FUNCT__ "DMGetBoundary"
5550a6ba4734SToby Isaac /*@C
5551a6ba4734SToby Isaac   DMGetBoundary - Add a boundary condition to the model
5552a6ba4734SToby Isaac 
5553a6ba4734SToby Isaac   Input Parameters:
5554a6ba4734SToby Isaac + dm          - The mesh object
5555a6ba4734SToby Isaac - bd          - The BC number
5556a6ba4734SToby Isaac 
5557a6ba4734SToby Isaac   Output Parameters:
5558a6ba4734SToby Isaac + isEssential - Flag for an essential (Dirichlet) condition, as opposed to a natural (Neumann) condition
5559a6ba4734SToby Isaac . name        - The BC name
5560a6ba4734SToby Isaac . labelname   - The label defining constrained points
5561a6ba4734SToby Isaac . field       - The field to constrain
5562a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5563a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5564a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5565a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5566a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5567a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5568a6ba4734SToby Isaac 
5569a6ba4734SToby Isaac   Options Database Keys:
5570a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5571a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5572a6ba4734SToby Isaac 
5573a6ba4734SToby Isaac   Level: developer
5574a6ba4734SToby Isaac 
5575a6ba4734SToby Isaac .seealso: DMAddBoundary()
5576a6ba4734SToby Isaac @*/
5577a6ba4734SToby 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)
5578a6ba4734SToby Isaac {
5579a6ba4734SToby Isaac   DMBoundary b    = dm->boundary;
5580a6ba4734SToby Isaac   PetscInt   n    = 0;
5581a6ba4734SToby Isaac 
5582a6ba4734SToby Isaac   PetscFunctionBegin;
5583a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5584a6ba4734SToby Isaac   while (b) {
5585a6ba4734SToby Isaac     if (n == bd) break;
5586a6ba4734SToby Isaac     b = b->next;
5587a6ba4734SToby Isaac     ++n;
5588a6ba4734SToby Isaac   }
5589a6ba4734SToby Isaac   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
5590a6ba4734SToby Isaac   if (isEssential) {
5591a6ba4734SToby Isaac     PetscValidPointer(isEssential, 3);
5592a6ba4734SToby Isaac     *isEssential = b->essential;
5593a6ba4734SToby Isaac   }
5594a6ba4734SToby Isaac   if (name) {
5595a6ba4734SToby Isaac     PetscValidPointer(name, 4);
5596a6ba4734SToby Isaac     *name = b->name;
5597a6ba4734SToby Isaac   }
5598a6ba4734SToby Isaac   if (labelname) {
5599a6ba4734SToby Isaac     PetscValidPointer(labelname, 5);
5600a6ba4734SToby Isaac     *labelname = b->labelname;
5601a6ba4734SToby Isaac   }
5602a6ba4734SToby Isaac   if (field) {
5603a6ba4734SToby Isaac     PetscValidPointer(field, 6);
5604a6ba4734SToby Isaac     *field = b->field;
5605a6ba4734SToby Isaac   }
5606a6ba4734SToby Isaac   if (numcomps) {
5607a6ba4734SToby Isaac     PetscValidPointer(numcomps, 7);
5608a6ba4734SToby Isaac     *numcomps = b->numcomps;
5609a6ba4734SToby Isaac   }
5610a6ba4734SToby Isaac   if (comps) {
5611a6ba4734SToby Isaac     PetscValidPointer(comps, 8);
5612a6ba4734SToby Isaac     *comps = b->comps;
5613a6ba4734SToby Isaac   }
5614a6ba4734SToby Isaac   if (func) {
5615a6ba4734SToby Isaac     PetscValidPointer(func, 9);
5616a6ba4734SToby Isaac     *func = b->func;
5617a6ba4734SToby Isaac   }
5618a6ba4734SToby Isaac   if (numids) {
5619a6ba4734SToby Isaac     PetscValidPointer(numids, 10);
5620a6ba4734SToby Isaac     *numids = b->numids;
5621a6ba4734SToby Isaac   }
5622a6ba4734SToby Isaac   if (ids) {
5623a6ba4734SToby Isaac     PetscValidPointer(ids, 11);
5624a6ba4734SToby Isaac     *ids = b->ids;
5625a6ba4734SToby Isaac   }
5626a6ba4734SToby Isaac   if (ctx) {
5627a6ba4734SToby Isaac     PetscValidPointer(ctx, 12);
5628a6ba4734SToby Isaac     *ctx = b->ctx;
5629a6ba4734SToby Isaac   }
5630a6ba4734SToby Isaac   PetscFunctionReturn(0);
5631a6ba4734SToby Isaac }
5632a6ba4734SToby Isaac 
5633a6ba4734SToby Isaac #undef __FUNCT__
5634a6ba4734SToby Isaac #define __FUNCT__ "DMIsBoundaryPoint"
5635a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
5636a6ba4734SToby Isaac {
5637a6ba4734SToby Isaac   DMBoundary     b    = dm->boundary;
5638a6ba4734SToby Isaac   PetscErrorCode ierr;
5639a6ba4734SToby Isaac 
5640a6ba4734SToby Isaac   PetscFunctionBegin;
5641a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5642a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
5643a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
5644a6ba4734SToby Isaac   while (b && !(*isBd)) {
5645a6ba4734SToby Isaac     if (b->label) {
5646a6ba4734SToby Isaac       PetscInt i;
5647a6ba4734SToby Isaac 
5648a6ba4734SToby Isaac       for (i = 0; i < b->numids && !(*isBd); ++i) {
5649a6ba4734SToby Isaac         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
5650a6ba4734SToby Isaac       }
5651a6ba4734SToby Isaac     }
5652a6ba4734SToby Isaac     b = b->next;
5653a6ba4734SToby Isaac   }
5654a6ba4734SToby Isaac   PetscFunctionReturn(0);
5655a6ba4734SToby Isaac }
5656*4d6f44ffSToby Isaac 
5657*4d6f44ffSToby Isaac #undef __FUNCT__
5658*4d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunction"
5659*4d6f44ffSToby Isaac /*@C
5660*4d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
5661*4d6f44ffSToby Isaac 
5662*4d6f44ffSToby Isaac   Input Parameters:
5663*4d6f44ffSToby Isaac + dm      - The DM
5664*4d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
5665*4d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
5666*4d6f44ffSToby Isaac - mode    - The insertion mode for values
5667*4d6f44ffSToby Isaac 
5668*4d6f44ffSToby Isaac   Output Parameter:
5669*4d6f44ffSToby Isaac . X - vector
5670*4d6f44ffSToby Isaac 
5671*4d6f44ffSToby Isaac    Calling sequence of func:
5672*4d6f44ffSToby Isaac $    func(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
5673*4d6f44ffSToby Isaac 
5674*4d6f44ffSToby Isaac +  dim - The spatial dimension
5675*4d6f44ffSToby Isaac .  x   - The coordinates
5676*4d6f44ffSToby Isaac .  Nf  - The number of fields
5677*4d6f44ffSToby Isaac .  u   - The output field values
5678*4d6f44ffSToby Isaac -  ctx - optional user-defined function context
5679*4d6f44ffSToby Isaac 
5680*4d6f44ffSToby Isaac   Level: developer
5681*4d6f44ffSToby Isaac 
5682*4d6f44ffSToby Isaac .seealso: DMPlexComputeL2Diff()
5683*4d6f44ffSToby Isaac @*/
5684*4d6f44ffSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
5685*4d6f44ffSToby Isaac {
5686*4d6f44ffSToby Isaac   Vec            localX;
5687*4d6f44ffSToby Isaac   PetscErrorCode ierr;
5688*4d6f44ffSToby Isaac 
5689*4d6f44ffSToby Isaac   PetscFunctionBegin;
5690*4d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5691*4d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
5692*4d6f44ffSToby Isaac   ierr = DMProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
5693*4d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
5694*4d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
5695*4d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
5696*4d6f44ffSToby Isaac   PetscFunctionReturn(0);
5697*4d6f44ffSToby Isaac }
5698*4d6f44ffSToby Isaac 
5699*4d6f44ffSToby Isaac #undef __FUNCT__
5700*4d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal"
5701*4d6f44ffSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
5702*4d6f44ffSToby Isaac {
5703*4d6f44ffSToby Isaac   PetscErrorCode ierr;
5704*4d6f44ffSToby Isaac 
5705*4d6f44ffSToby Isaac   PetscFunctionBegin;
5706*4d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5707*4d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
5708*4d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
5709*4d6f44ffSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
5710*4d6f44ffSToby Isaac   PetscFunctionReturn(0);
5711*4d6f44ffSToby Isaac }
5712*4d6f44ffSToby Isaac 
5713*4d6f44ffSToby Isaac #undef __FUNCT__
5714*4d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLabelLocal"
5715*4d6f44ffSToby Isaac PetscErrorCode DMProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
5716*4d6f44ffSToby Isaac {
5717*4d6f44ffSToby Isaac   PetscErrorCode ierr;
5718*4d6f44ffSToby Isaac 
5719*4d6f44ffSToby Isaac   PetscFunctionBegin;
5720*4d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5721*4d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
5722*4d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
5723*4d6f44ffSToby Isaac   ierr = (dm->ops->projectfunctionlabellocal) (dm, label, numIds, ids, funcs, ctxs, mode, localX);CHKERRQ(ierr);
5724*4d6f44ffSToby Isaac   PetscFunctionReturn(0);
5725*4d6f44ffSToby Isaac }
5726