xref: /petsc/src/dm/interface/dm.c (revision 4c274da1aed16108e7b9a73385c4d18ab69e2a5d)
1b45d2f2cSJed Brown #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 
4367c2884eSBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
44a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
451411c6eeSJed Brown 
46e7c4fc90SDmitry Karpeev 
470298fd71SBarry Smith   v->ltogmap                  = NULL;
481411c6eeSJed Brown   v->bs                       = 1;
49171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5188ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
520298fd71SBarry Smith   v->defaultSection           = NULL;
530298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
54fba222abSToby Isaac   v->defaultConstraintSection = NULL;
55fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
56c6b900c6SMatthew G. Knepley   v->L                        = NULL;
57c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
589a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
59435a35e8SMatthew G Knepley   {
60435a35e8SMatthew G Knepley     PetscInt i;
61435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
620298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
63435a35e8SMatthew G Knepley     }
64435a35e8SMatthew G Knepley   }
652764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6614f150ffSMatthew G. Knepley   v->dmBC = NULL;
67f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
68cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
69c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
70b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
711411c6eeSJed Brown   *dm = v;
72a4121054SBarry Smith   PetscFunctionReturn(0);
73a4121054SBarry Smith }
74a4121054SBarry Smith 
75a4121054SBarry Smith #undef __FUNCT__
7638221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
7738221697SMatthew G. Knepley /*@
7838221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
7938221697SMatthew G. Knepley 
8038221697SMatthew G. Knepley   Collective on MPI_Comm
8138221697SMatthew G. Knepley 
8238221697SMatthew G. Knepley   Input Parameter:
8338221697SMatthew G. Knepley . dm - The original DM object
8438221697SMatthew G. Knepley 
8538221697SMatthew G. Knepley   Output Parameter:
8638221697SMatthew G. Knepley . newdm  - The new DM object
8738221697SMatthew G. Knepley 
8838221697SMatthew G. Knepley   Level: beginner
8938221697SMatthew G. Knepley 
9038221697SMatthew G. Knepley .keywords: DM, topology, create
9138221697SMatthew G. Knepley @*/
9238221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9338221697SMatthew G. Knepley {
9438221697SMatthew G. Knepley   PetscSF        sf;
9538221697SMatthew G. Knepley   Vec            coords;
9638221697SMatthew G. Knepley   void          *ctx;
971de53e9aSMatthew G. Knepley   PetscInt       dim;
9838221697SMatthew G. Knepley   PetscErrorCode ierr;
9938221697SMatthew G. Knepley 
10038221697SMatthew G. Knepley   PetscFunctionBegin;
10138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10238221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10338221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
1041de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1051de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
10638221697SMatthew G. Knepley   if (dm->ops->clone) {
10738221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
10838221697SMatthew G. Knepley   }
109cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
11038221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
11138221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
11238221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
11338221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
11438221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
11538221697SMatthew G. Knepley   if (coords) {
11638221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
11738221697SMatthew G. Knepley   } else {
11838221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
11938221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
12038221697SMatthew G. Knepley   }
121c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
122c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
123c6b900c6SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L);CHKERRQ(ierr);
124c6b900c6SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L);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);
3779a42bb27SBarry Smith   PetscFunctionReturn(0);
3789a42bb27SBarry Smith }
3799a42bb27SBarry Smith 
3809a42bb27SBarry Smith #undef __FUNCT__
38147c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
38247c6ae99SBarry Smith /*@
383aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
38447c6ae99SBarry Smith 
38547c6ae99SBarry Smith     Collective on DM
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith     Input Parameter:
38847c6ae99SBarry Smith .   dm - the DM object to destroy
38947c6ae99SBarry Smith 
39047c6ae99SBarry Smith     Level: developer
39147c6ae99SBarry Smith 
392e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
39347c6ae99SBarry Smith 
39447c6ae99SBarry Smith @*/
395fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
39647c6ae99SBarry Smith {
397c83e3748SMatthew G. Knepley   PetscInt       i, cnt = 0;
398dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
39947c6ae99SBarry Smith   PetscErrorCode ierr;
40047c6ae99SBarry Smith 
40147c6ae99SBarry Smith   PetscFunctionBegin;
4026bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4036bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
40487e657c6SBarry Smith 
40587e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
406732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4078865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4088865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
409732e2eb9SMatthew G Knepley   }
410dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4112348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4122348bcf4SPeter Brune   if ((*dm)->x) {
4132348bcf4SPeter Brune     DM obj;
4142348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4152348bcf4SPeter Brune     if (obj == *dm) cnt++;
4162348bcf4SPeter Brune   }
417732e2eb9SMatthew G Knepley 
4186bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
419732e2eb9SMatthew G Knepley   /*
420732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
421732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
422732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
423732e2eb9SMatthew G Knepley   */
4246bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4256bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
426732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4276bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4286bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
429732e2eb9SMatthew G Knepley   }
430f490541aSPeter Brune   nnext=(*dm)->namedglobal;
4310298fd71SBarry Smith   (*dm)->namedglobal = NULL;
432f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
4332348bcf4SPeter Brune     nnext = nlink->next;
4342348bcf4SPeter 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);
4352348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4362348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4372348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4382348bcf4SPeter Brune   }
439f490541aSPeter Brune   nnext=(*dm)->namedlocal;
4400298fd71SBarry Smith   (*dm)->namedlocal = NULL;
441f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
442f490541aSPeter Brune     nnext = nlink->next;
443f490541aSPeter 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);
444f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
445f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
446f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
447f490541aSPeter Brune   }
4482348bcf4SPeter Brune 
449b17ce1afSJed Brown   /* Destroy the list of hooks */
450c833c3b5SJed Brown   {
451c833c3b5SJed Brown     DMCoarsenHookLink link,next;
452b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
453b17ce1afSJed Brown       next = link->next;
454b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
455b17ce1afSJed Brown     }
4560298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
457c833c3b5SJed Brown   }
458c833c3b5SJed Brown   {
459c833c3b5SJed Brown     DMRefineHookLink link,next;
460c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
461c833c3b5SJed Brown       next = link->next;
462c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
463c833c3b5SJed Brown     }
4640298fd71SBarry Smith     (*dm)->refinehook = NULL;
465c833c3b5SJed Brown   }
466be081cd6SPeter Brune   {
467be081cd6SPeter Brune     DMSubDomainHookLink link,next;
468be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
469be081cd6SPeter Brune       next = link->next;
470be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
471be081cd6SPeter Brune     }
4720298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
473be081cd6SPeter Brune   }
474baf369e7SPeter Brune   {
475baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
476baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
477baf369e7SPeter Brune       next = link->next;
478baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
479baf369e7SPeter Brune     }
4800298fd71SBarry Smith     (*dm)->gtolhook = NULL;
481baf369e7SPeter Brune   }
482d4d07f1eSToby Isaac   {
483d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
484d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
485d4d07f1eSToby Isaac       next = link->next;
486d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
487d4d07f1eSToby Isaac     }
488d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
489d4d07f1eSToby Isaac   }
490aa1993deSMatthew G Knepley   /* Destroy the work arrays */
491aa1993deSMatthew G Knepley   {
492aa1993deSMatthew G Knepley     DMWorkLink link,next;
493aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
494aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
495aa1993deSMatthew G Knepley       next = link->next;
496aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
497aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
498aa1993deSMatthew G Knepley     }
4990298fd71SBarry Smith     (*dm)->workin = NULL;
500aa1993deSMatthew G Knepley   }
501b17ce1afSJed Brown 
50252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
50352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
50452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
50552536dc3SBarry Smith 
5061a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5071a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5081a266240SBarry Smith   }
50987e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
51071cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5114dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5126bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5136bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
514073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
51588ed4aceSMatthew G Knepley 
51688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
51788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
5188b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
519fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
520fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
52188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
52288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
523af122d2aSMatthew G Knepley 
5246636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
5256636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
5266636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
527c6b900c6SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
528c6b900c6SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
5296636e97aSMatthew G Knepley 
5302764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
53114f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
532e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
533e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
534732e2eb9SMatthew G Knepley 
5356bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
536435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
537732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
53847c6ae99SBarry Smith   PetscFunctionReturn(0);
53947c6ae99SBarry Smith }
54047c6ae99SBarry Smith 
54147c6ae99SBarry Smith #undef __FUNCT__
542d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
543d7bf68aeSBarry Smith /*@
544d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
545d7bf68aeSBarry Smith 
546d7bf68aeSBarry Smith     Collective on DM
547d7bf68aeSBarry Smith 
548d7bf68aeSBarry Smith     Input Parameter:
549d7bf68aeSBarry Smith .   dm - the DM object to setup
550d7bf68aeSBarry Smith 
551d7bf68aeSBarry Smith     Level: developer
552d7bf68aeSBarry Smith 
553e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
554d7bf68aeSBarry Smith 
555d7bf68aeSBarry Smith @*/
5567087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
557d7bf68aeSBarry Smith {
558d7bf68aeSBarry Smith   PetscErrorCode ierr;
559d7bf68aeSBarry Smith 
560d7bf68aeSBarry Smith   PetscFunctionBegin;
561171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5628387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
563d7bf68aeSBarry Smith   if (dm->ops->setup) {
564d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
565d7bf68aeSBarry Smith   }
5668387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
567d7bf68aeSBarry Smith   PetscFunctionReturn(0);
568d7bf68aeSBarry Smith }
569d7bf68aeSBarry Smith 
570d7bf68aeSBarry Smith #undef __FUNCT__
571d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
572d7bf68aeSBarry Smith /*@
573d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
574d7bf68aeSBarry Smith 
575d7bf68aeSBarry Smith     Collective on DM
576d7bf68aeSBarry Smith 
577d7bf68aeSBarry Smith     Input Parameter:
578d7bf68aeSBarry Smith .   dm - the DM object to set options for
579d7bf68aeSBarry Smith 
580732e2eb9SMatthew G Knepley     Options Database:
581dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
582dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
583171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
584171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
585732e2eb9SMatthew G Knepley 
586d7bf68aeSBarry Smith     Level: developer
587d7bf68aeSBarry Smith 
588e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
589d7bf68aeSBarry Smith 
590d7bf68aeSBarry Smith @*/
5917087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
592d7bf68aeSBarry Smith {
5937781c08eSBarry Smith   char           typeName[256];
594ca266f36SBarry Smith   PetscBool      flg;
595d7bf68aeSBarry Smith   PetscErrorCode ierr;
596d7bf68aeSBarry Smith 
597d7bf68aeSBarry Smith   PetscFunctionBegin;
598171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5993194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
6000298fd71SBarry 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);
601a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
602f9ba7244SBarry Smith   if (flg) {
603f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
604f9ba7244SBarry Smith   }
605a264d7a6SBarry 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);
606073dac72SJed Brown   if (flg) {
607521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
608073dac72SJed Brown   }
6090298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
610f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
611f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
612f9ba7244SBarry Smith   }
613f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
614f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
61582fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
616d7bf68aeSBarry Smith   PetscFunctionReturn(0);
617d7bf68aeSBarry Smith }
618d7bf68aeSBarry Smith 
619d7bf68aeSBarry Smith #undef __FUNCT__
62047c6ae99SBarry Smith #define __FUNCT__ "DMView"
621fc9bc008SSatish Balay /*@C
622aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
62347c6ae99SBarry Smith 
62447c6ae99SBarry Smith     Collective on DM
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith     Input Parameter:
62747c6ae99SBarry Smith +   dm - the DM object to view
62847c6ae99SBarry Smith -   v - the viewer
62947c6ae99SBarry Smith 
63047c6ae99SBarry Smith     Level: developer
63147c6ae99SBarry Smith 
632e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
63347c6ae99SBarry Smith 
63447c6ae99SBarry Smith @*/
6357087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
63647c6ae99SBarry Smith {
63747c6ae99SBarry Smith   PetscErrorCode ierr;
63832c0f0efSBarry Smith   PetscBool      isbinary;
63947c6ae99SBarry Smith 
64047c6ae99SBarry Smith   PetscFunctionBegin;
641171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6423014e516SBarry Smith   if (!v) {
643ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6443014e516SBarry Smith   }
64598c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
64632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
64732c0f0efSBarry Smith   if (isbinary) {
64855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
64932c0f0efSBarry Smith     char     type[256];
65032c0f0efSBarry Smith 
65132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
65232c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
65332c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
65432c0f0efSBarry Smith   }
6550c010503SBarry Smith   if (dm->ops->view) {
6560c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
65747c6ae99SBarry Smith   }
65847c6ae99SBarry Smith   PetscFunctionReturn(0);
65947c6ae99SBarry Smith }
66047c6ae99SBarry Smith 
66147c6ae99SBarry Smith #undef __FUNCT__
66247c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
66347c6ae99SBarry Smith /*@
664aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
66547c6ae99SBarry Smith 
66647c6ae99SBarry Smith     Collective on DM
66747c6ae99SBarry Smith 
66847c6ae99SBarry Smith     Input Parameter:
66947c6ae99SBarry Smith .   dm - the DM object
67047c6ae99SBarry Smith 
67147c6ae99SBarry Smith     Output Parameter:
67247c6ae99SBarry Smith .   vec - the global vector
67347c6ae99SBarry Smith 
674073dac72SJed Brown     Level: beginner
67547c6ae99SBarry Smith 
676e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith @*/
6797087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
68047c6ae99SBarry Smith {
68147c6ae99SBarry Smith   PetscErrorCode ierr;
68247c6ae99SBarry Smith 
68347c6ae99SBarry Smith   PetscFunctionBegin;
684171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
68647c6ae99SBarry Smith   PetscFunctionReturn(0);
68747c6ae99SBarry Smith }
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith #undef __FUNCT__
69047c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
69147c6ae99SBarry Smith /*@
692aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith     Not Collective
69547c6ae99SBarry Smith 
69647c6ae99SBarry Smith     Input Parameter:
69747c6ae99SBarry Smith .   dm - the DM object
69847c6ae99SBarry Smith 
69947c6ae99SBarry Smith     Output Parameter:
70047c6ae99SBarry Smith .   vec - the local vector
70147c6ae99SBarry Smith 
702073dac72SJed Brown     Level: beginner
70347c6ae99SBarry Smith 
704e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith @*/
7077087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
70847c6ae99SBarry Smith {
70947c6ae99SBarry Smith   PetscErrorCode ierr;
71047c6ae99SBarry Smith 
71147c6ae99SBarry Smith   PetscFunctionBegin;
712171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71347c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
71447c6ae99SBarry Smith   PetscFunctionReturn(0);
71547c6ae99SBarry Smith }
71647c6ae99SBarry Smith 
71747c6ae99SBarry Smith #undef __FUNCT__
7181411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
7191411c6eeSJed Brown /*@
7201411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
7211411c6eeSJed Brown 
7221411c6eeSJed Brown    Collective on DM
7231411c6eeSJed Brown 
7241411c6eeSJed Brown    Input Parameter:
7251411c6eeSJed Brown .  dm - the DM that provides the mapping
7261411c6eeSJed Brown 
7271411c6eeSJed Brown    Output Parameter:
7281411c6eeSJed Brown .  ltog - the mapping
7291411c6eeSJed Brown 
7301411c6eeSJed Brown    Level: intermediate
7311411c6eeSJed Brown 
7321411c6eeSJed Brown    Notes:
7331411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
7341411c6eeSJed Brown    MatSetLocalToGlobalMapping().
7351411c6eeSJed Brown 
736fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
7371411c6eeSJed Brown @*/
7387087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7391411c6eeSJed Brown {
7401411c6eeSJed Brown   PetscErrorCode ierr;
7411411c6eeSJed Brown 
7421411c6eeSJed Brown   PetscFunctionBegin;
7431411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7441411c6eeSJed Brown   PetscValidPointer(ltog,2);
7451411c6eeSJed Brown   if (!dm->ltogmap) {
74637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
74737d0c07bSMatthew G Knepley 
74837d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
74937d0c07bSMatthew G Knepley     if (section) {
75037d0c07bSMatthew G Knepley       PetscInt *ltog;
75137d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
75237d0c07bSMatthew G Knepley 
75337d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
75437d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
75537d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
756785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
75737d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
75837d0c07bSMatthew G Knepley         PetscInt dof, off, c;
75937d0c07bSMatthew G Knepley 
76037d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
76137d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
76237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
76337d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
76437d0c07bSMatthew G Knepley           ltog[l] = off+c;
76537d0c07bSMatthew G Knepley         }
76637d0c07bSMatthew G Knepley       }
767f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
7683bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
76937d0c07bSMatthew G Knepley     } else {
770184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
771184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7721411c6eeSJed Brown     }
77337d0c07bSMatthew G Knepley   }
7741411c6eeSJed Brown   *ltog = dm->ltogmap;
7751411c6eeSJed Brown   PetscFunctionReturn(0);
7761411c6eeSJed Brown }
7771411c6eeSJed Brown 
7781411c6eeSJed Brown #undef __FUNCT__
7791411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7801411c6eeSJed Brown /*@
7811411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7821411c6eeSJed Brown 
7831411c6eeSJed Brown    Not Collective
7841411c6eeSJed Brown 
7851411c6eeSJed Brown    Input Parameter:
7861411c6eeSJed Brown .  dm - the DM with block structure
7871411c6eeSJed Brown 
7881411c6eeSJed Brown    Output Parameter:
7891411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7901411c6eeSJed Brown 
7911411c6eeSJed Brown    Level: intermediate
7921411c6eeSJed Brown 
793fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
7941411c6eeSJed Brown @*/
7957087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7961411c6eeSJed Brown {
7971411c6eeSJed Brown   PetscFunctionBegin;
7981411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7991411c6eeSJed Brown   PetscValidPointer(bs,2);
8001411c6eeSJed 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");
8011411c6eeSJed Brown   *bs = dm->bs;
8021411c6eeSJed Brown   PetscFunctionReturn(0);
8031411c6eeSJed Brown }
8041411c6eeSJed Brown 
8051411c6eeSJed Brown #undef __FUNCT__
806e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
80747c6ae99SBarry Smith /*@
808e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
80947c6ae99SBarry Smith 
81047c6ae99SBarry Smith     Collective on DM
81147c6ae99SBarry Smith 
81247c6ae99SBarry Smith     Input Parameter:
81347c6ae99SBarry Smith +   dm1 - the DM object
81447c6ae99SBarry Smith -   dm2 - the second, finer DM object
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith     Output Parameter:
81747c6ae99SBarry Smith +  mat - the interpolation
81847c6ae99SBarry Smith -  vec - the scaling (optional)
81947c6ae99SBarry Smith 
82047c6ae99SBarry Smith     Level: developer
82147c6ae99SBarry Smith 
82285afcc9aSBarry 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
82385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
824d52bd9f3SBarry Smith 
8251f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
826d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
82785afcc9aSBarry Smith 
82885afcc9aSBarry Smith 
829e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
83047c6ae99SBarry Smith 
83147c6ae99SBarry Smith @*/
832e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
83347c6ae99SBarry Smith {
83447c6ae99SBarry Smith   PetscErrorCode ierr;
83547c6ae99SBarry Smith 
83647c6ae99SBarry Smith   PetscFunctionBegin;
837171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
838171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83925296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
84047c6ae99SBarry Smith   PetscFunctionReturn(0);
84147c6ae99SBarry Smith }
84247c6ae99SBarry Smith 
84347c6ae99SBarry Smith #undef __FUNCT__
844e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
84547c6ae99SBarry Smith /*@
846e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith     Collective on DM
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith     Input Parameter:
85147c6ae99SBarry Smith +   dm1 - the DM object
85247c6ae99SBarry Smith -   dm2 - the second, finer DM object
85347c6ae99SBarry Smith 
85447c6ae99SBarry Smith     Output Parameter:
85547c6ae99SBarry Smith .   ctx - the injection
85647c6ae99SBarry Smith 
85747c6ae99SBarry Smith     Level: developer
85847c6ae99SBarry Smith 
85985afcc9aSBarry 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
86085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
86185afcc9aSBarry Smith 
862e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith @*/
865e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
86647c6ae99SBarry Smith {
86747c6ae99SBarry Smith   PetscErrorCode ierr;
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith   PetscFunctionBegin;
870171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
871171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
87247c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
87347c6ae99SBarry Smith   PetscFunctionReturn(0);
87447c6ae99SBarry Smith }
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith #undef __FUNCT__
877e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
878b412c318SBarry Smith /*@
879b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
88047c6ae99SBarry Smith 
88147c6ae99SBarry Smith     Collective on DM
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith     Input Parameter:
88447c6ae99SBarry Smith +   dm - the DM object
885b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith     Output Parameter:
88847c6ae99SBarry Smith .   coloring - the coloring
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith     Level: developer
89147c6ae99SBarry Smith 
892b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
89347c6ae99SBarry Smith 
894aab9d709SJed Brown @*/
895b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
89647c6ae99SBarry Smith {
89747c6ae99SBarry Smith   PetscErrorCode ierr;
89847c6ae99SBarry Smith 
89947c6ae99SBarry Smith   PetscFunctionBegin;
900171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
901ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
902b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
90347c6ae99SBarry Smith   PetscFunctionReturn(0);
90447c6ae99SBarry Smith }
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith #undef __FUNCT__
907950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
908b412c318SBarry Smith /*@
909950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith     Collective on DM
91247c6ae99SBarry Smith 
91347c6ae99SBarry Smith     Input Parameter:
914b412c318SBarry Smith .   dm - the DM object
91547c6ae99SBarry Smith 
91647c6ae99SBarry Smith     Output Parameter:
91747c6ae99SBarry Smith .   mat - the empty Jacobian
91847c6ae99SBarry Smith 
919073dac72SJed Brown     Level: beginner
92047c6ae99SBarry Smith 
92194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
92294013140SBarry Smith        do not need to do it yourself.
92394013140SBarry Smith 
92494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
925aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
92694013140SBarry Smith 
92794013140SBarry 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
92894013140SBarry Smith        internally by PETSc.
92994013140SBarry Smith 
93094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
931aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
93294013140SBarry Smith 
933b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
93447c6ae99SBarry Smith 
935aab9d709SJed Brown @*/
936b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
93747c6ae99SBarry Smith {
93847c6ae99SBarry Smith   PetscErrorCode ierr;
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith   PetscFunctionBegin;
941171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
942607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
943c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
944c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
945b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
94647c6ae99SBarry Smith   PetscFunctionReturn(0);
94747c6ae99SBarry Smith }
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith #undef __FUNCT__
950732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
951732e2eb9SMatthew G Knepley /*@
952950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
953732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
954732e2eb9SMatthew G Knepley 
955732e2eb9SMatthew G Knepley   Logically Collective on DMDA
956732e2eb9SMatthew G Knepley 
957732e2eb9SMatthew G Knepley   Input Parameter:
958732e2eb9SMatthew G Knepley + dm - the DM
959732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
960732e2eb9SMatthew G Knepley 
961732e2eb9SMatthew G Knepley   Level: developer
962950540a4SJed Brown .seealso DMCreateMatrix()
963732e2eb9SMatthew G Knepley @*/
964732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
965732e2eb9SMatthew G Knepley {
966732e2eb9SMatthew G Knepley   PetscFunctionBegin;
967732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
968732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
969732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
970732e2eb9SMatthew G Knepley }
971732e2eb9SMatthew G Knepley 
972732e2eb9SMatthew G Knepley #undef __FUNCT__
973a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
974a89ea682SMatthew G Knepley /*@C
975aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
976a89ea682SMatthew G Knepley 
977a89ea682SMatthew G Knepley   Not Collective
978a89ea682SMatthew G Knepley 
979a89ea682SMatthew G Knepley   Input Parameters:
980a89ea682SMatthew G Knepley + dm - the DM object
981aa1993deSMatthew G Knepley . count - The minium size
982aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
983a89ea682SMatthew G Knepley 
984a89ea682SMatthew G Knepley   Output Parameter:
985a89ea682SMatthew G Knepley . array - the work array
986a89ea682SMatthew G Knepley 
987a89ea682SMatthew G Knepley   Level: developer
988a89ea682SMatthew G Knepley 
989a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
990a89ea682SMatthew G Knepley @*/
991aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
992a89ea682SMatthew G Knepley {
993a89ea682SMatthew G Knepley   PetscErrorCode ierr;
994aa1993deSMatthew G Knepley   DMWorkLink     link;
995aa1993deSMatthew G Knepley   size_t         size;
996a89ea682SMatthew G Knepley 
997a89ea682SMatthew G Knepley   PetscFunctionBegin;
998a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
999aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1000aa1993deSMatthew G Knepley   if (dm->workin) {
1001aa1993deSMatthew G Knepley     link       = dm->workin;
1002aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1003aa1993deSMatthew G Knepley   } else {
1004b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1005a89ea682SMatthew G Knepley   }
1006aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1007aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1008aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1009aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1010aa1993deSMatthew G Knepley     link->bytes = size*count;
1011aa1993deSMatthew G Knepley   }
1012aa1993deSMatthew G Knepley   link->next   = dm->workout;
1013aa1993deSMatthew G Knepley   dm->workout  = link;
1014aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1015a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1016a89ea682SMatthew G Knepley }
1017a89ea682SMatthew G Knepley 
1018aa1993deSMatthew G Knepley #undef __FUNCT__
1019aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1020aa1993deSMatthew G Knepley /*@C
1021aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1022aa1993deSMatthew G Knepley 
1023aa1993deSMatthew G Knepley   Not Collective
1024aa1993deSMatthew G Knepley 
1025aa1993deSMatthew G Knepley   Input Parameters:
1026aa1993deSMatthew G Knepley + dm - the DM object
1027aa1993deSMatthew G Knepley . count - The minium size
1028aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1029aa1993deSMatthew G Knepley 
1030aa1993deSMatthew G Knepley   Output Parameter:
1031aa1993deSMatthew G Knepley . array - the work array
1032aa1993deSMatthew G Knepley 
1033aa1993deSMatthew G Knepley   Level: developer
1034aa1993deSMatthew G Knepley 
1035aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1036aa1993deSMatthew G Knepley @*/
1037aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1038aa1993deSMatthew G Knepley {
1039aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1040aa1993deSMatthew G Knepley 
1041aa1993deSMatthew G Knepley   PetscFunctionBegin;
1042aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1043aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1044aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1045aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1046aa1993deSMatthew G Knepley       *p           = link->next;
1047aa1993deSMatthew G Knepley       link->next   = dm->workin;
1048aa1993deSMatthew G Knepley       dm->workin   = link;
10490298fd71SBarry Smith       *(void**)mem = NULL;
1050aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1051aa1993deSMatthew G Knepley     }
1052aa1993deSMatthew G Knepley   }
1053aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1054aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1055aa1993deSMatthew G Knepley }
1056e7c4fc90SDmitry Karpeev 
1057e7c4fc90SDmitry Karpeev #undef __FUNCT__
1058435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1059435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1060435a35e8SMatthew G Knepley {
1061435a35e8SMatthew G Knepley   PetscFunctionBegin;
1062435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
106382f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1064435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1065435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1066435a35e8SMatthew G Knepley }
1067435a35e8SMatthew G Knepley 
1068435a35e8SMatthew G Knepley #undef __FUNCT__
10694d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10704f3b5142SJed Brown /*@C
10714d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10724d343eeaSMatthew G Knepley 
10734d343eeaSMatthew G Knepley   Not collective
10744d343eeaSMatthew G Knepley 
10754d343eeaSMatthew G Knepley   Input Parameter:
10764d343eeaSMatthew G Knepley . dm - the DM object
10774d343eeaSMatthew G Knepley 
10784d343eeaSMatthew G Knepley   Output Parameters:
10790298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10800298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10810298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10824d343eeaSMatthew G Knepley 
10834d343eeaSMatthew G Knepley   Level: intermediate
10844d343eeaSMatthew G Knepley 
108521c9b008SJed Brown   Notes:
108621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
108721c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
108821c9b008SJed Brown   PetscFree().
108921c9b008SJed Brown 
10904d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10914d343eeaSMatthew G Knepley @*/
109237d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10934d343eeaSMatthew G Knepley {
109437d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10954d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10964d343eeaSMatthew G Knepley 
10974d343eeaSMatthew G Knepley   PetscFunctionBegin;
10984d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
109969ca1f37SDmitry Karpeev   if (numFields) {
110069ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
110169ca1f37SDmitry Karpeev     *numFields = 0;
110269ca1f37SDmitry Karpeev   }
110337d0c07bSMatthew G Knepley   if (fieldNames) {
110437d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11050298fd71SBarry Smith     *fieldNames = NULL;
110669ca1f37SDmitry Karpeev   }
110769ca1f37SDmitry Karpeev   if (fields) {
110869ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11090298fd71SBarry Smith     *fields = NULL;
111069ca1f37SDmitry Karpeev   }
111137d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
111237d0c07bSMatthew G Knepley   if (section) {
111337d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
111437d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
111537d0c07bSMatthew G Knepley 
111637d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
111737d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1118dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
111937d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
112037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
112137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
112237d0c07bSMatthew G Knepley     }
112337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
112437d0c07bSMatthew G Knepley       PetscInt gdof;
112537d0c07bSMatthew G Knepley 
112637d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112737d0c07bSMatthew G Knepley       if (gdof > 0) {
112837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
113037d0c07bSMatthew G Knepley 
113137d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
113237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
113337d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
113437d0c07bSMatthew G Knepley         }
113537d0c07bSMatthew G Knepley       }
113637d0c07bSMatthew G Knepley     }
113737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1138785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
113937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
114037d0c07bSMatthew G Knepley     }
114137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
114237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
114337d0c07bSMatthew G Knepley 
114437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114537d0c07bSMatthew G Knepley       if (gdof > 0) {
114637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
114737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
114937d0c07bSMatthew G Knepley 
115037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
115137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
115237d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
115337d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
115437d0c07bSMatthew G Knepley           }
115537d0c07bSMatthew G Knepley         }
115637d0c07bSMatthew G Knepley       }
115737d0c07bSMatthew G Knepley     }
11588865f1eaSKarl Rupp     if (numFields) *numFields = nF;
115937d0c07bSMatthew G Knepley     if (fieldNames) {
1160785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
116137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116237d0c07bSMatthew G Knepley         const char *fieldName;
116337d0c07bSMatthew G Knepley 
116437d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
116537d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
116637d0c07bSMatthew G Knepley       }
116737d0c07bSMatthew G Knepley     }
116837d0c07bSMatthew G Knepley     if (fields) {
1169785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
117037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
117182f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
117237d0c07bSMatthew G Knepley       }
117337d0c07bSMatthew G Knepley     }
117437d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11758865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11768865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
117769ca1f37SDmitry Karpeev   }
11784d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11794d343eeaSMatthew G Knepley }
11804d343eeaSMatthew G Knepley 
118116621825SDmitry Karpeev 
118216621825SDmitry Karpeev #undef __FUNCT__
118316621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
118416621825SDmitry Karpeev /*@C
118516621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
118616621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
118716621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1188e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1189e7c4fc90SDmitry Karpeev 
1190e7c4fc90SDmitry Karpeev   Not collective
1191e7c4fc90SDmitry Karpeev 
1192e7c4fc90SDmitry Karpeev   Input Parameter:
1193e7c4fc90SDmitry Karpeev . dm - the DM object
1194e7c4fc90SDmitry Karpeev 
1195e7c4fc90SDmitry Karpeev   Output Parameters:
11960298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
11970298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
11980298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
11990298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1200e7c4fc90SDmitry Karpeev 
1201e7c4fc90SDmitry Karpeev   Level: intermediate
1202e7c4fc90SDmitry Karpeev 
1203e7c4fc90SDmitry Karpeev   Notes:
1204e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1205e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1206e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1207e7c4fc90SDmitry Karpeev 
1208e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1209e7c4fc90SDmitry Karpeev @*/
121016621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1211e7c4fc90SDmitry Karpeev {
1212e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1213e7c4fc90SDmitry Karpeev 
1214e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1215e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12168865f1eaSKarl Rupp   if (len) {
12178865f1eaSKarl Rupp     PetscValidPointer(len,2);
12188865f1eaSKarl Rupp     *len = 0;
12198865f1eaSKarl Rupp   }
12208865f1eaSKarl Rupp   if (namelist) {
12218865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12228865f1eaSKarl Rupp     *namelist = 0;
12238865f1eaSKarl Rupp   }
12248865f1eaSKarl Rupp   if (islist) {
12258865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12268865f1eaSKarl Rupp     *islist = 0;
12278865f1eaSKarl Rupp   }
12288865f1eaSKarl Rupp   if (dmlist) {
12298865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12308865f1eaSKarl Rupp     *dmlist = 0;
12318865f1eaSKarl Rupp   }
1232f3f0edfdSDmitry Karpeev   /*
1233f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1234f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1235f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1236f3f0edfdSDmitry Karpeev    */
1237ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
123816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1239435a35e8SMatthew G Knepley     PetscSection section;
1240435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1241435a35e8SMatthew G Knepley 
1242435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1243435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1244435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1245435a35e8SMatthew G Knepley       *len = numFields;
124603dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
124703dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
124803dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1249435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1250435a35e8SMatthew G Knepley         const char *fieldName;
1251435a35e8SMatthew G Knepley 
125203dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
125303dc3394SMatthew G. Knepley         if (namelist) {
1254435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1255435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1256435a35e8SMatthew G Knepley         }
125703dc3394SMatthew G. Knepley       }
1258435a35e8SMatthew G Knepley     } else {
125969ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1260e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12610298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1262e7c4fc90SDmitry Karpeev     }
12638865f1eaSKarl Rupp   } else {
126416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
126516621825SDmitry Karpeev   }
126616621825SDmitry Karpeev   PetscFunctionReturn(0);
126716621825SDmitry Karpeev }
126816621825SDmitry Karpeev 
126916621825SDmitry Karpeev #undef __FUNCT__
1270435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1271435a35e8SMatthew G Knepley /*@C
1272435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1273435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1274435a35e8SMatthew G Knepley 
1275435a35e8SMatthew G Knepley   Not collective
1276435a35e8SMatthew G Knepley 
1277435a35e8SMatthew G Knepley   Input Parameters:
1278435a35e8SMatthew G Knepley + dm - the DM object
1279435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12800298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1281435a35e8SMatthew G Knepley 
1282435a35e8SMatthew G Knepley   Output Parameters:
1283435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1284435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1285435a35e8SMatthew G Knepley 
1286435a35e8SMatthew G Knepley   Level: intermediate
1287435a35e8SMatthew G Knepley 
1288435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1289435a35e8SMatthew G Knepley @*/
1290435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1291435a35e8SMatthew G Knepley {
1292435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1293435a35e8SMatthew G Knepley 
1294435a35e8SMatthew G Knepley   PetscFunctionBegin;
1295435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1296435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
12978865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
12988865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1299435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1300435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
130182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1302435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1303435a35e8SMatthew G Knepley }
1304435a35e8SMatthew G Knepley 
130516621825SDmitry Karpeev 
130616621825SDmitry Karpeev #undef __FUNCT__
130716621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
130816621825SDmitry Karpeev /*@C
13098d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13108d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13118d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13128d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13138d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
131416621825SDmitry Karpeev 
131516621825SDmitry Karpeev   Not collective
131616621825SDmitry Karpeev 
131716621825SDmitry Karpeev   Input Parameter:
131816621825SDmitry Karpeev . dm - the DM object
131916621825SDmitry Karpeev 
132016621825SDmitry Karpeev   Output Parameters:
13210298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13220298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13230298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13240298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13250298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
132616621825SDmitry Karpeev 
132716621825SDmitry Karpeev   Level: intermediate
132816621825SDmitry Karpeev 
132916621825SDmitry Karpeev   Notes:
133016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
133116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
133216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
133316621825SDmitry Karpeev 
13348d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
133516621825SDmitry Karpeev @*/
13368d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
133716621825SDmitry Karpeev {
133816621825SDmitry Karpeev   PetscErrorCode      ierr;
1339be081cd6SPeter Brune   DMSubDomainHookLink link;
1340be081cd6SPeter Brune   PetscInt            i,l;
134116621825SDmitry Karpeev 
134216621825SDmitry Karpeev   PetscFunctionBegin;
134316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134414a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13450298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13460298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13470298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13480298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1349f3f0edfdSDmitry Karpeev   /*
1350f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1351f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1352f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1353f3f0edfdSDmitry Karpeev    */
1354ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
135516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1356be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
135714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
135814a18fd3SPeter Brune     if (dmlist) {
1359be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1360be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1361be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1362be081cd6SPeter Brune         }
1363d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1364e7c4fc90SDmitry Karpeev       }
136514a18fd3SPeter Brune     }
136614a18fd3SPeter Brune     if (len) *len = l;
136714a18fd3SPeter Brune   }
1368e30e807fSPeter Brune   PetscFunctionReturn(0);
1369e30e807fSPeter Brune }
1370e30e807fSPeter Brune 
1371e30e807fSPeter Brune 
1372e30e807fSPeter Brune #undef __FUNCT__
1373e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1374e30e807fSPeter Brune /*@C
1375e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1376e30e807fSPeter Brune 
1377e30e807fSPeter Brune   Not collective
1378e30e807fSPeter Brune 
1379e30e807fSPeter Brune   Input Parameters:
1380e30e807fSPeter Brune + dm - the DM object
1381e30e807fSPeter Brune . n  - the number of subdomain scatters
1382e30e807fSPeter Brune - subdms - the local subdomains
1383e30e807fSPeter Brune 
1384e30e807fSPeter Brune   Output Parameters:
1385e30e807fSPeter Brune + n     - the number of scatters returned
1386e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1387e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1388e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1389e30e807fSPeter Brune 
1390e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1391e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1392e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1393e30e807fSPeter Brune   solution and residual data.
1394e30e807fSPeter Brune 
1395e30e807fSPeter Brune   Level: developer
1396e30e807fSPeter Brune 
1397e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1398e30e807fSPeter Brune @*/
1399e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1400e30e807fSPeter Brune {
1401e30e807fSPeter Brune   PetscErrorCode ierr;
1402e30e807fSPeter Brune 
1403e30e807fSPeter Brune   PetscFunctionBegin;
1404e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1405e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1406e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1407e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
140882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1409e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1410e7c4fc90SDmitry Karpeev }
1411e7c4fc90SDmitry Karpeev 
1412731c8d9eSDmitry Karpeev #undef __FUNCT__
141347c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
141447c6ae99SBarry Smith /*@
141547c6ae99SBarry Smith   DMRefine - Refines a DM object
141647c6ae99SBarry Smith 
141747c6ae99SBarry Smith   Collective on DM
141847c6ae99SBarry Smith 
141947c6ae99SBarry Smith   Input Parameter:
142047c6ae99SBarry Smith + dm   - the DM object
142191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
142247c6ae99SBarry Smith 
142347c6ae99SBarry Smith   Output Parameter:
14240298fd71SBarry Smith . dmf - the refined DM, or NULL
1425ae0a1c52SMatthew G Knepley 
14260298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
142747c6ae99SBarry Smith 
142847c6ae99SBarry Smith   Level: developer
142947c6ae99SBarry Smith 
1430e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
143147c6ae99SBarry Smith @*/
14327087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
143347c6ae99SBarry Smith {
143447c6ae99SBarry Smith   PetscErrorCode   ierr;
1435c833c3b5SJed Brown   DMRefineHookLink link;
143647c6ae99SBarry Smith 
143747c6ae99SBarry Smith   PetscFunctionBegin;
1438732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14404057135bSMatthew G Knepley   if (*dmf) {
144143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14428865f1eaSKarl Rupp 
14438cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14448865f1eaSKarl Rupp 
1445644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14460598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1447656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14488865f1eaSKarl Rupp 
1449e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1450c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14518865f1eaSKarl Rupp       if (link->refinehook) {
14528865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14538865f1eaSKarl Rupp       }
1454c833c3b5SJed Brown     }
1455c833c3b5SJed Brown   }
1456c833c3b5SJed Brown   PetscFunctionReturn(0);
1457c833c3b5SJed Brown }
1458c833c3b5SJed Brown 
1459c833c3b5SJed Brown #undef __FUNCT__
1460c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1461bb9467b5SJed Brown /*@C
1462c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1463c833c3b5SJed Brown 
1464c833c3b5SJed Brown    Logically Collective
1465c833c3b5SJed Brown 
1466c833c3b5SJed Brown    Input Arguments:
1467c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1468c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1469c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14700298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1471c833c3b5SJed Brown 
1472c833c3b5SJed Brown    Calling sequence of refinehook:
1473c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1474c833c3b5SJed Brown 
1475c833c3b5SJed Brown +  coarse - coarse level DM
1476c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1477c833c3b5SJed Brown -  ctx - optional user-defined function context
1478c833c3b5SJed Brown 
1479c833c3b5SJed Brown    Calling sequence for interphook:
1480c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1481c833c3b5SJed Brown 
1482c833c3b5SJed Brown +  coarse - coarse level DM
1483c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1484c833c3b5SJed Brown .  fine - fine level DM to update
1485c833c3b5SJed Brown -  ctx - optional user-defined function context
1486c833c3b5SJed Brown 
1487c833c3b5SJed Brown    Level: advanced
1488c833c3b5SJed Brown 
1489c833c3b5SJed Brown    Notes:
1490c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1491c833c3b5SJed Brown 
1492c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1493c833c3b5SJed Brown 
1494bb9467b5SJed Brown    This function is currently not available from Fortran.
1495bb9467b5SJed Brown 
1496c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1497c833c3b5SJed Brown @*/
1498c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1499c833c3b5SJed Brown {
1500c833c3b5SJed Brown   PetscErrorCode   ierr;
1501c833c3b5SJed Brown   DMRefineHookLink link,*p;
1502c833c3b5SJed Brown 
1503c833c3b5SJed Brown   PetscFunctionBegin;
1504c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1505c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1506c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1507c833c3b5SJed Brown   link->refinehook = refinehook;
1508c833c3b5SJed Brown   link->interphook = interphook;
1509c833c3b5SJed Brown   link->ctx        = ctx;
15100298fd71SBarry Smith   link->next       = NULL;
1511c833c3b5SJed Brown   *p               = link;
1512c833c3b5SJed Brown   PetscFunctionReturn(0);
1513c833c3b5SJed Brown }
1514c833c3b5SJed Brown 
1515c833c3b5SJed Brown #undef __FUNCT__
1516c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1517c833c3b5SJed Brown /*@
1518c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1519c833c3b5SJed Brown 
1520c833c3b5SJed Brown    Collective if any hooks are
1521c833c3b5SJed Brown 
1522c833c3b5SJed Brown    Input Arguments:
1523c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1524c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1525c833c3b5SJed Brown -  fine - finer DM to update
1526c833c3b5SJed Brown 
1527c833c3b5SJed Brown    Level: developer
1528c833c3b5SJed Brown 
1529c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1530c833c3b5SJed Brown @*/
1531c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1532c833c3b5SJed Brown {
1533c833c3b5SJed Brown   PetscErrorCode   ierr;
1534c833c3b5SJed Brown   DMRefineHookLink link;
1535c833c3b5SJed Brown 
1536c833c3b5SJed Brown   PetscFunctionBegin;
1537c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15388865f1eaSKarl Rupp     if (link->interphook) {
15398865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15408865f1eaSKarl Rupp     }
15414057135bSMatthew G Knepley   }
154247c6ae99SBarry Smith   PetscFunctionReturn(0);
154347c6ae99SBarry Smith }
154447c6ae99SBarry Smith 
154547c6ae99SBarry Smith #undef __FUNCT__
1546eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1547eb3f98d2SBarry Smith /*@
1548eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1549eb3f98d2SBarry Smith 
1550eb3f98d2SBarry Smith     Not Collective
1551eb3f98d2SBarry Smith 
1552eb3f98d2SBarry Smith     Input Parameter:
1553eb3f98d2SBarry Smith .   dm - the DM object
1554eb3f98d2SBarry Smith 
1555eb3f98d2SBarry Smith     Output Parameter:
1556eb3f98d2SBarry Smith .   level - number of refinements
1557eb3f98d2SBarry Smith 
1558eb3f98d2SBarry Smith     Level: developer
1559eb3f98d2SBarry Smith 
15606a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1561eb3f98d2SBarry Smith 
1562eb3f98d2SBarry Smith @*/
1563eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1564eb3f98d2SBarry Smith {
1565eb3f98d2SBarry Smith   PetscFunctionBegin;
1566eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1567eb3f98d2SBarry Smith   *level = dm->levelup;
1568eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1569eb3f98d2SBarry Smith }
1570eb3f98d2SBarry Smith 
1571eb3f98d2SBarry Smith #undef __FUNCT__
1572baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1573bb9467b5SJed Brown /*@C
1574baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1575baf369e7SPeter Brune 
1576baf369e7SPeter Brune    Logically Collective
1577baf369e7SPeter Brune 
1578baf369e7SPeter Brune    Input Arguments:
1579baf369e7SPeter Brune +  dm - the DM
1580baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1581baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15820298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1583baf369e7SPeter Brune 
1584baf369e7SPeter Brune    Calling sequence for beginhook:
1585baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1586baf369e7SPeter Brune 
1587baf369e7SPeter Brune +  dm - global DM
1588baf369e7SPeter Brune .  g - global vector
1589baf369e7SPeter Brune .  mode - mode
1590baf369e7SPeter Brune .  l - local vector
1591baf369e7SPeter Brune -  ctx - optional user-defined function context
1592baf369e7SPeter Brune 
1593baf369e7SPeter Brune 
1594baf369e7SPeter Brune    Calling sequence for endhook:
1595ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1596baf369e7SPeter Brune 
1597baf369e7SPeter Brune +  global - global DM
1598baf369e7SPeter Brune -  ctx - optional user-defined function context
1599baf369e7SPeter Brune 
1600baf369e7SPeter Brune    Level: advanced
1601baf369e7SPeter Brune 
1602baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1603baf369e7SPeter Brune @*/
1604baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1605baf369e7SPeter Brune {
1606baf369e7SPeter Brune   PetscErrorCode          ierr;
1607baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1608baf369e7SPeter Brune 
1609baf369e7SPeter Brune   PetscFunctionBegin;
1610baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1611baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1612baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1613baf369e7SPeter Brune   link->beginhook = beginhook;
1614baf369e7SPeter Brune   link->endhook   = endhook;
1615baf369e7SPeter Brune   link->ctx       = ctx;
16160298fd71SBarry Smith   link->next      = NULL;
1617baf369e7SPeter Brune   *p              = link;
1618baf369e7SPeter Brune   PetscFunctionReturn(0);
1619baf369e7SPeter Brune }
1620baf369e7SPeter Brune 
1621baf369e7SPeter Brune #undef __FUNCT__
1622*4c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
1623*4c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
1624*4c274da1SToby Isaac {
1625*4c274da1SToby Isaac   Mat cMat;
1626*4c274da1SToby Isaac   Vec cVec;
1627*4c274da1SToby Isaac   PetscSection section, cSec;
1628*4c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
1629*4c274da1SToby Isaac   PetscErrorCode ierr;
1630*4c274da1SToby Isaac 
1631*4c274da1SToby Isaac   PetscFunctionBegin;
1632*4c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1633*4c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
1634*4c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
1635*4c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1636*4c274da1SToby Isaac     ierr = MatGetVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
1637*4c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
1638*4c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
1639*4c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
1640*4c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
1641*4c274da1SToby Isaac       if (dof) {
1642*4c274da1SToby Isaac         PetscScalar *vals;
1643*4c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
1644*4c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
1645*4c274da1SToby Isaac       }
1646*4c274da1SToby Isaac     }
1647*4c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
1648*4c274da1SToby Isaac   }
1649*4c274da1SToby Isaac   PetscFunctionReturn(0);
1650*4c274da1SToby Isaac }
1651*4c274da1SToby Isaac 
1652*4c274da1SToby Isaac #undef __FUNCT__
165347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
165447c6ae99SBarry Smith /*@
165547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
165647c6ae99SBarry Smith 
165747c6ae99SBarry Smith     Neighbor-wise Collective on DM
165847c6ae99SBarry Smith 
165947c6ae99SBarry Smith     Input Parameters:
166047c6ae99SBarry Smith +   dm - the DM object
166147c6ae99SBarry Smith .   g - the global vector
166247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
166347c6ae99SBarry Smith -   l - the local vector
166447c6ae99SBarry Smith 
166547c6ae99SBarry Smith 
166647c6ae99SBarry Smith     Level: beginner
166747c6ae99SBarry Smith 
1668e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
166947c6ae99SBarry Smith 
167047c6ae99SBarry Smith @*/
16717087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
167247c6ae99SBarry Smith {
16737128ae9fSMatthew G Knepley   PetscSF                 sf;
167447c6ae99SBarry Smith   PetscErrorCode          ierr;
1675baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
167647c6ae99SBarry Smith 
167747c6ae99SBarry Smith   PetscFunctionBegin;
1678171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1679baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16808865f1eaSKarl Rupp     if (link->beginhook) {
16818865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16828865f1eaSKarl Rupp     }
1683baf369e7SPeter Brune   }
16847128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16857128ae9fSMatthew G Knepley   if (sf) {
16867128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16877128ae9fSMatthew G Knepley 
168882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16897128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16907128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16917128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16927128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16937128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16947128ae9fSMatthew G Knepley   } else {
1695843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16967128ae9fSMatthew G Knepley   }
169747c6ae99SBarry Smith   PetscFunctionReturn(0);
169847c6ae99SBarry Smith }
169947c6ae99SBarry Smith 
170047c6ae99SBarry Smith #undef __FUNCT__
170147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
170247c6ae99SBarry Smith /*@
170347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
170447c6ae99SBarry Smith 
170547c6ae99SBarry Smith     Neighbor-wise Collective on DM
170647c6ae99SBarry Smith 
170747c6ae99SBarry Smith     Input Parameters:
170847c6ae99SBarry Smith +   dm - the DM object
170947c6ae99SBarry Smith .   g - the global vector
171047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
171147c6ae99SBarry Smith -   l - the local vector
171247c6ae99SBarry Smith 
171347c6ae99SBarry Smith 
171447c6ae99SBarry Smith     Level: beginner
171547c6ae99SBarry Smith 
1716e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
171747c6ae99SBarry Smith 
171847c6ae99SBarry Smith @*/
17197087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
172047c6ae99SBarry Smith {
17217128ae9fSMatthew G Knepley   PetscSF                 sf;
172247c6ae99SBarry Smith   PetscErrorCode          ierr;
172361a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1724baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
172547c6ae99SBarry Smith 
172647c6ae99SBarry Smith   PetscFunctionBegin;
1727171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17287128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17297128ae9fSMatthew G Knepley   if (sf) {
173082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17317128ae9fSMatthew G Knepley 
17327128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17337128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17347128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17357128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17367128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17377128ae9fSMatthew G Knepley   } else {
1738843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17397128ae9fSMatthew G Knepley   }
1740*4c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1741baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1742baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1743baf369e7SPeter Brune   }
174447c6ae99SBarry Smith   PetscFunctionReturn(0);
174547c6ae99SBarry Smith }
174647c6ae99SBarry Smith 
174747c6ae99SBarry Smith #undef __FUNCT__
1748d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1749d4d07f1eSToby Isaac /*@C
1750d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1751d4d07f1eSToby Isaac 
1752d4d07f1eSToby Isaac    Logically Collective
1753d4d07f1eSToby Isaac 
1754d4d07f1eSToby Isaac    Input Arguments:
1755d4d07f1eSToby Isaac +  dm - the DM
1756d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1757d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1758d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1759d4d07f1eSToby Isaac 
1760d4d07f1eSToby Isaac    Calling sequence for beginhook:
1761d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1762d4d07f1eSToby Isaac 
1763d4d07f1eSToby Isaac +  dm - global DM
1764d4d07f1eSToby Isaac .  l - local vector
1765d4d07f1eSToby Isaac .  mode - mode
1766d4d07f1eSToby Isaac .  g - global vector
1767d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1768d4d07f1eSToby Isaac 
1769d4d07f1eSToby Isaac 
1770d4d07f1eSToby Isaac    Calling sequence for endhook:
1771d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1772d4d07f1eSToby Isaac 
1773d4d07f1eSToby Isaac +  global - global DM
1774d4d07f1eSToby Isaac .  l - local vector
1775d4d07f1eSToby Isaac .  mode - mode
1776d4d07f1eSToby Isaac .  g - global vector
1777d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1778d4d07f1eSToby Isaac 
1779d4d07f1eSToby Isaac    Level: advanced
1780d4d07f1eSToby Isaac 
1781d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1782d4d07f1eSToby Isaac @*/
1783d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1784d4d07f1eSToby Isaac {
1785d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1786d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1787d4d07f1eSToby Isaac 
1788d4d07f1eSToby Isaac   PetscFunctionBegin;
1789d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1790d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1791d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1792d4d07f1eSToby Isaac   link->beginhook = beginhook;
1793d4d07f1eSToby Isaac   link->endhook   = endhook;
1794d4d07f1eSToby Isaac   link->ctx       = ctx;
1795d4d07f1eSToby Isaac   link->next      = NULL;
1796d4d07f1eSToby Isaac   *p              = link;
1797d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1798d4d07f1eSToby Isaac }
1799d4d07f1eSToby Isaac 
1800d4d07f1eSToby Isaac #undef __FUNCT__
1801*4c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
1802*4c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
1803*4c274da1SToby Isaac {
1804*4c274da1SToby Isaac   Mat cMat;
1805*4c274da1SToby Isaac   Vec cVec;
1806*4c274da1SToby Isaac   PetscSection section, cSec;
1807*4c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
1808*4c274da1SToby Isaac   PetscErrorCode ierr;
1809*4c274da1SToby Isaac 
1810*4c274da1SToby Isaac   PetscFunctionBegin;
1811*4c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1812*4c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
1813*4c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
1814*4c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1815*4c274da1SToby Isaac     ierr = MatGetVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
1816*4c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
1817*4c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
1818*4c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
1819*4c274da1SToby Isaac       if (dof) {
1820*4c274da1SToby Isaac         PetscInt d;
1821*4c274da1SToby Isaac         PetscScalar *vals;
1822*4c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
1823*4c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
1824*4c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
1825*4c274da1SToby Isaac          * we just extracted */
1826*4c274da1SToby Isaac         for (d = 0; d < dof; d++) {
1827*4c274da1SToby Isaac           vals[d] = 0.;
1828*4c274da1SToby Isaac         }
1829*4c274da1SToby Isaac       }
1830*4c274da1SToby Isaac     }
1831*4c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
1832*4c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
1833*4c274da1SToby Isaac   }
1834*4c274da1SToby Isaac   PetscFunctionReturn(0);
1835*4c274da1SToby Isaac }
1836*4c274da1SToby Isaac 
1837*4c274da1SToby Isaac #undef __FUNCT__
18389a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
183947c6ae99SBarry Smith /*@
18409a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
18419a42bb27SBarry Smith 
18429a42bb27SBarry Smith     Neighbor-wise Collective on DM
18439a42bb27SBarry Smith 
18449a42bb27SBarry Smith     Input Parameters:
18459a42bb27SBarry Smith +   dm - the DM object
1846f6813fd5SJed Brown .   l - the local vector
18479a42bb27SBarry 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
18489a42bb27SBarry Smith            base point.
1849f6813fd5SJed Brown - - the global vector
18509a42bb27SBarry Smith 
18519a42bb27SBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation. If you would like to simply add the non-ghosted values in the local
18529a42bb27SBarry Smith            array into the global array you need to either (1) zero the ghosted locations and use ADD_VALUES or (2) use INSERT_VALUES into a work global array and then add the work
18539a42bb27SBarry Smith            global array to the final global array with VecAXPY().
18549a42bb27SBarry Smith 
18559a42bb27SBarry Smith     Level: beginner
18569a42bb27SBarry Smith 
1857e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
18589a42bb27SBarry Smith 
18599a42bb27SBarry Smith @*/
18607087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
18619a42bb27SBarry Smith {
18627128ae9fSMatthew G Knepley   PetscSF                 sf;
18639a42bb27SBarry Smith   PetscErrorCode          ierr;
1864d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
18659a42bb27SBarry Smith 
18669a42bb27SBarry Smith   PetscFunctionBegin;
1867171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1868d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1869d4d07f1eSToby Isaac     if (link->beginhook) {
1870d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
1871d4d07f1eSToby Isaac     }
1872d4d07f1eSToby Isaac   }
1873*4c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
18747128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18757128ae9fSMatthew G Knepley   if (sf) {
18767128ae9fSMatthew G Knepley     MPI_Op      op;
18777128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
18787128ae9fSMatthew G Knepley 
18797128ae9fSMatthew G Knepley     switch (mode) {
18807128ae9fSMatthew G Knepley     case INSERT_VALUES:
18817128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18828bfbc91cSJed Brown       op = MPIU_REPLACE; break;
18837128ae9fSMatthew G Knepley     case ADD_VALUES:
18847128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18857128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18867128ae9fSMatthew G Knepley     default:
188782f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18887128ae9fSMatthew G Knepley     }
18897128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18907128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18917128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18927128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18937128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18947128ae9fSMatthew G Knepley   } else {
1895843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18967128ae9fSMatthew G Knepley   }
18979a42bb27SBarry Smith   PetscFunctionReturn(0);
18989a42bb27SBarry Smith }
18999a42bb27SBarry Smith 
19009a42bb27SBarry Smith #undef __FUNCT__
19019a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
19029a42bb27SBarry Smith /*@
19039a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
190447c6ae99SBarry Smith 
190547c6ae99SBarry Smith     Neighbor-wise Collective on DM
190647c6ae99SBarry Smith 
190747c6ae99SBarry Smith     Input Parameters:
190847c6ae99SBarry Smith +   dm - the DM object
1909f6813fd5SJed Brown .   l - the local vector
191047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1911f6813fd5SJed Brown -   g - the global vector
191247c6ae99SBarry Smith 
191347c6ae99SBarry Smith 
191447c6ae99SBarry Smith     Level: beginner
191547c6ae99SBarry Smith 
1916e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
191747c6ae99SBarry Smith 
191847c6ae99SBarry Smith @*/
19197087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
192047c6ae99SBarry Smith {
19217128ae9fSMatthew G Knepley   PetscSF        sf;
192247c6ae99SBarry Smith   PetscErrorCode ierr;
1923d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
192447c6ae99SBarry Smith 
192547c6ae99SBarry Smith   PetscFunctionBegin;
1926171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19277128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19287128ae9fSMatthew G Knepley   if (sf) {
19297128ae9fSMatthew G Knepley     MPI_Op      op;
19307128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
19317128ae9fSMatthew G Knepley 
19327128ae9fSMatthew G Knepley     switch (mode) {
19337128ae9fSMatthew G Knepley     case INSERT_VALUES:
19347128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
19358bfbc91cSJed Brown       op = MPIU_REPLACE; break;
19367128ae9fSMatthew G Knepley     case ADD_VALUES:
19377128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
19387128ae9fSMatthew G Knepley       op = MPI_SUM; break;
19397128ae9fSMatthew G Knepley     default:
194082f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19417128ae9fSMatthew G Knepley     }
19427128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
19437128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
19447128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
19457128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
19467128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
19477128ae9fSMatthew G Knepley   } else {
1948843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
19497128ae9fSMatthew G Knepley   }
1950d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1951d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1952d4d07f1eSToby Isaac   }
195347c6ae99SBarry Smith   PetscFunctionReturn(0);
195447c6ae99SBarry Smith }
195547c6ae99SBarry Smith 
195647c6ae99SBarry Smith #undef __FUNCT__
1957f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
1958f089877aSRichard Tran Mills /*@
1959bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
1960bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1961d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
1962f089877aSRichard Tran Mills 
1963bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1964f089877aSRichard Tran Mills 
1965f089877aSRichard Tran Mills    Input Parameters:
1966f089877aSRichard Tran Mills +  dm - the DM object
1967bc0a1609SRichard Tran Mills .  g - the original local vector
1968bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1969f089877aSRichard Tran Mills 
1970bc0a1609SRichard Tran Mills    Output Parameter:
1971bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1972f089877aSRichard Tran Mills 
1973f089877aSRichard Tran Mills    Level: intermediate
1974f089877aSRichard Tran Mills 
1975bc0a1609SRichard Tran Mills    Notes:
1976bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1977bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1978bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1979bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1980bc0a1609SRichard Tran Mills 
1981bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
1982bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1983f089877aSRichard Tran Mills 
1984f089877aSRichard Tran Mills @*/
1985f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
1986f089877aSRichard Tran Mills {
1987f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1988f089877aSRichard Tran Mills 
1989f089877aSRichard Tran Mills   PetscFunctionBegin;
1990f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1991f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1992f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1993f089877aSRichard Tran Mills }
1994f089877aSRichard Tran Mills 
1995f089877aSRichard Tran Mills #undef __FUNCT__
1996f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
1997f089877aSRichard Tran Mills /*@
1998bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
1999bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2000d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2001f089877aSRichard Tran Mills 
2002bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2003f089877aSRichard Tran Mills 
2004f089877aSRichard Tran Mills    Input Parameters:
2005bc0a1609SRichard Tran Mills +  da - the DM object
2006bc0a1609SRichard Tran Mills .  g - the original local vector
2007bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2008f089877aSRichard Tran Mills 
2009bc0a1609SRichard Tran Mills    Output Parameter:
2010bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2011f089877aSRichard Tran Mills 
2012f089877aSRichard Tran Mills    Level: intermediate
2013f089877aSRichard Tran Mills 
2014bc0a1609SRichard Tran Mills    Notes:
2015bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2016bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2017bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2018bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2019bc0a1609SRichard Tran Mills 
2020bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2021bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2022f089877aSRichard Tran Mills 
2023f089877aSRichard Tran Mills @*/
2024f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2025f089877aSRichard Tran Mills {
2026f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2027f089877aSRichard Tran Mills 
2028f089877aSRichard Tran Mills   PetscFunctionBegin;
2029f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2030f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2031f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2032f089877aSRichard Tran Mills }
2033f089877aSRichard Tran Mills 
2034f089877aSRichard Tran Mills 
2035f089877aSRichard Tran Mills #undef __FUNCT__
203647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
203747c6ae99SBarry Smith /*@
203847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith     Collective on DM
204147c6ae99SBarry Smith 
204247c6ae99SBarry Smith     Input Parameter:
204347c6ae99SBarry Smith +   dm - the DM object
204491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
204547c6ae99SBarry Smith 
204647c6ae99SBarry Smith     Output Parameter:
204747c6ae99SBarry Smith .   dmc - the coarsened DM
204847c6ae99SBarry Smith 
204947c6ae99SBarry Smith     Level: developer
205047c6ae99SBarry Smith 
2051e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
205247c6ae99SBarry Smith 
205347c6ae99SBarry Smith @*/
20547087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
205547c6ae99SBarry Smith {
205647c6ae99SBarry Smith   PetscErrorCode    ierr;
2057b17ce1afSJed Brown   DMCoarsenHookLink link;
205847c6ae99SBarry Smith 
205947c6ae99SBarry Smith   PetscFunctionBegin;
2060171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
206147c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
206243842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
20638cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2064644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
20650598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2066656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2067e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2068b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2069b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2070b17ce1afSJed Brown   }
2071b17ce1afSJed Brown   PetscFunctionReturn(0);
2072b17ce1afSJed Brown }
2073b17ce1afSJed Brown 
2074b17ce1afSJed Brown #undef __FUNCT__
2075b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2076bb9467b5SJed Brown /*@C
2077b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2078b17ce1afSJed Brown 
2079b17ce1afSJed Brown    Logically Collective
2080b17ce1afSJed Brown 
2081b17ce1afSJed Brown    Input Arguments:
2082b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2083b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2084b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
20850298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2086b17ce1afSJed Brown 
2087b17ce1afSJed Brown    Calling sequence of coarsenhook:
2088b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2089b17ce1afSJed Brown 
2090b17ce1afSJed Brown +  fine - fine level DM
2091b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2092b17ce1afSJed Brown -  ctx - optional user-defined function context
2093b17ce1afSJed Brown 
2094b17ce1afSJed Brown    Calling sequence for restricthook:
2095c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2096b17ce1afSJed Brown 
2097b17ce1afSJed Brown +  fine - fine level DM
2098b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2099c833c3b5SJed Brown .  rscale - scaling vector for restriction
2100c833c3b5SJed Brown .  inject - matrix restricting by injection
2101b17ce1afSJed Brown .  coarse - coarse level DM to update
2102b17ce1afSJed Brown -  ctx - optional user-defined function context
2103b17ce1afSJed Brown 
2104b17ce1afSJed Brown    Level: advanced
2105b17ce1afSJed Brown 
2106b17ce1afSJed Brown    Notes:
2107b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2108b17ce1afSJed Brown 
2109b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2110b17ce1afSJed Brown 
2111b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2112b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2113b17ce1afSJed Brown 
2114bb9467b5SJed Brown    This function is currently not available from Fortran.
2115bb9467b5SJed Brown 
2116c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2117b17ce1afSJed Brown @*/
2118b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2119b17ce1afSJed Brown {
2120b17ce1afSJed Brown   PetscErrorCode    ierr;
2121b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2122b17ce1afSJed Brown 
2123b17ce1afSJed Brown   PetscFunctionBegin;
2124b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
21256bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2126b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2127b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2128b17ce1afSJed Brown   link->restricthook = restricthook;
2129b17ce1afSJed Brown   link->ctx          = ctx;
21300298fd71SBarry Smith   link->next         = NULL;
2131b17ce1afSJed Brown   *p                 = link;
2132b17ce1afSJed Brown   PetscFunctionReturn(0);
2133b17ce1afSJed Brown }
2134b17ce1afSJed Brown 
2135b17ce1afSJed Brown #undef __FUNCT__
2136b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2137b17ce1afSJed Brown /*@
2138b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2139b17ce1afSJed Brown 
2140b17ce1afSJed Brown    Collective if any hooks are
2141b17ce1afSJed Brown 
2142b17ce1afSJed Brown    Input Arguments:
2143b17ce1afSJed Brown +  fine - finer DM to use as a base
2144b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2145b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2146b17ce1afSJed Brown -  coarse - coarer DM to update
2147b17ce1afSJed Brown 
2148b17ce1afSJed Brown    Level: developer
2149b17ce1afSJed Brown 
2150b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2151b17ce1afSJed Brown @*/
2152b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2153b17ce1afSJed Brown {
2154b17ce1afSJed Brown   PetscErrorCode    ierr;
2155b17ce1afSJed Brown   DMCoarsenHookLink link;
2156b17ce1afSJed Brown 
2157b17ce1afSJed Brown   PetscFunctionBegin;
2158b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
21598865f1eaSKarl Rupp     if (link->restricthook) {
21608865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
21618865f1eaSKarl Rupp     }
2162b17ce1afSJed Brown   }
216347c6ae99SBarry Smith   PetscFunctionReturn(0);
216447c6ae99SBarry Smith }
216547c6ae99SBarry Smith 
216647c6ae99SBarry Smith #undef __FUNCT__
2167be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2168bb9467b5SJed Brown /*@C
2169be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
21705dbd56e3SPeter Brune 
21715dbd56e3SPeter Brune    Logically Collective
21725dbd56e3SPeter Brune 
21735dbd56e3SPeter Brune    Input Arguments:
21745dbd56e3SPeter Brune +  global - global DM
2175ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
21765dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
21770298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
21785dbd56e3SPeter Brune 
2179ec4806b8SPeter Brune 
2180ec4806b8SPeter Brune    Calling sequence for ddhook:
2181ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2182ec4806b8SPeter Brune 
2183ec4806b8SPeter Brune +  global - global DM
2184ec4806b8SPeter Brune .  block  - block DM
2185ec4806b8SPeter Brune -  ctx - optional user-defined function context
2186ec4806b8SPeter Brune 
21875dbd56e3SPeter Brune    Calling sequence for restricthook:
2188ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
21895dbd56e3SPeter Brune 
21905dbd56e3SPeter Brune +  global - global DM
21915dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
21925dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2193ec4806b8SPeter Brune .  block  - block DM
21945dbd56e3SPeter Brune -  ctx - optional user-defined function context
21955dbd56e3SPeter Brune 
21965dbd56e3SPeter Brune    Level: advanced
21975dbd56e3SPeter Brune 
21985dbd56e3SPeter Brune    Notes:
2199ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
22005dbd56e3SPeter Brune 
22015dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
22025dbd56e3SPeter Brune 
22035dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2204ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
22055dbd56e3SPeter Brune 
2206bb9467b5SJed Brown    This function is currently not available from Fortran.
2207bb9467b5SJed Brown 
22085dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22095dbd56e3SPeter Brune @*/
2210be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
22115dbd56e3SPeter Brune {
22125dbd56e3SPeter Brune   PetscErrorCode      ierr;
2213be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
22145dbd56e3SPeter Brune 
22155dbd56e3SPeter Brune   PetscFunctionBegin;
22165dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2217be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2218be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
22195dbd56e3SPeter Brune   link->restricthook = restricthook;
2220be081cd6SPeter Brune   link->ddhook       = ddhook;
22215dbd56e3SPeter Brune   link->ctx          = ctx;
22220298fd71SBarry Smith   link->next         = NULL;
22235dbd56e3SPeter Brune   *p                 = link;
22245dbd56e3SPeter Brune   PetscFunctionReturn(0);
22255dbd56e3SPeter Brune }
22265dbd56e3SPeter Brune 
22275dbd56e3SPeter Brune #undef __FUNCT__
2228be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
22295dbd56e3SPeter Brune /*@
2230be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
22315dbd56e3SPeter Brune 
22325dbd56e3SPeter Brune    Collective if any hooks are
22335dbd56e3SPeter Brune 
22345dbd56e3SPeter Brune    Input Arguments:
22355dbd56e3SPeter Brune +  fine - finer DM to use as a base
2236be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2237be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
22385dbd56e3SPeter Brune -  coarse - coarer DM to update
22395dbd56e3SPeter Brune 
22405dbd56e3SPeter Brune    Level: developer
22415dbd56e3SPeter Brune 
22425dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
22435dbd56e3SPeter Brune @*/
2244be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
22455dbd56e3SPeter Brune {
22465dbd56e3SPeter Brune   PetscErrorCode      ierr;
2247be081cd6SPeter Brune   DMSubDomainHookLink link;
22485dbd56e3SPeter Brune 
22495dbd56e3SPeter Brune   PetscFunctionBegin;
2250be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
22518865f1eaSKarl Rupp     if (link->restricthook) {
22528865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
22538865f1eaSKarl Rupp     }
22545dbd56e3SPeter Brune   }
22555dbd56e3SPeter Brune   PetscFunctionReturn(0);
22565dbd56e3SPeter Brune }
22575dbd56e3SPeter Brune 
22585dbd56e3SPeter Brune #undef __FUNCT__
22595fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
22605fe1f584SPeter Brune /*@
22616a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
22625fe1f584SPeter Brune 
22635fe1f584SPeter Brune     Not Collective
22645fe1f584SPeter Brune 
22655fe1f584SPeter Brune     Input Parameter:
22665fe1f584SPeter Brune .   dm - the DM object
22675fe1f584SPeter Brune 
22685fe1f584SPeter Brune     Output Parameter:
22696a7d9d85SPeter Brune .   level - number of coarsenings
22705fe1f584SPeter Brune 
22715fe1f584SPeter Brune     Level: developer
22725fe1f584SPeter Brune 
22736a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
22745fe1f584SPeter Brune 
22755fe1f584SPeter Brune @*/
22765fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
22775fe1f584SPeter Brune {
22785fe1f584SPeter Brune   PetscFunctionBegin;
22795fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22805fe1f584SPeter Brune   *level = dm->leveldown;
22815fe1f584SPeter Brune   PetscFunctionReturn(0);
22825fe1f584SPeter Brune }
22835fe1f584SPeter Brune 
22845fe1f584SPeter Brune 
22855fe1f584SPeter Brune 
22865fe1f584SPeter Brune #undef __FUNCT__
228747c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
228847c6ae99SBarry Smith /*@C
228947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
229047c6ae99SBarry Smith 
229147c6ae99SBarry Smith     Collective on DM
229247c6ae99SBarry Smith 
229347c6ae99SBarry Smith     Input Parameter:
229447c6ae99SBarry Smith +   dm - the DM object
229547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
229647c6ae99SBarry Smith 
229747c6ae99SBarry Smith     Output Parameter:
229847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
229947c6ae99SBarry Smith 
230047c6ae99SBarry Smith     Level: developer
230147c6ae99SBarry Smith 
2302e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
230347c6ae99SBarry Smith 
230447c6ae99SBarry Smith @*/
23057087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
230647c6ae99SBarry Smith {
230747c6ae99SBarry Smith   PetscErrorCode ierr;
230847c6ae99SBarry Smith 
230947c6ae99SBarry Smith   PetscFunctionBegin;
2310171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2311ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
231247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
231347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
231447c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
231547c6ae99SBarry Smith   } else if (dm->ops->refine) {
231647c6ae99SBarry Smith     PetscInt i;
231747c6ae99SBarry Smith 
2318ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
231947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2320ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
232147c6ae99SBarry Smith     }
2322ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
232347c6ae99SBarry Smith   PetscFunctionReturn(0);
232447c6ae99SBarry Smith }
232547c6ae99SBarry Smith 
232647c6ae99SBarry Smith #undef __FUNCT__
232747c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
232847c6ae99SBarry Smith /*@C
232947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
233047c6ae99SBarry Smith 
233147c6ae99SBarry Smith     Collective on DM
233247c6ae99SBarry Smith 
233347c6ae99SBarry Smith     Input Parameter:
233447c6ae99SBarry Smith +   dm - the DM object
233547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
233647c6ae99SBarry Smith 
233747c6ae99SBarry Smith     Output Parameter:
233847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
233947c6ae99SBarry Smith 
234047c6ae99SBarry Smith     Level: developer
234147c6ae99SBarry Smith 
2342e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
234347c6ae99SBarry Smith 
234447c6ae99SBarry Smith @*/
23457087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
234647c6ae99SBarry Smith {
234747c6ae99SBarry Smith   PetscErrorCode ierr;
234847c6ae99SBarry Smith 
234947c6ae99SBarry Smith   PetscFunctionBegin;
2350171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2351ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
235247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
235347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
235447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
235547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
235647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
235747c6ae99SBarry Smith     PetscInt i;
235847c6ae99SBarry Smith 
2359ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
236047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2361ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
236247c6ae99SBarry Smith     }
2363ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
236447c6ae99SBarry Smith   PetscFunctionReturn(0);
236547c6ae99SBarry Smith }
236647c6ae99SBarry Smith 
236747c6ae99SBarry Smith #undef __FUNCT__
2368e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
236947c6ae99SBarry Smith /*@
2370e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
237147c6ae99SBarry Smith    grids associated with two DMs.
237247c6ae99SBarry Smith 
237347c6ae99SBarry Smith    Collective on DM
237447c6ae99SBarry Smith 
237547c6ae99SBarry Smith    Input Parameters:
237647c6ae99SBarry Smith +  dmc - the coarse grid DM
237747c6ae99SBarry Smith -  dmf - the fine grid DM
237847c6ae99SBarry Smith 
237947c6ae99SBarry Smith    Output Parameters:
238047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
238147c6ae99SBarry Smith 
238247c6ae99SBarry Smith    Level: intermediate
238347c6ae99SBarry Smith 
238447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
238547c6ae99SBarry Smith 
2386e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
238747c6ae99SBarry Smith @*/
2388e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
238947c6ae99SBarry Smith {
239047c6ae99SBarry Smith   PetscErrorCode ierr;
239147c6ae99SBarry Smith 
239247c6ae99SBarry Smith   PetscFunctionBegin;
2393171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2394171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
239547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
239647c6ae99SBarry Smith   PetscFunctionReturn(0);
239747c6ae99SBarry Smith }
239847c6ae99SBarry Smith 
239947c6ae99SBarry Smith #undef __FUNCT__
24001a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
24011a266240SBarry Smith /*@C
24021a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
24031a266240SBarry Smith 
24041a266240SBarry Smith     Not Collective
24051a266240SBarry Smith 
24061a266240SBarry Smith     Input Parameters:
24071a266240SBarry Smith +   dm - the DM object
24081a266240SBarry Smith -   destroy - the destroy function
24091a266240SBarry Smith 
24101a266240SBarry Smith     Level: intermediate
24111a266240SBarry Smith 
2412e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
24131a266240SBarry Smith 
2414f07f9ceaSJed Brown @*/
24151a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
24161a266240SBarry Smith {
24171a266240SBarry Smith   PetscFunctionBegin;
2418171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24191a266240SBarry Smith   dm->ctxdestroy = destroy;
24201a266240SBarry Smith   PetscFunctionReturn(0);
24211a266240SBarry Smith }
24221a266240SBarry Smith 
24231a266240SBarry Smith #undef __FUNCT__
24241b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2425b07ff414SBarry Smith /*@
24261b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
242747c6ae99SBarry Smith 
242847c6ae99SBarry Smith     Not Collective
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith     Input Parameters:
243147c6ae99SBarry Smith +   dm - the DM object
243247c6ae99SBarry Smith -   ctx - the user context
243347c6ae99SBarry Smith 
243447c6ae99SBarry Smith     Level: intermediate
243547c6ae99SBarry Smith 
2436e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
243747c6ae99SBarry Smith 
243847c6ae99SBarry Smith @*/
24391b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
244047c6ae99SBarry Smith {
244147c6ae99SBarry Smith   PetscFunctionBegin;
2442171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
244347c6ae99SBarry Smith   dm->ctx = ctx;
244447c6ae99SBarry Smith   PetscFunctionReturn(0);
244547c6ae99SBarry Smith }
244647c6ae99SBarry Smith 
244747c6ae99SBarry Smith #undef __FUNCT__
24481b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
244947c6ae99SBarry Smith /*@
24501b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
245147c6ae99SBarry Smith 
245247c6ae99SBarry Smith     Not Collective
245347c6ae99SBarry Smith 
245447c6ae99SBarry Smith     Input Parameter:
245547c6ae99SBarry Smith .   dm - the DM object
245647c6ae99SBarry Smith 
245747c6ae99SBarry Smith     Output Parameter:
245847c6ae99SBarry Smith .   ctx - the user context
245947c6ae99SBarry Smith 
246047c6ae99SBarry Smith     Level: intermediate
246147c6ae99SBarry Smith 
2462e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
246347c6ae99SBarry Smith 
246447c6ae99SBarry Smith @*/
24651b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
246647c6ae99SBarry Smith {
246747c6ae99SBarry Smith   PetscFunctionBegin;
2468171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24691b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
247047c6ae99SBarry Smith   PetscFunctionReturn(0);
247147c6ae99SBarry Smith }
247247c6ae99SBarry Smith 
247347c6ae99SBarry Smith #undef __FUNCT__
247408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
247508da532bSDmitry Karpeev /*@C
2476df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
247708da532bSDmitry Karpeev 
247808da532bSDmitry Karpeev     Logically Collective on DM
247908da532bSDmitry Karpeev 
248008da532bSDmitry Karpeev     Input Parameter:
248108da532bSDmitry Karpeev +   dm - the DM object
24820298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
248308da532bSDmitry Karpeev 
248408da532bSDmitry Karpeev     Level: intermediate
248508da532bSDmitry Karpeev 
2486835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
248708da532bSDmitry Karpeev          DMSetJacobian()
248808da532bSDmitry Karpeev 
248908da532bSDmitry Karpeev @*/
249008da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
249108da532bSDmitry Karpeev {
249208da532bSDmitry Karpeev   PetscFunctionBegin;
249308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
249408da532bSDmitry Karpeev   PetscFunctionReturn(0);
249508da532bSDmitry Karpeev }
249608da532bSDmitry Karpeev 
249708da532bSDmitry Karpeev #undef __FUNCT__
249808da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
249908da532bSDmitry Karpeev /*@
250008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
250108da532bSDmitry Karpeev 
250208da532bSDmitry Karpeev     Not Collective
250308da532bSDmitry Karpeev 
250408da532bSDmitry Karpeev     Input Parameter:
250508da532bSDmitry Karpeev .   dm - the DM object to destroy
250608da532bSDmitry Karpeev 
250708da532bSDmitry Karpeev     Output Parameter:
250808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
250908da532bSDmitry Karpeev 
251008da532bSDmitry Karpeev     Level: developer
251108da532bSDmitry Karpeev 
251274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
251308da532bSDmitry Karpeev 
251408da532bSDmitry Karpeev @*/
251508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
251608da532bSDmitry Karpeev {
251708da532bSDmitry Karpeev   PetscFunctionBegin;
251808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
251908da532bSDmitry Karpeev   PetscFunctionReturn(0);
252008da532bSDmitry Karpeev }
252108da532bSDmitry Karpeev 
252208da532bSDmitry Karpeev #undef __FUNCT__
252308da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
252408da532bSDmitry Karpeev /*@C
252508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
252608da532bSDmitry Karpeev 
252708da532bSDmitry Karpeev     Logically Collective on DM
252808da532bSDmitry Karpeev 
252908da532bSDmitry Karpeev     Input Parameters:
253008da532bSDmitry Karpeev +   dm - the DM object to destroy
253108da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
253208da532bSDmitry Karpeev 
253308da532bSDmitry Karpeev     Output parameters:
253408da532bSDmitry Karpeev +   xl - lower bound
253508da532bSDmitry Karpeev -   xu - upper bound
253608da532bSDmitry Karpeev 
253708da532bSDmitry Karpeev     Level: intermediate
253808da532bSDmitry Karpeev 
253974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
254008da532bSDmitry Karpeev 
254108da532bSDmitry Karpeev @*/
254208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
254308da532bSDmitry Karpeev {
254408da532bSDmitry Karpeev   PetscErrorCode ierr;
25455fd66863SKarl Rupp 
254608da532bSDmitry Karpeev   PetscFunctionBegin;
254708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
254808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
254908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
255008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
25518865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
255208da532bSDmitry Karpeev   PetscFunctionReturn(0);
255308da532bSDmitry Karpeev }
255408da532bSDmitry Karpeev 
255508da532bSDmitry Karpeev #undef __FUNCT__
2556b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2557b0ae01b7SPeter Brune /*@
2558b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2559b0ae01b7SPeter Brune 
2560b0ae01b7SPeter Brune     Not Collective
2561b0ae01b7SPeter Brune 
2562b0ae01b7SPeter Brune     Input Parameter:
2563b0ae01b7SPeter Brune .   dm - the DM object
2564b0ae01b7SPeter Brune 
2565b0ae01b7SPeter Brune     Output Parameter:
2566b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2567b0ae01b7SPeter Brune 
2568b0ae01b7SPeter Brune     Level: developer
2569b0ae01b7SPeter Brune 
2570b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2571b0ae01b7SPeter Brune 
2572b0ae01b7SPeter Brune @*/
2573b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2574b0ae01b7SPeter Brune {
2575b0ae01b7SPeter Brune   PetscFunctionBegin;
2576b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2577b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2578b0ae01b7SPeter Brune }
2579b0ae01b7SPeter Brune 
2580b0ae01b7SPeter Brune #undef  __FUNCT__
258108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2582748fac09SDmitry Karpeev /*@C
258308da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
258408da532bSDmitry Karpeev 
258508da532bSDmitry Karpeev     Collective on DM
258608da532bSDmitry Karpeev 
258708da532bSDmitry Karpeev     Input Parameter:
258808da532bSDmitry Karpeev +   dm - the DM object
25890298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
259008da532bSDmitry Karpeev 
259108da532bSDmitry Karpeev     Level: developer
259208da532bSDmitry Karpeev 
259374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
259408da532bSDmitry Karpeev 
259508da532bSDmitry Karpeev @*/
259608da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
259708da532bSDmitry Karpeev {
259808da532bSDmitry Karpeev   PetscErrorCode ierr;
25995fd66863SKarl Rupp 
260008da532bSDmitry Karpeev   PetscFunctionBegin;
260108da532bSDmitry Karpeev   if (x) {
260208da532bSDmitry Karpeev     if (!dm->x) {
260308da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
260408da532bSDmitry Karpeev     }
260508da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
26068865f1eaSKarl Rupp   } else if (dm->x) {
260708da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
260808da532bSDmitry Karpeev   }
260908da532bSDmitry Karpeev   PetscFunctionReturn(0);
261008da532bSDmitry Karpeev }
261108da532bSDmitry Karpeev 
26120298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2613264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2614264ace61SBarry Smith 
2615264ace61SBarry Smith #undef __FUNCT__
2616264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2617264ace61SBarry Smith /*@C
2618264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2619264ace61SBarry Smith 
2620264ace61SBarry Smith   Collective on DM
2621264ace61SBarry Smith 
2622264ace61SBarry Smith   Input Parameters:
2623264ace61SBarry Smith + dm     - The DM object
2624264ace61SBarry Smith - method - The name of the DM type
2625264ace61SBarry Smith 
2626264ace61SBarry Smith   Options Database Key:
2627264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2628264ace61SBarry Smith 
2629264ace61SBarry Smith   Notes:
2630e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2631264ace61SBarry Smith 
2632264ace61SBarry Smith   Level: intermediate
2633264ace61SBarry Smith 
2634264ace61SBarry Smith .keywords: DM, set, type
2635264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2636264ace61SBarry Smith @*/
263719fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2638264ace61SBarry Smith {
2639264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2640264ace61SBarry Smith   PetscBool      match;
2641264ace61SBarry Smith   PetscErrorCode ierr;
2642264ace61SBarry Smith 
2643264ace61SBarry Smith   PetscFunctionBegin;
2644264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2645251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2646264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2647264ace61SBarry Smith 
2648607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
26491c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2650ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2651264ace61SBarry Smith 
2652264ace61SBarry Smith   if (dm->ops->destroy) {
2653264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
26540298fd71SBarry Smith     dm->ops->destroy = NULL;
2655264ace61SBarry Smith   }
2656264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2657264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2658264ace61SBarry Smith   PetscFunctionReturn(0);
2659264ace61SBarry Smith }
2660264ace61SBarry Smith 
2661264ace61SBarry Smith #undef __FUNCT__
2662264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2663264ace61SBarry Smith /*@C
2664264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2665264ace61SBarry Smith 
2666264ace61SBarry Smith   Not Collective
2667264ace61SBarry Smith 
2668264ace61SBarry Smith   Input Parameter:
2669264ace61SBarry Smith . dm  - The DM
2670264ace61SBarry Smith 
2671264ace61SBarry Smith   Output Parameter:
2672264ace61SBarry Smith . type - The DM type name
2673264ace61SBarry Smith 
2674264ace61SBarry Smith   Level: intermediate
2675264ace61SBarry Smith 
2676264ace61SBarry Smith .keywords: DM, get, type, name
2677264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2678264ace61SBarry Smith @*/
267919fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2680264ace61SBarry Smith {
2681264ace61SBarry Smith   PetscErrorCode ierr;
2682264ace61SBarry Smith 
2683264ace61SBarry Smith   PetscFunctionBegin;
2684264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2685c959eef4SJed Brown   PetscValidPointer(type,2);
2686264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2687607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2688264ace61SBarry Smith   }
2689264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2690264ace61SBarry Smith   PetscFunctionReturn(0);
2691264ace61SBarry Smith }
2692264ace61SBarry Smith 
269367a56275SMatthew G Knepley #undef __FUNCT__
269467a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
269567a56275SMatthew G Knepley /*@C
269667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
269767a56275SMatthew G Knepley 
269867a56275SMatthew G Knepley   Collective on DM
269967a56275SMatthew G Knepley 
270067a56275SMatthew G Knepley   Input Parameters:
270167a56275SMatthew G Knepley + dm - the DM
270267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
270367a56275SMatthew G Knepley 
270467a56275SMatthew G Knepley   Output Parameter:
270567a56275SMatthew G Knepley . M - pointer to new DM
270667a56275SMatthew G Knepley 
270767a56275SMatthew G Knepley   Notes:
270867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
270967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
271067a56275SMatthew G Knepley   of the input DM.
271167a56275SMatthew G Knepley 
271267a56275SMatthew G Knepley   Level: intermediate
271367a56275SMatthew G Knepley 
271467a56275SMatthew G Knepley .seealso: DMCreate()
271567a56275SMatthew G Knepley @*/
271619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
271767a56275SMatthew G Knepley {
271867a56275SMatthew G Knepley   DM             B;
271967a56275SMatthew G Knepley   char           convname[256];
272067a56275SMatthew G Knepley   PetscBool      sametype, issame;
272167a56275SMatthew G Knepley   PetscErrorCode ierr;
272267a56275SMatthew G Knepley 
272367a56275SMatthew G Knepley   PetscFunctionBegin;
272467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
272567a56275SMatthew G Knepley   PetscValidType(dm,1);
272667a56275SMatthew G Knepley   PetscValidPointer(M,3);
2727251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
272867a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
272967a56275SMatthew G Knepley   {
27300298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
273167a56275SMatthew G Knepley 
273267a56275SMatthew G Knepley     /*
273367a56275SMatthew G Knepley        Order of precedence:
273467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
273567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
273667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
273767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
273867a56275SMatthew G Knepley        5) Use a really basic converter.
273967a56275SMatthew G Knepley     */
274067a56275SMatthew G Knepley 
274167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
274267a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
274367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
274467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
274567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
274667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
27470005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
274867a56275SMatthew G Knepley     if (conv) goto foundconv;
274967a56275SMatthew G Knepley 
275067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
275182f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
275267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
275367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
275467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
275567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
275667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
275767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
27580005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
275967a56275SMatthew G Knepley     if (conv) {
2760fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
276167a56275SMatthew G Knepley       goto foundconv;
276267a56275SMatthew G Knepley     }
276367a56275SMatthew G Knepley 
276467a56275SMatthew G Knepley #if 0
276567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
276667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2767fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
276867a56275SMatthew G Knepley     if (conv) goto foundconv;
276967a56275SMatthew G Knepley 
277067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
277167a56275SMatthew G Knepley     if (dm->ops->convert) {
277267a56275SMatthew G Knepley       conv = dm->ops->convert;
277367a56275SMatthew G Knepley     }
277467a56275SMatthew G Knepley     if (conv) goto foundconv;
277567a56275SMatthew G Knepley #endif
277667a56275SMatthew G Knepley 
277767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
277882f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
277967a56275SMatthew G Knepley 
278067a56275SMatthew G Knepley foundconv:
278167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
278267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
278367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
278467a56275SMatthew G Knepley   }
278567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
278667a56275SMatthew G Knepley   PetscFunctionReturn(0);
278767a56275SMatthew G Knepley }
2788264ace61SBarry Smith 
2789264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2790264ace61SBarry Smith 
2791264ace61SBarry Smith #undef __FUNCT__
2792264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2793264ace61SBarry Smith /*@C
27941c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
27951c84c290SBarry Smith 
27961c84c290SBarry Smith   Not Collective
27971c84c290SBarry Smith 
27981c84c290SBarry Smith   Input Parameters:
27991c84c290SBarry Smith + name        - The name of a new user-defined creation routine
28001c84c290SBarry Smith - create_func - The creation routine itself
28011c84c290SBarry Smith 
28021c84c290SBarry Smith   Notes:
28031c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
28041c84c290SBarry Smith 
28051c84c290SBarry Smith 
28061c84c290SBarry Smith   Sample usage:
28071c84c290SBarry Smith .vb
2808bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
28091c84c290SBarry Smith .ve
28101c84c290SBarry Smith 
28111c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
28121c84c290SBarry Smith .vb
28131c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
28141c84c290SBarry Smith     DMSetType(DM,"my_da");
28151c84c290SBarry Smith .ve
28161c84c290SBarry Smith    or at runtime via the option
28171c84c290SBarry Smith .vb
28181c84c290SBarry Smith     -da_type my_da
28191c84c290SBarry Smith .ve
2820264ace61SBarry Smith 
2821264ace61SBarry Smith   Level: advanced
28221c84c290SBarry Smith 
28231c84c290SBarry Smith .keywords: DM, register
2824bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
28251c84c290SBarry Smith 
2826264ace61SBarry Smith @*/
2827bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2828264ace61SBarry Smith {
2829264ace61SBarry Smith   PetscErrorCode ierr;
2830264ace61SBarry Smith 
2831264ace61SBarry Smith   PetscFunctionBegin;
2832a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2833264ace61SBarry Smith   PetscFunctionReturn(0);
2834264ace61SBarry Smith }
2835264ace61SBarry Smith 
2836b859378eSBarry Smith #undef __FUNCT__
2837b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2838b859378eSBarry Smith /*@C
283955849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2840b859378eSBarry Smith 
2841b859378eSBarry Smith   Collective on PetscViewer
2842b859378eSBarry Smith 
2843b859378eSBarry Smith   Input Parameters:
2844b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2845b859378eSBarry Smith            some related function before a call to DMLoad().
2846b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2847b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2848b859378eSBarry Smith 
2849b859378eSBarry Smith    Level: intermediate
2850b859378eSBarry Smith 
2851b859378eSBarry Smith   Notes:
285255849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2853b859378eSBarry Smith 
2854b859378eSBarry Smith   Notes for advanced users:
2855b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2856b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2857b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2858b859378eSBarry Smith   format is
2859b859378eSBarry Smith .vb
2860b859378eSBarry Smith      has not yet been determined
2861b859378eSBarry Smith .ve
2862b859378eSBarry Smith 
2863b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2864b859378eSBarry Smith @*/
2865b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2866b859378eSBarry Smith {
28679331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
2868b859378eSBarry Smith   PetscErrorCode ierr;
2869b859378eSBarry Smith 
2870b859378eSBarry Smith   PetscFunctionBegin;
2871b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2872b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
287332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
28749331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
28759331c7a4SMatthew G. Knepley   if (isbinary) {
28769331c7a4SMatthew G. Knepley     PetscInt classid;
28779331c7a4SMatthew G. Knepley     char     type[256];
2878b859378eSBarry Smith 
287932c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
28809200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
288132c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
288232c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
28839331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
28849331c7a4SMatthew G. Knepley   } else if (ishdf5) {
28859331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
28869331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
2887b859378eSBarry Smith   PetscFunctionReturn(0);
2888b859378eSBarry Smith }
2889b859378eSBarry Smith 
28907da65231SMatthew G Knepley /******************************** FEM Support **********************************/
28917da65231SMatthew G Knepley 
28927da65231SMatthew G Knepley #undef __FUNCT__
28937da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2894a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2895a6dfd86eSKarl Rupp {
28961d47ebbbSSatish Balay   PetscInt       f;
28971b30c384SMatthew G Knepley   PetscErrorCode ierr;
28981b30c384SMatthew G Knepley 
28997da65231SMatthew G Knepley   PetscFunctionBegin;
290074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29011d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
290257622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
29037da65231SMatthew G Knepley   }
29047da65231SMatthew G Knepley   PetscFunctionReturn(0);
29057da65231SMatthew G Knepley }
29067da65231SMatthew G Knepley 
29077da65231SMatthew G Knepley #undef __FUNCT__
29087da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2909a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2910a6dfd86eSKarl Rupp {
29111b30c384SMatthew G Knepley   PetscInt       f, g;
29127da65231SMatthew G Knepley   PetscErrorCode ierr;
29137da65231SMatthew G Knepley 
29147da65231SMatthew G Knepley   PetscFunctionBegin;
291574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29161d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
291774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
29181d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
2919e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
29207da65231SMatthew G Knepley     }
292174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
29227da65231SMatthew G Knepley   }
29237da65231SMatthew G Knepley   PetscFunctionReturn(0);
29247da65231SMatthew G Knepley }
2925e7c4fc90SDmitry Karpeev 
2926970e74d5SMatthew G Knepley #undef __FUNCT__
2927e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
29286113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
2929e759306cSMatthew G. Knepley {
2930e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
2931e759306cSMatthew G. Knepley   PetscInt       p;
2932e759306cSMatthew G. Knepley   PetscErrorCode ierr;
2933e759306cSMatthew G. Knepley 
2934e759306cSMatthew G. Knepley   PetscFunctionBegin;
2935e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
2936e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
2937e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
2938e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
2939e759306cSMatthew G. Knepley     if (p == rank) {
2940e759306cSMatthew G. Knepley       Vec x;
2941e759306cSMatthew G. Knepley 
2942e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
2943e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
29446113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
2945e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2946e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
2947e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2948e759306cSMatthew G. Knepley     }
2949e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
2950e759306cSMatthew G. Knepley   }
2951e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
2952e759306cSMatthew G. Knepley }
2953e759306cSMatthew G. Knepley 
2954e759306cSMatthew G. Knepley #undef __FUNCT__
295588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
295688ed4aceSMatthew G Knepley /*@
295788ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
295888ed4aceSMatthew G Knepley 
295988ed4aceSMatthew G Knepley   Input Parameter:
296088ed4aceSMatthew G Knepley . dm - The DM
296188ed4aceSMatthew G Knepley 
296288ed4aceSMatthew G Knepley   Output Parameter:
296388ed4aceSMatthew G Knepley . section - The PetscSection
296488ed4aceSMatthew G Knepley 
296588ed4aceSMatthew G Knepley   Level: intermediate
296688ed4aceSMatthew G Knepley 
296788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
296888ed4aceSMatthew G Knepley 
296988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
297088ed4aceSMatthew G Knepley @*/
29710adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
29720adebc6cSBarry Smith {
2973fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
2974fd59a867SMatthew G. Knepley 
297588ed4aceSMatthew G Knepley   PetscFunctionBegin;
297688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
297788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
2978fd59a867SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);}
297988ed4aceSMatthew G Knepley   *section = dm->defaultSection;
298088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
298188ed4aceSMatthew G Knepley }
298288ed4aceSMatthew G Knepley 
298388ed4aceSMatthew G Knepley #undef __FUNCT__
298488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
298588ed4aceSMatthew G Knepley /*@
298688ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
298788ed4aceSMatthew G Knepley 
298888ed4aceSMatthew G Knepley   Input Parameters:
298988ed4aceSMatthew G Knepley + dm - The DM
299088ed4aceSMatthew G Knepley - section - The PetscSection
299188ed4aceSMatthew G Knepley 
299288ed4aceSMatthew G Knepley   Level: intermediate
299388ed4aceSMatthew G Knepley 
299488ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
299588ed4aceSMatthew G Knepley 
299688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
299788ed4aceSMatthew G Knepley @*/
29980adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
29990adebc6cSBarry Smith {
3000af122d2aSMatthew G Knepley   PetscInt       numFields;
3001af122d2aSMatthew G Knepley   PetscInt       f;
300288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
300388ed4aceSMatthew G Knepley 
300488ed4aceSMatthew G Knepley   PetscFunctionBegin;
300588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30061d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
30071d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
300888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
300988ed4aceSMatthew G Knepley   dm->defaultSection = section;
3010af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
3011af122d2aSMatthew G Knepley   if (numFields) {
3012af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3013af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
30140f21e855SMatthew G. Knepley       PetscObject disc;
3015af122d2aSMatthew G Knepley       const char *name;
3016af122d2aSMatthew G Knepley 
3017af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
30180f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
30190f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3020af122d2aSMatthew G Knepley     }
3021af122d2aSMatthew G Knepley   }
30221d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
30231d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
302488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
302588ed4aceSMatthew G Knepley }
302688ed4aceSMatthew G Knepley 
302788ed4aceSMatthew G Knepley #undef __FUNCT__
30289435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
30299435951eSToby Isaac /*@
30309435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
30319435951eSToby Isaac 
30329435951eSToby Isaac   Input Parameter:
30339435951eSToby Isaac . dm - The DM
30349435951eSToby Isaac 
30359435951eSToby Isaac   Output Parameter:
30369435951eSToby 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.
30379435951eSToby 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.
30389435951eSToby Isaac 
30399435951eSToby Isaac   Level: advanced
30409435951eSToby Isaac 
30419435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
30429435951eSToby Isaac 
30439435951eSToby Isaac .seealso: DMSetDefaultConstraints()
30449435951eSToby Isaac @*/
30459435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
30469435951eSToby Isaac {
30479435951eSToby Isaac   PetscErrorCode ierr;
30489435951eSToby Isaac 
30499435951eSToby Isaac   PetscFunctionBegin;
30509435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30519435951eSToby Isaac   PetscValidPointer(section, 2);
30529435951eSToby Isaac   PetscValidPointer(mat, 3);
30539435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
30549435951eSToby Isaac   *section = dm->defaultConstraintSection;
30559435951eSToby Isaac   *mat = dm->defaultConstraintMat;
30569435951eSToby Isaac   PetscFunctionReturn(0);
30579435951eSToby Isaac }
30589435951eSToby Isaac 
30599435951eSToby Isaac #undef __FUNCT__
30609435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
30619435951eSToby Isaac /*@
30629435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
30639435951eSToby Isaac 
30649435951eSToby 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().
30659435951eSToby Isaac 
30669435951eSToby 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.
30679435951eSToby Isaac 
30689435951eSToby Isaac   Input Parameters:
30699435951eSToby Isaac + dm - The DM
30709435951eSToby Isaac + section - The PetscSection describing the range of the constraint matrix: relates rows of the constraint matrix to dofs of the default section.
30719435951eSToby Isaac - mat - The Mat that interpolates local constraints: its width should be the layout size of the default section:  NULL indicates no constraints.
30729435951eSToby Isaac 
30739435951eSToby Isaac   Level: advanced
30749435951eSToby Isaac 
30759435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
30769435951eSToby Isaac 
30779435951eSToby Isaac .seealso: DMGetDefaultConstraints()
30789435951eSToby Isaac @*/
30799435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
30809435951eSToby Isaac {
30819435951eSToby Isaac   PetscInt       numFields;
30829435951eSToby Isaac   PetscErrorCode ierr;
30839435951eSToby Isaac 
30849435951eSToby Isaac   PetscFunctionBegin;
30859435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30869435951eSToby Isaac   if (section) {PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);}
30879435951eSToby Isaac   if (mat) {PetscValidHeaderSpecific(mat,MAT_CLASSID,3);}
30889435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
30899435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
30909435951eSToby Isaac   dm->defaultConstraintSection = section;
30919435951eSToby Isaac   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
30929435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
30939435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
30949435951eSToby Isaac   dm->defaultConstraintMat = mat;
30959435951eSToby Isaac   PetscFunctionReturn(0);
30969435951eSToby Isaac }
30979435951eSToby Isaac 
30989435951eSToby Isaac #undef __FUNCT__
309988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
310088ed4aceSMatthew G Knepley /*@
310188ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
310288ed4aceSMatthew G Knepley 
31038b1ab98fSJed Brown   Collective on DM
31048b1ab98fSJed Brown 
310588ed4aceSMatthew G Knepley   Input Parameter:
310688ed4aceSMatthew G Knepley . dm - The DM
310788ed4aceSMatthew G Knepley 
310888ed4aceSMatthew G Knepley   Output Parameter:
310988ed4aceSMatthew G Knepley . section - The PetscSection
311088ed4aceSMatthew G Knepley 
311188ed4aceSMatthew G Knepley   Level: intermediate
311288ed4aceSMatthew G Knepley 
311388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
311488ed4aceSMatthew G Knepley 
311588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
311688ed4aceSMatthew G Knepley @*/
31170adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
31180adebc6cSBarry Smith {
311988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
312088ed4aceSMatthew G Knepley 
312188ed4aceSMatthew G Knepley   PetscFunctionBegin;
312288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
312388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
312488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3125fd59a867SMatthew G. Knepley     PetscSection s;
3126fd59a867SMatthew G. Knepley 
3127fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3128fd59a867SMatthew 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");
3129fd59a867SMatthew 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");
3130fd59a867SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3131cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3132ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
313388ed4aceSMatthew G Knepley   }
313488ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
313588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
313688ed4aceSMatthew G Knepley }
313788ed4aceSMatthew G Knepley 
313888ed4aceSMatthew G Knepley #undef __FUNCT__
3139b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3140b21d0597SMatthew G Knepley /*@
3141b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3142b21d0597SMatthew G Knepley 
3143b21d0597SMatthew G Knepley   Input Parameters:
3144b21d0597SMatthew G Knepley + dm - The DM
31455080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3146b21d0597SMatthew G Knepley 
3147b21d0597SMatthew G Knepley   Level: intermediate
3148b21d0597SMatthew G Knepley 
3149b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3150b21d0597SMatthew G Knepley 
3151b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3152b21d0597SMatthew G Knepley @*/
31530adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
31540adebc6cSBarry Smith {
3155b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3156b21d0597SMatthew G Knepley 
3157b21d0597SMatthew G Knepley   PetscFunctionBegin;
3158b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31595080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
31601d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3161b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3162b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3163b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3164b21d0597SMatthew G Knepley }
3165b21d0597SMatthew G Knepley 
3166b21d0597SMatthew G Knepley #undef __FUNCT__
316788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
316888ed4aceSMatthew G Knepley /*@
316988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
317088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
317188ed4aceSMatthew G Knepley 
317288ed4aceSMatthew G Knepley   Input Parameter:
317388ed4aceSMatthew G Knepley . dm - The DM
317488ed4aceSMatthew G Knepley 
317588ed4aceSMatthew G Knepley   Output Parameter:
317688ed4aceSMatthew G Knepley . sf - The PetscSF
317788ed4aceSMatthew G Knepley 
317888ed4aceSMatthew G Knepley   Level: intermediate
317988ed4aceSMatthew G Knepley 
318088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
318188ed4aceSMatthew G Knepley 
318288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
318388ed4aceSMatthew G Knepley @*/
31840adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
31850adebc6cSBarry Smith {
318688ed4aceSMatthew G Knepley   PetscInt       nroots;
318788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
318888ed4aceSMatthew G Knepley 
318988ed4aceSMatthew G Knepley   PetscFunctionBegin;
319088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
319188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
31920298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
319388ed4aceSMatthew G Knepley   if (nroots < 0) {
319488ed4aceSMatthew G Knepley     PetscSection section, gSection;
319588ed4aceSMatthew G Knepley 
319688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
319731ea6d37SMatthew G Knepley     if (section) {
319888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
319988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
320031ea6d37SMatthew G Knepley     } else {
32010298fd71SBarry Smith       *sf = NULL;
320231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
320331ea6d37SMatthew G Knepley     }
320488ed4aceSMatthew G Knepley   }
320588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
320688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
320788ed4aceSMatthew G Knepley }
320888ed4aceSMatthew G Knepley 
320988ed4aceSMatthew G Knepley #undef __FUNCT__
321088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
321188ed4aceSMatthew G Knepley /*@
321288ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
321388ed4aceSMatthew G Knepley 
321488ed4aceSMatthew G Knepley   Input Parameters:
321588ed4aceSMatthew G Knepley + dm - The DM
321688ed4aceSMatthew G Knepley - sf - The PetscSF
321788ed4aceSMatthew G Knepley 
321888ed4aceSMatthew G Knepley   Level: intermediate
321988ed4aceSMatthew G Knepley 
322088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
322188ed4aceSMatthew G Knepley 
322288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
322388ed4aceSMatthew G Knepley @*/
32240adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
32250adebc6cSBarry Smith {
322688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
322788ed4aceSMatthew G Knepley 
322888ed4aceSMatthew G Knepley   PetscFunctionBegin;
322988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
323088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
323188ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
323288ed4aceSMatthew G Knepley   dm->defaultSF = sf;
323388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
323488ed4aceSMatthew G Knepley }
323588ed4aceSMatthew G Knepley 
323688ed4aceSMatthew G Knepley #undef __FUNCT__
323788ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
323888ed4aceSMatthew G Knepley /*@C
323988ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
324088ed4aceSMatthew G Knepley   describing the data layout.
324188ed4aceSMatthew G Knepley 
324288ed4aceSMatthew G Knepley   Input Parameters:
324388ed4aceSMatthew G Knepley + dm - The DM
324488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
324588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
324688ed4aceSMatthew G Knepley 
324788ed4aceSMatthew G Knepley   Level: intermediate
324888ed4aceSMatthew G Knepley 
324988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
325088ed4aceSMatthew G Knepley @*/
325188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
325288ed4aceSMatthew G Knepley {
325382f516ccSBarry Smith   MPI_Comm       comm;
325488ed4aceSMatthew G Knepley   PetscLayout    layout;
325588ed4aceSMatthew G Knepley   const PetscInt *ranges;
325688ed4aceSMatthew G Knepley   PetscInt       *local;
325788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3258ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
325988ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
326088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
326188ed4aceSMatthew G Knepley 
326288ed4aceSMatthew G Knepley   PetscFunctionBegin;
326382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
326488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
326588ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
326688ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
326788ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
326888ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
326988ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
327088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
327188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
327288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
327388ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3274ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
32756636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
327688ed4aceSMatthew G Knepley 
32776636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
32786636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3279235fbf56SMatthew 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));
32806636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
328188ed4aceSMatthew G Knepley   }
3282785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3283785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
328488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
32851f588964SMatthew G Knepley     const PetscInt *cind;
32866636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
328788ed4aceSMatthew G Knepley 
328888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
328988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
329088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
329188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
329288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
32936636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
329488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
32956636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
32966636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
32976636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3298057b4bcdSMatthew 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);
32996636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
33006636e97aSMatthew G Knepley     }
330188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
330288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
330388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
330488ed4aceSMatthew G Knepley     }
330588ed4aceSMatthew G Knepley     if (gdof < 0) {
33066636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
330788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
330888ed4aceSMatthew G Knepley 
330905376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
331031d3f06eSJed Brown         if (r < 0) r = -(r+2);
331105376888SMatthew 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);
331288ed4aceSMatthew G Knepley         remote[l].rank  = r;
331388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
331488ed4aceSMatthew G Knepley       }
331588ed4aceSMatthew G Knepley     } else {
33166636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
331788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
331888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
331988ed4aceSMatthew G Knepley       }
332088ed4aceSMatthew G Knepley     }
332188ed4aceSMatthew G Knepley   }
33226636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
332388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
332488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
332588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
332688ed4aceSMatthew G Knepley }
3327af122d2aSMatthew G Knepley 
3328af122d2aSMatthew G Knepley #undef __FUNCT__
3329b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3330b21d0597SMatthew G Knepley /*@
3331b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3332b21d0597SMatthew G Knepley 
3333b21d0597SMatthew G Knepley   Input Parameter:
3334b21d0597SMatthew G Knepley . dm - The DM
3335b21d0597SMatthew G Knepley 
3336b21d0597SMatthew G Knepley   Output Parameter:
3337b21d0597SMatthew G Knepley . sf - The PetscSF
3338b21d0597SMatthew G Knepley 
3339b21d0597SMatthew G Knepley   Level: intermediate
3340b21d0597SMatthew G Knepley 
3341b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3342b21d0597SMatthew G Knepley 
3343057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3344b21d0597SMatthew G Knepley @*/
33450adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
33460adebc6cSBarry Smith {
3347b21d0597SMatthew G Knepley   PetscFunctionBegin;
3348b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3349b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3350b21d0597SMatthew G Knepley   *sf = dm->sf;
3351b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3352b21d0597SMatthew G Knepley }
3353b21d0597SMatthew G Knepley 
3354b21d0597SMatthew G Knepley #undef __FUNCT__
3355057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3356057b4bcdSMatthew G Knepley /*@
3357057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3358057b4bcdSMatthew G Knepley 
3359057b4bcdSMatthew G Knepley   Input Parameters:
3360057b4bcdSMatthew G Knepley + dm - The DM
3361057b4bcdSMatthew G Knepley - sf - The PetscSF
3362057b4bcdSMatthew G Knepley 
3363057b4bcdSMatthew G Knepley   Level: intermediate
3364057b4bcdSMatthew G Knepley 
3365057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3366057b4bcdSMatthew G Knepley @*/
33670adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
33680adebc6cSBarry Smith {
3369057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3370057b4bcdSMatthew G Knepley 
3371057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3372057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3373057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3374057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3375057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3376057b4bcdSMatthew G Knepley   dm->sf = sf;
3377057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3378057b4bcdSMatthew G Knepley }
3379057b4bcdSMatthew G Knepley 
3380057b4bcdSMatthew G Knepley #undef __FUNCT__
33812764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
33822764a2aaSMatthew G. Knepley /*@
33832764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
33842764a2aaSMatthew G. Knepley 
33852764a2aaSMatthew G. Knepley   Input Parameter:
33862764a2aaSMatthew G. Knepley . dm - The DM
33872764a2aaSMatthew G. Knepley 
33882764a2aaSMatthew G. Knepley   Output Parameter:
33892764a2aaSMatthew G. Knepley . prob - The PetscDS
33902764a2aaSMatthew G. Knepley 
33912764a2aaSMatthew G. Knepley   Level: developer
33922764a2aaSMatthew G. Knepley 
33932764a2aaSMatthew G. Knepley .seealso: DMSetDS()
33942764a2aaSMatthew G. Knepley @*/
33952764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3396af122d2aSMatthew G Knepley {
3397af122d2aSMatthew G Knepley   PetscFunctionBegin;
3398af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33990f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
34000f21e855SMatthew G. Knepley   *prob = dm->prob;
34010f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
34020f21e855SMatthew G. Knepley }
34030f21e855SMatthew G. Knepley 
34040f21e855SMatthew G. Knepley #undef __FUNCT__
34052764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
34062764a2aaSMatthew G. Knepley /*@
34072764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
34082764a2aaSMatthew G. Knepley 
34092764a2aaSMatthew G. Knepley   Input Parameters:
34102764a2aaSMatthew G. Knepley + dm - The DM
34112764a2aaSMatthew G. Knepley - prob - The PetscDS
34122764a2aaSMatthew G. Knepley 
34132764a2aaSMatthew G. Knepley   Level: developer
34142764a2aaSMatthew G. Knepley 
34152764a2aaSMatthew G. Knepley .seealso: DMGetDS()
34162764a2aaSMatthew G. Knepley @*/
34172764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
34180f21e855SMatthew G. Knepley {
34190f21e855SMatthew G. Knepley   PetscErrorCode ierr;
34200f21e855SMatthew G. Knepley 
34210f21e855SMatthew G. Knepley   PetscFunctionBegin;
34220f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34232764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
34242764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
34250f21e855SMatthew G. Knepley   dm->prob = prob;
34260f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
34270f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
34280f21e855SMatthew G. Knepley }
34290f21e855SMatthew G. Knepley 
34300f21e855SMatthew G. Knepley #undef __FUNCT__
34310f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
34320f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
34330f21e855SMatthew G. Knepley {
34340f21e855SMatthew G. Knepley   PetscErrorCode ierr;
34350f21e855SMatthew G. Knepley 
34360f21e855SMatthew G. Knepley   PetscFunctionBegin;
34370f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34382764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3439af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3440af122d2aSMatthew G Knepley }
3441af122d2aSMatthew G Knepley 
3442af122d2aSMatthew G Knepley #undef __FUNCT__
3443af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3444af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3445af122d2aSMatthew G Knepley {
34460f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3447af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3448af122d2aSMatthew G Knepley 
3449af122d2aSMatthew G Knepley   PetscFunctionBegin;
3450af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34512764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
34520f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
34530f21e855SMatthew G. Knepley     PetscContainer obj;
34540f21e855SMatthew G. Knepley 
34550f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
34562764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
34570f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3458af122d2aSMatthew G Knepley   }
3459af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3460af122d2aSMatthew G Knepley }
3461af122d2aSMatthew G Knepley 
3462af122d2aSMatthew G Knepley #undef __FUNCT__
3463af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3464c1929be8SMatthew G. Knepley /*@
3465c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3466c1929be8SMatthew G. Knepley 
3467c1929be8SMatthew G. Knepley   Not collective
3468c1929be8SMatthew G. Knepley 
3469c1929be8SMatthew G. Knepley   Input Parameters:
3470c1929be8SMatthew G. Knepley + dm - The DM
3471c1929be8SMatthew G. Knepley - f  - The field number
3472c1929be8SMatthew G. Knepley 
3473c1929be8SMatthew G. Knepley   Output Parameter:
3474c1929be8SMatthew G. Knepley . field - The discretization object
3475c1929be8SMatthew G. Knepley 
3476c1929be8SMatthew G. Knepley   Level: developer
3477c1929be8SMatthew G. Knepley 
3478c1929be8SMatthew G. Knepley .seealso: DMSetField()
3479c1929be8SMatthew G. Knepley @*/
3480af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3481af122d2aSMatthew G Knepley {
34820f21e855SMatthew G. Knepley   PetscErrorCode ierr;
34830f21e855SMatthew G. Knepley 
3484af122d2aSMatthew G Knepley   PetscFunctionBegin;
3485af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34862764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3487decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3488decb47aaSMatthew G. Knepley }
3489decb47aaSMatthew G. Knepley 
3490decb47aaSMatthew G. Knepley #undef __FUNCT__
3491decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3492c1929be8SMatthew G. Knepley /*@
3493c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3494c1929be8SMatthew G. Knepley 
3495c1929be8SMatthew G. Knepley   Logically collective on DM
3496c1929be8SMatthew G. Knepley 
3497c1929be8SMatthew G. Knepley   Input Parameters:
3498c1929be8SMatthew G. Knepley + dm - The DM
3499c1929be8SMatthew G. Knepley . f  - The field number
3500c1929be8SMatthew G. Knepley - field - The discretization object
3501c1929be8SMatthew G. Knepley 
3502c1929be8SMatthew G. Knepley   Level: developer
3503c1929be8SMatthew G. Knepley 
3504c1929be8SMatthew G. Knepley .seealso: DMGetField()
3505c1929be8SMatthew G. Knepley @*/
3506decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3507decb47aaSMatthew G. Knepley {
3508decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3509decb47aaSMatthew G. Knepley 
3510decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3511decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35122764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3513af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3514af122d2aSMatthew G Knepley }
35156636e97aSMatthew G Knepley 
35166636e97aSMatthew G Knepley #undef __FUNCT__
3517b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3518b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3519b64e0483SPeter Brune {
3520b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3521b64e0483SPeter Brune   PetscErrorCode ierr;
3522b64e0483SPeter Brune   Vec coords,ccoords;
3523b64e0483SPeter Brune   VecScatter scat;
3524b64e0483SPeter Brune   PetscFunctionBegin;
3525b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3526b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3527b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3528b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3529b64e0483SPeter Brune   if (coords && !ccoords) {
3530b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
3531b64e0483SPeter Brune     ierr = DMCreateInjection(dmc_coord,dm_coord,&scat);CHKERRQ(ierr);
3532b64e0483SPeter Brune     ierr = VecScatterBegin(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3533b64e0483SPeter Brune     ierr = VecScatterEnd(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3534b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3535b64e0483SPeter Brune     ierr = VecScatterDestroy(&scat);CHKERRQ(ierr);
3536b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3537b64e0483SPeter Brune   }
3538b64e0483SPeter Brune   PetscFunctionReturn(0);
3539b64e0483SPeter Brune }
3540b64e0483SPeter Brune 
3541b64e0483SPeter Brune #undef __FUNCT__
354203dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
354303dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
354403dadc2fSPeter Brune {
354503dadc2fSPeter Brune   DM dm_coord,subdm_coord;
354603dadc2fSPeter Brune   PetscErrorCode ierr;
354703dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
354803dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
354903dadc2fSPeter Brune   PetscFunctionBegin;
355003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
355103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
355203dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
355303dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
355403dadc2fSPeter Brune   if (coords && !ccoords) {
355503dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
355603dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
355703dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
355803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
355903dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
356003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
356103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
356203dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
356303dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
356403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
356503dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
356603dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
356703dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
356803dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
356903dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
357003dadc2fSPeter Brune   }
357103dadc2fSPeter Brune   PetscFunctionReturn(0);
357203dadc2fSPeter Brune }
357303dadc2fSPeter Brune 
357403dadc2fSPeter Brune #undef __FUNCT__
3575c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3576c73cfb54SMatthew G. Knepley /*@
3577c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3578c73cfb54SMatthew G. Knepley 
3579c73cfb54SMatthew G. Knepley   Not collective
3580c73cfb54SMatthew G. Knepley 
3581c73cfb54SMatthew G. Knepley   Input Parameter:
3582c73cfb54SMatthew G. Knepley . dm - The DM
3583c73cfb54SMatthew G. Knepley 
3584c73cfb54SMatthew G. Knepley   Output Parameter:
3585c73cfb54SMatthew G. Knepley . dim - The topological dimension
3586c73cfb54SMatthew G. Knepley 
3587c73cfb54SMatthew G. Knepley   Level: beginner
3588c73cfb54SMatthew G. Knepley 
3589c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3590c73cfb54SMatthew G. Knepley @*/
3591c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3592c73cfb54SMatthew G. Knepley {
3593c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3594c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3595c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3596c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3597c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3598c73cfb54SMatthew G. Knepley }
3599c73cfb54SMatthew G. Knepley 
3600c73cfb54SMatthew G. Knepley #undef __FUNCT__
3601c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3602c73cfb54SMatthew G. Knepley /*@
3603c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3604c73cfb54SMatthew G. Knepley 
3605c73cfb54SMatthew G. Knepley   Collective on dm
3606c73cfb54SMatthew G. Knepley 
3607c73cfb54SMatthew G. Knepley   Input Parameters:
3608c73cfb54SMatthew G. Knepley + dm - The DM
3609c73cfb54SMatthew G. Knepley - dim - The topological dimension
3610c73cfb54SMatthew G. Knepley 
3611c73cfb54SMatthew G. Knepley   Level: beginner
3612c73cfb54SMatthew G. Knepley 
3613c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3614c73cfb54SMatthew G. Knepley @*/
3615c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3616c73cfb54SMatthew G. Knepley {
3617c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3618c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3619c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3620c73cfb54SMatthew G. Knepley   dm->dim = dim;
3621c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3622c73cfb54SMatthew G. Knepley }
3623c73cfb54SMatthew G. Knepley 
3624793f3fe5SMatthew G. Knepley #undef __FUNCT__
3625793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3626793f3fe5SMatthew G. Knepley /*@
3627793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3628793f3fe5SMatthew G. Knepley 
3629793f3fe5SMatthew G. Knepley   Collective on DM
3630793f3fe5SMatthew G. Knepley 
3631793f3fe5SMatthew G. Knepley   Input Parameters:
3632793f3fe5SMatthew G. Knepley + dm - the DM
3633793f3fe5SMatthew G. Knepley - dim - the dimension
3634793f3fe5SMatthew G. Knepley 
3635793f3fe5SMatthew G. Knepley   Output Parameters:
3636793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3637793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3638793f3fe5SMatthew G. Knepley 
3639793f3fe5SMatthew G. Knepley   Note:
3640793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3641793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3642793f3fe5SMatthew G. Knepley   then the interval is empty.
3643793f3fe5SMatthew G. Knepley 
3644793f3fe5SMatthew G. Knepley   Level: intermediate
3645793f3fe5SMatthew G. Knepley 
3646793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3647793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3648793f3fe5SMatthew G. Knepley @*/
3649793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3650793f3fe5SMatthew G. Knepley {
3651793f3fe5SMatthew G. Knepley   PetscInt       d;
3652793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3653793f3fe5SMatthew G. Knepley 
3654793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3655793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3656793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3657793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3658793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3659793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3660793f3fe5SMatthew G. Knepley }
3661793f3fe5SMatthew G. Knepley 
3662793f3fe5SMatthew G. Knepley #undef __FUNCT__
36636636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
36646636e97aSMatthew G Knepley /*@
36656636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
36666636e97aSMatthew G Knepley 
36676636e97aSMatthew G Knepley   Collective on DM
36686636e97aSMatthew G Knepley 
36696636e97aSMatthew G Knepley   Input Parameters:
36706636e97aSMatthew G Knepley + dm - the DM
36716636e97aSMatthew G Knepley - c - coordinate vector
36726636e97aSMatthew G Knepley 
36736636e97aSMatthew G Knepley   Note:
36746636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
36756636e97aSMatthew G Knepley 
36766636e97aSMatthew G Knepley   Level: intermediate
36776636e97aSMatthew G Knepley 
36786636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36796636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
36806636e97aSMatthew G Knepley @*/
36816636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
36826636e97aSMatthew G Knepley {
36836636e97aSMatthew G Knepley   PetscErrorCode ierr;
36846636e97aSMatthew G Knepley 
36856636e97aSMatthew G Knepley   PetscFunctionBegin;
36866636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36876636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
36886636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
36896636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
36906636e97aSMatthew G Knepley   dm->coordinates = c;
36916636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3692b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
369303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
36946636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36956636e97aSMatthew G Knepley }
36966636e97aSMatthew G Knepley 
36976636e97aSMatthew G Knepley #undef __FUNCT__
36986636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
36996636e97aSMatthew G Knepley /*@
37006636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
37016636e97aSMatthew G Knepley 
37026636e97aSMatthew G Knepley   Collective on DM
37036636e97aSMatthew G Knepley 
37046636e97aSMatthew G Knepley    Input Parameters:
37056636e97aSMatthew G Knepley +  dm - the DM
37066636e97aSMatthew G Knepley -  c - coordinate vector
37076636e97aSMatthew G Knepley 
37086636e97aSMatthew G Knepley   Note:
37096636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
37106636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
37116636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
37126636e97aSMatthew G Knepley 
37136636e97aSMatthew G Knepley   Level: intermediate
37146636e97aSMatthew G Knepley 
37156636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
37166636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
37176636e97aSMatthew G Knepley @*/
37186636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
37196636e97aSMatthew G Knepley {
37206636e97aSMatthew G Knepley   PetscErrorCode ierr;
37216636e97aSMatthew G Knepley 
37226636e97aSMatthew G Knepley   PetscFunctionBegin;
37236636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
37246636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
37256636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
37266636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
37278865f1eaSKarl Rupp 
37286636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
37298865f1eaSKarl Rupp 
37306636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
37316636e97aSMatthew G Knepley   PetscFunctionReturn(0);
37326636e97aSMatthew G Knepley }
37336636e97aSMatthew G Knepley 
37346636e97aSMatthew G Knepley #undef __FUNCT__
37356636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
37366636e97aSMatthew G Knepley /*@
37376636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
37386636e97aSMatthew G Knepley 
37396636e97aSMatthew G Knepley   Not Collective
37406636e97aSMatthew G Knepley 
37416636e97aSMatthew G Knepley   Input Parameter:
37426636e97aSMatthew G Knepley . dm - the DM
37436636e97aSMatthew G Knepley 
37446636e97aSMatthew G Knepley   Output Parameter:
37456636e97aSMatthew G Knepley . c - global coordinate vector
37466636e97aSMatthew G Knepley 
37476636e97aSMatthew G Knepley   Note:
37486636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
37496636e97aSMatthew G Knepley 
37506636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
37516636e97aSMatthew G Knepley 
37526636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
37536636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
37546636e97aSMatthew G Knepley 
37556636e97aSMatthew G Knepley   Level: intermediate
37566636e97aSMatthew G Knepley 
37576636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
37586636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
37596636e97aSMatthew G Knepley @*/
37606636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
37616636e97aSMatthew G Knepley {
37626636e97aSMatthew G Knepley   PetscErrorCode ierr;
37636636e97aSMatthew G Knepley 
37646636e97aSMatthew G Knepley   PetscFunctionBegin;
37656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
37666636e97aSMatthew G Knepley   PetscValidPointer(c,2);
37671f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
37680298fd71SBarry Smith     DM cdm = NULL;
37696636e97aSMatthew G Knepley 
37706636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
37716636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
37726636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
37736636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
37746636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
37756636e97aSMatthew G Knepley   }
37766636e97aSMatthew G Knepley   *c = dm->coordinates;
37776636e97aSMatthew G Knepley   PetscFunctionReturn(0);
37786636e97aSMatthew G Knepley }
37796636e97aSMatthew G Knepley 
37806636e97aSMatthew G Knepley #undef __FUNCT__
37816636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
37826636e97aSMatthew G Knepley /*@
37836636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
37846636e97aSMatthew G Knepley 
37856636e97aSMatthew G Knepley   Collective on DM
37866636e97aSMatthew G Knepley 
37876636e97aSMatthew G Knepley   Input Parameter:
37886636e97aSMatthew G Knepley . dm - the DM
37896636e97aSMatthew G Knepley 
37906636e97aSMatthew G Knepley   Output Parameter:
37916636e97aSMatthew G Knepley . c - coordinate vector
37926636e97aSMatthew G Knepley 
37936636e97aSMatthew G Knepley   Note:
37946636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
37956636e97aSMatthew G Knepley 
37966636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
37976636e97aSMatthew G Knepley 
37986636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
37996636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
38006636e97aSMatthew G Knepley 
38016636e97aSMatthew G Knepley   Level: intermediate
38026636e97aSMatthew G Knepley 
38036636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38046636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
38056636e97aSMatthew G Knepley @*/
38066636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
38076636e97aSMatthew G Knepley {
38086636e97aSMatthew G Knepley   PetscErrorCode ierr;
38096636e97aSMatthew G Knepley 
38106636e97aSMatthew G Knepley   PetscFunctionBegin;
38116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38126636e97aSMatthew G Knepley   PetscValidPointer(c,2);
38131f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
38140298fd71SBarry Smith     DM cdm = NULL;
38156636e97aSMatthew G Knepley 
38166636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
38176636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
38186636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
38196636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
38206636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
38216636e97aSMatthew G Knepley   }
38226636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
38236636e97aSMatthew G Knepley   PetscFunctionReturn(0);
38246636e97aSMatthew G Knepley }
38256636e97aSMatthew G Knepley 
38266636e97aSMatthew G Knepley #undef __FUNCT__
38276636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
38286636e97aSMatthew G Knepley /*@
38291cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
38306636e97aSMatthew G Knepley 
38316636e97aSMatthew G Knepley   Collective on DM
38326636e97aSMatthew G Knepley 
38336636e97aSMatthew G Knepley   Input Parameter:
38346636e97aSMatthew G Knepley . dm - the DM
38356636e97aSMatthew G Knepley 
38366636e97aSMatthew G Knepley   Output Parameter:
38376636e97aSMatthew G Knepley . cdm - coordinate DM
38386636e97aSMatthew G Knepley 
38396636e97aSMatthew G Knepley   Level: intermediate
38406636e97aSMatthew G Knepley 
38416636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38421cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
38436636e97aSMatthew G Knepley @*/
38446636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
38456636e97aSMatthew G Knepley {
38466636e97aSMatthew G Knepley   PetscErrorCode ierr;
38476636e97aSMatthew G Knepley 
38486636e97aSMatthew G Knepley   PetscFunctionBegin;
38496636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38506636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
38516636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
385282f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
38536636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
38546636e97aSMatthew G Knepley   }
38556636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
38566636e97aSMatthew G Knepley   PetscFunctionReturn(0);
38576636e97aSMatthew G Knepley }
3858e87bb0d3SMatthew G Knepley 
3859e87bb0d3SMatthew G Knepley #undef __FUNCT__
38601cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
38611cfe2091SMatthew G. Knepley /*@
38621cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
38631cfe2091SMatthew G. Knepley 
38641cfe2091SMatthew G. Knepley   Logically Collective on DM
38651cfe2091SMatthew G. Knepley 
38661cfe2091SMatthew G. Knepley   Input Parameters:
38671cfe2091SMatthew G. Knepley + dm - the DM
38681cfe2091SMatthew G. Knepley - cdm - coordinate DM
38691cfe2091SMatthew G. Knepley 
38701cfe2091SMatthew G. Knepley   Level: intermediate
38711cfe2091SMatthew G. Knepley 
38721cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38731cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
38741cfe2091SMatthew G. Knepley @*/
38751cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
38761cfe2091SMatthew G. Knepley {
38771cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
38781cfe2091SMatthew G. Knepley 
38791cfe2091SMatthew G. Knepley   PetscFunctionBegin;
38801cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38811cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
38821cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
38831cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
38841cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
38851cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
38861cfe2091SMatthew G. Knepley }
38871cfe2091SMatthew G. Knepley 
38881cfe2091SMatthew G. Knepley #undef __FUNCT__
388946e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
389046e270d4SMatthew G. Knepley /*@
389146e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
389246e270d4SMatthew G. Knepley 
389346e270d4SMatthew G. Knepley   Not Collective
389446e270d4SMatthew G. Knepley 
389546e270d4SMatthew G. Knepley   Input Parameter:
389646e270d4SMatthew G. Knepley . dm - The DM object
389746e270d4SMatthew G. Knepley 
389846e270d4SMatthew G. Knepley   Output Parameter:
389946e270d4SMatthew G. Knepley . dim - The embedding dimension
390046e270d4SMatthew G. Knepley 
390146e270d4SMatthew G. Knepley   Level: intermediate
390246e270d4SMatthew G. Knepley 
390346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
390446e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
390546e270d4SMatthew G. Knepley @*/
390646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
390746e270d4SMatthew G. Knepley {
390846e270d4SMatthew G. Knepley   PetscFunctionBegin;
390946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
391046e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
39119a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
39129a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
39139a9a41abSToby Isaac   }
391446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
391546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
391646e270d4SMatthew G. Knepley }
391746e270d4SMatthew G. Knepley 
391846e270d4SMatthew G. Knepley #undef __FUNCT__
391946e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
392046e270d4SMatthew G. Knepley /*@
392146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
392246e270d4SMatthew G. Knepley 
392346e270d4SMatthew G. Knepley   Not Collective
392446e270d4SMatthew G. Knepley 
392546e270d4SMatthew G. Knepley   Input Parameters:
392646e270d4SMatthew G. Knepley + dm  - The DM object
392746e270d4SMatthew G. Knepley - dim - The embedding dimension
392846e270d4SMatthew G. Knepley 
392946e270d4SMatthew G. Knepley   Level: intermediate
393046e270d4SMatthew G. Knepley 
393146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
393246e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
393346e270d4SMatthew G. Knepley @*/
393446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
393546e270d4SMatthew G. Knepley {
393646e270d4SMatthew G. Knepley   PetscFunctionBegin;
393746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
393846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
393946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
394046e270d4SMatthew G. Knepley }
394146e270d4SMatthew G. Knepley 
394246e270d4SMatthew G. Knepley #undef __FUNCT__
3943e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
3944e8abe2deSMatthew G. Knepley /*@
3945e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
3946e8abe2deSMatthew G. Knepley 
3947e8abe2deSMatthew G. Knepley   Not Collective
3948e8abe2deSMatthew G. Knepley 
3949e8abe2deSMatthew G. Knepley   Input Parameter:
3950e8abe2deSMatthew G. Knepley . dm - The DM object
3951e8abe2deSMatthew G. Knepley 
3952e8abe2deSMatthew G. Knepley   Output Parameter:
3953e8abe2deSMatthew G. Knepley . section - The PetscSection object
3954e8abe2deSMatthew G. Knepley 
3955e8abe2deSMatthew G. Knepley   Level: intermediate
3956e8abe2deSMatthew G. Knepley 
3957e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
3958e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
3959e8abe2deSMatthew G. Knepley @*/
3960e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
3961e8abe2deSMatthew G. Knepley {
3962e8abe2deSMatthew G. Knepley   DM             cdm;
3963e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
3964e8abe2deSMatthew G. Knepley 
3965e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
3966e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3967e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
3968e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3969e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
3970e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
3971e8abe2deSMatthew G. Knepley }
3972e8abe2deSMatthew G. Knepley 
3973e8abe2deSMatthew G. Knepley #undef __FUNCT__
3974e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
3975e8abe2deSMatthew G. Knepley /*@
3976e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
3977e8abe2deSMatthew G. Knepley 
3978e8abe2deSMatthew G. Knepley   Not Collective
3979e8abe2deSMatthew G. Knepley 
3980e8abe2deSMatthew G. Knepley   Input Parameters:
3981e8abe2deSMatthew G. Knepley + dm      - The DM object
398246e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
3983e8abe2deSMatthew G. Knepley - section - The PetscSection object
3984e8abe2deSMatthew G. Knepley 
3985e8abe2deSMatthew G. Knepley   Level: intermediate
3986e8abe2deSMatthew G. Knepley 
3987e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
3988e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
3989e8abe2deSMatthew G. Knepley @*/
399046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
3991e8abe2deSMatthew G. Knepley {
3992e8abe2deSMatthew G. Knepley   DM             cdm;
3993e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
3994e8abe2deSMatthew G. Knepley 
3995e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
3996e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
399746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
3998e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3999e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
400046e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
400146e270d4SMatthew G. Knepley     PetscInt d = dim;
400246e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
400346e270d4SMatthew G. Knepley 
400446e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
400546e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
400646e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
400746e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
400846e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
400946e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
401046e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
401146e270d4SMatthew G. Knepley     }
401246e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
401346e270d4SMatthew G. Knepley   }
4014e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4015e8abe2deSMatthew G. Knepley }
4016e8abe2deSMatthew G. Knepley 
4017e8abe2deSMatthew G. Knepley #undef __FUNCT__
4018c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
4019c6b900c6SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L)
4020c6b900c6SMatthew G. Knepley {
4021c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4022c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4023c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4024c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
4025c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4026c6b900c6SMatthew G. Knepley }
4027c6b900c6SMatthew G. Knepley 
4028c6b900c6SMatthew G. Knepley #undef __FUNCT__
4029c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
4030c6b900c6SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[])
4031c6b900c6SMatthew G. Knepley {
4032c6b900c6SMatthew G. Knepley   Vec            coordinates;
4033c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4034c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4035c6b900c6SMatthew G. Knepley 
4036c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4037c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4038c6b900c6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
4039c6b900c6SMatthew G. Knepley   ierr = VecGetBlockSize(coordinates, &dim);CHKERRQ(ierr);
4040c6b900c6SMatthew G. Knepley   ierr = PetscMalloc1(dim,&dm->L);CHKERRQ(ierr);
4041c6b900c6SMatthew G. Knepley   ierr = PetscMalloc1(dim,&dm->maxCell);CHKERRQ(ierr);
4042c6b900c6SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d];}
4043c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4044c6b900c6SMatthew G. Knepley }
4045c6b900c6SMatthew G. Knepley 
4046c6b900c6SMatthew G. Knepley #undef __FUNCT__
4047e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4048e87bb0d3SMatthew G Knepley /*@
4049e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4050e87bb0d3SMatthew G Knepley 
4051e87bb0d3SMatthew G Knepley   Not collective
4052e87bb0d3SMatthew G Knepley 
4053e87bb0d3SMatthew G Knepley   Input Parameters:
4054e87bb0d3SMatthew G Knepley + dm - The DM
4055e87bb0d3SMatthew G Knepley - v - The Vec of points
4056e87bb0d3SMatthew G Knepley 
405761e3bb9bSMatthew G Knepley   Output Parameter:
4058e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4059e87bb0d3SMatthew G Knepley 
4060e87bb0d3SMatthew G Knepley   Level: developer
406161e3bb9bSMatthew G Knepley 
406261e3bb9bSMatthew G Knepley .keywords: point location, mesh
406361e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
406461e3bb9bSMatthew G Knepley @*/
4065e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4066e87bb0d3SMatthew G Knepley {
4067735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4068735aa83eSMatthew G Knepley 
4069e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4070e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4071e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4072e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
4073735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4074735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
407582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
4076e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4077e87bb0d3SMatthew G Knepley }
407814f150ffSMatthew G. Knepley 
407914f150ffSMatthew G. Knepley #undef __FUNCT__
408014f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4081f4d763aaSMatthew G. Knepley /*@
4082f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4083f4d763aaSMatthew G. Knepley 
4084f4d763aaSMatthew G. Knepley   Input Parameter:
4085f4d763aaSMatthew G. Knepley . dm - The original DM
4086f4d763aaSMatthew G. Knepley 
4087f4d763aaSMatthew G. Knepley   Output Parameter:
4088f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4089f4d763aaSMatthew G. Knepley 
4090f4d763aaSMatthew G. Knepley   Level: intermediate
4091f4d763aaSMatthew G. Knepley 
4092f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4093f4d763aaSMatthew G. Knepley @*/
409414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
409514f150ffSMatthew G. Knepley {
4096c26acbdeSMatthew G. Knepley   PetscSection   section;
4097c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
409814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
409914f150ffSMatthew G. Knepley 
410014f150ffSMatthew G. Knepley   PetscFunctionBegin;
410114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
410214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4103c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4104c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4105c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4106c26acbdeSMatthew G. Knepley     *odm = dm;
4107c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4108c26acbdeSMatthew G. Knepley   }
410914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4110c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
411114f150ffSMatthew G. Knepley     PetscSF      sf;
411214f150ffSMatthew G. Knepley 
411314f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
411414f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
411514f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
411614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
411714f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
411814f150ffSMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, &gsection);CHKERRQ(ierr);
411914f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
412014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
412114f150ffSMatthew G. Knepley   }
412214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
412314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
412414f150ffSMatthew G. Knepley }
4125f4d763aaSMatthew G. Knepley 
4126f4d763aaSMatthew G. Knepley #undef __FUNCT__
4127f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4128f4d763aaSMatthew G. Knepley /*@
4129cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4130f4d763aaSMatthew G. Knepley 
4131f4d763aaSMatthew G. Knepley   Input Parameter:
4132f4d763aaSMatthew G. Knepley . dm - The original DM
4133f4d763aaSMatthew G. Knepley 
4134cdb7a50dSMatthew G. Knepley   Output Parameters:
4135cdb7a50dSMatthew G. Knepley + num - The output sequence number
4136cdb7a50dSMatthew G. Knepley - val - The output sequence value
4137f4d763aaSMatthew G. Knepley 
4138f4d763aaSMatthew G. Knepley   Level: intermediate
4139f4d763aaSMatthew G. Knepley 
4140f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4141f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4142f4d763aaSMatthew G. Knepley 
4143f4d763aaSMatthew G. Knepley .seealso: VecView()
4144f4d763aaSMatthew G. Knepley @*/
4145cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4146f4d763aaSMatthew G. Knepley {
4147f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4148f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4149cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4150cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4151f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4152f4d763aaSMatthew G. Knepley }
4153f4d763aaSMatthew G. Knepley 
4154f4d763aaSMatthew G. Knepley #undef __FUNCT__
4155f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4156f4d763aaSMatthew G. Knepley /*@
4157cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4158f4d763aaSMatthew G. Knepley 
4159f4d763aaSMatthew G. Knepley   Input Parameters:
4160f4d763aaSMatthew G. Knepley + dm - The original DM
4161cdb7a50dSMatthew G. Knepley . num - The output sequence number
4162cdb7a50dSMatthew G. Knepley - val - The output sequence value
4163f4d763aaSMatthew G. Knepley 
4164f4d763aaSMatthew G. Knepley   Level: intermediate
4165f4d763aaSMatthew G. Knepley 
4166f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4167f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4168f4d763aaSMatthew G. Knepley 
4169f4d763aaSMatthew G. Knepley .seealso: VecView()
4170f4d763aaSMatthew G. Knepley @*/
4171cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4172f4d763aaSMatthew G. Knepley {
4173f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4174f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4175f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4176cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4177cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4178cdb7a50dSMatthew G. Knepley }
4179cdb7a50dSMatthew G. Knepley 
4180cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4181cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4182cdb7a50dSMatthew G. Knepley /*@C
4183cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4184cdb7a50dSMatthew G. Knepley 
4185cdb7a50dSMatthew G. Knepley   Input Parameters:
4186cdb7a50dSMatthew G. Knepley + dm   - The original DM
4187cdb7a50dSMatthew G. Knepley . name - The sequence name
4188cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4189cdb7a50dSMatthew G. Knepley 
4190cdb7a50dSMatthew G. Knepley   Output Parameter:
4191cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4192cdb7a50dSMatthew G. Knepley 
4193cdb7a50dSMatthew G. Knepley   Level: intermediate
4194cdb7a50dSMatthew G. Knepley 
4195cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4196cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4197cdb7a50dSMatthew G. Knepley 
4198cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4199cdb7a50dSMatthew G. Knepley @*/
4200cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4201cdb7a50dSMatthew G. Knepley {
4202cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4203cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4204cdb7a50dSMatthew G. Knepley 
4205cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4206cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4207cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4208cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4209cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4210cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4211cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4212cdb7a50dSMatthew G. Knepley     PetscScalar value;
4213cdb7a50dSMatthew G. Knepley 
4214cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
42154aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4216cdb7a50dSMatthew G. Knepley #endif
4217cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4218f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4219f4d763aaSMatthew G. Knepley }
4220