xref: /petsc/src/dm/interface/dm.c (revision 1de53e9a733dc3b19fc42f4647824e8637397fc6)
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;
54c6b900c6SMatthew G. Knepley   v->L                    = NULL;
55c6b900c6SMatthew G. Knepley   v->maxCell              = NULL;
56435a35e8SMatthew G Knepley   {
57435a35e8SMatthew G Knepley     PetscInt i;
58435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
590298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
60435a35e8SMatthew G Knepley     }
61435a35e8SMatthew G Knepley   }
622764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6314f150ffSMatthew G. Knepley   v->dmBC = NULL;
64f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
65cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
66c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
67b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
681411c6eeSJed Brown   *dm = v;
69a4121054SBarry Smith   PetscFunctionReturn(0);
70a4121054SBarry Smith }
71a4121054SBarry Smith 
72a4121054SBarry Smith #undef __FUNCT__
7338221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
7438221697SMatthew G. Knepley /*@
7538221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
7638221697SMatthew G. Knepley 
7738221697SMatthew G. Knepley   Collective on MPI_Comm
7838221697SMatthew G. Knepley 
7938221697SMatthew G. Knepley   Input Parameter:
8038221697SMatthew G. Knepley . dm - The original DM object
8138221697SMatthew G. Knepley 
8238221697SMatthew G. Knepley   Output Parameter:
8338221697SMatthew G. Knepley . newdm  - The new DM object
8438221697SMatthew G. Knepley 
8538221697SMatthew G. Knepley   Level: beginner
8638221697SMatthew G. Knepley 
8738221697SMatthew G. Knepley .keywords: DM, topology, create
8838221697SMatthew G. Knepley @*/
8938221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9038221697SMatthew G. Knepley {
9138221697SMatthew G. Knepley   PetscSF        sf;
9238221697SMatthew G. Knepley   Vec            coords;
9338221697SMatthew G. Knepley   void          *ctx;
94*1de53e9aSMatthew G. Knepley   PetscInt       dim;
9538221697SMatthew G. Knepley   PetscErrorCode ierr;
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley   PetscFunctionBegin;
9838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9938221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10038221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
101*1de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
102*1de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
10338221697SMatthew G. Knepley   if (dm->ops->clone) {
10438221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
10538221697SMatthew G. Knepley   }
106cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
10738221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
10838221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
10938221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
11038221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
11138221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
11238221697SMatthew G. Knepley   if (coords) {
11338221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
11438221697SMatthew G. Knepley   } else {
11538221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
11638221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
11738221697SMatthew G. Knepley   }
118c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
119c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
120c6b900c6SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L);CHKERRQ(ierr);
121c6b900c6SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L);CHKERRQ(ierr);
122c6b900c6SMatthew G. Knepley   }
12338221697SMatthew G. Knepley   PetscFunctionReturn(0);
12438221697SMatthew G. Knepley }
12538221697SMatthew G. Knepley 
12638221697SMatthew G. Knepley #undef __FUNCT__
1279a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1289a42bb27SBarry Smith /*@C
129564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1309a42bb27SBarry Smith 
131aa219208SBarry Smith    Logically Collective on DMDA
1329a42bb27SBarry Smith 
1339a42bb27SBarry Smith    Input Parameter:
1349a42bb27SBarry Smith +  da - initial distributed array
1358154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1369a42bb27SBarry Smith 
1379a42bb27SBarry Smith    Options Database:
138dd85299cSBarry Smith .   -dm_vec_type ctype
1399a42bb27SBarry Smith 
1409a42bb27SBarry Smith    Level: intermediate
1419a42bb27SBarry Smith 
142c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType, DMGetVecType()
1439a42bb27SBarry Smith @*/
14419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1459a42bb27SBarry Smith {
1469a42bb27SBarry Smith   PetscErrorCode ierr;
1479a42bb27SBarry Smith 
1489a42bb27SBarry Smith   PetscFunctionBegin;
1499a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1509a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
15119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1529a42bb27SBarry Smith   PetscFunctionReturn(0);
1539a42bb27SBarry Smith }
1549a42bb27SBarry Smith 
1559a42bb27SBarry Smith #undef __FUNCT__
156c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
157c0dedaeaSBarry Smith /*@C
158c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
159c0dedaeaSBarry Smith 
160c0dedaeaSBarry Smith    Logically Collective on DMDA
161c0dedaeaSBarry Smith 
162c0dedaeaSBarry Smith    Input Parameter:
163c0dedaeaSBarry Smith .  da - initial distributed array
164c0dedaeaSBarry Smith 
165c0dedaeaSBarry Smith    Output Parameter:
166c0dedaeaSBarry Smith .  ctype - the vector type
167c0dedaeaSBarry Smith 
168c0dedaeaSBarry Smith    Level: intermediate
169c0dedaeaSBarry Smith 
170c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
171c0dedaeaSBarry Smith @*/
172c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
173c0dedaeaSBarry Smith {
174c0dedaeaSBarry Smith   PetscFunctionBegin;
175c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
176c0dedaeaSBarry Smith   *ctype = da->vectype;
177c0dedaeaSBarry Smith   PetscFunctionReturn(0);
178c0dedaeaSBarry Smith }
179c0dedaeaSBarry Smith 
180c0dedaeaSBarry Smith #undef __FUNCT__
1815f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1825f1ad066SMatthew G Knepley /*@
18334f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1845f1ad066SMatthew G Knepley 
1855f1ad066SMatthew G Knepley   Not collective
1865f1ad066SMatthew G Knepley 
1875f1ad066SMatthew G Knepley   Input Parameter:
1885f1ad066SMatthew G Knepley . v - The Vec
1895f1ad066SMatthew G Knepley 
1905f1ad066SMatthew G Knepley   Output Parameter:
1915f1ad066SMatthew G Knepley . dm - The DM
1925f1ad066SMatthew G Knepley 
1935f1ad066SMatthew G Knepley   Level: intermediate
1945f1ad066SMatthew G Knepley 
1955f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1965f1ad066SMatthew G Knepley @*/
1975f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1985f1ad066SMatthew G Knepley {
1995f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2005f1ad066SMatthew G Knepley 
2015f1ad066SMatthew G Knepley   PetscFunctionBegin;
2025f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2035f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2045f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2055f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2065f1ad066SMatthew G Knepley }
2075f1ad066SMatthew G Knepley 
2085f1ad066SMatthew G Knepley #undef __FUNCT__
2095f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2105f1ad066SMatthew G Knepley /*@
2115f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
2125f1ad066SMatthew G Knepley 
2135f1ad066SMatthew G Knepley   Not collective
2145f1ad066SMatthew G Knepley 
2155f1ad066SMatthew G Knepley   Input Parameters:
2165f1ad066SMatthew G Knepley + v - The Vec
2175f1ad066SMatthew G Knepley - dm - The DM
2185f1ad066SMatthew G Knepley 
2195f1ad066SMatthew G Knepley   Level: intermediate
2205f1ad066SMatthew G Knepley 
2215f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2225f1ad066SMatthew G Knepley @*/
2235f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2245f1ad066SMatthew G Knepley {
2255f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2265f1ad066SMatthew G Knepley 
2275f1ad066SMatthew G Knepley   PetscFunctionBegin;
2285f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
229d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2305f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2315f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2325f1ad066SMatthew G Knepley }
2335f1ad066SMatthew G Knepley 
2345f1ad066SMatthew G Knepley #undef __FUNCT__
235521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
236521d9a4cSLisandro Dalcin /*@C
237521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
238521d9a4cSLisandro Dalcin 
239521d9a4cSLisandro Dalcin    Logically Collective on DM
240521d9a4cSLisandro Dalcin 
241521d9a4cSLisandro Dalcin    Input Parameter:
242521d9a4cSLisandro Dalcin +  dm - the DM context
243521d9a4cSLisandro Dalcin .  ctype - the matrix type
244521d9a4cSLisandro Dalcin 
245521d9a4cSLisandro Dalcin    Options Database:
246521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
247521d9a4cSLisandro Dalcin 
248521d9a4cSLisandro Dalcin    Level: intermediate
249521d9a4cSLisandro Dalcin 
250c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
251521d9a4cSLisandro Dalcin @*/
25219fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
253521d9a4cSLisandro Dalcin {
254521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
25588f0584fSBarry Smith 
256521d9a4cSLisandro Dalcin   PetscFunctionBegin;
257521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
258521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
25919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
260521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
261521d9a4cSLisandro Dalcin }
262521d9a4cSLisandro Dalcin 
263521d9a4cSLisandro Dalcin #undef __FUNCT__
264c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
265c0dedaeaSBarry Smith /*@C
266c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
267c0dedaeaSBarry Smith 
268c0dedaeaSBarry Smith    Logically Collective on DM
269c0dedaeaSBarry Smith 
270c0dedaeaSBarry Smith    Input Parameter:
271c0dedaeaSBarry Smith .  dm - the DM context
272c0dedaeaSBarry Smith 
273c0dedaeaSBarry Smith    Output Parameter:
274c0dedaeaSBarry Smith .  ctype - the matrix type
275c0dedaeaSBarry Smith 
276c0dedaeaSBarry Smith    Options Database:
277c0dedaeaSBarry Smith .   -dm_mat_type ctype
278c0dedaeaSBarry Smith 
279c0dedaeaSBarry Smith    Level: intermediate
280c0dedaeaSBarry Smith 
281c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
282c0dedaeaSBarry Smith @*/
283c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
284c0dedaeaSBarry Smith {
285c0dedaeaSBarry Smith   PetscFunctionBegin;
286c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
287c0dedaeaSBarry Smith   *ctype = dm->mattype;
288c0dedaeaSBarry Smith   PetscFunctionReturn(0);
289c0dedaeaSBarry Smith }
290c0dedaeaSBarry Smith 
291c0dedaeaSBarry Smith #undef __FUNCT__
292c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
293c688c046SMatthew G Knepley /*@
29434f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
295c688c046SMatthew G Knepley 
296c688c046SMatthew G Knepley   Not collective
297c688c046SMatthew G Knepley 
298c688c046SMatthew G Knepley   Input Parameter:
299c688c046SMatthew G Knepley . A - The Mat
300c688c046SMatthew G Knepley 
301c688c046SMatthew G Knepley   Output Parameter:
302c688c046SMatthew G Knepley . dm - The DM
303c688c046SMatthew G Knepley 
304c688c046SMatthew G Knepley   Level: intermediate
305c688c046SMatthew G Knepley 
306c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
307c688c046SMatthew G Knepley @*/
308c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
309c688c046SMatthew G Knepley {
310c688c046SMatthew G Knepley   PetscErrorCode ierr;
311c688c046SMatthew G Knepley 
312c688c046SMatthew G Knepley   PetscFunctionBegin;
313c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
314c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
315c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
316c688c046SMatthew G Knepley   PetscFunctionReturn(0);
317c688c046SMatthew G Knepley }
318c688c046SMatthew G Knepley 
319c688c046SMatthew G Knepley #undef __FUNCT__
320c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
321c688c046SMatthew G Knepley /*@
322c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
323c688c046SMatthew G Knepley 
324c688c046SMatthew G Knepley   Not collective
325c688c046SMatthew G Knepley 
326c688c046SMatthew G Knepley   Input Parameters:
327c688c046SMatthew G Knepley + A - The Mat
328c688c046SMatthew G Knepley - dm - The DM
329c688c046SMatthew G Knepley 
330c688c046SMatthew G Knepley   Level: intermediate
331c688c046SMatthew G Knepley 
332c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
333c688c046SMatthew G Knepley @*/
334c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
335c688c046SMatthew G Knepley {
336c688c046SMatthew G Knepley   PetscErrorCode ierr;
337c688c046SMatthew G Knepley 
338c688c046SMatthew G Knepley   PetscFunctionBegin;
339c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3408865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
341c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
342c688c046SMatthew G Knepley   PetscFunctionReturn(0);
343c688c046SMatthew G Knepley }
344c688c046SMatthew G Knepley 
345c688c046SMatthew G Knepley #undef __FUNCT__
3469a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3479a42bb27SBarry Smith /*@C
3489a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
349aa219208SBarry Smith    DMDA options in the database.
3509a42bb27SBarry Smith 
351aa219208SBarry Smith    Logically Collective on DMDA
3529a42bb27SBarry Smith 
3539a42bb27SBarry Smith    Input Parameter:
354aa219208SBarry Smith +  da - the DMDA context
3559a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3569a42bb27SBarry Smith 
3579a42bb27SBarry Smith    Notes:
3589a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3599a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3609a42bb27SBarry Smith 
3619a42bb27SBarry Smith    Level: advanced
3629a42bb27SBarry Smith 
363aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
3649a42bb27SBarry Smith 
3659a42bb27SBarry Smith .seealso: DMSetFromOptions()
3669a42bb27SBarry Smith @*/
3677087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3689a42bb27SBarry Smith {
3699a42bb27SBarry Smith   PetscErrorCode ierr;
3709a42bb27SBarry Smith 
3719a42bb27SBarry Smith   PetscFunctionBegin;
3729a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3739a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
3749a42bb27SBarry Smith   PetscFunctionReturn(0);
3759a42bb27SBarry Smith }
3769a42bb27SBarry Smith 
3779a42bb27SBarry Smith #undef __FUNCT__
37847c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
37947c6ae99SBarry Smith /*@
380aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
38147c6ae99SBarry Smith 
38247c6ae99SBarry Smith     Collective on DM
38347c6ae99SBarry Smith 
38447c6ae99SBarry Smith     Input Parameter:
38547c6ae99SBarry Smith .   dm - the DM object to destroy
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith     Level: developer
38847c6ae99SBarry Smith 
389e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
39047c6ae99SBarry Smith 
39147c6ae99SBarry Smith @*/
392fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
39347c6ae99SBarry Smith {
3940f21e855SMatthew G. Knepley   PetscInt       i, cnt = 0, Nf = 0, f;
395dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
39647c6ae99SBarry Smith   PetscErrorCode ierr;
39747c6ae99SBarry Smith 
39847c6ae99SBarry Smith   PetscFunctionBegin;
3996bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4006bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
40187e657c6SBarry Smith 
4020f21e855SMatthew G. Knepley   if ((*dm)->prob) {
4030f21e855SMatthew G. Knepley     PetscObject disc;
4040f21e855SMatthew G. Knepley 
405a546ed68SMatthew G. Knepley     /* I think it makes sense to dump all attached things when you are destroyed, which also eliminates circular references */
4062764a2aaSMatthew G. Knepley     ierr = PetscDSGetNumFields((*dm)->prob, &Nf);CHKERRQ(ierr);
4070f21e855SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4082764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization((*dm)->prob, f, &disc);CHKERRQ(ierr);
4090f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "pmat", NULL);CHKERRQ(ierr);
4100f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "nullspace", NULL);CHKERRQ(ierr);
4110f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "nearnullspace", NULL);CHKERRQ(ierr);
4120f21e855SMatthew G. Knepley     }
413a546ed68SMatthew G. Knepley   }
41487e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
415732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4168865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4178865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
418732e2eb9SMatthew G Knepley   }
419dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4202348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4212348bcf4SPeter Brune   if ((*dm)->x) {
4222348bcf4SPeter Brune     DM obj;
4232348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4242348bcf4SPeter Brune     if (obj == *dm) cnt++;
4252348bcf4SPeter Brune   }
426732e2eb9SMatthew G Knepley 
4276bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
428732e2eb9SMatthew G Knepley   /*
429732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
430732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
431732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
432732e2eb9SMatthew G Knepley   */
4336bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4346bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
435732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4366bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4376bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
438732e2eb9SMatthew G Knepley   }
439f490541aSPeter Brune   nnext=(*dm)->namedglobal;
4400298fd71SBarry Smith   (*dm)->namedglobal = NULL;
441f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
4422348bcf4SPeter Brune     nnext = nlink->next;
4432348bcf4SPeter 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);
4442348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4452348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4462348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4472348bcf4SPeter Brune   }
448f490541aSPeter Brune   nnext=(*dm)->namedlocal;
4490298fd71SBarry Smith   (*dm)->namedlocal = NULL;
450f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
451f490541aSPeter Brune     nnext = nlink->next;
452f490541aSPeter 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);
453f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
454f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
455f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
456f490541aSPeter Brune   }
4572348bcf4SPeter Brune 
458b17ce1afSJed Brown   /* Destroy the list of hooks */
459c833c3b5SJed Brown   {
460c833c3b5SJed Brown     DMCoarsenHookLink link,next;
461b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
462b17ce1afSJed Brown       next = link->next;
463b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
464b17ce1afSJed Brown     }
4650298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
466c833c3b5SJed Brown   }
467c833c3b5SJed Brown   {
468c833c3b5SJed Brown     DMRefineHookLink link,next;
469c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
470c833c3b5SJed Brown       next = link->next;
471c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
472c833c3b5SJed Brown     }
4730298fd71SBarry Smith     (*dm)->refinehook = NULL;
474c833c3b5SJed Brown   }
475be081cd6SPeter Brune   {
476be081cd6SPeter Brune     DMSubDomainHookLink link,next;
477be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
478be081cd6SPeter Brune       next = link->next;
479be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
480be081cd6SPeter Brune     }
4810298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
482be081cd6SPeter Brune   }
483baf369e7SPeter Brune   {
484baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
485baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
486baf369e7SPeter Brune       next = link->next;
487baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
488baf369e7SPeter Brune     }
4890298fd71SBarry Smith     (*dm)->gtolhook = NULL;
490baf369e7SPeter Brune   }
491aa1993deSMatthew G Knepley   /* Destroy the work arrays */
492aa1993deSMatthew G Knepley   {
493aa1993deSMatthew G Knepley     DMWorkLink link,next;
494aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
495aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
496aa1993deSMatthew G Knepley       next = link->next;
497aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
498aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
499aa1993deSMatthew G Knepley     }
5000298fd71SBarry Smith     (*dm)->workin = NULL;
501aa1993deSMatthew G Knepley   }
502b17ce1afSJed Brown 
50352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
50452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
50552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
50652536dc3SBarry Smith 
5071a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5081a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5091a266240SBarry Smith   }
51087e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
51171cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5124dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5136bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5146bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
515073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
51688ed4aceSMatthew G Knepley 
51788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
51888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
5198b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
52088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
52188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
522af122d2aSMatthew G Knepley 
5236636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
5246636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
5256636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
526c6b900c6SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
527c6b900c6SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
5286636e97aSMatthew G Knepley 
5292764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
53014f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
531e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
532e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
533732e2eb9SMatthew G Knepley 
5346bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
535435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
536732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
53747c6ae99SBarry Smith   PetscFunctionReturn(0);
53847c6ae99SBarry Smith }
53947c6ae99SBarry Smith 
54047c6ae99SBarry Smith #undef __FUNCT__
541d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
542d7bf68aeSBarry Smith /*@
543d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
544d7bf68aeSBarry Smith 
545d7bf68aeSBarry Smith     Collective on DM
546d7bf68aeSBarry Smith 
547d7bf68aeSBarry Smith     Input Parameter:
548d7bf68aeSBarry Smith .   dm - the DM object to setup
549d7bf68aeSBarry Smith 
550d7bf68aeSBarry Smith     Level: developer
551d7bf68aeSBarry Smith 
552e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
553d7bf68aeSBarry Smith 
554d7bf68aeSBarry Smith @*/
5557087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
556d7bf68aeSBarry Smith {
557d7bf68aeSBarry Smith   PetscErrorCode ierr;
558d7bf68aeSBarry Smith 
559d7bf68aeSBarry Smith   PetscFunctionBegin;
560171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5618387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
562d7bf68aeSBarry Smith   if (dm->ops->setup) {
563d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
564d7bf68aeSBarry Smith   }
5658387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
566d7bf68aeSBarry Smith   PetscFunctionReturn(0);
567d7bf68aeSBarry Smith }
568d7bf68aeSBarry Smith 
569d7bf68aeSBarry Smith #undef __FUNCT__
570d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
571d7bf68aeSBarry Smith /*@
572d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
573d7bf68aeSBarry Smith 
574d7bf68aeSBarry Smith     Collective on DM
575d7bf68aeSBarry Smith 
576d7bf68aeSBarry Smith     Input Parameter:
577d7bf68aeSBarry Smith .   dm - the DM object to set options for
578d7bf68aeSBarry Smith 
579732e2eb9SMatthew G Knepley     Options Database:
580dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
581dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
582171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
583171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
584732e2eb9SMatthew G Knepley 
585d7bf68aeSBarry Smith     Level: developer
586d7bf68aeSBarry Smith 
587e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
588d7bf68aeSBarry Smith 
589d7bf68aeSBarry Smith @*/
5907087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
591d7bf68aeSBarry Smith {
5927781c08eSBarry Smith   char           typeName[256];
593ca266f36SBarry Smith   PetscBool      flg;
594d7bf68aeSBarry Smith   PetscErrorCode ierr;
595d7bf68aeSBarry Smith 
596d7bf68aeSBarry Smith   PetscFunctionBegin;
597171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5983194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
5990298fd71SBarry 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);
600a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
601f9ba7244SBarry Smith   if (flg) {
602f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
603f9ba7244SBarry Smith   }
604a264d7a6SBarry 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);
605073dac72SJed Brown   if (flg) {
606521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
607073dac72SJed Brown   }
6080298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
609f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
610f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
611f9ba7244SBarry Smith   }
612f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
613f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
61482fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
615d7bf68aeSBarry Smith   PetscFunctionReturn(0);
616d7bf68aeSBarry Smith }
617d7bf68aeSBarry Smith 
618d7bf68aeSBarry Smith #undef __FUNCT__
61947c6ae99SBarry Smith #define __FUNCT__ "DMView"
620fc9bc008SSatish Balay /*@C
621aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
62247c6ae99SBarry Smith 
62347c6ae99SBarry Smith     Collective on DM
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Input Parameter:
62647c6ae99SBarry Smith +   dm - the DM object to view
62747c6ae99SBarry Smith -   v - the viewer
62847c6ae99SBarry Smith 
62947c6ae99SBarry Smith     Level: developer
63047c6ae99SBarry Smith 
631e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
63247c6ae99SBarry Smith 
63347c6ae99SBarry Smith @*/
6347087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
63547c6ae99SBarry Smith {
63647c6ae99SBarry Smith   PetscErrorCode ierr;
63732c0f0efSBarry Smith   PetscBool      isbinary;
63847c6ae99SBarry Smith 
63947c6ae99SBarry Smith   PetscFunctionBegin;
640171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6413014e516SBarry Smith   if (!v) {
642ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6433014e516SBarry Smith   }
64498c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
64532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
64632c0f0efSBarry Smith   if (isbinary) {
64755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
64832c0f0efSBarry Smith     char     type[256];
64932c0f0efSBarry Smith 
65032c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
65132c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
65232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
65332c0f0efSBarry Smith   }
6540c010503SBarry Smith   if (dm->ops->view) {
6550c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
65647c6ae99SBarry Smith   }
65747c6ae99SBarry Smith   PetscFunctionReturn(0);
65847c6ae99SBarry Smith }
65947c6ae99SBarry Smith 
66047c6ae99SBarry Smith #undef __FUNCT__
66147c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
66247c6ae99SBarry Smith /*@
663aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
66447c6ae99SBarry Smith 
66547c6ae99SBarry Smith     Collective on DM
66647c6ae99SBarry Smith 
66747c6ae99SBarry Smith     Input Parameter:
66847c6ae99SBarry Smith .   dm - the DM object
66947c6ae99SBarry Smith 
67047c6ae99SBarry Smith     Output Parameter:
67147c6ae99SBarry Smith .   vec - the global vector
67247c6ae99SBarry Smith 
673073dac72SJed Brown     Level: beginner
67447c6ae99SBarry Smith 
675e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
67647c6ae99SBarry Smith 
67747c6ae99SBarry Smith @*/
6787087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
67947c6ae99SBarry Smith {
68047c6ae99SBarry Smith   PetscErrorCode ierr;
68147c6ae99SBarry Smith 
68247c6ae99SBarry Smith   PetscFunctionBegin;
683171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
68547c6ae99SBarry Smith   PetscFunctionReturn(0);
68647c6ae99SBarry Smith }
68747c6ae99SBarry Smith 
68847c6ae99SBarry Smith #undef __FUNCT__
68947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
69047c6ae99SBarry Smith /*@
691aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
69247c6ae99SBarry Smith 
69347c6ae99SBarry Smith     Not Collective
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith     Input Parameter:
69647c6ae99SBarry Smith .   dm - the DM object
69747c6ae99SBarry Smith 
69847c6ae99SBarry Smith     Output Parameter:
69947c6ae99SBarry Smith .   vec - the local vector
70047c6ae99SBarry Smith 
701073dac72SJed Brown     Level: beginner
70247c6ae99SBarry Smith 
703e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
70447c6ae99SBarry Smith 
70547c6ae99SBarry Smith @*/
7067087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
70747c6ae99SBarry Smith {
70847c6ae99SBarry Smith   PetscErrorCode ierr;
70947c6ae99SBarry Smith 
71047c6ae99SBarry Smith   PetscFunctionBegin;
711171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71247c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
71347c6ae99SBarry Smith   PetscFunctionReturn(0);
71447c6ae99SBarry Smith }
71547c6ae99SBarry Smith 
71647c6ae99SBarry Smith #undef __FUNCT__
7171411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
7181411c6eeSJed Brown /*@
7191411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
7201411c6eeSJed Brown 
7211411c6eeSJed Brown    Collective on DM
7221411c6eeSJed Brown 
7231411c6eeSJed Brown    Input Parameter:
7241411c6eeSJed Brown .  dm - the DM that provides the mapping
7251411c6eeSJed Brown 
7261411c6eeSJed Brown    Output Parameter:
7271411c6eeSJed Brown .  ltog - the mapping
7281411c6eeSJed Brown 
7291411c6eeSJed Brown    Level: intermediate
7301411c6eeSJed Brown 
7311411c6eeSJed Brown    Notes:
7321411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
7331411c6eeSJed Brown    MatSetLocalToGlobalMapping().
7341411c6eeSJed Brown 
735fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
7361411c6eeSJed Brown @*/
7377087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7381411c6eeSJed Brown {
7391411c6eeSJed Brown   PetscErrorCode ierr;
7401411c6eeSJed Brown 
7411411c6eeSJed Brown   PetscFunctionBegin;
7421411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7431411c6eeSJed Brown   PetscValidPointer(ltog,2);
7441411c6eeSJed Brown   if (!dm->ltogmap) {
74537d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
74637d0c07bSMatthew G Knepley 
74737d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
74837d0c07bSMatthew G Knepley     if (section) {
74937d0c07bSMatthew G Knepley       PetscInt *ltog;
75037d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
75137d0c07bSMatthew G Knepley 
75237d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
75337d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
75437d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
755785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
75637d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
75737d0c07bSMatthew G Knepley         PetscInt dof, off, c;
75837d0c07bSMatthew G Knepley 
75937d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
76037d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
76137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
76237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
76337d0c07bSMatthew G Knepley           ltog[l] = off+c;
76437d0c07bSMatthew G Knepley         }
76537d0c07bSMatthew G Knepley       }
766f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
7673bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
76837d0c07bSMatthew G Knepley     } else {
769184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
770184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7711411c6eeSJed Brown     }
77237d0c07bSMatthew G Knepley   }
7731411c6eeSJed Brown   *ltog = dm->ltogmap;
7741411c6eeSJed Brown   PetscFunctionReturn(0);
7751411c6eeSJed Brown }
7761411c6eeSJed Brown 
7771411c6eeSJed Brown #undef __FUNCT__
7781411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7791411c6eeSJed Brown /*@
7801411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7811411c6eeSJed Brown 
7821411c6eeSJed Brown    Not Collective
7831411c6eeSJed Brown 
7841411c6eeSJed Brown    Input Parameter:
7851411c6eeSJed Brown .  dm - the DM with block structure
7861411c6eeSJed Brown 
7871411c6eeSJed Brown    Output Parameter:
7881411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7891411c6eeSJed Brown 
7901411c6eeSJed Brown    Level: intermediate
7911411c6eeSJed Brown 
792fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
7931411c6eeSJed Brown @*/
7947087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7951411c6eeSJed Brown {
7961411c6eeSJed Brown   PetscFunctionBegin;
7971411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7981411c6eeSJed Brown   PetscValidPointer(bs,2);
7991411c6eeSJed 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");
8001411c6eeSJed Brown   *bs = dm->bs;
8011411c6eeSJed Brown   PetscFunctionReturn(0);
8021411c6eeSJed Brown }
8031411c6eeSJed Brown 
8041411c6eeSJed Brown #undef __FUNCT__
805e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
80647c6ae99SBarry Smith /*@
807e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith     Collective on DM
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith     Input Parameter:
81247c6ae99SBarry Smith +   dm1 - the DM object
81347c6ae99SBarry Smith -   dm2 - the second, finer DM object
81447c6ae99SBarry Smith 
81547c6ae99SBarry Smith     Output Parameter:
81647c6ae99SBarry Smith +  mat - the interpolation
81747c6ae99SBarry Smith -  vec - the scaling (optional)
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith     Level: developer
82047c6ae99SBarry Smith 
82185afcc9aSBarry 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
82285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
823d52bd9f3SBarry Smith 
8241f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
825d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
82685afcc9aSBarry Smith 
82785afcc9aSBarry Smith 
828e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
82947c6ae99SBarry Smith 
83047c6ae99SBarry Smith @*/
831e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
83247c6ae99SBarry Smith {
83347c6ae99SBarry Smith   PetscErrorCode ierr;
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith   PetscFunctionBegin;
836171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
837171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83825296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
83947c6ae99SBarry Smith   PetscFunctionReturn(0);
84047c6ae99SBarry Smith }
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith #undef __FUNCT__
843e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
84447c6ae99SBarry Smith /*@
845e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
84647c6ae99SBarry Smith 
84747c6ae99SBarry Smith     Collective on DM
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith     Input Parameter:
85047c6ae99SBarry Smith +   dm1 - the DM object
85147c6ae99SBarry Smith -   dm2 - the second, finer DM object
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Output Parameter:
85447c6ae99SBarry Smith .   ctx - the injection
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith     Level: developer
85747c6ae99SBarry Smith 
85885afcc9aSBarry 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
85985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
86085afcc9aSBarry Smith 
861e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith @*/
864e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
86547c6ae99SBarry Smith {
86647c6ae99SBarry Smith   PetscErrorCode ierr;
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith   PetscFunctionBegin;
869171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
870171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
87147c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
87247c6ae99SBarry Smith   PetscFunctionReturn(0);
87347c6ae99SBarry Smith }
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith #undef __FUNCT__
876e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
877b412c318SBarry Smith /*@
878b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Collective on DM
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Input Parameter:
88347c6ae99SBarry Smith +   dm - the DM object
884b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith     Output Parameter:
88747c6ae99SBarry Smith .   coloring - the coloring
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith     Level: developer
89047c6ae99SBarry Smith 
891b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
89247c6ae99SBarry Smith 
893aab9d709SJed Brown @*/
894b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
89547c6ae99SBarry Smith {
89647c6ae99SBarry Smith   PetscErrorCode ierr;
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith   PetscFunctionBegin;
899171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
900ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
901b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
90247c6ae99SBarry Smith   PetscFunctionReturn(0);
90347c6ae99SBarry Smith }
90447c6ae99SBarry Smith 
90547c6ae99SBarry Smith #undef __FUNCT__
906950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
907b412c318SBarry Smith /*@
908950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
90947c6ae99SBarry Smith 
91047c6ae99SBarry Smith     Collective on DM
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Input Parameter:
913b412c318SBarry Smith .   dm - the DM object
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith     Output Parameter:
91647c6ae99SBarry Smith .   mat - the empty Jacobian
91747c6ae99SBarry Smith 
918073dac72SJed Brown     Level: beginner
91947c6ae99SBarry Smith 
92094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
92194013140SBarry Smith        do not need to do it yourself.
92294013140SBarry Smith 
92394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
924aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
92594013140SBarry Smith 
92694013140SBarry 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
92794013140SBarry Smith        internally by PETSc.
92894013140SBarry Smith 
92994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
930aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
93194013140SBarry Smith 
932b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
93347c6ae99SBarry Smith 
934aab9d709SJed Brown @*/
935b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
93647c6ae99SBarry Smith {
93747c6ae99SBarry Smith   PetscErrorCode ierr;
93847c6ae99SBarry Smith 
93947c6ae99SBarry Smith   PetscFunctionBegin;
940171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
941607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
942c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
943c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
944b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
94547c6ae99SBarry Smith   PetscFunctionReturn(0);
94647c6ae99SBarry Smith }
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith #undef __FUNCT__
949732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
950732e2eb9SMatthew G Knepley /*@
951950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
952732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
953732e2eb9SMatthew G Knepley 
954732e2eb9SMatthew G Knepley   Logically Collective on DMDA
955732e2eb9SMatthew G Knepley 
956732e2eb9SMatthew G Knepley   Input Parameter:
957732e2eb9SMatthew G Knepley + dm - the DM
958732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
959732e2eb9SMatthew G Knepley 
960732e2eb9SMatthew G Knepley   Level: developer
961950540a4SJed Brown .seealso DMCreateMatrix()
962732e2eb9SMatthew G Knepley @*/
963732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
964732e2eb9SMatthew G Knepley {
965732e2eb9SMatthew G Knepley   PetscFunctionBegin;
966732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
967732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
968732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
969732e2eb9SMatthew G Knepley }
970732e2eb9SMatthew G Knepley 
971732e2eb9SMatthew G Knepley #undef __FUNCT__
972a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
973a89ea682SMatthew G Knepley /*@C
974aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
975a89ea682SMatthew G Knepley 
976a89ea682SMatthew G Knepley   Not Collective
977a89ea682SMatthew G Knepley 
978a89ea682SMatthew G Knepley   Input Parameters:
979a89ea682SMatthew G Knepley + dm - the DM object
980aa1993deSMatthew G Knepley . count - The minium size
981aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
982a89ea682SMatthew G Knepley 
983a89ea682SMatthew G Knepley   Output Parameter:
984a89ea682SMatthew G Knepley . array - the work array
985a89ea682SMatthew G Knepley 
986a89ea682SMatthew G Knepley   Level: developer
987a89ea682SMatthew G Knepley 
988a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
989a89ea682SMatthew G Knepley @*/
990aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
991a89ea682SMatthew G Knepley {
992a89ea682SMatthew G Knepley   PetscErrorCode ierr;
993aa1993deSMatthew G Knepley   DMWorkLink     link;
994aa1993deSMatthew G Knepley   size_t         size;
995a89ea682SMatthew G Knepley 
996a89ea682SMatthew G Knepley   PetscFunctionBegin;
997a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
998aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
999aa1993deSMatthew G Knepley   if (dm->workin) {
1000aa1993deSMatthew G Knepley     link       = dm->workin;
1001aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1002aa1993deSMatthew G Knepley   } else {
1003b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1004a89ea682SMatthew G Knepley   }
1005aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1006aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1007aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1008aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1009aa1993deSMatthew G Knepley     link->bytes = size*count;
1010aa1993deSMatthew G Knepley   }
1011aa1993deSMatthew G Knepley   link->next   = dm->workout;
1012aa1993deSMatthew G Knepley   dm->workout  = link;
1013aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1014a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1015a89ea682SMatthew G Knepley }
1016a89ea682SMatthew G Knepley 
1017aa1993deSMatthew G Knepley #undef __FUNCT__
1018aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1019aa1993deSMatthew G Knepley /*@C
1020aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1021aa1993deSMatthew G Knepley 
1022aa1993deSMatthew G Knepley   Not Collective
1023aa1993deSMatthew G Knepley 
1024aa1993deSMatthew G Knepley   Input Parameters:
1025aa1993deSMatthew G Knepley + dm - the DM object
1026aa1993deSMatthew G Knepley . count - The minium size
1027aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1028aa1993deSMatthew G Knepley 
1029aa1993deSMatthew G Knepley   Output Parameter:
1030aa1993deSMatthew G Knepley . array - the work array
1031aa1993deSMatthew G Knepley 
1032aa1993deSMatthew G Knepley   Level: developer
1033aa1993deSMatthew G Knepley 
1034aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1035aa1993deSMatthew G Knepley @*/
1036aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1037aa1993deSMatthew G Knepley {
1038aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1039aa1993deSMatthew G Knepley 
1040aa1993deSMatthew G Knepley   PetscFunctionBegin;
1041aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1042aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1043aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1044aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1045aa1993deSMatthew G Knepley       *p           = link->next;
1046aa1993deSMatthew G Knepley       link->next   = dm->workin;
1047aa1993deSMatthew G Knepley       dm->workin   = link;
10480298fd71SBarry Smith       *(void**)mem = NULL;
1049aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1050aa1993deSMatthew G Knepley     }
1051aa1993deSMatthew G Knepley   }
1052aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1053aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1054aa1993deSMatthew G Knepley }
1055e7c4fc90SDmitry Karpeev 
1056e7c4fc90SDmitry Karpeev #undef __FUNCT__
1057435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1058435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1059435a35e8SMatthew G Knepley {
1060435a35e8SMatthew G Knepley   PetscFunctionBegin;
1061435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
106282f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1063435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1064435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1065435a35e8SMatthew G Knepley }
1066435a35e8SMatthew G Knepley 
1067435a35e8SMatthew G Knepley #undef __FUNCT__
10684d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10694f3b5142SJed Brown /*@C
10704d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10714d343eeaSMatthew G Knepley 
10724d343eeaSMatthew G Knepley   Not collective
10734d343eeaSMatthew G Knepley 
10744d343eeaSMatthew G Knepley   Input Parameter:
10754d343eeaSMatthew G Knepley . dm - the DM object
10764d343eeaSMatthew G Knepley 
10774d343eeaSMatthew G Knepley   Output Parameters:
10780298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10790298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10800298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10814d343eeaSMatthew G Knepley 
10824d343eeaSMatthew G Knepley   Level: intermediate
10834d343eeaSMatthew G Knepley 
108421c9b008SJed Brown   Notes:
108521c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
108621c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
108721c9b008SJed Brown   PetscFree().
108821c9b008SJed Brown 
10894d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10904d343eeaSMatthew G Knepley @*/
109137d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10924d343eeaSMatthew G Knepley {
109337d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10944d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10954d343eeaSMatthew G Knepley 
10964d343eeaSMatthew G Knepley   PetscFunctionBegin;
10974d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
109869ca1f37SDmitry Karpeev   if (numFields) {
109969ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
110069ca1f37SDmitry Karpeev     *numFields = 0;
110169ca1f37SDmitry Karpeev   }
110237d0c07bSMatthew G Knepley   if (fieldNames) {
110337d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11040298fd71SBarry Smith     *fieldNames = NULL;
110569ca1f37SDmitry Karpeev   }
110669ca1f37SDmitry Karpeev   if (fields) {
110769ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11080298fd71SBarry Smith     *fields = NULL;
110969ca1f37SDmitry Karpeev   }
111037d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
111137d0c07bSMatthew G Knepley   if (section) {
111237d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
111337d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
111437d0c07bSMatthew G Knepley 
111537d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
111637d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1117dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
111837d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
111937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
112037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
112137d0c07bSMatthew G Knepley     }
112237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
112337d0c07bSMatthew G Knepley       PetscInt gdof;
112437d0c07bSMatthew G Knepley 
112537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112637d0c07bSMatthew G Knepley       if (gdof > 0) {
112737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
112937d0c07bSMatthew G Knepley 
113037d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
113137d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
113237d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
113337d0c07bSMatthew G Knepley         }
113437d0c07bSMatthew G Knepley       }
113537d0c07bSMatthew G Knepley     }
113637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1137785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
113837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113937d0c07bSMatthew G Knepley     }
114037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
114137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
114237d0c07bSMatthew G Knepley 
114337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114437d0c07bSMatthew G Knepley       if (gdof > 0) {
114537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
114637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
114837d0c07bSMatthew G Knepley 
114937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
115037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
115137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
115237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
115337d0c07bSMatthew G Knepley           }
115437d0c07bSMatthew G Knepley         }
115537d0c07bSMatthew G Knepley       }
115637d0c07bSMatthew G Knepley     }
11578865f1eaSKarl Rupp     if (numFields) *numFields = nF;
115837d0c07bSMatthew G Knepley     if (fieldNames) {
1159785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
116037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116137d0c07bSMatthew G Knepley         const char *fieldName;
116237d0c07bSMatthew G Knepley 
116337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
116437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
116537d0c07bSMatthew G Knepley       }
116637d0c07bSMatthew G Knepley     }
116737d0c07bSMatthew G Knepley     if (fields) {
1168785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
116937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
117082f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
117137d0c07bSMatthew G Knepley       }
117237d0c07bSMatthew G Knepley     }
117337d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11748865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11758865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
117669ca1f37SDmitry Karpeev   }
11774d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11784d343eeaSMatthew G Knepley }
11794d343eeaSMatthew G Knepley 
118016621825SDmitry Karpeev 
118116621825SDmitry Karpeev #undef __FUNCT__
118216621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
118316621825SDmitry Karpeev /*@C
118416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
118516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
118616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1187e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1188e7c4fc90SDmitry Karpeev 
1189e7c4fc90SDmitry Karpeev   Not collective
1190e7c4fc90SDmitry Karpeev 
1191e7c4fc90SDmitry Karpeev   Input Parameter:
1192e7c4fc90SDmitry Karpeev . dm - the DM object
1193e7c4fc90SDmitry Karpeev 
1194e7c4fc90SDmitry Karpeev   Output Parameters:
11950298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
11960298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
11970298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
11980298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1199e7c4fc90SDmitry Karpeev 
1200e7c4fc90SDmitry Karpeev   Level: intermediate
1201e7c4fc90SDmitry Karpeev 
1202e7c4fc90SDmitry Karpeev   Notes:
1203e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1204e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1205e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1206e7c4fc90SDmitry Karpeev 
1207e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1208e7c4fc90SDmitry Karpeev @*/
120916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1210e7c4fc90SDmitry Karpeev {
1211e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1212e7c4fc90SDmitry Karpeev 
1213e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1214e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12158865f1eaSKarl Rupp   if (len) {
12168865f1eaSKarl Rupp     PetscValidPointer(len,2);
12178865f1eaSKarl Rupp     *len = 0;
12188865f1eaSKarl Rupp   }
12198865f1eaSKarl Rupp   if (namelist) {
12208865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12218865f1eaSKarl Rupp     *namelist = 0;
12228865f1eaSKarl Rupp   }
12238865f1eaSKarl Rupp   if (islist) {
12248865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12258865f1eaSKarl Rupp     *islist = 0;
12268865f1eaSKarl Rupp   }
12278865f1eaSKarl Rupp   if (dmlist) {
12288865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12298865f1eaSKarl Rupp     *dmlist = 0;
12308865f1eaSKarl Rupp   }
1231f3f0edfdSDmitry Karpeev   /*
1232f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1233f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1234f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1235f3f0edfdSDmitry Karpeev    */
1236ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
123716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1238435a35e8SMatthew G Knepley     PetscSection section;
1239435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1240435a35e8SMatthew G Knepley 
1241435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1242435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1243435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1244435a35e8SMatthew G Knepley       *len = numFields;
124503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
124603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
124703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1248435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1249435a35e8SMatthew G Knepley         const char *fieldName;
1250435a35e8SMatthew G Knepley 
125103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
125203dc3394SMatthew G. Knepley         if (namelist) {
1253435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1254435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1255435a35e8SMatthew G Knepley         }
125603dc3394SMatthew G. Knepley       }
1257435a35e8SMatthew G Knepley     } else {
125869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1259e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12600298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1261e7c4fc90SDmitry Karpeev     }
12628865f1eaSKarl Rupp   } else {
126316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
126416621825SDmitry Karpeev   }
126516621825SDmitry Karpeev   PetscFunctionReturn(0);
126616621825SDmitry Karpeev }
126716621825SDmitry Karpeev 
126816621825SDmitry Karpeev #undef __FUNCT__
1269435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1270435a35e8SMatthew G Knepley /*@C
1271435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1272435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1273435a35e8SMatthew G Knepley 
1274435a35e8SMatthew G Knepley   Not collective
1275435a35e8SMatthew G Knepley 
1276435a35e8SMatthew G Knepley   Input Parameters:
1277435a35e8SMatthew G Knepley + dm - the DM object
1278435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12790298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1280435a35e8SMatthew G Knepley 
1281435a35e8SMatthew G Knepley   Output Parameters:
1282435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1283435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1284435a35e8SMatthew G Knepley 
1285435a35e8SMatthew G Knepley   Level: intermediate
1286435a35e8SMatthew G Knepley 
1287435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1288435a35e8SMatthew G Knepley @*/
1289435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1290435a35e8SMatthew G Knepley {
1291435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1292435a35e8SMatthew G Knepley 
1293435a35e8SMatthew G Knepley   PetscFunctionBegin;
1294435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1295435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
12968865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
12978865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1298435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1299435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
130082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1301435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1302435a35e8SMatthew G Knepley }
1303435a35e8SMatthew G Knepley 
130416621825SDmitry Karpeev 
130516621825SDmitry Karpeev #undef __FUNCT__
130616621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
130716621825SDmitry Karpeev /*@C
13088d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13098d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13108d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13118d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13128d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
131316621825SDmitry Karpeev 
131416621825SDmitry Karpeev   Not collective
131516621825SDmitry Karpeev 
131616621825SDmitry Karpeev   Input Parameter:
131716621825SDmitry Karpeev . dm - the DM object
131816621825SDmitry Karpeev 
131916621825SDmitry Karpeev   Output Parameters:
13200298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13210298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13220298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13230298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13240298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
132516621825SDmitry Karpeev 
132616621825SDmitry Karpeev   Level: intermediate
132716621825SDmitry Karpeev 
132816621825SDmitry Karpeev   Notes:
132916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
133016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
133116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
133216621825SDmitry Karpeev 
13338d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
133416621825SDmitry Karpeev @*/
13358d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
133616621825SDmitry Karpeev {
133716621825SDmitry Karpeev   PetscErrorCode      ierr;
1338be081cd6SPeter Brune   DMSubDomainHookLink link;
1339be081cd6SPeter Brune   PetscInt            i,l;
134016621825SDmitry Karpeev 
134116621825SDmitry Karpeev   PetscFunctionBegin;
134216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134314a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13440298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13450298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13460298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13470298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1348f3f0edfdSDmitry Karpeev   /*
1349f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1350f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1351f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1352f3f0edfdSDmitry Karpeev    */
1353ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
135416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1355be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
135614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
135714a18fd3SPeter Brune     if (dmlist) {
1358be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1359be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1360be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1361be081cd6SPeter Brune         }
1362d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1363e7c4fc90SDmitry Karpeev       }
136414a18fd3SPeter Brune     }
136514a18fd3SPeter Brune     if (len) *len = l;
136614a18fd3SPeter Brune   }
1367e30e807fSPeter Brune   PetscFunctionReturn(0);
1368e30e807fSPeter Brune }
1369e30e807fSPeter Brune 
1370e30e807fSPeter Brune 
1371e30e807fSPeter Brune #undef __FUNCT__
1372e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1373e30e807fSPeter Brune /*@C
1374e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1375e30e807fSPeter Brune 
1376e30e807fSPeter Brune   Not collective
1377e30e807fSPeter Brune 
1378e30e807fSPeter Brune   Input Parameters:
1379e30e807fSPeter Brune + dm - the DM object
1380e30e807fSPeter Brune . n  - the number of subdomain scatters
1381e30e807fSPeter Brune - subdms - the local subdomains
1382e30e807fSPeter Brune 
1383e30e807fSPeter Brune   Output Parameters:
1384e30e807fSPeter Brune + n     - the number of scatters returned
1385e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1386e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1387e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1388e30e807fSPeter Brune 
1389e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1390e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1391e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1392e30e807fSPeter Brune   solution and residual data.
1393e30e807fSPeter Brune 
1394e30e807fSPeter Brune   Level: developer
1395e30e807fSPeter Brune 
1396e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1397e30e807fSPeter Brune @*/
1398e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1399e30e807fSPeter Brune {
1400e30e807fSPeter Brune   PetscErrorCode ierr;
1401e30e807fSPeter Brune 
1402e30e807fSPeter Brune   PetscFunctionBegin;
1403e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1404e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1405e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1406e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
140782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1408e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1409e7c4fc90SDmitry Karpeev }
1410e7c4fc90SDmitry Karpeev 
1411731c8d9eSDmitry Karpeev #undef __FUNCT__
141247c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
141347c6ae99SBarry Smith /*@
141447c6ae99SBarry Smith   DMRefine - Refines a DM object
141547c6ae99SBarry Smith 
141647c6ae99SBarry Smith   Collective on DM
141747c6ae99SBarry Smith 
141847c6ae99SBarry Smith   Input Parameter:
141947c6ae99SBarry Smith + dm   - the DM object
142091d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
142147c6ae99SBarry Smith 
142247c6ae99SBarry Smith   Output Parameter:
14230298fd71SBarry Smith . dmf - the refined DM, or NULL
1424ae0a1c52SMatthew G Knepley 
14250298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
142647c6ae99SBarry Smith 
142747c6ae99SBarry Smith   Level: developer
142847c6ae99SBarry Smith 
1429e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
143047c6ae99SBarry Smith @*/
14317087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
143247c6ae99SBarry Smith {
143347c6ae99SBarry Smith   PetscErrorCode   ierr;
1434c833c3b5SJed Brown   DMRefineHookLink link;
143547c6ae99SBarry Smith 
143647c6ae99SBarry Smith   PetscFunctionBegin;
1437732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14394057135bSMatthew G Knepley   if (*dmf) {
144043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14418865f1eaSKarl Rupp 
14428cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14438865f1eaSKarl Rupp 
1444644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14450598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1446656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14478865f1eaSKarl Rupp 
1448e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1449c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14508865f1eaSKarl Rupp       if (link->refinehook) {
14518865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14528865f1eaSKarl Rupp       }
1453c833c3b5SJed Brown     }
1454c833c3b5SJed Brown   }
1455c833c3b5SJed Brown   PetscFunctionReturn(0);
1456c833c3b5SJed Brown }
1457c833c3b5SJed Brown 
1458c833c3b5SJed Brown #undef __FUNCT__
1459c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1460bb9467b5SJed Brown /*@C
1461c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1462c833c3b5SJed Brown 
1463c833c3b5SJed Brown    Logically Collective
1464c833c3b5SJed Brown 
1465c833c3b5SJed Brown    Input Arguments:
1466c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1467c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1468c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14690298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1470c833c3b5SJed Brown 
1471c833c3b5SJed Brown    Calling sequence of refinehook:
1472c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1473c833c3b5SJed Brown 
1474c833c3b5SJed Brown +  coarse - coarse level DM
1475c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1476c833c3b5SJed Brown -  ctx - optional user-defined function context
1477c833c3b5SJed Brown 
1478c833c3b5SJed Brown    Calling sequence for interphook:
1479c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1480c833c3b5SJed Brown 
1481c833c3b5SJed Brown +  coarse - coarse level DM
1482c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1483c833c3b5SJed Brown .  fine - fine level DM to update
1484c833c3b5SJed Brown -  ctx - optional user-defined function context
1485c833c3b5SJed Brown 
1486c833c3b5SJed Brown    Level: advanced
1487c833c3b5SJed Brown 
1488c833c3b5SJed Brown    Notes:
1489c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1490c833c3b5SJed Brown 
1491c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1492c833c3b5SJed Brown 
1493bb9467b5SJed Brown    This function is currently not available from Fortran.
1494bb9467b5SJed Brown 
1495c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1496c833c3b5SJed Brown @*/
1497c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1498c833c3b5SJed Brown {
1499c833c3b5SJed Brown   PetscErrorCode   ierr;
1500c833c3b5SJed Brown   DMRefineHookLink link,*p;
1501c833c3b5SJed Brown 
1502c833c3b5SJed Brown   PetscFunctionBegin;
1503c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1504c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1505c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1506c833c3b5SJed Brown   link->refinehook = refinehook;
1507c833c3b5SJed Brown   link->interphook = interphook;
1508c833c3b5SJed Brown   link->ctx        = ctx;
15090298fd71SBarry Smith   link->next       = NULL;
1510c833c3b5SJed Brown   *p               = link;
1511c833c3b5SJed Brown   PetscFunctionReturn(0);
1512c833c3b5SJed Brown }
1513c833c3b5SJed Brown 
1514c833c3b5SJed Brown #undef __FUNCT__
1515c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1516c833c3b5SJed Brown /*@
1517c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1518c833c3b5SJed Brown 
1519c833c3b5SJed Brown    Collective if any hooks are
1520c833c3b5SJed Brown 
1521c833c3b5SJed Brown    Input Arguments:
1522c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1523c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1524c833c3b5SJed Brown -  fine - finer DM to update
1525c833c3b5SJed Brown 
1526c833c3b5SJed Brown    Level: developer
1527c833c3b5SJed Brown 
1528c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1529c833c3b5SJed Brown @*/
1530c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1531c833c3b5SJed Brown {
1532c833c3b5SJed Brown   PetscErrorCode   ierr;
1533c833c3b5SJed Brown   DMRefineHookLink link;
1534c833c3b5SJed Brown 
1535c833c3b5SJed Brown   PetscFunctionBegin;
1536c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15378865f1eaSKarl Rupp     if (link->interphook) {
15388865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15398865f1eaSKarl Rupp     }
15404057135bSMatthew G Knepley   }
154147c6ae99SBarry Smith   PetscFunctionReturn(0);
154247c6ae99SBarry Smith }
154347c6ae99SBarry Smith 
154447c6ae99SBarry Smith #undef __FUNCT__
1545eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1546eb3f98d2SBarry Smith /*@
1547eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1548eb3f98d2SBarry Smith 
1549eb3f98d2SBarry Smith     Not Collective
1550eb3f98d2SBarry Smith 
1551eb3f98d2SBarry Smith     Input Parameter:
1552eb3f98d2SBarry Smith .   dm - the DM object
1553eb3f98d2SBarry Smith 
1554eb3f98d2SBarry Smith     Output Parameter:
1555eb3f98d2SBarry Smith .   level - number of refinements
1556eb3f98d2SBarry Smith 
1557eb3f98d2SBarry Smith     Level: developer
1558eb3f98d2SBarry Smith 
15596a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1560eb3f98d2SBarry Smith 
1561eb3f98d2SBarry Smith @*/
1562eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1563eb3f98d2SBarry Smith {
1564eb3f98d2SBarry Smith   PetscFunctionBegin;
1565eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1566eb3f98d2SBarry Smith   *level = dm->levelup;
1567eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1568eb3f98d2SBarry Smith }
1569eb3f98d2SBarry Smith 
1570eb3f98d2SBarry Smith #undef __FUNCT__
1571baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1572bb9467b5SJed Brown /*@C
1573baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1574baf369e7SPeter Brune 
1575baf369e7SPeter Brune    Logically Collective
1576baf369e7SPeter Brune 
1577baf369e7SPeter Brune    Input Arguments:
1578baf369e7SPeter Brune +  dm - the DM
1579baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1580baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15810298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1582baf369e7SPeter Brune 
1583baf369e7SPeter Brune    Calling sequence for beginhook:
1584baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1585baf369e7SPeter Brune 
1586baf369e7SPeter Brune +  dm - global DM
1587baf369e7SPeter Brune .  g - global vector
1588baf369e7SPeter Brune .  mode - mode
1589baf369e7SPeter Brune .  l - local vector
1590baf369e7SPeter Brune -  ctx - optional user-defined function context
1591baf369e7SPeter Brune 
1592baf369e7SPeter Brune 
1593baf369e7SPeter Brune    Calling sequence for endhook:
1594ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1595baf369e7SPeter Brune 
1596baf369e7SPeter Brune +  global - global DM
1597baf369e7SPeter Brune -  ctx - optional user-defined function context
1598baf369e7SPeter Brune 
1599baf369e7SPeter Brune    Level: advanced
1600baf369e7SPeter Brune 
1601baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1602baf369e7SPeter Brune @*/
1603baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1604baf369e7SPeter Brune {
1605baf369e7SPeter Brune   PetscErrorCode          ierr;
1606baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1607baf369e7SPeter Brune 
1608baf369e7SPeter Brune   PetscFunctionBegin;
1609baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1610baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1611baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1612baf369e7SPeter Brune   link->beginhook = beginhook;
1613baf369e7SPeter Brune   link->endhook   = endhook;
1614baf369e7SPeter Brune   link->ctx       = ctx;
16150298fd71SBarry Smith   link->next      = NULL;
1616baf369e7SPeter Brune   *p              = link;
1617baf369e7SPeter Brune   PetscFunctionReturn(0);
1618baf369e7SPeter Brune }
1619baf369e7SPeter Brune 
1620baf369e7SPeter Brune #undef __FUNCT__
162147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
162247c6ae99SBarry Smith /*@
162347c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
162447c6ae99SBarry Smith 
162547c6ae99SBarry Smith     Neighbor-wise Collective on DM
162647c6ae99SBarry Smith 
162747c6ae99SBarry Smith     Input Parameters:
162847c6ae99SBarry Smith +   dm - the DM object
162947c6ae99SBarry Smith .   g - the global vector
163047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
163147c6ae99SBarry Smith -   l - the local vector
163247c6ae99SBarry Smith 
163347c6ae99SBarry Smith 
163447c6ae99SBarry Smith     Level: beginner
163547c6ae99SBarry Smith 
1636e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
163747c6ae99SBarry Smith 
163847c6ae99SBarry Smith @*/
16397087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
164047c6ae99SBarry Smith {
16417128ae9fSMatthew G Knepley   PetscSF                 sf;
164247c6ae99SBarry Smith   PetscErrorCode          ierr;
1643baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
164447c6ae99SBarry Smith 
164547c6ae99SBarry Smith   PetscFunctionBegin;
1646171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1647baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16488865f1eaSKarl Rupp     if (link->beginhook) {
16498865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16508865f1eaSKarl Rupp     }
1651baf369e7SPeter Brune   }
16527128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16537128ae9fSMatthew G Knepley   if (sf) {
16547128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16557128ae9fSMatthew G Knepley 
165682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16577128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16587128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16597128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16607128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16617128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16627128ae9fSMatthew G Knepley   } else {
1663843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16647128ae9fSMatthew G Knepley   }
166547c6ae99SBarry Smith   PetscFunctionReturn(0);
166647c6ae99SBarry Smith }
166747c6ae99SBarry Smith 
166847c6ae99SBarry Smith #undef __FUNCT__
166947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
167047c6ae99SBarry Smith /*@
167147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
167247c6ae99SBarry Smith 
167347c6ae99SBarry Smith     Neighbor-wise Collective on DM
167447c6ae99SBarry Smith 
167547c6ae99SBarry Smith     Input Parameters:
167647c6ae99SBarry Smith +   dm - the DM object
167747c6ae99SBarry Smith .   g - the global vector
167847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
167947c6ae99SBarry Smith -   l - the local vector
168047c6ae99SBarry Smith 
168147c6ae99SBarry Smith 
168247c6ae99SBarry Smith     Level: beginner
168347c6ae99SBarry Smith 
1684e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
168547c6ae99SBarry Smith 
168647c6ae99SBarry Smith @*/
16877087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
168847c6ae99SBarry Smith {
16897128ae9fSMatthew G Knepley   PetscSF                 sf;
169047c6ae99SBarry Smith   PetscErrorCode          ierr;
169161a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1692baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
169347c6ae99SBarry Smith 
169447c6ae99SBarry Smith   PetscFunctionBegin;
1695171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16967128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16977128ae9fSMatthew G Knepley   if (sf) {
169882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16997128ae9fSMatthew G Knepley 
17007128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17017128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17027128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17037128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17047128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17057128ae9fSMatthew G Knepley   } else {
1706843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17077128ae9fSMatthew G Knepley   }
1708baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1709baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1710baf369e7SPeter Brune   }
171147c6ae99SBarry Smith   PetscFunctionReturn(0);
171247c6ae99SBarry Smith }
171347c6ae99SBarry Smith 
171447c6ae99SBarry Smith #undef __FUNCT__
17159a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
171647c6ae99SBarry Smith /*@
17179a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
17189a42bb27SBarry Smith 
17199a42bb27SBarry Smith     Neighbor-wise Collective on DM
17209a42bb27SBarry Smith 
17219a42bb27SBarry Smith     Input Parameters:
17229a42bb27SBarry Smith +   dm - the DM object
1723f6813fd5SJed Brown .   l - the local vector
17249a42bb27SBarry 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
17259a42bb27SBarry Smith            base point.
1726f6813fd5SJed Brown - - the global vector
17279a42bb27SBarry Smith 
17289a42bb27SBarry 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
17299a42bb27SBarry 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
17309a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17319a42bb27SBarry Smith 
17329a42bb27SBarry Smith     Level: beginner
17339a42bb27SBarry Smith 
1734e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17359a42bb27SBarry Smith 
17369a42bb27SBarry Smith @*/
17377087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17389a42bb27SBarry Smith {
17397128ae9fSMatthew G Knepley   PetscSF        sf;
17409a42bb27SBarry Smith   PetscErrorCode ierr;
17419a42bb27SBarry Smith 
17429a42bb27SBarry Smith   PetscFunctionBegin;
1743171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17447128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17457128ae9fSMatthew G Knepley   if (sf) {
17467128ae9fSMatthew G Knepley     MPI_Op      op;
17477128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17487128ae9fSMatthew G Knepley 
17497128ae9fSMatthew G Knepley     switch (mode) {
17507128ae9fSMatthew G Knepley     case INSERT_VALUES:
17517128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17528bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17537128ae9fSMatthew G Knepley     case ADD_VALUES:
17547128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17557128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17567128ae9fSMatthew G Knepley     default:
175782f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17587128ae9fSMatthew G Knepley     }
17597128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17607128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17617128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17627128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17637128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17647128ae9fSMatthew G Knepley   } else {
1765843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17667128ae9fSMatthew G Knepley   }
17679a42bb27SBarry Smith   PetscFunctionReturn(0);
17689a42bb27SBarry Smith }
17699a42bb27SBarry Smith 
17709a42bb27SBarry Smith #undef __FUNCT__
17719a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17729a42bb27SBarry Smith /*@
17739a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
177447c6ae99SBarry Smith 
177547c6ae99SBarry Smith     Neighbor-wise Collective on DM
177647c6ae99SBarry Smith 
177747c6ae99SBarry Smith     Input Parameters:
177847c6ae99SBarry Smith +   dm - the DM object
1779f6813fd5SJed Brown .   l - the local vector
178047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1781f6813fd5SJed Brown -   g - the global vector
178247c6ae99SBarry Smith 
178347c6ae99SBarry Smith 
178447c6ae99SBarry Smith     Level: beginner
178547c6ae99SBarry Smith 
1786e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
178747c6ae99SBarry Smith 
178847c6ae99SBarry Smith @*/
17897087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
179047c6ae99SBarry Smith {
17917128ae9fSMatthew G Knepley   PetscSF        sf;
179247c6ae99SBarry Smith   PetscErrorCode ierr;
179347c6ae99SBarry Smith 
179447c6ae99SBarry Smith   PetscFunctionBegin;
1795171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17967128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17977128ae9fSMatthew G Knepley   if (sf) {
17987128ae9fSMatthew G Knepley     MPI_Op      op;
17997128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
18007128ae9fSMatthew G Knepley 
18017128ae9fSMatthew G Knepley     switch (mode) {
18027128ae9fSMatthew G Knepley     case INSERT_VALUES:
18037128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18048bfbc91cSJed Brown       op = MPIU_REPLACE; break;
18057128ae9fSMatthew G Knepley     case ADD_VALUES:
18067128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18077128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18087128ae9fSMatthew G Knepley     default:
180982f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18107128ae9fSMatthew G Knepley     }
18117128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18127128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18137128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18147128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18157128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18167128ae9fSMatthew G Knepley   } else {
1817843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18187128ae9fSMatthew G Knepley   }
181947c6ae99SBarry Smith   PetscFunctionReturn(0);
182047c6ae99SBarry Smith }
182147c6ae99SBarry Smith 
182247c6ae99SBarry Smith #undef __FUNCT__
1823f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
1824f089877aSRichard Tran Mills /*@
1825bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
1826bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1827d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
1828f089877aSRichard Tran Mills 
1829bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1830f089877aSRichard Tran Mills 
1831f089877aSRichard Tran Mills    Input Parameters:
1832f089877aSRichard Tran Mills +  dm - the DM object
1833bc0a1609SRichard Tran Mills .  g - the original local vector
1834bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1835f089877aSRichard Tran Mills 
1836bc0a1609SRichard Tran Mills    Output Parameter:
1837bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1838f089877aSRichard Tran Mills 
1839f089877aSRichard Tran Mills    Level: intermediate
1840f089877aSRichard Tran Mills 
1841bc0a1609SRichard Tran Mills    Notes:
1842bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1843bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1844bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1845bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1846bc0a1609SRichard Tran Mills 
1847bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
1848bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1849f089877aSRichard Tran Mills 
1850f089877aSRichard Tran Mills @*/
1851f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
1852f089877aSRichard Tran Mills {
1853f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1854f089877aSRichard Tran Mills 
1855f089877aSRichard Tran Mills   PetscFunctionBegin;
1856f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1857f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1858f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1859f089877aSRichard Tran Mills }
1860f089877aSRichard Tran Mills 
1861f089877aSRichard Tran Mills #undef __FUNCT__
1862f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
1863f089877aSRichard Tran Mills /*@
1864bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
1865bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1866d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
1867f089877aSRichard Tran Mills 
1868bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1869f089877aSRichard Tran Mills 
1870f089877aSRichard Tran Mills    Input Parameters:
1871bc0a1609SRichard Tran Mills +  da - the DM object
1872bc0a1609SRichard Tran Mills .  g - the original local vector
1873bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1874f089877aSRichard Tran Mills 
1875bc0a1609SRichard Tran Mills    Output Parameter:
1876bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1877f089877aSRichard Tran Mills 
1878f089877aSRichard Tran Mills    Level: intermediate
1879f089877aSRichard Tran Mills 
1880bc0a1609SRichard Tran Mills    Notes:
1881bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1882bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1883bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1884bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1885bc0a1609SRichard Tran Mills 
1886bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
1887bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1888f089877aSRichard Tran Mills 
1889f089877aSRichard Tran Mills @*/
1890f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
1891f089877aSRichard Tran Mills {
1892f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1893f089877aSRichard Tran Mills 
1894f089877aSRichard Tran Mills   PetscFunctionBegin;
1895f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1896f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1897f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1898f089877aSRichard Tran Mills }
1899f089877aSRichard Tran Mills 
1900f089877aSRichard Tran Mills 
1901f089877aSRichard Tran Mills #undef __FUNCT__
190247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
190347c6ae99SBarry Smith /*@
190447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
190547c6ae99SBarry Smith 
190647c6ae99SBarry Smith     Collective on DM
190747c6ae99SBarry Smith 
190847c6ae99SBarry Smith     Input Parameter:
190947c6ae99SBarry Smith +   dm - the DM object
191091d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
191147c6ae99SBarry Smith 
191247c6ae99SBarry Smith     Output Parameter:
191347c6ae99SBarry Smith .   dmc - the coarsened DM
191447c6ae99SBarry Smith 
191547c6ae99SBarry Smith     Level: developer
191647c6ae99SBarry Smith 
1917e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
191847c6ae99SBarry Smith 
191947c6ae99SBarry Smith @*/
19207087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
192147c6ae99SBarry Smith {
192247c6ae99SBarry Smith   PetscErrorCode    ierr;
1923b17ce1afSJed Brown   DMCoarsenHookLink link;
192447c6ae99SBarry Smith 
192547c6ae99SBarry Smith   PetscFunctionBegin;
1926171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
192747c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
192843842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
19298cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1930644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
19310598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1932656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1933e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1934b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1935b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1936b17ce1afSJed Brown   }
1937b17ce1afSJed Brown   PetscFunctionReturn(0);
1938b17ce1afSJed Brown }
1939b17ce1afSJed Brown 
1940b17ce1afSJed Brown #undef __FUNCT__
1941b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1942bb9467b5SJed Brown /*@C
1943b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1944b17ce1afSJed Brown 
1945b17ce1afSJed Brown    Logically Collective
1946b17ce1afSJed Brown 
1947b17ce1afSJed Brown    Input Arguments:
1948b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1949b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1950b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
19510298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1952b17ce1afSJed Brown 
1953b17ce1afSJed Brown    Calling sequence of coarsenhook:
1954b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1955b17ce1afSJed Brown 
1956b17ce1afSJed Brown +  fine - fine level DM
1957b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1958b17ce1afSJed Brown -  ctx - optional user-defined function context
1959b17ce1afSJed Brown 
1960b17ce1afSJed Brown    Calling sequence for restricthook:
1961c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1962b17ce1afSJed Brown 
1963b17ce1afSJed Brown +  fine - fine level DM
1964b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1965c833c3b5SJed Brown .  rscale - scaling vector for restriction
1966c833c3b5SJed Brown .  inject - matrix restricting by injection
1967b17ce1afSJed Brown .  coarse - coarse level DM to update
1968b17ce1afSJed Brown -  ctx - optional user-defined function context
1969b17ce1afSJed Brown 
1970b17ce1afSJed Brown    Level: advanced
1971b17ce1afSJed Brown 
1972b17ce1afSJed Brown    Notes:
1973b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1974b17ce1afSJed Brown 
1975b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1976b17ce1afSJed Brown 
1977b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1978b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1979b17ce1afSJed Brown 
1980bb9467b5SJed Brown    This function is currently not available from Fortran.
1981bb9467b5SJed Brown 
1982c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1983b17ce1afSJed Brown @*/
1984b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1985b17ce1afSJed Brown {
1986b17ce1afSJed Brown   PetscErrorCode    ierr;
1987b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1988b17ce1afSJed Brown 
1989b17ce1afSJed Brown   PetscFunctionBegin;
1990b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
19916bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1992b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1993b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
1994b17ce1afSJed Brown   link->restricthook = restricthook;
1995b17ce1afSJed Brown   link->ctx          = ctx;
19960298fd71SBarry Smith   link->next         = NULL;
1997b17ce1afSJed Brown   *p                 = link;
1998b17ce1afSJed Brown   PetscFunctionReturn(0);
1999b17ce1afSJed Brown }
2000b17ce1afSJed Brown 
2001b17ce1afSJed Brown #undef __FUNCT__
2002b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2003b17ce1afSJed Brown /*@
2004b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2005b17ce1afSJed Brown 
2006b17ce1afSJed Brown    Collective if any hooks are
2007b17ce1afSJed Brown 
2008b17ce1afSJed Brown    Input Arguments:
2009b17ce1afSJed Brown +  fine - finer DM to use as a base
2010b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2011b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2012b17ce1afSJed Brown -  coarse - coarer DM to update
2013b17ce1afSJed Brown 
2014b17ce1afSJed Brown    Level: developer
2015b17ce1afSJed Brown 
2016b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2017b17ce1afSJed Brown @*/
2018b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2019b17ce1afSJed Brown {
2020b17ce1afSJed Brown   PetscErrorCode    ierr;
2021b17ce1afSJed Brown   DMCoarsenHookLink link;
2022b17ce1afSJed Brown 
2023b17ce1afSJed Brown   PetscFunctionBegin;
2024b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
20258865f1eaSKarl Rupp     if (link->restricthook) {
20268865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
20278865f1eaSKarl Rupp     }
2028b17ce1afSJed Brown   }
202947c6ae99SBarry Smith   PetscFunctionReturn(0);
203047c6ae99SBarry Smith }
203147c6ae99SBarry Smith 
203247c6ae99SBarry Smith #undef __FUNCT__
2033be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2034bb9467b5SJed Brown /*@C
2035be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
20365dbd56e3SPeter Brune 
20375dbd56e3SPeter Brune    Logically Collective
20385dbd56e3SPeter Brune 
20395dbd56e3SPeter Brune    Input Arguments:
20405dbd56e3SPeter Brune +  global - global DM
2041ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
20425dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
20430298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20445dbd56e3SPeter Brune 
2045ec4806b8SPeter Brune 
2046ec4806b8SPeter Brune    Calling sequence for ddhook:
2047ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2048ec4806b8SPeter Brune 
2049ec4806b8SPeter Brune +  global - global DM
2050ec4806b8SPeter Brune .  block  - block DM
2051ec4806b8SPeter Brune -  ctx - optional user-defined function context
2052ec4806b8SPeter Brune 
20535dbd56e3SPeter Brune    Calling sequence for restricthook:
2054ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
20555dbd56e3SPeter Brune 
20565dbd56e3SPeter Brune +  global - global DM
20575dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
20585dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2059ec4806b8SPeter Brune .  block  - block DM
20605dbd56e3SPeter Brune -  ctx - optional user-defined function context
20615dbd56e3SPeter Brune 
20625dbd56e3SPeter Brune    Level: advanced
20635dbd56e3SPeter Brune 
20645dbd56e3SPeter Brune    Notes:
2065ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
20665dbd56e3SPeter Brune 
20675dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
20685dbd56e3SPeter Brune 
20695dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2070ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
20715dbd56e3SPeter Brune 
2072bb9467b5SJed Brown    This function is currently not available from Fortran.
2073bb9467b5SJed Brown 
20745dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20755dbd56e3SPeter Brune @*/
2076be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
20775dbd56e3SPeter Brune {
20785dbd56e3SPeter Brune   PetscErrorCode      ierr;
2079be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
20805dbd56e3SPeter Brune 
20815dbd56e3SPeter Brune   PetscFunctionBegin;
20825dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2083be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2084be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
20855dbd56e3SPeter Brune   link->restricthook = restricthook;
2086be081cd6SPeter Brune   link->ddhook       = ddhook;
20875dbd56e3SPeter Brune   link->ctx          = ctx;
20880298fd71SBarry Smith   link->next         = NULL;
20895dbd56e3SPeter Brune   *p                 = link;
20905dbd56e3SPeter Brune   PetscFunctionReturn(0);
20915dbd56e3SPeter Brune }
20925dbd56e3SPeter Brune 
20935dbd56e3SPeter Brune #undef __FUNCT__
2094be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
20955dbd56e3SPeter Brune /*@
2096be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
20975dbd56e3SPeter Brune 
20985dbd56e3SPeter Brune    Collective if any hooks are
20995dbd56e3SPeter Brune 
21005dbd56e3SPeter Brune    Input Arguments:
21015dbd56e3SPeter Brune +  fine - finer DM to use as a base
2102be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2103be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
21045dbd56e3SPeter Brune -  coarse - coarer DM to update
21055dbd56e3SPeter Brune 
21065dbd56e3SPeter Brune    Level: developer
21075dbd56e3SPeter Brune 
21085dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
21095dbd56e3SPeter Brune @*/
2110be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
21115dbd56e3SPeter Brune {
21125dbd56e3SPeter Brune   PetscErrorCode      ierr;
2113be081cd6SPeter Brune   DMSubDomainHookLink link;
21145dbd56e3SPeter Brune 
21155dbd56e3SPeter Brune   PetscFunctionBegin;
2116be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
21178865f1eaSKarl Rupp     if (link->restricthook) {
21188865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
21198865f1eaSKarl Rupp     }
21205dbd56e3SPeter Brune   }
21215dbd56e3SPeter Brune   PetscFunctionReturn(0);
21225dbd56e3SPeter Brune }
21235dbd56e3SPeter Brune 
21245dbd56e3SPeter Brune #undef __FUNCT__
21255fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
21265fe1f584SPeter Brune /*@
21276a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
21285fe1f584SPeter Brune 
21295fe1f584SPeter Brune     Not Collective
21305fe1f584SPeter Brune 
21315fe1f584SPeter Brune     Input Parameter:
21325fe1f584SPeter Brune .   dm - the DM object
21335fe1f584SPeter Brune 
21345fe1f584SPeter Brune     Output Parameter:
21356a7d9d85SPeter Brune .   level - number of coarsenings
21365fe1f584SPeter Brune 
21375fe1f584SPeter Brune     Level: developer
21385fe1f584SPeter Brune 
21396a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
21405fe1f584SPeter Brune 
21415fe1f584SPeter Brune @*/
21425fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
21435fe1f584SPeter Brune {
21445fe1f584SPeter Brune   PetscFunctionBegin;
21455fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21465fe1f584SPeter Brune   *level = dm->leveldown;
21475fe1f584SPeter Brune   PetscFunctionReturn(0);
21485fe1f584SPeter Brune }
21495fe1f584SPeter Brune 
21505fe1f584SPeter Brune 
21515fe1f584SPeter Brune 
21525fe1f584SPeter Brune #undef __FUNCT__
215347c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
215447c6ae99SBarry Smith /*@C
215547c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
215647c6ae99SBarry Smith 
215747c6ae99SBarry Smith     Collective on DM
215847c6ae99SBarry Smith 
215947c6ae99SBarry Smith     Input Parameter:
216047c6ae99SBarry Smith +   dm - the DM object
216147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
216247c6ae99SBarry Smith 
216347c6ae99SBarry Smith     Output Parameter:
216447c6ae99SBarry Smith .   dmf - the refined DM hierarchy
216547c6ae99SBarry Smith 
216647c6ae99SBarry Smith     Level: developer
216747c6ae99SBarry Smith 
2168e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith @*/
21717087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
217247c6ae99SBarry Smith {
217347c6ae99SBarry Smith   PetscErrorCode ierr;
217447c6ae99SBarry Smith 
217547c6ae99SBarry Smith   PetscFunctionBegin;
2176171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2177ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
217847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
217947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
218047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
218147c6ae99SBarry Smith   } else if (dm->ops->refine) {
218247c6ae99SBarry Smith     PetscInt i;
218347c6ae99SBarry Smith 
2184ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
218547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2186ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
218747c6ae99SBarry Smith     }
2188ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
218947c6ae99SBarry Smith   PetscFunctionReturn(0);
219047c6ae99SBarry Smith }
219147c6ae99SBarry Smith 
219247c6ae99SBarry Smith #undef __FUNCT__
219347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
219447c6ae99SBarry Smith /*@C
219547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
219647c6ae99SBarry Smith 
219747c6ae99SBarry Smith     Collective on DM
219847c6ae99SBarry Smith 
219947c6ae99SBarry Smith     Input Parameter:
220047c6ae99SBarry Smith +   dm - the DM object
220147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
220247c6ae99SBarry Smith 
220347c6ae99SBarry Smith     Output Parameter:
220447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
220547c6ae99SBarry Smith 
220647c6ae99SBarry Smith     Level: developer
220747c6ae99SBarry Smith 
2208e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
220947c6ae99SBarry Smith 
221047c6ae99SBarry Smith @*/
22117087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
221247c6ae99SBarry Smith {
221347c6ae99SBarry Smith   PetscErrorCode ierr;
221447c6ae99SBarry Smith 
221547c6ae99SBarry Smith   PetscFunctionBegin;
2216171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2217ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
221847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
221947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
222047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
222147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
222247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
222347c6ae99SBarry Smith     PetscInt i;
222447c6ae99SBarry Smith 
2225ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
222647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2227ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
222847c6ae99SBarry Smith     }
2229ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
223047c6ae99SBarry Smith   PetscFunctionReturn(0);
223147c6ae99SBarry Smith }
223247c6ae99SBarry Smith 
223347c6ae99SBarry Smith #undef __FUNCT__
2234e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
223547c6ae99SBarry Smith /*@
2236e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
223747c6ae99SBarry Smith    grids associated with two DMs.
223847c6ae99SBarry Smith 
223947c6ae99SBarry Smith    Collective on DM
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith    Input Parameters:
224247c6ae99SBarry Smith +  dmc - the coarse grid DM
224347c6ae99SBarry Smith -  dmf - the fine grid DM
224447c6ae99SBarry Smith 
224547c6ae99SBarry Smith    Output Parameters:
224647c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
224747c6ae99SBarry Smith 
224847c6ae99SBarry Smith    Level: intermediate
224947c6ae99SBarry Smith 
225047c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
225147c6ae99SBarry Smith 
2252e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
225347c6ae99SBarry Smith @*/
2254e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
225547c6ae99SBarry Smith {
225647c6ae99SBarry Smith   PetscErrorCode ierr;
225747c6ae99SBarry Smith 
225847c6ae99SBarry Smith   PetscFunctionBegin;
2259171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2260171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
226147c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
226247c6ae99SBarry Smith   PetscFunctionReturn(0);
226347c6ae99SBarry Smith }
226447c6ae99SBarry Smith 
226547c6ae99SBarry Smith #undef __FUNCT__
22661a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
22671a266240SBarry Smith /*@C
22681a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
22691a266240SBarry Smith 
22701a266240SBarry Smith     Not Collective
22711a266240SBarry Smith 
22721a266240SBarry Smith     Input Parameters:
22731a266240SBarry Smith +   dm - the DM object
22741a266240SBarry Smith -   destroy - the destroy function
22751a266240SBarry Smith 
22761a266240SBarry Smith     Level: intermediate
22771a266240SBarry Smith 
2278e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
22791a266240SBarry Smith 
2280f07f9ceaSJed Brown @*/
22811a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
22821a266240SBarry Smith {
22831a266240SBarry Smith   PetscFunctionBegin;
2284171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22851a266240SBarry Smith   dm->ctxdestroy = destroy;
22861a266240SBarry Smith   PetscFunctionReturn(0);
22871a266240SBarry Smith }
22881a266240SBarry Smith 
22891a266240SBarry Smith #undef __FUNCT__
22901b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2291b07ff414SBarry Smith /*@
22921b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
229347c6ae99SBarry Smith 
229447c6ae99SBarry Smith     Not Collective
229547c6ae99SBarry Smith 
229647c6ae99SBarry Smith     Input Parameters:
229747c6ae99SBarry Smith +   dm - the DM object
229847c6ae99SBarry Smith -   ctx - the user context
229947c6ae99SBarry Smith 
230047c6ae99SBarry Smith     Level: intermediate
230147c6ae99SBarry Smith 
2302e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
230347c6ae99SBarry Smith 
230447c6ae99SBarry Smith @*/
23051b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
230647c6ae99SBarry Smith {
230747c6ae99SBarry Smith   PetscFunctionBegin;
2308171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
230947c6ae99SBarry Smith   dm->ctx = ctx;
231047c6ae99SBarry Smith   PetscFunctionReturn(0);
231147c6ae99SBarry Smith }
231247c6ae99SBarry Smith 
231347c6ae99SBarry Smith #undef __FUNCT__
23141b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
231547c6ae99SBarry Smith /*@
23161b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
231747c6ae99SBarry Smith 
231847c6ae99SBarry Smith     Not Collective
231947c6ae99SBarry Smith 
232047c6ae99SBarry Smith     Input Parameter:
232147c6ae99SBarry Smith .   dm - the DM object
232247c6ae99SBarry Smith 
232347c6ae99SBarry Smith     Output Parameter:
232447c6ae99SBarry Smith .   ctx - the user context
232547c6ae99SBarry Smith 
232647c6ae99SBarry Smith     Level: intermediate
232747c6ae99SBarry Smith 
2328e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
232947c6ae99SBarry Smith 
233047c6ae99SBarry Smith @*/
23311b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
233247c6ae99SBarry Smith {
233347c6ae99SBarry Smith   PetscFunctionBegin;
2334171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23351b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
233647c6ae99SBarry Smith   PetscFunctionReturn(0);
233747c6ae99SBarry Smith }
233847c6ae99SBarry Smith 
233947c6ae99SBarry Smith #undef __FUNCT__
234008da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
234108da532bSDmitry Karpeev /*@C
234208da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
234308da532bSDmitry Karpeev 
234408da532bSDmitry Karpeev     Logically Collective on DM
234508da532bSDmitry Karpeev 
234608da532bSDmitry Karpeev     Input Parameter:
234708da532bSDmitry Karpeev +   dm - the DM object
23480298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
234908da532bSDmitry Karpeev 
235008da532bSDmitry Karpeev     Level: intermediate
235108da532bSDmitry Karpeev 
2352835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
235308da532bSDmitry Karpeev          DMSetJacobian()
235408da532bSDmitry Karpeev 
235508da532bSDmitry Karpeev @*/
235608da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
235708da532bSDmitry Karpeev {
235808da532bSDmitry Karpeev   PetscFunctionBegin;
235908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
236008da532bSDmitry Karpeev   PetscFunctionReturn(0);
236108da532bSDmitry Karpeev }
236208da532bSDmitry Karpeev 
236308da532bSDmitry Karpeev #undef __FUNCT__
236408da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
236508da532bSDmitry Karpeev /*@
236608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
236708da532bSDmitry Karpeev 
236808da532bSDmitry Karpeev     Not Collective
236908da532bSDmitry Karpeev 
237008da532bSDmitry Karpeev     Input Parameter:
237108da532bSDmitry Karpeev .   dm - the DM object to destroy
237208da532bSDmitry Karpeev 
237308da532bSDmitry Karpeev     Output Parameter:
237408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
237508da532bSDmitry Karpeev 
237608da532bSDmitry Karpeev     Level: developer
237708da532bSDmitry Karpeev 
237874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
237908da532bSDmitry Karpeev 
238008da532bSDmitry Karpeev @*/
238108da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
238208da532bSDmitry Karpeev {
238308da532bSDmitry Karpeev   PetscFunctionBegin;
238408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
238508da532bSDmitry Karpeev   PetscFunctionReturn(0);
238608da532bSDmitry Karpeev }
238708da532bSDmitry Karpeev 
238808da532bSDmitry Karpeev #undef __FUNCT__
238908da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
239008da532bSDmitry Karpeev /*@C
239108da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
239208da532bSDmitry Karpeev 
239308da532bSDmitry Karpeev     Logically Collective on DM
239408da532bSDmitry Karpeev 
239508da532bSDmitry Karpeev     Input Parameters:
239608da532bSDmitry Karpeev +   dm - the DM object to destroy
239708da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
239808da532bSDmitry Karpeev 
239908da532bSDmitry Karpeev     Output parameters:
240008da532bSDmitry Karpeev +   xl - lower bound
240108da532bSDmitry Karpeev -   xu - upper bound
240208da532bSDmitry Karpeev 
240308da532bSDmitry Karpeev     Level: intermediate
240408da532bSDmitry Karpeev 
240574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
240608da532bSDmitry Karpeev 
240708da532bSDmitry Karpeev @*/
240808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
240908da532bSDmitry Karpeev {
241008da532bSDmitry Karpeev   PetscErrorCode ierr;
24115fd66863SKarl Rupp 
241208da532bSDmitry Karpeev   PetscFunctionBegin;
241308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
241408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
241508da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
241608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
24178865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
241808da532bSDmitry Karpeev   PetscFunctionReturn(0);
241908da532bSDmitry Karpeev }
242008da532bSDmitry Karpeev 
242108da532bSDmitry Karpeev #undef __FUNCT__
2422b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2423b0ae01b7SPeter Brune /*@
2424b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2425b0ae01b7SPeter Brune 
2426b0ae01b7SPeter Brune     Not Collective
2427b0ae01b7SPeter Brune 
2428b0ae01b7SPeter Brune     Input Parameter:
2429b0ae01b7SPeter Brune .   dm - the DM object
2430b0ae01b7SPeter Brune 
2431b0ae01b7SPeter Brune     Output Parameter:
2432b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2433b0ae01b7SPeter Brune 
2434b0ae01b7SPeter Brune     Level: developer
2435b0ae01b7SPeter Brune 
2436b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2437b0ae01b7SPeter Brune 
2438b0ae01b7SPeter Brune @*/
2439b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2440b0ae01b7SPeter Brune {
2441b0ae01b7SPeter Brune   PetscFunctionBegin;
2442b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2443b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2444b0ae01b7SPeter Brune }
2445b0ae01b7SPeter Brune 
2446b0ae01b7SPeter Brune #undef  __FUNCT__
244708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2448748fac09SDmitry Karpeev /*@C
244908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
245008da532bSDmitry Karpeev 
245108da532bSDmitry Karpeev     Collective on DM
245208da532bSDmitry Karpeev 
245308da532bSDmitry Karpeev     Input Parameter:
245408da532bSDmitry Karpeev +   dm - the DM object
24550298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
245608da532bSDmitry Karpeev 
245708da532bSDmitry Karpeev     Level: developer
245808da532bSDmitry Karpeev 
245974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
246008da532bSDmitry Karpeev 
246108da532bSDmitry Karpeev @*/
246208da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
246308da532bSDmitry Karpeev {
246408da532bSDmitry Karpeev   PetscErrorCode ierr;
24655fd66863SKarl Rupp 
246608da532bSDmitry Karpeev   PetscFunctionBegin;
246708da532bSDmitry Karpeev   if (x) {
246808da532bSDmitry Karpeev     if (!dm->x) {
246908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
247008da532bSDmitry Karpeev     }
247108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
24728865f1eaSKarl Rupp   } else if (dm->x) {
247308da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
247408da532bSDmitry Karpeev   }
247508da532bSDmitry Karpeev   PetscFunctionReturn(0);
247608da532bSDmitry Karpeev }
247708da532bSDmitry Karpeev 
24780298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2479264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2480264ace61SBarry Smith 
2481264ace61SBarry Smith #undef __FUNCT__
2482264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2483264ace61SBarry Smith /*@C
2484264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2485264ace61SBarry Smith 
2486264ace61SBarry Smith   Collective on DM
2487264ace61SBarry Smith 
2488264ace61SBarry Smith   Input Parameters:
2489264ace61SBarry Smith + dm     - The DM object
2490264ace61SBarry Smith - method - The name of the DM type
2491264ace61SBarry Smith 
2492264ace61SBarry Smith   Options Database Key:
2493264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2494264ace61SBarry Smith 
2495264ace61SBarry Smith   Notes:
2496e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2497264ace61SBarry Smith 
2498264ace61SBarry Smith   Level: intermediate
2499264ace61SBarry Smith 
2500264ace61SBarry Smith .keywords: DM, set, type
2501264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2502264ace61SBarry Smith @*/
250319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2504264ace61SBarry Smith {
2505264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2506264ace61SBarry Smith   PetscBool      match;
2507264ace61SBarry Smith   PetscErrorCode ierr;
2508264ace61SBarry Smith 
2509264ace61SBarry Smith   PetscFunctionBegin;
2510264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2511251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2512264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2513264ace61SBarry Smith 
2514607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
25151c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2516ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2517264ace61SBarry Smith 
2518264ace61SBarry Smith   if (dm->ops->destroy) {
2519264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
25200298fd71SBarry Smith     dm->ops->destroy = NULL;
2521264ace61SBarry Smith   }
2522264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2523264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2524264ace61SBarry Smith   PetscFunctionReturn(0);
2525264ace61SBarry Smith }
2526264ace61SBarry Smith 
2527264ace61SBarry Smith #undef __FUNCT__
2528264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2529264ace61SBarry Smith /*@C
2530264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2531264ace61SBarry Smith 
2532264ace61SBarry Smith   Not Collective
2533264ace61SBarry Smith 
2534264ace61SBarry Smith   Input Parameter:
2535264ace61SBarry Smith . dm  - The DM
2536264ace61SBarry Smith 
2537264ace61SBarry Smith   Output Parameter:
2538264ace61SBarry Smith . type - The DM type name
2539264ace61SBarry Smith 
2540264ace61SBarry Smith   Level: intermediate
2541264ace61SBarry Smith 
2542264ace61SBarry Smith .keywords: DM, get, type, name
2543264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2544264ace61SBarry Smith @*/
254519fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2546264ace61SBarry Smith {
2547264ace61SBarry Smith   PetscErrorCode ierr;
2548264ace61SBarry Smith 
2549264ace61SBarry Smith   PetscFunctionBegin;
2550264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2551264ace61SBarry Smith   PetscValidCharPointer(type,2);
2552264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2553607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2554264ace61SBarry Smith   }
2555264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2556264ace61SBarry Smith   PetscFunctionReturn(0);
2557264ace61SBarry Smith }
2558264ace61SBarry Smith 
255967a56275SMatthew G Knepley #undef __FUNCT__
256067a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
256167a56275SMatthew G Knepley /*@C
256267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
256367a56275SMatthew G Knepley 
256467a56275SMatthew G Knepley   Collective on DM
256567a56275SMatthew G Knepley 
256667a56275SMatthew G Knepley   Input Parameters:
256767a56275SMatthew G Knepley + dm - the DM
256867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
256967a56275SMatthew G Knepley 
257067a56275SMatthew G Knepley   Output Parameter:
257167a56275SMatthew G Knepley . M - pointer to new DM
257267a56275SMatthew G Knepley 
257367a56275SMatthew G Knepley   Notes:
257467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
257567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
257667a56275SMatthew G Knepley   of the input DM.
257767a56275SMatthew G Knepley 
257867a56275SMatthew G Knepley   Level: intermediate
257967a56275SMatthew G Knepley 
258067a56275SMatthew G Knepley .seealso: DMCreate()
258167a56275SMatthew G Knepley @*/
258219fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
258367a56275SMatthew G Knepley {
258467a56275SMatthew G Knepley   DM             B;
258567a56275SMatthew G Knepley   char           convname[256];
258667a56275SMatthew G Knepley   PetscBool      sametype, issame;
258767a56275SMatthew G Knepley   PetscErrorCode ierr;
258867a56275SMatthew G Knepley 
258967a56275SMatthew G Knepley   PetscFunctionBegin;
259067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
259167a56275SMatthew G Knepley   PetscValidType(dm,1);
259267a56275SMatthew G Knepley   PetscValidPointer(M,3);
2593251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
259467a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
259567a56275SMatthew G Knepley   {
25960298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
259767a56275SMatthew G Knepley 
259867a56275SMatthew G Knepley     /*
259967a56275SMatthew G Knepley        Order of precedence:
260067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
260167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
260267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
260367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
260467a56275SMatthew G Knepley        5) Use a really basic converter.
260567a56275SMatthew G Knepley     */
260667a56275SMatthew G Knepley 
260767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
260867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
260967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
261067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
261167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
261267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26130005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
261467a56275SMatthew G Knepley     if (conv) goto foundconv;
261567a56275SMatthew G Knepley 
261667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
261782f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
261867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
261967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
262067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
262167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
262267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
262367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26240005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
262567a56275SMatthew G Knepley     if (conv) {
2626fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
262767a56275SMatthew G Knepley       goto foundconv;
262867a56275SMatthew G Knepley     }
262967a56275SMatthew G Knepley 
263067a56275SMatthew G Knepley #if 0
263167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
263267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2633fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
263467a56275SMatthew G Knepley     if (conv) goto foundconv;
263567a56275SMatthew G Knepley 
263667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
263767a56275SMatthew G Knepley     if (dm->ops->convert) {
263867a56275SMatthew G Knepley       conv = dm->ops->convert;
263967a56275SMatthew G Knepley     }
264067a56275SMatthew G Knepley     if (conv) goto foundconv;
264167a56275SMatthew G Knepley #endif
264267a56275SMatthew G Knepley 
264367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
264482f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
264567a56275SMatthew G Knepley 
264667a56275SMatthew G Knepley foundconv:
264767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
264867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
264967a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
265067a56275SMatthew G Knepley   }
265167a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
265267a56275SMatthew G Knepley   PetscFunctionReturn(0);
265367a56275SMatthew G Knepley }
2654264ace61SBarry Smith 
2655264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2656264ace61SBarry Smith 
2657264ace61SBarry Smith #undef __FUNCT__
2658264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2659264ace61SBarry Smith /*@C
26601c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
26611c84c290SBarry Smith 
26621c84c290SBarry Smith   Not Collective
26631c84c290SBarry Smith 
26641c84c290SBarry Smith   Input Parameters:
26651c84c290SBarry Smith + name        - The name of a new user-defined creation routine
26661c84c290SBarry Smith - create_func - The creation routine itself
26671c84c290SBarry Smith 
26681c84c290SBarry Smith   Notes:
26691c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
26701c84c290SBarry Smith 
26711c84c290SBarry Smith 
26721c84c290SBarry Smith   Sample usage:
26731c84c290SBarry Smith .vb
2674bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
26751c84c290SBarry Smith .ve
26761c84c290SBarry Smith 
26771c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
26781c84c290SBarry Smith .vb
26791c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
26801c84c290SBarry Smith     DMSetType(DM,"my_da");
26811c84c290SBarry Smith .ve
26821c84c290SBarry Smith    or at runtime via the option
26831c84c290SBarry Smith .vb
26841c84c290SBarry Smith     -da_type my_da
26851c84c290SBarry Smith .ve
2686264ace61SBarry Smith 
2687264ace61SBarry Smith   Level: advanced
26881c84c290SBarry Smith 
26891c84c290SBarry Smith .keywords: DM, register
2690bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
26911c84c290SBarry Smith 
2692264ace61SBarry Smith @*/
2693bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2694264ace61SBarry Smith {
2695264ace61SBarry Smith   PetscErrorCode ierr;
2696264ace61SBarry Smith 
2697264ace61SBarry Smith   PetscFunctionBegin;
2698a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2699264ace61SBarry Smith   PetscFunctionReturn(0);
2700264ace61SBarry Smith }
2701264ace61SBarry Smith 
2702b859378eSBarry Smith #undef __FUNCT__
2703b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2704b859378eSBarry Smith /*@C
270555849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2706b859378eSBarry Smith 
2707b859378eSBarry Smith   Collective on PetscViewer
2708b859378eSBarry Smith 
2709b859378eSBarry Smith   Input Parameters:
2710b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2711b859378eSBarry Smith            some related function before a call to DMLoad().
2712b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2713b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2714b859378eSBarry Smith 
2715b859378eSBarry Smith    Level: intermediate
2716b859378eSBarry Smith 
2717b859378eSBarry Smith   Notes:
271855849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2719b859378eSBarry Smith 
2720b859378eSBarry Smith   Notes for advanced users:
2721b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2722b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2723b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2724b859378eSBarry Smith   format is
2725b859378eSBarry Smith .vb
2726b859378eSBarry Smith      has not yet been determined
2727b859378eSBarry Smith .ve
2728b859378eSBarry Smith 
2729b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2730b859378eSBarry Smith @*/
2731b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2732b859378eSBarry Smith {
27339331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
2734b859378eSBarry Smith   PetscErrorCode ierr;
2735b859378eSBarry Smith 
2736b859378eSBarry Smith   PetscFunctionBegin;
2737b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2738b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
273932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
27409331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
27419331c7a4SMatthew G. Knepley   if (isbinary) {
27429331c7a4SMatthew G. Knepley     PetscInt classid;
27439331c7a4SMatthew G. Knepley     char     type[256];
2744b859378eSBarry Smith 
274532c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
27469200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
274732c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
274832c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
27499331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
27509331c7a4SMatthew G. Knepley   } else if (ishdf5) {
27519331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
27529331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
2753b859378eSBarry Smith   PetscFunctionReturn(0);
2754b859378eSBarry Smith }
2755b859378eSBarry Smith 
27567da65231SMatthew G Knepley /******************************** FEM Support **********************************/
27577da65231SMatthew G Knepley 
27587da65231SMatthew G Knepley #undef __FUNCT__
27597da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2760a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2761a6dfd86eSKarl Rupp {
27621d47ebbbSSatish Balay   PetscInt       f;
27631b30c384SMatthew G Knepley   PetscErrorCode ierr;
27641b30c384SMatthew G Knepley 
27657da65231SMatthew G Knepley   PetscFunctionBegin;
276674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27671d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
276857622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
27697da65231SMatthew G Knepley   }
27707da65231SMatthew G Knepley   PetscFunctionReturn(0);
27717da65231SMatthew G Knepley }
27727da65231SMatthew G Knepley 
27737da65231SMatthew G Knepley #undef __FUNCT__
27747da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2775a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2776a6dfd86eSKarl Rupp {
27771b30c384SMatthew G Knepley   PetscInt       f, g;
27787da65231SMatthew G Knepley   PetscErrorCode ierr;
27797da65231SMatthew G Knepley 
27807da65231SMatthew G Knepley   PetscFunctionBegin;
278174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27821d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
278374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
27841d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
2785e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
27867da65231SMatthew G Knepley     }
278774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
27887da65231SMatthew G Knepley   }
27897da65231SMatthew G Knepley   PetscFunctionReturn(0);
27907da65231SMatthew G Knepley }
2791e7c4fc90SDmitry Karpeev 
2792970e74d5SMatthew G Knepley #undef __FUNCT__
2793e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
27946113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
2795e759306cSMatthew G. Knepley {
2796e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
2797e759306cSMatthew G. Knepley   PetscInt       p;
2798e759306cSMatthew G. Knepley   PetscErrorCode ierr;
2799e759306cSMatthew G. Knepley 
2800e759306cSMatthew G. Knepley   PetscFunctionBegin;
2801e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
2802e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
2803e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
2804e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
2805e759306cSMatthew G. Knepley     if (p == rank) {
2806e759306cSMatthew G. Knepley       Vec x;
2807e759306cSMatthew G. Knepley 
2808e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
2809e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
28106113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
2811e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2812e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
2813e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2814e759306cSMatthew G. Knepley     }
2815e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
2816e759306cSMatthew G. Knepley   }
2817e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
2818e759306cSMatthew G. Knepley }
2819e759306cSMatthew G. Knepley 
2820e759306cSMatthew G. Knepley #undef __FUNCT__
282188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
282288ed4aceSMatthew G Knepley /*@
282388ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
282488ed4aceSMatthew G Knepley 
282588ed4aceSMatthew G Knepley   Input Parameter:
282688ed4aceSMatthew G Knepley . dm - The DM
282788ed4aceSMatthew G Knepley 
282888ed4aceSMatthew G Knepley   Output Parameter:
282988ed4aceSMatthew G Knepley . section - The PetscSection
283088ed4aceSMatthew G Knepley 
283188ed4aceSMatthew G Knepley   Level: intermediate
283288ed4aceSMatthew G Knepley 
283388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
283488ed4aceSMatthew G Knepley 
283588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
283688ed4aceSMatthew G Knepley @*/
28370adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
28380adebc6cSBarry Smith {
2839fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
2840fd59a867SMatthew G. Knepley 
284188ed4aceSMatthew G Knepley   PetscFunctionBegin;
284288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
2844fd59a867SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);}
284588ed4aceSMatthew G Knepley   *section = dm->defaultSection;
284688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
284788ed4aceSMatthew G Knepley }
284888ed4aceSMatthew G Knepley 
284988ed4aceSMatthew G Knepley #undef __FUNCT__
285088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
285188ed4aceSMatthew G Knepley /*@
285288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
285388ed4aceSMatthew G Knepley 
285488ed4aceSMatthew G Knepley   Input Parameters:
285588ed4aceSMatthew G Knepley + dm - The DM
285688ed4aceSMatthew G Knepley - section - The PetscSection
285788ed4aceSMatthew G Knepley 
285888ed4aceSMatthew G Knepley   Level: intermediate
285988ed4aceSMatthew G Knepley 
286088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
286188ed4aceSMatthew G Knepley 
286288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
286388ed4aceSMatthew G Knepley @*/
28640adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
28650adebc6cSBarry Smith {
2866af122d2aSMatthew G Knepley   PetscInt       numFields;
2867af122d2aSMatthew G Knepley   PetscInt       f;
286888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
286988ed4aceSMatthew G Knepley 
287088ed4aceSMatthew G Knepley   PetscFunctionBegin;
287188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28721d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
28731d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
287488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
287588ed4aceSMatthew G Knepley   dm->defaultSection = section;
2876af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2877af122d2aSMatthew G Knepley   if (numFields) {
2878af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2879af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
28800f21e855SMatthew G. Knepley       PetscObject disc;
2881af122d2aSMatthew G Knepley       const char *name;
2882af122d2aSMatthew G Knepley 
2883af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
28840f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
28850f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
2886af122d2aSMatthew G Knepley     }
2887af122d2aSMatthew G Knepley   }
28881d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
28891d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
289088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
289188ed4aceSMatthew G Knepley }
289288ed4aceSMatthew G Knepley 
289388ed4aceSMatthew G Knepley #undef __FUNCT__
289488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
289588ed4aceSMatthew G Knepley /*@
289688ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
289788ed4aceSMatthew G Knepley 
28988b1ab98fSJed Brown   Collective on DM
28998b1ab98fSJed Brown 
290088ed4aceSMatthew G Knepley   Input Parameter:
290188ed4aceSMatthew G Knepley . dm - The DM
290288ed4aceSMatthew G Knepley 
290388ed4aceSMatthew G Knepley   Output Parameter:
290488ed4aceSMatthew G Knepley . section - The PetscSection
290588ed4aceSMatthew G Knepley 
290688ed4aceSMatthew G Knepley   Level: intermediate
290788ed4aceSMatthew G Knepley 
290888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
290988ed4aceSMatthew G Knepley 
291088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
291188ed4aceSMatthew G Knepley @*/
29120adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
29130adebc6cSBarry Smith {
291488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
291588ed4aceSMatthew G Knepley 
291688ed4aceSMatthew G Knepley   PetscFunctionBegin;
291788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
291988ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
2920fd59a867SMatthew G. Knepley     PetscSection s;
2921fd59a867SMatthew G. Knepley 
2922fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
2923fd59a867SMatthew 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");
2924fd59a867SMatthew 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");
2925fd59a867SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2926cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
2927ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
292888ed4aceSMatthew G Knepley   }
292988ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
293088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
293188ed4aceSMatthew G Knepley }
293288ed4aceSMatthew G Knepley 
293388ed4aceSMatthew G Knepley #undef __FUNCT__
2934b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2935b21d0597SMatthew G Knepley /*@
2936b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2937b21d0597SMatthew G Knepley 
2938b21d0597SMatthew G Knepley   Input Parameters:
2939b21d0597SMatthew G Knepley + dm - The DM
29405080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2941b21d0597SMatthew G Knepley 
2942b21d0597SMatthew G Knepley   Level: intermediate
2943b21d0597SMatthew G Knepley 
2944b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2945b21d0597SMatthew G Knepley 
2946b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2947b21d0597SMatthew G Knepley @*/
29480adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
29490adebc6cSBarry Smith {
2950b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2951b21d0597SMatthew G Knepley 
2952b21d0597SMatthew G Knepley   PetscFunctionBegin;
2953b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29545080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
29551d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2956b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2957b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2958b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2959b21d0597SMatthew G Knepley }
2960b21d0597SMatthew G Knepley 
2961b21d0597SMatthew G Knepley #undef __FUNCT__
296288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
296388ed4aceSMatthew G Knepley /*@
296488ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
296588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
296688ed4aceSMatthew G Knepley 
296788ed4aceSMatthew G Knepley   Input Parameter:
296888ed4aceSMatthew G Knepley . dm - The DM
296988ed4aceSMatthew G Knepley 
297088ed4aceSMatthew G Knepley   Output Parameter:
297188ed4aceSMatthew G Knepley . sf - The PetscSF
297288ed4aceSMatthew G Knepley 
297388ed4aceSMatthew G Knepley   Level: intermediate
297488ed4aceSMatthew G Knepley 
297588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
297688ed4aceSMatthew G Knepley 
297788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
297888ed4aceSMatthew G Knepley @*/
29790adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
29800adebc6cSBarry Smith {
298188ed4aceSMatthew G Knepley   PetscInt       nroots;
298288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
298388ed4aceSMatthew G Knepley 
298488ed4aceSMatthew G Knepley   PetscFunctionBegin;
298588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
298688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
29870298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
298888ed4aceSMatthew G Knepley   if (nroots < 0) {
298988ed4aceSMatthew G Knepley     PetscSection section, gSection;
299088ed4aceSMatthew G Knepley 
299188ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
299231ea6d37SMatthew G Knepley     if (section) {
299388ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
299488ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
299531ea6d37SMatthew G Knepley     } else {
29960298fd71SBarry Smith       *sf = NULL;
299731ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
299831ea6d37SMatthew G Knepley     }
299988ed4aceSMatthew G Knepley   }
300088ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
300188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
300288ed4aceSMatthew G Knepley }
300388ed4aceSMatthew G Knepley 
300488ed4aceSMatthew G Knepley #undef __FUNCT__
300588ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
300688ed4aceSMatthew G Knepley /*@
300788ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
300888ed4aceSMatthew G Knepley 
300988ed4aceSMatthew G Knepley   Input Parameters:
301088ed4aceSMatthew G Knepley + dm - The DM
301188ed4aceSMatthew G Knepley - sf - The PetscSF
301288ed4aceSMatthew G Knepley 
301388ed4aceSMatthew G Knepley   Level: intermediate
301488ed4aceSMatthew G Knepley 
301588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
301688ed4aceSMatthew G Knepley 
301788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
301888ed4aceSMatthew G Knepley @*/
30190adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
30200adebc6cSBarry Smith {
302188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
302288ed4aceSMatthew G Knepley 
302388ed4aceSMatthew G Knepley   PetscFunctionBegin;
302488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
302688ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
302788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
302888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
302988ed4aceSMatthew G Knepley }
303088ed4aceSMatthew G Knepley 
303188ed4aceSMatthew G Knepley #undef __FUNCT__
303288ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
303388ed4aceSMatthew G Knepley /*@C
303488ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
303588ed4aceSMatthew G Knepley   describing the data layout.
303688ed4aceSMatthew G Knepley 
303788ed4aceSMatthew G Knepley   Input Parameters:
303888ed4aceSMatthew G Knepley + dm - The DM
303988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
304088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
304188ed4aceSMatthew G Knepley 
304288ed4aceSMatthew G Knepley   Level: intermediate
304388ed4aceSMatthew G Knepley 
304488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
304588ed4aceSMatthew G Knepley @*/
304688ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
304788ed4aceSMatthew G Knepley {
304882f516ccSBarry Smith   MPI_Comm       comm;
304988ed4aceSMatthew G Knepley   PetscLayout    layout;
305088ed4aceSMatthew G Knepley   const PetscInt *ranges;
305188ed4aceSMatthew G Knepley   PetscInt       *local;
305288ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3053ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
305488ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
305588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
305688ed4aceSMatthew G Knepley 
305788ed4aceSMatthew G Knepley   PetscFunctionBegin;
305882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
305988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
306088ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
306188ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
306288ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
306388ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
306488ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
306588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
306688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
306788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
306888ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3069ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
30706636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
307188ed4aceSMatthew G Knepley 
30726636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30736636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3074235fbf56SMatthew 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));
30756636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
307688ed4aceSMatthew G Knepley   }
3077785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3078785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
307988ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
30801f588964SMatthew G Knepley     const PetscInt *cind;
30816636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
308288ed4aceSMatthew G Knepley 
308388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
308488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
308588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
308688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
308788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30886636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
308988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
30906636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
30916636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
30926636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3093057b4bcdSMatthew 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);
30946636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
30956636e97aSMatthew G Knepley     }
309688ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
309788ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
309888ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
309988ed4aceSMatthew G Knepley     }
310088ed4aceSMatthew G Knepley     if (gdof < 0) {
31016636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
310288ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
310388ed4aceSMatthew G Knepley 
310405376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
310531d3f06eSJed Brown         if (r < 0) r = -(r+2);
310605376888SMatthew 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);
310788ed4aceSMatthew G Knepley         remote[l].rank  = r;
310888ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
310988ed4aceSMatthew G Knepley       }
311088ed4aceSMatthew G Knepley     } else {
31116636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
311288ed4aceSMatthew G Knepley         remote[l].rank  = rank;
311388ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
311488ed4aceSMatthew G Knepley       }
311588ed4aceSMatthew G Knepley     }
311688ed4aceSMatthew G Knepley   }
31176636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
311888ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
311988ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
312088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
312188ed4aceSMatthew G Knepley }
3122af122d2aSMatthew G Knepley 
3123af122d2aSMatthew G Knepley #undef __FUNCT__
3124b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3125b21d0597SMatthew G Knepley /*@
3126b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3127b21d0597SMatthew G Knepley 
3128b21d0597SMatthew G Knepley   Input Parameter:
3129b21d0597SMatthew G Knepley . dm - The DM
3130b21d0597SMatthew G Knepley 
3131b21d0597SMatthew G Knepley   Output Parameter:
3132b21d0597SMatthew G Knepley . sf - The PetscSF
3133b21d0597SMatthew G Knepley 
3134b21d0597SMatthew G Knepley   Level: intermediate
3135b21d0597SMatthew G Knepley 
3136b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3137b21d0597SMatthew G Knepley 
3138057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3139b21d0597SMatthew G Knepley @*/
31400adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
31410adebc6cSBarry Smith {
3142b21d0597SMatthew G Knepley   PetscFunctionBegin;
3143b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3144b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3145b21d0597SMatthew G Knepley   *sf = dm->sf;
3146b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3147b21d0597SMatthew G Knepley }
3148b21d0597SMatthew G Knepley 
3149b21d0597SMatthew G Knepley #undef __FUNCT__
3150057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3151057b4bcdSMatthew G Knepley /*@
3152057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3153057b4bcdSMatthew G Knepley 
3154057b4bcdSMatthew G Knepley   Input Parameters:
3155057b4bcdSMatthew G Knepley + dm - The DM
3156057b4bcdSMatthew G Knepley - sf - The PetscSF
3157057b4bcdSMatthew G Knepley 
3158057b4bcdSMatthew G Knepley   Level: intermediate
3159057b4bcdSMatthew G Knepley 
3160057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3161057b4bcdSMatthew G Knepley @*/
31620adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
31630adebc6cSBarry Smith {
3164057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3165057b4bcdSMatthew G Knepley 
3166057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3167057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3168057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3169057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3170057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3171057b4bcdSMatthew G Knepley   dm->sf = sf;
3172057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3173057b4bcdSMatthew G Knepley }
3174057b4bcdSMatthew G Knepley 
3175057b4bcdSMatthew G Knepley #undef __FUNCT__
31762764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
31772764a2aaSMatthew G. Knepley /*@
31782764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
31792764a2aaSMatthew G. Knepley 
31802764a2aaSMatthew G. Knepley   Input Parameter:
31812764a2aaSMatthew G. Knepley . dm - The DM
31822764a2aaSMatthew G. Knepley 
31832764a2aaSMatthew G. Knepley   Output Parameter:
31842764a2aaSMatthew G. Knepley . prob - The PetscDS
31852764a2aaSMatthew G. Knepley 
31862764a2aaSMatthew G. Knepley   Level: developer
31872764a2aaSMatthew G. Knepley 
31882764a2aaSMatthew G. Knepley .seealso: DMSetDS()
31892764a2aaSMatthew G. Knepley @*/
31902764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3191af122d2aSMatthew G Knepley {
3192af122d2aSMatthew G Knepley   PetscFunctionBegin;
3193af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31940f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
31950f21e855SMatthew G. Knepley   *prob = dm->prob;
31960f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
31970f21e855SMatthew G. Knepley }
31980f21e855SMatthew G. Knepley 
31990f21e855SMatthew G. Knepley #undef __FUNCT__
32002764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
32012764a2aaSMatthew G. Knepley /*@
32022764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
32032764a2aaSMatthew G. Knepley 
32042764a2aaSMatthew G. Knepley   Input Parameters:
32052764a2aaSMatthew G. Knepley + dm - The DM
32062764a2aaSMatthew G. Knepley - prob - The PetscDS
32072764a2aaSMatthew G. Knepley 
32082764a2aaSMatthew G. Knepley   Level: developer
32092764a2aaSMatthew G. Knepley 
32102764a2aaSMatthew G. Knepley .seealso: DMGetDS()
32112764a2aaSMatthew G. Knepley @*/
32122764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
32130f21e855SMatthew G. Knepley {
32140f21e855SMatthew G. Knepley   PetscErrorCode ierr;
32150f21e855SMatthew G. Knepley 
32160f21e855SMatthew G. Knepley   PetscFunctionBegin;
32170f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32182764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
32192764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
32200f21e855SMatthew G. Knepley   dm->prob = prob;
32210f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
32220f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
32230f21e855SMatthew G. Knepley }
32240f21e855SMatthew G. Knepley 
32250f21e855SMatthew G. Knepley #undef __FUNCT__
32260f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
32270f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
32280f21e855SMatthew G. Knepley {
32290f21e855SMatthew G. Knepley   PetscErrorCode ierr;
32300f21e855SMatthew G. Knepley 
32310f21e855SMatthew G. Knepley   PetscFunctionBegin;
32320f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32332764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3234af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3235af122d2aSMatthew G Knepley }
3236af122d2aSMatthew G Knepley 
3237af122d2aSMatthew G Knepley #undef __FUNCT__
3238af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3239af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3240af122d2aSMatthew G Knepley {
32410f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3242af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3243af122d2aSMatthew G Knepley 
3244af122d2aSMatthew G Knepley   PetscFunctionBegin;
3245af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32462764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
32470f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
32480f21e855SMatthew G. Knepley     PetscContainer obj;
32490f21e855SMatthew G. Knepley 
32500f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
32512764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
32520f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3253af122d2aSMatthew G Knepley   }
3254af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3255af122d2aSMatthew G Knepley }
3256af122d2aSMatthew G Knepley 
3257af122d2aSMatthew G Knepley #undef __FUNCT__
3258af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3259c1929be8SMatthew G. Knepley /*@
3260c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3261c1929be8SMatthew G. Knepley 
3262c1929be8SMatthew G. Knepley   Not collective
3263c1929be8SMatthew G. Knepley 
3264c1929be8SMatthew G. Knepley   Input Parameters:
3265c1929be8SMatthew G. Knepley + dm - The DM
3266c1929be8SMatthew G. Knepley - f  - The field number
3267c1929be8SMatthew G. Knepley 
3268c1929be8SMatthew G. Knepley   Output Parameter:
3269c1929be8SMatthew G. Knepley . field - The discretization object
3270c1929be8SMatthew G. Knepley 
3271c1929be8SMatthew G. Knepley   Level: developer
3272c1929be8SMatthew G. Knepley 
3273c1929be8SMatthew G. Knepley .seealso: DMSetField()
3274c1929be8SMatthew G. Knepley @*/
3275af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3276af122d2aSMatthew G Knepley {
32770f21e855SMatthew G. Knepley   PetscErrorCode ierr;
32780f21e855SMatthew G. Knepley 
3279af122d2aSMatthew G Knepley   PetscFunctionBegin;
3280af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32812764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3282decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3283decb47aaSMatthew G. Knepley }
3284decb47aaSMatthew G. Knepley 
3285decb47aaSMatthew G. Knepley #undef __FUNCT__
3286decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3287c1929be8SMatthew G. Knepley /*@
3288c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3289c1929be8SMatthew G. Knepley 
3290c1929be8SMatthew G. Knepley   Logically collective on DM
3291c1929be8SMatthew G. Knepley 
3292c1929be8SMatthew G. Knepley   Input Parameters:
3293c1929be8SMatthew G. Knepley + dm - The DM
3294c1929be8SMatthew G. Knepley . f  - The field number
3295c1929be8SMatthew G. Knepley - field - The discretization object
3296c1929be8SMatthew G. Knepley 
3297c1929be8SMatthew G. Knepley   Level: developer
3298c1929be8SMatthew G. Knepley 
3299c1929be8SMatthew G. Knepley .seealso: DMGetField()
3300c1929be8SMatthew G. Knepley @*/
3301decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3302decb47aaSMatthew G. Knepley {
3303decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3304decb47aaSMatthew G. Knepley 
3305decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3306decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33072764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3308af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3309af122d2aSMatthew G Knepley }
33106636e97aSMatthew G Knepley 
33116636e97aSMatthew G Knepley #undef __FUNCT__
3312b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3313b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3314b64e0483SPeter Brune {
3315b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3316b64e0483SPeter Brune   PetscErrorCode ierr;
3317b64e0483SPeter Brune   Vec coords,ccoords;
3318b64e0483SPeter Brune   VecScatter scat;
3319b64e0483SPeter Brune   PetscFunctionBegin;
3320b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3321b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3322b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3323b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3324b64e0483SPeter Brune   if (coords && !ccoords) {
3325b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
3326b64e0483SPeter Brune     ierr = DMCreateInjection(dmc_coord,dm_coord,&scat);CHKERRQ(ierr);
3327b64e0483SPeter Brune     ierr = VecScatterBegin(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3328b64e0483SPeter Brune     ierr = VecScatterEnd(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3329b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3330b64e0483SPeter Brune     ierr = VecScatterDestroy(&scat);CHKERRQ(ierr);
3331b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3332b64e0483SPeter Brune   }
3333b64e0483SPeter Brune   PetscFunctionReturn(0);
3334b64e0483SPeter Brune }
3335b64e0483SPeter Brune 
3336b64e0483SPeter Brune #undef __FUNCT__
333703dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
333803dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
333903dadc2fSPeter Brune {
334003dadc2fSPeter Brune   DM dm_coord,subdm_coord;
334103dadc2fSPeter Brune   PetscErrorCode ierr;
334203dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
334303dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
334403dadc2fSPeter Brune   PetscFunctionBegin;
334503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
334603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
334703dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
334803dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
334903dadc2fSPeter Brune   if (coords && !ccoords) {
335003dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
335103dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
335203dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
335303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
335403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
335503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
335603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
335703dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
335803dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
335903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
336003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
336103dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
336203dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
336303dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
336403dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
336503dadc2fSPeter Brune   }
336603dadc2fSPeter Brune   PetscFunctionReturn(0);
336703dadc2fSPeter Brune }
336803dadc2fSPeter Brune 
336903dadc2fSPeter Brune #undef __FUNCT__
3370c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3371c73cfb54SMatthew G. Knepley /*@
3372c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3373c73cfb54SMatthew G. Knepley 
3374c73cfb54SMatthew G. Knepley   Not collective
3375c73cfb54SMatthew G. Knepley 
3376c73cfb54SMatthew G. Knepley   Input Parameter:
3377c73cfb54SMatthew G. Knepley . dm - The DM
3378c73cfb54SMatthew G. Knepley 
3379c73cfb54SMatthew G. Knepley   Output Parameter:
3380c73cfb54SMatthew G. Knepley . dim - The topological dimension
3381c73cfb54SMatthew G. Knepley 
3382c73cfb54SMatthew G. Knepley   Level: beginner
3383c73cfb54SMatthew G. Knepley 
3384c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3385c73cfb54SMatthew G. Knepley @*/
3386c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3387c73cfb54SMatthew G. Knepley {
3388c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3389c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3390c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3391c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3392c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3393c73cfb54SMatthew G. Knepley }
3394c73cfb54SMatthew G. Knepley 
3395c73cfb54SMatthew G. Knepley #undef __FUNCT__
3396c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3397c73cfb54SMatthew G. Knepley /*@
3398c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3399c73cfb54SMatthew G. Knepley 
3400c73cfb54SMatthew G. Knepley   Collective on dm
3401c73cfb54SMatthew G. Knepley 
3402c73cfb54SMatthew G. Knepley   Input Parameters:
3403c73cfb54SMatthew G. Knepley + dm - The DM
3404c73cfb54SMatthew G. Knepley - dim - The topological dimension
3405c73cfb54SMatthew G. Knepley 
3406c73cfb54SMatthew G. Knepley   Level: beginner
3407c73cfb54SMatthew G. Knepley 
3408c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3409c73cfb54SMatthew G. Knepley @*/
3410c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3411c73cfb54SMatthew G. Knepley {
3412c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3413c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3414c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3415c73cfb54SMatthew G. Knepley   dm->dim = dim;
3416c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3417c73cfb54SMatthew G. Knepley }
3418c73cfb54SMatthew G. Knepley 
3419793f3fe5SMatthew G. Knepley #undef __FUNCT__
3420793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3421793f3fe5SMatthew G. Knepley /*@
3422793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3423793f3fe5SMatthew G. Knepley 
3424793f3fe5SMatthew G. Knepley   Collective on DM
3425793f3fe5SMatthew G. Knepley 
3426793f3fe5SMatthew G. Knepley   Input Parameters:
3427793f3fe5SMatthew G. Knepley + dm - the DM
3428793f3fe5SMatthew G. Knepley - dim - the dimension
3429793f3fe5SMatthew G. Knepley 
3430793f3fe5SMatthew G. Knepley   Output Parameters:
3431793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3432793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3433793f3fe5SMatthew G. Knepley 
3434793f3fe5SMatthew G. Knepley   Note:
3435793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3436793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3437793f3fe5SMatthew G. Knepley   then the interval is empty.
3438793f3fe5SMatthew G. Knepley 
3439793f3fe5SMatthew G. Knepley   Level: intermediate
3440793f3fe5SMatthew G. Knepley 
3441793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3442793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3443793f3fe5SMatthew G. Knepley @*/
3444793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3445793f3fe5SMatthew G. Knepley {
3446793f3fe5SMatthew G. Knepley   PetscInt       d;
3447793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3448793f3fe5SMatthew G. Knepley 
3449793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3450793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3451793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3452793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3453793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3454793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3455793f3fe5SMatthew G. Knepley }
3456793f3fe5SMatthew G. Knepley 
3457793f3fe5SMatthew G. Knepley #undef __FUNCT__
34586636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
34596636e97aSMatthew G Knepley /*@
34606636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
34616636e97aSMatthew G Knepley 
34626636e97aSMatthew G Knepley   Collective on DM
34636636e97aSMatthew G Knepley 
34646636e97aSMatthew G Knepley   Input Parameters:
34656636e97aSMatthew G Knepley + dm - the DM
34666636e97aSMatthew G Knepley - c - coordinate vector
34676636e97aSMatthew G Knepley 
34686636e97aSMatthew G Knepley   Note:
34696636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
34706636e97aSMatthew G Knepley 
34716636e97aSMatthew G Knepley   Level: intermediate
34726636e97aSMatthew G Knepley 
34736636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
34746636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
34756636e97aSMatthew G Knepley @*/
34766636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
34776636e97aSMatthew G Knepley {
34786636e97aSMatthew G Knepley   PetscErrorCode ierr;
34796636e97aSMatthew G Knepley 
34806636e97aSMatthew G Knepley   PetscFunctionBegin;
34816636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34826636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
34836636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
34846636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
34856636e97aSMatthew G Knepley   dm->coordinates = c;
34866636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3487b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
348803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
34896636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34906636e97aSMatthew G Knepley }
34916636e97aSMatthew G Knepley 
34926636e97aSMatthew G Knepley #undef __FUNCT__
34936636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
34946636e97aSMatthew G Knepley /*@
34956636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
34966636e97aSMatthew G Knepley 
34976636e97aSMatthew G Knepley   Collective on DM
34986636e97aSMatthew G Knepley 
34996636e97aSMatthew G Knepley    Input Parameters:
35006636e97aSMatthew G Knepley +  dm - the DM
35016636e97aSMatthew G Knepley -  c - coordinate vector
35026636e97aSMatthew G Knepley 
35036636e97aSMatthew G Knepley   Note:
35046636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
35056636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
35066636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
35076636e97aSMatthew G Knepley 
35086636e97aSMatthew G Knepley   Level: intermediate
35096636e97aSMatthew G Knepley 
35106636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35116636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
35126636e97aSMatthew G Knepley @*/
35136636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
35146636e97aSMatthew G Knepley {
35156636e97aSMatthew G Knepley   PetscErrorCode ierr;
35166636e97aSMatthew G Knepley 
35176636e97aSMatthew G Knepley   PetscFunctionBegin;
35186636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35196636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
35206636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
35216636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
35228865f1eaSKarl Rupp 
35236636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
35248865f1eaSKarl Rupp 
35256636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
35266636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35276636e97aSMatthew G Knepley }
35286636e97aSMatthew G Knepley 
35296636e97aSMatthew G Knepley #undef __FUNCT__
35306636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
35316636e97aSMatthew G Knepley /*@
35326636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
35336636e97aSMatthew G Knepley 
35346636e97aSMatthew G Knepley   Not Collective
35356636e97aSMatthew G Knepley 
35366636e97aSMatthew G Knepley   Input Parameter:
35376636e97aSMatthew G Knepley . dm - the DM
35386636e97aSMatthew G Knepley 
35396636e97aSMatthew G Knepley   Output Parameter:
35406636e97aSMatthew G Knepley . c - global coordinate vector
35416636e97aSMatthew G Knepley 
35426636e97aSMatthew G Knepley   Note:
35436636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
35446636e97aSMatthew G Knepley 
35456636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
35466636e97aSMatthew G Knepley 
35476636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
35486636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
35496636e97aSMatthew G Knepley 
35506636e97aSMatthew G Knepley   Level: intermediate
35516636e97aSMatthew G Knepley 
35526636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35536636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
35546636e97aSMatthew G Knepley @*/
35556636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
35566636e97aSMatthew G Knepley {
35576636e97aSMatthew G Knepley   PetscErrorCode ierr;
35586636e97aSMatthew G Knepley 
35596636e97aSMatthew G Knepley   PetscFunctionBegin;
35606636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35616636e97aSMatthew G Knepley   PetscValidPointer(c,2);
35621f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
35630298fd71SBarry Smith     DM cdm = NULL;
35646636e97aSMatthew G Knepley 
35656636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
35666636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
35676636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
35686636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
35696636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
35706636e97aSMatthew G Knepley   }
35716636e97aSMatthew G Knepley   *c = dm->coordinates;
35726636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35736636e97aSMatthew G Knepley }
35746636e97aSMatthew G Knepley 
35756636e97aSMatthew G Knepley #undef __FUNCT__
35766636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
35776636e97aSMatthew G Knepley /*@
35786636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
35796636e97aSMatthew G Knepley 
35806636e97aSMatthew G Knepley   Collective on DM
35816636e97aSMatthew G Knepley 
35826636e97aSMatthew G Knepley   Input Parameter:
35836636e97aSMatthew G Knepley . dm - the DM
35846636e97aSMatthew G Knepley 
35856636e97aSMatthew G Knepley   Output Parameter:
35866636e97aSMatthew G Knepley . c - coordinate vector
35876636e97aSMatthew G Knepley 
35886636e97aSMatthew G Knepley   Note:
35896636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
35906636e97aSMatthew G Knepley 
35916636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
35926636e97aSMatthew G Knepley 
35936636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
35946636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
35956636e97aSMatthew G Knepley 
35966636e97aSMatthew G Knepley   Level: intermediate
35976636e97aSMatthew G Knepley 
35986636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35996636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
36006636e97aSMatthew G Knepley @*/
36016636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
36026636e97aSMatthew G Knepley {
36036636e97aSMatthew G Knepley   PetscErrorCode ierr;
36046636e97aSMatthew G Knepley 
36056636e97aSMatthew G Knepley   PetscFunctionBegin;
36066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36076636e97aSMatthew G Knepley   PetscValidPointer(c,2);
36081f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
36090298fd71SBarry Smith     DM cdm = NULL;
36106636e97aSMatthew G Knepley 
36116636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
36126636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
36136636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
36146636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
36156636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
36166636e97aSMatthew G Knepley   }
36176636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
36186636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36196636e97aSMatthew G Knepley }
36206636e97aSMatthew G Knepley 
36216636e97aSMatthew G Knepley #undef __FUNCT__
36226636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
36236636e97aSMatthew G Knepley /*@
36241cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
36256636e97aSMatthew G Knepley 
36266636e97aSMatthew G Knepley   Collective on DM
36276636e97aSMatthew G Knepley 
36286636e97aSMatthew G Knepley   Input Parameter:
36296636e97aSMatthew G Knepley . dm - the DM
36306636e97aSMatthew G Knepley 
36316636e97aSMatthew G Knepley   Output Parameter:
36326636e97aSMatthew G Knepley . cdm - coordinate DM
36336636e97aSMatthew G Knepley 
36346636e97aSMatthew G Knepley   Level: intermediate
36356636e97aSMatthew G Knepley 
36366636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36371cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
36386636e97aSMatthew G Knepley @*/
36396636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
36406636e97aSMatthew G Knepley {
36416636e97aSMatthew G Knepley   PetscErrorCode ierr;
36426636e97aSMatthew G Knepley 
36436636e97aSMatthew G Knepley   PetscFunctionBegin;
36446636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36456636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
36466636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
364782f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
36486636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
36496636e97aSMatthew G Knepley   }
36506636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
36516636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36526636e97aSMatthew G Knepley }
3653e87bb0d3SMatthew G Knepley 
3654e87bb0d3SMatthew G Knepley #undef __FUNCT__
36551cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
36561cfe2091SMatthew G. Knepley /*@
36571cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
36581cfe2091SMatthew G. Knepley 
36591cfe2091SMatthew G. Knepley   Logically Collective on DM
36601cfe2091SMatthew G. Knepley 
36611cfe2091SMatthew G. Knepley   Input Parameters:
36621cfe2091SMatthew G. Knepley + dm - the DM
36631cfe2091SMatthew G. Knepley - cdm - coordinate DM
36641cfe2091SMatthew G. Knepley 
36651cfe2091SMatthew G. Knepley   Level: intermediate
36661cfe2091SMatthew G. Knepley 
36671cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36681cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
36691cfe2091SMatthew G. Knepley @*/
36701cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
36711cfe2091SMatthew G. Knepley {
36721cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
36731cfe2091SMatthew G. Knepley 
36741cfe2091SMatthew G. Knepley   PetscFunctionBegin;
36751cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36761cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
36771cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
36781cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
36791cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
36801cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
36811cfe2091SMatthew G. Knepley }
36821cfe2091SMatthew G. Knepley 
36831cfe2091SMatthew G. Knepley #undef __FUNCT__
368446e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
368546e270d4SMatthew G. Knepley /*@
368646e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
368746e270d4SMatthew G. Knepley 
368846e270d4SMatthew G. Knepley   Not Collective
368946e270d4SMatthew G. Knepley 
369046e270d4SMatthew G. Knepley   Input Parameter:
369146e270d4SMatthew G. Knepley . dm - The DM object
369246e270d4SMatthew G. Knepley 
369346e270d4SMatthew G. Knepley   Output Parameter:
369446e270d4SMatthew G. Knepley . dim - The embedding dimension
369546e270d4SMatthew G. Knepley 
369646e270d4SMatthew G. Knepley   Level: intermediate
369746e270d4SMatthew G. Knepley 
369846e270d4SMatthew G. Knepley .keywords: mesh, coordinates
369946e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
370046e270d4SMatthew G. Knepley @*/
370146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
370246e270d4SMatthew G. Knepley {
370346e270d4SMatthew G. Knepley   PetscFunctionBegin;
370446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
370546e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
370646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
370746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
370846e270d4SMatthew G. Knepley }
370946e270d4SMatthew G. Knepley 
371046e270d4SMatthew G. Knepley #undef __FUNCT__
371146e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
371246e270d4SMatthew G. Knepley /*@
371346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
371446e270d4SMatthew G. Knepley 
371546e270d4SMatthew G. Knepley   Not Collective
371646e270d4SMatthew G. Knepley 
371746e270d4SMatthew G. Knepley   Input Parameters:
371846e270d4SMatthew G. Knepley + dm  - The DM object
371946e270d4SMatthew G. Knepley - dim - The embedding dimension
372046e270d4SMatthew G. Knepley 
372146e270d4SMatthew G. Knepley   Level: intermediate
372246e270d4SMatthew G. Knepley 
372346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
372446e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
372546e270d4SMatthew G. Knepley @*/
372646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
372746e270d4SMatthew G. Knepley {
372846e270d4SMatthew G. Knepley   PetscFunctionBegin;
372946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
373046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
373146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
373246e270d4SMatthew G. Knepley }
373346e270d4SMatthew G. Knepley 
373446e270d4SMatthew G. Knepley #undef __FUNCT__
3735e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
3736e8abe2deSMatthew G. Knepley /*@
3737e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
3738e8abe2deSMatthew G. Knepley 
3739e8abe2deSMatthew G. Knepley   Not Collective
3740e8abe2deSMatthew G. Knepley 
3741e8abe2deSMatthew G. Knepley   Input Parameter:
3742e8abe2deSMatthew G. Knepley . dm - The DM object
3743e8abe2deSMatthew G. Knepley 
3744e8abe2deSMatthew G. Knepley   Output Parameter:
3745e8abe2deSMatthew G. Knepley . section - The PetscSection object
3746e8abe2deSMatthew G. Knepley 
3747e8abe2deSMatthew G. Knepley   Level: intermediate
3748e8abe2deSMatthew G. Knepley 
3749e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
3750e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
3751e8abe2deSMatthew G. Knepley @*/
3752e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
3753e8abe2deSMatthew G. Knepley {
3754e8abe2deSMatthew G. Knepley   DM             cdm;
3755e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
3756e8abe2deSMatthew G. Knepley 
3757e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
3758e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3759e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
3760e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3761e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
3762e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
3763e8abe2deSMatthew G. Knepley }
3764e8abe2deSMatthew G. Knepley 
3765e8abe2deSMatthew G. Knepley #undef __FUNCT__
3766e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
3767e8abe2deSMatthew G. Knepley /*@
3768e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
3769e8abe2deSMatthew G. Knepley 
3770e8abe2deSMatthew G. Knepley   Not Collective
3771e8abe2deSMatthew G. Knepley 
3772e8abe2deSMatthew G. Knepley   Input Parameters:
3773e8abe2deSMatthew G. Knepley + dm      - The DM object
377446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
3775e8abe2deSMatthew G. Knepley - section - The PetscSection object
3776e8abe2deSMatthew G. Knepley 
3777e8abe2deSMatthew G. Knepley   Level: intermediate
3778e8abe2deSMatthew G. Knepley 
3779e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
3780e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
3781e8abe2deSMatthew G. Knepley @*/
378246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
3783e8abe2deSMatthew G. Knepley {
3784e8abe2deSMatthew G. Knepley   DM             cdm;
3785e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
3786e8abe2deSMatthew G. Knepley 
3787e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
3788e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
378946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
3790e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3791e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
379246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
379346e270d4SMatthew G. Knepley     PetscInt d = dim;
379446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
379546e270d4SMatthew G. Knepley 
379646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
379746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
379846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
379946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
380046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
380146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
380246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
380346e270d4SMatthew G. Knepley     }
380446e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
380546e270d4SMatthew G. Knepley   }
3806e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
3807e8abe2deSMatthew G. Knepley }
3808e8abe2deSMatthew G. Knepley 
3809e8abe2deSMatthew G. Knepley #undef __FUNCT__
3810c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
3811c6b900c6SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L)
3812c6b900c6SMatthew G. Knepley {
3813c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
3814c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3815c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
3816c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
3817c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
3818c6b900c6SMatthew G. Knepley }
3819c6b900c6SMatthew G. Knepley 
3820c6b900c6SMatthew G. Knepley #undef __FUNCT__
3821c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
3822c6b900c6SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[])
3823c6b900c6SMatthew G. Knepley {
3824c6b900c6SMatthew G. Knepley   Vec            coordinates;
3825c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
3826c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
3827c6b900c6SMatthew G. Knepley 
3828c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
3829c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3830c6b900c6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
3831c6b900c6SMatthew G. Knepley   ierr = VecGetBlockSize(coordinates, &dim);CHKERRQ(ierr);
3832c6b900c6SMatthew G. Knepley   ierr = PetscMalloc1(dim,&dm->L);CHKERRQ(ierr);
3833c6b900c6SMatthew G. Knepley   ierr = PetscMalloc1(dim,&dm->maxCell);CHKERRQ(ierr);
3834c6b900c6SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d];}
3835c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
3836c6b900c6SMatthew G. Knepley }
3837c6b900c6SMatthew G. Knepley 
3838c6b900c6SMatthew G. Knepley #undef __FUNCT__
3839e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3840e87bb0d3SMatthew G Knepley /*@
3841e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3842e87bb0d3SMatthew G Knepley 
3843e87bb0d3SMatthew G Knepley   Not collective
3844e87bb0d3SMatthew G Knepley 
3845e87bb0d3SMatthew G Knepley   Input Parameters:
3846e87bb0d3SMatthew G Knepley + dm - The DM
3847e87bb0d3SMatthew G Knepley - v - The Vec of points
3848e87bb0d3SMatthew G Knepley 
384961e3bb9bSMatthew G Knepley   Output Parameter:
3850e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3851e87bb0d3SMatthew G Knepley 
3852e87bb0d3SMatthew G Knepley   Level: developer
385361e3bb9bSMatthew G Knepley 
385461e3bb9bSMatthew G Knepley .keywords: point location, mesh
385561e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
385661e3bb9bSMatthew G Knepley @*/
3857e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3858e87bb0d3SMatthew G Knepley {
3859735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3860735aa83eSMatthew G Knepley 
3861e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3862e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3863e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3864e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3865735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3866735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
386782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3868e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3869e87bb0d3SMatthew G Knepley }
387014f150ffSMatthew G. Knepley 
387114f150ffSMatthew G. Knepley #undef __FUNCT__
387214f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
3873f4d763aaSMatthew G. Knepley /*@
3874f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
3875f4d763aaSMatthew G. Knepley 
3876f4d763aaSMatthew G. Knepley   Input Parameter:
3877f4d763aaSMatthew G. Knepley . dm - The original DM
3878f4d763aaSMatthew G. Knepley 
3879f4d763aaSMatthew G. Knepley   Output Parameter:
3880f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
3881f4d763aaSMatthew G. Knepley 
3882f4d763aaSMatthew G. Knepley   Level: intermediate
3883f4d763aaSMatthew G. Knepley 
3884f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
3885f4d763aaSMatthew G. Knepley @*/
388614f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
388714f150ffSMatthew G. Knepley {
3888c26acbdeSMatthew G. Knepley   PetscSection   section;
3889c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
389014f150ffSMatthew G. Knepley   PetscErrorCode ierr;
389114f150ffSMatthew G. Knepley 
389214f150ffSMatthew G. Knepley   PetscFunctionBegin;
389314f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
389414f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
3895c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
3896c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
3897c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
3898c26acbdeSMatthew G. Knepley     *odm = dm;
3899c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
3900c26acbdeSMatthew G. Knepley   }
390114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
3902c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
390314f150ffSMatthew G. Knepley     PetscSF      sf;
390414f150ffSMatthew G. Knepley 
390514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
390614f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
390714f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
390814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
390914f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
391014f150ffSMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, &gsection);CHKERRQ(ierr);
391114f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
391214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
391314f150ffSMatthew G. Knepley   }
391414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
391514f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
391614f150ffSMatthew G. Knepley }
3917f4d763aaSMatthew G. Knepley 
3918f4d763aaSMatthew G. Knepley #undef __FUNCT__
3919f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
3920f4d763aaSMatthew G. Knepley /*@
3921cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
3922f4d763aaSMatthew G. Knepley 
3923f4d763aaSMatthew G. Knepley   Input Parameter:
3924f4d763aaSMatthew G. Knepley . dm - The original DM
3925f4d763aaSMatthew G. Knepley 
3926cdb7a50dSMatthew G. Knepley   Output Parameters:
3927cdb7a50dSMatthew G. Knepley + num - The output sequence number
3928cdb7a50dSMatthew G. Knepley - val - The output sequence value
3929f4d763aaSMatthew G. Knepley 
3930f4d763aaSMatthew G. Knepley   Level: intermediate
3931f4d763aaSMatthew G. Knepley 
3932f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
3933f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
3934f4d763aaSMatthew G. Knepley 
3935f4d763aaSMatthew G. Knepley .seealso: VecView()
3936f4d763aaSMatthew G. Knepley @*/
3937cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
3938f4d763aaSMatthew G. Knepley {
3939f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
3940f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3941cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
3942cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
3943f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
3944f4d763aaSMatthew G. Knepley }
3945f4d763aaSMatthew G. Knepley 
3946f4d763aaSMatthew G. Knepley #undef __FUNCT__
3947f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
3948f4d763aaSMatthew G. Knepley /*@
3949cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
3950f4d763aaSMatthew G. Knepley 
3951f4d763aaSMatthew G. Knepley   Input Parameters:
3952f4d763aaSMatthew G. Knepley + dm - The original DM
3953cdb7a50dSMatthew G. Knepley . num - The output sequence number
3954cdb7a50dSMatthew G. Knepley - val - The output sequence value
3955f4d763aaSMatthew G. Knepley 
3956f4d763aaSMatthew G. Knepley   Level: intermediate
3957f4d763aaSMatthew G. Knepley 
3958f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
3959f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
3960f4d763aaSMatthew G. Knepley 
3961f4d763aaSMatthew G. Knepley .seealso: VecView()
3962f4d763aaSMatthew G. Knepley @*/
3963cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
3964f4d763aaSMatthew G. Knepley {
3965f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
3966f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3967f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
3968cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
3969cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
3970cdb7a50dSMatthew G. Knepley }
3971cdb7a50dSMatthew G. Knepley 
3972cdb7a50dSMatthew G. Knepley #undef __FUNCT__
3973cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
3974cdb7a50dSMatthew G. Knepley /*@C
3975cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
3976cdb7a50dSMatthew G. Knepley 
3977cdb7a50dSMatthew G. Knepley   Input Parameters:
3978cdb7a50dSMatthew G. Knepley + dm   - The original DM
3979cdb7a50dSMatthew G. Knepley . name - The sequence name
3980cdb7a50dSMatthew G. Knepley - num  - The output sequence number
3981cdb7a50dSMatthew G. Knepley 
3982cdb7a50dSMatthew G. Knepley   Output Parameter:
3983cdb7a50dSMatthew G. Knepley . val  - The output sequence value
3984cdb7a50dSMatthew G. Knepley 
3985cdb7a50dSMatthew G. Knepley   Level: intermediate
3986cdb7a50dSMatthew G. Knepley 
3987cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
3988cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
3989cdb7a50dSMatthew G. Knepley 
3990cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
3991cdb7a50dSMatthew G. Knepley @*/
3992cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
3993cdb7a50dSMatthew G. Knepley {
3994cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
3995cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
3996cdb7a50dSMatthew G. Knepley 
3997cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
3998cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3999cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4000cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4001cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4002cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4003cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4004cdb7a50dSMatthew G. Knepley     PetscScalar value;
4005cdb7a50dSMatthew G. Knepley 
4006cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
40074aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4008cdb7a50dSMatthew G. Knepley #endif
4009cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4010f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4011f4d763aaSMatthew G. Knepley }
4012