xref: /petsc/src/dm/interface/dm.c (revision cdb7a50d6a9f8d660b40f78cea9f3e1b894b0dc1)
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;
65*cdb7a50dSMatthew 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;
9438221697SMatthew G. Knepley   PetscErrorCode ierr;
9538221697SMatthew G. Knepley 
9638221697SMatthew G. Knepley   PetscFunctionBegin;
9738221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9838221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
9938221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
10038221697SMatthew G. Knepley   if (dm->ops->clone) {
10138221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
10238221697SMatthew G. Knepley   }
103cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
10438221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
10538221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
10638221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
10738221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
10838221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
10938221697SMatthew G. Knepley   if (coords) {
11038221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
11138221697SMatthew G. Knepley   } else {
11238221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
11338221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
11438221697SMatthew G. Knepley   }
115c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
116c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
117c6b900c6SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L);CHKERRQ(ierr);
118c6b900c6SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L);CHKERRQ(ierr);
119c6b900c6SMatthew G. Knepley   }
12038221697SMatthew G. Knepley   PetscFunctionReturn(0);
12138221697SMatthew G. Knepley }
12238221697SMatthew G. Knepley 
12338221697SMatthew G. Knepley #undef __FUNCT__
1249a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1259a42bb27SBarry Smith /*@C
126564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1279a42bb27SBarry Smith 
128aa219208SBarry Smith    Logically Collective on DMDA
1299a42bb27SBarry Smith 
1309a42bb27SBarry Smith    Input Parameter:
1319a42bb27SBarry Smith +  da - initial distributed array
1328154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1339a42bb27SBarry Smith 
1349a42bb27SBarry Smith    Options Database:
135dd85299cSBarry Smith .   -dm_vec_type ctype
1369a42bb27SBarry Smith 
1379a42bb27SBarry Smith    Level: intermediate
1389a42bb27SBarry Smith 
139c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType, DMGetVecType()
1409a42bb27SBarry Smith @*/
14119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1429a42bb27SBarry Smith {
1439a42bb27SBarry Smith   PetscErrorCode ierr;
1449a42bb27SBarry Smith 
1459a42bb27SBarry Smith   PetscFunctionBegin;
1469a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1479a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
14819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1499a42bb27SBarry Smith   PetscFunctionReturn(0);
1509a42bb27SBarry Smith }
1519a42bb27SBarry Smith 
1529a42bb27SBarry Smith #undef __FUNCT__
153c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
154c0dedaeaSBarry Smith /*@C
155c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
156c0dedaeaSBarry Smith 
157c0dedaeaSBarry Smith    Logically Collective on DMDA
158c0dedaeaSBarry Smith 
159c0dedaeaSBarry Smith    Input Parameter:
160c0dedaeaSBarry Smith .  da - initial distributed array
161c0dedaeaSBarry Smith 
162c0dedaeaSBarry Smith    Output Parameter:
163c0dedaeaSBarry Smith .  ctype - the vector type
164c0dedaeaSBarry Smith 
165c0dedaeaSBarry Smith    Level: intermediate
166c0dedaeaSBarry Smith 
167c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
168c0dedaeaSBarry Smith @*/
169c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
170c0dedaeaSBarry Smith {
171c0dedaeaSBarry Smith   PetscFunctionBegin;
172c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
173c0dedaeaSBarry Smith   *ctype = da->vectype;
174c0dedaeaSBarry Smith   PetscFunctionReturn(0);
175c0dedaeaSBarry Smith }
176c0dedaeaSBarry Smith 
177c0dedaeaSBarry Smith #undef __FUNCT__
1785f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1795f1ad066SMatthew G Knepley /*@
18034f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1815f1ad066SMatthew G Knepley 
1825f1ad066SMatthew G Knepley   Not collective
1835f1ad066SMatthew G Knepley 
1845f1ad066SMatthew G Knepley   Input Parameter:
1855f1ad066SMatthew G Knepley . v - The Vec
1865f1ad066SMatthew G Knepley 
1875f1ad066SMatthew G Knepley   Output Parameter:
1885f1ad066SMatthew G Knepley . dm - The DM
1895f1ad066SMatthew G Knepley 
1905f1ad066SMatthew G Knepley   Level: intermediate
1915f1ad066SMatthew G Knepley 
1925f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1935f1ad066SMatthew G Knepley @*/
1945f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1955f1ad066SMatthew G Knepley {
1965f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1975f1ad066SMatthew G Knepley 
1985f1ad066SMatthew G Knepley   PetscFunctionBegin;
1995f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2005f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2015f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2025f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2035f1ad066SMatthew G Knepley }
2045f1ad066SMatthew G Knepley 
2055f1ad066SMatthew G Knepley #undef __FUNCT__
2065f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2075f1ad066SMatthew G Knepley /*@
2085f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
2095f1ad066SMatthew G Knepley 
2105f1ad066SMatthew G Knepley   Not collective
2115f1ad066SMatthew G Knepley 
2125f1ad066SMatthew G Knepley   Input Parameters:
2135f1ad066SMatthew G Knepley + v - The Vec
2145f1ad066SMatthew G Knepley - dm - The DM
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley   Level: intermediate
2175f1ad066SMatthew G Knepley 
2185f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2195f1ad066SMatthew G Knepley @*/
2205f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2215f1ad066SMatthew G Knepley {
2225f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2235f1ad066SMatthew G Knepley 
2245f1ad066SMatthew G Knepley   PetscFunctionBegin;
2255f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
226d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2275f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2285f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2295f1ad066SMatthew G Knepley }
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley #undef __FUNCT__
232521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
233521d9a4cSLisandro Dalcin /*@C
234521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
235521d9a4cSLisandro Dalcin 
236521d9a4cSLisandro Dalcin    Logically Collective on DM
237521d9a4cSLisandro Dalcin 
238521d9a4cSLisandro Dalcin    Input Parameter:
239521d9a4cSLisandro Dalcin +  dm - the DM context
240521d9a4cSLisandro Dalcin .  ctype - the matrix type
241521d9a4cSLisandro Dalcin 
242521d9a4cSLisandro Dalcin    Options Database:
243521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
244521d9a4cSLisandro Dalcin 
245521d9a4cSLisandro Dalcin    Level: intermediate
246521d9a4cSLisandro Dalcin 
247c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
248521d9a4cSLisandro Dalcin @*/
24919fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
250521d9a4cSLisandro Dalcin {
251521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
25288f0584fSBarry Smith 
253521d9a4cSLisandro Dalcin   PetscFunctionBegin;
254521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
255521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
25619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
257521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
258521d9a4cSLisandro Dalcin }
259521d9a4cSLisandro Dalcin 
260521d9a4cSLisandro Dalcin #undef __FUNCT__
261c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
262c0dedaeaSBarry Smith /*@C
263c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
264c0dedaeaSBarry Smith 
265c0dedaeaSBarry Smith    Logically Collective on DM
266c0dedaeaSBarry Smith 
267c0dedaeaSBarry Smith    Input Parameter:
268c0dedaeaSBarry Smith .  dm - the DM context
269c0dedaeaSBarry Smith 
270c0dedaeaSBarry Smith    Output Parameter:
271c0dedaeaSBarry Smith .  ctype - the matrix type
272c0dedaeaSBarry Smith 
273c0dedaeaSBarry Smith    Options Database:
274c0dedaeaSBarry Smith .   -dm_mat_type ctype
275c0dedaeaSBarry Smith 
276c0dedaeaSBarry Smith    Level: intermediate
277c0dedaeaSBarry Smith 
278c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
279c0dedaeaSBarry Smith @*/
280c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
281c0dedaeaSBarry Smith {
282c0dedaeaSBarry Smith   PetscFunctionBegin;
283c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
284c0dedaeaSBarry Smith   *ctype = dm->mattype;
285c0dedaeaSBarry Smith   PetscFunctionReturn(0);
286c0dedaeaSBarry Smith }
287c0dedaeaSBarry Smith 
288c0dedaeaSBarry Smith #undef __FUNCT__
289c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
290c688c046SMatthew G Knepley /*@
29134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
292c688c046SMatthew G Knepley 
293c688c046SMatthew G Knepley   Not collective
294c688c046SMatthew G Knepley 
295c688c046SMatthew G Knepley   Input Parameter:
296c688c046SMatthew G Knepley . A - The Mat
297c688c046SMatthew G Knepley 
298c688c046SMatthew G Knepley   Output Parameter:
299c688c046SMatthew G Knepley . dm - The DM
300c688c046SMatthew G Knepley 
301c688c046SMatthew G Knepley   Level: intermediate
302c688c046SMatthew G Knepley 
303c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
304c688c046SMatthew G Knepley @*/
305c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
306c688c046SMatthew G Knepley {
307c688c046SMatthew G Knepley   PetscErrorCode ierr;
308c688c046SMatthew G Knepley 
309c688c046SMatthew G Knepley   PetscFunctionBegin;
310c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
311c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
312c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
313c688c046SMatthew G Knepley   PetscFunctionReturn(0);
314c688c046SMatthew G Knepley }
315c688c046SMatthew G Knepley 
316c688c046SMatthew G Knepley #undef __FUNCT__
317c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
318c688c046SMatthew G Knepley /*@
319c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
320c688c046SMatthew G Knepley 
321c688c046SMatthew G Knepley   Not collective
322c688c046SMatthew G Knepley 
323c688c046SMatthew G Knepley   Input Parameters:
324c688c046SMatthew G Knepley + A - The Mat
325c688c046SMatthew G Knepley - dm - The DM
326c688c046SMatthew G Knepley 
327c688c046SMatthew G Knepley   Level: intermediate
328c688c046SMatthew G Knepley 
329c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
330c688c046SMatthew G Knepley @*/
331c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
332c688c046SMatthew G Knepley {
333c688c046SMatthew G Knepley   PetscErrorCode ierr;
334c688c046SMatthew G Knepley 
335c688c046SMatthew G Knepley   PetscFunctionBegin;
336c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3378865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
338c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
339c688c046SMatthew G Knepley   PetscFunctionReturn(0);
340c688c046SMatthew G Knepley }
341c688c046SMatthew G Knepley 
342c688c046SMatthew G Knepley #undef __FUNCT__
3439a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3449a42bb27SBarry Smith /*@C
3459a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
346aa219208SBarry Smith    DMDA options in the database.
3479a42bb27SBarry Smith 
348aa219208SBarry Smith    Logically Collective on DMDA
3499a42bb27SBarry Smith 
3509a42bb27SBarry Smith    Input Parameter:
351aa219208SBarry Smith +  da - the DMDA context
3529a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3539a42bb27SBarry Smith 
3549a42bb27SBarry Smith    Notes:
3559a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3569a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3579a42bb27SBarry Smith 
3589a42bb27SBarry Smith    Level: advanced
3599a42bb27SBarry Smith 
360aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
3619a42bb27SBarry Smith 
3629a42bb27SBarry Smith .seealso: DMSetFromOptions()
3639a42bb27SBarry Smith @*/
3647087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3659a42bb27SBarry Smith {
3669a42bb27SBarry Smith   PetscErrorCode ierr;
3679a42bb27SBarry Smith 
3689a42bb27SBarry Smith   PetscFunctionBegin;
3699a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3709a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
3719a42bb27SBarry Smith   PetscFunctionReturn(0);
3729a42bb27SBarry Smith }
3739a42bb27SBarry Smith 
3749a42bb27SBarry Smith #undef __FUNCT__
37547c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
37647c6ae99SBarry Smith /*@
377aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
37847c6ae99SBarry Smith 
37947c6ae99SBarry Smith     Collective on DM
38047c6ae99SBarry Smith 
38147c6ae99SBarry Smith     Input Parameter:
38247c6ae99SBarry Smith .   dm - the DM object to destroy
38347c6ae99SBarry Smith 
38447c6ae99SBarry Smith     Level: developer
38547c6ae99SBarry Smith 
386e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
38747c6ae99SBarry Smith 
38847c6ae99SBarry Smith @*/
389fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
39047c6ae99SBarry Smith {
3910f21e855SMatthew G. Knepley   PetscInt       i, cnt = 0, Nf = 0, f;
392dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
39347c6ae99SBarry Smith   PetscErrorCode ierr;
39447c6ae99SBarry Smith 
39547c6ae99SBarry Smith   PetscFunctionBegin;
3966bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
3976bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
39887e657c6SBarry Smith 
3990f21e855SMatthew G. Knepley   if ((*dm)->prob) {
4000f21e855SMatthew G. Knepley     PetscObject disc;
4010f21e855SMatthew G. Knepley 
402a546ed68SMatthew G. Knepley     /* I think it makes sense to dump all attached things when you are destroyed, which also eliminates circular references */
4032764a2aaSMatthew G. Knepley     ierr = PetscDSGetNumFields((*dm)->prob, &Nf);CHKERRQ(ierr);
4040f21e855SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4052764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization((*dm)->prob, f, &disc);CHKERRQ(ierr);
4060f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "pmat", NULL);CHKERRQ(ierr);
4070f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "nullspace", NULL);CHKERRQ(ierr);
4080f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "nearnullspace", NULL);CHKERRQ(ierr);
4090f21e855SMatthew G. Knepley     }
410a546ed68SMatthew G. Knepley   }
41187e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
412732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4138865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4148865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
415732e2eb9SMatthew G Knepley   }
416dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4172348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4182348bcf4SPeter Brune   if ((*dm)->x) {
4192348bcf4SPeter Brune     DM obj;
4202348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4212348bcf4SPeter Brune     if (obj == *dm) cnt++;
4222348bcf4SPeter Brune   }
423732e2eb9SMatthew G Knepley 
4246bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
425732e2eb9SMatthew G Knepley   /*
426732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
427732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
428732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
429732e2eb9SMatthew G Knepley   */
4306bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4316bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
432732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4336bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4346bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
435732e2eb9SMatthew G Knepley   }
436f490541aSPeter Brune   nnext=(*dm)->namedglobal;
4370298fd71SBarry Smith   (*dm)->namedglobal = NULL;
438f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
4392348bcf4SPeter Brune     nnext = nlink->next;
4402348bcf4SPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
4412348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4422348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4432348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4442348bcf4SPeter Brune   }
445f490541aSPeter Brune   nnext=(*dm)->namedlocal;
4460298fd71SBarry Smith   (*dm)->namedlocal = NULL;
447f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
448f490541aSPeter Brune     nnext = nlink->next;
449f490541aSPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
450f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
451f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
452f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
453f490541aSPeter Brune   }
4542348bcf4SPeter Brune 
455b17ce1afSJed Brown   /* Destroy the list of hooks */
456c833c3b5SJed Brown   {
457c833c3b5SJed Brown     DMCoarsenHookLink link,next;
458b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
459b17ce1afSJed Brown       next = link->next;
460b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
461b17ce1afSJed Brown     }
4620298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
463c833c3b5SJed Brown   }
464c833c3b5SJed Brown   {
465c833c3b5SJed Brown     DMRefineHookLink link,next;
466c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
467c833c3b5SJed Brown       next = link->next;
468c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
469c833c3b5SJed Brown     }
4700298fd71SBarry Smith     (*dm)->refinehook = NULL;
471c833c3b5SJed Brown   }
472be081cd6SPeter Brune   {
473be081cd6SPeter Brune     DMSubDomainHookLink link,next;
474be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
475be081cd6SPeter Brune       next = link->next;
476be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
477be081cd6SPeter Brune     }
4780298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
479be081cd6SPeter Brune   }
480baf369e7SPeter Brune   {
481baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
482baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
483baf369e7SPeter Brune       next = link->next;
484baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
485baf369e7SPeter Brune     }
4860298fd71SBarry Smith     (*dm)->gtolhook = NULL;
487baf369e7SPeter Brune   }
488aa1993deSMatthew G Knepley   /* Destroy the work arrays */
489aa1993deSMatthew G Knepley   {
490aa1993deSMatthew G Knepley     DMWorkLink link,next;
491aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
492aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
493aa1993deSMatthew G Knepley       next = link->next;
494aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
495aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
496aa1993deSMatthew G Knepley     }
4970298fd71SBarry Smith     (*dm)->workin = NULL;
498aa1993deSMatthew G Knepley   }
499b17ce1afSJed Brown 
50052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
50152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
50252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
50352536dc3SBarry Smith 
5041a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5051a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5061a266240SBarry Smith   }
50787e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
50871cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5094dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5106bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5116bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
512073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
51388ed4aceSMatthew G Knepley 
51488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
51588ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
5168b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
51788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
51888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
519af122d2aSMatthew G Knepley 
5206636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
5216636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
5226636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
523c6b900c6SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
524c6b900c6SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
5256636e97aSMatthew G Knepley 
5262764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
52714f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
528e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
529e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
530732e2eb9SMatthew G Knepley 
5316bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
532435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
533732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
53447c6ae99SBarry Smith   PetscFunctionReturn(0);
53547c6ae99SBarry Smith }
53647c6ae99SBarry Smith 
53747c6ae99SBarry Smith #undef __FUNCT__
538d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
539d7bf68aeSBarry Smith /*@
540d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
541d7bf68aeSBarry Smith 
542d7bf68aeSBarry Smith     Collective on DM
543d7bf68aeSBarry Smith 
544d7bf68aeSBarry Smith     Input Parameter:
545d7bf68aeSBarry Smith .   dm - the DM object to setup
546d7bf68aeSBarry Smith 
547d7bf68aeSBarry Smith     Level: developer
548d7bf68aeSBarry Smith 
549e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
550d7bf68aeSBarry Smith 
551d7bf68aeSBarry Smith @*/
5527087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
553d7bf68aeSBarry Smith {
554d7bf68aeSBarry Smith   PetscErrorCode ierr;
555d7bf68aeSBarry Smith 
556d7bf68aeSBarry Smith   PetscFunctionBegin;
557171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5588387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
559d7bf68aeSBarry Smith   if (dm->ops->setup) {
560d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
561d7bf68aeSBarry Smith   }
5628387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
563d7bf68aeSBarry Smith   PetscFunctionReturn(0);
564d7bf68aeSBarry Smith }
565d7bf68aeSBarry Smith 
566d7bf68aeSBarry Smith #undef __FUNCT__
567d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
568d7bf68aeSBarry Smith /*@
569d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
570d7bf68aeSBarry Smith 
571d7bf68aeSBarry Smith     Collective on DM
572d7bf68aeSBarry Smith 
573d7bf68aeSBarry Smith     Input Parameter:
574d7bf68aeSBarry Smith .   dm - the DM object to set options for
575d7bf68aeSBarry Smith 
576732e2eb9SMatthew G Knepley     Options Database:
577dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
578dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
579171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
580171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
581732e2eb9SMatthew G Knepley 
582d7bf68aeSBarry Smith     Level: developer
583d7bf68aeSBarry Smith 
584e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
585d7bf68aeSBarry Smith 
586d7bf68aeSBarry Smith @*/
5877087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
588d7bf68aeSBarry Smith {
5897781c08eSBarry Smith   char           typeName[256];
590ca266f36SBarry Smith   PetscBool      flg;
591d7bf68aeSBarry Smith   PetscErrorCode ierr;
592d7bf68aeSBarry Smith 
593d7bf68aeSBarry Smith   PetscFunctionBegin;
594171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5953194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
5960298fd71SBarry 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);
597a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
598f9ba7244SBarry Smith   if (flg) {
599f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
600f9ba7244SBarry Smith   }
601a264d7a6SBarry 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);
602073dac72SJed Brown   if (flg) {
603521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
604073dac72SJed Brown   }
6050298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
606f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
607f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
608f9ba7244SBarry Smith   }
609f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
610f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
61182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
612d7bf68aeSBarry Smith   PetscFunctionReturn(0);
613d7bf68aeSBarry Smith }
614d7bf68aeSBarry Smith 
615d7bf68aeSBarry Smith #undef __FUNCT__
61647c6ae99SBarry Smith #define __FUNCT__ "DMView"
617fc9bc008SSatish Balay /*@C
618aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
61947c6ae99SBarry Smith 
62047c6ae99SBarry Smith     Collective on DM
62147c6ae99SBarry Smith 
62247c6ae99SBarry Smith     Input Parameter:
62347c6ae99SBarry Smith +   dm - the DM object to view
62447c6ae99SBarry Smith -   v - the viewer
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith     Level: developer
62747c6ae99SBarry Smith 
628e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
62947c6ae99SBarry Smith 
63047c6ae99SBarry Smith @*/
6317087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
63247c6ae99SBarry Smith {
63347c6ae99SBarry Smith   PetscErrorCode ierr;
63432c0f0efSBarry Smith   PetscBool      isbinary;
63547c6ae99SBarry Smith 
63647c6ae99SBarry Smith   PetscFunctionBegin;
637171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6383014e516SBarry Smith   if (!v) {
639ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6403014e516SBarry Smith   }
64198c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
64232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
64332c0f0efSBarry Smith   if (isbinary) {
64455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
64532c0f0efSBarry Smith     char     type[256];
64632c0f0efSBarry Smith 
64732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
64832c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
64932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
65032c0f0efSBarry Smith   }
6510c010503SBarry Smith   if (dm->ops->view) {
6520c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
65347c6ae99SBarry Smith   }
65447c6ae99SBarry Smith   PetscFunctionReturn(0);
65547c6ae99SBarry Smith }
65647c6ae99SBarry Smith 
65747c6ae99SBarry Smith #undef __FUNCT__
65847c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
65947c6ae99SBarry Smith /*@
660aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith     Collective on DM
66347c6ae99SBarry Smith 
66447c6ae99SBarry Smith     Input Parameter:
66547c6ae99SBarry Smith .   dm - the DM object
66647c6ae99SBarry Smith 
66747c6ae99SBarry Smith     Output Parameter:
66847c6ae99SBarry Smith .   vec - the global vector
66947c6ae99SBarry Smith 
670073dac72SJed Brown     Level: beginner
67147c6ae99SBarry Smith 
672e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
67347c6ae99SBarry Smith 
67447c6ae99SBarry Smith @*/
6757087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
67647c6ae99SBarry Smith {
67747c6ae99SBarry Smith   PetscErrorCode ierr;
67847c6ae99SBarry Smith 
67947c6ae99SBarry Smith   PetscFunctionBegin;
680171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
68247c6ae99SBarry Smith   PetscFunctionReturn(0);
68347c6ae99SBarry Smith }
68447c6ae99SBarry Smith 
68547c6ae99SBarry Smith #undef __FUNCT__
68647c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
68747c6ae99SBarry Smith /*@
688aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
68947c6ae99SBarry Smith 
69047c6ae99SBarry Smith     Not Collective
69147c6ae99SBarry Smith 
69247c6ae99SBarry Smith     Input Parameter:
69347c6ae99SBarry Smith .   dm - the DM object
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith     Output Parameter:
69647c6ae99SBarry Smith .   vec - the local vector
69747c6ae99SBarry Smith 
698073dac72SJed Brown     Level: beginner
69947c6ae99SBarry Smith 
700e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
70147c6ae99SBarry Smith 
70247c6ae99SBarry Smith @*/
7037087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
70447c6ae99SBarry Smith {
70547c6ae99SBarry Smith   PetscErrorCode ierr;
70647c6ae99SBarry Smith 
70747c6ae99SBarry Smith   PetscFunctionBegin;
708171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
70947c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
71047c6ae99SBarry Smith   PetscFunctionReturn(0);
71147c6ae99SBarry Smith }
71247c6ae99SBarry Smith 
71347c6ae99SBarry Smith #undef __FUNCT__
7141411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
7151411c6eeSJed Brown /*@
7161411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
7171411c6eeSJed Brown 
7181411c6eeSJed Brown    Collective on DM
7191411c6eeSJed Brown 
7201411c6eeSJed Brown    Input Parameter:
7211411c6eeSJed Brown .  dm - the DM that provides the mapping
7221411c6eeSJed Brown 
7231411c6eeSJed Brown    Output Parameter:
7241411c6eeSJed Brown .  ltog - the mapping
7251411c6eeSJed Brown 
7261411c6eeSJed Brown    Level: intermediate
7271411c6eeSJed Brown 
7281411c6eeSJed Brown    Notes:
7291411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
7301411c6eeSJed Brown    MatSetLocalToGlobalMapping().
7311411c6eeSJed Brown 
732fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
7331411c6eeSJed Brown @*/
7347087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7351411c6eeSJed Brown {
7361411c6eeSJed Brown   PetscErrorCode ierr;
7371411c6eeSJed Brown 
7381411c6eeSJed Brown   PetscFunctionBegin;
7391411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7401411c6eeSJed Brown   PetscValidPointer(ltog,2);
7411411c6eeSJed Brown   if (!dm->ltogmap) {
74237d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
74337d0c07bSMatthew G Knepley 
74437d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
74537d0c07bSMatthew G Knepley     if (section) {
74637d0c07bSMatthew G Knepley       PetscInt *ltog;
74737d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
74837d0c07bSMatthew G Knepley 
74937d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
75037d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
75137d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
752785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
75337d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
75437d0c07bSMatthew G Knepley         PetscInt dof, off, c;
75537d0c07bSMatthew G Knepley 
75637d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
75737d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
75837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
75937d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
76037d0c07bSMatthew G Knepley           ltog[l] = off+c;
76137d0c07bSMatthew G Knepley         }
76237d0c07bSMatthew G Knepley       }
763f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
7643bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
76537d0c07bSMatthew G Knepley     } else {
766184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
767184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7681411c6eeSJed Brown     }
76937d0c07bSMatthew G Knepley   }
7701411c6eeSJed Brown   *ltog = dm->ltogmap;
7711411c6eeSJed Brown   PetscFunctionReturn(0);
7721411c6eeSJed Brown }
7731411c6eeSJed Brown 
7741411c6eeSJed Brown #undef __FUNCT__
7751411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7761411c6eeSJed Brown /*@
7771411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7781411c6eeSJed Brown 
7791411c6eeSJed Brown    Not Collective
7801411c6eeSJed Brown 
7811411c6eeSJed Brown    Input Parameter:
7821411c6eeSJed Brown .  dm - the DM with block structure
7831411c6eeSJed Brown 
7841411c6eeSJed Brown    Output Parameter:
7851411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7861411c6eeSJed Brown 
7871411c6eeSJed Brown    Level: intermediate
7881411c6eeSJed Brown 
789fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
7901411c6eeSJed Brown @*/
7917087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7921411c6eeSJed Brown {
7931411c6eeSJed Brown   PetscFunctionBegin;
7941411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7951411c6eeSJed Brown   PetscValidPointer(bs,2);
7961411c6eeSJed 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");
7971411c6eeSJed Brown   *bs = dm->bs;
7981411c6eeSJed Brown   PetscFunctionReturn(0);
7991411c6eeSJed Brown }
8001411c6eeSJed Brown 
8011411c6eeSJed Brown #undef __FUNCT__
802e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
80347c6ae99SBarry Smith /*@
804e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
80547c6ae99SBarry Smith 
80647c6ae99SBarry Smith     Collective on DM
80747c6ae99SBarry Smith 
80847c6ae99SBarry Smith     Input Parameter:
80947c6ae99SBarry Smith +   dm1 - the DM object
81047c6ae99SBarry Smith -   dm2 - the second, finer DM object
81147c6ae99SBarry Smith 
81247c6ae99SBarry Smith     Output Parameter:
81347c6ae99SBarry Smith +  mat - the interpolation
81447c6ae99SBarry Smith -  vec - the scaling (optional)
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith     Level: developer
81747c6ae99SBarry Smith 
81885afcc9aSBarry 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
81985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
820d52bd9f3SBarry Smith 
8211f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
822d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
82385afcc9aSBarry Smith 
82485afcc9aSBarry Smith 
825e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
82647c6ae99SBarry Smith 
82747c6ae99SBarry Smith @*/
828e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
82947c6ae99SBarry Smith {
83047c6ae99SBarry Smith   PetscErrorCode ierr;
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith   PetscFunctionBegin;
833171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
834171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
83647c6ae99SBarry Smith   PetscFunctionReturn(0);
83747c6ae99SBarry Smith }
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith #undef __FUNCT__
840e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
84147c6ae99SBarry Smith /*@
842e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith     Collective on DM
84547c6ae99SBarry Smith 
84647c6ae99SBarry Smith     Input Parameter:
84747c6ae99SBarry Smith +   dm1 - the DM object
84847c6ae99SBarry Smith -   dm2 - the second, finer DM object
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith     Output Parameter:
85147c6ae99SBarry Smith .   ctx - the injection
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Level: developer
85447c6ae99SBarry Smith 
85585afcc9aSBarry 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
85685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
85785afcc9aSBarry Smith 
858e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
85947c6ae99SBarry Smith 
86047c6ae99SBarry Smith @*/
861e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
86247c6ae99SBarry Smith {
86347c6ae99SBarry Smith   PetscErrorCode ierr;
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith   PetscFunctionBegin;
866171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
867171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
86847c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
86947c6ae99SBarry Smith   PetscFunctionReturn(0);
87047c6ae99SBarry Smith }
87147c6ae99SBarry Smith 
87247c6ae99SBarry Smith #undef __FUNCT__
873e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
874b412c318SBarry Smith /*@
875b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith     Collective on DM
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith     Input Parameter:
88047c6ae99SBarry Smith +   dm - the DM object
881b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith     Output Parameter:
88447c6ae99SBarry Smith .   coloring - the coloring
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith     Level: developer
88747c6ae99SBarry Smith 
888b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
88947c6ae99SBarry Smith 
890aab9d709SJed Brown @*/
891b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
89247c6ae99SBarry Smith {
89347c6ae99SBarry Smith   PetscErrorCode ierr;
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith   PetscFunctionBegin;
896171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
897ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
898b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
89947c6ae99SBarry Smith   PetscFunctionReturn(0);
90047c6ae99SBarry Smith }
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith #undef __FUNCT__
903950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
904b412c318SBarry Smith /*@
905950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith     Collective on DM
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith     Input Parameter:
910b412c318SBarry Smith .   dm - the DM object
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Output Parameter:
91347c6ae99SBarry Smith .   mat - the empty Jacobian
91447c6ae99SBarry Smith 
915073dac72SJed Brown     Level: beginner
91647c6ae99SBarry Smith 
91794013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
91894013140SBarry Smith        do not need to do it yourself.
91994013140SBarry Smith 
92094013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
921aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
92294013140SBarry Smith 
92394013140SBarry 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
92494013140SBarry Smith        internally by PETSc.
92594013140SBarry Smith 
92694013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
927aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
92894013140SBarry Smith 
929b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
93047c6ae99SBarry Smith 
931aab9d709SJed Brown @*/
932b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
93347c6ae99SBarry Smith {
93447c6ae99SBarry Smith   PetscErrorCode ierr;
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith   PetscFunctionBegin;
937171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
938607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
939c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
940c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
941b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
94247c6ae99SBarry Smith   PetscFunctionReturn(0);
94347c6ae99SBarry Smith }
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith #undef __FUNCT__
946732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
947732e2eb9SMatthew G Knepley /*@
948950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
949732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
950732e2eb9SMatthew G Knepley 
951732e2eb9SMatthew G Knepley   Logically Collective on DMDA
952732e2eb9SMatthew G Knepley 
953732e2eb9SMatthew G Knepley   Input Parameter:
954732e2eb9SMatthew G Knepley + dm - the DM
955732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
956732e2eb9SMatthew G Knepley 
957732e2eb9SMatthew G Knepley   Level: developer
958950540a4SJed Brown .seealso DMCreateMatrix()
959732e2eb9SMatthew G Knepley @*/
960732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
961732e2eb9SMatthew G Knepley {
962732e2eb9SMatthew G Knepley   PetscFunctionBegin;
963732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
964732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
965732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
966732e2eb9SMatthew G Knepley }
967732e2eb9SMatthew G Knepley 
968732e2eb9SMatthew G Knepley #undef __FUNCT__
969a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
970a89ea682SMatthew G Knepley /*@C
971aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
972a89ea682SMatthew G Knepley 
973a89ea682SMatthew G Knepley   Not Collective
974a89ea682SMatthew G Knepley 
975a89ea682SMatthew G Knepley   Input Parameters:
976a89ea682SMatthew G Knepley + dm - the DM object
977aa1993deSMatthew G Knepley . count - The minium size
978aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
979a89ea682SMatthew G Knepley 
980a89ea682SMatthew G Knepley   Output Parameter:
981a89ea682SMatthew G Knepley . array - the work array
982a89ea682SMatthew G Knepley 
983a89ea682SMatthew G Knepley   Level: developer
984a89ea682SMatthew G Knepley 
985a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
986a89ea682SMatthew G Knepley @*/
987aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
988a89ea682SMatthew G Knepley {
989a89ea682SMatthew G Knepley   PetscErrorCode ierr;
990aa1993deSMatthew G Knepley   DMWorkLink     link;
991aa1993deSMatthew G Knepley   size_t         size;
992a89ea682SMatthew G Knepley 
993a89ea682SMatthew G Knepley   PetscFunctionBegin;
994a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
995aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
996aa1993deSMatthew G Knepley   if (dm->workin) {
997aa1993deSMatthew G Knepley     link       = dm->workin;
998aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
999aa1993deSMatthew G Knepley   } else {
1000b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1001a89ea682SMatthew G Knepley   }
1002aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1003aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1004aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1005aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1006aa1993deSMatthew G Knepley     link->bytes = size*count;
1007aa1993deSMatthew G Knepley   }
1008aa1993deSMatthew G Knepley   link->next   = dm->workout;
1009aa1993deSMatthew G Knepley   dm->workout  = link;
1010aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1011a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1012a89ea682SMatthew G Knepley }
1013a89ea682SMatthew G Knepley 
1014aa1993deSMatthew G Knepley #undef __FUNCT__
1015aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1016aa1993deSMatthew G Knepley /*@C
1017aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1018aa1993deSMatthew G Knepley 
1019aa1993deSMatthew G Knepley   Not Collective
1020aa1993deSMatthew G Knepley 
1021aa1993deSMatthew G Knepley   Input Parameters:
1022aa1993deSMatthew G Knepley + dm - the DM object
1023aa1993deSMatthew G Knepley . count - The minium size
1024aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1025aa1993deSMatthew G Knepley 
1026aa1993deSMatthew G Knepley   Output Parameter:
1027aa1993deSMatthew G Knepley . array - the work array
1028aa1993deSMatthew G Knepley 
1029aa1993deSMatthew G Knepley   Level: developer
1030aa1993deSMatthew G Knepley 
1031aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1032aa1993deSMatthew G Knepley @*/
1033aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1034aa1993deSMatthew G Knepley {
1035aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1036aa1993deSMatthew G Knepley 
1037aa1993deSMatthew G Knepley   PetscFunctionBegin;
1038aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1039aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1040aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1041aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1042aa1993deSMatthew G Knepley       *p           = link->next;
1043aa1993deSMatthew G Knepley       link->next   = dm->workin;
1044aa1993deSMatthew G Knepley       dm->workin   = link;
10450298fd71SBarry Smith       *(void**)mem = NULL;
1046aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1047aa1993deSMatthew G Knepley     }
1048aa1993deSMatthew G Knepley   }
1049aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1050aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1051aa1993deSMatthew G Knepley }
1052e7c4fc90SDmitry Karpeev 
1053e7c4fc90SDmitry Karpeev #undef __FUNCT__
1054435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1055435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1056435a35e8SMatthew G Knepley {
1057435a35e8SMatthew G Knepley   PetscFunctionBegin;
1058435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
105982f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1060435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1061435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1062435a35e8SMatthew G Knepley }
1063435a35e8SMatthew G Knepley 
1064435a35e8SMatthew G Knepley #undef __FUNCT__
10654d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10664f3b5142SJed Brown /*@C
10674d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10684d343eeaSMatthew G Knepley 
10694d343eeaSMatthew G Knepley   Not collective
10704d343eeaSMatthew G Knepley 
10714d343eeaSMatthew G Knepley   Input Parameter:
10724d343eeaSMatthew G Knepley . dm - the DM object
10734d343eeaSMatthew G Knepley 
10744d343eeaSMatthew G Knepley   Output Parameters:
10750298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10760298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10770298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10784d343eeaSMatthew G Knepley 
10794d343eeaSMatthew G Knepley   Level: intermediate
10804d343eeaSMatthew G Knepley 
108121c9b008SJed Brown   Notes:
108221c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
108321c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
108421c9b008SJed Brown   PetscFree().
108521c9b008SJed Brown 
10864d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10874d343eeaSMatthew G Knepley @*/
108837d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10894d343eeaSMatthew G Knepley {
109037d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10914d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10924d343eeaSMatthew G Knepley 
10934d343eeaSMatthew G Knepley   PetscFunctionBegin;
10944d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
109569ca1f37SDmitry Karpeev   if (numFields) {
109669ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
109769ca1f37SDmitry Karpeev     *numFields = 0;
109869ca1f37SDmitry Karpeev   }
109937d0c07bSMatthew G Knepley   if (fieldNames) {
110037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11010298fd71SBarry Smith     *fieldNames = NULL;
110269ca1f37SDmitry Karpeev   }
110369ca1f37SDmitry Karpeev   if (fields) {
110469ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11050298fd71SBarry Smith     *fields = NULL;
110669ca1f37SDmitry Karpeev   }
110737d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
110837d0c07bSMatthew G Knepley   if (section) {
110937d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
111037d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
111137d0c07bSMatthew G Knepley 
111237d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
111337d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1114dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
111537d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
111637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
111737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
111837d0c07bSMatthew G Knepley     }
111937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
112037d0c07bSMatthew G Knepley       PetscInt gdof;
112137d0c07bSMatthew G Knepley 
112237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112337d0c07bSMatthew G Knepley       if (gdof > 0) {
112437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
112637d0c07bSMatthew G Knepley 
112737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
112837d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
112937d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
113037d0c07bSMatthew G Knepley         }
113137d0c07bSMatthew G Knepley       }
113237d0c07bSMatthew G Knepley     }
113337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1134785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
113537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113637d0c07bSMatthew G Knepley     }
113737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
113837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
113937d0c07bSMatthew G Knepley 
114037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114137d0c07bSMatthew G Knepley       if (gdof > 0) {
114237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
114337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
114537d0c07bSMatthew G Knepley 
114637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
114737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
114837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
114937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
115037d0c07bSMatthew G Knepley           }
115137d0c07bSMatthew G Knepley         }
115237d0c07bSMatthew G Knepley       }
115337d0c07bSMatthew G Knepley     }
11548865f1eaSKarl Rupp     if (numFields) *numFields = nF;
115537d0c07bSMatthew G Knepley     if (fieldNames) {
1156785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
115737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
115837d0c07bSMatthew G Knepley         const char *fieldName;
115937d0c07bSMatthew G Knepley 
116037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
116137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
116237d0c07bSMatthew G Knepley       }
116337d0c07bSMatthew G Knepley     }
116437d0c07bSMatthew G Knepley     if (fields) {
1165785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
116637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116782f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
116837d0c07bSMatthew G Knepley       }
116937d0c07bSMatthew G Knepley     }
117037d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11718865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11728865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
117369ca1f37SDmitry Karpeev   }
11744d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11754d343eeaSMatthew G Knepley }
11764d343eeaSMatthew G Knepley 
117716621825SDmitry Karpeev 
117816621825SDmitry Karpeev #undef __FUNCT__
117916621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
118016621825SDmitry Karpeev /*@C
118116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
118216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
118316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1184e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1185e7c4fc90SDmitry Karpeev 
1186e7c4fc90SDmitry Karpeev   Not collective
1187e7c4fc90SDmitry Karpeev 
1188e7c4fc90SDmitry Karpeev   Input Parameter:
1189e7c4fc90SDmitry Karpeev . dm - the DM object
1190e7c4fc90SDmitry Karpeev 
1191e7c4fc90SDmitry Karpeev   Output Parameters:
11920298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
11930298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
11940298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
11950298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1196e7c4fc90SDmitry Karpeev 
1197e7c4fc90SDmitry Karpeev   Level: intermediate
1198e7c4fc90SDmitry Karpeev 
1199e7c4fc90SDmitry Karpeev   Notes:
1200e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1201e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1202e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1203e7c4fc90SDmitry Karpeev 
1204e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1205e7c4fc90SDmitry Karpeev @*/
120616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1207e7c4fc90SDmitry Karpeev {
1208e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1209e7c4fc90SDmitry Karpeev 
1210e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1211e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12128865f1eaSKarl Rupp   if (len) {
12138865f1eaSKarl Rupp     PetscValidPointer(len,2);
12148865f1eaSKarl Rupp     *len = 0;
12158865f1eaSKarl Rupp   }
12168865f1eaSKarl Rupp   if (namelist) {
12178865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12188865f1eaSKarl Rupp     *namelist = 0;
12198865f1eaSKarl Rupp   }
12208865f1eaSKarl Rupp   if (islist) {
12218865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12228865f1eaSKarl Rupp     *islist = 0;
12238865f1eaSKarl Rupp   }
12248865f1eaSKarl Rupp   if (dmlist) {
12258865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12268865f1eaSKarl Rupp     *dmlist = 0;
12278865f1eaSKarl Rupp   }
1228f3f0edfdSDmitry Karpeev   /*
1229f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1230f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1231f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1232f3f0edfdSDmitry Karpeev    */
1233ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
123416621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1235435a35e8SMatthew G Knepley     PetscSection section;
1236435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1237435a35e8SMatthew G Knepley 
1238435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1239435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1240435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1241435a35e8SMatthew G Knepley       *len = numFields;
124203dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
124303dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
124403dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1245435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1246435a35e8SMatthew G Knepley         const char *fieldName;
1247435a35e8SMatthew G Knepley 
124803dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
124903dc3394SMatthew G. Knepley         if (namelist) {
1250435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1251435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1252435a35e8SMatthew G Knepley         }
125303dc3394SMatthew G. Knepley       }
1254435a35e8SMatthew G Knepley     } else {
125569ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1256e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12570298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1258e7c4fc90SDmitry Karpeev     }
12598865f1eaSKarl Rupp   } else {
126016621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
126116621825SDmitry Karpeev   }
126216621825SDmitry Karpeev   PetscFunctionReturn(0);
126316621825SDmitry Karpeev }
126416621825SDmitry Karpeev 
126516621825SDmitry Karpeev #undef __FUNCT__
1266435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1267435a35e8SMatthew G Knepley /*@C
1268435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1269435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1270435a35e8SMatthew G Knepley 
1271435a35e8SMatthew G Knepley   Not collective
1272435a35e8SMatthew G Knepley 
1273435a35e8SMatthew G Knepley   Input Parameters:
1274435a35e8SMatthew G Knepley + dm - the DM object
1275435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12760298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1277435a35e8SMatthew G Knepley 
1278435a35e8SMatthew G Knepley   Output Parameters:
1279435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1280435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1281435a35e8SMatthew G Knepley 
1282435a35e8SMatthew G Knepley   Level: intermediate
1283435a35e8SMatthew G Knepley 
1284435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1285435a35e8SMatthew G Knepley @*/
1286435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1287435a35e8SMatthew G Knepley {
1288435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1289435a35e8SMatthew G Knepley 
1290435a35e8SMatthew G Knepley   PetscFunctionBegin;
1291435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1292435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
12938865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
12948865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1295435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1296435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
129782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1298435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1299435a35e8SMatthew G Knepley }
1300435a35e8SMatthew G Knepley 
130116621825SDmitry Karpeev 
130216621825SDmitry Karpeev #undef __FUNCT__
130316621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
130416621825SDmitry Karpeev /*@C
13058d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13068d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13078d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13088d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13098d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
131016621825SDmitry Karpeev 
131116621825SDmitry Karpeev   Not collective
131216621825SDmitry Karpeev 
131316621825SDmitry Karpeev   Input Parameter:
131416621825SDmitry Karpeev . dm - the DM object
131516621825SDmitry Karpeev 
131616621825SDmitry Karpeev   Output Parameters:
13170298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13180298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13190298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13200298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13210298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
132216621825SDmitry Karpeev 
132316621825SDmitry Karpeev   Level: intermediate
132416621825SDmitry Karpeev 
132516621825SDmitry Karpeev   Notes:
132616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
132716621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
132816621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
132916621825SDmitry Karpeev 
13308d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
133116621825SDmitry Karpeev @*/
13328d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
133316621825SDmitry Karpeev {
133416621825SDmitry Karpeev   PetscErrorCode      ierr;
1335be081cd6SPeter Brune   DMSubDomainHookLink link;
1336be081cd6SPeter Brune   PetscInt            i,l;
133716621825SDmitry Karpeev 
133816621825SDmitry Karpeev   PetscFunctionBegin;
133916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134014a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13410298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13420298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13430298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13440298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1345f3f0edfdSDmitry Karpeev   /*
1346f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1347f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1348f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1349f3f0edfdSDmitry Karpeev    */
1350ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
135116621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1352be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
135314a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
135414a18fd3SPeter Brune     if (dmlist) {
1355be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1356be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1357be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1358be081cd6SPeter Brune         }
1359d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1360e7c4fc90SDmitry Karpeev       }
136114a18fd3SPeter Brune     }
136214a18fd3SPeter Brune     if (len) *len = l;
136314a18fd3SPeter Brune   }
1364e30e807fSPeter Brune   PetscFunctionReturn(0);
1365e30e807fSPeter Brune }
1366e30e807fSPeter Brune 
1367e30e807fSPeter Brune 
1368e30e807fSPeter Brune #undef __FUNCT__
1369e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1370e30e807fSPeter Brune /*@C
1371e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1372e30e807fSPeter Brune 
1373e30e807fSPeter Brune   Not collective
1374e30e807fSPeter Brune 
1375e30e807fSPeter Brune   Input Parameters:
1376e30e807fSPeter Brune + dm - the DM object
1377e30e807fSPeter Brune . n  - the number of subdomain scatters
1378e30e807fSPeter Brune - subdms - the local subdomains
1379e30e807fSPeter Brune 
1380e30e807fSPeter Brune   Output Parameters:
1381e30e807fSPeter Brune + n     - the number of scatters returned
1382e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1383e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1384e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1385e30e807fSPeter Brune 
1386e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1387e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1388e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1389e30e807fSPeter Brune   solution and residual data.
1390e30e807fSPeter Brune 
1391e30e807fSPeter Brune   Level: developer
1392e30e807fSPeter Brune 
1393e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1394e30e807fSPeter Brune @*/
1395e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1396e30e807fSPeter Brune {
1397e30e807fSPeter Brune   PetscErrorCode ierr;
1398e30e807fSPeter Brune 
1399e30e807fSPeter Brune   PetscFunctionBegin;
1400e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1401e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1402e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1403e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
140482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1405e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1406e7c4fc90SDmitry Karpeev }
1407e7c4fc90SDmitry Karpeev 
1408731c8d9eSDmitry Karpeev #undef __FUNCT__
140947c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
141047c6ae99SBarry Smith /*@
141147c6ae99SBarry Smith   DMRefine - Refines a DM object
141247c6ae99SBarry Smith 
141347c6ae99SBarry Smith   Collective on DM
141447c6ae99SBarry Smith 
141547c6ae99SBarry Smith   Input Parameter:
141647c6ae99SBarry Smith + dm   - the DM object
141791d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
141847c6ae99SBarry Smith 
141947c6ae99SBarry Smith   Output Parameter:
14200298fd71SBarry Smith . dmf - the refined DM, or NULL
1421ae0a1c52SMatthew G Knepley 
14220298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
142347c6ae99SBarry Smith 
142447c6ae99SBarry Smith   Level: developer
142547c6ae99SBarry Smith 
1426e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
142747c6ae99SBarry Smith @*/
14287087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
142947c6ae99SBarry Smith {
143047c6ae99SBarry Smith   PetscErrorCode   ierr;
1431c833c3b5SJed Brown   DMRefineHookLink link;
143247c6ae99SBarry Smith 
143347c6ae99SBarry Smith   PetscFunctionBegin;
1434732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143547c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14364057135bSMatthew G Knepley   if (*dmf) {
143743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14388865f1eaSKarl Rupp 
14398cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14408865f1eaSKarl Rupp 
1441644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14420598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1443656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14448865f1eaSKarl Rupp 
1445e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1446c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14478865f1eaSKarl Rupp       if (link->refinehook) {
14488865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14498865f1eaSKarl Rupp       }
1450c833c3b5SJed Brown     }
1451c833c3b5SJed Brown   }
1452c833c3b5SJed Brown   PetscFunctionReturn(0);
1453c833c3b5SJed Brown }
1454c833c3b5SJed Brown 
1455c833c3b5SJed Brown #undef __FUNCT__
1456c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1457bb9467b5SJed Brown /*@C
1458c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1459c833c3b5SJed Brown 
1460c833c3b5SJed Brown    Logically Collective
1461c833c3b5SJed Brown 
1462c833c3b5SJed Brown    Input Arguments:
1463c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1464c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1465c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14660298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1467c833c3b5SJed Brown 
1468c833c3b5SJed Brown    Calling sequence of refinehook:
1469c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1470c833c3b5SJed Brown 
1471c833c3b5SJed Brown +  coarse - coarse level DM
1472c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1473c833c3b5SJed Brown -  ctx - optional user-defined function context
1474c833c3b5SJed Brown 
1475c833c3b5SJed Brown    Calling sequence for interphook:
1476c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1477c833c3b5SJed Brown 
1478c833c3b5SJed Brown +  coarse - coarse level DM
1479c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1480c833c3b5SJed Brown .  fine - fine level DM to update
1481c833c3b5SJed Brown -  ctx - optional user-defined function context
1482c833c3b5SJed Brown 
1483c833c3b5SJed Brown    Level: advanced
1484c833c3b5SJed Brown 
1485c833c3b5SJed Brown    Notes:
1486c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1487c833c3b5SJed Brown 
1488c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1489c833c3b5SJed Brown 
1490bb9467b5SJed Brown    This function is currently not available from Fortran.
1491bb9467b5SJed Brown 
1492c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1493c833c3b5SJed Brown @*/
1494c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1495c833c3b5SJed Brown {
1496c833c3b5SJed Brown   PetscErrorCode   ierr;
1497c833c3b5SJed Brown   DMRefineHookLink link,*p;
1498c833c3b5SJed Brown 
1499c833c3b5SJed Brown   PetscFunctionBegin;
1500c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1501c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1502c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1503c833c3b5SJed Brown   link->refinehook = refinehook;
1504c833c3b5SJed Brown   link->interphook = interphook;
1505c833c3b5SJed Brown   link->ctx        = ctx;
15060298fd71SBarry Smith   link->next       = NULL;
1507c833c3b5SJed Brown   *p               = link;
1508c833c3b5SJed Brown   PetscFunctionReturn(0);
1509c833c3b5SJed Brown }
1510c833c3b5SJed Brown 
1511c833c3b5SJed Brown #undef __FUNCT__
1512c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1513c833c3b5SJed Brown /*@
1514c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1515c833c3b5SJed Brown 
1516c833c3b5SJed Brown    Collective if any hooks are
1517c833c3b5SJed Brown 
1518c833c3b5SJed Brown    Input Arguments:
1519c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1520c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1521c833c3b5SJed Brown -  fine - finer DM to update
1522c833c3b5SJed Brown 
1523c833c3b5SJed Brown    Level: developer
1524c833c3b5SJed Brown 
1525c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1526c833c3b5SJed Brown @*/
1527c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1528c833c3b5SJed Brown {
1529c833c3b5SJed Brown   PetscErrorCode   ierr;
1530c833c3b5SJed Brown   DMRefineHookLink link;
1531c833c3b5SJed Brown 
1532c833c3b5SJed Brown   PetscFunctionBegin;
1533c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15348865f1eaSKarl Rupp     if (link->interphook) {
15358865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15368865f1eaSKarl Rupp     }
15374057135bSMatthew G Knepley   }
153847c6ae99SBarry Smith   PetscFunctionReturn(0);
153947c6ae99SBarry Smith }
154047c6ae99SBarry Smith 
154147c6ae99SBarry Smith #undef __FUNCT__
1542eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1543eb3f98d2SBarry Smith /*@
1544eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1545eb3f98d2SBarry Smith 
1546eb3f98d2SBarry Smith     Not Collective
1547eb3f98d2SBarry Smith 
1548eb3f98d2SBarry Smith     Input Parameter:
1549eb3f98d2SBarry Smith .   dm - the DM object
1550eb3f98d2SBarry Smith 
1551eb3f98d2SBarry Smith     Output Parameter:
1552eb3f98d2SBarry Smith .   level - number of refinements
1553eb3f98d2SBarry Smith 
1554eb3f98d2SBarry Smith     Level: developer
1555eb3f98d2SBarry Smith 
15566a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1557eb3f98d2SBarry Smith 
1558eb3f98d2SBarry Smith @*/
1559eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1560eb3f98d2SBarry Smith {
1561eb3f98d2SBarry Smith   PetscFunctionBegin;
1562eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1563eb3f98d2SBarry Smith   *level = dm->levelup;
1564eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1565eb3f98d2SBarry Smith }
1566eb3f98d2SBarry Smith 
1567eb3f98d2SBarry Smith #undef __FUNCT__
1568baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1569bb9467b5SJed Brown /*@C
1570baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1571baf369e7SPeter Brune 
1572baf369e7SPeter Brune    Logically Collective
1573baf369e7SPeter Brune 
1574baf369e7SPeter Brune    Input Arguments:
1575baf369e7SPeter Brune +  dm - the DM
1576baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1577baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15780298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1579baf369e7SPeter Brune 
1580baf369e7SPeter Brune    Calling sequence for beginhook:
1581baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1582baf369e7SPeter Brune 
1583baf369e7SPeter Brune +  dm - global DM
1584baf369e7SPeter Brune .  g - global vector
1585baf369e7SPeter Brune .  mode - mode
1586baf369e7SPeter Brune .  l - local vector
1587baf369e7SPeter Brune -  ctx - optional user-defined function context
1588baf369e7SPeter Brune 
1589baf369e7SPeter Brune 
1590baf369e7SPeter Brune    Calling sequence for endhook:
1591ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1592baf369e7SPeter Brune 
1593baf369e7SPeter Brune +  global - global DM
1594baf369e7SPeter Brune -  ctx - optional user-defined function context
1595baf369e7SPeter Brune 
1596baf369e7SPeter Brune    Level: advanced
1597baf369e7SPeter Brune 
1598baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1599baf369e7SPeter Brune @*/
1600baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1601baf369e7SPeter Brune {
1602baf369e7SPeter Brune   PetscErrorCode          ierr;
1603baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1604baf369e7SPeter Brune 
1605baf369e7SPeter Brune   PetscFunctionBegin;
1606baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1607baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1608baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1609baf369e7SPeter Brune   link->beginhook = beginhook;
1610baf369e7SPeter Brune   link->endhook   = endhook;
1611baf369e7SPeter Brune   link->ctx       = ctx;
16120298fd71SBarry Smith   link->next      = NULL;
1613baf369e7SPeter Brune   *p              = link;
1614baf369e7SPeter Brune   PetscFunctionReturn(0);
1615baf369e7SPeter Brune }
1616baf369e7SPeter Brune 
1617baf369e7SPeter Brune #undef __FUNCT__
161847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
161947c6ae99SBarry Smith /*@
162047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
162147c6ae99SBarry Smith 
162247c6ae99SBarry Smith     Neighbor-wise Collective on DM
162347c6ae99SBarry Smith 
162447c6ae99SBarry Smith     Input Parameters:
162547c6ae99SBarry Smith +   dm - the DM object
162647c6ae99SBarry Smith .   g - the global vector
162747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
162847c6ae99SBarry Smith -   l - the local vector
162947c6ae99SBarry Smith 
163047c6ae99SBarry Smith 
163147c6ae99SBarry Smith     Level: beginner
163247c6ae99SBarry Smith 
1633e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
163447c6ae99SBarry Smith 
163547c6ae99SBarry Smith @*/
16367087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
163747c6ae99SBarry Smith {
16387128ae9fSMatthew G Knepley   PetscSF                 sf;
163947c6ae99SBarry Smith   PetscErrorCode          ierr;
1640baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
164147c6ae99SBarry Smith 
164247c6ae99SBarry Smith   PetscFunctionBegin;
1643171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1644baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16458865f1eaSKarl Rupp     if (link->beginhook) {
16468865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16478865f1eaSKarl Rupp     }
1648baf369e7SPeter Brune   }
16497128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16507128ae9fSMatthew G Knepley   if (sf) {
16517128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16527128ae9fSMatthew G Knepley 
165382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16547128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16557128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16567128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16577128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16587128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16597128ae9fSMatthew G Knepley   } else {
1660843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16617128ae9fSMatthew G Knepley   }
166247c6ae99SBarry Smith   PetscFunctionReturn(0);
166347c6ae99SBarry Smith }
166447c6ae99SBarry Smith 
166547c6ae99SBarry Smith #undef __FUNCT__
166647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
166747c6ae99SBarry Smith /*@
166847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
166947c6ae99SBarry Smith 
167047c6ae99SBarry Smith     Neighbor-wise Collective on DM
167147c6ae99SBarry Smith 
167247c6ae99SBarry Smith     Input Parameters:
167347c6ae99SBarry Smith +   dm - the DM object
167447c6ae99SBarry Smith .   g - the global vector
167547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
167647c6ae99SBarry Smith -   l - the local vector
167747c6ae99SBarry Smith 
167847c6ae99SBarry Smith 
167947c6ae99SBarry Smith     Level: beginner
168047c6ae99SBarry Smith 
1681e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
168247c6ae99SBarry Smith 
168347c6ae99SBarry Smith @*/
16847087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
168547c6ae99SBarry Smith {
16867128ae9fSMatthew G Knepley   PetscSF                 sf;
168747c6ae99SBarry Smith   PetscErrorCode          ierr;
168861a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1689baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
169047c6ae99SBarry Smith 
169147c6ae99SBarry Smith   PetscFunctionBegin;
1692171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16937128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16947128ae9fSMatthew G Knepley   if (sf) {
169582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16967128ae9fSMatthew G Knepley 
16977128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16987128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16997128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17007128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17017128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17027128ae9fSMatthew G Knepley   } else {
1703843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17047128ae9fSMatthew G Knepley   }
1705baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1706baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1707baf369e7SPeter Brune   }
170847c6ae99SBarry Smith   PetscFunctionReturn(0);
170947c6ae99SBarry Smith }
171047c6ae99SBarry Smith 
171147c6ae99SBarry Smith #undef __FUNCT__
17129a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
171347c6ae99SBarry Smith /*@
17149a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
17159a42bb27SBarry Smith 
17169a42bb27SBarry Smith     Neighbor-wise Collective on DM
17179a42bb27SBarry Smith 
17189a42bb27SBarry Smith     Input Parameters:
17199a42bb27SBarry Smith +   dm - the DM object
1720f6813fd5SJed Brown .   l - the local vector
17219a42bb27SBarry 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
17229a42bb27SBarry Smith            base point.
1723f6813fd5SJed Brown - - the global vector
17249a42bb27SBarry Smith 
17259a42bb27SBarry 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
17269a42bb27SBarry 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
17279a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17289a42bb27SBarry Smith 
17299a42bb27SBarry Smith     Level: beginner
17309a42bb27SBarry Smith 
1731e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17329a42bb27SBarry Smith 
17339a42bb27SBarry Smith @*/
17347087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17359a42bb27SBarry Smith {
17367128ae9fSMatthew G Knepley   PetscSF        sf;
17379a42bb27SBarry Smith   PetscErrorCode ierr;
17389a42bb27SBarry Smith 
17399a42bb27SBarry Smith   PetscFunctionBegin;
1740171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17417128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17427128ae9fSMatthew G Knepley   if (sf) {
17437128ae9fSMatthew G Knepley     MPI_Op      op;
17447128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17457128ae9fSMatthew G Knepley 
17467128ae9fSMatthew G Knepley     switch (mode) {
17477128ae9fSMatthew G Knepley     case INSERT_VALUES:
17487128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17498bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17507128ae9fSMatthew G Knepley     case ADD_VALUES:
17517128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17527128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17537128ae9fSMatthew G Knepley     default:
175482f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17557128ae9fSMatthew G Knepley     }
17567128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17577128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17587128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17597128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17607128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17617128ae9fSMatthew G Knepley   } else {
1762843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17637128ae9fSMatthew G Knepley   }
17649a42bb27SBarry Smith   PetscFunctionReturn(0);
17659a42bb27SBarry Smith }
17669a42bb27SBarry Smith 
17679a42bb27SBarry Smith #undef __FUNCT__
17689a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17699a42bb27SBarry Smith /*@
17709a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
177147c6ae99SBarry Smith 
177247c6ae99SBarry Smith     Neighbor-wise Collective on DM
177347c6ae99SBarry Smith 
177447c6ae99SBarry Smith     Input Parameters:
177547c6ae99SBarry Smith +   dm - the DM object
1776f6813fd5SJed Brown .   l - the local vector
177747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1778f6813fd5SJed Brown -   g - the global vector
177947c6ae99SBarry Smith 
178047c6ae99SBarry Smith 
178147c6ae99SBarry Smith     Level: beginner
178247c6ae99SBarry Smith 
1783e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
178447c6ae99SBarry Smith 
178547c6ae99SBarry Smith @*/
17867087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
178747c6ae99SBarry Smith {
17887128ae9fSMatthew G Knepley   PetscSF        sf;
178947c6ae99SBarry Smith   PetscErrorCode ierr;
179047c6ae99SBarry Smith 
179147c6ae99SBarry Smith   PetscFunctionBegin;
1792171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17937128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17947128ae9fSMatthew G Knepley   if (sf) {
17957128ae9fSMatthew G Knepley     MPI_Op      op;
17967128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17977128ae9fSMatthew G Knepley 
17987128ae9fSMatthew G Knepley     switch (mode) {
17997128ae9fSMatthew G Knepley     case INSERT_VALUES:
18007128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18018bfbc91cSJed Brown       op = MPIU_REPLACE; break;
18027128ae9fSMatthew G Knepley     case ADD_VALUES:
18037128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18047128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18057128ae9fSMatthew G Knepley     default:
180682f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18077128ae9fSMatthew G Knepley     }
18087128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18097128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18107128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18117128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18127128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18137128ae9fSMatthew G Knepley   } else {
1814843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18157128ae9fSMatthew G Knepley   }
181647c6ae99SBarry Smith   PetscFunctionReturn(0);
181747c6ae99SBarry Smith }
181847c6ae99SBarry Smith 
181947c6ae99SBarry Smith #undef __FUNCT__
1820f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
1821f089877aSRichard Tran Mills /*@
1822bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
1823bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1824d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
1825f089877aSRichard Tran Mills 
1826bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1827f089877aSRichard Tran Mills 
1828f089877aSRichard Tran Mills    Input Parameters:
1829f089877aSRichard Tran Mills +  dm - the DM object
1830bc0a1609SRichard Tran Mills .  g - the original local vector
1831bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1832f089877aSRichard Tran Mills 
1833bc0a1609SRichard Tran Mills    Output Parameter:
1834bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1835f089877aSRichard Tran Mills 
1836f089877aSRichard Tran Mills    Level: intermediate
1837f089877aSRichard Tran Mills 
1838bc0a1609SRichard Tran Mills    Notes:
1839bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1840bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1841bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1842bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1843bc0a1609SRichard Tran Mills 
1844bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
1845bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1846f089877aSRichard Tran Mills 
1847f089877aSRichard Tran Mills @*/
1848f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
1849f089877aSRichard Tran Mills {
1850f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1851f089877aSRichard Tran Mills 
1852f089877aSRichard Tran Mills   PetscFunctionBegin;
1853f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1854f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1855f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1856f089877aSRichard Tran Mills }
1857f089877aSRichard Tran Mills 
1858f089877aSRichard Tran Mills #undef __FUNCT__
1859f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
1860f089877aSRichard Tran Mills /*@
1861bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
1862bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1863d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
1864f089877aSRichard Tran Mills 
1865bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1866f089877aSRichard Tran Mills 
1867f089877aSRichard Tran Mills    Input Parameters:
1868bc0a1609SRichard Tran Mills +  da - the DM object
1869bc0a1609SRichard Tran Mills .  g - the original local vector
1870bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1871f089877aSRichard Tran Mills 
1872bc0a1609SRichard Tran Mills    Output Parameter:
1873bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1874f089877aSRichard Tran Mills 
1875f089877aSRichard Tran Mills    Level: intermediate
1876f089877aSRichard Tran Mills 
1877bc0a1609SRichard Tran Mills    Notes:
1878bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1879bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1880bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1881bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1882bc0a1609SRichard Tran Mills 
1883bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
1884bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1885f089877aSRichard Tran Mills 
1886f089877aSRichard Tran Mills @*/
1887f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
1888f089877aSRichard Tran Mills {
1889f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1890f089877aSRichard Tran Mills 
1891f089877aSRichard Tran Mills   PetscFunctionBegin;
1892f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1893f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1894f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1895f089877aSRichard Tran Mills }
1896f089877aSRichard Tran Mills 
1897f089877aSRichard Tran Mills 
1898f089877aSRichard Tran Mills #undef __FUNCT__
189947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
190047c6ae99SBarry Smith /*@
190147c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
190247c6ae99SBarry Smith 
190347c6ae99SBarry Smith     Collective on DM
190447c6ae99SBarry Smith 
190547c6ae99SBarry Smith     Input Parameter:
190647c6ae99SBarry Smith +   dm - the DM object
190791d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
190847c6ae99SBarry Smith 
190947c6ae99SBarry Smith     Output Parameter:
191047c6ae99SBarry Smith .   dmc - the coarsened DM
191147c6ae99SBarry Smith 
191247c6ae99SBarry Smith     Level: developer
191347c6ae99SBarry Smith 
1914e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
191547c6ae99SBarry Smith 
191647c6ae99SBarry Smith @*/
19177087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
191847c6ae99SBarry Smith {
191947c6ae99SBarry Smith   PetscErrorCode    ierr;
1920b17ce1afSJed Brown   DMCoarsenHookLink link;
192147c6ae99SBarry Smith 
192247c6ae99SBarry Smith   PetscFunctionBegin;
1923171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
192447c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
192543842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
19268cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1927644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
19280598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1929656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1930e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1931b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1932b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1933b17ce1afSJed Brown   }
1934b17ce1afSJed Brown   PetscFunctionReturn(0);
1935b17ce1afSJed Brown }
1936b17ce1afSJed Brown 
1937b17ce1afSJed Brown #undef __FUNCT__
1938b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1939bb9467b5SJed Brown /*@C
1940b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1941b17ce1afSJed Brown 
1942b17ce1afSJed Brown    Logically Collective
1943b17ce1afSJed Brown 
1944b17ce1afSJed Brown    Input Arguments:
1945b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1946b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1947b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
19480298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1949b17ce1afSJed Brown 
1950b17ce1afSJed Brown    Calling sequence of coarsenhook:
1951b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1952b17ce1afSJed Brown 
1953b17ce1afSJed Brown +  fine - fine level DM
1954b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1955b17ce1afSJed Brown -  ctx - optional user-defined function context
1956b17ce1afSJed Brown 
1957b17ce1afSJed Brown    Calling sequence for restricthook:
1958c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1959b17ce1afSJed Brown 
1960b17ce1afSJed Brown +  fine - fine level DM
1961b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1962c833c3b5SJed Brown .  rscale - scaling vector for restriction
1963c833c3b5SJed Brown .  inject - matrix restricting by injection
1964b17ce1afSJed Brown .  coarse - coarse level DM to update
1965b17ce1afSJed Brown -  ctx - optional user-defined function context
1966b17ce1afSJed Brown 
1967b17ce1afSJed Brown    Level: advanced
1968b17ce1afSJed Brown 
1969b17ce1afSJed Brown    Notes:
1970b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1971b17ce1afSJed Brown 
1972b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1973b17ce1afSJed Brown 
1974b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1975b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1976b17ce1afSJed Brown 
1977bb9467b5SJed Brown    This function is currently not available from Fortran.
1978bb9467b5SJed Brown 
1979c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1980b17ce1afSJed Brown @*/
1981b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1982b17ce1afSJed Brown {
1983b17ce1afSJed Brown   PetscErrorCode    ierr;
1984b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1985b17ce1afSJed Brown 
1986b17ce1afSJed Brown   PetscFunctionBegin;
1987b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
19886bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1989b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1990b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
1991b17ce1afSJed Brown   link->restricthook = restricthook;
1992b17ce1afSJed Brown   link->ctx          = ctx;
19930298fd71SBarry Smith   link->next         = NULL;
1994b17ce1afSJed Brown   *p                 = link;
1995b17ce1afSJed Brown   PetscFunctionReturn(0);
1996b17ce1afSJed Brown }
1997b17ce1afSJed Brown 
1998b17ce1afSJed Brown #undef __FUNCT__
1999b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2000b17ce1afSJed Brown /*@
2001b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2002b17ce1afSJed Brown 
2003b17ce1afSJed Brown    Collective if any hooks are
2004b17ce1afSJed Brown 
2005b17ce1afSJed Brown    Input Arguments:
2006b17ce1afSJed Brown +  fine - finer DM to use as a base
2007b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2008b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2009b17ce1afSJed Brown -  coarse - coarer DM to update
2010b17ce1afSJed Brown 
2011b17ce1afSJed Brown    Level: developer
2012b17ce1afSJed Brown 
2013b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2014b17ce1afSJed Brown @*/
2015b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2016b17ce1afSJed Brown {
2017b17ce1afSJed Brown   PetscErrorCode    ierr;
2018b17ce1afSJed Brown   DMCoarsenHookLink link;
2019b17ce1afSJed Brown 
2020b17ce1afSJed Brown   PetscFunctionBegin;
2021b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
20228865f1eaSKarl Rupp     if (link->restricthook) {
20238865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
20248865f1eaSKarl Rupp     }
2025b17ce1afSJed Brown   }
202647c6ae99SBarry Smith   PetscFunctionReturn(0);
202747c6ae99SBarry Smith }
202847c6ae99SBarry Smith 
202947c6ae99SBarry Smith #undef __FUNCT__
2030be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2031bb9467b5SJed Brown /*@C
2032be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
20335dbd56e3SPeter Brune 
20345dbd56e3SPeter Brune    Logically Collective
20355dbd56e3SPeter Brune 
20365dbd56e3SPeter Brune    Input Arguments:
20375dbd56e3SPeter Brune +  global - global DM
2038ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
20395dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
20400298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20415dbd56e3SPeter Brune 
2042ec4806b8SPeter Brune 
2043ec4806b8SPeter Brune    Calling sequence for ddhook:
2044ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2045ec4806b8SPeter Brune 
2046ec4806b8SPeter Brune +  global - global DM
2047ec4806b8SPeter Brune .  block  - block DM
2048ec4806b8SPeter Brune -  ctx - optional user-defined function context
2049ec4806b8SPeter Brune 
20505dbd56e3SPeter Brune    Calling sequence for restricthook:
2051ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
20525dbd56e3SPeter Brune 
20535dbd56e3SPeter Brune +  global - global DM
20545dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
20555dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2056ec4806b8SPeter Brune .  block  - block DM
20575dbd56e3SPeter Brune -  ctx - optional user-defined function context
20585dbd56e3SPeter Brune 
20595dbd56e3SPeter Brune    Level: advanced
20605dbd56e3SPeter Brune 
20615dbd56e3SPeter Brune    Notes:
2062ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
20635dbd56e3SPeter Brune 
20645dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
20655dbd56e3SPeter Brune 
20665dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2067ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
20685dbd56e3SPeter Brune 
2069bb9467b5SJed Brown    This function is currently not available from Fortran.
2070bb9467b5SJed Brown 
20715dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20725dbd56e3SPeter Brune @*/
2073be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
20745dbd56e3SPeter Brune {
20755dbd56e3SPeter Brune   PetscErrorCode      ierr;
2076be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
20775dbd56e3SPeter Brune 
20785dbd56e3SPeter Brune   PetscFunctionBegin;
20795dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2080be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2081be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
20825dbd56e3SPeter Brune   link->restricthook = restricthook;
2083be081cd6SPeter Brune   link->ddhook       = ddhook;
20845dbd56e3SPeter Brune   link->ctx          = ctx;
20850298fd71SBarry Smith   link->next         = NULL;
20865dbd56e3SPeter Brune   *p                 = link;
20875dbd56e3SPeter Brune   PetscFunctionReturn(0);
20885dbd56e3SPeter Brune }
20895dbd56e3SPeter Brune 
20905dbd56e3SPeter Brune #undef __FUNCT__
2091be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
20925dbd56e3SPeter Brune /*@
2093be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
20945dbd56e3SPeter Brune 
20955dbd56e3SPeter Brune    Collective if any hooks are
20965dbd56e3SPeter Brune 
20975dbd56e3SPeter Brune    Input Arguments:
20985dbd56e3SPeter Brune +  fine - finer DM to use as a base
2099be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2100be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
21015dbd56e3SPeter Brune -  coarse - coarer DM to update
21025dbd56e3SPeter Brune 
21035dbd56e3SPeter Brune    Level: developer
21045dbd56e3SPeter Brune 
21055dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
21065dbd56e3SPeter Brune @*/
2107be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
21085dbd56e3SPeter Brune {
21095dbd56e3SPeter Brune   PetscErrorCode      ierr;
2110be081cd6SPeter Brune   DMSubDomainHookLink link;
21115dbd56e3SPeter Brune 
21125dbd56e3SPeter Brune   PetscFunctionBegin;
2113be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
21148865f1eaSKarl Rupp     if (link->restricthook) {
21158865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
21168865f1eaSKarl Rupp     }
21175dbd56e3SPeter Brune   }
21185dbd56e3SPeter Brune   PetscFunctionReturn(0);
21195dbd56e3SPeter Brune }
21205dbd56e3SPeter Brune 
21215dbd56e3SPeter Brune #undef __FUNCT__
21225fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
21235fe1f584SPeter Brune /*@
21246a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
21255fe1f584SPeter Brune 
21265fe1f584SPeter Brune     Not Collective
21275fe1f584SPeter Brune 
21285fe1f584SPeter Brune     Input Parameter:
21295fe1f584SPeter Brune .   dm - the DM object
21305fe1f584SPeter Brune 
21315fe1f584SPeter Brune     Output Parameter:
21326a7d9d85SPeter Brune .   level - number of coarsenings
21335fe1f584SPeter Brune 
21345fe1f584SPeter Brune     Level: developer
21355fe1f584SPeter Brune 
21366a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
21375fe1f584SPeter Brune 
21385fe1f584SPeter Brune @*/
21395fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
21405fe1f584SPeter Brune {
21415fe1f584SPeter Brune   PetscFunctionBegin;
21425fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21435fe1f584SPeter Brune   *level = dm->leveldown;
21445fe1f584SPeter Brune   PetscFunctionReturn(0);
21455fe1f584SPeter Brune }
21465fe1f584SPeter Brune 
21475fe1f584SPeter Brune 
21485fe1f584SPeter Brune 
21495fe1f584SPeter Brune #undef __FUNCT__
215047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
215147c6ae99SBarry Smith /*@C
215247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
215347c6ae99SBarry Smith 
215447c6ae99SBarry Smith     Collective on DM
215547c6ae99SBarry Smith 
215647c6ae99SBarry Smith     Input Parameter:
215747c6ae99SBarry Smith +   dm - the DM object
215847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
215947c6ae99SBarry Smith 
216047c6ae99SBarry Smith     Output Parameter:
216147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
216247c6ae99SBarry Smith 
216347c6ae99SBarry Smith     Level: developer
216447c6ae99SBarry Smith 
2165e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
216647c6ae99SBarry Smith 
216747c6ae99SBarry Smith @*/
21687087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
216947c6ae99SBarry Smith {
217047c6ae99SBarry Smith   PetscErrorCode ierr;
217147c6ae99SBarry Smith 
217247c6ae99SBarry Smith   PetscFunctionBegin;
2173171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2174ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
217547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
217647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
217747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
217847c6ae99SBarry Smith   } else if (dm->ops->refine) {
217947c6ae99SBarry Smith     PetscInt i;
218047c6ae99SBarry Smith 
2181ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
218247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2183ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
218447c6ae99SBarry Smith     }
2185ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
218647c6ae99SBarry Smith   PetscFunctionReturn(0);
218747c6ae99SBarry Smith }
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith #undef __FUNCT__
219047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
219147c6ae99SBarry Smith /*@C
219247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
219347c6ae99SBarry Smith 
219447c6ae99SBarry Smith     Collective on DM
219547c6ae99SBarry Smith 
219647c6ae99SBarry Smith     Input Parameter:
219747c6ae99SBarry Smith +   dm - the DM object
219847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
219947c6ae99SBarry Smith 
220047c6ae99SBarry Smith     Output Parameter:
220147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
220247c6ae99SBarry Smith 
220347c6ae99SBarry Smith     Level: developer
220447c6ae99SBarry Smith 
2205e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
220647c6ae99SBarry Smith 
220747c6ae99SBarry Smith @*/
22087087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
220947c6ae99SBarry Smith {
221047c6ae99SBarry Smith   PetscErrorCode ierr;
221147c6ae99SBarry Smith 
221247c6ae99SBarry Smith   PetscFunctionBegin;
2213171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2214ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
221547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
221647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
221747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
221847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
221947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
222047c6ae99SBarry Smith     PetscInt i;
222147c6ae99SBarry Smith 
2222ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
222347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2224ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
222547c6ae99SBarry Smith     }
2226ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
222747c6ae99SBarry Smith   PetscFunctionReturn(0);
222847c6ae99SBarry Smith }
222947c6ae99SBarry Smith 
223047c6ae99SBarry Smith #undef __FUNCT__
2231e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
223247c6ae99SBarry Smith /*@
2233e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
223447c6ae99SBarry Smith    grids associated with two DMs.
223547c6ae99SBarry Smith 
223647c6ae99SBarry Smith    Collective on DM
223747c6ae99SBarry Smith 
223847c6ae99SBarry Smith    Input Parameters:
223947c6ae99SBarry Smith +  dmc - the coarse grid DM
224047c6ae99SBarry Smith -  dmf - the fine grid DM
224147c6ae99SBarry Smith 
224247c6ae99SBarry Smith    Output Parameters:
224347c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
224447c6ae99SBarry Smith 
224547c6ae99SBarry Smith    Level: intermediate
224647c6ae99SBarry Smith 
224747c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
224847c6ae99SBarry Smith 
2249e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
225047c6ae99SBarry Smith @*/
2251e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
225247c6ae99SBarry Smith {
225347c6ae99SBarry Smith   PetscErrorCode ierr;
225447c6ae99SBarry Smith 
225547c6ae99SBarry Smith   PetscFunctionBegin;
2256171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2257171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
225847c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
225947c6ae99SBarry Smith   PetscFunctionReturn(0);
226047c6ae99SBarry Smith }
226147c6ae99SBarry Smith 
226247c6ae99SBarry Smith #undef __FUNCT__
22631a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
22641a266240SBarry Smith /*@C
22651a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
22661a266240SBarry Smith 
22671a266240SBarry Smith     Not Collective
22681a266240SBarry Smith 
22691a266240SBarry Smith     Input Parameters:
22701a266240SBarry Smith +   dm - the DM object
22711a266240SBarry Smith -   destroy - the destroy function
22721a266240SBarry Smith 
22731a266240SBarry Smith     Level: intermediate
22741a266240SBarry Smith 
2275e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
22761a266240SBarry Smith 
2277f07f9ceaSJed Brown @*/
22781a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
22791a266240SBarry Smith {
22801a266240SBarry Smith   PetscFunctionBegin;
2281171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22821a266240SBarry Smith   dm->ctxdestroy = destroy;
22831a266240SBarry Smith   PetscFunctionReturn(0);
22841a266240SBarry Smith }
22851a266240SBarry Smith 
22861a266240SBarry Smith #undef __FUNCT__
22871b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2288b07ff414SBarry Smith /*@
22891b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
229047c6ae99SBarry Smith 
229147c6ae99SBarry Smith     Not Collective
229247c6ae99SBarry Smith 
229347c6ae99SBarry Smith     Input Parameters:
229447c6ae99SBarry Smith +   dm - the DM object
229547c6ae99SBarry Smith -   ctx - the user context
229647c6ae99SBarry Smith 
229747c6ae99SBarry Smith     Level: intermediate
229847c6ae99SBarry Smith 
2299e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
230047c6ae99SBarry Smith 
230147c6ae99SBarry Smith @*/
23021b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
230347c6ae99SBarry Smith {
230447c6ae99SBarry Smith   PetscFunctionBegin;
2305171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
230647c6ae99SBarry Smith   dm->ctx = ctx;
230747c6ae99SBarry Smith   PetscFunctionReturn(0);
230847c6ae99SBarry Smith }
230947c6ae99SBarry Smith 
231047c6ae99SBarry Smith #undef __FUNCT__
23111b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
231247c6ae99SBarry Smith /*@
23131b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
231447c6ae99SBarry Smith 
231547c6ae99SBarry Smith     Not Collective
231647c6ae99SBarry Smith 
231747c6ae99SBarry Smith     Input Parameter:
231847c6ae99SBarry Smith .   dm - the DM object
231947c6ae99SBarry Smith 
232047c6ae99SBarry Smith     Output Parameter:
232147c6ae99SBarry Smith .   ctx - the user context
232247c6ae99SBarry Smith 
232347c6ae99SBarry Smith     Level: intermediate
232447c6ae99SBarry Smith 
2325e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
232647c6ae99SBarry Smith 
232747c6ae99SBarry Smith @*/
23281b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
232947c6ae99SBarry Smith {
233047c6ae99SBarry Smith   PetscFunctionBegin;
2331171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23321b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
233347c6ae99SBarry Smith   PetscFunctionReturn(0);
233447c6ae99SBarry Smith }
233547c6ae99SBarry Smith 
233647c6ae99SBarry Smith #undef __FUNCT__
233708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
233808da532bSDmitry Karpeev /*@C
233908da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
234008da532bSDmitry Karpeev 
234108da532bSDmitry Karpeev     Logically Collective on DM
234208da532bSDmitry Karpeev 
234308da532bSDmitry Karpeev     Input Parameter:
234408da532bSDmitry Karpeev +   dm - the DM object
23450298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
234608da532bSDmitry Karpeev 
234708da532bSDmitry Karpeev     Level: intermediate
234808da532bSDmitry Karpeev 
2349835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
235008da532bSDmitry Karpeev          DMSetJacobian()
235108da532bSDmitry Karpeev 
235208da532bSDmitry Karpeev @*/
235308da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
235408da532bSDmitry Karpeev {
235508da532bSDmitry Karpeev   PetscFunctionBegin;
235608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
235708da532bSDmitry Karpeev   PetscFunctionReturn(0);
235808da532bSDmitry Karpeev }
235908da532bSDmitry Karpeev 
236008da532bSDmitry Karpeev #undef __FUNCT__
236108da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
236208da532bSDmitry Karpeev /*@
236308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
236408da532bSDmitry Karpeev 
236508da532bSDmitry Karpeev     Not Collective
236608da532bSDmitry Karpeev 
236708da532bSDmitry Karpeev     Input Parameter:
236808da532bSDmitry Karpeev .   dm - the DM object to destroy
236908da532bSDmitry Karpeev 
237008da532bSDmitry Karpeev     Output Parameter:
237108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
237208da532bSDmitry Karpeev 
237308da532bSDmitry Karpeev     Level: developer
237408da532bSDmitry Karpeev 
237574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
237608da532bSDmitry Karpeev 
237708da532bSDmitry Karpeev @*/
237808da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
237908da532bSDmitry Karpeev {
238008da532bSDmitry Karpeev   PetscFunctionBegin;
238108da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
238208da532bSDmitry Karpeev   PetscFunctionReturn(0);
238308da532bSDmitry Karpeev }
238408da532bSDmitry Karpeev 
238508da532bSDmitry Karpeev #undef __FUNCT__
238608da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
238708da532bSDmitry Karpeev /*@C
238808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
238908da532bSDmitry Karpeev 
239008da532bSDmitry Karpeev     Logically Collective on DM
239108da532bSDmitry Karpeev 
239208da532bSDmitry Karpeev     Input Parameters:
239308da532bSDmitry Karpeev +   dm - the DM object to destroy
239408da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
239508da532bSDmitry Karpeev 
239608da532bSDmitry Karpeev     Output parameters:
239708da532bSDmitry Karpeev +   xl - lower bound
239808da532bSDmitry Karpeev -   xu - upper bound
239908da532bSDmitry Karpeev 
240008da532bSDmitry Karpeev     Level: intermediate
240108da532bSDmitry Karpeev 
240274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
240308da532bSDmitry Karpeev 
240408da532bSDmitry Karpeev @*/
240508da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
240608da532bSDmitry Karpeev {
240708da532bSDmitry Karpeev   PetscErrorCode ierr;
24085fd66863SKarl Rupp 
240908da532bSDmitry Karpeev   PetscFunctionBegin;
241008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
241108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
241208da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
241308da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
24148865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
241508da532bSDmitry Karpeev   PetscFunctionReturn(0);
241608da532bSDmitry Karpeev }
241708da532bSDmitry Karpeev 
241808da532bSDmitry Karpeev #undef __FUNCT__
2419b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2420b0ae01b7SPeter Brune /*@
2421b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2422b0ae01b7SPeter Brune 
2423b0ae01b7SPeter Brune     Not Collective
2424b0ae01b7SPeter Brune 
2425b0ae01b7SPeter Brune     Input Parameter:
2426b0ae01b7SPeter Brune .   dm - the DM object
2427b0ae01b7SPeter Brune 
2428b0ae01b7SPeter Brune     Output Parameter:
2429b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2430b0ae01b7SPeter Brune 
2431b0ae01b7SPeter Brune     Level: developer
2432b0ae01b7SPeter Brune 
2433b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2434b0ae01b7SPeter Brune 
2435b0ae01b7SPeter Brune @*/
2436b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2437b0ae01b7SPeter Brune {
2438b0ae01b7SPeter Brune   PetscFunctionBegin;
2439b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2440b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2441b0ae01b7SPeter Brune }
2442b0ae01b7SPeter Brune 
2443b0ae01b7SPeter Brune #undef  __FUNCT__
244408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2445748fac09SDmitry Karpeev /*@C
244608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
244708da532bSDmitry Karpeev 
244808da532bSDmitry Karpeev     Collective on DM
244908da532bSDmitry Karpeev 
245008da532bSDmitry Karpeev     Input Parameter:
245108da532bSDmitry Karpeev +   dm - the DM object
24520298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
245308da532bSDmitry Karpeev 
245408da532bSDmitry Karpeev     Level: developer
245508da532bSDmitry Karpeev 
245674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
245708da532bSDmitry Karpeev 
245808da532bSDmitry Karpeev @*/
245908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
246008da532bSDmitry Karpeev {
246108da532bSDmitry Karpeev   PetscErrorCode ierr;
24625fd66863SKarl Rupp 
246308da532bSDmitry Karpeev   PetscFunctionBegin;
246408da532bSDmitry Karpeev   if (x) {
246508da532bSDmitry Karpeev     if (!dm->x) {
246608da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
246708da532bSDmitry Karpeev     }
246808da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
24698865f1eaSKarl Rupp   } else if (dm->x) {
247008da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
247108da532bSDmitry Karpeev   }
247208da532bSDmitry Karpeev   PetscFunctionReturn(0);
247308da532bSDmitry Karpeev }
247408da532bSDmitry Karpeev 
24750298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2476264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2477264ace61SBarry Smith 
2478264ace61SBarry Smith #undef __FUNCT__
2479264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2480264ace61SBarry Smith /*@C
2481264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2482264ace61SBarry Smith 
2483264ace61SBarry Smith   Collective on DM
2484264ace61SBarry Smith 
2485264ace61SBarry Smith   Input Parameters:
2486264ace61SBarry Smith + dm     - The DM object
2487264ace61SBarry Smith - method - The name of the DM type
2488264ace61SBarry Smith 
2489264ace61SBarry Smith   Options Database Key:
2490264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2491264ace61SBarry Smith 
2492264ace61SBarry Smith   Notes:
2493e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2494264ace61SBarry Smith 
2495264ace61SBarry Smith   Level: intermediate
2496264ace61SBarry Smith 
2497264ace61SBarry Smith .keywords: DM, set, type
2498264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2499264ace61SBarry Smith @*/
250019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2501264ace61SBarry Smith {
2502264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2503264ace61SBarry Smith   PetscBool      match;
2504264ace61SBarry Smith   PetscErrorCode ierr;
2505264ace61SBarry Smith 
2506264ace61SBarry Smith   PetscFunctionBegin;
2507264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2508251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2509264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2510264ace61SBarry Smith 
2511607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
25121c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2513ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2514264ace61SBarry Smith 
2515264ace61SBarry Smith   if (dm->ops->destroy) {
2516264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
25170298fd71SBarry Smith     dm->ops->destroy = NULL;
2518264ace61SBarry Smith   }
2519264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2520264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2521264ace61SBarry Smith   PetscFunctionReturn(0);
2522264ace61SBarry Smith }
2523264ace61SBarry Smith 
2524264ace61SBarry Smith #undef __FUNCT__
2525264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2526264ace61SBarry Smith /*@C
2527264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2528264ace61SBarry Smith 
2529264ace61SBarry Smith   Not Collective
2530264ace61SBarry Smith 
2531264ace61SBarry Smith   Input Parameter:
2532264ace61SBarry Smith . dm  - The DM
2533264ace61SBarry Smith 
2534264ace61SBarry Smith   Output Parameter:
2535264ace61SBarry Smith . type - The DM type name
2536264ace61SBarry Smith 
2537264ace61SBarry Smith   Level: intermediate
2538264ace61SBarry Smith 
2539264ace61SBarry Smith .keywords: DM, get, type, name
2540264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2541264ace61SBarry Smith @*/
254219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2543264ace61SBarry Smith {
2544264ace61SBarry Smith   PetscErrorCode ierr;
2545264ace61SBarry Smith 
2546264ace61SBarry Smith   PetscFunctionBegin;
2547264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2548264ace61SBarry Smith   PetscValidCharPointer(type,2);
2549264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2550607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2551264ace61SBarry Smith   }
2552264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2553264ace61SBarry Smith   PetscFunctionReturn(0);
2554264ace61SBarry Smith }
2555264ace61SBarry Smith 
255667a56275SMatthew G Knepley #undef __FUNCT__
255767a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
255867a56275SMatthew G Knepley /*@C
255967a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
256067a56275SMatthew G Knepley 
256167a56275SMatthew G Knepley   Collective on DM
256267a56275SMatthew G Knepley 
256367a56275SMatthew G Knepley   Input Parameters:
256467a56275SMatthew G Knepley + dm - the DM
256567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
256667a56275SMatthew G Knepley 
256767a56275SMatthew G Knepley   Output Parameter:
256867a56275SMatthew G Knepley . M - pointer to new DM
256967a56275SMatthew G Knepley 
257067a56275SMatthew G Knepley   Notes:
257167a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
257267a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
257367a56275SMatthew G Knepley   of the input DM.
257467a56275SMatthew G Knepley 
257567a56275SMatthew G Knepley   Level: intermediate
257667a56275SMatthew G Knepley 
257767a56275SMatthew G Knepley .seealso: DMCreate()
257867a56275SMatthew G Knepley @*/
257919fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
258067a56275SMatthew G Knepley {
258167a56275SMatthew G Knepley   DM             B;
258267a56275SMatthew G Knepley   char           convname[256];
258367a56275SMatthew G Knepley   PetscBool      sametype, issame;
258467a56275SMatthew G Knepley   PetscErrorCode ierr;
258567a56275SMatthew G Knepley 
258667a56275SMatthew G Knepley   PetscFunctionBegin;
258767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
258867a56275SMatthew G Knepley   PetscValidType(dm,1);
258967a56275SMatthew G Knepley   PetscValidPointer(M,3);
2590251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
259167a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
259267a56275SMatthew G Knepley   {
25930298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
259467a56275SMatthew G Knepley 
259567a56275SMatthew G Knepley     /*
259667a56275SMatthew G Knepley        Order of precedence:
259767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
259867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
259967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
260067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
260167a56275SMatthew G Knepley        5) Use a really basic converter.
260267a56275SMatthew G Knepley     */
260367a56275SMatthew G Knepley 
260467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
260567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
260667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
260767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
260867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
260967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26100005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
261167a56275SMatthew G Knepley     if (conv) goto foundconv;
261267a56275SMatthew G Knepley 
261367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
261482f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
261567a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
261667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
261767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
261867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
261967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
262067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26210005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
262267a56275SMatthew G Knepley     if (conv) {
2623fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
262467a56275SMatthew G Knepley       goto foundconv;
262567a56275SMatthew G Knepley     }
262667a56275SMatthew G Knepley 
262767a56275SMatthew G Knepley #if 0
262867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
262967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2630fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
263167a56275SMatthew G Knepley     if (conv) goto foundconv;
263267a56275SMatthew G Knepley 
263367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
263467a56275SMatthew G Knepley     if (dm->ops->convert) {
263567a56275SMatthew G Knepley       conv = dm->ops->convert;
263667a56275SMatthew G Knepley     }
263767a56275SMatthew G Knepley     if (conv) goto foundconv;
263867a56275SMatthew G Knepley #endif
263967a56275SMatthew G Knepley 
264067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
264182f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
264267a56275SMatthew G Knepley 
264367a56275SMatthew G Knepley foundconv:
264467a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
264567a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
264667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
264767a56275SMatthew G Knepley   }
264867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
264967a56275SMatthew G Knepley   PetscFunctionReturn(0);
265067a56275SMatthew G Knepley }
2651264ace61SBarry Smith 
2652264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2653264ace61SBarry Smith 
2654264ace61SBarry Smith #undef __FUNCT__
2655264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2656264ace61SBarry Smith /*@C
26571c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
26581c84c290SBarry Smith 
26591c84c290SBarry Smith   Not Collective
26601c84c290SBarry Smith 
26611c84c290SBarry Smith   Input Parameters:
26621c84c290SBarry Smith + name        - The name of a new user-defined creation routine
26631c84c290SBarry Smith - create_func - The creation routine itself
26641c84c290SBarry Smith 
26651c84c290SBarry Smith   Notes:
26661c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
26671c84c290SBarry Smith 
26681c84c290SBarry Smith 
26691c84c290SBarry Smith   Sample usage:
26701c84c290SBarry Smith .vb
2671bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
26721c84c290SBarry Smith .ve
26731c84c290SBarry Smith 
26741c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
26751c84c290SBarry Smith .vb
26761c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
26771c84c290SBarry Smith     DMSetType(DM,"my_da");
26781c84c290SBarry Smith .ve
26791c84c290SBarry Smith    or at runtime via the option
26801c84c290SBarry Smith .vb
26811c84c290SBarry Smith     -da_type my_da
26821c84c290SBarry Smith .ve
2683264ace61SBarry Smith 
2684264ace61SBarry Smith   Level: advanced
26851c84c290SBarry Smith 
26861c84c290SBarry Smith .keywords: DM, register
2687bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
26881c84c290SBarry Smith 
2689264ace61SBarry Smith @*/
2690bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2691264ace61SBarry Smith {
2692264ace61SBarry Smith   PetscErrorCode ierr;
2693264ace61SBarry Smith 
2694264ace61SBarry Smith   PetscFunctionBegin;
2695a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2696264ace61SBarry Smith   PetscFunctionReturn(0);
2697264ace61SBarry Smith }
2698264ace61SBarry Smith 
2699b859378eSBarry Smith #undef __FUNCT__
2700b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2701b859378eSBarry Smith /*@C
270255849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2703b859378eSBarry Smith 
2704b859378eSBarry Smith   Collective on PetscViewer
2705b859378eSBarry Smith 
2706b859378eSBarry Smith   Input Parameters:
2707b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2708b859378eSBarry Smith            some related function before a call to DMLoad().
2709b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2710b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2711b859378eSBarry Smith 
2712b859378eSBarry Smith    Level: intermediate
2713b859378eSBarry Smith 
2714b859378eSBarry Smith   Notes:
271555849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2716b859378eSBarry Smith 
2717b859378eSBarry Smith   Notes for advanced users:
2718b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2719b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2720b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2721b859378eSBarry Smith   format is
2722b859378eSBarry Smith .vb
2723b859378eSBarry Smith      has not yet been determined
2724b859378eSBarry Smith .ve
2725b859378eSBarry Smith 
2726b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2727b859378eSBarry Smith @*/
2728b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2729b859378eSBarry Smith {
27309331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
2731b859378eSBarry Smith   PetscErrorCode ierr;
2732b859378eSBarry Smith 
2733b859378eSBarry Smith   PetscFunctionBegin;
2734b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2735b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
273632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
27379331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
27389331c7a4SMatthew G. Knepley   if (isbinary) {
27399331c7a4SMatthew G. Knepley     PetscInt classid;
27409331c7a4SMatthew G. Knepley     char     type[256];
2741b859378eSBarry Smith 
274232c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
27439200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
274432c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
274532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
27469331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
27479331c7a4SMatthew G. Knepley   } else if (ishdf5) {
27489331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
27499331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
2750b859378eSBarry Smith   PetscFunctionReturn(0);
2751b859378eSBarry Smith }
2752b859378eSBarry Smith 
27537da65231SMatthew G Knepley /******************************** FEM Support **********************************/
27547da65231SMatthew G Knepley 
27557da65231SMatthew G Knepley #undef __FUNCT__
27567da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2757a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2758a6dfd86eSKarl Rupp {
27591d47ebbbSSatish Balay   PetscInt       f;
27601b30c384SMatthew G Knepley   PetscErrorCode ierr;
27611b30c384SMatthew G Knepley 
27627da65231SMatthew G Knepley   PetscFunctionBegin;
276374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27641d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
276557622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
27667da65231SMatthew G Knepley   }
27677da65231SMatthew G Knepley   PetscFunctionReturn(0);
27687da65231SMatthew G Knepley }
27697da65231SMatthew G Knepley 
27707da65231SMatthew G Knepley #undef __FUNCT__
27717da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2772a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2773a6dfd86eSKarl Rupp {
27741b30c384SMatthew G Knepley   PetscInt       f, g;
27757da65231SMatthew G Knepley   PetscErrorCode ierr;
27767da65231SMatthew G Knepley 
27777da65231SMatthew G Knepley   PetscFunctionBegin;
277874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27791d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
278074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
27811d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
2782e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
27837da65231SMatthew G Knepley     }
278474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
27857da65231SMatthew G Knepley   }
27867da65231SMatthew G Knepley   PetscFunctionReturn(0);
27877da65231SMatthew G Knepley }
2788e7c4fc90SDmitry Karpeev 
2789970e74d5SMatthew G Knepley #undef __FUNCT__
2790e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
27916113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
2792e759306cSMatthew G. Knepley {
2793e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
2794e759306cSMatthew G. Knepley   PetscInt       p;
2795e759306cSMatthew G. Knepley   PetscErrorCode ierr;
2796e759306cSMatthew G. Knepley 
2797e759306cSMatthew G. Knepley   PetscFunctionBegin;
2798e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
2799e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
2800e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
2801e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
2802e759306cSMatthew G. Knepley     if (p == rank) {
2803e759306cSMatthew G. Knepley       Vec x;
2804e759306cSMatthew G. Knepley 
2805e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
2806e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
28076113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
2808e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2809e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
2810e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2811e759306cSMatthew G. Knepley     }
2812e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
2813e759306cSMatthew G. Knepley   }
2814e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
2815e759306cSMatthew G. Knepley }
2816e759306cSMatthew G. Knepley 
2817e759306cSMatthew G. Knepley #undef __FUNCT__
281888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
281988ed4aceSMatthew G Knepley /*@
282088ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
282188ed4aceSMatthew G Knepley 
282288ed4aceSMatthew G Knepley   Input Parameter:
282388ed4aceSMatthew G Knepley . dm - The DM
282488ed4aceSMatthew G Knepley 
282588ed4aceSMatthew G Knepley   Output Parameter:
282688ed4aceSMatthew G Knepley . section - The PetscSection
282788ed4aceSMatthew G Knepley 
282888ed4aceSMatthew G Knepley   Level: intermediate
282988ed4aceSMatthew G Knepley 
283088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
283188ed4aceSMatthew G Knepley 
283288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
283388ed4aceSMatthew G Knepley @*/
28340adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
28350adebc6cSBarry Smith {
2836fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
2837fd59a867SMatthew G. Knepley 
283888ed4aceSMatthew G Knepley   PetscFunctionBegin;
283988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
2841fd59a867SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);}
284288ed4aceSMatthew G Knepley   *section = dm->defaultSection;
284388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
284488ed4aceSMatthew G Knepley }
284588ed4aceSMatthew G Knepley 
284688ed4aceSMatthew G Knepley #undef __FUNCT__
284788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
284888ed4aceSMatthew G Knepley /*@
284988ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
285088ed4aceSMatthew G Knepley 
285188ed4aceSMatthew G Knepley   Input Parameters:
285288ed4aceSMatthew G Knepley + dm - The DM
285388ed4aceSMatthew G Knepley - section - The PetscSection
285488ed4aceSMatthew G Knepley 
285588ed4aceSMatthew G Knepley   Level: intermediate
285688ed4aceSMatthew G Knepley 
285788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
285888ed4aceSMatthew G Knepley 
285988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
286088ed4aceSMatthew G Knepley @*/
28610adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
28620adebc6cSBarry Smith {
2863af122d2aSMatthew G Knepley   PetscInt       numFields;
2864af122d2aSMatthew G Knepley   PetscInt       f;
286588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
286688ed4aceSMatthew G Knepley 
286788ed4aceSMatthew G Knepley   PetscFunctionBegin;
286888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28691d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
28701d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
287188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
287288ed4aceSMatthew G Knepley   dm->defaultSection = section;
2873af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2874af122d2aSMatthew G Knepley   if (numFields) {
2875af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2876af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
28770f21e855SMatthew G. Knepley       PetscObject disc;
2878af122d2aSMatthew G Knepley       const char *name;
2879af122d2aSMatthew G Knepley 
2880af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
28810f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
28820f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
2883af122d2aSMatthew G Knepley     }
2884af122d2aSMatthew G Knepley   }
28851d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
28861d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
288788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
288888ed4aceSMatthew G Knepley }
288988ed4aceSMatthew G Knepley 
289088ed4aceSMatthew G Knepley #undef __FUNCT__
289188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
289288ed4aceSMatthew G Knepley /*@
289388ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
289488ed4aceSMatthew G Knepley 
28958b1ab98fSJed Brown   Collective on DM
28968b1ab98fSJed Brown 
289788ed4aceSMatthew G Knepley   Input Parameter:
289888ed4aceSMatthew G Knepley . dm - The DM
289988ed4aceSMatthew G Knepley 
290088ed4aceSMatthew G Knepley   Output Parameter:
290188ed4aceSMatthew G Knepley . section - The PetscSection
290288ed4aceSMatthew G Knepley 
290388ed4aceSMatthew G Knepley   Level: intermediate
290488ed4aceSMatthew G Knepley 
290588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
290688ed4aceSMatthew G Knepley 
290788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
290888ed4aceSMatthew G Knepley @*/
29090adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
29100adebc6cSBarry Smith {
291188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
291288ed4aceSMatthew G Knepley 
291388ed4aceSMatthew G Knepley   PetscFunctionBegin;
291488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
291688ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
2917fd59a867SMatthew G. Knepley     PetscSection s;
2918fd59a867SMatthew G. Knepley 
2919fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
2920fd59a867SMatthew 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");
2921fd59a867SMatthew 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");
2922fd59a867SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2923cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
2924ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
292588ed4aceSMatthew G Knepley   }
292688ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
292788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
292888ed4aceSMatthew G Knepley }
292988ed4aceSMatthew G Knepley 
293088ed4aceSMatthew G Knepley #undef __FUNCT__
2931b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2932b21d0597SMatthew G Knepley /*@
2933b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2934b21d0597SMatthew G Knepley 
2935b21d0597SMatthew G Knepley   Input Parameters:
2936b21d0597SMatthew G Knepley + dm - The DM
29375080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2938b21d0597SMatthew G Knepley 
2939b21d0597SMatthew G Knepley   Level: intermediate
2940b21d0597SMatthew G Knepley 
2941b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2942b21d0597SMatthew G Knepley 
2943b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2944b21d0597SMatthew G Knepley @*/
29450adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
29460adebc6cSBarry Smith {
2947b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2948b21d0597SMatthew G Knepley 
2949b21d0597SMatthew G Knepley   PetscFunctionBegin;
2950b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29515080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
29521d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2953b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2954b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2955b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2956b21d0597SMatthew G Knepley }
2957b21d0597SMatthew G Knepley 
2958b21d0597SMatthew G Knepley #undef __FUNCT__
295988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
296088ed4aceSMatthew G Knepley /*@
296188ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
296288ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
296388ed4aceSMatthew G Knepley 
296488ed4aceSMatthew G Knepley   Input Parameter:
296588ed4aceSMatthew G Knepley . dm - The DM
296688ed4aceSMatthew G Knepley 
296788ed4aceSMatthew G Knepley   Output Parameter:
296888ed4aceSMatthew G Knepley . sf - The PetscSF
296988ed4aceSMatthew G Knepley 
297088ed4aceSMatthew G Knepley   Level: intermediate
297188ed4aceSMatthew G Knepley 
297288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
297388ed4aceSMatthew G Knepley 
297488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
297588ed4aceSMatthew G Knepley @*/
29760adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
29770adebc6cSBarry Smith {
297888ed4aceSMatthew G Knepley   PetscInt       nroots;
297988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
298088ed4aceSMatthew G Knepley 
298188ed4aceSMatthew G Knepley   PetscFunctionBegin;
298288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
298388ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
29840298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
298588ed4aceSMatthew G Knepley   if (nroots < 0) {
298688ed4aceSMatthew G Knepley     PetscSection section, gSection;
298788ed4aceSMatthew G Knepley 
298888ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
298931ea6d37SMatthew G Knepley     if (section) {
299088ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
299188ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
299231ea6d37SMatthew G Knepley     } else {
29930298fd71SBarry Smith       *sf = NULL;
299431ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
299531ea6d37SMatthew G Knepley     }
299688ed4aceSMatthew G Knepley   }
299788ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
299888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
299988ed4aceSMatthew G Knepley }
300088ed4aceSMatthew G Knepley 
300188ed4aceSMatthew G Knepley #undef __FUNCT__
300288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
300388ed4aceSMatthew G Knepley /*@
300488ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
300588ed4aceSMatthew G Knepley 
300688ed4aceSMatthew G Knepley   Input Parameters:
300788ed4aceSMatthew G Knepley + dm - The DM
300888ed4aceSMatthew G Knepley - sf - The PetscSF
300988ed4aceSMatthew G Knepley 
301088ed4aceSMatthew G Knepley   Level: intermediate
301188ed4aceSMatthew G Knepley 
301288ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
301388ed4aceSMatthew G Knepley 
301488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
301588ed4aceSMatthew G Knepley @*/
30160adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
30170adebc6cSBarry Smith {
301888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
301988ed4aceSMatthew G Knepley 
302088ed4aceSMatthew G Knepley   PetscFunctionBegin;
302188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
302388ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
302488ed4aceSMatthew G Knepley   dm->defaultSF = sf;
302588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
302688ed4aceSMatthew G Knepley }
302788ed4aceSMatthew G Knepley 
302888ed4aceSMatthew G Knepley #undef __FUNCT__
302988ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
303088ed4aceSMatthew G Knepley /*@C
303188ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
303288ed4aceSMatthew G Knepley   describing the data layout.
303388ed4aceSMatthew G Knepley 
303488ed4aceSMatthew G Knepley   Input Parameters:
303588ed4aceSMatthew G Knepley + dm - The DM
303688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
303788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
303888ed4aceSMatthew G Knepley 
303988ed4aceSMatthew G Knepley   Level: intermediate
304088ed4aceSMatthew G Knepley 
304188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
304288ed4aceSMatthew G Knepley @*/
304388ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
304488ed4aceSMatthew G Knepley {
304582f516ccSBarry Smith   MPI_Comm       comm;
304688ed4aceSMatthew G Knepley   PetscLayout    layout;
304788ed4aceSMatthew G Knepley   const PetscInt *ranges;
304888ed4aceSMatthew G Knepley   PetscInt       *local;
304988ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3050ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
305188ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
305288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
305388ed4aceSMatthew G Knepley 
305488ed4aceSMatthew G Knepley   PetscFunctionBegin;
305582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
305688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
305788ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
305888ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
305988ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
306088ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
306188ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
306288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
306388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
306488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
306588ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3066ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
30676636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
306888ed4aceSMatthew G Knepley 
30696636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30706636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3071235fbf56SMatthew 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));
30726636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
307388ed4aceSMatthew G Knepley   }
3074785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3075785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
307688ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
30771f588964SMatthew G Knepley     const PetscInt *cind;
30786636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
307988ed4aceSMatthew G Knepley 
308088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
308188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
308288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
308388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
308488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30856636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
308688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
30876636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
30886636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
30896636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3090057b4bcdSMatthew 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);
30916636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
30926636e97aSMatthew G Knepley     }
309388ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
309488ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
309588ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
309688ed4aceSMatthew G Knepley     }
309788ed4aceSMatthew G Knepley     if (gdof < 0) {
30986636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
309988ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
310088ed4aceSMatthew G Knepley 
310105376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
310231d3f06eSJed Brown         if (r < 0) r = -(r+2);
310305376888SMatthew 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);
310488ed4aceSMatthew G Knepley         remote[l].rank  = r;
310588ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
310688ed4aceSMatthew G Knepley       }
310788ed4aceSMatthew G Knepley     } else {
31086636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
310988ed4aceSMatthew G Knepley         remote[l].rank  = rank;
311088ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
311188ed4aceSMatthew G Knepley       }
311288ed4aceSMatthew G Knepley     }
311388ed4aceSMatthew G Knepley   }
31146636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
311588ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
311688ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
311788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
311888ed4aceSMatthew G Knepley }
3119af122d2aSMatthew G Knepley 
3120af122d2aSMatthew G Knepley #undef __FUNCT__
3121b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3122b21d0597SMatthew G Knepley /*@
3123b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3124b21d0597SMatthew G Knepley 
3125b21d0597SMatthew G Knepley   Input Parameter:
3126b21d0597SMatthew G Knepley . dm - The DM
3127b21d0597SMatthew G Knepley 
3128b21d0597SMatthew G Knepley   Output Parameter:
3129b21d0597SMatthew G Knepley . sf - The PetscSF
3130b21d0597SMatthew G Knepley 
3131b21d0597SMatthew G Knepley   Level: intermediate
3132b21d0597SMatthew G Knepley 
3133b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3134b21d0597SMatthew G Knepley 
3135057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3136b21d0597SMatthew G Knepley @*/
31370adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
31380adebc6cSBarry Smith {
3139b21d0597SMatthew G Knepley   PetscFunctionBegin;
3140b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3141b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3142b21d0597SMatthew G Knepley   *sf = dm->sf;
3143b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3144b21d0597SMatthew G Knepley }
3145b21d0597SMatthew G Knepley 
3146b21d0597SMatthew G Knepley #undef __FUNCT__
3147057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3148057b4bcdSMatthew G Knepley /*@
3149057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3150057b4bcdSMatthew G Knepley 
3151057b4bcdSMatthew G Knepley   Input Parameters:
3152057b4bcdSMatthew G Knepley + dm - The DM
3153057b4bcdSMatthew G Knepley - sf - The PetscSF
3154057b4bcdSMatthew G Knepley 
3155057b4bcdSMatthew G Knepley   Level: intermediate
3156057b4bcdSMatthew G Knepley 
3157057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3158057b4bcdSMatthew G Knepley @*/
31590adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
31600adebc6cSBarry Smith {
3161057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3162057b4bcdSMatthew G Knepley 
3163057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3164057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3165057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3166057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3167057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3168057b4bcdSMatthew G Knepley   dm->sf = sf;
3169057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3170057b4bcdSMatthew G Knepley }
3171057b4bcdSMatthew G Knepley 
3172057b4bcdSMatthew G Knepley #undef __FUNCT__
31732764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
31742764a2aaSMatthew G. Knepley /*@
31752764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
31762764a2aaSMatthew G. Knepley 
31772764a2aaSMatthew G. Knepley   Input Parameter:
31782764a2aaSMatthew G. Knepley . dm - The DM
31792764a2aaSMatthew G. Knepley 
31802764a2aaSMatthew G. Knepley   Output Parameter:
31812764a2aaSMatthew G. Knepley . prob - The PetscDS
31822764a2aaSMatthew G. Knepley 
31832764a2aaSMatthew G. Knepley   Level: developer
31842764a2aaSMatthew G. Knepley 
31852764a2aaSMatthew G. Knepley .seealso: DMSetDS()
31862764a2aaSMatthew G. Knepley @*/
31872764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3188af122d2aSMatthew G Knepley {
3189af122d2aSMatthew G Knepley   PetscFunctionBegin;
3190af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31910f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
31920f21e855SMatthew G. Knepley   *prob = dm->prob;
31930f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
31940f21e855SMatthew G. Knepley }
31950f21e855SMatthew G. Knepley 
31960f21e855SMatthew G. Knepley #undef __FUNCT__
31972764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
31982764a2aaSMatthew G. Knepley /*@
31992764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
32002764a2aaSMatthew G. Knepley 
32012764a2aaSMatthew G. Knepley   Input Parameters:
32022764a2aaSMatthew G. Knepley + dm - The DM
32032764a2aaSMatthew G. Knepley - prob - The PetscDS
32042764a2aaSMatthew G. Knepley 
32052764a2aaSMatthew G. Knepley   Level: developer
32062764a2aaSMatthew G. Knepley 
32072764a2aaSMatthew G. Knepley .seealso: DMGetDS()
32082764a2aaSMatthew G. Knepley @*/
32092764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
32100f21e855SMatthew G. Knepley {
32110f21e855SMatthew G. Knepley   PetscErrorCode ierr;
32120f21e855SMatthew G. Knepley 
32130f21e855SMatthew G. Knepley   PetscFunctionBegin;
32140f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32152764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
32162764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
32170f21e855SMatthew G. Knepley   dm->prob = prob;
32180f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
32190f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
32200f21e855SMatthew G. Knepley }
32210f21e855SMatthew G. Knepley 
32220f21e855SMatthew G. Knepley #undef __FUNCT__
32230f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
32240f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
32250f21e855SMatthew G. Knepley {
32260f21e855SMatthew G. Knepley   PetscErrorCode ierr;
32270f21e855SMatthew G. Knepley 
32280f21e855SMatthew G. Knepley   PetscFunctionBegin;
32290f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32302764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3231af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3232af122d2aSMatthew G Knepley }
3233af122d2aSMatthew G Knepley 
3234af122d2aSMatthew G Knepley #undef __FUNCT__
3235af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3236af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3237af122d2aSMatthew G Knepley {
32380f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3239af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3240af122d2aSMatthew G Knepley 
3241af122d2aSMatthew G Knepley   PetscFunctionBegin;
3242af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32432764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
32440f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
32450f21e855SMatthew G. Knepley     PetscContainer obj;
32460f21e855SMatthew G. Knepley 
32470f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
32482764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
32490f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3250af122d2aSMatthew G Knepley   }
3251af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3252af122d2aSMatthew G Knepley }
3253af122d2aSMatthew G Knepley 
3254af122d2aSMatthew G Knepley #undef __FUNCT__
3255af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3256c1929be8SMatthew G. Knepley /*@
3257c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3258c1929be8SMatthew G. Knepley 
3259c1929be8SMatthew G. Knepley   Not collective
3260c1929be8SMatthew G. Knepley 
3261c1929be8SMatthew G. Knepley   Input Parameters:
3262c1929be8SMatthew G. Knepley + dm - The DM
3263c1929be8SMatthew G. Knepley - f  - The field number
3264c1929be8SMatthew G. Knepley 
3265c1929be8SMatthew G. Knepley   Output Parameter:
3266c1929be8SMatthew G. Knepley . field - The discretization object
3267c1929be8SMatthew G. Knepley 
3268c1929be8SMatthew G. Knepley   Level: developer
3269c1929be8SMatthew G. Knepley 
3270c1929be8SMatthew G. Knepley .seealso: DMSetField()
3271c1929be8SMatthew G. Knepley @*/
3272af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3273af122d2aSMatthew G Knepley {
32740f21e855SMatthew G. Knepley   PetscErrorCode ierr;
32750f21e855SMatthew G. Knepley 
3276af122d2aSMatthew G Knepley   PetscFunctionBegin;
3277af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32782764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3279decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3280decb47aaSMatthew G. Knepley }
3281decb47aaSMatthew G. Knepley 
3282decb47aaSMatthew G. Knepley #undef __FUNCT__
3283decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3284c1929be8SMatthew G. Knepley /*@
3285c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3286c1929be8SMatthew G. Knepley 
3287c1929be8SMatthew G. Knepley   Logically collective on DM
3288c1929be8SMatthew G. Knepley 
3289c1929be8SMatthew G. Knepley   Input Parameters:
3290c1929be8SMatthew G. Knepley + dm - The DM
3291c1929be8SMatthew G. Knepley . f  - The field number
3292c1929be8SMatthew G. Knepley - field - The discretization object
3293c1929be8SMatthew G. Knepley 
3294c1929be8SMatthew G. Knepley   Level: developer
3295c1929be8SMatthew G. Knepley 
3296c1929be8SMatthew G. Knepley .seealso: DMGetField()
3297c1929be8SMatthew G. Knepley @*/
3298decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3299decb47aaSMatthew G. Knepley {
3300decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3301decb47aaSMatthew G. Knepley 
3302decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3303decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33042764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3305af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3306af122d2aSMatthew G Knepley }
33076636e97aSMatthew G Knepley 
33086636e97aSMatthew G Knepley #undef __FUNCT__
3309b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3310b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3311b64e0483SPeter Brune {
3312b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3313b64e0483SPeter Brune   PetscErrorCode ierr;
3314b64e0483SPeter Brune   Vec coords,ccoords;
3315b64e0483SPeter Brune   VecScatter scat;
3316b64e0483SPeter Brune   PetscFunctionBegin;
3317b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3318b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3319b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3320b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3321b64e0483SPeter Brune   if (coords && !ccoords) {
3322b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
3323b64e0483SPeter Brune     ierr = DMCreateInjection(dmc_coord,dm_coord,&scat);CHKERRQ(ierr);
3324b64e0483SPeter Brune     ierr = VecScatterBegin(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3325b64e0483SPeter Brune     ierr = VecScatterEnd(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3326b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3327b64e0483SPeter Brune     ierr = VecScatterDestroy(&scat);CHKERRQ(ierr);
3328b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3329b64e0483SPeter Brune   }
3330b64e0483SPeter Brune   PetscFunctionReturn(0);
3331b64e0483SPeter Brune }
3332b64e0483SPeter Brune 
3333b64e0483SPeter Brune #undef __FUNCT__
333403dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
333503dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
333603dadc2fSPeter Brune {
333703dadc2fSPeter Brune   DM dm_coord,subdm_coord;
333803dadc2fSPeter Brune   PetscErrorCode ierr;
333903dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
334003dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
334103dadc2fSPeter Brune   PetscFunctionBegin;
334203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
334303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
334403dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
334503dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
334603dadc2fSPeter Brune   if (coords && !ccoords) {
334703dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
334803dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
334903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
335003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
335103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
335203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
335303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
335403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
335503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
335603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
335703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
335803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
335903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
336003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
336103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
336203dadc2fSPeter Brune   }
336303dadc2fSPeter Brune   PetscFunctionReturn(0);
336403dadc2fSPeter Brune }
336503dadc2fSPeter Brune 
336603dadc2fSPeter Brune #undef __FUNCT__
33676636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
33686636e97aSMatthew G Knepley /*@
33696636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
33706636e97aSMatthew G Knepley 
33716636e97aSMatthew G Knepley   Collective on DM
33726636e97aSMatthew G Knepley 
33736636e97aSMatthew G Knepley   Input Parameters:
33746636e97aSMatthew G Knepley + dm - the DM
33756636e97aSMatthew G Knepley - c - coordinate vector
33766636e97aSMatthew G Knepley 
33776636e97aSMatthew G Knepley   Note:
33786636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
33796636e97aSMatthew G Knepley 
33806636e97aSMatthew G Knepley   Level: intermediate
33816636e97aSMatthew G Knepley 
33826636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33836636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
33846636e97aSMatthew G Knepley @*/
33856636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
33866636e97aSMatthew G Knepley {
33876636e97aSMatthew G Knepley   PetscErrorCode ierr;
33886636e97aSMatthew G Knepley 
33896636e97aSMatthew G Knepley   PetscFunctionBegin;
33906636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33916636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
33926636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
33936636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
33946636e97aSMatthew G Knepley   dm->coordinates = c;
33956636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3396b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
339703dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
33986636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33996636e97aSMatthew G Knepley }
34006636e97aSMatthew G Knepley 
34016636e97aSMatthew G Knepley #undef __FUNCT__
34026636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
34036636e97aSMatthew G Knepley /*@
34046636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
34056636e97aSMatthew G Knepley 
34066636e97aSMatthew G Knepley   Collective on DM
34076636e97aSMatthew G Knepley 
34086636e97aSMatthew G Knepley    Input Parameters:
34096636e97aSMatthew G Knepley +  dm - the DM
34106636e97aSMatthew G Knepley -  c - coordinate vector
34116636e97aSMatthew G Knepley 
34126636e97aSMatthew G Knepley   Note:
34136636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
34146636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
34156636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
34166636e97aSMatthew G Knepley 
34176636e97aSMatthew G Knepley   Level: intermediate
34186636e97aSMatthew G Knepley 
34196636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
34206636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
34216636e97aSMatthew G Knepley @*/
34226636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
34236636e97aSMatthew G Knepley {
34246636e97aSMatthew G Knepley   PetscErrorCode ierr;
34256636e97aSMatthew G Knepley 
34266636e97aSMatthew G Knepley   PetscFunctionBegin;
34276636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
34296636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
34306636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
34318865f1eaSKarl Rupp 
34326636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
34338865f1eaSKarl Rupp 
34346636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
34356636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34366636e97aSMatthew G Knepley }
34376636e97aSMatthew G Knepley 
34386636e97aSMatthew G Knepley #undef __FUNCT__
34396636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
34406636e97aSMatthew G Knepley /*@
34416636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
34426636e97aSMatthew G Knepley 
34436636e97aSMatthew G Knepley   Not Collective
34446636e97aSMatthew G Knepley 
34456636e97aSMatthew G Knepley   Input Parameter:
34466636e97aSMatthew G Knepley . dm - the DM
34476636e97aSMatthew G Knepley 
34486636e97aSMatthew G Knepley   Output Parameter:
34496636e97aSMatthew G Knepley . c - global coordinate vector
34506636e97aSMatthew G Knepley 
34516636e97aSMatthew G Knepley   Note:
34526636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
34536636e97aSMatthew G Knepley 
34546636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
34556636e97aSMatthew G Knepley 
34566636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
34576636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
34586636e97aSMatthew G Knepley 
34596636e97aSMatthew G Knepley   Level: intermediate
34606636e97aSMatthew G Knepley 
34616636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
34626636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
34636636e97aSMatthew G Knepley @*/
34646636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
34656636e97aSMatthew G Knepley {
34666636e97aSMatthew G Knepley   PetscErrorCode ierr;
34676636e97aSMatthew G Knepley 
34686636e97aSMatthew G Knepley   PetscFunctionBegin;
34696636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34706636e97aSMatthew G Knepley   PetscValidPointer(c,2);
34711f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
34720298fd71SBarry Smith     DM cdm = NULL;
34736636e97aSMatthew G Knepley 
34746636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
34756636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
34766636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
34776636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
34786636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
34796636e97aSMatthew G Knepley   }
34806636e97aSMatthew G Knepley   *c = dm->coordinates;
34816636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34826636e97aSMatthew G Knepley }
34836636e97aSMatthew G Knepley 
34846636e97aSMatthew G Knepley #undef __FUNCT__
34856636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
34866636e97aSMatthew G Knepley /*@
34876636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
34886636e97aSMatthew G Knepley 
34896636e97aSMatthew G Knepley   Collective on DM
34906636e97aSMatthew G Knepley 
34916636e97aSMatthew G Knepley   Input Parameter:
34926636e97aSMatthew G Knepley . dm - the DM
34936636e97aSMatthew G Knepley 
34946636e97aSMatthew G Knepley   Output Parameter:
34956636e97aSMatthew G Knepley . c - coordinate vector
34966636e97aSMatthew G Knepley 
34976636e97aSMatthew G Knepley   Note:
34986636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
34996636e97aSMatthew G Knepley 
35006636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
35016636e97aSMatthew G Knepley 
35026636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
35036636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
35046636e97aSMatthew G Knepley 
35056636e97aSMatthew G Knepley   Level: intermediate
35066636e97aSMatthew G Knepley 
35076636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35086636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
35096636e97aSMatthew G Knepley @*/
35106636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
35116636e97aSMatthew G Knepley {
35126636e97aSMatthew G Knepley   PetscErrorCode ierr;
35136636e97aSMatthew G Knepley 
35146636e97aSMatthew G Knepley   PetscFunctionBegin;
35156636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35166636e97aSMatthew G Knepley   PetscValidPointer(c,2);
35171f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
35180298fd71SBarry Smith     DM cdm = NULL;
35196636e97aSMatthew G Knepley 
35206636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
35216636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
35226636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
35236636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
35246636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
35256636e97aSMatthew G Knepley   }
35266636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
35276636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35286636e97aSMatthew G Knepley }
35296636e97aSMatthew G Knepley 
35306636e97aSMatthew G Knepley #undef __FUNCT__
35316636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
35326636e97aSMatthew G Knepley /*@
35331cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
35346636e97aSMatthew G Knepley 
35356636e97aSMatthew G Knepley   Collective on DM
35366636e97aSMatthew G Knepley 
35376636e97aSMatthew G Knepley   Input Parameter:
35386636e97aSMatthew G Knepley . dm - the DM
35396636e97aSMatthew G Knepley 
35406636e97aSMatthew G Knepley   Output Parameter:
35416636e97aSMatthew G Knepley . cdm - coordinate DM
35426636e97aSMatthew G Knepley 
35436636e97aSMatthew G Knepley   Level: intermediate
35446636e97aSMatthew G Knepley 
35456636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35461cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
35476636e97aSMatthew G Knepley @*/
35486636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
35496636e97aSMatthew G Knepley {
35506636e97aSMatthew G Knepley   PetscErrorCode ierr;
35516636e97aSMatthew G Knepley 
35526636e97aSMatthew G Knepley   PetscFunctionBegin;
35536636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35546636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
35556636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
355682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
35576636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
35586636e97aSMatthew G Knepley   }
35596636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
35606636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35616636e97aSMatthew G Knepley }
3562e87bb0d3SMatthew G Knepley 
3563e87bb0d3SMatthew G Knepley #undef __FUNCT__
35641cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
35651cfe2091SMatthew G. Knepley /*@
35661cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
35671cfe2091SMatthew G. Knepley 
35681cfe2091SMatthew G. Knepley   Logically Collective on DM
35691cfe2091SMatthew G. Knepley 
35701cfe2091SMatthew G. Knepley   Input Parameters:
35711cfe2091SMatthew G. Knepley + dm - the DM
35721cfe2091SMatthew G. Knepley - cdm - coordinate DM
35731cfe2091SMatthew G. Knepley 
35741cfe2091SMatthew G. Knepley   Level: intermediate
35751cfe2091SMatthew G. Knepley 
35761cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35771cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
35781cfe2091SMatthew G. Knepley @*/
35791cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
35801cfe2091SMatthew G. Knepley {
35811cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
35821cfe2091SMatthew G. Knepley 
35831cfe2091SMatthew G. Knepley   PetscFunctionBegin;
35841cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35851cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
35861cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
35871cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
35881cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
35891cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
35901cfe2091SMatthew G. Knepley }
35911cfe2091SMatthew G. Knepley 
35921cfe2091SMatthew G. Knepley #undef __FUNCT__
3593e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
3594e8abe2deSMatthew G. Knepley /*@
3595e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
3596e8abe2deSMatthew G. Knepley 
3597e8abe2deSMatthew G. Knepley   Not Collective
3598e8abe2deSMatthew G. Knepley 
3599e8abe2deSMatthew G. Knepley   Input Parameter:
3600e8abe2deSMatthew G. Knepley . dm - The DM object
3601e8abe2deSMatthew G. Knepley 
3602e8abe2deSMatthew G. Knepley   Output Parameter:
3603e8abe2deSMatthew G. Knepley . section - The PetscSection object
3604e8abe2deSMatthew G. Knepley 
3605e8abe2deSMatthew G. Knepley   Level: intermediate
3606e8abe2deSMatthew G. Knepley 
3607e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
3608e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
3609e8abe2deSMatthew G. Knepley @*/
3610e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
3611e8abe2deSMatthew G. Knepley {
3612e8abe2deSMatthew G. Knepley   DM             cdm;
3613e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
3614e8abe2deSMatthew G. Knepley 
3615e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
3616e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3617e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
3618e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3619e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
3620e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
3621e8abe2deSMatthew G. Knepley }
3622e8abe2deSMatthew G. Knepley 
3623e8abe2deSMatthew G. Knepley #undef __FUNCT__
3624e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
3625e8abe2deSMatthew G. Knepley /*@
3626e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
3627e8abe2deSMatthew G. Knepley 
3628e8abe2deSMatthew G. Knepley   Not Collective
3629e8abe2deSMatthew G. Knepley 
3630e8abe2deSMatthew G. Knepley   Input Parameters:
3631e8abe2deSMatthew G. Knepley + dm      - The DM object
3632e8abe2deSMatthew G. Knepley - section - The PetscSection object
3633e8abe2deSMatthew G. Knepley 
3634e8abe2deSMatthew G. Knepley   Level: intermediate
3635e8abe2deSMatthew G. Knepley 
3636e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
3637e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
3638e8abe2deSMatthew G. Knepley @*/
3639e8abe2deSMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscSection section)
3640e8abe2deSMatthew G. Knepley {
3641e8abe2deSMatthew G. Knepley   DM             cdm;
3642e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
3643e8abe2deSMatthew G. Knepley 
3644e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
3645e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3646e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3647e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3648e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
3649e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
3650e8abe2deSMatthew G. Knepley }
3651e8abe2deSMatthew G. Knepley 
3652e8abe2deSMatthew G. Knepley #undef __FUNCT__
3653c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
3654c6b900c6SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L)
3655c6b900c6SMatthew G. Knepley {
3656c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
3657c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3658c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
3659c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
3660c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
3661c6b900c6SMatthew G. Knepley }
3662c6b900c6SMatthew G. Knepley 
3663c6b900c6SMatthew G. Knepley #undef __FUNCT__
3664c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
3665c6b900c6SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[])
3666c6b900c6SMatthew G. Knepley {
3667c6b900c6SMatthew G. Knepley   Vec            coordinates;
3668c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
3669c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
3670c6b900c6SMatthew G. Knepley 
3671c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
3672c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3673c6b900c6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
3674c6b900c6SMatthew G. Knepley   ierr = VecGetBlockSize(coordinates, &dim);CHKERRQ(ierr);
3675c6b900c6SMatthew G. Knepley   ierr = PetscMalloc1(dim,&dm->L);CHKERRQ(ierr);
3676c6b900c6SMatthew G. Knepley   ierr = PetscMalloc1(dim,&dm->maxCell);CHKERRQ(ierr);
3677c6b900c6SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d];}
3678c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
3679c6b900c6SMatthew G. Knepley }
3680c6b900c6SMatthew G. Knepley 
3681c6b900c6SMatthew G. Knepley #undef __FUNCT__
3682e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3683e87bb0d3SMatthew G Knepley /*@
3684e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3685e87bb0d3SMatthew G Knepley 
3686e87bb0d3SMatthew G Knepley   Not collective
3687e87bb0d3SMatthew G Knepley 
3688e87bb0d3SMatthew G Knepley   Input Parameters:
3689e87bb0d3SMatthew G Knepley + dm - The DM
3690e87bb0d3SMatthew G Knepley - v - The Vec of points
3691e87bb0d3SMatthew G Knepley 
369261e3bb9bSMatthew G Knepley   Output Parameter:
3693e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3694e87bb0d3SMatthew G Knepley 
3695e87bb0d3SMatthew G Knepley   Level: developer
369661e3bb9bSMatthew G Knepley 
369761e3bb9bSMatthew G Knepley .keywords: point location, mesh
369861e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
369961e3bb9bSMatthew G Knepley @*/
3700e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3701e87bb0d3SMatthew G Knepley {
3702735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3703735aa83eSMatthew G Knepley 
3704e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3705e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3706e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3707e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3708735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3709735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
371082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3711e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3712e87bb0d3SMatthew G Knepley }
371314f150ffSMatthew G. Knepley 
371414f150ffSMatthew G. Knepley #undef __FUNCT__
371514f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
3716f4d763aaSMatthew G. Knepley /*@
3717f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
3718f4d763aaSMatthew G. Knepley 
3719f4d763aaSMatthew G. Knepley   Input Parameter:
3720f4d763aaSMatthew G. Knepley . dm - The original DM
3721f4d763aaSMatthew G. Knepley 
3722f4d763aaSMatthew G. Knepley   Output Parameter:
3723f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
3724f4d763aaSMatthew G. Knepley 
3725f4d763aaSMatthew G. Knepley   Level: intermediate
3726f4d763aaSMatthew G. Knepley 
3727f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
3728f4d763aaSMatthew G. Knepley @*/
372914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
373014f150ffSMatthew G. Knepley {
3731c26acbdeSMatthew G. Knepley   PetscSection   section;
3732c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
373314f150ffSMatthew G. Knepley   PetscErrorCode ierr;
373414f150ffSMatthew G. Knepley 
373514f150ffSMatthew G. Knepley   PetscFunctionBegin;
373614f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
373714f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
3738c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
3739c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
3740c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
3741c26acbdeSMatthew G. Knepley     *odm = dm;
3742c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
3743c26acbdeSMatthew G. Knepley   }
374414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
3745c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
374614f150ffSMatthew G. Knepley     PetscSF      sf;
374714f150ffSMatthew G. Knepley 
374814f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
374914f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
375014f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
375114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
375214f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
375314f150ffSMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, &gsection);CHKERRQ(ierr);
375414f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
375514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
375614f150ffSMatthew G. Knepley   }
375714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
375814f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
375914f150ffSMatthew G. Knepley }
3760f4d763aaSMatthew G. Knepley 
3761f4d763aaSMatthew G. Knepley #undef __FUNCT__
3762f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
3763f4d763aaSMatthew G. Knepley /*@
3764*cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
3765f4d763aaSMatthew G. Knepley 
3766f4d763aaSMatthew G. Knepley   Input Parameter:
3767f4d763aaSMatthew G. Knepley . dm - The original DM
3768f4d763aaSMatthew G. Knepley 
3769*cdb7a50dSMatthew G. Knepley   Output Parameters:
3770*cdb7a50dSMatthew G. Knepley + num - The output sequence number
3771*cdb7a50dSMatthew G. Knepley - val - The output sequence value
3772f4d763aaSMatthew G. Knepley 
3773f4d763aaSMatthew G. Knepley   Level: intermediate
3774f4d763aaSMatthew G. Knepley 
3775f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
3776f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
3777f4d763aaSMatthew G. Knepley 
3778f4d763aaSMatthew G. Knepley .seealso: VecView()
3779f4d763aaSMatthew G. Knepley @*/
3780*cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
3781f4d763aaSMatthew G. Knepley {
3782f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
3783f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3784*cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
3785*cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
3786f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
3787f4d763aaSMatthew G. Knepley }
3788f4d763aaSMatthew G. Knepley 
3789f4d763aaSMatthew G. Knepley #undef __FUNCT__
3790f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
3791f4d763aaSMatthew G. Knepley /*@
3792*cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
3793f4d763aaSMatthew G. Knepley 
3794f4d763aaSMatthew G. Knepley   Input Parameters:
3795f4d763aaSMatthew G. Knepley + dm - The original DM
3796*cdb7a50dSMatthew G. Knepley . num - The output sequence number
3797*cdb7a50dSMatthew G. Knepley - val - The output sequence value
3798f4d763aaSMatthew G. Knepley 
3799f4d763aaSMatthew G. Knepley   Level: intermediate
3800f4d763aaSMatthew G. Knepley 
3801f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
3802f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
3803f4d763aaSMatthew G. Knepley 
3804f4d763aaSMatthew G. Knepley .seealso: VecView()
3805f4d763aaSMatthew G. Knepley @*/
3806*cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
3807f4d763aaSMatthew G. Knepley {
3808f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
3809f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3810f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
3811*cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
3812*cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
3813*cdb7a50dSMatthew G. Knepley }
3814*cdb7a50dSMatthew G. Knepley 
3815*cdb7a50dSMatthew G. Knepley #undef __FUNCT__
3816*cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
3817*cdb7a50dSMatthew G. Knepley /*@C
3818*cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
3819*cdb7a50dSMatthew G. Knepley 
3820*cdb7a50dSMatthew G. Knepley   Input Parameters:
3821*cdb7a50dSMatthew G. Knepley + dm   - The original DM
3822*cdb7a50dSMatthew G. Knepley . name - The sequence name
3823*cdb7a50dSMatthew G. Knepley - num  - The output sequence number
3824*cdb7a50dSMatthew G. Knepley 
3825*cdb7a50dSMatthew G. Knepley   Output Parameter:
3826*cdb7a50dSMatthew G. Knepley . val  - The output sequence value
3827*cdb7a50dSMatthew G. Knepley 
3828*cdb7a50dSMatthew G. Knepley   Level: intermediate
3829*cdb7a50dSMatthew G. Knepley 
3830*cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
3831*cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
3832*cdb7a50dSMatthew G. Knepley 
3833*cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
3834*cdb7a50dSMatthew G. Knepley @*/
3835*cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
3836*cdb7a50dSMatthew G. Knepley {
3837*cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
3838*cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
3839*cdb7a50dSMatthew G. Knepley 
3840*cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
3841*cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3842*cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3843*cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
3844*cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3845*cdb7a50dSMatthew G. Knepley   if (ishdf5) {
3846*cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
3847*cdb7a50dSMatthew G. Knepley     PetscScalar value;
3848*cdb7a50dSMatthew G. Knepley 
3849*cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
3850*cdb7a50dSMatthew G. Knepley     *val = (PetscReal) value;
3851*cdb7a50dSMatthew G. Knepley #endif
3852*cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
3853f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
3854f4d763aaSMatthew G. Knepley }
3855