xref: /petsc/src/dm/interface/dm.c (revision 8e4ac7ea3bc28ad0f3cfa3efd1a18c07a02c026b)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
32764a2aaSMatthew G. Knepley #include <petscds.h>
447c6ae99SBarry Smith 
5732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
6f089877aSRichard Tran Mills PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal;
767a56275SMatthew G Knepley 
8bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
9bff4a2f0SMatthew G. Knepley 
1047c6ae99SBarry Smith #undef __FUNCT__
11a4121054SBarry Smith #define __FUNCT__ "DMCreate"
12a4121054SBarry Smith /*@
13de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
14a4121054SBarry Smith 
15a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
16a4121054SBarry Smith    error when you try to use the vector.
17a4121054SBarry Smith 
18a4121054SBarry Smith   Collective on MPI_Comm
19a4121054SBarry Smith 
20a4121054SBarry Smith   Input Parameter:
21a4121054SBarry Smith . comm - The communicator for the DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Output Parameter:
24a4121054SBarry Smith . dm - The DM object
25a4121054SBarry Smith 
26a4121054SBarry Smith   Level: beginner
27a4121054SBarry Smith 
28a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
29a4121054SBarry Smith @*/
307087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
31a4121054SBarry Smith {
32a4121054SBarry Smith   DM             v;
33a4121054SBarry Smith   PetscErrorCode ierr;
34a4121054SBarry Smith 
35a4121054SBarry Smith   PetscFunctionBegin;
361411c6eeSJed Brown   PetscValidPointer(dm,2);
370298fd71SBarry Smith   *dm = NULL;
388b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
39607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
42a4121054SBarry Smith 
4373107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
44e7c4fc90SDmitry Karpeev 
450298fd71SBarry Smith   v->ltogmap                  = NULL;
461411c6eeSJed Brown   v->bs                       = 1;
47171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
4888ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
4988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
500298fd71SBarry Smith   v->defaultSection           = NULL;
510298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
52fba222abSToby Isaac   v->defaultConstraintSection = NULL;
53fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
54c6b900c6SMatthew G. Knepley   v->L                        = NULL;
55c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
565dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
579a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
58435a35e8SMatthew G Knepley   {
59435a35e8SMatthew G Knepley     PetscInt i;
60435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
610298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
62435a35e8SMatthew G Knepley     }
63435a35e8SMatthew G Knepley   }
642764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6514f150ffSMatthew G. Knepley   v->dmBC = NULL;
66f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
67cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
68c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
69b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
701411c6eeSJed Brown   *dm = v;
71a4121054SBarry Smith   PetscFunctionReturn(0);
72a4121054SBarry Smith }
73a4121054SBarry Smith 
74a4121054SBarry Smith #undef __FUNCT__
7538221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
7638221697SMatthew G. Knepley /*@
7738221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
7838221697SMatthew G. Knepley 
7938221697SMatthew G. Knepley   Collective on MPI_Comm
8038221697SMatthew G. Knepley 
8138221697SMatthew G. Knepley   Input Parameter:
8238221697SMatthew G. Knepley . dm - The original DM object
8338221697SMatthew G. Knepley 
8438221697SMatthew G. Knepley   Output Parameter:
8538221697SMatthew G. Knepley . newdm  - The new DM object
8638221697SMatthew G. Knepley 
8738221697SMatthew G. Knepley   Level: beginner
8838221697SMatthew G. Knepley 
8938221697SMatthew G. Knepley .keywords: DM, topology, create
9038221697SMatthew G. Knepley @*/
9138221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9238221697SMatthew G. Knepley {
9338221697SMatthew G. Knepley   PetscSF        sf;
9438221697SMatthew G. Knepley   Vec            coords;
9538221697SMatthew G. Knepley   void          *ctx;
961de53e9aSMatthew G. Knepley   PetscInt       dim;
9738221697SMatthew G. Knepley   PetscErrorCode ierr;
9838221697SMatthew G. Knepley 
9938221697SMatthew G. Knepley   PetscFunctionBegin;
10038221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10138221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10238221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
1031de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1041de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
10538221697SMatthew G. Knepley   if (dm->ops->clone) {
10638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
10738221697SMatthew G. Knepley   }
108cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
10938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
11038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
11138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
11238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
11338221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
11438221697SMatthew G. Knepley   if (coords) {
11538221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
11638221697SMatthew G. Knepley   } else {
11738221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
11838221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
11938221697SMatthew G. Knepley   }
120c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
121c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1225dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1235dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1245dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
125c6b900c6SMatthew G. Knepley   }
12638221697SMatthew G. Knepley   PetscFunctionReturn(0);
12738221697SMatthew G. Knepley }
12838221697SMatthew G. Knepley 
12938221697SMatthew G. Knepley #undef __FUNCT__
1309a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1319a42bb27SBarry Smith /*@C
132564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1339a42bb27SBarry Smith 
134aa219208SBarry Smith    Logically Collective on DMDA
1359a42bb27SBarry Smith 
1369a42bb27SBarry Smith    Input Parameter:
1379a42bb27SBarry Smith +  da - initial distributed array
1388154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1399a42bb27SBarry Smith 
1409a42bb27SBarry Smith    Options Database:
141dd85299cSBarry Smith .   -dm_vec_type ctype
1429a42bb27SBarry Smith 
1439a42bb27SBarry Smith    Level: intermediate
1449a42bb27SBarry Smith 
145c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType, DMGetVecType()
1469a42bb27SBarry Smith @*/
14719fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1489a42bb27SBarry Smith {
1499a42bb27SBarry Smith   PetscErrorCode ierr;
1509a42bb27SBarry Smith 
1519a42bb27SBarry Smith   PetscFunctionBegin;
1529a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1539a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
15419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1559a42bb27SBarry Smith   PetscFunctionReturn(0);
1569a42bb27SBarry Smith }
1579a42bb27SBarry Smith 
1589a42bb27SBarry Smith #undef __FUNCT__
159c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
160c0dedaeaSBarry Smith /*@C
161c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
162c0dedaeaSBarry Smith 
163c0dedaeaSBarry Smith    Logically Collective on DMDA
164c0dedaeaSBarry Smith 
165c0dedaeaSBarry Smith    Input Parameter:
166c0dedaeaSBarry Smith .  da - initial distributed array
167c0dedaeaSBarry Smith 
168c0dedaeaSBarry Smith    Output Parameter:
169c0dedaeaSBarry Smith .  ctype - the vector type
170c0dedaeaSBarry Smith 
171c0dedaeaSBarry Smith    Level: intermediate
172c0dedaeaSBarry Smith 
173c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
174c0dedaeaSBarry Smith @*/
175c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
176c0dedaeaSBarry Smith {
177c0dedaeaSBarry Smith   PetscFunctionBegin;
178c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
179c0dedaeaSBarry Smith   *ctype = da->vectype;
180c0dedaeaSBarry Smith   PetscFunctionReturn(0);
181c0dedaeaSBarry Smith }
182c0dedaeaSBarry Smith 
183c0dedaeaSBarry Smith #undef __FUNCT__
1845f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1855f1ad066SMatthew G Knepley /*@
18634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1875f1ad066SMatthew G Knepley 
1885f1ad066SMatthew G Knepley   Not collective
1895f1ad066SMatthew G Knepley 
1905f1ad066SMatthew G Knepley   Input Parameter:
1915f1ad066SMatthew G Knepley . v - The Vec
1925f1ad066SMatthew G Knepley 
1935f1ad066SMatthew G Knepley   Output Parameter:
1945f1ad066SMatthew G Knepley . dm - The DM
1955f1ad066SMatthew G Knepley 
1965f1ad066SMatthew G Knepley   Level: intermediate
1975f1ad066SMatthew G Knepley 
1985f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1995f1ad066SMatthew G Knepley @*/
2005f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2015f1ad066SMatthew G Knepley {
2025f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2035f1ad066SMatthew G Knepley 
2045f1ad066SMatthew G Knepley   PetscFunctionBegin;
2055f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2065f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2075f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2085f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2095f1ad066SMatthew G Knepley }
2105f1ad066SMatthew G Knepley 
2115f1ad066SMatthew G Knepley #undef __FUNCT__
2125f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2135f1ad066SMatthew G Knepley /*@
2145f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley   Not collective
2175f1ad066SMatthew G Knepley 
2185f1ad066SMatthew G Knepley   Input Parameters:
2195f1ad066SMatthew G Knepley + v - The Vec
2205f1ad066SMatthew G Knepley - dm - The DM
2215f1ad066SMatthew G Knepley 
2225f1ad066SMatthew G Knepley   Level: intermediate
2235f1ad066SMatthew G Knepley 
2245f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2255f1ad066SMatthew G Knepley @*/
2265f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2275f1ad066SMatthew G Knepley {
2285f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley   PetscFunctionBegin;
2315f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
232d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2335f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2345f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2355f1ad066SMatthew G Knepley }
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley #undef __FUNCT__
238521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
239521d9a4cSLisandro Dalcin /*@C
240521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
241521d9a4cSLisandro Dalcin 
242521d9a4cSLisandro Dalcin    Logically Collective on DM
243521d9a4cSLisandro Dalcin 
244521d9a4cSLisandro Dalcin    Input Parameter:
245521d9a4cSLisandro Dalcin +  dm - the DM context
246521d9a4cSLisandro Dalcin .  ctype - the matrix type
247521d9a4cSLisandro Dalcin 
248521d9a4cSLisandro Dalcin    Options Database:
249521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
250521d9a4cSLisandro Dalcin 
251521d9a4cSLisandro Dalcin    Level: intermediate
252521d9a4cSLisandro Dalcin 
253c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
254521d9a4cSLisandro Dalcin @*/
25519fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
256521d9a4cSLisandro Dalcin {
257521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
25888f0584fSBarry Smith 
259521d9a4cSLisandro Dalcin   PetscFunctionBegin;
260521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
261521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
26219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
263521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
264521d9a4cSLisandro Dalcin }
265521d9a4cSLisandro Dalcin 
266521d9a4cSLisandro Dalcin #undef __FUNCT__
267c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
268c0dedaeaSBarry Smith /*@C
269c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
270c0dedaeaSBarry Smith 
271c0dedaeaSBarry Smith    Logically Collective on DM
272c0dedaeaSBarry Smith 
273c0dedaeaSBarry Smith    Input Parameter:
274c0dedaeaSBarry Smith .  dm - the DM context
275c0dedaeaSBarry Smith 
276c0dedaeaSBarry Smith    Output Parameter:
277c0dedaeaSBarry Smith .  ctype - the matrix type
278c0dedaeaSBarry Smith 
279c0dedaeaSBarry Smith    Options Database:
280c0dedaeaSBarry Smith .   -dm_mat_type ctype
281c0dedaeaSBarry Smith 
282c0dedaeaSBarry Smith    Level: intermediate
283c0dedaeaSBarry Smith 
284c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
285c0dedaeaSBarry Smith @*/
286c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
287c0dedaeaSBarry Smith {
288c0dedaeaSBarry Smith   PetscFunctionBegin;
289c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
290c0dedaeaSBarry Smith   *ctype = dm->mattype;
291c0dedaeaSBarry Smith   PetscFunctionReturn(0);
292c0dedaeaSBarry Smith }
293c0dedaeaSBarry Smith 
294c0dedaeaSBarry Smith #undef __FUNCT__
295c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
296c688c046SMatthew G Knepley /*@
29734f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
298c688c046SMatthew G Knepley 
299c688c046SMatthew G Knepley   Not collective
300c688c046SMatthew G Knepley 
301c688c046SMatthew G Knepley   Input Parameter:
302c688c046SMatthew G Knepley . A - The Mat
303c688c046SMatthew G Knepley 
304c688c046SMatthew G Knepley   Output Parameter:
305c688c046SMatthew G Knepley . dm - The DM
306c688c046SMatthew G Knepley 
307c688c046SMatthew G Knepley   Level: intermediate
308c688c046SMatthew G Knepley 
309c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
310c688c046SMatthew G Knepley @*/
311c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
312c688c046SMatthew G Knepley {
313c688c046SMatthew G Knepley   PetscErrorCode ierr;
314c688c046SMatthew G Knepley 
315c688c046SMatthew G Knepley   PetscFunctionBegin;
316c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
317c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
318c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
319c688c046SMatthew G Knepley   PetscFunctionReturn(0);
320c688c046SMatthew G Knepley }
321c688c046SMatthew G Knepley 
322c688c046SMatthew G Knepley #undef __FUNCT__
323c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
324c688c046SMatthew G Knepley /*@
325c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
326c688c046SMatthew G Knepley 
327c688c046SMatthew G Knepley   Not collective
328c688c046SMatthew G Knepley 
329c688c046SMatthew G Knepley   Input Parameters:
330c688c046SMatthew G Knepley + A - The Mat
331c688c046SMatthew G Knepley - dm - The DM
332c688c046SMatthew G Knepley 
333c688c046SMatthew G Knepley   Level: intermediate
334c688c046SMatthew G Knepley 
335c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
336c688c046SMatthew G Knepley @*/
337c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
338c688c046SMatthew G Knepley {
339c688c046SMatthew G Knepley   PetscErrorCode ierr;
340c688c046SMatthew G Knepley 
341c688c046SMatthew G Knepley   PetscFunctionBegin;
342c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3438865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
344c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
345c688c046SMatthew G Knepley   PetscFunctionReturn(0);
346c688c046SMatthew G Knepley }
347c688c046SMatthew G Knepley 
348c688c046SMatthew G Knepley #undef __FUNCT__
3499a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3509a42bb27SBarry Smith /*@C
3519a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
352aa219208SBarry Smith    DMDA options in the database.
3539a42bb27SBarry Smith 
354aa219208SBarry Smith    Logically Collective on DMDA
3559a42bb27SBarry Smith 
3569a42bb27SBarry Smith    Input Parameter:
357aa219208SBarry Smith +  da - the DMDA context
3589a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3599a42bb27SBarry Smith 
3609a42bb27SBarry Smith    Notes:
3619a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3629a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3639a42bb27SBarry Smith 
3649a42bb27SBarry Smith    Level: advanced
3659a42bb27SBarry Smith 
366aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
3679a42bb27SBarry Smith 
3689a42bb27SBarry Smith .seealso: DMSetFromOptions()
3699a42bb27SBarry Smith @*/
3707087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3719a42bb27SBarry Smith {
3729a42bb27SBarry Smith   PetscErrorCode ierr;
3739a42bb27SBarry Smith 
3749a42bb27SBarry Smith   PetscFunctionBegin;
3759a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3769a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
377691be533SLawrence Mitchell   if (dm->sf) {
378691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
379691be533SLawrence Mitchell   }
380691be533SLawrence Mitchell   if (dm->defaultSF) {
381691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
382691be533SLawrence Mitchell   }
3839a42bb27SBarry Smith   PetscFunctionReturn(0);
3849a42bb27SBarry Smith }
3859a42bb27SBarry Smith 
3869a42bb27SBarry Smith #undef __FUNCT__
38747c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
38847c6ae99SBarry Smith /*@
389aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
39047c6ae99SBarry Smith 
39147c6ae99SBarry Smith     Collective on DM
39247c6ae99SBarry Smith 
39347c6ae99SBarry Smith     Input Parameter:
39447c6ae99SBarry Smith .   dm - the DM object to destroy
39547c6ae99SBarry Smith 
39647c6ae99SBarry Smith     Level: developer
39747c6ae99SBarry Smith 
398e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
39947c6ae99SBarry Smith 
40047c6ae99SBarry Smith @*/
401fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
40247c6ae99SBarry Smith {
403c83e3748SMatthew G. Knepley   PetscInt       i, cnt = 0;
404dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
40547c6ae99SBarry Smith   PetscErrorCode ierr;
40647c6ae99SBarry Smith 
40747c6ae99SBarry Smith   PetscFunctionBegin;
4086bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4096bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
41087e657c6SBarry Smith 
41187e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
412732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4138865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4148865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
415732e2eb9SMatthew G Knepley   }
416dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4172348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4182348bcf4SPeter Brune   if ((*dm)->x) {
4192348bcf4SPeter Brune     DM obj;
4202348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4212348bcf4SPeter Brune     if (obj == *dm) cnt++;
4222348bcf4SPeter Brune   }
423732e2eb9SMatthew G Knepley 
4246bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
425732e2eb9SMatthew G Knepley   /*
426732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
427732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
428732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
429732e2eb9SMatthew G Knepley   */
4306bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4316bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
432732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4336bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4346bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
435732e2eb9SMatthew G Knepley   }
436f490541aSPeter Brune   nnext=(*dm)->namedglobal;
4370298fd71SBarry Smith   (*dm)->namedglobal = NULL;
438f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
4392348bcf4SPeter Brune     nnext = nlink->next;
4402348bcf4SPeter 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);
4412348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4422348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4432348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4442348bcf4SPeter Brune   }
445f490541aSPeter Brune   nnext=(*dm)->namedlocal;
4460298fd71SBarry Smith   (*dm)->namedlocal = NULL;
447f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
448f490541aSPeter Brune     nnext = nlink->next;
449f490541aSPeter 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);
450f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
451f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
452f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
453f490541aSPeter Brune   }
4542348bcf4SPeter Brune 
455b17ce1afSJed Brown   /* Destroy the list of hooks */
456c833c3b5SJed Brown   {
457c833c3b5SJed Brown     DMCoarsenHookLink link,next;
458b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
459b17ce1afSJed Brown       next = link->next;
460b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
461b17ce1afSJed Brown     }
4620298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
463c833c3b5SJed Brown   }
464c833c3b5SJed Brown   {
465c833c3b5SJed Brown     DMRefineHookLink link,next;
466c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
467c833c3b5SJed Brown       next = link->next;
468c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
469c833c3b5SJed Brown     }
4700298fd71SBarry Smith     (*dm)->refinehook = NULL;
471c833c3b5SJed Brown   }
472be081cd6SPeter Brune   {
473be081cd6SPeter Brune     DMSubDomainHookLink link,next;
474be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
475be081cd6SPeter Brune       next = link->next;
476be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
477be081cd6SPeter Brune     }
4780298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
479be081cd6SPeter Brune   }
480baf369e7SPeter Brune   {
481baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
482baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
483baf369e7SPeter Brune       next = link->next;
484baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
485baf369e7SPeter Brune     }
4860298fd71SBarry Smith     (*dm)->gtolhook = NULL;
487baf369e7SPeter Brune   }
488d4d07f1eSToby Isaac   {
489d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
490d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
491d4d07f1eSToby Isaac       next = link->next;
492d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
493d4d07f1eSToby Isaac     }
494d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
495d4d07f1eSToby Isaac   }
496aa1993deSMatthew G Knepley   /* Destroy the work arrays */
497aa1993deSMatthew G Knepley   {
498aa1993deSMatthew G Knepley     DMWorkLink link,next;
499aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
500aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
501aa1993deSMatthew G Knepley       next = link->next;
502aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
503aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
504aa1993deSMatthew G Knepley     }
5050298fd71SBarry Smith     (*dm)->workin = NULL;
506aa1993deSMatthew G Knepley   }
507b17ce1afSJed Brown 
50852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
50952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
51052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
51152536dc3SBarry Smith 
5121a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5131a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5141a266240SBarry Smith   }
51587e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
51671cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5174dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5186bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5196bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
520073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
52188ed4aceSMatthew G Knepley 
52288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
52388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
5248b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
525fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
526fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
52788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
52888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
529*8e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
530af122d2aSMatthew G Knepley 
5316636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
5326636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
5336636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
5345dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
5356636e97aSMatthew G Knepley 
5362764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
53714f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
538e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
539e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
540732e2eb9SMatthew G Knepley 
5416bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
542435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
543732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
54447c6ae99SBarry Smith   PetscFunctionReturn(0);
54547c6ae99SBarry Smith }
54647c6ae99SBarry Smith 
54747c6ae99SBarry Smith #undef __FUNCT__
548d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
549d7bf68aeSBarry Smith /*@
550d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
551d7bf68aeSBarry Smith 
552d7bf68aeSBarry Smith     Collective on DM
553d7bf68aeSBarry Smith 
554d7bf68aeSBarry Smith     Input Parameter:
555d7bf68aeSBarry Smith .   dm - the DM object to setup
556d7bf68aeSBarry Smith 
557d7bf68aeSBarry Smith     Level: developer
558d7bf68aeSBarry Smith 
559e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
560d7bf68aeSBarry Smith 
561d7bf68aeSBarry Smith @*/
5627087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
563d7bf68aeSBarry Smith {
564d7bf68aeSBarry Smith   PetscErrorCode ierr;
565d7bf68aeSBarry Smith 
566d7bf68aeSBarry Smith   PetscFunctionBegin;
567171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5688387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
569d7bf68aeSBarry Smith   if (dm->ops->setup) {
570d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
571d7bf68aeSBarry Smith   }
5728387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
573d7bf68aeSBarry Smith   PetscFunctionReturn(0);
574d7bf68aeSBarry Smith }
575d7bf68aeSBarry Smith 
576d7bf68aeSBarry Smith #undef __FUNCT__
577d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
578d7bf68aeSBarry Smith /*@
579d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
580d7bf68aeSBarry Smith 
581d7bf68aeSBarry Smith     Collective on DM
582d7bf68aeSBarry Smith 
583d7bf68aeSBarry Smith     Input Parameter:
584d7bf68aeSBarry Smith .   dm - the DM object to set options for
585d7bf68aeSBarry Smith 
586732e2eb9SMatthew G Knepley     Options Database:
5874164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
5884164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
5894164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
5904164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
591732e2eb9SMatthew G Knepley 
592d7bf68aeSBarry Smith     Level: developer
593d7bf68aeSBarry Smith 
594e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
595d7bf68aeSBarry Smith 
596d7bf68aeSBarry Smith @*/
5977087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
598d7bf68aeSBarry Smith {
5997781c08eSBarry Smith   char           typeName[256];
600ca266f36SBarry Smith   PetscBool      flg;
601d7bf68aeSBarry Smith   PetscErrorCode ierr;
602d7bf68aeSBarry Smith 
603d7bf68aeSBarry Smith   PetscFunctionBegin;
604171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
605691be533SLawrence Mitchell   if (dm->sf) {
606691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
607691be533SLawrence Mitchell   }
608691be533SLawrence Mitchell   if (dm->defaultSF) {
609691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
610691be533SLawrence Mitchell   }
6113194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
6120298fd71SBarry 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);
613a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
614f9ba7244SBarry Smith   if (flg) {
615f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
616f9ba7244SBarry Smith   }
617a264d7a6SBarry 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);
618073dac72SJed Brown   if (flg) {
619521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
620073dac72SJed Brown   }
6210298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
622f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
623e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
624f9ba7244SBarry Smith   }
625f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
626f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
62782fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
628d7bf68aeSBarry Smith   PetscFunctionReturn(0);
629d7bf68aeSBarry Smith }
630d7bf68aeSBarry Smith 
631d7bf68aeSBarry Smith #undef __FUNCT__
63247c6ae99SBarry Smith #define __FUNCT__ "DMView"
633fc9bc008SSatish Balay /*@C
634aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
63547c6ae99SBarry Smith 
63647c6ae99SBarry Smith     Collective on DM
63747c6ae99SBarry Smith 
63847c6ae99SBarry Smith     Input Parameter:
63947c6ae99SBarry Smith +   dm - the DM object to view
64047c6ae99SBarry Smith -   v - the viewer
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith     Level: developer
64347c6ae99SBarry Smith 
644e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
64547c6ae99SBarry Smith 
64647c6ae99SBarry Smith @*/
6477087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
64847c6ae99SBarry Smith {
64947c6ae99SBarry Smith   PetscErrorCode ierr;
65032c0f0efSBarry Smith   PetscBool      isbinary;
65147c6ae99SBarry Smith 
65247c6ae99SBarry Smith   PetscFunctionBegin;
653171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6543014e516SBarry Smith   if (!v) {
655ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6563014e516SBarry Smith   }
65798c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
65832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
65932c0f0efSBarry Smith   if (isbinary) {
66055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
66132c0f0efSBarry Smith     char     type[256];
66232c0f0efSBarry Smith 
66332c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
66432c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
66532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
66632c0f0efSBarry Smith   }
6670c010503SBarry Smith   if (dm->ops->view) {
6680c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
66947c6ae99SBarry Smith   }
67047c6ae99SBarry Smith   PetscFunctionReturn(0);
67147c6ae99SBarry Smith }
67247c6ae99SBarry Smith 
67347c6ae99SBarry Smith #undef __FUNCT__
67447c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
67547c6ae99SBarry Smith /*@
676aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith     Collective on DM
67947c6ae99SBarry Smith 
68047c6ae99SBarry Smith     Input Parameter:
68147c6ae99SBarry Smith .   dm - the DM object
68247c6ae99SBarry Smith 
68347c6ae99SBarry Smith     Output Parameter:
68447c6ae99SBarry Smith .   vec - the global vector
68547c6ae99SBarry Smith 
686073dac72SJed Brown     Level: beginner
68747c6ae99SBarry Smith 
688e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
68947c6ae99SBarry Smith 
69047c6ae99SBarry Smith @*/
6917087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
69247c6ae99SBarry Smith {
69347c6ae99SBarry Smith   PetscErrorCode ierr;
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith   PetscFunctionBegin;
696171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
69747c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
69847c6ae99SBarry Smith   PetscFunctionReturn(0);
69947c6ae99SBarry Smith }
70047c6ae99SBarry Smith 
70147c6ae99SBarry Smith #undef __FUNCT__
70247c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
70347c6ae99SBarry Smith /*@
704aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith     Not Collective
70747c6ae99SBarry Smith 
70847c6ae99SBarry Smith     Input Parameter:
70947c6ae99SBarry Smith .   dm - the DM object
71047c6ae99SBarry Smith 
71147c6ae99SBarry Smith     Output Parameter:
71247c6ae99SBarry Smith .   vec - the local vector
71347c6ae99SBarry Smith 
714073dac72SJed Brown     Level: beginner
71547c6ae99SBarry Smith 
716e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith @*/
7197087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
72047c6ae99SBarry Smith {
72147c6ae99SBarry Smith   PetscErrorCode ierr;
72247c6ae99SBarry Smith 
72347c6ae99SBarry Smith   PetscFunctionBegin;
724171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
72547c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
72647c6ae99SBarry Smith   PetscFunctionReturn(0);
72747c6ae99SBarry Smith }
72847c6ae99SBarry Smith 
72947c6ae99SBarry Smith #undef __FUNCT__
7301411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
7311411c6eeSJed Brown /*@
7321411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
7331411c6eeSJed Brown 
7341411c6eeSJed Brown    Collective on DM
7351411c6eeSJed Brown 
7361411c6eeSJed Brown    Input Parameter:
7371411c6eeSJed Brown .  dm - the DM that provides the mapping
7381411c6eeSJed Brown 
7391411c6eeSJed Brown    Output Parameter:
7401411c6eeSJed Brown .  ltog - the mapping
7411411c6eeSJed Brown 
7421411c6eeSJed Brown    Level: intermediate
7431411c6eeSJed Brown 
7441411c6eeSJed Brown    Notes:
7451411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
7461411c6eeSJed Brown    MatSetLocalToGlobalMapping().
7471411c6eeSJed Brown 
748fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
7491411c6eeSJed Brown @*/
7507087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7511411c6eeSJed Brown {
7521411c6eeSJed Brown   PetscErrorCode ierr;
7531411c6eeSJed Brown 
7541411c6eeSJed Brown   PetscFunctionBegin;
7551411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7561411c6eeSJed Brown   PetscValidPointer(ltog,2);
7571411c6eeSJed Brown   if (!dm->ltogmap) {
75837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
75937d0c07bSMatthew G Knepley 
76037d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
76137d0c07bSMatthew G Knepley     if (section) {
76237d0c07bSMatthew G Knepley       PetscInt *ltog;
76337d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
76437d0c07bSMatthew G Knepley 
76537d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
76637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
76737d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
768785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
76937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
77037d0c07bSMatthew G Knepley         PetscInt dof, off, c;
77137d0c07bSMatthew G Knepley 
77237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
77337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
77437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
77537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
77637d0c07bSMatthew G Knepley           ltog[l] = off+c;
77737d0c07bSMatthew G Knepley         }
77837d0c07bSMatthew G Knepley       }
779f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
7803bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
78137d0c07bSMatthew G Knepley     } else {
782184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
783184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7841411c6eeSJed Brown     }
78537d0c07bSMatthew G Knepley   }
7861411c6eeSJed Brown   *ltog = dm->ltogmap;
7871411c6eeSJed Brown   PetscFunctionReturn(0);
7881411c6eeSJed Brown }
7891411c6eeSJed Brown 
7901411c6eeSJed Brown #undef __FUNCT__
7911411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7921411c6eeSJed Brown /*@
7931411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7941411c6eeSJed Brown 
7951411c6eeSJed Brown    Not Collective
7961411c6eeSJed Brown 
7971411c6eeSJed Brown    Input Parameter:
7981411c6eeSJed Brown .  dm - the DM with block structure
7991411c6eeSJed Brown 
8001411c6eeSJed Brown    Output Parameter:
8011411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
8021411c6eeSJed Brown 
8031411c6eeSJed Brown    Level: intermediate
8041411c6eeSJed Brown 
805fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
8061411c6eeSJed Brown @*/
8077087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
8081411c6eeSJed Brown {
8091411c6eeSJed Brown   PetscFunctionBegin;
8101411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8111411c6eeSJed Brown   PetscValidPointer(bs,2);
8121411c6eeSJed 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");
8131411c6eeSJed Brown   *bs = dm->bs;
8141411c6eeSJed Brown   PetscFunctionReturn(0);
8151411c6eeSJed Brown }
8161411c6eeSJed Brown 
8171411c6eeSJed Brown #undef __FUNCT__
818e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
81947c6ae99SBarry Smith /*@
820e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
82147c6ae99SBarry Smith 
82247c6ae99SBarry Smith     Collective on DM
82347c6ae99SBarry Smith 
82447c6ae99SBarry Smith     Input Parameter:
82547c6ae99SBarry Smith +   dm1 - the DM object
82647c6ae99SBarry Smith -   dm2 - the second, finer DM object
82747c6ae99SBarry Smith 
82847c6ae99SBarry Smith     Output Parameter:
82947c6ae99SBarry Smith +  mat - the interpolation
83047c6ae99SBarry Smith -  vec - the scaling (optional)
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith     Level: developer
83347c6ae99SBarry Smith 
83485afcc9aSBarry 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
83585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
836d52bd9f3SBarry Smith 
8371f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
838d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
83985afcc9aSBarry Smith 
84085afcc9aSBarry Smith 
841e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
84247c6ae99SBarry Smith 
84347c6ae99SBarry Smith @*/
844e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
84547c6ae99SBarry Smith {
84647c6ae99SBarry Smith   PetscErrorCode ierr;
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith   PetscFunctionBegin;
849171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
850171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
85125296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
85247c6ae99SBarry Smith   PetscFunctionReturn(0);
85347c6ae99SBarry Smith }
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith #undef __FUNCT__
856e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
85747c6ae99SBarry Smith /*@
858e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
85947c6ae99SBarry Smith 
86047c6ae99SBarry Smith     Collective on DM
86147c6ae99SBarry Smith 
86247c6ae99SBarry Smith     Input Parameter:
86347c6ae99SBarry Smith +   dm1 - the DM object
86447c6ae99SBarry Smith -   dm2 - the second, finer DM object
86547c6ae99SBarry Smith 
86647c6ae99SBarry Smith     Output Parameter:
8676dbf9973SLawrence Mitchell .   mat - the injection
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith     Level: developer
87047c6ae99SBarry Smith 
87185afcc9aSBarry 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
87285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
87385afcc9aSBarry Smith 
874e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith @*/
8776dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
87847c6ae99SBarry Smith {
87947c6ae99SBarry Smith   PetscErrorCode ierr;
88047c6ae99SBarry Smith 
88147c6ae99SBarry Smith   PetscFunctionBegin;
882171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
883171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
8846dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
88547c6ae99SBarry Smith   PetscFunctionReturn(0);
88647c6ae99SBarry Smith }
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith #undef __FUNCT__
889e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
890b412c318SBarry Smith /*@
891b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
89247c6ae99SBarry Smith 
89347c6ae99SBarry Smith     Collective on DM
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith     Input Parameter:
89647c6ae99SBarry Smith +   dm - the DM object
897b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
89847c6ae99SBarry Smith 
89947c6ae99SBarry Smith     Output Parameter:
90047c6ae99SBarry Smith .   coloring - the coloring
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith     Level: developer
90347c6ae99SBarry Smith 
904b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
90547c6ae99SBarry Smith 
906aab9d709SJed Brown @*/
907b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
90847c6ae99SBarry Smith {
90947c6ae99SBarry Smith   PetscErrorCode ierr;
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith   PetscFunctionBegin;
912171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
913ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
914b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
91547c6ae99SBarry Smith   PetscFunctionReturn(0);
91647c6ae99SBarry Smith }
91747c6ae99SBarry Smith 
91847c6ae99SBarry Smith #undef __FUNCT__
919950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
920b412c318SBarry Smith /*@
921950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
92247c6ae99SBarry Smith 
92347c6ae99SBarry Smith     Collective on DM
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith     Input Parameter:
926b412c318SBarry Smith .   dm - the DM object
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith     Output Parameter:
92947c6ae99SBarry Smith .   mat - the empty Jacobian
93047c6ae99SBarry Smith 
931073dac72SJed Brown     Level: beginner
93247c6ae99SBarry Smith 
93394013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
93494013140SBarry Smith        do not need to do it yourself.
93594013140SBarry Smith 
93694013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
937aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
93894013140SBarry Smith 
93994013140SBarry 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
94094013140SBarry Smith        internally by PETSc.
94194013140SBarry Smith 
94294013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
943aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
94494013140SBarry Smith 
945b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
94647c6ae99SBarry Smith 
947aab9d709SJed Brown @*/
948b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
94947c6ae99SBarry Smith {
95047c6ae99SBarry Smith   PetscErrorCode ierr;
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith   PetscFunctionBegin;
953171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
954607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
955c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
956c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
957b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
95847c6ae99SBarry Smith   PetscFunctionReturn(0);
95947c6ae99SBarry Smith }
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith #undef __FUNCT__
962732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
963732e2eb9SMatthew G Knepley /*@
964950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
965732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
966732e2eb9SMatthew G Knepley 
967732e2eb9SMatthew G Knepley   Logically Collective on DMDA
968732e2eb9SMatthew G Knepley 
969732e2eb9SMatthew G Knepley   Input Parameter:
970732e2eb9SMatthew G Knepley + dm - the DM
971732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
972732e2eb9SMatthew G Knepley 
973732e2eb9SMatthew G Knepley   Level: developer
974950540a4SJed Brown .seealso DMCreateMatrix()
975732e2eb9SMatthew G Knepley @*/
976732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
977732e2eb9SMatthew G Knepley {
978732e2eb9SMatthew G Knepley   PetscFunctionBegin;
979732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
980732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
981732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
982732e2eb9SMatthew G Knepley }
983732e2eb9SMatthew G Knepley 
984732e2eb9SMatthew G Knepley #undef __FUNCT__
985a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
986a89ea682SMatthew G Knepley /*@C
987aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
988a89ea682SMatthew G Knepley 
989a89ea682SMatthew G Knepley   Not Collective
990a89ea682SMatthew G Knepley 
991a89ea682SMatthew G Knepley   Input Parameters:
992a89ea682SMatthew G Knepley + dm - the DM object
993aa1993deSMatthew G Knepley . count - The minium size
994aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
995a89ea682SMatthew G Knepley 
996a89ea682SMatthew G Knepley   Output Parameter:
997a89ea682SMatthew G Knepley . array - the work array
998a89ea682SMatthew G Knepley 
999a89ea682SMatthew G Knepley   Level: developer
1000a89ea682SMatthew G Knepley 
1001a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1002a89ea682SMatthew G Knepley @*/
1003aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1004a89ea682SMatthew G Knepley {
1005a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1006aa1993deSMatthew G Knepley   DMWorkLink     link;
1007854ce69bSBarry Smith   size_t         dsize;
1008a89ea682SMatthew G Knepley 
1009a89ea682SMatthew G Knepley   PetscFunctionBegin;
1010a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1011aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1012aa1993deSMatthew G Knepley   if (dm->workin) {
1013aa1993deSMatthew G Knepley     link       = dm->workin;
1014aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1015aa1993deSMatthew G Knepley   } else {
1016b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1017a89ea682SMatthew G Knepley   }
1018854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1019854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1020aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1021854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1022854ce69bSBarry Smith     link->bytes = dsize*count;
1023aa1993deSMatthew G Knepley   }
1024aa1993deSMatthew G Knepley   link->next   = dm->workout;
1025aa1993deSMatthew G Knepley   dm->workout  = link;
1026aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1027a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1028a89ea682SMatthew G Knepley }
1029a89ea682SMatthew G Knepley 
1030aa1993deSMatthew G Knepley #undef __FUNCT__
1031aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1032aa1993deSMatthew G Knepley /*@C
1033aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1034aa1993deSMatthew G Knepley 
1035aa1993deSMatthew G Knepley   Not Collective
1036aa1993deSMatthew G Knepley 
1037aa1993deSMatthew G Knepley   Input Parameters:
1038aa1993deSMatthew G Knepley + dm - the DM object
1039aa1993deSMatthew G Knepley . count - The minium size
1040aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1041aa1993deSMatthew G Knepley 
1042aa1993deSMatthew G Knepley   Output Parameter:
1043aa1993deSMatthew G Knepley . array - the work array
1044aa1993deSMatthew G Knepley 
1045aa1993deSMatthew G Knepley   Level: developer
1046aa1993deSMatthew G Knepley 
1047aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1048aa1993deSMatthew G Knepley @*/
1049aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1050aa1993deSMatthew G Knepley {
1051aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1052aa1993deSMatthew G Knepley 
1053aa1993deSMatthew G Knepley   PetscFunctionBegin;
1054aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1055aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1056aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1057aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1058aa1993deSMatthew G Knepley       *p           = link->next;
1059aa1993deSMatthew G Knepley       link->next   = dm->workin;
1060aa1993deSMatthew G Knepley       dm->workin   = link;
10610298fd71SBarry Smith       *(void**)mem = NULL;
1062aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1063aa1993deSMatthew G Knepley     }
1064aa1993deSMatthew G Knepley   }
1065aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1066aa1993deSMatthew G Knepley }
1067e7c4fc90SDmitry Karpeev 
1068e7c4fc90SDmitry Karpeev #undef __FUNCT__
1069435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1070435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1071435a35e8SMatthew G Knepley {
1072435a35e8SMatthew G Knepley   PetscFunctionBegin;
1073435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
107482f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1075435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1076435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1077435a35e8SMatthew G Knepley }
1078435a35e8SMatthew G Knepley 
1079435a35e8SMatthew G Knepley #undef __FUNCT__
10804d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10814f3b5142SJed Brown /*@C
10824d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10834d343eeaSMatthew G Knepley 
10844d343eeaSMatthew G Knepley   Not collective
10854d343eeaSMatthew G Knepley 
10864d343eeaSMatthew G Knepley   Input Parameter:
10874d343eeaSMatthew G Knepley . dm - the DM object
10884d343eeaSMatthew G Knepley 
10894d343eeaSMatthew G Knepley   Output Parameters:
10900298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10910298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10920298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10934d343eeaSMatthew G Knepley 
10944d343eeaSMatthew G Knepley   Level: intermediate
10954d343eeaSMatthew G Knepley 
109621c9b008SJed Brown   Notes:
109721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
109821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
109921c9b008SJed Brown   PetscFree().
110021c9b008SJed Brown 
11014d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11024d343eeaSMatthew G Knepley @*/
110337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11044d343eeaSMatthew G Knepley {
110537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11064d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11074d343eeaSMatthew G Knepley 
11084d343eeaSMatthew G Knepley   PetscFunctionBegin;
11094d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
111069ca1f37SDmitry Karpeev   if (numFields) {
111169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
111269ca1f37SDmitry Karpeev     *numFields = 0;
111369ca1f37SDmitry Karpeev   }
111437d0c07bSMatthew G Knepley   if (fieldNames) {
111537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11160298fd71SBarry Smith     *fieldNames = NULL;
111769ca1f37SDmitry Karpeev   }
111869ca1f37SDmitry Karpeev   if (fields) {
111969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11200298fd71SBarry Smith     *fields = NULL;
112169ca1f37SDmitry Karpeev   }
112237d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
112337d0c07bSMatthew G Knepley   if (section) {
112437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
112537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
112637d0c07bSMatthew G Knepley 
112737d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
112837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1129dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
113037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
113137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
113237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113337d0c07bSMatthew G Knepley     }
113437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
113537d0c07bSMatthew G Knepley       PetscInt gdof;
113637d0c07bSMatthew G Knepley 
113737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
113837d0c07bSMatthew G Knepley       if (gdof > 0) {
113937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
114137d0c07bSMatthew G Knepley 
114237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
114337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
114437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
114537d0c07bSMatthew G Knepley         }
114637d0c07bSMatthew G Knepley       }
114737d0c07bSMatthew G Knepley     }
114837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1149785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
115037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
115137d0c07bSMatthew G Knepley     }
115237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
115337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
115437d0c07bSMatthew G Knepley 
115537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
115637d0c07bSMatthew G Knepley       if (gdof > 0) {
115737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
115837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
115937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
116037d0c07bSMatthew G Knepley 
116137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
116237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
116337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
116437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
116537d0c07bSMatthew G Knepley           }
116637d0c07bSMatthew G Knepley         }
116737d0c07bSMatthew G Knepley       }
116837d0c07bSMatthew G Knepley     }
11698865f1eaSKarl Rupp     if (numFields) *numFields = nF;
117037d0c07bSMatthew G Knepley     if (fieldNames) {
1171785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
117237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
117337d0c07bSMatthew G Knepley         const char *fieldName;
117437d0c07bSMatthew G Knepley 
117537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
117637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
117737d0c07bSMatthew G Knepley       }
117837d0c07bSMatthew G Knepley     }
117937d0c07bSMatthew G Knepley     if (fields) {
1180785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
118137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
118282f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
118337d0c07bSMatthew G Knepley       }
118437d0c07bSMatthew G Knepley     }
118537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11868865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11878865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
118869ca1f37SDmitry Karpeev   }
11894d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11904d343eeaSMatthew G Knepley }
11914d343eeaSMatthew G Knepley 
119216621825SDmitry Karpeev 
119316621825SDmitry Karpeev #undef __FUNCT__
119416621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
119516621825SDmitry Karpeev /*@C
119616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
119716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
119816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1199e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1200e7c4fc90SDmitry Karpeev 
1201e7c4fc90SDmitry Karpeev   Not collective
1202e7c4fc90SDmitry Karpeev 
1203e7c4fc90SDmitry Karpeev   Input Parameter:
1204e7c4fc90SDmitry Karpeev . dm - the DM object
1205e7c4fc90SDmitry Karpeev 
1206e7c4fc90SDmitry Karpeev   Output Parameters:
12070298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
12080298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
12090298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12100298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1211e7c4fc90SDmitry Karpeev 
1212e7c4fc90SDmitry Karpeev   Level: intermediate
1213e7c4fc90SDmitry Karpeev 
1214e7c4fc90SDmitry Karpeev   Notes:
1215e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1216e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1217e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1218e7c4fc90SDmitry Karpeev 
1219e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1220e7c4fc90SDmitry Karpeev @*/
122116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1222e7c4fc90SDmitry Karpeev {
1223e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1224e7c4fc90SDmitry Karpeev 
1225e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1226e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12278865f1eaSKarl Rupp   if (len) {
12288865f1eaSKarl Rupp     PetscValidPointer(len,2);
12298865f1eaSKarl Rupp     *len = 0;
12308865f1eaSKarl Rupp   }
12318865f1eaSKarl Rupp   if (namelist) {
12328865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12338865f1eaSKarl Rupp     *namelist = 0;
12348865f1eaSKarl Rupp   }
12358865f1eaSKarl Rupp   if (islist) {
12368865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12378865f1eaSKarl Rupp     *islist = 0;
12388865f1eaSKarl Rupp   }
12398865f1eaSKarl Rupp   if (dmlist) {
12408865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12418865f1eaSKarl Rupp     *dmlist = 0;
12428865f1eaSKarl Rupp   }
1243f3f0edfdSDmitry Karpeev   /*
1244f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1245f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1246f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1247f3f0edfdSDmitry Karpeev    */
1248ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
124916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1250435a35e8SMatthew G Knepley     PetscSection section;
1251435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1252435a35e8SMatthew G Knepley 
1253435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1254435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1255435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1256435a35e8SMatthew G Knepley       *len = numFields;
125703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
125803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
125903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1260435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1261435a35e8SMatthew G Knepley         const char *fieldName;
1262435a35e8SMatthew G Knepley 
126303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
126403dc3394SMatthew G. Knepley         if (namelist) {
1265435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1266435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1267435a35e8SMatthew G Knepley         }
126803dc3394SMatthew G. Knepley       }
1269435a35e8SMatthew G Knepley     } else {
127069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1271e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12720298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1273e7c4fc90SDmitry Karpeev     }
12748865f1eaSKarl Rupp   } else {
127516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
127616621825SDmitry Karpeev   }
127716621825SDmitry Karpeev   PetscFunctionReturn(0);
127816621825SDmitry Karpeev }
127916621825SDmitry Karpeev 
128016621825SDmitry Karpeev #undef __FUNCT__
1281435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1282564cec59SMatthew G. Knepley /*@
1283435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1284435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1285435a35e8SMatthew G Knepley 
1286435a35e8SMatthew G Knepley   Not collective
1287435a35e8SMatthew G Knepley 
1288435a35e8SMatthew G Knepley   Input Parameters:
1289435a35e8SMatthew G Knepley + dm - the DM object
1290435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12910298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1292435a35e8SMatthew G Knepley 
1293435a35e8SMatthew G Knepley   Output Parameters:
1294435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1295435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1296435a35e8SMatthew G Knepley 
1297435a35e8SMatthew G Knepley   Level: intermediate
1298435a35e8SMatthew G Knepley 
1299435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1300435a35e8SMatthew G Knepley @*/
1301435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1302435a35e8SMatthew G Knepley {
1303435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1304435a35e8SMatthew G Knepley 
1305435a35e8SMatthew G Knepley   PetscFunctionBegin;
1306435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1307435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
13088865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
13098865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1310435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1311435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
131282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1313435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1314435a35e8SMatthew G Knepley }
1315435a35e8SMatthew G Knepley 
131616621825SDmitry Karpeev 
131716621825SDmitry Karpeev #undef __FUNCT__
131816621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
131916621825SDmitry Karpeev /*@C
13208d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13218d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13228d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13238d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13248d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
132516621825SDmitry Karpeev 
132616621825SDmitry Karpeev   Not collective
132716621825SDmitry Karpeev 
132816621825SDmitry Karpeev   Input Parameter:
132916621825SDmitry Karpeev . dm - the DM object
133016621825SDmitry Karpeev 
133116621825SDmitry Karpeev   Output Parameters:
13320298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13330298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13340298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13350298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13360298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
133716621825SDmitry Karpeev 
133816621825SDmitry Karpeev   Level: intermediate
133916621825SDmitry Karpeev 
134016621825SDmitry Karpeev   Notes:
134116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
134216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
134316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
134416621825SDmitry Karpeev 
13458d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
134616621825SDmitry Karpeev @*/
13478d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
134816621825SDmitry Karpeev {
134916621825SDmitry Karpeev   PetscErrorCode      ierr;
1350be081cd6SPeter Brune   DMSubDomainHookLink link;
1351be081cd6SPeter Brune   PetscInt            i,l;
135216621825SDmitry Karpeev 
135316621825SDmitry Karpeev   PetscFunctionBegin;
135416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
135514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13560298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13570298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13580298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13590298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1360f3f0edfdSDmitry Karpeev   /*
1361f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1362f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1363f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1364f3f0edfdSDmitry Karpeev    */
1365ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
136616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1367be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
136814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
136914a18fd3SPeter Brune     if (dmlist) {
1370be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1371be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1372be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1373be081cd6SPeter Brune         }
1374d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1375e7c4fc90SDmitry Karpeev       }
137614a18fd3SPeter Brune     }
137714a18fd3SPeter Brune     if (len) *len = l;
137814a18fd3SPeter Brune   }
1379e30e807fSPeter Brune   PetscFunctionReturn(0);
1380e30e807fSPeter Brune }
1381e30e807fSPeter Brune 
1382e30e807fSPeter Brune 
1383e30e807fSPeter Brune #undef __FUNCT__
1384e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1385e30e807fSPeter Brune /*@C
1386e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1387e30e807fSPeter Brune 
1388e30e807fSPeter Brune   Not collective
1389e30e807fSPeter Brune 
1390e30e807fSPeter Brune   Input Parameters:
1391e30e807fSPeter Brune + dm - the DM object
1392e30e807fSPeter Brune . n  - the number of subdomain scatters
1393e30e807fSPeter Brune - subdms - the local subdomains
1394e30e807fSPeter Brune 
1395e30e807fSPeter Brune   Output Parameters:
1396e30e807fSPeter Brune + n     - the number of scatters returned
1397e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1398e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1399e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1400e30e807fSPeter Brune 
1401e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1402e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1403e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1404e30e807fSPeter Brune   solution and residual data.
1405e30e807fSPeter Brune 
1406e30e807fSPeter Brune   Level: developer
1407e30e807fSPeter Brune 
1408e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1409e30e807fSPeter Brune @*/
1410e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1411e30e807fSPeter Brune {
1412e30e807fSPeter Brune   PetscErrorCode ierr;
1413e30e807fSPeter Brune 
1414e30e807fSPeter Brune   PetscFunctionBegin;
1415e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1416e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1417e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1418e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
141982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1420e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1421e7c4fc90SDmitry Karpeev }
1422e7c4fc90SDmitry Karpeev 
1423731c8d9eSDmitry Karpeev #undef __FUNCT__
142447c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
142547c6ae99SBarry Smith /*@
142647c6ae99SBarry Smith   DMRefine - Refines a DM object
142747c6ae99SBarry Smith 
142847c6ae99SBarry Smith   Collective on DM
142947c6ae99SBarry Smith 
143047c6ae99SBarry Smith   Input Parameter:
143147c6ae99SBarry Smith + dm   - the DM object
143291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
143347c6ae99SBarry Smith 
143447c6ae99SBarry Smith   Output Parameter:
14350298fd71SBarry Smith . dmf - the refined DM, or NULL
1436ae0a1c52SMatthew G Knepley 
14370298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
143847c6ae99SBarry Smith 
143947c6ae99SBarry Smith   Level: developer
144047c6ae99SBarry Smith 
1441e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
144247c6ae99SBarry Smith @*/
14437087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
144447c6ae99SBarry Smith {
144547c6ae99SBarry Smith   PetscErrorCode   ierr;
1446c833c3b5SJed Brown   DMRefineHookLink link;
144747c6ae99SBarry Smith 
144847c6ae99SBarry Smith   PetscFunctionBegin;
1449732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
145047c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14514057135bSMatthew G Knepley   if (*dmf) {
145243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14538865f1eaSKarl Rupp 
14548cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14558865f1eaSKarl Rupp 
1456644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14570598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1458656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14598865f1eaSKarl Rupp 
1460e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1461c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14628865f1eaSKarl Rupp       if (link->refinehook) {
14638865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14648865f1eaSKarl Rupp       }
1465c833c3b5SJed Brown     }
1466c833c3b5SJed Brown   }
1467c833c3b5SJed Brown   PetscFunctionReturn(0);
1468c833c3b5SJed Brown }
1469c833c3b5SJed Brown 
1470c833c3b5SJed Brown #undef __FUNCT__
1471c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1472bb9467b5SJed Brown /*@C
1473c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1474c833c3b5SJed Brown 
1475c833c3b5SJed Brown    Logically Collective
1476c833c3b5SJed Brown 
1477c833c3b5SJed Brown    Input Arguments:
1478c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1479c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1480c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14810298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1482c833c3b5SJed Brown 
1483c833c3b5SJed Brown    Calling sequence of refinehook:
1484c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1485c833c3b5SJed Brown 
1486c833c3b5SJed Brown +  coarse - coarse level DM
1487c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1488c833c3b5SJed Brown -  ctx - optional user-defined function context
1489c833c3b5SJed Brown 
1490c833c3b5SJed Brown    Calling sequence for interphook:
1491c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1492c833c3b5SJed Brown 
1493c833c3b5SJed Brown +  coarse - coarse level DM
1494c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1495c833c3b5SJed Brown .  fine - fine level DM to update
1496c833c3b5SJed Brown -  ctx - optional user-defined function context
1497c833c3b5SJed Brown 
1498c833c3b5SJed Brown    Level: advanced
1499c833c3b5SJed Brown 
1500c833c3b5SJed Brown    Notes:
1501c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1502c833c3b5SJed Brown 
1503c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1504c833c3b5SJed Brown 
1505bb9467b5SJed Brown    This function is currently not available from Fortran.
1506bb9467b5SJed Brown 
1507c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1508c833c3b5SJed Brown @*/
1509c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1510c833c3b5SJed Brown {
1511c833c3b5SJed Brown   PetscErrorCode   ierr;
1512c833c3b5SJed Brown   DMRefineHookLink link,*p;
1513c833c3b5SJed Brown 
1514c833c3b5SJed Brown   PetscFunctionBegin;
1515c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1516c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1517c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1518c833c3b5SJed Brown   link->refinehook = refinehook;
1519c833c3b5SJed Brown   link->interphook = interphook;
1520c833c3b5SJed Brown   link->ctx        = ctx;
15210298fd71SBarry Smith   link->next       = NULL;
1522c833c3b5SJed Brown   *p               = link;
1523c833c3b5SJed Brown   PetscFunctionReturn(0);
1524c833c3b5SJed Brown }
1525c833c3b5SJed Brown 
1526c833c3b5SJed Brown #undef __FUNCT__
1527c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1528c833c3b5SJed Brown /*@
1529c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1530c833c3b5SJed Brown 
1531c833c3b5SJed Brown    Collective if any hooks are
1532c833c3b5SJed Brown 
1533c833c3b5SJed Brown    Input Arguments:
1534c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1535c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1536c833c3b5SJed Brown -  fine - finer DM to update
1537c833c3b5SJed Brown 
1538c833c3b5SJed Brown    Level: developer
1539c833c3b5SJed Brown 
1540c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1541c833c3b5SJed Brown @*/
1542c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1543c833c3b5SJed Brown {
1544c833c3b5SJed Brown   PetscErrorCode   ierr;
1545c833c3b5SJed Brown   DMRefineHookLink link;
1546c833c3b5SJed Brown 
1547c833c3b5SJed Brown   PetscFunctionBegin;
1548c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15498865f1eaSKarl Rupp     if (link->interphook) {
15508865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15518865f1eaSKarl Rupp     }
15524057135bSMatthew G Knepley   }
155347c6ae99SBarry Smith   PetscFunctionReturn(0);
155447c6ae99SBarry Smith }
155547c6ae99SBarry Smith 
155647c6ae99SBarry Smith #undef __FUNCT__
1557eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1558eb3f98d2SBarry Smith /*@
1559eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1560eb3f98d2SBarry Smith 
1561eb3f98d2SBarry Smith     Not Collective
1562eb3f98d2SBarry Smith 
1563eb3f98d2SBarry Smith     Input Parameter:
1564eb3f98d2SBarry Smith .   dm - the DM object
1565eb3f98d2SBarry Smith 
1566eb3f98d2SBarry Smith     Output Parameter:
1567eb3f98d2SBarry Smith .   level - number of refinements
1568eb3f98d2SBarry Smith 
1569eb3f98d2SBarry Smith     Level: developer
1570eb3f98d2SBarry Smith 
15716a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1572eb3f98d2SBarry Smith 
1573eb3f98d2SBarry Smith @*/
1574eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1575eb3f98d2SBarry Smith {
1576eb3f98d2SBarry Smith   PetscFunctionBegin;
1577eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1578eb3f98d2SBarry Smith   *level = dm->levelup;
1579eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1580eb3f98d2SBarry Smith }
1581eb3f98d2SBarry Smith 
1582eb3f98d2SBarry Smith #undef __FUNCT__
1583baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1584bb9467b5SJed Brown /*@C
1585baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1586baf369e7SPeter Brune 
1587baf369e7SPeter Brune    Logically Collective
1588baf369e7SPeter Brune 
1589baf369e7SPeter Brune    Input Arguments:
1590baf369e7SPeter Brune +  dm - the DM
1591baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1592baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15930298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1594baf369e7SPeter Brune 
1595baf369e7SPeter Brune    Calling sequence for beginhook:
1596baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1597baf369e7SPeter Brune 
1598baf369e7SPeter Brune +  dm - global DM
1599baf369e7SPeter Brune .  g - global vector
1600baf369e7SPeter Brune .  mode - mode
1601baf369e7SPeter Brune .  l - local vector
1602baf369e7SPeter Brune -  ctx - optional user-defined function context
1603baf369e7SPeter Brune 
1604baf369e7SPeter Brune 
1605baf369e7SPeter Brune    Calling sequence for endhook:
1606ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1607baf369e7SPeter Brune 
1608baf369e7SPeter Brune +  global - global DM
1609baf369e7SPeter Brune -  ctx - optional user-defined function context
1610baf369e7SPeter Brune 
1611baf369e7SPeter Brune    Level: advanced
1612baf369e7SPeter Brune 
1613baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1614baf369e7SPeter Brune @*/
1615baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1616baf369e7SPeter Brune {
1617baf369e7SPeter Brune   PetscErrorCode          ierr;
1618baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1619baf369e7SPeter Brune 
1620baf369e7SPeter Brune   PetscFunctionBegin;
1621baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1622baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1623baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1624baf369e7SPeter Brune   link->beginhook = beginhook;
1625baf369e7SPeter Brune   link->endhook   = endhook;
1626baf369e7SPeter Brune   link->ctx       = ctx;
16270298fd71SBarry Smith   link->next      = NULL;
1628baf369e7SPeter Brune   *p              = link;
1629baf369e7SPeter Brune   PetscFunctionReturn(0);
1630baf369e7SPeter Brune }
1631baf369e7SPeter Brune 
1632baf369e7SPeter Brune #undef __FUNCT__
16334c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
16344c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
16354c274da1SToby Isaac {
16364c274da1SToby Isaac   Mat cMat;
16374c274da1SToby Isaac   Vec cVec;
16384c274da1SToby Isaac   PetscSection section, cSec;
16394c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
16404c274da1SToby Isaac   PetscErrorCode ierr;
16414c274da1SToby Isaac 
16424c274da1SToby Isaac   PetscFunctionBegin;
16434c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16444c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
16454c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
16464c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
16477711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
16484c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
16494c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
16504c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
16514c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
16524c274da1SToby Isaac       if (dof) {
16534c274da1SToby Isaac         PetscScalar *vals;
16544c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
16554c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
16564c274da1SToby Isaac       }
16574c274da1SToby Isaac     }
16584c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
16594c274da1SToby Isaac   }
16604c274da1SToby Isaac   PetscFunctionReturn(0);
16614c274da1SToby Isaac }
16624c274da1SToby Isaac 
16634c274da1SToby Isaac #undef __FUNCT__
166447c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
166547c6ae99SBarry Smith /*@
166647c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
166747c6ae99SBarry Smith 
166847c6ae99SBarry Smith     Neighbor-wise Collective on DM
166947c6ae99SBarry Smith 
167047c6ae99SBarry Smith     Input Parameters:
167147c6ae99SBarry Smith +   dm - the DM object
167247c6ae99SBarry Smith .   g - the global vector
167347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
167447c6ae99SBarry Smith -   l - the local vector
167547c6ae99SBarry Smith 
167647c6ae99SBarry Smith 
167747c6ae99SBarry Smith     Level: beginner
167847c6ae99SBarry Smith 
1679e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
168047c6ae99SBarry Smith 
168147c6ae99SBarry Smith @*/
16827087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
168347c6ae99SBarry Smith {
16847128ae9fSMatthew G Knepley   PetscSF                 sf;
168547c6ae99SBarry Smith   PetscErrorCode          ierr;
1686baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
168747c6ae99SBarry Smith 
168847c6ae99SBarry Smith   PetscFunctionBegin;
1689171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1690baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16918865f1eaSKarl Rupp     if (link->beginhook) {
16928865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16938865f1eaSKarl Rupp     }
1694baf369e7SPeter Brune   }
16957128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16967128ae9fSMatthew G Knepley   if (sf) {
1697ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1698ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
16997128ae9fSMatthew G Knepley 
170082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17017128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1702ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
17037128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17047128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1705ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
17067128ae9fSMatthew G Knepley   } else {
1707843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17087128ae9fSMatthew G Knepley   }
170947c6ae99SBarry Smith   PetscFunctionReturn(0);
171047c6ae99SBarry Smith }
171147c6ae99SBarry Smith 
171247c6ae99SBarry Smith #undef __FUNCT__
171347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
171447c6ae99SBarry Smith /*@
171547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
171647c6ae99SBarry Smith 
171747c6ae99SBarry Smith     Neighbor-wise Collective on DM
171847c6ae99SBarry Smith 
171947c6ae99SBarry Smith     Input Parameters:
172047c6ae99SBarry Smith +   dm - the DM object
172147c6ae99SBarry Smith .   g - the global vector
172247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
172347c6ae99SBarry Smith -   l - the local vector
172447c6ae99SBarry Smith 
172547c6ae99SBarry Smith 
172647c6ae99SBarry Smith     Level: beginner
172747c6ae99SBarry Smith 
1728e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
172947c6ae99SBarry Smith 
173047c6ae99SBarry Smith @*/
17317087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
173247c6ae99SBarry Smith {
17337128ae9fSMatthew G Knepley   PetscSF                 sf;
173447c6ae99SBarry Smith   PetscErrorCode          ierr;
1735ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1736ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1737baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
173847c6ae99SBarry Smith 
173947c6ae99SBarry Smith   PetscFunctionBegin;
1740171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17417128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17427128ae9fSMatthew G Knepley   if (sf) {
174382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17447128ae9fSMatthew G Knepley 
17457128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1746ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
17477128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17487128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1749ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
17507128ae9fSMatthew G Knepley   } else {
1751843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17527128ae9fSMatthew G Knepley   }
17534c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1754baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1755baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1756baf369e7SPeter Brune   }
175747c6ae99SBarry Smith   PetscFunctionReturn(0);
175847c6ae99SBarry Smith }
175947c6ae99SBarry Smith 
176047c6ae99SBarry Smith #undef __FUNCT__
1761d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1762d4d07f1eSToby Isaac /*@C
1763d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1764d4d07f1eSToby Isaac 
1765d4d07f1eSToby Isaac    Logically Collective
1766d4d07f1eSToby Isaac 
1767d4d07f1eSToby Isaac    Input Arguments:
1768d4d07f1eSToby Isaac +  dm - the DM
1769d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1770d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1771d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1772d4d07f1eSToby Isaac 
1773d4d07f1eSToby Isaac    Calling sequence for beginhook:
1774d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1775d4d07f1eSToby Isaac 
1776d4d07f1eSToby Isaac +  dm - global DM
1777d4d07f1eSToby Isaac .  l - local vector
1778d4d07f1eSToby Isaac .  mode - mode
1779d4d07f1eSToby Isaac .  g - global vector
1780d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1781d4d07f1eSToby Isaac 
1782d4d07f1eSToby Isaac 
1783d4d07f1eSToby Isaac    Calling sequence for endhook:
1784d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1785d4d07f1eSToby Isaac 
1786d4d07f1eSToby Isaac +  global - global DM
1787d4d07f1eSToby Isaac .  l - local vector
1788d4d07f1eSToby Isaac .  mode - mode
1789d4d07f1eSToby Isaac .  g - global vector
1790d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1791d4d07f1eSToby Isaac 
1792d4d07f1eSToby Isaac    Level: advanced
1793d4d07f1eSToby Isaac 
1794d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1795d4d07f1eSToby Isaac @*/
1796d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1797d4d07f1eSToby Isaac {
1798d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1799d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1800d4d07f1eSToby Isaac 
1801d4d07f1eSToby Isaac   PetscFunctionBegin;
1802d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1803d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1804d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1805d4d07f1eSToby Isaac   link->beginhook = beginhook;
1806d4d07f1eSToby Isaac   link->endhook   = endhook;
1807d4d07f1eSToby Isaac   link->ctx       = ctx;
1808d4d07f1eSToby Isaac   link->next      = NULL;
1809d4d07f1eSToby Isaac   *p              = link;
1810d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1811d4d07f1eSToby Isaac }
1812d4d07f1eSToby Isaac 
1813d4d07f1eSToby Isaac #undef __FUNCT__
18144c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
18154c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
18164c274da1SToby Isaac {
18174c274da1SToby Isaac   Mat cMat;
18184c274da1SToby Isaac   Vec cVec;
18194c274da1SToby Isaac   PetscSection section, cSec;
18204c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18214c274da1SToby Isaac   PetscErrorCode ierr;
18224c274da1SToby Isaac 
18234c274da1SToby Isaac   PetscFunctionBegin;
18244c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18254c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18264c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
18274c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
18287711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
18294c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
18304c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
18314c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
18324c274da1SToby Isaac       if (dof) {
18334c274da1SToby Isaac         PetscInt d;
18344c274da1SToby Isaac         PetscScalar *vals;
18354c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
18364c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
18374c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
18384c274da1SToby Isaac          * we just extracted */
18394c274da1SToby Isaac         for (d = 0; d < dof; d++) {
18404c274da1SToby Isaac           vals[d] = 0.;
18414c274da1SToby Isaac         }
18424c274da1SToby Isaac       }
18434c274da1SToby Isaac     }
18444c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
18454c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
18464c274da1SToby Isaac   }
18474c274da1SToby Isaac   PetscFunctionReturn(0);
18484c274da1SToby Isaac }
18494c274da1SToby Isaac 
18504c274da1SToby Isaac #undef __FUNCT__
18519a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
185247c6ae99SBarry Smith /*@
18539a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
18549a42bb27SBarry Smith 
18559a42bb27SBarry Smith     Neighbor-wise Collective on DM
18569a42bb27SBarry Smith 
18579a42bb27SBarry Smith     Input Parameters:
18589a42bb27SBarry Smith +   dm - the DM object
1859f6813fd5SJed Brown .   l - the local vector
18601eb28f2eSBarry 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.
18611eb28f2eSBarry Smith -   g - the global vector
18629a42bb27SBarry Smith 
1863d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
186484330215SMatthew 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.
18659a42bb27SBarry Smith 
18669a42bb27SBarry Smith     Level: beginner
18679a42bb27SBarry Smith 
1868e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
18699a42bb27SBarry Smith 
18709a42bb27SBarry Smith @*/
18717087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
18729a42bb27SBarry Smith {
18737128ae9fSMatthew G Knepley   PetscSF                 sf;
187484330215SMatthew G. Knepley   PetscSection            s, gs;
1875d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
1876ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
1877ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
187884330215SMatthew G. Knepley   PetscBool               isInsert;
187984330215SMatthew G. Knepley   PetscErrorCode          ierr;
18809a42bb27SBarry Smith 
18819a42bb27SBarry Smith   PetscFunctionBegin;
1882171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1883d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1884d4d07f1eSToby Isaac     if (link->beginhook) {
1885d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
1886d4d07f1eSToby Isaac     }
1887d4d07f1eSToby Isaac   }
18884c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
18897128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
189084330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
18917128ae9fSMatthew G Knepley   switch (mode) {
18927128ae9fSMatthew G Knepley   case INSERT_VALUES:
18937128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
189484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
18957128ae9fSMatthew G Knepley   case ADD_VALUES:
18967128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
189784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
18987128ae9fSMatthew G Knepley   default:
189982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19007128ae9fSMatthew G Knepley   }
190184330215SMatthew G. Knepley   if (sf && !isInsert) {
1902ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
19037128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
1904a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
1905ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
190684330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
190784330215SMatthew G. Knepley   } else if (s && isInsert) {
190884330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
190984330215SMatthew G. Knepley 
191084330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
191184330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
191284330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
1913ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
191484330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
191584330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
1916b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
191784330215SMatthew G. Knepley 
191884330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
191903442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
192084330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
1921b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
192284330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
192384330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
1924b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
192503442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
1926b3b16f48SMatthew 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);
1927b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
1928b3b16f48SMatthew G. Knepley       if (!gcdof) {
192984330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
1930b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
1931b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
193284330215SMatthew G. Knepley         const PetscInt *cdofs;
193384330215SMatthew G. Knepley         PetscInt        cind = 0;
193484330215SMatthew G. Knepley 
193584330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
1936b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
193784330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
1938b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
193984330215SMatthew G. Knepley         }
1940b3b16f48SMatthew 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);
194184330215SMatthew G. Knepley     }
1942ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
19437128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
19447128ae9fSMatthew G Knepley   } else {
1945843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
19467128ae9fSMatthew G Knepley   }
19479a42bb27SBarry Smith   PetscFunctionReturn(0);
19489a42bb27SBarry Smith }
19499a42bb27SBarry Smith 
19509a42bb27SBarry Smith #undef __FUNCT__
19519a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
19529a42bb27SBarry Smith /*@
19539a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
195447c6ae99SBarry Smith 
195547c6ae99SBarry Smith     Neighbor-wise Collective on DM
195647c6ae99SBarry Smith 
195747c6ae99SBarry Smith     Input Parameters:
195847c6ae99SBarry Smith +   dm - the DM object
1959f6813fd5SJed Brown .   l - the local vector
196047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1961f6813fd5SJed Brown -   g - the global vector
196247c6ae99SBarry Smith 
196347c6ae99SBarry Smith 
196447c6ae99SBarry Smith     Level: beginner
196547c6ae99SBarry Smith 
1966e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
196747c6ae99SBarry Smith 
196847c6ae99SBarry Smith @*/
19697087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
197047c6ae99SBarry Smith {
19717128ae9fSMatthew G Knepley   PetscSF                 sf;
197284330215SMatthew G. Knepley   PetscSection            s;
1973d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
197484330215SMatthew G. Knepley   PetscBool               isInsert;
197584330215SMatthew G. Knepley   PetscErrorCode          ierr;
197647c6ae99SBarry Smith 
197747c6ae99SBarry Smith   PetscFunctionBegin;
1978171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19797128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
198084330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19817128ae9fSMatthew G Knepley   switch (mode) {
19827128ae9fSMatthew G Knepley   case INSERT_VALUES:
19837128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
198484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
19857128ae9fSMatthew G Knepley   case ADD_VALUES:
19867128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
198784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
19887128ae9fSMatthew G Knepley   default:
198982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19907128ae9fSMatthew G Knepley   }
199184330215SMatthew G. Knepley   if (sf && !isInsert) {
1992ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
1993ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
199484330215SMatthew G. Knepley 
1995ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
19967128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
1997a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
1998ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
19997128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
200084330215SMatthew G. Knepley   } else if (s && isInsert) {
20017128ae9fSMatthew G Knepley   } else {
2002843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20037128ae9fSMatthew G Knepley   }
2004d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2005d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2006d4d07f1eSToby Isaac   }
200747c6ae99SBarry Smith   PetscFunctionReturn(0);
200847c6ae99SBarry Smith }
200947c6ae99SBarry Smith 
201047c6ae99SBarry Smith #undef __FUNCT__
2011f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2012f089877aSRichard Tran Mills /*@
2013bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2014bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2015d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2016f089877aSRichard Tran Mills 
2017bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2018f089877aSRichard Tran Mills 
2019f089877aSRichard Tran Mills    Input Parameters:
2020f089877aSRichard Tran Mills +  dm - the DM object
2021bc0a1609SRichard Tran Mills .  g - the original local vector
2022bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2023f089877aSRichard Tran Mills 
2024bc0a1609SRichard Tran Mills    Output Parameter:
2025bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2026f089877aSRichard Tran Mills 
2027f089877aSRichard Tran Mills    Level: intermediate
2028f089877aSRichard Tran Mills 
2029bc0a1609SRichard Tran Mills    Notes:
2030bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2031bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2032bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2033bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2034bc0a1609SRichard Tran Mills 
2035bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2036bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2037f089877aSRichard Tran Mills 
2038f089877aSRichard Tran Mills @*/
2039f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2040f089877aSRichard Tran Mills {
2041f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2042f089877aSRichard Tran Mills 
2043f089877aSRichard Tran Mills   PetscFunctionBegin;
2044f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2045f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2046f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2047f089877aSRichard Tran Mills }
2048f089877aSRichard Tran Mills 
2049f089877aSRichard Tran Mills #undef __FUNCT__
2050f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2051f089877aSRichard Tran Mills /*@
2052bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2053bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2054d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2055f089877aSRichard Tran Mills 
2056bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2057f089877aSRichard Tran Mills 
2058f089877aSRichard Tran Mills    Input Parameters:
2059bc0a1609SRichard Tran Mills +  da - the DM object
2060bc0a1609SRichard Tran Mills .  g - the original local vector
2061bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2062f089877aSRichard Tran Mills 
2063bc0a1609SRichard Tran Mills    Output Parameter:
2064bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2065f089877aSRichard Tran Mills 
2066f089877aSRichard Tran Mills    Level: intermediate
2067f089877aSRichard Tran Mills 
2068bc0a1609SRichard Tran Mills    Notes:
2069bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2070bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2071bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2072bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2073bc0a1609SRichard Tran Mills 
2074bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2075bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2076f089877aSRichard Tran Mills 
2077f089877aSRichard Tran Mills @*/
2078f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2079f089877aSRichard Tran Mills {
2080f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2081f089877aSRichard Tran Mills 
2082f089877aSRichard Tran Mills   PetscFunctionBegin;
2083f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2084f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2085f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2086f089877aSRichard Tran Mills }
2087f089877aSRichard Tran Mills 
2088f089877aSRichard Tran Mills 
2089f089877aSRichard Tran Mills #undef __FUNCT__
209047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
209147c6ae99SBarry Smith /*@
209247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
209347c6ae99SBarry Smith 
209447c6ae99SBarry Smith     Collective on DM
209547c6ae99SBarry Smith 
209647c6ae99SBarry Smith     Input Parameter:
209747c6ae99SBarry Smith +   dm - the DM object
209891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
209947c6ae99SBarry Smith 
210047c6ae99SBarry Smith     Output Parameter:
210147c6ae99SBarry Smith .   dmc - the coarsened DM
210247c6ae99SBarry Smith 
210347c6ae99SBarry Smith     Level: developer
210447c6ae99SBarry Smith 
2105e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
210647c6ae99SBarry Smith 
210747c6ae99SBarry Smith @*/
21087087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
210947c6ae99SBarry Smith {
211047c6ae99SBarry Smith   PetscErrorCode    ierr;
2111b17ce1afSJed Brown   DMCoarsenHookLink link;
211247c6ae99SBarry Smith 
211347c6ae99SBarry Smith   PetscFunctionBegin;
2114171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
211547c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
21168892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
211743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
21188cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2119644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
21200598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2121656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2122e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2123b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2124b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2125b17ce1afSJed Brown   }
2126b17ce1afSJed Brown   PetscFunctionReturn(0);
2127b17ce1afSJed Brown }
2128b17ce1afSJed Brown 
2129b17ce1afSJed Brown #undef __FUNCT__
2130b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2131bb9467b5SJed Brown /*@C
2132b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2133b17ce1afSJed Brown 
2134b17ce1afSJed Brown    Logically Collective
2135b17ce1afSJed Brown 
2136b17ce1afSJed Brown    Input Arguments:
2137b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2138b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2139b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
21400298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2141b17ce1afSJed Brown 
2142b17ce1afSJed Brown    Calling sequence of coarsenhook:
2143b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2144b17ce1afSJed Brown 
2145b17ce1afSJed Brown +  fine - fine level DM
2146b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2147b17ce1afSJed Brown -  ctx - optional user-defined function context
2148b17ce1afSJed Brown 
2149b17ce1afSJed Brown    Calling sequence for restricthook:
2150c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2151b17ce1afSJed Brown 
2152b17ce1afSJed Brown +  fine - fine level DM
2153b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2154c833c3b5SJed Brown .  rscale - scaling vector for restriction
2155c833c3b5SJed Brown .  inject - matrix restricting by injection
2156b17ce1afSJed Brown .  coarse - coarse level DM to update
2157b17ce1afSJed Brown -  ctx - optional user-defined function context
2158b17ce1afSJed Brown 
2159b17ce1afSJed Brown    Level: advanced
2160b17ce1afSJed Brown 
2161b17ce1afSJed Brown    Notes:
2162b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2163b17ce1afSJed Brown 
2164b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2165b17ce1afSJed Brown 
2166b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2167b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2168b17ce1afSJed Brown 
2169bb9467b5SJed Brown    This function is currently not available from Fortran.
2170bb9467b5SJed Brown 
2171c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2172b17ce1afSJed Brown @*/
2173b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2174b17ce1afSJed Brown {
2175b17ce1afSJed Brown   PetscErrorCode    ierr;
2176b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2177b17ce1afSJed Brown 
2178b17ce1afSJed Brown   PetscFunctionBegin;
2179b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
21806bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2181b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2182b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2183b17ce1afSJed Brown   link->restricthook = restricthook;
2184b17ce1afSJed Brown   link->ctx          = ctx;
21850298fd71SBarry Smith   link->next         = NULL;
2186b17ce1afSJed Brown   *p                 = link;
2187b17ce1afSJed Brown   PetscFunctionReturn(0);
2188b17ce1afSJed Brown }
2189b17ce1afSJed Brown 
2190b17ce1afSJed Brown #undef __FUNCT__
2191b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2192b17ce1afSJed Brown /*@
2193b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2194b17ce1afSJed Brown 
2195b17ce1afSJed Brown    Collective if any hooks are
2196b17ce1afSJed Brown 
2197b17ce1afSJed Brown    Input Arguments:
2198b17ce1afSJed Brown +  fine - finer DM to use as a base
2199b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2200b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2201b17ce1afSJed Brown -  coarse - coarer DM to update
2202b17ce1afSJed Brown 
2203b17ce1afSJed Brown    Level: developer
2204b17ce1afSJed Brown 
2205b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2206b17ce1afSJed Brown @*/
2207b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2208b17ce1afSJed Brown {
2209b17ce1afSJed Brown   PetscErrorCode    ierr;
2210b17ce1afSJed Brown   DMCoarsenHookLink link;
2211b17ce1afSJed Brown 
2212b17ce1afSJed Brown   PetscFunctionBegin;
2213b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
22148865f1eaSKarl Rupp     if (link->restricthook) {
22158865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
22168865f1eaSKarl Rupp     }
2217b17ce1afSJed Brown   }
221847c6ae99SBarry Smith   PetscFunctionReturn(0);
221947c6ae99SBarry Smith }
222047c6ae99SBarry Smith 
222147c6ae99SBarry Smith #undef __FUNCT__
2222be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2223bb9467b5SJed Brown /*@C
2224be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
22255dbd56e3SPeter Brune 
22265dbd56e3SPeter Brune    Logically Collective
22275dbd56e3SPeter Brune 
22285dbd56e3SPeter Brune    Input Arguments:
22295dbd56e3SPeter Brune +  global - global DM
2230ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
22315dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
22320298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22335dbd56e3SPeter Brune 
2234ec4806b8SPeter Brune 
2235ec4806b8SPeter Brune    Calling sequence for ddhook:
2236ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2237ec4806b8SPeter Brune 
2238ec4806b8SPeter Brune +  global - global DM
2239ec4806b8SPeter Brune .  block  - block DM
2240ec4806b8SPeter Brune -  ctx - optional user-defined function context
2241ec4806b8SPeter Brune 
22425dbd56e3SPeter Brune    Calling sequence for restricthook:
2243ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
22445dbd56e3SPeter Brune 
22455dbd56e3SPeter Brune +  global - global DM
22465dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
22475dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2248ec4806b8SPeter Brune .  block  - block DM
22495dbd56e3SPeter Brune -  ctx - optional user-defined function context
22505dbd56e3SPeter Brune 
22515dbd56e3SPeter Brune    Level: advanced
22525dbd56e3SPeter Brune 
22535dbd56e3SPeter Brune    Notes:
2254ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
22555dbd56e3SPeter Brune 
22565dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
22575dbd56e3SPeter Brune 
22585dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2259ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
22605dbd56e3SPeter Brune 
2261bb9467b5SJed Brown    This function is currently not available from Fortran.
2262bb9467b5SJed Brown 
22635dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22645dbd56e3SPeter Brune @*/
2265be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
22665dbd56e3SPeter Brune {
22675dbd56e3SPeter Brune   PetscErrorCode      ierr;
2268be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
22695dbd56e3SPeter Brune 
22705dbd56e3SPeter Brune   PetscFunctionBegin;
22715dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2272be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2273be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
22745dbd56e3SPeter Brune   link->restricthook = restricthook;
2275be081cd6SPeter Brune   link->ddhook       = ddhook;
22765dbd56e3SPeter Brune   link->ctx          = ctx;
22770298fd71SBarry Smith   link->next         = NULL;
22785dbd56e3SPeter Brune   *p                 = link;
22795dbd56e3SPeter Brune   PetscFunctionReturn(0);
22805dbd56e3SPeter Brune }
22815dbd56e3SPeter Brune 
22825dbd56e3SPeter Brune #undef __FUNCT__
2283be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
22845dbd56e3SPeter Brune /*@
2285be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
22865dbd56e3SPeter Brune 
22875dbd56e3SPeter Brune    Collective if any hooks are
22885dbd56e3SPeter Brune 
22895dbd56e3SPeter Brune    Input Arguments:
22905dbd56e3SPeter Brune +  fine - finer DM to use as a base
2291be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2292be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
22935dbd56e3SPeter Brune -  coarse - coarer DM to update
22945dbd56e3SPeter Brune 
22955dbd56e3SPeter Brune    Level: developer
22965dbd56e3SPeter Brune 
22975dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
22985dbd56e3SPeter Brune @*/
2299be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
23005dbd56e3SPeter Brune {
23015dbd56e3SPeter Brune   PetscErrorCode      ierr;
2302be081cd6SPeter Brune   DMSubDomainHookLink link;
23035dbd56e3SPeter Brune 
23045dbd56e3SPeter Brune   PetscFunctionBegin;
2305be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
23068865f1eaSKarl Rupp     if (link->restricthook) {
23078865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
23088865f1eaSKarl Rupp     }
23095dbd56e3SPeter Brune   }
23105dbd56e3SPeter Brune   PetscFunctionReturn(0);
23115dbd56e3SPeter Brune }
23125dbd56e3SPeter Brune 
23135dbd56e3SPeter Brune #undef __FUNCT__
23145fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
23155fe1f584SPeter Brune /*@
23166a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
23175fe1f584SPeter Brune 
23185fe1f584SPeter Brune     Not Collective
23195fe1f584SPeter Brune 
23205fe1f584SPeter Brune     Input Parameter:
23215fe1f584SPeter Brune .   dm - the DM object
23225fe1f584SPeter Brune 
23235fe1f584SPeter Brune     Output Parameter:
23246a7d9d85SPeter Brune .   level - number of coarsenings
23255fe1f584SPeter Brune 
23265fe1f584SPeter Brune     Level: developer
23275fe1f584SPeter Brune 
23286a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
23295fe1f584SPeter Brune 
23305fe1f584SPeter Brune @*/
23315fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
23325fe1f584SPeter Brune {
23335fe1f584SPeter Brune   PetscFunctionBegin;
23345fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23355fe1f584SPeter Brune   *level = dm->leveldown;
23365fe1f584SPeter Brune   PetscFunctionReturn(0);
23375fe1f584SPeter Brune }
23385fe1f584SPeter Brune 
23395fe1f584SPeter Brune 
23405fe1f584SPeter Brune 
23415fe1f584SPeter Brune #undef __FUNCT__
234247c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
234347c6ae99SBarry Smith /*@C
234447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
234547c6ae99SBarry Smith 
234647c6ae99SBarry Smith     Collective on DM
234747c6ae99SBarry Smith 
234847c6ae99SBarry Smith     Input Parameter:
234947c6ae99SBarry Smith +   dm - the DM object
235047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
235147c6ae99SBarry Smith 
235247c6ae99SBarry Smith     Output Parameter:
235347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
235447c6ae99SBarry Smith 
235547c6ae99SBarry Smith     Level: developer
235647c6ae99SBarry Smith 
2357e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
235847c6ae99SBarry Smith 
235947c6ae99SBarry Smith @*/
23607087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
236147c6ae99SBarry Smith {
236247c6ae99SBarry Smith   PetscErrorCode ierr;
236347c6ae99SBarry Smith 
236447c6ae99SBarry Smith   PetscFunctionBegin;
2365171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2366ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
236747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
236847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
236947c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
237047c6ae99SBarry Smith   } else if (dm->ops->refine) {
237147c6ae99SBarry Smith     PetscInt i;
237247c6ae99SBarry Smith 
2373ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
237447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2375ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
237647c6ae99SBarry Smith     }
2377ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
237847c6ae99SBarry Smith   PetscFunctionReturn(0);
237947c6ae99SBarry Smith }
238047c6ae99SBarry Smith 
238147c6ae99SBarry Smith #undef __FUNCT__
238247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
238347c6ae99SBarry Smith /*@C
238447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
238547c6ae99SBarry Smith 
238647c6ae99SBarry Smith     Collective on DM
238747c6ae99SBarry Smith 
238847c6ae99SBarry Smith     Input Parameter:
238947c6ae99SBarry Smith +   dm - the DM object
239047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
239147c6ae99SBarry Smith 
239247c6ae99SBarry Smith     Output Parameter:
239347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
239447c6ae99SBarry Smith 
239547c6ae99SBarry Smith     Level: developer
239647c6ae99SBarry Smith 
2397e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
239847c6ae99SBarry Smith 
239947c6ae99SBarry Smith @*/
24007087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
240147c6ae99SBarry Smith {
240247c6ae99SBarry Smith   PetscErrorCode ierr;
240347c6ae99SBarry Smith 
240447c6ae99SBarry Smith   PetscFunctionBegin;
2405171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2406ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
240747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
240847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
240947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
241047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
241147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
241247c6ae99SBarry Smith     PetscInt i;
241347c6ae99SBarry Smith 
2414ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
241547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2416ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
241747c6ae99SBarry Smith     }
2418ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
241947c6ae99SBarry Smith   PetscFunctionReturn(0);
242047c6ae99SBarry Smith }
242147c6ae99SBarry Smith 
242247c6ae99SBarry Smith #undef __FUNCT__
2423e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
242447c6ae99SBarry Smith /*@
2425e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
242647c6ae99SBarry Smith    grids associated with two DMs.
242747c6ae99SBarry Smith 
242847c6ae99SBarry Smith    Collective on DM
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith    Input Parameters:
243147c6ae99SBarry Smith +  dmc - the coarse grid DM
243247c6ae99SBarry Smith -  dmf - the fine grid DM
243347c6ae99SBarry Smith 
243447c6ae99SBarry Smith    Output Parameters:
243547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
243647c6ae99SBarry Smith 
243747c6ae99SBarry Smith    Level: intermediate
243847c6ae99SBarry Smith 
243947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
244047c6ae99SBarry Smith 
2441e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
244247c6ae99SBarry Smith @*/
2443e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
244447c6ae99SBarry Smith {
244547c6ae99SBarry Smith   PetscErrorCode ierr;
244647c6ae99SBarry Smith 
244747c6ae99SBarry Smith   PetscFunctionBegin;
2448171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2449171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
245047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
245147c6ae99SBarry Smith   PetscFunctionReturn(0);
245247c6ae99SBarry Smith }
245347c6ae99SBarry Smith 
245447c6ae99SBarry Smith #undef __FUNCT__
24551a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
24561a266240SBarry Smith /*@C
24571a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
24581a266240SBarry Smith 
24591a266240SBarry Smith     Not Collective
24601a266240SBarry Smith 
24611a266240SBarry Smith     Input Parameters:
24621a266240SBarry Smith +   dm - the DM object
24631a266240SBarry Smith -   destroy - the destroy function
24641a266240SBarry Smith 
24651a266240SBarry Smith     Level: intermediate
24661a266240SBarry Smith 
2467e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
24681a266240SBarry Smith 
2469f07f9ceaSJed Brown @*/
24701a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
24711a266240SBarry Smith {
24721a266240SBarry Smith   PetscFunctionBegin;
2473171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24741a266240SBarry Smith   dm->ctxdestroy = destroy;
24751a266240SBarry Smith   PetscFunctionReturn(0);
24761a266240SBarry Smith }
24771a266240SBarry Smith 
24781a266240SBarry Smith #undef __FUNCT__
24791b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2480b07ff414SBarry Smith /*@
24811b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
248247c6ae99SBarry Smith 
248347c6ae99SBarry Smith     Not Collective
248447c6ae99SBarry Smith 
248547c6ae99SBarry Smith     Input Parameters:
248647c6ae99SBarry Smith +   dm - the DM object
248747c6ae99SBarry Smith -   ctx - the user context
248847c6ae99SBarry Smith 
248947c6ae99SBarry Smith     Level: intermediate
249047c6ae99SBarry Smith 
2491e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
249247c6ae99SBarry Smith 
249347c6ae99SBarry Smith @*/
24941b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
249547c6ae99SBarry Smith {
249647c6ae99SBarry Smith   PetscFunctionBegin;
2497171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
249847c6ae99SBarry Smith   dm->ctx = ctx;
249947c6ae99SBarry Smith   PetscFunctionReturn(0);
250047c6ae99SBarry Smith }
250147c6ae99SBarry Smith 
250247c6ae99SBarry Smith #undef __FUNCT__
25031b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
250447c6ae99SBarry Smith /*@
25051b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
250647c6ae99SBarry Smith 
250747c6ae99SBarry Smith     Not Collective
250847c6ae99SBarry Smith 
250947c6ae99SBarry Smith     Input Parameter:
251047c6ae99SBarry Smith .   dm - the DM object
251147c6ae99SBarry Smith 
251247c6ae99SBarry Smith     Output Parameter:
251347c6ae99SBarry Smith .   ctx - the user context
251447c6ae99SBarry Smith 
251547c6ae99SBarry Smith     Level: intermediate
251647c6ae99SBarry Smith 
2517e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
251847c6ae99SBarry Smith 
251947c6ae99SBarry Smith @*/
25201b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
252147c6ae99SBarry Smith {
252247c6ae99SBarry Smith   PetscFunctionBegin;
2523171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25241b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
252547c6ae99SBarry Smith   PetscFunctionReturn(0);
252647c6ae99SBarry Smith }
252747c6ae99SBarry Smith 
252847c6ae99SBarry Smith #undef __FUNCT__
252908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
253008da532bSDmitry Karpeev /*@C
2531df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
253208da532bSDmitry Karpeev 
253308da532bSDmitry Karpeev     Logically Collective on DM
253408da532bSDmitry Karpeev 
253508da532bSDmitry Karpeev     Input Parameter:
253608da532bSDmitry Karpeev +   dm - the DM object
25370298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
253808da532bSDmitry Karpeev 
253908da532bSDmitry Karpeev     Level: intermediate
254008da532bSDmitry Karpeev 
2541835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
254208da532bSDmitry Karpeev          DMSetJacobian()
254308da532bSDmitry Karpeev 
254408da532bSDmitry Karpeev @*/
254508da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
254608da532bSDmitry Karpeev {
254708da532bSDmitry Karpeev   PetscFunctionBegin;
254808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
254908da532bSDmitry Karpeev   PetscFunctionReturn(0);
255008da532bSDmitry Karpeev }
255108da532bSDmitry Karpeev 
255208da532bSDmitry Karpeev #undef __FUNCT__
255308da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
255408da532bSDmitry Karpeev /*@
255508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
255608da532bSDmitry Karpeev 
255708da532bSDmitry Karpeev     Not Collective
255808da532bSDmitry Karpeev 
255908da532bSDmitry Karpeev     Input Parameter:
256008da532bSDmitry Karpeev .   dm - the DM object to destroy
256108da532bSDmitry Karpeev 
256208da532bSDmitry Karpeev     Output Parameter:
256308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
256408da532bSDmitry Karpeev 
256508da532bSDmitry Karpeev     Level: developer
256608da532bSDmitry Karpeev 
256774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
256808da532bSDmitry Karpeev 
256908da532bSDmitry Karpeev @*/
257008da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
257108da532bSDmitry Karpeev {
257208da532bSDmitry Karpeev   PetscFunctionBegin;
257308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
257408da532bSDmitry Karpeev   PetscFunctionReturn(0);
257508da532bSDmitry Karpeev }
257608da532bSDmitry Karpeev 
257708da532bSDmitry Karpeev #undef __FUNCT__
257808da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
257908da532bSDmitry Karpeev /*@C
258008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
258108da532bSDmitry Karpeev 
258208da532bSDmitry Karpeev     Logically Collective on DM
258308da532bSDmitry Karpeev 
258408da532bSDmitry Karpeev     Input Parameters:
2585907376e6SBarry Smith .   dm - the DM object
258608da532bSDmitry Karpeev 
258708da532bSDmitry Karpeev     Output parameters:
258808da532bSDmitry Karpeev +   xl - lower bound
258908da532bSDmitry Karpeev -   xu - upper bound
259008da532bSDmitry Karpeev 
2591907376e6SBarry Smith     Level: advanced
2592907376e6SBarry Smith 
2593907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
259408da532bSDmitry Karpeev 
259574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
259608da532bSDmitry Karpeev 
259708da532bSDmitry Karpeev @*/
259808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
259908da532bSDmitry Karpeev {
260008da532bSDmitry Karpeev   PetscErrorCode ierr;
26015fd66863SKarl Rupp 
260208da532bSDmitry Karpeev   PetscFunctionBegin;
260308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
260408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
260508da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
260608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
26078865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
260808da532bSDmitry Karpeev   PetscFunctionReturn(0);
260908da532bSDmitry Karpeev }
261008da532bSDmitry Karpeev 
261108da532bSDmitry Karpeev #undef __FUNCT__
2612b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2613b0ae01b7SPeter Brune /*@
2614b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2615b0ae01b7SPeter Brune 
2616b0ae01b7SPeter Brune     Not Collective
2617b0ae01b7SPeter Brune 
2618b0ae01b7SPeter Brune     Input Parameter:
2619b0ae01b7SPeter Brune .   dm - the DM object
2620b0ae01b7SPeter Brune 
2621b0ae01b7SPeter Brune     Output Parameter:
2622b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2623b0ae01b7SPeter Brune 
2624b0ae01b7SPeter Brune     Level: developer
2625b0ae01b7SPeter Brune 
2626b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2627b0ae01b7SPeter Brune 
2628b0ae01b7SPeter Brune @*/
2629b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2630b0ae01b7SPeter Brune {
2631b0ae01b7SPeter Brune   PetscFunctionBegin;
2632b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2633b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2634b0ae01b7SPeter Brune }
2635b0ae01b7SPeter Brune 
2636b0ae01b7SPeter Brune #undef  __FUNCT__
263708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2638748fac09SDmitry Karpeev /*@C
263908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
264008da532bSDmitry Karpeev 
264108da532bSDmitry Karpeev     Collective on DM
264208da532bSDmitry Karpeev 
264308da532bSDmitry Karpeev     Input Parameter:
264408da532bSDmitry Karpeev +   dm - the DM object
26450298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
264608da532bSDmitry Karpeev 
264708da532bSDmitry Karpeev     Level: developer
264808da532bSDmitry Karpeev 
264974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
265008da532bSDmitry Karpeev 
265108da532bSDmitry Karpeev @*/
265208da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
265308da532bSDmitry Karpeev {
265408da532bSDmitry Karpeev   PetscErrorCode ierr;
26555fd66863SKarl Rupp 
265608da532bSDmitry Karpeev   PetscFunctionBegin;
265708da532bSDmitry Karpeev   if (x) {
265808da532bSDmitry Karpeev     if (!dm->x) {
265908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
266008da532bSDmitry Karpeev     }
266108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
26628865f1eaSKarl Rupp   } else if (dm->x) {
266308da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
266408da532bSDmitry Karpeev   }
266508da532bSDmitry Karpeev   PetscFunctionReturn(0);
266608da532bSDmitry Karpeev }
266708da532bSDmitry Karpeev 
26680298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2669264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2670264ace61SBarry Smith 
2671264ace61SBarry Smith #undef __FUNCT__
2672264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2673264ace61SBarry Smith /*@C
2674264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2675264ace61SBarry Smith 
2676264ace61SBarry Smith   Collective on DM
2677264ace61SBarry Smith 
2678264ace61SBarry Smith   Input Parameters:
2679264ace61SBarry Smith + dm     - The DM object
2680264ace61SBarry Smith - method - The name of the DM type
2681264ace61SBarry Smith 
2682264ace61SBarry Smith   Options Database Key:
2683264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2684264ace61SBarry Smith 
2685264ace61SBarry Smith   Notes:
2686e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2687264ace61SBarry Smith 
2688264ace61SBarry Smith   Level: intermediate
2689264ace61SBarry Smith 
2690264ace61SBarry Smith .keywords: DM, set, type
2691264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2692264ace61SBarry Smith @*/
269319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2694264ace61SBarry Smith {
2695264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2696264ace61SBarry Smith   PetscBool      match;
2697264ace61SBarry Smith   PetscErrorCode ierr;
2698264ace61SBarry Smith 
2699264ace61SBarry Smith   PetscFunctionBegin;
2700264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2701251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2702264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2703264ace61SBarry Smith 
27040f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
27051c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2706ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2707264ace61SBarry Smith 
2708264ace61SBarry Smith   if (dm->ops->destroy) {
2709264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
27100298fd71SBarry Smith     dm->ops->destroy = NULL;
2711264ace61SBarry Smith   }
2712264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2713264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2714264ace61SBarry Smith   PetscFunctionReturn(0);
2715264ace61SBarry Smith }
2716264ace61SBarry Smith 
2717264ace61SBarry Smith #undef __FUNCT__
2718264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2719264ace61SBarry Smith /*@C
2720264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2721264ace61SBarry Smith 
2722264ace61SBarry Smith   Not Collective
2723264ace61SBarry Smith 
2724264ace61SBarry Smith   Input Parameter:
2725264ace61SBarry Smith . dm  - The DM
2726264ace61SBarry Smith 
2727264ace61SBarry Smith   Output Parameter:
2728264ace61SBarry Smith . type - The DM type name
2729264ace61SBarry Smith 
2730264ace61SBarry Smith   Level: intermediate
2731264ace61SBarry Smith 
2732264ace61SBarry Smith .keywords: DM, get, type, name
2733264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2734264ace61SBarry Smith @*/
273519fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2736264ace61SBarry Smith {
2737264ace61SBarry Smith   PetscErrorCode ierr;
2738264ace61SBarry Smith 
2739264ace61SBarry Smith   PetscFunctionBegin;
2740264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2741c959eef4SJed Brown   PetscValidPointer(type,2);
2742607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2743264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2744264ace61SBarry Smith   PetscFunctionReturn(0);
2745264ace61SBarry Smith }
2746264ace61SBarry Smith 
274767a56275SMatthew G Knepley #undef __FUNCT__
274867a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
274967a56275SMatthew G Knepley /*@C
275067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
275167a56275SMatthew G Knepley 
275267a56275SMatthew G Knepley   Collective on DM
275367a56275SMatthew G Knepley 
275467a56275SMatthew G Knepley   Input Parameters:
275567a56275SMatthew G Knepley + dm - the DM
275667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
275767a56275SMatthew G Knepley 
275867a56275SMatthew G Knepley   Output Parameter:
275967a56275SMatthew G Knepley . M - pointer to new DM
276067a56275SMatthew G Knepley 
276167a56275SMatthew G Knepley   Notes:
276267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
276367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
276467a56275SMatthew G Knepley   of the input DM.
276567a56275SMatthew G Knepley 
276667a56275SMatthew G Knepley   Level: intermediate
276767a56275SMatthew G Knepley 
276867a56275SMatthew G Knepley .seealso: DMCreate()
276967a56275SMatthew G Knepley @*/
277019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
277167a56275SMatthew G Knepley {
277267a56275SMatthew G Knepley   DM             B;
277367a56275SMatthew G Knepley   char           convname[256];
277467a56275SMatthew G Knepley   PetscBool      sametype, issame;
277567a56275SMatthew G Knepley   PetscErrorCode ierr;
277667a56275SMatthew G Knepley 
277767a56275SMatthew G Knepley   PetscFunctionBegin;
277867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
277967a56275SMatthew G Knepley   PetscValidType(dm,1);
278067a56275SMatthew G Knepley   PetscValidPointer(M,3);
2781251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
278267a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
278367a56275SMatthew G Knepley   {
27840298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
278567a56275SMatthew G Knepley 
278667a56275SMatthew G Knepley     /*
278767a56275SMatthew G Knepley        Order of precedence:
278867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
278967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
279067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
279167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
279267a56275SMatthew G Knepley        5) Use a really basic converter.
279367a56275SMatthew G Knepley     */
279467a56275SMatthew G Knepley 
279567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
279667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
279767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
279867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
279967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
280067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28010005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
280267a56275SMatthew G Knepley     if (conv) goto foundconv;
280367a56275SMatthew G Knepley 
280467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
280582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
280667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
280767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
280867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
280967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
281067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
281167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28120005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
281367a56275SMatthew G Knepley     if (conv) {
2814fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
281567a56275SMatthew G Knepley       goto foundconv;
281667a56275SMatthew G Knepley     }
281767a56275SMatthew G Knepley 
281867a56275SMatthew G Knepley #if 0
281967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
282067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2821fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
282267a56275SMatthew G Knepley     if (conv) goto foundconv;
282367a56275SMatthew G Knepley 
282467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
282567a56275SMatthew G Knepley     if (dm->ops->convert) {
282667a56275SMatthew G Knepley       conv = dm->ops->convert;
282767a56275SMatthew G Knepley     }
282867a56275SMatthew G Knepley     if (conv) goto foundconv;
282967a56275SMatthew G Knepley #endif
283067a56275SMatthew G Knepley 
283167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
283282f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
283367a56275SMatthew G Knepley 
283467a56275SMatthew G Knepley foundconv:
283567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
283667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
283767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
283867a56275SMatthew G Knepley   }
283967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
284067a56275SMatthew G Knepley   PetscFunctionReturn(0);
284167a56275SMatthew G Knepley }
2842264ace61SBarry Smith 
2843264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2844264ace61SBarry Smith 
2845264ace61SBarry Smith #undef __FUNCT__
2846264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2847264ace61SBarry Smith /*@C
28481c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
28491c84c290SBarry Smith 
28501c84c290SBarry Smith   Not Collective
28511c84c290SBarry Smith 
28521c84c290SBarry Smith   Input Parameters:
28531c84c290SBarry Smith + name        - The name of a new user-defined creation routine
28541c84c290SBarry Smith - create_func - The creation routine itself
28551c84c290SBarry Smith 
28561c84c290SBarry Smith   Notes:
28571c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
28581c84c290SBarry Smith 
28591c84c290SBarry Smith 
28601c84c290SBarry Smith   Sample usage:
28611c84c290SBarry Smith .vb
2862bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
28631c84c290SBarry Smith .ve
28641c84c290SBarry Smith 
28651c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
28661c84c290SBarry Smith .vb
28671c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
28681c84c290SBarry Smith     DMSetType(DM,"my_da");
28691c84c290SBarry Smith .ve
28701c84c290SBarry Smith    or at runtime via the option
28711c84c290SBarry Smith .vb
28721c84c290SBarry Smith     -da_type my_da
28731c84c290SBarry Smith .ve
2874264ace61SBarry Smith 
2875264ace61SBarry Smith   Level: advanced
28761c84c290SBarry Smith 
28771c84c290SBarry Smith .keywords: DM, register
2878bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
28791c84c290SBarry Smith 
2880264ace61SBarry Smith @*/
2881bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2882264ace61SBarry Smith {
2883264ace61SBarry Smith   PetscErrorCode ierr;
2884264ace61SBarry Smith 
2885264ace61SBarry Smith   PetscFunctionBegin;
2886a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2887264ace61SBarry Smith   PetscFunctionReturn(0);
2888264ace61SBarry Smith }
2889264ace61SBarry Smith 
2890b859378eSBarry Smith #undef __FUNCT__
2891b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2892b859378eSBarry Smith /*@C
289355849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2894b859378eSBarry Smith 
2895b859378eSBarry Smith   Collective on PetscViewer
2896b859378eSBarry Smith 
2897b859378eSBarry Smith   Input Parameters:
2898b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2899b859378eSBarry Smith            some related function before a call to DMLoad().
2900b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2901b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2902b859378eSBarry Smith 
2903b859378eSBarry Smith    Level: intermediate
2904b859378eSBarry Smith 
2905b859378eSBarry Smith   Notes:
290655849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2907b859378eSBarry Smith 
2908b859378eSBarry Smith   Notes for advanced users:
2909b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2910b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2911b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2912b859378eSBarry Smith   format is
2913b859378eSBarry Smith .vb
2914b859378eSBarry Smith      has not yet been determined
2915b859378eSBarry Smith .ve
2916b859378eSBarry Smith 
2917b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2918b859378eSBarry Smith @*/
2919b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2920b859378eSBarry Smith {
29219331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
2922b859378eSBarry Smith   PetscErrorCode ierr;
2923b859378eSBarry Smith 
2924b859378eSBarry Smith   PetscFunctionBegin;
2925b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2926b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
292732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
29289331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
29299331c7a4SMatthew G. Knepley   if (isbinary) {
29309331c7a4SMatthew G. Knepley     PetscInt classid;
29319331c7a4SMatthew G. Knepley     char     type[256];
2932b859378eSBarry Smith 
2933060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
29349200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
2935060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
293632c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
29379331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
29389331c7a4SMatthew G. Knepley   } else if (ishdf5) {
29399331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
29409331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
2941b859378eSBarry Smith   PetscFunctionReturn(0);
2942b859378eSBarry Smith }
2943b859378eSBarry Smith 
29447da65231SMatthew G Knepley /******************************** FEM Support **********************************/
29457da65231SMatthew G Knepley 
29467da65231SMatthew G Knepley #undef __FUNCT__
29477da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2948a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2949a6dfd86eSKarl Rupp {
29501d47ebbbSSatish Balay   PetscInt       f;
29511b30c384SMatthew G Knepley   PetscErrorCode ierr;
29521b30c384SMatthew G Knepley 
29537da65231SMatthew G Knepley   PetscFunctionBegin;
295474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29551d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
295657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
29577da65231SMatthew G Knepley   }
29587da65231SMatthew G Knepley   PetscFunctionReturn(0);
29597da65231SMatthew G Knepley }
29607da65231SMatthew G Knepley 
29617da65231SMatthew G Knepley #undef __FUNCT__
29627da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2963a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2964a6dfd86eSKarl Rupp {
29651b30c384SMatthew G Knepley   PetscInt       f, g;
29667da65231SMatthew G Knepley   PetscErrorCode ierr;
29677da65231SMatthew G Knepley 
29687da65231SMatthew G Knepley   PetscFunctionBegin;
296974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29701d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
297174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
29721d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
2973e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
29747da65231SMatthew G Knepley     }
297574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
29767da65231SMatthew G Knepley   }
29777da65231SMatthew G Knepley   PetscFunctionReturn(0);
29787da65231SMatthew G Knepley }
2979e7c4fc90SDmitry Karpeev 
2980970e74d5SMatthew G Knepley #undef __FUNCT__
2981e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
29826113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
2983e759306cSMatthew G. Knepley {
2984e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
2985e759306cSMatthew G. Knepley   PetscInt       p;
2986e759306cSMatthew G. Knepley   PetscErrorCode ierr;
2987e759306cSMatthew G. Knepley 
2988e759306cSMatthew G. Knepley   PetscFunctionBegin;
2989e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
2990e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
2991e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
2992e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
2993e759306cSMatthew G. Knepley     if (p == rank) {
2994e759306cSMatthew G. Knepley       Vec x;
2995e759306cSMatthew G. Knepley 
2996e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
2997e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
29986113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
2999e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3000e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3001e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3002e759306cSMatthew G. Knepley     }
3003e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3004e759306cSMatthew G. Knepley   }
3005e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3006e759306cSMatthew G. Knepley }
3007e759306cSMatthew G. Knepley 
3008e759306cSMatthew G. Knepley #undef __FUNCT__
300988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
301088ed4aceSMatthew G Knepley /*@
301188ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
301288ed4aceSMatthew G Knepley 
301388ed4aceSMatthew G Knepley   Input Parameter:
301488ed4aceSMatthew G Knepley . dm - The DM
301588ed4aceSMatthew G Knepley 
301688ed4aceSMatthew G Knepley   Output Parameter:
301788ed4aceSMatthew G Knepley . section - The PetscSection
301888ed4aceSMatthew G Knepley 
301988ed4aceSMatthew G Knepley   Level: intermediate
302088ed4aceSMatthew G Knepley 
302188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
302288ed4aceSMatthew G Knepley 
302388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
302488ed4aceSMatthew G Knepley @*/
30250adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
30260adebc6cSBarry Smith {
3027fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3028fd59a867SMatthew G. Knepley 
302988ed4aceSMatthew G Knepley   PetscFunctionBegin;
303088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
303188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
30322f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
30332f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3034685405a1SBarry Smith     ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);
30352f0f8703SMatthew G. Knepley   }
303688ed4aceSMatthew G Knepley   *section = dm->defaultSection;
303788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
303888ed4aceSMatthew G Knepley }
303988ed4aceSMatthew G Knepley 
304088ed4aceSMatthew G Knepley #undef __FUNCT__
304188ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
304288ed4aceSMatthew G Knepley /*@
304388ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
304488ed4aceSMatthew G Knepley 
304588ed4aceSMatthew G Knepley   Input Parameters:
304688ed4aceSMatthew G Knepley + dm - The DM
304788ed4aceSMatthew G Knepley - section - The PetscSection
304888ed4aceSMatthew G Knepley 
304988ed4aceSMatthew G Knepley   Level: intermediate
305088ed4aceSMatthew G Knepley 
305188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
305288ed4aceSMatthew G Knepley 
305388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
305488ed4aceSMatthew G Knepley @*/
30550adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
30560adebc6cSBarry Smith {
3057c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3058af122d2aSMatthew G Knepley   PetscInt       f;
305988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
306088ed4aceSMatthew G Knepley 
306188ed4aceSMatthew G Knepley   PetscFunctionBegin;
306288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3063c473ab19SMatthew G. Knepley   if (section) {
30641d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
30651d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3066c473ab19SMatthew G. Knepley   }
306788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
306888ed4aceSMatthew G Knepley   dm->defaultSection = section;
3069c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3070af122d2aSMatthew G Knepley   if (numFields) {
3071af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3072af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
30730f21e855SMatthew G. Knepley       PetscObject disc;
3074af122d2aSMatthew G Knepley       const char *name;
3075af122d2aSMatthew G Knepley 
3076af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
30770f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
30780f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3079af122d2aSMatthew G Knepley     }
3080af122d2aSMatthew G Knepley   }
30811d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
30821d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
308388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
308488ed4aceSMatthew G Knepley }
308588ed4aceSMatthew G Knepley 
308688ed4aceSMatthew G Knepley #undef __FUNCT__
30879435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
30889435951eSToby Isaac /*@
30899435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
30909435951eSToby Isaac 
3091e228b242SToby Isaac   not collective
3092e228b242SToby Isaac 
30939435951eSToby Isaac   Input Parameter:
30949435951eSToby Isaac . dm - The DM
30959435951eSToby Isaac 
30969435951eSToby Isaac   Output Parameter:
30979435951eSToby 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.
30989435951eSToby 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.
30999435951eSToby Isaac 
31009435951eSToby Isaac   Level: advanced
31019435951eSToby Isaac 
31029435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
31039435951eSToby Isaac 
31049435951eSToby Isaac .seealso: DMSetDefaultConstraints()
31059435951eSToby Isaac @*/
31069435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
31079435951eSToby Isaac {
31089435951eSToby Isaac   PetscErrorCode ierr;
31099435951eSToby Isaac 
31109435951eSToby Isaac   PetscFunctionBegin;
31119435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31129435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
311345a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
311445a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
31159435951eSToby Isaac   PetscFunctionReturn(0);
31169435951eSToby Isaac }
31179435951eSToby Isaac 
31189435951eSToby Isaac #undef __FUNCT__
31199435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
31209435951eSToby Isaac /*@
31219435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
31229435951eSToby Isaac 
31239435951eSToby 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().
31249435951eSToby Isaac 
31259435951eSToby 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.
31269435951eSToby Isaac 
3127e228b242SToby Isaac   collective on dm
3128e228b242SToby Isaac 
31299435951eSToby Isaac   Input Parameters:
31309435951eSToby Isaac + dm - The DM
3131e228b242SToby 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).
3132e228b242SToby 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).
31339435951eSToby Isaac 
31349435951eSToby Isaac   Level: advanced
31359435951eSToby Isaac 
31369435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
31379435951eSToby Isaac 
31389435951eSToby Isaac .seealso: DMGetDefaultConstraints()
31399435951eSToby Isaac @*/
31409435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
31419435951eSToby Isaac {
3142e228b242SToby Isaac   PetscMPIInt result;
31439435951eSToby Isaac   PetscErrorCode ierr;
31449435951eSToby Isaac 
31459435951eSToby Isaac   PetscFunctionBegin;
31469435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3147e228b242SToby Isaac   if (section) {
3148e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3149e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3150e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3151e228b242SToby Isaac   }
3152e228b242SToby Isaac   if (mat) {
3153e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3154e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3155e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3156e228b242SToby Isaac   }
31579435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
31589435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
31599435951eSToby Isaac   dm->defaultConstraintSection = section;
31609435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
31619435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
31629435951eSToby Isaac   dm->defaultConstraintMat = mat;
31639435951eSToby Isaac   PetscFunctionReturn(0);
31649435951eSToby Isaac }
31659435951eSToby Isaac 
3166f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
31679435951eSToby Isaac #undef __FUNCT__
3168284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3169507e4973SMatthew G. Knepley /*
3170507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3171507e4973SMatthew G. Knepley 
3172507e4973SMatthew G. Knepley   Input Parameters:
3173507e4973SMatthew G. Knepley + dm - The DM
3174507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3175507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3176507e4973SMatthew G. Knepley 
3177507e4973SMatthew G. Knepley   Level: intermediate
3178507e4973SMatthew G. Knepley 
3179507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3180507e4973SMatthew G. Knepley */
3181f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3182507e4973SMatthew G. Knepley {
3183507e4973SMatthew G. Knepley   MPI_Comm        comm;
3184507e4973SMatthew G. Knepley   PetscLayout     layout;
3185507e4973SMatthew G. Knepley   const PetscInt *ranges;
3186507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3187507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3188507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3189507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3190507e4973SMatthew G. Knepley 
3191507e4973SMatthew G. Knepley   PetscFunctionBegin;
3192507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3193507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3194507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3195507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3196507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3197507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3198507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3199507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3200507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3201507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3202507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3203507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3204f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3205507e4973SMatthew G. Knepley 
3206507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3207507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3208507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3209507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3210507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3211507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3212507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3213507e4973SMatthew 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;}
3214507e4973SMatthew 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;}
3215507e4973SMatthew G. Knepley     if (gdof < 0) {
3216507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3217507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3218507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3219507e4973SMatthew G. Knepley 
3220507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3221507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3222507e4973SMatthew 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;}
3223507e4973SMatthew G. Knepley       }
3224507e4973SMatthew G. Knepley     }
3225507e4973SMatthew G. Knepley   }
3226507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3227507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3228507e4973SMatthew G. Knepley   ierr = MPI_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3229507e4973SMatthew G. Knepley   if (!gvalid) {
3230507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3231507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3232507e4973SMatthew G. Knepley   }
3233507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3234507e4973SMatthew G. Knepley }
3235f741bcd2SMatthew G. Knepley #endif
3236507e4973SMatthew G. Knepley 
3237507e4973SMatthew G. Knepley #undef __FUNCT__
323888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
323988ed4aceSMatthew G Knepley /*@
324088ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
324188ed4aceSMatthew G Knepley 
32428b1ab98fSJed Brown   Collective on DM
32438b1ab98fSJed Brown 
324488ed4aceSMatthew G Knepley   Input Parameter:
324588ed4aceSMatthew G Knepley . dm - The DM
324688ed4aceSMatthew G Knepley 
324788ed4aceSMatthew G Knepley   Output Parameter:
324888ed4aceSMatthew G Knepley . section - The PetscSection
324988ed4aceSMatthew G Knepley 
325088ed4aceSMatthew G Knepley   Level: intermediate
325188ed4aceSMatthew G Knepley 
325288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
325388ed4aceSMatthew G Knepley 
325488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
325588ed4aceSMatthew G Knepley @*/
32560adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
32570adebc6cSBarry Smith {
325888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
325988ed4aceSMatthew G Knepley 
326088ed4aceSMatthew G Knepley   PetscFunctionBegin;
326188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
326288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
326388ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3264fd59a867SMatthew G. Knepley     PetscSection s;
3265fd59a867SMatthew G. Knepley 
3266fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3267fd59a867SMatthew 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");
3268fd59a867SMatthew 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");
3269fd59a867SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3270cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3271ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3272685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
327388ed4aceSMatthew G Knepley   }
327488ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
327588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
327688ed4aceSMatthew G Knepley }
327788ed4aceSMatthew G Knepley 
327888ed4aceSMatthew G Knepley #undef __FUNCT__
3279b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3280b21d0597SMatthew G Knepley /*@
3281b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3282b21d0597SMatthew G Knepley 
3283b21d0597SMatthew G Knepley   Input Parameters:
3284b21d0597SMatthew G Knepley + dm - The DM
32855080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3286b21d0597SMatthew G Knepley 
3287b21d0597SMatthew G Knepley   Level: intermediate
3288b21d0597SMatthew G Knepley 
3289b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3290b21d0597SMatthew G Knepley 
3291b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3292b21d0597SMatthew G Knepley @*/
32930adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
32940adebc6cSBarry Smith {
3295b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3296b21d0597SMatthew G Knepley 
3297b21d0597SMatthew G Knepley   PetscFunctionBegin;
3298b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32995080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33001d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3301b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3302b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3303507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3304f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3305507e4973SMatthew G. Knepley #endif
3306b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3307b21d0597SMatthew G Knepley }
3308b21d0597SMatthew G Knepley 
3309b21d0597SMatthew G Knepley #undef __FUNCT__
331088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
331188ed4aceSMatthew G Knepley /*@
331288ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
331388ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
331488ed4aceSMatthew G Knepley 
331588ed4aceSMatthew G Knepley   Input Parameter:
331688ed4aceSMatthew G Knepley . dm - The DM
331788ed4aceSMatthew G Knepley 
331888ed4aceSMatthew G Knepley   Output Parameter:
331988ed4aceSMatthew G Knepley . sf - The PetscSF
332088ed4aceSMatthew G Knepley 
332188ed4aceSMatthew G Knepley   Level: intermediate
332288ed4aceSMatthew G Knepley 
332388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
332488ed4aceSMatthew G Knepley 
332588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
332688ed4aceSMatthew G Knepley @*/
33270adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
33280adebc6cSBarry Smith {
332988ed4aceSMatthew G Knepley   PetscInt       nroots;
333088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
333188ed4aceSMatthew G Knepley 
333288ed4aceSMatthew G Knepley   PetscFunctionBegin;
333388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
333488ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
33350298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
333688ed4aceSMatthew G Knepley   if (nroots < 0) {
333788ed4aceSMatthew G Knepley     PetscSection section, gSection;
333888ed4aceSMatthew G Knepley 
333988ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
334031ea6d37SMatthew G Knepley     if (section) {
334188ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
334288ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
334331ea6d37SMatthew G Knepley     } else {
33440298fd71SBarry Smith       *sf = NULL;
334531ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
334631ea6d37SMatthew G Knepley     }
334788ed4aceSMatthew G Knepley   }
334888ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
334988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
335088ed4aceSMatthew G Knepley }
335188ed4aceSMatthew G Knepley 
335288ed4aceSMatthew G Knepley #undef __FUNCT__
335388ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
335488ed4aceSMatthew G Knepley /*@
335588ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
335688ed4aceSMatthew G Knepley 
335788ed4aceSMatthew G Knepley   Input Parameters:
335888ed4aceSMatthew G Knepley + dm - The DM
335988ed4aceSMatthew G Knepley - sf - The PetscSF
336088ed4aceSMatthew G Knepley 
336188ed4aceSMatthew G Knepley   Level: intermediate
336288ed4aceSMatthew G Knepley 
336388ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
336488ed4aceSMatthew G Knepley 
336588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
336688ed4aceSMatthew G Knepley @*/
33670adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
33680adebc6cSBarry Smith {
336988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
337088ed4aceSMatthew G Knepley 
337188ed4aceSMatthew G Knepley   PetscFunctionBegin;
337288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
337388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
337488ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
337588ed4aceSMatthew G Knepley   dm->defaultSF = sf;
337688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
337788ed4aceSMatthew G Knepley }
337888ed4aceSMatthew G Knepley 
337988ed4aceSMatthew G Knepley #undef __FUNCT__
338088ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
338188ed4aceSMatthew G Knepley /*@C
338288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
338388ed4aceSMatthew G Knepley   describing the data layout.
338488ed4aceSMatthew G Knepley 
338588ed4aceSMatthew G Knepley   Input Parameters:
338688ed4aceSMatthew G Knepley + dm - The DM
338788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
338888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
338988ed4aceSMatthew G Knepley 
339088ed4aceSMatthew G Knepley   Level: intermediate
339188ed4aceSMatthew G Knepley 
339288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
339388ed4aceSMatthew G Knepley @*/
339488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
339588ed4aceSMatthew G Knepley {
339682f516ccSBarry Smith   MPI_Comm       comm;
339788ed4aceSMatthew G Knepley   PetscLayout    layout;
339888ed4aceSMatthew G Knepley   const PetscInt *ranges;
339988ed4aceSMatthew G Knepley   PetscInt       *local;
340088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3401ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
340288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
340388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
340488ed4aceSMatthew G Knepley 
340588ed4aceSMatthew G Knepley   PetscFunctionBegin;
340682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
340788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
340888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
340988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
341088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
341188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
341288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
341388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
341488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
341588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
341688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3417ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
34186636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
341988ed4aceSMatthew G Knepley 
34206636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
34216636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3422235fbf56SMatthew 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));
34236636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
342488ed4aceSMatthew G Knepley   }
3425785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3426785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
342788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
34281f588964SMatthew G Knepley     const PetscInt *cind;
34296636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
343088ed4aceSMatthew G Knepley 
343188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
343288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
343388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
343488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
343588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
34366636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
343788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
34386636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
34396636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
34406636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3441057b4bcdSMatthew 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);
34426636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
34436636e97aSMatthew G Knepley     }
344488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
344588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
344688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
344788ed4aceSMatthew G Knepley     }
344888ed4aceSMatthew G Knepley     if (gdof < 0) {
34496636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
345088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
345188ed4aceSMatthew G Knepley 
345205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
345331d3f06eSJed Brown         if (r < 0) r = -(r+2);
345405376888SMatthew 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);
345588ed4aceSMatthew G Knepley         remote[l].rank  = r;
345688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
345788ed4aceSMatthew G Knepley       }
345888ed4aceSMatthew G Knepley     } else {
34596636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
346088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
346188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
346288ed4aceSMatthew G Knepley       }
346388ed4aceSMatthew G Knepley     }
346488ed4aceSMatthew G Knepley   }
34656636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
346688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
346788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
346888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
346988ed4aceSMatthew G Knepley }
3470af122d2aSMatthew G Knepley 
3471af122d2aSMatthew G Knepley #undef __FUNCT__
3472b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3473b21d0597SMatthew G Knepley /*@
3474b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3475b21d0597SMatthew G Knepley 
3476b21d0597SMatthew G Knepley   Input Parameter:
3477b21d0597SMatthew G Knepley . dm - The DM
3478b21d0597SMatthew G Knepley 
3479b21d0597SMatthew G Knepley   Output Parameter:
3480b21d0597SMatthew G Knepley . sf - The PetscSF
3481b21d0597SMatthew G Knepley 
3482b21d0597SMatthew G Knepley   Level: intermediate
3483b21d0597SMatthew G Knepley 
3484b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3485b21d0597SMatthew G Knepley 
3486057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3487b21d0597SMatthew G Knepley @*/
34880adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
34890adebc6cSBarry Smith {
3490b21d0597SMatthew G Knepley   PetscFunctionBegin;
3491b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3492b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3493b21d0597SMatthew G Knepley   *sf = dm->sf;
3494b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3495b21d0597SMatthew G Knepley }
3496b21d0597SMatthew G Knepley 
3497b21d0597SMatthew G Knepley #undef __FUNCT__
3498057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3499057b4bcdSMatthew G Knepley /*@
3500057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3501057b4bcdSMatthew G Knepley 
3502057b4bcdSMatthew G Knepley   Input Parameters:
3503057b4bcdSMatthew G Knepley + dm - The DM
3504057b4bcdSMatthew G Knepley - sf - The PetscSF
3505057b4bcdSMatthew G Knepley 
3506057b4bcdSMatthew G Knepley   Level: intermediate
3507057b4bcdSMatthew G Knepley 
3508057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3509057b4bcdSMatthew G Knepley @*/
35100adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
35110adebc6cSBarry Smith {
3512057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3513057b4bcdSMatthew G Knepley 
3514057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3515057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3516057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3517057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3518057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3519057b4bcdSMatthew G Knepley   dm->sf = sf;
3520057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3521057b4bcdSMatthew G Knepley }
3522057b4bcdSMatthew G Knepley 
3523057b4bcdSMatthew G Knepley #undef __FUNCT__
35242764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
35252764a2aaSMatthew G. Knepley /*@
35262764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
35272764a2aaSMatthew G. Knepley 
35282764a2aaSMatthew G. Knepley   Input Parameter:
35292764a2aaSMatthew G. Knepley . dm - The DM
35302764a2aaSMatthew G. Knepley 
35312764a2aaSMatthew G. Knepley   Output Parameter:
35322764a2aaSMatthew G. Knepley . prob - The PetscDS
35332764a2aaSMatthew G. Knepley 
35342764a2aaSMatthew G. Knepley   Level: developer
35352764a2aaSMatthew G. Knepley 
35362764a2aaSMatthew G. Knepley .seealso: DMSetDS()
35372764a2aaSMatthew G. Knepley @*/
35382764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3539af122d2aSMatthew G Knepley {
3540af122d2aSMatthew G Knepley   PetscFunctionBegin;
3541af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35420f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
35430f21e855SMatthew G. Knepley   *prob = dm->prob;
35440f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
35450f21e855SMatthew G. Knepley }
35460f21e855SMatthew G. Knepley 
35470f21e855SMatthew G. Knepley #undef __FUNCT__
35482764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
35492764a2aaSMatthew G. Knepley /*@
35502764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
35512764a2aaSMatthew G. Knepley 
35522764a2aaSMatthew G. Knepley   Input Parameters:
35532764a2aaSMatthew G. Knepley + dm - The DM
35542764a2aaSMatthew G. Knepley - prob - The PetscDS
35552764a2aaSMatthew G. Knepley 
35562764a2aaSMatthew G. Knepley   Level: developer
35572764a2aaSMatthew G. Knepley 
35582764a2aaSMatthew G. Knepley .seealso: DMGetDS()
35592764a2aaSMatthew G. Knepley @*/
35602764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
35610f21e855SMatthew G. Knepley {
35620f21e855SMatthew G. Knepley   PetscErrorCode ierr;
35630f21e855SMatthew G. Knepley 
35640f21e855SMatthew G. Knepley   PetscFunctionBegin;
35650f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35662764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
35672764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
35680f21e855SMatthew G. Knepley   dm->prob = prob;
35690f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
35700f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
35710f21e855SMatthew G. Knepley }
35720f21e855SMatthew G. Knepley 
35730f21e855SMatthew G. Knepley #undef __FUNCT__
35740f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
35750f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
35760f21e855SMatthew G. Knepley {
35770f21e855SMatthew G. Knepley   PetscErrorCode ierr;
35780f21e855SMatthew G. Knepley 
35790f21e855SMatthew G. Knepley   PetscFunctionBegin;
35800f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35812764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3582af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3583af122d2aSMatthew G Knepley }
3584af122d2aSMatthew G Knepley 
3585af122d2aSMatthew G Knepley #undef __FUNCT__
3586af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3587af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3588af122d2aSMatthew G Knepley {
35890f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3590af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3591af122d2aSMatthew G Knepley 
3592af122d2aSMatthew G Knepley   PetscFunctionBegin;
3593af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35942764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
35950f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
35960f21e855SMatthew G. Knepley     PetscContainer obj;
35970f21e855SMatthew G. Knepley 
35980f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
35992764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
36000f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3601af122d2aSMatthew G Knepley   }
3602af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3603af122d2aSMatthew G Knepley }
3604af122d2aSMatthew G Knepley 
3605af122d2aSMatthew G Knepley #undef __FUNCT__
3606af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3607c1929be8SMatthew G. Knepley /*@
3608c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3609c1929be8SMatthew G. Knepley 
3610c1929be8SMatthew G. Knepley   Not collective
3611c1929be8SMatthew G. Knepley 
3612c1929be8SMatthew G. Knepley   Input Parameters:
3613c1929be8SMatthew G. Knepley + dm - The DM
3614c1929be8SMatthew G. Knepley - f  - The field number
3615c1929be8SMatthew G. Knepley 
3616c1929be8SMatthew G. Knepley   Output Parameter:
3617c1929be8SMatthew G. Knepley . field - The discretization object
3618c1929be8SMatthew G. Knepley 
3619c1929be8SMatthew G. Knepley   Level: developer
3620c1929be8SMatthew G. Knepley 
3621c1929be8SMatthew G. Knepley .seealso: DMSetField()
3622c1929be8SMatthew G. Knepley @*/
3623af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3624af122d2aSMatthew G Knepley {
36250f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36260f21e855SMatthew G. Knepley 
3627af122d2aSMatthew G Knepley   PetscFunctionBegin;
3628af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36292764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3630decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3631decb47aaSMatthew G. Knepley }
3632decb47aaSMatthew G. Knepley 
3633decb47aaSMatthew G. Knepley #undef __FUNCT__
3634decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3635c1929be8SMatthew G. Knepley /*@
3636c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3637c1929be8SMatthew G. Knepley 
3638c1929be8SMatthew G. Knepley   Logically collective on DM
3639c1929be8SMatthew G. Knepley 
3640c1929be8SMatthew G. Knepley   Input Parameters:
3641c1929be8SMatthew G. Knepley + dm - The DM
3642c1929be8SMatthew G. Knepley . f  - The field number
3643c1929be8SMatthew G. Knepley - field - The discretization object
3644c1929be8SMatthew G. Knepley 
3645c1929be8SMatthew G. Knepley   Level: developer
3646c1929be8SMatthew G. Knepley 
3647c1929be8SMatthew G. Knepley .seealso: DMGetField()
3648c1929be8SMatthew G. Knepley @*/
3649decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3650decb47aaSMatthew G. Knepley {
3651decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3652decb47aaSMatthew G. Knepley 
3653decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3654decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36552764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3656af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3657af122d2aSMatthew G Knepley }
36586636e97aSMatthew G Knepley 
36596636e97aSMatthew G Knepley #undef __FUNCT__
3660b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3661b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3662b64e0483SPeter Brune {
3663b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3664b64e0483SPeter Brune   PetscErrorCode ierr;
3665b64e0483SPeter Brune   Vec coords,ccoords;
36666dbf9973SLawrence Mitchell   Mat inject;
3667b64e0483SPeter Brune   PetscFunctionBegin;
3668b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3669b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3670b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3671b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3672b64e0483SPeter Brune   if (coords && !ccoords) {
3673b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
36746dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
36752adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
36766dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3677b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3678b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3679b64e0483SPeter Brune   }
3680b64e0483SPeter Brune   PetscFunctionReturn(0);
3681b64e0483SPeter Brune }
3682b64e0483SPeter Brune 
3683b64e0483SPeter Brune #undef __FUNCT__
368403dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
368503dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
368603dadc2fSPeter Brune {
368703dadc2fSPeter Brune   DM dm_coord,subdm_coord;
368803dadc2fSPeter Brune   PetscErrorCode ierr;
368903dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
369003dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
369103dadc2fSPeter Brune   PetscFunctionBegin;
369203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
369303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
369403dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
369503dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
369603dadc2fSPeter Brune   if (coords && !ccoords) {
369703dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
369803dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
369903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
370003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
370103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
370203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
370303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
370403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
370503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
370603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
370703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
370803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
370903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
371003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
371103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
371203dadc2fSPeter Brune   }
371303dadc2fSPeter Brune   PetscFunctionReturn(0);
371403dadc2fSPeter Brune }
371503dadc2fSPeter Brune 
371603dadc2fSPeter Brune #undef __FUNCT__
3717c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3718c73cfb54SMatthew G. Knepley /*@
3719c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3720c73cfb54SMatthew G. Knepley 
3721c73cfb54SMatthew G. Knepley   Not collective
3722c73cfb54SMatthew G. Knepley 
3723c73cfb54SMatthew G. Knepley   Input Parameter:
3724c73cfb54SMatthew G. Knepley . dm - The DM
3725c73cfb54SMatthew G. Knepley 
3726c73cfb54SMatthew G. Knepley   Output Parameter:
3727c73cfb54SMatthew G. Knepley . dim - The topological dimension
3728c73cfb54SMatthew G. Knepley 
3729c73cfb54SMatthew G. Knepley   Level: beginner
3730c73cfb54SMatthew G. Knepley 
3731c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3732c73cfb54SMatthew G. Knepley @*/
3733c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3734c73cfb54SMatthew G. Knepley {
3735c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3736c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3737c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3738c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3739c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3740c73cfb54SMatthew G. Knepley }
3741c73cfb54SMatthew G. Knepley 
3742c73cfb54SMatthew G. Knepley #undef __FUNCT__
3743c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3744c73cfb54SMatthew G. Knepley /*@
3745c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3746c73cfb54SMatthew G. Knepley 
3747c73cfb54SMatthew G. Knepley   Collective on dm
3748c73cfb54SMatthew G. Knepley 
3749c73cfb54SMatthew G. Knepley   Input Parameters:
3750c73cfb54SMatthew G. Knepley + dm - The DM
3751c73cfb54SMatthew G. Knepley - dim - The topological dimension
3752c73cfb54SMatthew G. Knepley 
3753c73cfb54SMatthew G. Knepley   Level: beginner
3754c73cfb54SMatthew G. Knepley 
3755c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3756c73cfb54SMatthew G. Knepley @*/
3757c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3758c73cfb54SMatthew G. Knepley {
3759c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3760c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3761c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3762c73cfb54SMatthew G. Knepley   dm->dim = dim;
3763c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3764c73cfb54SMatthew G. Knepley }
3765c73cfb54SMatthew G. Knepley 
3766793f3fe5SMatthew G. Knepley #undef __FUNCT__
3767793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3768793f3fe5SMatthew G. Knepley /*@
3769793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3770793f3fe5SMatthew G. Knepley 
3771793f3fe5SMatthew G. Knepley   Collective on DM
3772793f3fe5SMatthew G. Knepley 
3773793f3fe5SMatthew G. Knepley   Input Parameters:
3774793f3fe5SMatthew G. Knepley + dm - the DM
3775793f3fe5SMatthew G. Knepley - dim - the dimension
3776793f3fe5SMatthew G. Knepley 
3777793f3fe5SMatthew G. Knepley   Output Parameters:
3778793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3779793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3780793f3fe5SMatthew G. Knepley 
3781793f3fe5SMatthew G. Knepley   Note:
3782793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3783793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3784793f3fe5SMatthew G. Knepley   then the interval is empty.
3785793f3fe5SMatthew G. Knepley 
3786793f3fe5SMatthew G. Knepley   Level: intermediate
3787793f3fe5SMatthew G. Knepley 
3788793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3789793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3790793f3fe5SMatthew G. Knepley @*/
3791793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3792793f3fe5SMatthew G. Knepley {
3793793f3fe5SMatthew G. Knepley   PetscInt       d;
3794793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3795793f3fe5SMatthew G. Knepley 
3796793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3797793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3798793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3799793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3800793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3801793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3802793f3fe5SMatthew G. Knepley }
3803793f3fe5SMatthew G. Knepley 
3804793f3fe5SMatthew G. Knepley #undef __FUNCT__
38056636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
38066636e97aSMatthew G Knepley /*@
38076636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
38086636e97aSMatthew G Knepley 
38096636e97aSMatthew G Knepley   Collective on DM
38106636e97aSMatthew G Knepley 
38116636e97aSMatthew G Knepley   Input Parameters:
38126636e97aSMatthew G Knepley + dm - the DM
38136636e97aSMatthew G Knepley - c - coordinate vector
38146636e97aSMatthew G Knepley 
38156636e97aSMatthew G Knepley   Note:
38166636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
38176636e97aSMatthew G Knepley 
38186636e97aSMatthew G Knepley   Level: intermediate
38196636e97aSMatthew G Knepley 
38206636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38216636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
38226636e97aSMatthew G Knepley @*/
38236636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
38246636e97aSMatthew G Knepley {
38256636e97aSMatthew G Knepley   PetscErrorCode ierr;
38266636e97aSMatthew G Knepley 
38276636e97aSMatthew G Knepley   PetscFunctionBegin;
38286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38296636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
38306636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
38316636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
38326636e97aSMatthew G Knepley   dm->coordinates = c;
38336636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3834b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
383503dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
38366636e97aSMatthew G Knepley   PetscFunctionReturn(0);
38376636e97aSMatthew G Knepley }
38386636e97aSMatthew G Knepley 
38396636e97aSMatthew G Knepley #undef __FUNCT__
38406636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
38416636e97aSMatthew G Knepley /*@
38426636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
38436636e97aSMatthew G Knepley 
38446636e97aSMatthew G Knepley   Collective on DM
38456636e97aSMatthew G Knepley 
38466636e97aSMatthew G Knepley    Input Parameters:
38476636e97aSMatthew G Knepley +  dm - the DM
38486636e97aSMatthew G Knepley -  c - coordinate vector
38496636e97aSMatthew G Knepley 
38506636e97aSMatthew G Knepley   Note:
38516636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
38526636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
38536636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
38546636e97aSMatthew G Knepley 
38556636e97aSMatthew G Knepley   Level: intermediate
38566636e97aSMatthew G Knepley 
38576636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38586636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
38596636e97aSMatthew G Knepley @*/
38606636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
38616636e97aSMatthew G Knepley {
38626636e97aSMatthew G Knepley   PetscErrorCode ierr;
38636636e97aSMatthew G Knepley 
38646636e97aSMatthew G Knepley   PetscFunctionBegin;
38656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
38676636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
38686636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
38698865f1eaSKarl Rupp 
38706636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
38718865f1eaSKarl Rupp 
38726636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
38736636e97aSMatthew G Knepley   PetscFunctionReturn(0);
38746636e97aSMatthew G Knepley }
38756636e97aSMatthew G Knepley 
38766636e97aSMatthew G Knepley #undef __FUNCT__
38776636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
38786636e97aSMatthew G Knepley /*@
38796636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
38806636e97aSMatthew G Knepley 
38816636e97aSMatthew G Knepley   Not Collective
38826636e97aSMatthew G Knepley 
38836636e97aSMatthew G Knepley   Input Parameter:
38846636e97aSMatthew G Knepley . dm - the DM
38856636e97aSMatthew G Knepley 
38866636e97aSMatthew G Knepley   Output Parameter:
38876636e97aSMatthew G Knepley . c - global coordinate vector
38886636e97aSMatthew G Knepley 
38896636e97aSMatthew G Knepley   Note:
38906636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
38916636e97aSMatthew G Knepley 
38926636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
38936636e97aSMatthew G Knepley 
38946636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
38956636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
38966636e97aSMatthew G Knepley 
38976636e97aSMatthew G Knepley   Level: intermediate
38986636e97aSMatthew G Knepley 
38996636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39006636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
39016636e97aSMatthew G Knepley @*/
39026636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
39036636e97aSMatthew G Knepley {
39046636e97aSMatthew G Knepley   PetscErrorCode ierr;
39056636e97aSMatthew G Knepley 
39066636e97aSMatthew G Knepley   PetscFunctionBegin;
39076636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39086636e97aSMatthew G Knepley   PetscValidPointer(c,2);
39091f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
39100298fd71SBarry Smith     DM cdm = NULL;
39116636e97aSMatthew G Knepley 
39126636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
39136636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
39146636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
39156636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
39166636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
39176636e97aSMatthew G Knepley   }
39186636e97aSMatthew G Knepley   *c = dm->coordinates;
39196636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39206636e97aSMatthew G Knepley }
39216636e97aSMatthew G Knepley 
39226636e97aSMatthew G Knepley #undef __FUNCT__
39236636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
39246636e97aSMatthew G Knepley /*@
39256636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
39266636e97aSMatthew G Knepley 
39276636e97aSMatthew G Knepley   Collective on DM
39286636e97aSMatthew G Knepley 
39296636e97aSMatthew G Knepley   Input Parameter:
39306636e97aSMatthew G Knepley . dm - the DM
39316636e97aSMatthew G Knepley 
39326636e97aSMatthew G Knepley   Output Parameter:
39336636e97aSMatthew G Knepley . c - coordinate vector
39346636e97aSMatthew G Knepley 
39356636e97aSMatthew G Knepley   Note:
39366636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
39376636e97aSMatthew G Knepley 
39386636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
39396636e97aSMatthew G Knepley 
39406636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
39416636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
39426636e97aSMatthew G Knepley 
39436636e97aSMatthew G Knepley   Level: intermediate
39446636e97aSMatthew G Knepley 
39456636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39466636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
39476636e97aSMatthew G Knepley @*/
39486636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
39496636e97aSMatthew G Knepley {
39506636e97aSMatthew G Knepley   PetscErrorCode ierr;
39516636e97aSMatthew G Knepley 
39526636e97aSMatthew G Knepley   PetscFunctionBegin;
39536636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39546636e97aSMatthew G Knepley   PetscValidPointer(c,2);
39551f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
39560298fd71SBarry Smith     DM cdm = NULL;
39576636e97aSMatthew G Knepley 
39586636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
39596636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
39606636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
39616636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
39626636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
39636636e97aSMatthew G Knepley   }
39646636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
39656636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39666636e97aSMatthew G Knepley }
39676636e97aSMatthew G Knepley 
39686636e97aSMatthew G Knepley #undef __FUNCT__
39696636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
39706636e97aSMatthew G Knepley /*@
39711cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
39726636e97aSMatthew G Knepley 
39736636e97aSMatthew G Knepley   Collective on DM
39746636e97aSMatthew G Knepley 
39756636e97aSMatthew G Knepley   Input Parameter:
39766636e97aSMatthew G Knepley . dm - the DM
39776636e97aSMatthew G Knepley 
39786636e97aSMatthew G Knepley   Output Parameter:
39796636e97aSMatthew G Knepley . cdm - coordinate DM
39806636e97aSMatthew G Knepley 
39816636e97aSMatthew G Knepley   Level: intermediate
39826636e97aSMatthew G Knepley 
39836636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39841cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
39856636e97aSMatthew G Knepley @*/
39866636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
39876636e97aSMatthew G Knepley {
39886636e97aSMatthew G Knepley   PetscErrorCode ierr;
39896636e97aSMatthew G Knepley 
39906636e97aSMatthew G Knepley   PetscFunctionBegin;
39916636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39926636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
39936636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
399482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
39956636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
39966636e97aSMatthew G Knepley   }
39976636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
39986636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39996636e97aSMatthew G Knepley }
4000e87bb0d3SMatthew G Knepley 
4001e87bb0d3SMatthew G Knepley #undef __FUNCT__
40021cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
40031cfe2091SMatthew G. Knepley /*@
40041cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
40051cfe2091SMatthew G. Knepley 
40061cfe2091SMatthew G. Knepley   Logically Collective on DM
40071cfe2091SMatthew G. Knepley 
40081cfe2091SMatthew G. Knepley   Input Parameters:
40091cfe2091SMatthew G. Knepley + dm - the DM
40101cfe2091SMatthew G. Knepley - cdm - coordinate DM
40111cfe2091SMatthew G. Knepley 
40121cfe2091SMatthew G. Knepley   Level: intermediate
40131cfe2091SMatthew G. Knepley 
40141cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40151cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
40161cfe2091SMatthew G. Knepley @*/
40171cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
40181cfe2091SMatthew G. Knepley {
40191cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
40201cfe2091SMatthew G. Knepley 
40211cfe2091SMatthew G. Knepley   PetscFunctionBegin;
40221cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40231cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
40241cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
40251cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
40261cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
40271cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
40281cfe2091SMatthew G. Knepley }
40291cfe2091SMatthew G. Knepley 
40301cfe2091SMatthew G. Knepley #undef __FUNCT__
403146e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
403246e270d4SMatthew G. Knepley /*@
403346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
403446e270d4SMatthew G. Knepley 
403546e270d4SMatthew G. Knepley   Not Collective
403646e270d4SMatthew G. Knepley 
403746e270d4SMatthew G. Knepley   Input Parameter:
403846e270d4SMatthew G. Knepley . dm - The DM object
403946e270d4SMatthew G. Knepley 
404046e270d4SMatthew G. Knepley   Output Parameter:
404146e270d4SMatthew G. Knepley . dim - The embedding dimension
404246e270d4SMatthew G. Knepley 
404346e270d4SMatthew G. Knepley   Level: intermediate
404446e270d4SMatthew G. Knepley 
404546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
404646e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
404746e270d4SMatthew G. Knepley @*/
404846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
404946e270d4SMatthew G. Knepley {
405046e270d4SMatthew G. Knepley   PetscFunctionBegin;
405146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
405246e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
40539a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
40549a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
40559a9a41abSToby Isaac   }
405646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
405746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
405846e270d4SMatthew G. Knepley }
405946e270d4SMatthew G. Knepley 
406046e270d4SMatthew G. Knepley #undef __FUNCT__
406146e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
406246e270d4SMatthew G. Knepley /*@
406346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
406446e270d4SMatthew G. Knepley 
406546e270d4SMatthew G. Knepley   Not Collective
406646e270d4SMatthew G. Knepley 
406746e270d4SMatthew G. Knepley   Input Parameters:
406846e270d4SMatthew G. Knepley + dm  - The DM object
406946e270d4SMatthew G. Knepley - dim - The embedding dimension
407046e270d4SMatthew G. Knepley 
407146e270d4SMatthew G. Knepley   Level: intermediate
407246e270d4SMatthew G. Knepley 
407346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
407446e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
407546e270d4SMatthew G. Knepley @*/
407646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
407746e270d4SMatthew G. Knepley {
407846e270d4SMatthew G. Knepley   PetscFunctionBegin;
407946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
408046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
408146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
408246e270d4SMatthew G. Knepley }
408346e270d4SMatthew G. Knepley 
408446e270d4SMatthew G. Knepley #undef __FUNCT__
4085e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4086e8abe2deSMatthew G. Knepley /*@
4087e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4088e8abe2deSMatthew G. Knepley 
4089e8abe2deSMatthew G. Knepley   Not Collective
4090e8abe2deSMatthew G. Knepley 
4091e8abe2deSMatthew G. Knepley   Input Parameter:
4092e8abe2deSMatthew G. Knepley . dm - The DM object
4093e8abe2deSMatthew G. Knepley 
4094e8abe2deSMatthew G. Knepley   Output Parameter:
4095e8abe2deSMatthew G. Knepley . section - The PetscSection object
4096e8abe2deSMatthew G. Knepley 
4097e8abe2deSMatthew G. Knepley   Level: intermediate
4098e8abe2deSMatthew G. Knepley 
4099e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4100e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4101e8abe2deSMatthew G. Knepley @*/
4102e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4103e8abe2deSMatthew G. Knepley {
4104e8abe2deSMatthew G. Knepley   DM             cdm;
4105e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4106e8abe2deSMatthew G. Knepley 
4107e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4108e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4109e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4110e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4111e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4112e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4113e8abe2deSMatthew G. Knepley }
4114e8abe2deSMatthew G. Knepley 
4115e8abe2deSMatthew G. Knepley #undef __FUNCT__
4116e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4117e8abe2deSMatthew G. Knepley /*@
4118e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4119e8abe2deSMatthew G. Knepley 
4120e8abe2deSMatthew G. Knepley   Not Collective
4121e8abe2deSMatthew G. Knepley 
4122e8abe2deSMatthew G. Knepley   Input Parameters:
4123e8abe2deSMatthew G. Knepley + dm      - The DM object
412446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4125e8abe2deSMatthew G. Knepley - section - The PetscSection object
4126e8abe2deSMatthew G. Knepley 
4127e8abe2deSMatthew G. Knepley   Level: intermediate
4128e8abe2deSMatthew G. Knepley 
4129e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4130e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4131e8abe2deSMatthew G. Knepley @*/
413246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4133e8abe2deSMatthew G. Knepley {
4134e8abe2deSMatthew G. Knepley   DM             cdm;
4135e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4136e8abe2deSMatthew G. Knepley 
4137e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4138e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
413946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4140e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4141e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
414246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
414346e270d4SMatthew G. Knepley     PetscInt d = dim;
414446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
414546e270d4SMatthew G. Knepley 
414646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
414746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
414846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
414946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
415046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
415146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
415246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
415346e270d4SMatthew G. Knepley     }
415446e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
415546e270d4SMatthew G. Knepley   }
4156e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4157e8abe2deSMatthew G. Knepley }
4158e8abe2deSMatthew G. Knepley 
4159e8abe2deSMatthew G. Knepley #undef __FUNCT__
4160c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
41615dc8c3f7SMatthew G. Knepley /*@C
41625dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
41635dc8c3f7SMatthew G. Knepley 
41645dc8c3f7SMatthew G. Knepley   Input Parameters:
41655dc8c3f7SMatthew G. Knepley + dm      - The DM object
41665dc8c3f7SMatthew 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
41675dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
41685dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
41695dc8c3f7SMatthew G. Knepley 
41705dc8c3f7SMatthew G. Knepley   Level: developer
41715dc8c3f7SMatthew G. Knepley 
41725dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
41735dc8c3f7SMatthew G. Knepley @*/
41745dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4175c6b900c6SMatthew G. Knepley {
4176c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4177c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4178c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4179c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
41805dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4181c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4182c6b900c6SMatthew G. Knepley }
4183c6b900c6SMatthew G. Knepley 
4184c6b900c6SMatthew G. Knepley #undef __FUNCT__
4185c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
41865dc8c3f7SMatthew G. Knepley /*@C
41875dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
41885dc8c3f7SMatthew G. Knepley 
41895dc8c3f7SMatthew G. Knepley   Input Parameters:
41905dc8c3f7SMatthew G. Knepley + dm      - The DM object
41915dc8c3f7SMatthew 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
41925dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
41935dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
41945dc8c3f7SMatthew G. Knepley 
41955dc8c3f7SMatthew G. Knepley   Level: developer
41965dc8c3f7SMatthew G. Knepley 
41975dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
41985dc8c3f7SMatthew G. Knepley @*/
41995dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4200c6b900c6SMatthew G. Knepley {
4201c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4202c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4203c6b900c6SMatthew G. Knepley 
4204c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4205c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42065dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
42075dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
42085dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
42095dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
42105dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4211c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4212c6b900c6SMatthew G. Knepley }
4213c6b900c6SMatthew G. Knepley 
4214c6b900c6SMatthew G. Knepley #undef __FUNCT__
4215e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4216e87bb0d3SMatthew G Knepley /*@
4217e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4218e87bb0d3SMatthew G Knepley 
4219e87bb0d3SMatthew G Knepley   Not collective
4220e87bb0d3SMatthew G Knepley 
4221e87bb0d3SMatthew G Knepley   Input Parameters:
4222e87bb0d3SMatthew G Knepley + dm - The DM
4223e87bb0d3SMatthew G Knepley - v - The Vec of points
4224e87bb0d3SMatthew G Knepley 
422561e3bb9bSMatthew G Knepley   Output Parameter:
4226e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4227e87bb0d3SMatthew G Knepley 
4228e87bb0d3SMatthew G Knepley   Level: developer
422961e3bb9bSMatthew G Knepley 
423061e3bb9bSMatthew G Knepley .keywords: point location, mesh
423161e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
423261e3bb9bSMatthew G Knepley @*/
4233e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4234e87bb0d3SMatthew G Knepley {
4235735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4236735aa83eSMatthew G Knepley 
4237e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4238e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4239e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4240e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
4241735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4242735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
424382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
4244e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4245e87bb0d3SMatthew G Knepley }
424614f150ffSMatthew G. Knepley 
424714f150ffSMatthew G. Knepley #undef __FUNCT__
424814f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4249f4d763aaSMatthew G. Knepley /*@
4250f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4251f4d763aaSMatthew G. Knepley 
4252f4d763aaSMatthew G. Knepley   Input Parameter:
4253f4d763aaSMatthew G. Knepley . dm - The original DM
4254f4d763aaSMatthew G. Knepley 
4255f4d763aaSMatthew G. Knepley   Output Parameter:
4256f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4257f4d763aaSMatthew G. Knepley 
4258f4d763aaSMatthew G. Knepley   Level: intermediate
4259f4d763aaSMatthew G. Knepley 
4260f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4261f4d763aaSMatthew G. Knepley @*/
426214f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
426314f150ffSMatthew G. Knepley {
4264c26acbdeSMatthew G. Knepley   PetscSection   section;
4265c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
426614f150ffSMatthew G. Knepley   PetscErrorCode ierr;
426714f150ffSMatthew G. Knepley 
426814f150ffSMatthew G. Knepley   PetscFunctionBegin;
426914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
427014f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4271c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4272c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4273c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4274c26acbdeSMatthew G. Knepley     *odm = dm;
4275c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4276c26acbdeSMatthew G. Knepley   }
427714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4278c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
427914f150ffSMatthew G. Knepley     PetscSF      sf;
428014f150ffSMatthew G. Knepley 
428114f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
428214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
428314f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
428414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
428514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
428614f150ffSMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, &gsection);CHKERRQ(ierr);
428714f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
428814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
428914f150ffSMatthew G. Knepley   }
429014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
429114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
429214f150ffSMatthew G. Knepley }
4293f4d763aaSMatthew G. Knepley 
4294f4d763aaSMatthew G. Knepley #undef __FUNCT__
4295f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4296f4d763aaSMatthew G. Knepley /*@
4297cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4298f4d763aaSMatthew G. Knepley 
4299f4d763aaSMatthew G. Knepley   Input Parameter:
4300f4d763aaSMatthew G. Knepley . dm - The original DM
4301f4d763aaSMatthew G. Knepley 
4302cdb7a50dSMatthew G. Knepley   Output Parameters:
4303cdb7a50dSMatthew G. Knepley + num - The output sequence number
4304cdb7a50dSMatthew G. Knepley - val - The output sequence value
4305f4d763aaSMatthew G. Knepley 
4306f4d763aaSMatthew G. Knepley   Level: intermediate
4307f4d763aaSMatthew G. Knepley 
4308f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4309f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4310f4d763aaSMatthew G. Knepley 
4311f4d763aaSMatthew G. Knepley .seealso: VecView()
4312f4d763aaSMatthew G. Knepley @*/
4313cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4314f4d763aaSMatthew G. Knepley {
4315f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4316f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4317cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4318cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4319f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4320f4d763aaSMatthew G. Knepley }
4321f4d763aaSMatthew G. Knepley 
4322f4d763aaSMatthew G. Knepley #undef __FUNCT__
4323f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4324f4d763aaSMatthew G. Knepley /*@
4325cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4326f4d763aaSMatthew G. Knepley 
4327f4d763aaSMatthew G. Knepley   Input Parameters:
4328f4d763aaSMatthew G. Knepley + dm - The original DM
4329cdb7a50dSMatthew G. Knepley . num - The output sequence number
4330cdb7a50dSMatthew G. Knepley - val - The output sequence value
4331f4d763aaSMatthew G. Knepley 
4332f4d763aaSMatthew G. Knepley   Level: intermediate
4333f4d763aaSMatthew G. Knepley 
4334f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4335f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4336f4d763aaSMatthew G. Knepley 
4337f4d763aaSMatthew G. Knepley .seealso: VecView()
4338f4d763aaSMatthew G. Knepley @*/
4339cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4340f4d763aaSMatthew G. Knepley {
4341f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4342f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4343f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4344cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4345cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4346cdb7a50dSMatthew G. Knepley }
4347cdb7a50dSMatthew G. Knepley 
4348cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4349cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4350cdb7a50dSMatthew G. Knepley /*@C
4351cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4352cdb7a50dSMatthew G. Knepley 
4353cdb7a50dSMatthew G. Knepley   Input Parameters:
4354cdb7a50dSMatthew G. Knepley + dm   - The original DM
4355cdb7a50dSMatthew G. Knepley . name - The sequence name
4356cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4357cdb7a50dSMatthew G. Knepley 
4358cdb7a50dSMatthew G. Knepley   Output Parameter:
4359cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4360cdb7a50dSMatthew G. Knepley 
4361cdb7a50dSMatthew G. Knepley   Level: intermediate
4362cdb7a50dSMatthew G. Knepley 
4363cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4364cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4365cdb7a50dSMatthew G. Knepley 
4366cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4367cdb7a50dSMatthew G. Knepley @*/
4368cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4369cdb7a50dSMatthew G. Knepley {
4370cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4371cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4372cdb7a50dSMatthew G. Knepley 
4373cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4374cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4375cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4376cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4377cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4378cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4379cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4380cdb7a50dSMatthew G. Knepley     PetscScalar value;
4381cdb7a50dSMatthew G. Knepley 
4382cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
43834aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4384cdb7a50dSMatthew G. Knepley #endif
4385cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4386f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4387f4d763aaSMatthew G. Knepley }
4388*8e4ac7eaSMatthew G. Knepley 
4389*8e4ac7eaSMatthew G. Knepley #undef __FUNCT__
4390*8e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
4391*8e4ac7eaSMatthew G. Knepley /*@
4392*8e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
4393*8e4ac7eaSMatthew G. Knepley 
4394*8e4ac7eaSMatthew G. Knepley   Not collective
4395*8e4ac7eaSMatthew G. Knepley 
4396*8e4ac7eaSMatthew G. Knepley   Input Parameter:
4397*8e4ac7eaSMatthew G. Knepley . dm - The DM
4398*8e4ac7eaSMatthew G. Knepley 
4399*8e4ac7eaSMatthew G. Knepley   Output Parameter:
4400*8e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
4401*8e4ac7eaSMatthew G. Knepley 
4402*8e4ac7eaSMatthew G. Knepley   Level: beginner
4403*8e4ac7eaSMatthew G. Knepley 
4404*8e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
4405*8e4ac7eaSMatthew G. Knepley @*/
4406*8e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
4407*8e4ac7eaSMatthew G. Knepley {
4408*8e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
4409*8e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4410*8e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
4411*8e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
4412*8e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
4413*8e4ac7eaSMatthew G. Knepley }
4414*8e4ac7eaSMatthew G. Knepley 
4415*8e4ac7eaSMatthew G. Knepley #undef __FUNCT__
4416*8e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
4417*8e4ac7eaSMatthew G. Knepley /*@
4418*8e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
4419*8e4ac7eaSMatthew G. Knepley 
4420*8e4ac7eaSMatthew G. Knepley   Collective on dm
4421*8e4ac7eaSMatthew G. Knepley 
4422*8e4ac7eaSMatthew G. Knepley   Input Parameters:
4423*8e4ac7eaSMatthew G. Knepley + dm - The DM
4424*8e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
4425*8e4ac7eaSMatthew G. Knepley 
4426*8e4ac7eaSMatthew G. Knepley   Level: beginner
4427*8e4ac7eaSMatthew G. Knepley 
4428*8e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
4429*8e4ac7eaSMatthew G. Knepley @*/
4430*8e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
4431*8e4ac7eaSMatthew G. Knepley {
4432*8e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
4433*8e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4434*8e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
4435*8e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
4436*8e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
4437*8e4ac7eaSMatthew G. Knepley }
4438