xref: /petsc/src/dm/interface/dm.c (revision 38221697d93bc7c9a6149445938a5de22e85bc83)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8ca266f36SBarry Smith #define __FUNCT__ "DMViewFromOptions"
9ca266f36SBarry Smith /*
10c55c6a9aSJed Brown   DMViewFromOptions - Processes command line options to determine if/how a DM is to be viewed.
11ca266f36SBarry Smith 
12c55c6a9aSJed Brown   Collective on Vec
13c55c6a9aSJed Brown 
14c55c6a9aSJed Brown   Input Parameters:
15c55c6a9aSJed Brown + dm   - the DM
16c55c6a9aSJed Brown . prefix - prefix to use for viewing, or NULL to use prefix of 'rnd'
17c55c6a9aSJed Brown - optionname - option to activate viewing
18c55c6a9aSJed Brown 
19c55c6a9aSJed Brown   Level: intermediate
20c55c6a9aSJed Brown 
21c55c6a9aSJed Brown .keywords: DM, view, options, database
22c55c6a9aSJed Brown .seealso: VecViewFromOptions(), MatViewFromOptions()
23ca266f36SBarry Smith */
24146574abSBarry Smith PetscErrorCode  DMViewFromOptions(DM dm,const char prefix[],const char optionname[])
25ca266f36SBarry Smith {
26ca266f36SBarry Smith   PetscErrorCode    ierr;
27ca266f36SBarry Smith   PetscBool         flg;
28ca266f36SBarry Smith   PetscViewer       viewer;
29cffb1e40SBarry Smith   PetscViewerFormat format;
30ca266f36SBarry Smith 
31ca266f36SBarry Smith   PetscFunctionBegin;
32146574abSBarry Smith   if (prefix) {
33146574abSBarry Smith     ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm),prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr);
34146574abSBarry Smith   } else {
35ce94432eSBarry Smith     ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr);
36146574abSBarry Smith   }
37ca266f36SBarry Smith   if (flg) {
38cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
39ca266f36SBarry Smith     ierr = DMView(dm,viewer);CHKERRQ(ierr);
40cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
41cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
42ca266f36SBarry Smith   }
43ca266f36SBarry Smith   PetscFunctionReturn(0);
44ca266f36SBarry Smith }
45ca266f36SBarry Smith 
46ca266f36SBarry Smith 
47ca266f36SBarry Smith #undef __FUNCT__
48a4121054SBarry Smith #define __FUNCT__ "DMCreate"
49a4121054SBarry Smith /*@
50de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
51a4121054SBarry Smith 
52a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
53a4121054SBarry Smith    error when you try to use the vector.
54a4121054SBarry Smith 
55a4121054SBarry Smith   Collective on MPI_Comm
56a4121054SBarry Smith 
57a4121054SBarry Smith   Input Parameter:
58a4121054SBarry Smith . comm - The communicator for the DM object
59a4121054SBarry Smith 
60a4121054SBarry Smith   Output Parameter:
61a4121054SBarry Smith . dm - The DM object
62a4121054SBarry Smith 
63a4121054SBarry Smith   Level: beginner
64a4121054SBarry Smith 
65a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
66a4121054SBarry Smith @*/
677087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
68a4121054SBarry Smith {
69a4121054SBarry Smith   DM             v;
70a4121054SBarry Smith   PetscErrorCode ierr;
71a4121054SBarry Smith 
72a4121054SBarry Smith   PetscFunctionBegin;
731411c6eeSJed Brown   PetscValidPointer(dm,2);
740298fd71SBarry Smith   *dm = NULL;
75519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
76607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
77607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
78607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
79a4121054SBarry Smith #endif
80a4121054SBarry Smith 
8167c2884eSBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
82a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
831411c6eeSJed Brown 
84e7c4fc90SDmitry Karpeev 
850298fd71SBarry Smith   v->ltogmap              = NULL;
860298fd71SBarry Smith   v->ltogmapb             = NULL;
871411c6eeSJed Brown   v->bs                   = 1;
88171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
8988ed4aceSMatthew G Knepley   ierr                    = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
9088ed4aceSMatthew G Knepley   ierr                    = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
910298fd71SBarry Smith   v->defaultSection       = NULL;
920298fd71SBarry Smith   v->defaultGlobalSection = NULL;
93435a35e8SMatthew G Knepley   {
94435a35e8SMatthew G Knepley     PetscInt i;
95435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
960298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
97435a35e8SMatthew G Knepley     }
98435a35e8SMatthew G Knepley   }
99af122d2aSMatthew G Knepley   v->numFields = 0;
1000298fd71SBarry Smith   v->fields    = NULL;
1011411c6eeSJed Brown 
1021411c6eeSJed Brown   *dm = v;
103a4121054SBarry Smith   PetscFunctionReturn(0);
104a4121054SBarry Smith }
105a4121054SBarry Smith 
106a4121054SBarry Smith #undef __FUNCT__
107*38221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
108*38221697SMatthew G. Knepley /*@
109*38221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
110*38221697SMatthew G. Knepley 
111*38221697SMatthew G. Knepley   Collective on MPI_Comm
112*38221697SMatthew G. Knepley 
113*38221697SMatthew G. Knepley   Input Parameter:
114*38221697SMatthew G. Knepley . dm - The original DM object
115*38221697SMatthew G. Knepley 
116*38221697SMatthew G. Knepley   Output Parameter:
117*38221697SMatthew G. Knepley . newdm  - The new DM object
118*38221697SMatthew G. Knepley 
119*38221697SMatthew G. Knepley   Level: beginner
120*38221697SMatthew G. Knepley 
121*38221697SMatthew G. Knepley .keywords: DM, topology, create
122*38221697SMatthew G. Knepley @*/
123*38221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
124*38221697SMatthew G. Knepley {
125*38221697SMatthew G. Knepley   PetscSF        sf;
126*38221697SMatthew G. Knepley   Vec            coords;
127*38221697SMatthew G. Knepley   void          *ctx;
128*38221697SMatthew G. Knepley   PetscErrorCode ierr;
129*38221697SMatthew G. Knepley 
130*38221697SMatthew G. Knepley   PetscFunctionBegin;
131*38221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
132*38221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
133*38221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
134*38221697SMatthew G. Knepley   if (dm->ops->clone) {
135*38221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
136*38221697SMatthew G. Knepley   }
137*38221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
138*38221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
139*38221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
140*38221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
141*38221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
142*38221697SMatthew G. Knepley   if (coords) {
143*38221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
144*38221697SMatthew G. Knepley   } else {
145*38221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
146*38221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
147*38221697SMatthew G. Knepley   }
148*38221697SMatthew G. Knepley   PetscFunctionReturn(0);
149*38221697SMatthew G. Knepley }
150*38221697SMatthew G. Knepley 
151*38221697SMatthew G. Knepley #undef __FUNCT__
1529a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1539a42bb27SBarry Smith /*@C
154564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1559a42bb27SBarry Smith 
156aa219208SBarry Smith    Logically Collective on DMDA
1579a42bb27SBarry Smith 
1589a42bb27SBarry Smith    Input Parameter:
1599a42bb27SBarry Smith +  da - initial distributed array
1608154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1619a42bb27SBarry Smith 
1629a42bb27SBarry Smith    Options Database:
163dd85299cSBarry Smith .   -dm_vec_type ctype
1649a42bb27SBarry Smith 
1659a42bb27SBarry Smith    Level: intermediate
1669a42bb27SBarry Smith 
167aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
1689a42bb27SBarry Smith @*/
16919fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1709a42bb27SBarry Smith {
1719a42bb27SBarry Smith   PetscErrorCode ierr;
1729a42bb27SBarry Smith 
1739a42bb27SBarry Smith   PetscFunctionBegin;
1749a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1759a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
17619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1779a42bb27SBarry Smith   PetscFunctionReturn(0);
1789a42bb27SBarry Smith }
1799a42bb27SBarry Smith 
1809a42bb27SBarry Smith #undef __FUNCT__
1815f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1825f1ad066SMatthew G Knepley /*@
18334f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1845f1ad066SMatthew G Knepley 
1855f1ad066SMatthew G Knepley   Not collective
1865f1ad066SMatthew G Knepley 
1875f1ad066SMatthew G Knepley   Input Parameter:
1885f1ad066SMatthew G Knepley . v - The Vec
1895f1ad066SMatthew G Knepley 
1905f1ad066SMatthew G Knepley   Output Parameter:
1915f1ad066SMatthew G Knepley . dm - The DM
1925f1ad066SMatthew G Knepley 
1935f1ad066SMatthew G Knepley   Level: intermediate
1945f1ad066SMatthew G Knepley 
1955f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1965f1ad066SMatthew G Knepley @*/
1975f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1985f1ad066SMatthew G Knepley {
1995f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2005f1ad066SMatthew G Knepley 
2015f1ad066SMatthew G Knepley   PetscFunctionBegin;
2025f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2035f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2045f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2055f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2065f1ad066SMatthew G Knepley }
2075f1ad066SMatthew G Knepley 
2085f1ad066SMatthew G Knepley #undef __FUNCT__
2095f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2105f1ad066SMatthew G Knepley /*@
2115f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
2125f1ad066SMatthew G Knepley 
2135f1ad066SMatthew G Knepley   Not collective
2145f1ad066SMatthew G Knepley 
2155f1ad066SMatthew G Knepley   Input Parameters:
2165f1ad066SMatthew G Knepley + v - The Vec
2175f1ad066SMatthew G Knepley - dm - The DM
2185f1ad066SMatthew G Knepley 
2195f1ad066SMatthew G Knepley   Level: intermediate
2205f1ad066SMatthew G Knepley 
2215f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2225f1ad066SMatthew G Knepley @*/
2235f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2245f1ad066SMatthew G Knepley {
2255f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2265f1ad066SMatthew G Knepley 
2275f1ad066SMatthew G Knepley   PetscFunctionBegin;
2285f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
229d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2305f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2315f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2325f1ad066SMatthew G Knepley }
2335f1ad066SMatthew G Knepley 
2345f1ad066SMatthew G Knepley #undef __FUNCT__
235521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
236521d9a4cSLisandro Dalcin /*@C
237521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
238521d9a4cSLisandro Dalcin 
239521d9a4cSLisandro Dalcin    Logically Collective on DM
240521d9a4cSLisandro Dalcin 
241521d9a4cSLisandro Dalcin    Input Parameter:
242521d9a4cSLisandro Dalcin +  dm - the DM context
243521d9a4cSLisandro Dalcin .  ctype - the matrix type
244521d9a4cSLisandro Dalcin 
245521d9a4cSLisandro Dalcin    Options Database:
246521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
247521d9a4cSLisandro Dalcin 
248521d9a4cSLisandro Dalcin    Level: intermediate
249521d9a4cSLisandro Dalcin 
250521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
251521d9a4cSLisandro Dalcin @*/
25219fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
253521d9a4cSLisandro Dalcin {
254521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
25588f0584fSBarry Smith 
256521d9a4cSLisandro Dalcin   PetscFunctionBegin;
257521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
258521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
25919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
260521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
261521d9a4cSLisandro Dalcin }
262521d9a4cSLisandro Dalcin 
263521d9a4cSLisandro Dalcin #undef __FUNCT__
264c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
265c688c046SMatthew G Knepley /*@
26634f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
267c688c046SMatthew G Knepley 
268c688c046SMatthew G Knepley   Not collective
269c688c046SMatthew G Knepley 
270c688c046SMatthew G Knepley   Input Parameter:
271c688c046SMatthew G Knepley . A - The Mat
272c688c046SMatthew G Knepley 
273c688c046SMatthew G Knepley   Output Parameter:
274c688c046SMatthew G Knepley . dm - The DM
275c688c046SMatthew G Knepley 
276c688c046SMatthew G Knepley   Level: intermediate
277c688c046SMatthew G Knepley 
278c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
279c688c046SMatthew G Knepley @*/
280c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
281c688c046SMatthew G Knepley {
282c688c046SMatthew G Knepley   PetscErrorCode ierr;
283c688c046SMatthew G Knepley 
284c688c046SMatthew G Knepley   PetscFunctionBegin;
285c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
286c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
287c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
288c688c046SMatthew G Knepley   PetscFunctionReturn(0);
289c688c046SMatthew G Knepley }
290c688c046SMatthew G Knepley 
291c688c046SMatthew G Knepley #undef __FUNCT__
292c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
293c688c046SMatthew G Knepley /*@
294c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
295c688c046SMatthew G Knepley 
296c688c046SMatthew G Knepley   Not collective
297c688c046SMatthew G Knepley 
298c688c046SMatthew G Knepley   Input Parameters:
299c688c046SMatthew G Knepley + A - The Mat
300c688c046SMatthew G Knepley - dm - The DM
301c688c046SMatthew G Knepley 
302c688c046SMatthew G Knepley   Level: intermediate
303c688c046SMatthew G Knepley 
304c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
305c688c046SMatthew G Knepley @*/
306c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
307c688c046SMatthew G Knepley {
308c688c046SMatthew G Knepley   PetscErrorCode ierr;
309c688c046SMatthew G Knepley 
310c688c046SMatthew G Knepley   PetscFunctionBegin;
311c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3128865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
313c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
314c688c046SMatthew G Knepley   PetscFunctionReturn(0);
315c688c046SMatthew G Knepley }
316c688c046SMatthew G Knepley 
317c688c046SMatthew G Knepley #undef __FUNCT__
3189a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3199a42bb27SBarry Smith /*@C
3209a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
321aa219208SBarry Smith    DMDA options in the database.
3229a42bb27SBarry Smith 
323aa219208SBarry Smith    Logically Collective on DMDA
3249a42bb27SBarry Smith 
3259a42bb27SBarry Smith    Input Parameter:
326aa219208SBarry Smith +  da - the DMDA context
3279a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3289a42bb27SBarry Smith 
3299a42bb27SBarry Smith    Notes:
3309a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3319a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3329a42bb27SBarry Smith 
3339a42bb27SBarry Smith    Level: advanced
3349a42bb27SBarry Smith 
335aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
3369a42bb27SBarry Smith 
3379a42bb27SBarry Smith .seealso: DMSetFromOptions()
3389a42bb27SBarry Smith @*/
3397087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3409a42bb27SBarry Smith {
3419a42bb27SBarry Smith   PetscErrorCode ierr;
3429a42bb27SBarry Smith 
3439a42bb27SBarry Smith   PetscFunctionBegin;
3449a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3459a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
3469a42bb27SBarry Smith   PetscFunctionReturn(0);
3479a42bb27SBarry Smith }
3489a42bb27SBarry Smith 
3499a42bb27SBarry Smith #undef __FUNCT__
35047c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
35147c6ae99SBarry Smith /*@
352aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
35347c6ae99SBarry Smith 
35447c6ae99SBarry Smith     Collective on DM
35547c6ae99SBarry Smith 
35647c6ae99SBarry Smith     Input Parameter:
35747c6ae99SBarry Smith .   dm - the DM object to destroy
35847c6ae99SBarry Smith 
35947c6ae99SBarry Smith     Level: developer
36047c6ae99SBarry Smith 
361e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
36247c6ae99SBarry Smith 
36347c6ae99SBarry Smith @*/
364fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
36547c6ae99SBarry Smith {
366af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
367dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
36847c6ae99SBarry Smith   PetscErrorCode ierr;
36947c6ae99SBarry Smith 
37047c6ae99SBarry Smith   PetscFunctionBegin;
3716bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
3726bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
37387e657c6SBarry Smith 
37487e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
375732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3768865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
3778865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
378732e2eb9SMatthew G Knepley   }
379dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
38071cd77b2SBarry Smith   if ((*dm)->x) {
381c688c046SMatthew G Knepley     DM obj;
382c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
383c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
38471cd77b2SBarry Smith   }
3852348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
3862348bcf4SPeter Brune   if ((*dm)->x) {
3872348bcf4SPeter Brune     DM obj;
3882348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
3892348bcf4SPeter Brune     if (obj == *dm) cnt++;
3902348bcf4SPeter Brune   }
391732e2eb9SMatthew G Knepley 
3926bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
393732e2eb9SMatthew G Knepley   /*
394732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
395732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
396732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
397732e2eb9SMatthew G Knepley   */
3986bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
3996bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
400732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4016bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4026bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
403732e2eb9SMatthew G Knepley   }
404dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
405dfe15315SJed Brown     nnext = nlink->next;
406dfe15315SJed Brown     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
407dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
408dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
409dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
410dfe15315SJed Brown   }
4110298fd71SBarry Smith   (*dm)->namedglobal = NULL;
4121a266240SBarry Smith 
4132348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */
4142348bcf4SPeter Brune     nnext = nlink->next;
4152348bcf4SPeter 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);
4162348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4172348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4182348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4192348bcf4SPeter Brune   }
4200298fd71SBarry Smith   (*dm)->namedlocal = NULL;
4212348bcf4SPeter Brune 
422b17ce1afSJed Brown   /* Destroy the list of hooks */
423c833c3b5SJed Brown   {
424c833c3b5SJed Brown     DMCoarsenHookLink link,next;
425b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
426b17ce1afSJed Brown       next = link->next;
427b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
428b17ce1afSJed Brown     }
4290298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
430c833c3b5SJed Brown   }
431c833c3b5SJed Brown   {
432c833c3b5SJed Brown     DMRefineHookLink link,next;
433c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
434c833c3b5SJed Brown       next = link->next;
435c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
436c833c3b5SJed Brown     }
4370298fd71SBarry Smith     (*dm)->refinehook = NULL;
438c833c3b5SJed Brown   }
439be081cd6SPeter Brune   {
440be081cd6SPeter Brune     DMSubDomainHookLink link,next;
441be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
442be081cd6SPeter Brune       next = link->next;
443be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
444be081cd6SPeter Brune     }
4450298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
446be081cd6SPeter Brune   }
447baf369e7SPeter Brune   {
448baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
449baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
450baf369e7SPeter Brune       next = link->next;
451baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
452baf369e7SPeter Brune     }
4530298fd71SBarry Smith     (*dm)->gtolhook = NULL;
454baf369e7SPeter Brune   }
455aa1993deSMatthew G Knepley   /* Destroy the work arrays */
456aa1993deSMatthew G Knepley   {
457aa1993deSMatthew G Knepley     DMWorkLink link,next;
458aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
459aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
460aa1993deSMatthew G Knepley       next = link->next;
461aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
462aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
463aa1993deSMatthew G Knepley     }
4640298fd71SBarry Smith     (*dm)->workin = NULL;
465aa1993deSMatthew G Knepley   }
466b17ce1afSJed Brown 
46752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
46852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
46952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
47052536dc3SBarry Smith 
4711a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
4721a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
4731a266240SBarry Smith   }
47487e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
47571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
4764dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
4776bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
4786bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
4796bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
480073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
48188ed4aceSMatthew G Knepley 
48288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
48388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
4848b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
48588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
48688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
487af122d2aSMatthew G Knepley 
4886636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
4896636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
4906636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
4916636e97aSMatthew G Knepley 
492af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
493af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
494af122d2aSMatthew G Knepley   }
495af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
496732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
497f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*dm);CHKERRQ(ierr);
498732e2eb9SMatthew G Knepley 
4996bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
500435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
501732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
50247c6ae99SBarry Smith   PetscFunctionReturn(0);
50347c6ae99SBarry Smith }
50447c6ae99SBarry Smith 
50547c6ae99SBarry Smith #undef __FUNCT__
506d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
507d7bf68aeSBarry Smith /*@
508d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
509d7bf68aeSBarry Smith 
510d7bf68aeSBarry Smith     Collective on DM
511d7bf68aeSBarry Smith 
512d7bf68aeSBarry Smith     Input Parameter:
513d7bf68aeSBarry Smith .   dm - the DM object to setup
514d7bf68aeSBarry Smith 
515d7bf68aeSBarry Smith     Level: developer
516d7bf68aeSBarry Smith 
517e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
518d7bf68aeSBarry Smith 
519d7bf68aeSBarry Smith @*/
5207087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
521d7bf68aeSBarry Smith {
522d7bf68aeSBarry Smith   PetscErrorCode ierr;
523d7bf68aeSBarry Smith 
524d7bf68aeSBarry Smith   PetscFunctionBegin;
525171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5268387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
527d7bf68aeSBarry Smith   if (dm->ops->setup) {
528d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
529d7bf68aeSBarry Smith   }
5308387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
531d7bf68aeSBarry Smith   PetscFunctionReturn(0);
532d7bf68aeSBarry Smith }
533d7bf68aeSBarry Smith 
534d7bf68aeSBarry Smith #undef __FUNCT__
535d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
536d7bf68aeSBarry Smith /*@
537d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
538d7bf68aeSBarry Smith 
539d7bf68aeSBarry Smith     Collective on DM
540d7bf68aeSBarry Smith 
541d7bf68aeSBarry Smith     Input Parameter:
542d7bf68aeSBarry Smith .   dm - the DM object to set options for
543d7bf68aeSBarry Smith 
544732e2eb9SMatthew G Knepley     Options Database:
545dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
546dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
547171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
548171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
549732e2eb9SMatthew G Knepley 
550d7bf68aeSBarry Smith     Level: developer
551d7bf68aeSBarry Smith 
552e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
553d7bf68aeSBarry Smith 
554d7bf68aeSBarry Smith @*/
5557087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
556d7bf68aeSBarry Smith {
557f55f929bSMatthew G Knepley   char           typeName[256] = MATAIJ;
558ca266f36SBarry Smith   PetscBool      flg;
559d7bf68aeSBarry Smith   PetscErrorCode ierr;
560d7bf68aeSBarry Smith 
561d7bf68aeSBarry Smith   PetscFunctionBegin;
562171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5633194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
5640298fd71SBarry 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);
565f9ba7244SBarry Smith   ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
566f9ba7244SBarry Smith   if (flg) {
567f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
568f9ba7244SBarry Smith   }
5698caf3d72SBarry Smith   ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
570073dac72SJed Brown   if (flg) {
571521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
572073dac72SJed Brown   }
5730298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
574f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
575f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
576f9ba7244SBarry Smith   }
577f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
578f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
57982fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
580146574abSBarry Smith   ierr = DMViewFromOptions(dm,NULL,"-dm_view");CHKERRQ(ierr);
581d7bf68aeSBarry Smith   PetscFunctionReturn(0);
582d7bf68aeSBarry Smith }
583d7bf68aeSBarry Smith 
584d7bf68aeSBarry Smith #undef __FUNCT__
58547c6ae99SBarry Smith #define __FUNCT__ "DMView"
586fc9bc008SSatish Balay /*@C
587aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
58847c6ae99SBarry Smith 
58947c6ae99SBarry Smith     Collective on DM
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith     Input Parameter:
59247c6ae99SBarry Smith +   dm - the DM object to view
59347c6ae99SBarry Smith -   v - the viewer
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith     Level: developer
59647c6ae99SBarry Smith 
597e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith @*/
6007087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
60147c6ae99SBarry Smith {
60247c6ae99SBarry Smith   PetscErrorCode ierr;
60332c0f0efSBarry Smith   PetscBool      isbinary;
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith   PetscFunctionBegin;
606171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6073014e516SBarry Smith   if (!v) {
608ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6093014e516SBarry Smith   }
61032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
61132c0f0efSBarry Smith   if (isbinary) {
61255849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
61332c0f0efSBarry Smith     char     type[256];
61432c0f0efSBarry Smith 
61532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
61632c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
61732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
61832c0f0efSBarry Smith   }
6190c010503SBarry Smith   if (dm->ops->view) {
6200c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
62147c6ae99SBarry Smith   }
62247c6ae99SBarry Smith   PetscFunctionReturn(0);
62347c6ae99SBarry Smith }
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith #undef __FUNCT__
62647c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
62747c6ae99SBarry Smith /*@
628aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
62947c6ae99SBarry Smith 
63047c6ae99SBarry Smith     Collective on DM
63147c6ae99SBarry Smith 
63247c6ae99SBarry Smith     Input Parameter:
63347c6ae99SBarry Smith .   dm - the DM object
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith     Output Parameter:
63647c6ae99SBarry Smith .   vec - the global vector
63747c6ae99SBarry Smith 
638073dac72SJed Brown     Level: beginner
63947c6ae99SBarry Smith 
640e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith @*/
6437087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
64447c6ae99SBarry Smith {
64547c6ae99SBarry Smith   PetscErrorCode ierr;
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith   PetscFunctionBegin;
648171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64947c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
65047c6ae99SBarry Smith   PetscFunctionReturn(0);
65147c6ae99SBarry Smith }
65247c6ae99SBarry Smith 
65347c6ae99SBarry Smith #undef __FUNCT__
65447c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
65547c6ae99SBarry Smith /*@
656aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
65747c6ae99SBarry Smith 
65847c6ae99SBarry Smith     Not Collective
65947c6ae99SBarry Smith 
66047c6ae99SBarry Smith     Input Parameter:
66147c6ae99SBarry Smith .   dm - the DM object
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith     Output Parameter:
66447c6ae99SBarry Smith .   vec - the local vector
66547c6ae99SBarry Smith 
666073dac72SJed Brown     Level: beginner
66747c6ae99SBarry Smith 
668e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
66947c6ae99SBarry Smith 
67047c6ae99SBarry Smith @*/
6717087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
67247c6ae99SBarry Smith {
67347c6ae99SBarry Smith   PetscErrorCode ierr;
67447c6ae99SBarry Smith 
67547c6ae99SBarry Smith   PetscFunctionBegin;
676171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
67847c6ae99SBarry Smith   PetscFunctionReturn(0);
67947c6ae99SBarry Smith }
68047c6ae99SBarry Smith 
68147c6ae99SBarry Smith #undef __FUNCT__
6821411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6831411c6eeSJed Brown /*@
6841411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6851411c6eeSJed Brown 
6861411c6eeSJed Brown    Collective on DM
6871411c6eeSJed Brown 
6881411c6eeSJed Brown    Input Parameter:
6891411c6eeSJed Brown .  dm - the DM that provides the mapping
6901411c6eeSJed Brown 
6911411c6eeSJed Brown    Output Parameter:
6921411c6eeSJed Brown .  ltog - the mapping
6931411c6eeSJed Brown 
6941411c6eeSJed Brown    Level: intermediate
6951411c6eeSJed Brown 
6961411c6eeSJed Brown    Notes:
6971411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6981411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6991411c6eeSJed Brown 
7001411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
7011411c6eeSJed Brown @*/
7027087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7031411c6eeSJed Brown {
7041411c6eeSJed Brown   PetscErrorCode ierr;
7051411c6eeSJed Brown 
7061411c6eeSJed Brown   PetscFunctionBegin;
7071411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7081411c6eeSJed Brown   PetscValidPointer(ltog,2);
7091411c6eeSJed Brown   if (!dm->ltogmap) {
71037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
71137d0c07bSMatthew G Knepley 
71237d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
71337d0c07bSMatthew G Knepley     if (section) {
71437d0c07bSMatthew G Knepley       PetscInt *ltog;
71537d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
71637d0c07bSMatthew G Knepley 
71737d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
71837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
71937d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
72037d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
72137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
72237d0c07bSMatthew G Knepley         PetscInt dof, off, c;
72337d0c07bSMatthew G Knepley 
72437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
72537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
72637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
72737d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
72837d0c07bSMatthew G Knepley           ltog[l] = off+c;
72937d0c07bSMatthew G Knepley         }
73037d0c07bSMatthew G Knepley       }
73137d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
73237d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
73337d0c07bSMatthew G Knepley     } else {
734184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
735184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7361411c6eeSJed Brown     }
73737d0c07bSMatthew G Knepley   }
7381411c6eeSJed Brown   *ltog = dm->ltogmap;
7391411c6eeSJed Brown   PetscFunctionReturn(0);
7401411c6eeSJed Brown }
7411411c6eeSJed Brown 
7421411c6eeSJed Brown #undef __FUNCT__
7431411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
7441411c6eeSJed Brown /*@
7451411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7461411c6eeSJed Brown 
7471411c6eeSJed Brown    Collective on DM
7481411c6eeSJed Brown 
7491411c6eeSJed Brown    Input Parameter:
7501411c6eeSJed Brown .  da - the distributed array that provides the mapping
7511411c6eeSJed Brown 
7521411c6eeSJed Brown    Output Parameter:
7531411c6eeSJed Brown .  ltog - the block mapping
7541411c6eeSJed Brown 
7551411c6eeSJed Brown    Level: intermediate
7561411c6eeSJed Brown 
7571411c6eeSJed Brown    Notes:
7581411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7591411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7601411c6eeSJed Brown 
7611411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7621411c6eeSJed Brown @*/
7637087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7641411c6eeSJed Brown {
7651411c6eeSJed Brown   PetscErrorCode ierr;
7661411c6eeSJed Brown 
7671411c6eeSJed Brown   PetscFunctionBegin;
7681411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7691411c6eeSJed Brown   PetscValidPointer(ltog,2);
7701411c6eeSJed Brown   if (!dm->ltogmapb) {
7711411c6eeSJed Brown     PetscInt bs;
7721411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7731411c6eeSJed Brown     if (bs > 1) {
774184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmappingblock) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
775184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7761411c6eeSJed Brown     } else {
7771411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7781411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7791411c6eeSJed Brown     }
7801411c6eeSJed Brown   }
7811411c6eeSJed Brown   *ltog = dm->ltogmapb;
7821411c6eeSJed Brown   PetscFunctionReturn(0);
7831411c6eeSJed Brown }
7841411c6eeSJed Brown 
7851411c6eeSJed Brown #undef __FUNCT__
7861411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7871411c6eeSJed Brown /*@
7881411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7891411c6eeSJed Brown 
7901411c6eeSJed Brown    Not Collective
7911411c6eeSJed Brown 
7921411c6eeSJed Brown    Input Parameter:
7931411c6eeSJed Brown .  dm - the DM with block structure
7941411c6eeSJed Brown 
7951411c6eeSJed Brown    Output Parameter:
7961411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7971411c6eeSJed Brown 
7981411c6eeSJed Brown    Level: intermediate
7991411c6eeSJed Brown 
8001411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
8011411c6eeSJed Brown @*/
8027087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
8031411c6eeSJed Brown {
8041411c6eeSJed Brown   PetscFunctionBegin;
8051411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8061411c6eeSJed Brown   PetscValidPointer(bs,2);
8071411c6eeSJed 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");
8081411c6eeSJed Brown   *bs = dm->bs;
8091411c6eeSJed Brown   PetscFunctionReturn(0);
8101411c6eeSJed Brown }
8111411c6eeSJed Brown 
8121411c6eeSJed Brown #undef __FUNCT__
813e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
81447c6ae99SBarry Smith /*@
815e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
81647c6ae99SBarry Smith 
81747c6ae99SBarry Smith     Collective on DM
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith     Input Parameter:
82047c6ae99SBarry Smith +   dm1 - the DM object
82147c6ae99SBarry Smith -   dm2 - the second, finer DM object
82247c6ae99SBarry Smith 
82347c6ae99SBarry Smith     Output Parameter:
82447c6ae99SBarry Smith +  mat - the interpolation
82547c6ae99SBarry Smith -  vec - the scaling (optional)
82647c6ae99SBarry Smith 
82747c6ae99SBarry Smith     Level: developer
82847c6ae99SBarry Smith 
82985afcc9aSBarry 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
83085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
831d52bd9f3SBarry Smith 
8321f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
833d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
83485afcc9aSBarry Smith 
83585afcc9aSBarry Smith 
836e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
83747c6ae99SBarry Smith 
83847c6ae99SBarry Smith @*/
839e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
84047c6ae99SBarry Smith {
84147c6ae99SBarry Smith   PetscErrorCode ierr;
84247c6ae99SBarry Smith 
84347c6ae99SBarry Smith   PetscFunctionBegin;
844171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
845171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
84625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
84747c6ae99SBarry Smith   PetscFunctionReturn(0);
84847c6ae99SBarry Smith }
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith #undef __FUNCT__
851e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
85247c6ae99SBarry Smith /*@
853e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith     Collective on DM
85647c6ae99SBarry Smith 
85747c6ae99SBarry Smith     Input Parameter:
85847c6ae99SBarry Smith +   dm1 - the DM object
85947c6ae99SBarry Smith -   dm2 - the second, finer DM object
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith     Output Parameter:
86247c6ae99SBarry Smith .   ctx - the injection
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith     Level: developer
86547c6ae99SBarry Smith 
86685afcc9aSBarry 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
86785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
86885afcc9aSBarry Smith 
869e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith @*/
872e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
87347c6ae99SBarry Smith {
87447c6ae99SBarry Smith   PetscErrorCode ierr;
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith   PetscFunctionBegin;
877171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
878171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
87947c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
88047c6ae99SBarry Smith   PetscFunctionReturn(0);
88147c6ae99SBarry Smith }
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith #undef __FUNCT__
884e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
885d1e2c406SBarry Smith /*@C
886e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith     Collective on DM
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith     Input Parameter:
89147c6ae99SBarry Smith +   dm - the DM object
89247c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
89347c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith     Output Parameter:
89647c6ae99SBarry Smith .   coloring - the coloring
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith     Level: developer
89947c6ae99SBarry Smith 
900e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
90147c6ae99SBarry Smith 
902aab9d709SJed Brown @*/
90319fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
90447c6ae99SBarry Smith {
90547c6ae99SBarry Smith   PetscErrorCode ierr;
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith   PetscFunctionBegin;
908171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
909ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
91047c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
91147c6ae99SBarry Smith   PetscFunctionReturn(0);
91247c6ae99SBarry Smith }
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith #undef __FUNCT__
915950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
91647c6ae99SBarry Smith /*@C
917950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
91847c6ae99SBarry Smith 
91947c6ae99SBarry Smith     Collective on DM
92047c6ae99SBarry Smith 
92147c6ae99SBarry Smith     Input Parameter:
92247c6ae99SBarry Smith +   dm - the DM object
92347c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
92494013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
92547c6ae99SBarry Smith 
92647c6ae99SBarry Smith     Output Parameter:
92747c6ae99SBarry Smith .   mat - the empty Jacobian
92847c6ae99SBarry Smith 
929073dac72SJed Brown     Level: beginner
93047c6ae99SBarry Smith 
93194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
93294013140SBarry Smith        do not need to do it yourself.
93394013140SBarry Smith 
93494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
935aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
93694013140SBarry Smith 
93794013140SBarry 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
93894013140SBarry Smith        internally by PETSc.
93994013140SBarry Smith 
94094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
941aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
94294013140SBarry Smith 
943e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
94447c6ae99SBarry Smith 
945aab9d709SJed Brown @*/
94619fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
94747c6ae99SBarry Smith {
94847c6ae99SBarry Smith   PetscErrorCode ierr;
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith   PetscFunctionBegin;
951171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
952519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
953607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
954235683edSBarry Smith #endif
955c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
956c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
957073dac72SJed Brown   if (dm->mattype) {
95825296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
959073dac72SJed Brown   } else {
96025296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
961c7b7c8a4SJed Brown   }
96247c6ae99SBarry Smith   PetscFunctionReturn(0);
96347c6ae99SBarry Smith }
96447c6ae99SBarry Smith 
96547c6ae99SBarry Smith #undef __FUNCT__
966732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
967732e2eb9SMatthew G Knepley /*@
968950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
969732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
970732e2eb9SMatthew G Knepley 
971732e2eb9SMatthew G Knepley   Logically Collective on DMDA
972732e2eb9SMatthew G Knepley 
973732e2eb9SMatthew G Knepley   Input Parameter:
974732e2eb9SMatthew G Knepley + dm - the DM
975732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
976732e2eb9SMatthew G Knepley 
977732e2eb9SMatthew G Knepley   Level: developer
978950540a4SJed Brown .seealso DMCreateMatrix()
979732e2eb9SMatthew G Knepley @*/
980732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
981732e2eb9SMatthew G Knepley {
982732e2eb9SMatthew G Knepley   PetscFunctionBegin;
983732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
984732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
985732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
986732e2eb9SMatthew G Knepley }
987732e2eb9SMatthew G Knepley 
988732e2eb9SMatthew G Knepley #undef __FUNCT__
989a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
990a89ea682SMatthew G Knepley /*@C
991aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
992a89ea682SMatthew G Knepley 
993a89ea682SMatthew G Knepley   Not Collective
994a89ea682SMatthew G Knepley 
995a89ea682SMatthew G Knepley   Input Parameters:
996a89ea682SMatthew G Knepley + dm - the DM object
997aa1993deSMatthew G Knepley . count - The minium size
998aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
999a89ea682SMatthew G Knepley 
1000a89ea682SMatthew G Knepley   Output Parameter:
1001a89ea682SMatthew G Knepley . array - the work array
1002a89ea682SMatthew G Knepley 
1003a89ea682SMatthew G Knepley   Level: developer
1004a89ea682SMatthew G Knepley 
1005a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1006a89ea682SMatthew G Knepley @*/
1007aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1008a89ea682SMatthew G Knepley {
1009a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1010aa1993deSMatthew G Knepley   DMWorkLink     link;
1011aa1993deSMatthew G Knepley   size_t         size;
1012a89ea682SMatthew G Knepley 
1013a89ea682SMatthew G Knepley   PetscFunctionBegin;
1014a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1015aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1016aa1993deSMatthew G Knepley   if (dm->workin) {
1017aa1993deSMatthew G Knepley     link       = dm->workin;
1018aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1019aa1993deSMatthew G Knepley   } else {
1020aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
1021a89ea682SMatthew G Knepley   }
1022aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1023aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1024aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1025aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1026aa1993deSMatthew G Knepley     link->bytes = size*count;
1027aa1993deSMatthew G Knepley   }
1028aa1993deSMatthew G Knepley   link->next   = dm->workout;
1029aa1993deSMatthew G Knepley   dm->workout  = link;
1030aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1031a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1032a89ea682SMatthew G Knepley }
1033a89ea682SMatthew G Knepley 
1034aa1993deSMatthew G Knepley #undef __FUNCT__
1035aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1036aa1993deSMatthew G Knepley /*@C
1037aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1038aa1993deSMatthew G Knepley 
1039aa1993deSMatthew G Knepley   Not Collective
1040aa1993deSMatthew G Knepley 
1041aa1993deSMatthew G Knepley   Input Parameters:
1042aa1993deSMatthew G Knepley + dm - the DM object
1043aa1993deSMatthew G Knepley . count - The minium size
1044aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1045aa1993deSMatthew G Knepley 
1046aa1993deSMatthew G Knepley   Output Parameter:
1047aa1993deSMatthew G Knepley . array - the work array
1048aa1993deSMatthew G Knepley 
1049aa1993deSMatthew G Knepley   Level: developer
1050aa1993deSMatthew G Knepley 
1051aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1052aa1993deSMatthew G Knepley @*/
1053aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1054aa1993deSMatthew G Knepley {
1055aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1056aa1993deSMatthew G Knepley 
1057aa1993deSMatthew G Knepley   PetscFunctionBegin;
1058aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1059aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1060aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1061aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1062aa1993deSMatthew G Knepley       *p           = link->next;
1063aa1993deSMatthew G Knepley       link->next   = dm->workin;
1064aa1993deSMatthew G Knepley       dm->workin   = link;
10650298fd71SBarry Smith       *(void**)mem = NULL;
1066aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1067aa1993deSMatthew G Knepley     }
1068aa1993deSMatthew G Knepley   }
1069aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1070aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1071aa1993deSMatthew G Knepley }
1072e7c4fc90SDmitry Karpeev 
1073e7c4fc90SDmitry Karpeev #undef __FUNCT__
1074435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1075435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1076435a35e8SMatthew G Knepley {
1077435a35e8SMatthew G Knepley   PetscFunctionBegin;
1078435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
107982f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1080435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1081435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1082435a35e8SMatthew G Knepley }
1083435a35e8SMatthew G Knepley 
1084435a35e8SMatthew G Knepley #undef __FUNCT__
10854d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10864f3b5142SJed Brown /*@C
10874d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10884d343eeaSMatthew G Knepley 
10894d343eeaSMatthew G Knepley   Not collective
10904d343eeaSMatthew G Knepley 
10914d343eeaSMatthew G Knepley   Input Parameter:
10924d343eeaSMatthew G Knepley . dm - the DM object
10934d343eeaSMatthew G Knepley 
10944d343eeaSMatthew G Knepley   Output Parameters:
10950298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10960298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10970298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10984d343eeaSMatthew G Knepley 
10994d343eeaSMatthew G Knepley   Level: intermediate
11004d343eeaSMatthew G Knepley 
110121c9b008SJed Brown   Notes:
110221c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
110321c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
110421c9b008SJed Brown   PetscFree().
110521c9b008SJed Brown 
11064d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11074d343eeaSMatthew G Knepley @*/
110837d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11094d343eeaSMatthew G Knepley {
111037d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11114d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11124d343eeaSMatthew G Knepley 
11134d343eeaSMatthew G Knepley   PetscFunctionBegin;
11144d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
111569ca1f37SDmitry Karpeev   if (numFields) {
111669ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
111769ca1f37SDmitry Karpeev     *numFields = 0;
111869ca1f37SDmitry Karpeev   }
111937d0c07bSMatthew G Knepley   if (fieldNames) {
112037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11210298fd71SBarry Smith     *fieldNames = NULL;
112269ca1f37SDmitry Karpeev   }
112369ca1f37SDmitry Karpeev   if (fields) {
112469ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11250298fd71SBarry Smith     *fields = NULL;
112669ca1f37SDmitry Karpeev   }
112737d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
112837d0c07bSMatthew G Knepley   if (section) {
112937d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
113037d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
113137d0c07bSMatthew G Knepley 
113237d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
113337d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
113437d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt*,&fieldIndices);CHKERRQ(ierr);
113537d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
113637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
113737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113837d0c07bSMatthew G Knepley     }
113937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
114037d0c07bSMatthew G Knepley       PetscInt gdof;
114137d0c07bSMatthew G Knepley 
114237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114337d0c07bSMatthew G Knepley       if (gdof > 0) {
114437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
114637d0c07bSMatthew G Knepley 
114737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
114837d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
114937d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
115037d0c07bSMatthew G Knepley         }
115137d0c07bSMatthew G Knepley       }
115237d0c07bSMatthew G Knepley     }
115337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
115437d0c07bSMatthew G Knepley       ierr          = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
115537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
115637d0c07bSMatthew G Knepley     }
115737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
115837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
115937d0c07bSMatthew G Knepley 
116037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
116137d0c07bSMatthew G Knepley       if (gdof > 0) {
116237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
116337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
116437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
116537d0c07bSMatthew G Knepley 
116637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
116737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
116837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
116937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
117037d0c07bSMatthew G Knepley           }
117137d0c07bSMatthew G Knepley         }
117237d0c07bSMatthew G Knepley       }
117337d0c07bSMatthew G Knepley     }
11748865f1eaSKarl Rupp     if (numFields) *numFields = nF;
117537d0c07bSMatthew G Knepley     if (fieldNames) {
117637d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char*), fieldNames);CHKERRQ(ierr);
117737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
117837d0c07bSMatthew G Knepley         const char *fieldName;
117937d0c07bSMatthew G Knepley 
118037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
118137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
118237d0c07bSMatthew G Knepley       }
118337d0c07bSMatthew G Knepley     }
118437d0c07bSMatthew G Knepley     if (fields) {
118537d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
118637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
118782f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
118837d0c07bSMatthew G Knepley       }
118937d0c07bSMatthew G Knepley     }
119037d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11918865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11928865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
119369ca1f37SDmitry Karpeev   }
11944d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11954d343eeaSMatthew G Knepley }
11964d343eeaSMatthew G Knepley 
119716621825SDmitry Karpeev 
119816621825SDmitry Karpeev #undef __FUNCT__
119916621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
120016621825SDmitry Karpeev /*@C
120116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
120216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
120316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1204e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1205e7c4fc90SDmitry Karpeev 
1206e7c4fc90SDmitry Karpeev   Not collective
1207e7c4fc90SDmitry Karpeev 
1208e7c4fc90SDmitry Karpeev   Input Parameter:
1209e7c4fc90SDmitry Karpeev . dm - the DM object
1210e7c4fc90SDmitry Karpeev 
1211e7c4fc90SDmitry Karpeev   Output Parameters:
12120298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
12130298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
12140298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12150298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1216e7c4fc90SDmitry Karpeev 
1217e7c4fc90SDmitry Karpeev   Level: intermediate
1218e7c4fc90SDmitry Karpeev 
1219e7c4fc90SDmitry Karpeev   Notes:
1220e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1221e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1222e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1223e7c4fc90SDmitry Karpeev 
1224e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1225e7c4fc90SDmitry Karpeev @*/
122616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1227e7c4fc90SDmitry Karpeev {
1228e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1229e7c4fc90SDmitry Karpeev 
1230e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1231e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12328865f1eaSKarl Rupp   if (len) {
12338865f1eaSKarl Rupp     PetscValidPointer(len,2);
12348865f1eaSKarl Rupp     *len = 0;
12358865f1eaSKarl Rupp   }
12368865f1eaSKarl Rupp   if (namelist) {
12378865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12388865f1eaSKarl Rupp     *namelist = 0;
12398865f1eaSKarl Rupp   }
12408865f1eaSKarl Rupp   if (islist) {
12418865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12428865f1eaSKarl Rupp     *islist = 0;
12438865f1eaSKarl Rupp   }
12448865f1eaSKarl Rupp   if (dmlist) {
12458865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12468865f1eaSKarl Rupp     *dmlist = 0;
12478865f1eaSKarl Rupp   }
1248f3f0edfdSDmitry Karpeev   /*
1249f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1250f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1251f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1252f3f0edfdSDmitry Karpeev    */
1253ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
125416621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1255435a35e8SMatthew G Knepley     PetscSection section;
1256435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1257435a35e8SMatthew G Knepley 
1258435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1259435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1260435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1261435a35e8SMatthew G Knepley       *len = numFields;
1262435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1263435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1264435a35e8SMatthew G Knepley         const char *fieldName;
1265435a35e8SMatthew G Knepley 
1266435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1267435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1268435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1269435a35e8SMatthew G Knepley       }
1270435a35e8SMatthew G Knepley     } else {
127169ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1272e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12730298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1274e7c4fc90SDmitry Karpeev     }
12758865f1eaSKarl Rupp   } else {
127616621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
127716621825SDmitry Karpeev   }
127816621825SDmitry Karpeev   PetscFunctionReturn(0);
127916621825SDmitry Karpeev }
128016621825SDmitry Karpeev 
128116621825SDmitry Karpeev #undef __FUNCT__
1282435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1283435a35e8SMatthew G Knepley /*@C
1284435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1285435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1286435a35e8SMatthew G Knepley 
1287435a35e8SMatthew G Knepley   Not collective
1288435a35e8SMatthew G Knepley 
1289435a35e8SMatthew G Knepley   Input Parameters:
1290435a35e8SMatthew G Knepley + dm - the DM object
1291435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12920298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1293435a35e8SMatthew G Knepley 
1294435a35e8SMatthew G Knepley   Output Parameters:
1295435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1296435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1297435a35e8SMatthew G Knepley 
1298435a35e8SMatthew G Knepley   Level: intermediate
1299435a35e8SMatthew G Knepley 
1300435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1301435a35e8SMatthew G Knepley @*/
1302435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1303435a35e8SMatthew G Knepley {
1304435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1305435a35e8SMatthew G Knepley 
1306435a35e8SMatthew G Knepley   PetscFunctionBegin;
1307435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1308435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
13098865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
13108865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1311435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1312435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
131382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1314435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1315435a35e8SMatthew G Knepley }
1316435a35e8SMatthew G Knepley 
131716621825SDmitry Karpeev 
131816621825SDmitry Karpeev #undef __FUNCT__
131916621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
132016621825SDmitry Karpeev /*@C
13218d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13228d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13238d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13248d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13258d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
132616621825SDmitry Karpeev 
132716621825SDmitry Karpeev   Not collective
132816621825SDmitry Karpeev 
132916621825SDmitry Karpeev   Input Parameter:
133016621825SDmitry Karpeev . dm - the DM object
133116621825SDmitry Karpeev 
133216621825SDmitry Karpeev   Output Parameters:
13330298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13340298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13350298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13360298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13370298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
133816621825SDmitry Karpeev 
133916621825SDmitry Karpeev   Level: intermediate
134016621825SDmitry Karpeev 
134116621825SDmitry Karpeev   Notes:
134216621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
134316621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
134416621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
134516621825SDmitry Karpeev 
13468d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
134716621825SDmitry Karpeev @*/
13488d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
134916621825SDmitry Karpeev {
135016621825SDmitry Karpeev   PetscErrorCode      ierr;
1351be081cd6SPeter Brune   DMSubDomainHookLink link;
1352be081cd6SPeter Brune   PetscInt            i,l;
135316621825SDmitry Karpeev 
135416621825SDmitry Karpeev   PetscFunctionBegin;
135516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
135614a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13570298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13580298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13590298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13600298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1361f3f0edfdSDmitry Karpeev   /*
1362f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1363f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1364f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1365f3f0edfdSDmitry Karpeev    */
1366ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
136716621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1368be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
136914a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
137014a18fd3SPeter Brune     if (dmlist) {
1371be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1372be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1373be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1374be081cd6SPeter Brune         }
1375d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1376e7c4fc90SDmitry Karpeev       }
137714a18fd3SPeter Brune     }
137814a18fd3SPeter Brune     if (len) *len = l;
137914a18fd3SPeter Brune   }
1380e30e807fSPeter Brune   PetscFunctionReturn(0);
1381e30e807fSPeter Brune }
1382e30e807fSPeter Brune 
1383e30e807fSPeter Brune 
1384e30e807fSPeter Brune #undef __FUNCT__
1385e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1386e30e807fSPeter Brune /*@C
1387e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1388e30e807fSPeter Brune 
1389e30e807fSPeter Brune   Not collective
1390e30e807fSPeter Brune 
1391e30e807fSPeter Brune   Input Parameters:
1392e30e807fSPeter Brune + dm - the DM object
1393e30e807fSPeter Brune . n  - the number of subdomain scatters
1394e30e807fSPeter Brune - subdms - the local subdomains
1395e30e807fSPeter Brune 
1396e30e807fSPeter Brune   Output Parameters:
1397e30e807fSPeter Brune + n     - the number of scatters returned
1398e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1399e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1400e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1401e30e807fSPeter Brune 
1402e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1403e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1404e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1405e30e807fSPeter Brune   solution and residual data.
1406e30e807fSPeter Brune 
1407e30e807fSPeter Brune   Level: developer
1408e30e807fSPeter Brune 
1409e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1410e30e807fSPeter Brune @*/
1411e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1412e30e807fSPeter Brune {
1413e30e807fSPeter Brune   PetscErrorCode ierr;
1414e30e807fSPeter Brune 
1415e30e807fSPeter Brune   PetscFunctionBegin;
1416e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1417e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1418e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1419e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
142082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1421e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1422e7c4fc90SDmitry Karpeev }
1423e7c4fc90SDmitry Karpeev 
1424731c8d9eSDmitry Karpeev #undef __FUNCT__
142547c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
142647c6ae99SBarry Smith /*@
142747c6ae99SBarry Smith   DMRefine - Refines a DM object
142847c6ae99SBarry Smith 
142947c6ae99SBarry Smith   Collective on DM
143047c6ae99SBarry Smith 
143147c6ae99SBarry Smith   Input Parameter:
143247c6ae99SBarry Smith + dm   - the DM object
143391d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
143447c6ae99SBarry Smith 
143547c6ae99SBarry Smith   Output Parameter:
14360298fd71SBarry Smith . dmf - the refined DM, or NULL
1437ae0a1c52SMatthew G Knepley 
14380298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
143947c6ae99SBarry Smith 
144047c6ae99SBarry Smith   Level: developer
144147c6ae99SBarry Smith 
1442e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
144347c6ae99SBarry Smith @*/
14447087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
144547c6ae99SBarry Smith {
144647c6ae99SBarry Smith   PetscErrorCode   ierr;
1447c833c3b5SJed Brown   DMRefineHookLink link;
144847c6ae99SBarry Smith 
144947c6ae99SBarry Smith   PetscFunctionBegin;
1450732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
145147c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14524057135bSMatthew G Knepley   if (*dmf) {
145343842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14548865f1eaSKarl Rupp 
14558cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14568865f1eaSKarl Rupp 
1457644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14580598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1459656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14608865f1eaSKarl Rupp 
1461e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1462c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14638865f1eaSKarl Rupp       if (link->refinehook) {
14648865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14658865f1eaSKarl Rupp       }
1466c833c3b5SJed Brown     }
1467c833c3b5SJed Brown   }
1468c833c3b5SJed Brown   PetscFunctionReturn(0);
1469c833c3b5SJed Brown }
1470c833c3b5SJed Brown 
1471c833c3b5SJed Brown #undef __FUNCT__
1472c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1473bb9467b5SJed Brown /*@C
1474c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1475c833c3b5SJed Brown 
1476c833c3b5SJed Brown    Logically Collective
1477c833c3b5SJed Brown 
1478c833c3b5SJed Brown    Input Arguments:
1479c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1480c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1481c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14820298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1483c833c3b5SJed Brown 
1484c833c3b5SJed Brown    Calling sequence of refinehook:
1485c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1486c833c3b5SJed Brown 
1487c833c3b5SJed Brown +  coarse - coarse level DM
1488c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1489c833c3b5SJed Brown -  ctx - optional user-defined function context
1490c833c3b5SJed Brown 
1491c833c3b5SJed Brown    Calling sequence for interphook:
1492c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1493c833c3b5SJed Brown 
1494c833c3b5SJed Brown +  coarse - coarse level DM
1495c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1496c833c3b5SJed Brown .  fine - fine level DM to update
1497c833c3b5SJed Brown -  ctx - optional user-defined function context
1498c833c3b5SJed Brown 
1499c833c3b5SJed Brown    Level: advanced
1500c833c3b5SJed Brown 
1501c833c3b5SJed Brown    Notes:
1502c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1503c833c3b5SJed Brown 
1504c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1505c833c3b5SJed Brown 
1506bb9467b5SJed Brown    This function is currently not available from Fortran.
1507bb9467b5SJed Brown 
1508c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1509c833c3b5SJed Brown @*/
1510c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1511c833c3b5SJed Brown {
1512c833c3b5SJed Brown   PetscErrorCode   ierr;
1513c833c3b5SJed Brown   DMRefineHookLink link,*p;
1514c833c3b5SJed Brown 
1515c833c3b5SJed Brown   PetscFunctionBegin;
1516c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1517c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1518c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1519c833c3b5SJed Brown   link->refinehook = refinehook;
1520c833c3b5SJed Brown   link->interphook = interphook;
1521c833c3b5SJed Brown   link->ctx        = ctx;
15220298fd71SBarry Smith   link->next       = NULL;
1523c833c3b5SJed Brown   *p               = link;
1524c833c3b5SJed Brown   PetscFunctionReturn(0);
1525c833c3b5SJed Brown }
1526c833c3b5SJed Brown 
1527c833c3b5SJed Brown #undef __FUNCT__
1528c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1529c833c3b5SJed Brown /*@
1530c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1531c833c3b5SJed Brown 
1532c833c3b5SJed Brown    Collective if any hooks are
1533c833c3b5SJed Brown 
1534c833c3b5SJed Brown    Input Arguments:
1535c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1536c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1537c833c3b5SJed Brown -  fine - finer DM to update
1538c833c3b5SJed Brown 
1539c833c3b5SJed Brown    Level: developer
1540c833c3b5SJed Brown 
1541c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1542c833c3b5SJed Brown @*/
1543c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1544c833c3b5SJed Brown {
1545c833c3b5SJed Brown   PetscErrorCode   ierr;
1546c833c3b5SJed Brown   DMRefineHookLink link;
1547c833c3b5SJed Brown 
1548c833c3b5SJed Brown   PetscFunctionBegin;
1549c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15508865f1eaSKarl Rupp     if (link->interphook) {
15518865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15528865f1eaSKarl Rupp     }
15534057135bSMatthew G Knepley   }
155447c6ae99SBarry Smith   PetscFunctionReturn(0);
155547c6ae99SBarry Smith }
155647c6ae99SBarry Smith 
155747c6ae99SBarry Smith #undef __FUNCT__
1558eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1559eb3f98d2SBarry Smith /*@
1560eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1561eb3f98d2SBarry Smith 
1562eb3f98d2SBarry Smith     Not Collective
1563eb3f98d2SBarry Smith 
1564eb3f98d2SBarry Smith     Input Parameter:
1565eb3f98d2SBarry Smith .   dm - the DM object
1566eb3f98d2SBarry Smith 
1567eb3f98d2SBarry Smith     Output Parameter:
1568eb3f98d2SBarry Smith .   level - number of refinements
1569eb3f98d2SBarry Smith 
1570eb3f98d2SBarry Smith     Level: developer
1571eb3f98d2SBarry Smith 
15726a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1573eb3f98d2SBarry Smith 
1574eb3f98d2SBarry Smith @*/
1575eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1576eb3f98d2SBarry Smith {
1577eb3f98d2SBarry Smith   PetscFunctionBegin;
1578eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1579eb3f98d2SBarry Smith   *level = dm->levelup;
1580eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1581eb3f98d2SBarry Smith }
1582eb3f98d2SBarry Smith 
1583eb3f98d2SBarry Smith #undef __FUNCT__
1584baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1585bb9467b5SJed Brown /*@C
1586baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1587baf369e7SPeter Brune 
1588baf369e7SPeter Brune    Logically Collective
1589baf369e7SPeter Brune 
1590baf369e7SPeter Brune    Input Arguments:
1591baf369e7SPeter Brune +  dm - the DM
1592baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1593baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15940298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1595baf369e7SPeter Brune 
1596baf369e7SPeter Brune    Calling sequence for beginhook:
1597baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1598baf369e7SPeter Brune 
1599baf369e7SPeter Brune +  dm - global DM
1600baf369e7SPeter Brune .  g - global vector
1601baf369e7SPeter Brune .  mode - mode
1602baf369e7SPeter Brune .  l - local vector
1603baf369e7SPeter Brune -  ctx - optional user-defined function context
1604baf369e7SPeter Brune 
1605baf369e7SPeter Brune 
1606baf369e7SPeter Brune    Calling sequence for endhook:
1607ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1608baf369e7SPeter Brune 
1609baf369e7SPeter Brune +  global - global DM
1610baf369e7SPeter Brune -  ctx - optional user-defined function context
1611baf369e7SPeter Brune 
1612baf369e7SPeter Brune    Level: advanced
1613baf369e7SPeter Brune 
1614baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1615baf369e7SPeter Brune @*/
1616baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1617baf369e7SPeter Brune {
1618baf369e7SPeter Brune   PetscErrorCode          ierr;
1619baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1620baf369e7SPeter Brune 
1621baf369e7SPeter Brune   PetscFunctionBegin;
1622baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1623baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1624baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1625baf369e7SPeter Brune   link->beginhook = beginhook;
1626baf369e7SPeter Brune   link->endhook   = endhook;
1627baf369e7SPeter Brune   link->ctx       = ctx;
16280298fd71SBarry Smith   link->next      = NULL;
1629baf369e7SPeter Brune   *p              = link;
1630baf369e7SPeter Brune   PetscFunctionReturn(0);
1631baf369e7SPeter Brune }
1632baf369e7SPeter Brune 
1633baf369e7SPeter Brune #undef __FUNCT__
163447c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
163547c6ae99SBarry Smith /*@
163647c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
163747c6ae99SBarry Smith 
163847c6ae99SBarry Smith     Neighbor-wise Collective on DM
163947c6ae99SBarry Smith 
164047c6ae99SBarry Smith     Input Parameters:
164147c6ae99SBarry Smith +   dm - the DM object
164247c6ae99SBarry Smith .   g - the global vector
164347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
164447c6ae99SBarry Smith -   l - the local vector
164547c6ae99SBarry Smith 
164647c6ae99SBarry Smith 
164747c6ae99SBarry Smith     Level: beginner
164847c6ae99SBarry Smith 
1649e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
165047c6ae99SBarry Smith 
165147c6ae99SBarry Smith @*/
16527087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
165347c6ae99SBarry Smith {
16547128ae9fSMatthew G Knepley   PetscSF                 sf;
165547c6ae99SBarry Smith   PetscErrorCode          ierr;
1656baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
165747c6ae99SBarry Smith 
165847c6ae99SBarry Smith   PetscFunctionBegin;
1659171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1660baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16618865f1eaSKarl Rupp     if (link->beginhook) {
16628865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16638865f1eaSKarl Rupp     }
1664baf369e7SPeter Brune   }
16657128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16667128ae9fSMatthew G Knepley   if (sf) {
16677128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16687128ae9fSMatthew G Knepley 
166982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16707128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16717128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16727128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16737128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16747128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16757128ae9fSMatthew G Knepley   } else {
1676843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16777128ae9fSMatthew G Knepley   }
167847c6ae99SBarry Smith   PetscFunctionReturn(0);
167947c6ae99SBarry Smith }
168047c6ae99SBarry Smith 
168147c6ae99SBarry Smith #undef __FUNCT__
168247c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
168347c6ae99SBarry Smith /*@
168447c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
168547c6ae99SBarry Smith 
168647c6ae99SBarry Smith     Neighbor-wise Collective on DM
168747c6ae99SBarry Smith 
168847c6ae99SBarry Smith     Input Parameters:
168947c6ae99SBarry Smith +   dm - the DM object
169047c6ae99SBarry Smith .   g - the global vector
169147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
169247c6ae99SBarry Smith -   l - the local vector
169347c6ae99SBarry Smith 
169447c6ae99SBarry Smith 
169547c6ae99SBarry Smith     Level: beginner
169647c6ae99SBarry Smith 
1697e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
169847c6ae99SBarry Smith 
169947c6ae99SBarry Smith @*/
17007087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
170147c6ae99SBarry Smith {
17027128ae9fSMatthew G Knepley   PetscSF                 sf;
170347c6ae99SBarry Smith   PetscErrorCode          ierr;
170461a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1705baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
170647c6ae99SBarry Smith 
170747c6ae99SBarry Smith   PetscFunctionBegin;
1708171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17097128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17107128ae9fSMatthew G Knepley   if (sf) {
171182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17127128ae9fSMatthew G Knepley 
17137128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17147128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17157128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17167128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17177128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17187128ae9fSMatthew G Knepley   } else {
1719843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17207128ae9fSMatthew G Knepley   }
1721baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1722baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1723baf369e7SPeter Brune   }
172447c6ae99SBarry Smith   PetscFunctionReturn(0);
172547c6ae99SBarry Smith }
172647c6ae99SBarry Smith 
172747c6ae99SBarry Smith #undef __FUNCT__
17289a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
172947c6ae99SBarry Smith /*@
17309a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
17319a42bb27SBarry Smith 
17329a42bb27SBarry Smith     Neighbor-wise Collective on DM
17339a42bb27SBarry Smith 
17349a42bb27SBarry Smith     Input Parameters:
17359a42bb27SBarry Smith +   dm - the DM object
1736f6813fd5SJed Brown .   l - the local vector
17379a42bb27SBarry 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
17389a42bb27SBarry Smith            base point.
1739f6813fd5SJed Brown - - the global vector
17409a42bb27SBarry Smith 
17419a42bb27SBarry 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
17429a42bb27SBarry 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
17439a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17449a42bb27SBarry Smith 
17459a42bb27SBarry Smith     Level: beginner
17469a42bb27SBarry Smith 
1747e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17489a42bb27SBarry Smith 
17499a42bb27SBarry Smith @*/
17507087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17519a42bb27SBarry Smith {
17527128ae9fSMatthew G Knepley   PetscSF        sf;
17539a42bb27SBarry Smith   PetscErrorCode ierr;
17549a42bb27SBarry Smith 
17559a42bb27SBarry Smith   PetscFunctionBegin;
1756171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17577128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17587128ae9fSMatthew G Knepley   if (sf) {
17597128ae9fSMatthew G Knepley     MPI_Op      op;
17607128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17617128ae9fSMatthew G Knepley 
17627128ae9fSMatthew G Knepley     switch (mode) {
17637128ae9fSMatthew G Knepley     case INSERT_VALUES:
17647128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17658bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17667128ae9fSMatthew G Knepley     case ADD_VALUES:
17677128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17687128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17697128ae9fSMatthew G Knepley     default:
177082f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17717128ae9fSMatthew G Knepley     }
17727128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17737128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17747128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17757128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17767128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17777128ae9fSMatthew G Knepley   } else {
1778843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17797128ae9fSMatthew G Knepley   }
17809a42bb27SBarry Smith   PetscFunctionReturn(0);
17819a42bb27SBarry Smith }
17829a42bb27SBarry Smith 
17839a42bb27SBarry Smith #undef __FUNCT__
17849a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17859a42bb27SBarry Smith /*@
17869a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
178747c6ae99SBarry Smith 
178847c6ae99SBarry Smith     Neighbor-wise Collective on DM
178947c6ae99SBarry Smith 
179047c6ae99SBarry Smith     Input Parameters:
179147c6ae99SBarry Smith +   dm - the DM object
1792f6813fd5SJed Brown .   l - the local vector
179347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1794f6813fd5SJed Brown -   g - the global vector
179547c6ae99SBarry Smith 
179647c6ae99SBarry Smith 
179747c6ae99SBarry Smith     Level: beginner
179847c6ae99SBarry Smith 
1799e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
180047c6ae99SBarry Smith 
180147c6ae99SBarry Smith @*/
18027087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
180347c6ae99SBarry Smith {
18047128ae9fSMatthew G Knepley   PetscSF        sf;
180547c6ae99SBarry Smith   PetscErrorCode ierr;
180647c6ae99SBarry Smith 
180747c6ae99SBarry Smith   PetscFunctionBegin;
1808171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18097128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18107128ae9fSMatthew G Knepley   if (sf) {
18117128ae9fSMatthew G Knepley     MPI_Op      op;
18127128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
18137128ae9fSMatthew G Knepley 
18147128ae9fSMatthew G Knepley     switch (mode) {
18157128ae9fSMatthew G Knepley     case INSERT_VALUES:
18167128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18178bfbc91cSJed Brown       op = MPIU_REPLACE; break;
18187128ae9fSMatthew G Knepley     case ADD_VALUES:
18197128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18207128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18217128ae9fSMatthew G Knepley     default:
182282f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18237128ae9fSMatthew G Knepley     }
18247128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18257128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18267128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18277128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18287128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18297128ae9fSMatthew G Knepley   } else {
1830843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18317128ae9fSMatthew G Knepley   }
183247c6ae99SBarry Smith   PetscFunctionReturn(0);
183347c6ae99SBarry Smith }
183447c6ae99SBarry Smith 
183547c6ae99SBarry Smith #undef __FUNCT__
183647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
183747c6ae99SBarry Smith /*@
183847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
183947c6ae99SBarry Smith 
184047c6ae99SBarry Smith     Collective on DM
184147c6ae99SBarry Smith 
184247c6ae99SBarry Smith     Input Parameter:
184347c6ae99SBarry Smith +   dm - the DM object
184491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
184547c6ae99SBarry Smith 
184647c6ae99SBarry Smith     Output Parameter:
184747c6ae99SBarry Smith .   dmc - the coarsened DM
184847c6ae99SBarry Smith 
184947c6ae99SBarry Smith     Level: developer
185047c6ae99SBarry Smith 
1851e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
185247c6ae99SBarry Smith 
185347c6ae99SBarry Smith @*/
18547087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
185547c6ae99SBarry Smith {
185647c6ae99SBarry Smith   PetscErrorCode    ierr;
1857b17ce1afSJed Brown   DMCoarsenHookLink link;
185847c6ae99SBarry Smith 
185947c6ae99SBarry Smith   PetscFunctionBegin;
1860171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
186147c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
186243842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
18638cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1864644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
18650598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1866656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1867e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1868b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1869b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1870b17ce1afSJed Brown   }
1871b17ce1afSJed Brown   PetscFunctionReturn(0);
1872b17ce1afSJed Brown }
1873b17ce1afSJed Brown 
1874b17ce1afSJed Brown #undef __FUNCT__
1875b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1876bb9467b5SJed Brown /*@C
1877b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1878b17ce1afSJed Brown 
1879b17ce1afSJed Brown    Logically Collective
1880b17ce1afSJed Brown 
1881b17ce1afSJed Brown    Input Arguments:
1882b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1883b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1884b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
18850298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1886b17ce1afSJed Brown 
1887b17ce1afSJed Brown    Calling sequence of coarsenhook:
1888b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1889b17ce1afSJed Brown 
1890b17ce1afSJed Brown +  fine - fine level DM
1891b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1892b17ce1afSJed Brown -  ctx - optional user-defined function context
1893b17ce1afSJed Brown 
1894b17ce1afSJed Brown    Calling sequence for restricthook:
1895c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1896b17ce1afSJed Brown 
1897b17ce1afSJed Brown +  fine - fine level DM
1898b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1899c833c3b5SJed Brown .  rscale - scaling vector for restriction
1900c833c3b5SJed Brown .  inject - matrix restricting by injection
1901b17ce1afSJed Brown .  coarse - coarse level DM to update
1902b17ce1afSJed Brown -  ctx - optional user-defined function context
1903b17ce1afSJed Brown 
1904b17ce1afSJed Brown    Level: advanced
1905b17ce1afSJed Brown 
1906b17ce1afSJed Brown    Notes:
1907b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1908b17ce1afSJed Brown 
1909b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1910b17ce1afSJed Brown 
1911b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1912b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1913b17ce1afSJed Brown 
1914bb9467b5SJed Brown    This function is currently not available from Fortran.
1915bb9467b5SJed Brown 
1916c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1917b17ce1afSJed Brown @*/
1918b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1919b17ce1afSJed Brown {
1920b17ce1afSJed Brown   PetscErrorCode    ierr;
1921b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1922b17ce1afSJed Brown 
1923b17ce1afSJed Brown   PetscFunctionBegin;
1924b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
19256bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1926b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1927b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
1928b17ce1afSJed Brown   link->restricthook = restricthook;
1929b17ce1afSJed Brown   link->ctx          = ctx;
19300298fd71SBarry Smith   link->next         = NULL;
1931b17ce1afSJed Brown   *p                 = link;
1932b17ce1afSJed Brown   PetscFunctionReturn(0);
1933b17ce1afSJed Brown }
1934b17ce1afSJed Brown 
1935b17ce1afSJed Brown #undef __FUNCT__
1936b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1937b17ce1afSJed Brown /*@
1938b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1939b17ce1afSJed Brown 
1940b17ce1afSJed Brown    Collective if any hooks are
1941b17ce1afSJed Brown 
1942b17ce1afSJed Brown    Input Arguments:
1943b17ce1afSJed Brown +  fine - finer DM to use as a base
1944b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1945b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1946b17ce1afSJed Brown -  coarse - coarer DM to update
1947b17ce1afSJed Brown 
1948b17ce1afSJed Brown    Level: developer
1949b17ce1afSJed Brown 
1950b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1951b17ce1afSJed Brown @*/
1952b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1953b17ce1afSJed Brown {
1954b17ce1afSJed Brown   PetscErrorCode    ierr;
1955b17ce1afSJed Brown   DMCoarsenHookLink link;
1956b17ce1afSJed Brown 
1957b17ce1afSJed Brown   PetscFunctionBegin;
1958b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
19598865f1eaSKarl Rupp     if (link->restricthook) {
19608865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
19618865f1eaSKarl Rupp     }
1962b17ce1afSJed Brown   }
196347c6ae99SBarry Smith   PetscFunctionReturn(0);
196447c6ae99SBarry Smith }
196547c6ae99SBarry Smith 
196647c6ae99SBarry Smith #undef __FUNCT__
1967be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
1968bb9467b5SJed Brown /*@C
1969be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
19705dbd56e3SPeter Brune 
19715dbd56e3SPeter Brune    Logically Collective
19725dbd56e3SPeter Brune 
19735dbd56e3SPeter Brune    Input Arguments:
19745dbd56e3SPeter Brune +  global - global DM
1975ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
19765dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
19770298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19785dbd56e3SPeter Brune 
1979ec4806b8SPeter Brune 
1980ec4806b8SPeter Brune    Calling sequence for ddhook:
1981ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
1982ec4806b8SPeter Brune 
1983ec4806b8SPeter Brune +  global - global DM
1984ec4806b8SPeter Brune .  block  - block DM
1985ec4806b8SPeter Brune -  ctx - optional user-defined function context
1986ec4806b8SPeter Brune 
19875dbd56e3SPeter Brune    Calling sequence for restricthook:
1988ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
19895dbd56e3SPeter Brune 
19905dbd56e3SPeter Brune +  global - global DM
19915dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
19925dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
1993ec4806b8SPeter Brune .  block  - block DM
19945dbd56e3SPeter Brune -  ctx - optional user-defined function context
19955dbd56e3SPeter Brune 
19965dbd56e3SPeter Brune    Level: advanced
19975dbd56e3SPeter Brune 
19985dbd56e3SPeter Brune    Notes:
1999ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
20005dbd56e3SPeter Brune 
20015dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
20025dbd56e3SPeter Brune 
20035dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2004ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
20055dbd56e3SPeter Brune 
2006bb9467b5SJed Brown    This function is currently not available from Fortran.
2007bb9467b5SJed Brown 
20085dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20095dbd56e3SPeter Brune @*/
2010be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
20115dbd56e3SPeter Brune {
20125dbd56e3SPeter Brune   PetscErrorCode      ierr;
2013be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
20145dbd56e3SPeter Brune 
20155dbd56e3SPeter Brune   PetscFunctionBegin;
20165dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2017be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2018be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
20195dbd56e3SPeter Brune   link->restricthook = restricthook;
2020be081cd6SPeter Brune   link->ddhook       = ddhook;
20215dbd56e3SPeter Brune   link->ctx          = ctx;
20220298fd71SBarry Smith   link->next         = NULL;
20235dbd56e3SPeter Brune   *p                 = link;
20245dbd56e3SPeter Brune   PetscFunctionReturn(0);
20255dbd56e3SPeter Brune }
20265dbd56e3SPeter Brune 
20275dbd56e3SPeter Brune #undef __FUNCT__
2028be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
20295dbd56e3SPeter Brune /*@
2030be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
20315dbd56e3SPeter Brune 
20325dbd56e3SPeter Brune    Collective if any hooks are
20335dbd56e3SPeter Brune 
20345dbd56e3SPeter Brune    Input Arguments:
20355dbd56e3SPeter Brune +  fine - finer DM to use as a base
2036be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2037be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
20385dbd56e3SPeter Brune -  coarse - coarer DM to update
20395dbd56e3SPeter Brune 
20405dbd56e3SPeter Brune    Level: developer
20415dbd56e3SPeter Brune 
20425dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
20435dbd56e3SPeter Brune @*/
2044be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
20455dbd56e3SPeter Brune {
20465dbd56e3SPeter Brune   PetscErrorCode      ierr;
2047be081cd6SPeter Brune   DMSubDomainHookLink link;
20485dbd56e3SPeter Brune 
20495dbd56e3SPeter Brune   PetscFunctionBegin;
2050be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
20518865f1eaSKarl Rupp     if (link->restricthook) {
20528865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
20538865f1eaSKarl Rupp     }
20545dbd56e3SPeter Brune   }
20555dbd56e3SPeter Brune   PetscFunctionReturn(0);
20565dbd56e3SPeter Brune }
20575dbd56e3SPeter Brune 
20585dbd56e3SPeter Brune #undef __FUNCT__
20595fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
20605fe1f584SPeter Brune /*@
20616a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
20625fe1f584SPeter Brune 
20635fe1f584SPeter Brune     Not Collective
20645fe1f584SPeter Brune 
20655fe1f584SPeter Brune     Input Parameter:
20665fe1f584SPeter Brune .   dm - the DM object
20675fe1f584SPeter Brune 
20685fe1f584SPeter Brune     Output Parameter:
20696a7d9d85SPeter Brune .   level - number of coarsenings
20705fe1f584SPeter Brune 
20715fe1f584SPeter Brune     Level: developer
20725fe1f584SPeter Brune 
20736a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
20745fe1f584SPeter Brune 
20755fe1f584SPeter Brune @*/
20765fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
20775fe1f584SPeter Brune {
20785fe1f584SPeter Brune   PetscFunctionBegin;
20795fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20805fe1f584SPeter Brune   *level = dm->leveldown;
20815fe1f584SPeter Brune   PetscFunctionReturn(0);
20825fe1f584SPeter Brune }
20835fe1f584SPeter Brune 
20845fe1f584SPeter Brune 
20855fe1f584SPeter Brune 
20865fe1f584SPeter Brune #undef __FUNCT__
208747c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
208847c6ae99SBarry Smith /*@C
208947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
209047c6ae99SBarry Smith 
209147c6ae99SBarry Smith     Collective on DM
209247c6ae99SBarry Smith 
209347c6ae99SBarry Smith     Input Parameter:
209447c6ae99SBarry Smith +   dm - the DM object
209547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
209647c6ae99SBarry Smith 
209747c6ae99SBarry Smith     Output Parameter:
209847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
209947c6ae99SBarry Smith 
210047c6ae99SBarry Smith     Level: developer
210147c6ae99SBarry Smith 
2102e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
210347c6ae99SBarry Smith 
210447c6ae99SBarry Smith @*/
21057087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
210647c6ae99SBarry Smith {
210747c6ae99SBarry Smith   PetscErrorCode ierr;
210847c6ae99SBarry Smith 
210947c6ae99SBarry Smith   PetscFunctionBegin;
2110171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2111ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
211247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
211347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
211447c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
211547c6ae99SBarry Smith   } else if (dm->ops->refine) {
211647c6ae99SBarry Smith     PetscInt i;
211747c6ae99SBarry Smith 
2118ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
211947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2120ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
212147c6ae99SBarry Smith     }
2122ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
212347c6ae99SBarry Smith   PetscFunctionReturn(0);
212447c6ae99SBarry Smith }
212547c6ae99SBarry Smith 
212647c6ae99SBarry Smith #undef __FUNCT__
212747c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
212847c6ae99SBarry Smith /*@C
212947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
213047c6ae99SBarry Smith 
213147c6ae99SBarry Smith     Collective on DM
213247c6ae99SBarry Smith 
213347c6ae99SBarry Smith     Input Parameter:
213447c6ae99SBarry Smith +   dm - the DM object
213547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith     Output Parameter:
213847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
213947c6ae99SBarry Smith 
214047c6ae99SBarry Smith     Level: developer
214147c6ae99SBarry Smith 
2142e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
214347c6ae99SBarry Smith 
214447c6ae99SBarry Smith @*/
21457087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
214647c6ae99SBarry Smith {
214747c6ae99SBarry Smith   PetscErrorCode ierr;
214847c6ae99SBarry Smith 
214947c6ae99SBarry Smith   PetscFunctionBegin;
2150171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2151ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
215247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
215347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
215447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
215547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
215647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
215747c6ae99SBarry Smith     PetscInt i;
215847c6ae99SBarry Smith 
2159ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
216047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2161ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
216247c6ae99SBarry Smith     }
2163ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
216447c6ae99SBarry Smith   PetscFunctionReturn(0);
216547c6ae99SBarry Smith }
216647c6ae99SBarry Smith 
216747c6ae99SBarry Smith #undef __FUNCT__
2168e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
216947c6ae99SBarry Smith /*@
2170e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
217147c6ae99SBarry Smith    grids associated with two DMs.
217247c6ae99SBarry Smith 
217347c6ae99SBarry Smith    Collective on DM
217447c6ae99SBarry Smith 
217547c6ae99SBarry Smith    Input Parameters:
217647c6ae99SBarry Smith +  dmc - the coarse grid DM
217747c6ae99SBarry Smith -  dmf - the fine grid DM
217847c6ae99SBarry Smith 
217947c6ae99SBarry Smith    Output Parameters:
218047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
218147c6ae99SBarry Smith 
218247c6ae99SBarry Smith    Level: intermediate
218347c6ae99SBarry Smith 
218447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
218547c6ae99SBarry Smith 
2186e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
218747c6ae99SBarry Smith @*/
2188e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
218947c6ae99SBarry Smith {
219047c6ae99SBarry Smith   PetscErrorCode ierr;
219147c6ae99SBarry Smith 
219247c6ae99SBarry Smith   PetscFunctionBegin;
2193171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2194171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
219547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
219647c6ae99SBarry Smith   PetscFunctionReturn(0);
219747c6ae99SBarry Smith }
219847c6ae99SBarry Smith 
219947c6ae99SBarry Smith #undef __FUNCT__
22001a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
22011a266240SBarry Smith /*@C
22021a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
22031a266240SBarry Smith 
22041a266240SBarry Smith     Not Collective
22051a266240SBarry Smith 
22061a266240SBarry Smith     Input Parameters:
22071a266240SBarry Smith +   dm - the DM object
22081a266240SBarry Smith -   destroy - the destroy function
22091a266240SBarry Smith 
22101a266240SBarry Smith     Level: intermediate
22111a266240SBarry Smith 
2212e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
22131a266240SBarry Smith 
2214f07f9ceaSJed Brown @*/
22151a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
22161a266240SBarry Smith {
22171a266240SBarry Smith   PetscFunctionBegin;
2218171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22191a266240SBarry Smith   dm->ctxdestroy = destroy;
22201a266240SBarry Smith   PetscFunctionReturn(0);
22211a266240SBarry Smith }
22221a266240SBarry Smith 
22231a266240SBarry Smith #undef __FUNCT__
22241b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2225b07ff414SBarry Smith /*@
22261b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
222747c6ae99SBarry Smith 
222847c6ae99SBarry Smith     Not Collective
222947c6ae99SBarry Smith 
223047c6ae99SBarry Smith     Input Parameters:
223147c6ae99SBarry Smith +   dm - the DM object
223247c6ae99SBarry Smith -   ctx - the user context
223347c6ae99SBarry Smith 
223447c6ae99SBarry Smith     Level: intermediate
223547c6ae99SBarry Smith 
2236e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
223747c6ae99SBarry Smith 
223847c6ae99SBarry Smith @*/
22391b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
224047c6ae99SBarry Smith {
224147c6ae99SBarry Smith   PetscFunctionBegin;
2242171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
224347c6ae99SBarry Smith   dm->ctx = ctx;
224447c6ae99SBarry Smith   PetscFunctionReturn(0);
224547c6ae99SBarry Smith }
224647c6ae99SBarry Smith 
224747c6ae99SBarry Smith #undef __FUNCT__
22481b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
224947c6ae99SBarry Smith /*@
22501b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
225147c6ae99SBarry Smith 
225247c6ae99SBarry Smith     Not Collective
225347c6ae99SBarry Smith 
225447c6ae99SBarry Smith     Input Parameter:
225547c6ae99SBarry Smith .   dm - the DM object
225647c6ae99SBarry Smith 
225747c6ae99SBarry Smith     Output Parameter:
225847c6ae99SBarry Smith .   ctx - the user context
225947c6ae99SBarry Smith 
226047c6ae99SBarry Smith     Level: intermediate
226147c6ae99SBarry Smith 
2262e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
226347c6ae99SBarry Smith 
226447c6ae99SBarry Smith @*/
22651b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
226647c6ae99SBarry Smith {
226747c6ae99SBarry Smith   PetscFunctionBegin;
2268171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22691b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
227047c6ae99SBarry Smith   PetscFunctionReturn(0);
227147c6ae99SBarry Smith }
227247c6ae99SBarry Smith 
227347c6ae99SBarry Smith #undef __FUNCT__
227408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
227508da532bSDmitry Karpeev /*@C
227608da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
227708da532bSDmitry Karpeev 
227808da532bSDmitry Karpeev     Logically Collective on DM
227908da532bSDmitry Karpeev 
228008da532bSDmitry Karpeev     Input Parameter:
228108da532bSDmitry Karpeev +   dm - the DM object
22820298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
228308da532bSDmitry Karpeev 
228408da532bSDmitry Karpeev     Level: intermediate
228508da532bSDmitry Karpeev 
2286835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
228708da532bSDmitry Karpeev          DMSetJacobian()
228808da532bSDmitry Karpeev 
228908da532bSDmitry Karpeev @*/
229008da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
229108da532bSDmitry Karpeev {
229208da532bSDmitry Karpeev   PetscFunctionBegin;
229308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
229408da532bSDmitry Karpeev   PetscFunctionReturn(0);
229508da532bSDmitry Karpeev }
229608da532bSDmitry Karpeev 
229708da532bSDmitry Karpeev #undef __FUNCT__
229808da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
229908da532bSDmitry Karpeev /*@
230008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
230108da532bSDmitry Karpeev 
230208da532bSDmitry Karpeev     Not Collective
230308da532bSDmitry Karpeev 
230408da532bSDmitry Karpeev     Input Parameter:
230508da532bSDmitry Karpeev .   dm - the DM object to destroy
230608da532bSDmitry Karpeev 
230708da532bSDmitry Karpeev     Output Parameter:
230808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
230908da532bSDmitry Karpeev 
231008da532bSDmitry Karpeev     Level: developer
231108da532bSDmitry Karpeev 
231274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
231308da532bSDmitry Karpeev 
231408da532bSDmitry Karpeev @*/
231508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
231608da532bSDmitry Karpeev {
231708da532bSDmitry Karpeev   PetscFunctionBegin;
231808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
231908da532bSDmitry Karpeev   PetscFunctionReturn(0);
232008da532bSDmitry Karpeev }
232108da532bSDmitry Karpeev 
232208da532bSDmitry Karpeev #undef __FUNCT__
232308da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
232408da532bSDmitry Karpeev /*@C
232508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
232608da532bSDmitry Karpeev 
232708da532bSDmitry Karpeev     Logically Collective on DM
232808da532bSDmitry Karpeev 
232908da532bSDmitry Karpeev     Input Parameters:
233008da532bSDmitry Karpeev +   dm - the DM object to destroy
233108da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
233208da532bSDmitry Karpeev 
233308da532bSDmitry Karpeev     Output parameters:
233408da532bSDmitry Karpeev +   xl - lower bound
233508da532bSDmitry Karpeev -   xu - upper bound
233608da532bSDmitry Karpeev 
233708da532bSDmitry Karpeev     Level: intermediate
233808da532bSDmitry Karpeev 
233974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
234008da532bSDmitry Karpeev 
234108da532bSDmitry Karpeev @*/
234208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
234308da532bSDmitry Karpeev {
234408da532bSDmitry Karpeev   PetscErrorCode ierr;
23455fd66863SKarl Rupp 
234608da532bSDmitry Karpeev   PetscFunctionBegin;
234708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
234808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
234908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
235008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
23518865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
235208da532bSDmitry Karpeev   PetscFunctionReturn(0);
235308da532bSDmitry Karpeev }
235408da532bSDmitry Karpeev 
235508da532bSDmitry Karpeev #undef __FUNCT__
2356b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2357b0ae01b7SPeter Brune /*@
2358b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2359b0ae01b7SPeter Brune 
2360b0ae01b7SPeter Brune     Not Collective
2361b0ae01b7SPeter Brune 
2362b0ae01b7SPeter Brune     Input Parameter:
2363b0ae01b7SPeter Brune .   dm - the DM object
2364b0ae01b7SPeter Brune 
2365b0ae01b7SPeter Brune     Output Parameter:
2366b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2367b0ae01b7SPeter Brune 
2368b0ae01b7SPeter Brune     Level: developer
2369b0ae01b7SPeter Brune 
2370b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2371b0ae01b7SPeter Brune 
2372b0ae01b7SPeter Brune @*/
2373b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2374b0ae01b7SPeter Brune {
2375b0ae01b7SPeter Brune   PetscFunctionBegin;
2376b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2377b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2378b0ae01b7SPeter Brune }
2379b0ae01b7SPeter Brune 
2380b0ae01b7SPeter Brune #undef  __FUNCT__
238108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2382748fac09SDmitry Karpeev /*@C
238308da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
238408da532bSDmitry Karpeev 
238508da532bSDmitry Karpeev     Collective on DM
238608da532bSDmitry Karpeev 
238708da532bSDmitry Karpeev     Input Parameter:
238808da532bSDmitry Karpeev +   dm - the DM object
23890298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
239008da532bSDmitry Karpeev 
239108da532bSDmitry Karpeev     Level: developer
239208da532bSDmitry Karpeev 
239374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
239408da532bSDmitry Karpeev 
239508da532bSDmitry Karpeev @*/
239608da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
239708da532bSDmitry Karpeev {
239808da532bSDmitry Karpeev   PetscErrorCode ierr;
23995fd66863SKarl Rupp 
240008da532bSDmitry Karpeev   PetscFunctionBegin;
240108da532bSDmitry Karpeev   if (x) {
240208da532bSDmitry Karpeev     if (!dm->x) {
240308da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
240408da532bSDmitry Karpeev     }
240508da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
24068865f1eaSKarl Rupp   } else if (dm->x) {
240708da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
240808da532bSDmitry Karpeev   }
240908da532bSDmitry Karpeev   PetscFunctionReturn(0);
241008da532bSDmitry Karpeev }
241108da532bSDmitry Karpeev 
24120298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2413264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2414264ace61SBarry Smith 
2415264ace61SBarry Smith #undef __FUNCT__
2416264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2417264ace61SBarry Smith /*@C
2418264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2419264ace61SBarry Smith 
2420264ace61SBarry Smith   Collective on DM
2421264ace61SBarry Smith 
2422264ace61SBarry Smith   Input Parameters:
2423264ace61SBarry Smith + dm     - The DM object
2424264ace61SBarry Smith - method - The name of the DM type
2425264ace61SBarry Smith 
2426264ace61SBarry Smith   Options Database Key:
2427264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2428264ace61SBarry Smith 
2429264ace61SBarry Smith   Notes:
2430e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2431264ace61SBarry Smith 
2432264ace61SBarry Smith   Level: intermediate
2433264ace61SBarry Smith 
2434264ace61SBarry Smith .keywords: DM, set, type
2435264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2436264ace61SBarry Smith @*/
243719fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2438264ace61SBarry Smith {
2439264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2440264ace61SBarry Smith   PetscBool      match;
2441264ace61SBarry Smith   PetscErrorCode ierr;
2442264ace61SBarry Smith 
2443264ace61SBarry Smith   PetscFunctionBegin;
2444264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2445251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2446264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2447264ace61SBarry Smith 
2448607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
24491c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2450ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2451264ace61SBarry Smith 
2452264ace61SBarry Smith   if (dm->ops->destroy) {
2453264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
24540298fd71SBarry Smith     dm->ops->destroy = NULL;
2455264ace61SBarry Smith   }
2456264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2457264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2458264ace61SBarry Smith   PetscFunctionReturn(0);
2459264ace61SBarry Smith }
2460264ace61SBarry Smith 
2461264ace61SBarry Smith #undef __FUNCT__
2462264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2463264ace61SBarry Smith /*@C
2464264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2465264ace61SBarry Smith 
2466264ace61SBarry Smith   Not Collective
2467264ace61SBarry Smith 
2468264ace61SBarry Smith   Input Parameter:
2469264ace61SBarry Smith . dm  - The DM
2470264ace61SBarry Smith 
2471264ace61SBarry Smith   Output Parameter:
2472264ace61SBarry Smith . type - The DM type name
2473264ace61SBarry Smith 
2474264ace61SBarry Smith   Level: intermediate
2475264ace61SBarry Smith 
2476264ace61SBarry Smith .keywords: DM, get, type, name
2477264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2478264ace61SBarry Smith @*/
247919fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2480264ace61SBarry Smith {
2481264ace61SBarry Smith   PetscErrorCode ierr;
2482264ace61SBarry Smith 
2483264ace61SBarry Smith   PetscFunctionBegin;
2484264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2485264ace61SBarry Smith   PetscValidCharPointer(type,2);
2486264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2487607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2488264ace61SBarry Smith   }
2489264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2490264ace61SBarry Smith   PetscFunctionReturn(0);
2491264ace61SBarry Smith }
2492264ace61SBarry Smith 
249367a56275SMatthew G Knepley #undef __FUNCT__
249467a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
249567a56275SMatthew G Knepley /*@C
249667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
249767a56275SMatthew G Knepley 
249867a56275SMatthew G Knepley   Collective on DM
249967a56275SMatthew G Knepley 
250067a56275SMatthew G Knepley   Input Parameters:
250167a56275SMatthew G Knepley + dm - the DM
250267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
250367a56275SMatthew G Knepley 
250467a56275SMatthew G Knepley   Output Parameter:
250567a56275SMatthew G Knepley . M - pointer to new DM
250667a56275SMatthew G Knepley 
250767a56275SMatthew G Knepley   Notes:
250867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
250967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
251067a56275SMatthew G Knepley   of the input DM.
251167a56275SMatthew G Knepley 
251267a56275SMatthew G Knepley   Level: intermediate
251367a56275SMatthew G Knepley 
251467a56275SMatthew G Knepley .seealso: DMCreate()
251567a56275SMatthew G Knepley @*/
251619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
251767a56275SMatthew G Knepley {
251867a56275SMatthew G Knepley   DM             B;
251967a56275SMatthew G Knepley   char           convname[256];
252067a56275SMatthew G Knepley   PetscBool      sametype, issame;
252167a56275SMatthew G Knepley   PetscErrorCode ierr;
252267a56275SMatthew G Knepley 
252367a56275SMatthew G Knepley   PetscFunctionBegin;
252467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
252567a56275SMatthew G Knepley   PetscValidType(dm,1);
252667a56275SMatthew G Knepley   PetscValidPointer(M,3);
2527251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
252867a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
252967a56275SMatthew G Knepley   {
25300298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
253167a56275SMatthew G Knepley 
253267a56275SMatthew G Knepley     /*
253367a56275SMatthew G Knepley        Order of precedence:
253467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
253567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
253667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
253767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
253867a56275SMatthew G Knepley        5) Use a really basic converter.
253967a56275SMatthew G Knepley     */
254067a56275SMatthew G Knepley 
254167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
254267a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
254367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
254467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
254567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
254667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
25470005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
254867a56275SMatthew G Knepley     if (conv) goto foundconv;
254967a56275SMatthew G Knepley 
255067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
255182f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
255267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
255367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
255467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
255567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
255667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
255767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
25580005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
255967a56275SMatthew G Knepley     if (conv) {
2560fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
256167a56275SMatthew G Knepley       goto foundconv;
256267a56275SMatthew G Knepley     }
256367a56275SMatthew G Knepley 
256467a56275SMatthew G Knepley #if 0
256567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
256667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2567fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
256867a56275SMatthew G Knepley     if (conv) goto foundconv;
256967a56275SMatthew G Knepley 
257067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
257167a56275SMatthew G Knepley     if (dm->ops->convert) {
257267a56275SMatthew G Knepley       conv = dm->ops->convert;
257367a56275SMatthew G Knepley     }
257467a56275SMatthew G Knepley     if (conv) goto foundconv;
257567a56275SMatthew G Knepley #endif
257667a56275SMatthew G Knepley 
257767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
257882f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
257967a56275SMatthew G Knepley 
258067a56275SMatthew G Knepley foundconv:
258167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
258267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
258367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
258467a56275SMatthew G Knepley   }
258567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
258667a56275SMatthew G Knepley   PetscFunctionReturn(0);
258767a56275SMatthew G Knepley }
2588264ace61SBarry Smith 
2589264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2590264ace61SBarry Smith 
2591264ace61SBarry Smith #undef __FUNCT__
2592264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2593264ace61SBarry Smith /*@C
25941c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
25951c84c290SBarry Smith 
25961c84c290SBarry Smith   Not Collective
25971c84c290SBarry Smith 
25981c84c290SBarry Smith   Input Parameters:
25991c84c290SBarry Smith + name        - The name of a new user-defined creation routine
26001c84c290SBarry Smith - create_func - The creation routine itself
26011c84c290SBarry Smith 
26021c84c290SBarry Smith   Notes:
26031c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
26041c84c290SBarry Smith 
26051c84c290SBarry Smith 
26061c84c290SBarry Smith   Sample usage:
26071c84c290SBarry Smith .vb
2608bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
26091c84c290SBarry Smith .ve
26101c84c290SBarry Smith 
26111c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
26121c84c290SBarry Smith .vb
26131c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
26141c84c290SBarry Smith     DMSetType(DM,"my_da");
26151c84c290SBarry Smith .ve
26161c84c290SBarry Smith    or at runtime via the option
26171c84c290SBarry Smith .vb
26181c84c290SBarry Smith     -da_type my_da
26191c84c290SBarry Smith .ve
2620264ace61SBarry Smith 
2621264ace61SBarry Smith   Level: advanced
26221c84c290SBarry Smith 
26231c84c290SBarry Smith .keywords: DM, register
2624bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
26251c84c290SBarry Smith 
2626264ace61SBarry Smith @*/
2627bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2628264ace61SBarry Smith {
2629264ace61SBarry Smith   PetscErrorCode ierr;
2630264ace61SBarry Smith 
2631264ace61SBarry Smith   PetscFunctionBegin;
2632a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2633264ace61SBarry Smith   PetscFunctionReturn(0);
2634264ace61SBarry Smith }
2635264ace61SBarry Smith 
2636b859378eSBarry Smith #undef __FUNCT__
2637b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2638b859378eSBarry Smith /*@C
263955849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2640b859378eSBarry Smith 
2641b859378eSBarry Smith   Collective on PetscViewer
2642b859378eSBarry Smith 
2643b859378eSBarry Smith   Input Parameters:
2644b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2645b859378eSBarry Smith            some related function before a call to DMLoad().
2646b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2647b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2648b859378eSBarry Smith 
2649b859378eSBarry Smith    Level: intermediate
2650b859378eSBarry Smith 
2651b859378eSBarry Smith   Notes:
265255849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2653b859378eSBarry Smith 
2654b859378eSBarry Smith   Notes for advanced users:
2655b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2656b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2657b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2658b859378eSBarry Smith   format is
2659b859378eSBarry Smith .vb
2660b859378eSBarry Smith      has not yet been determined
2661b859378eSBarry Smith .ve
2662b859378eSBarry Smith 
2663b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2664b859378eSBarry Smith @*/
2665b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2666b859378eSBarry Smith {
2667b859378eSBarry Smith   PetscErrorCode ierr;
266832c0f0efSBarry Smith   PetscBool      isbinary;
266955849f57SBarry Smith   PetscInt       classid;
267032c0f0efSBarry Smith   char           type[256];
2671b859378eSBarry Smith 
2672b859378eSBarry Smith   PetscFunctionBegin;
2673b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2674b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
267532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
267632c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2677b859378eSBarry Smith 
267832c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
2679ce94432eSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file");
268032c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
268132c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
26822d53ad75SBarry Smith   if (newdm->ops->load) {
2683b859378eSBarry Smith     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
26842d53ad75SBarry Smith   }
2685b859378eSBarry Smith   PetscFunctionReturn(0);
2686b859378eSBarry Smith }
2687b859378eSBarry Smith 
26887da65231SMatthew G Knepley /******************************** FEM Support **********************************/
26897da65231SMatthew G Knepley 
26907da65231SMatthew G Knepley #undef __FUNCT__
26917da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2692a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2693a6dfd86eSKarl Rupp {
26941d47ebbbSSatish Balay   PetscInt       f;
26951b30c384SMatthew G Knepley   PetscErrorCode ierr;
26961b30c384SMatthew G Knepley 
26977da65231SMatthew G Knepley   PetscFunctionBegin;
269874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26991d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
270074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
27017da65231SMatthew G Knepley   }
27027da65231SMatthew G Knepley   PetscFunctionReturn(0);
27037da65231SMatthew G Knepley }
27047da65231SMatthew G Knepley 
27057da65231SMatthew G Knepley #undef __FUNCT__
27067da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2707a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2708a6dfd86eSKarl Rupp {
27091b30c384SMatthew G Knepley   PetscInt       f, g;
27107da65231SMatthew G Knepley   PetscErrorCode ierr;
27117da65231SMatthew G Knepley 
27127da65231SMatthew G Knepley   PetscFunctionBegin;
271374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27141d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
271574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
27161d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
271774778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
27187da65231SMatthew G Knepley     }
271974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
27207da65231SMatthew G Knepley   }
27217da65231SMatthew G Knepley   PetscFunctionReturn(0);
27227da65231SMatthew G Knepley }
2723e7c4fc90SDmitry Karpeev 
2724970e74d5SMatthew G Knepley #undef __FUNCT__
272588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
272688ed4aceSMatthew G Knepley /*@
272788ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
272888ed4aceSMatthew G Knepley 
272988ed4aceSMatthew G Knepley   Input Parameter:
273088ed4aceSMatthew G Knepley . dm - The DM
273188ed4aceSMatthew G Knepley 
273288ed4aceSMatthew G Knepley   Output Parameter:
273388ed4aceSMatthew G Knepley . section - The PetscSection
273488ed4aceSMatthew G Knepley 
273588ed4aceSMatthew G Knepley   Level: intermediate
273688ed4aceSMatthew G Knepley 
273788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
273888ed4aceSMatthew G Knepley 
273988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
274088ed4aceSMatthew G Knepley @*/
27410adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
27420adebc6cSBarry Smith {
274388ed4aceSMatthew G Knepley   PetscFunctionBegin;
274488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
274588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
274688ed4aceSMatthew G Knepley   *section = dm->defaultSection;
274788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
274888ed4aceSMatthew G Knepley }
274988ed4aceSMatthew G Knepley 
275088ed4aceSMatthew G Knepley #undef __FUNCT__
275188ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
275288ed4aceSMatthew G Knepley /*@
275388ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
275488ed4aceSMatthew G Knepley 
275588ed4aceSMatthew G Knepley   Input Parameters:
275688ed4aceSMatthew G Knepley + dm - The DM
275788ed4aceSMatthew G Knepley - section - The PetscSection
275888ed4aceSMatthew G Knepley 
275988ed4aceSMatthew G Knepley   Level: intermediate
276088ed4aceSMatthew G Knepley 
276188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
276288ed4aceSMatthew G Knepley 
276388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
276488ed4aceSMatthew G Knepley @*/
27650adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
27660adebc6cSBarry Smith {
2767af122d2aSMatthew G Knepley   PetscInt       numFields;
2768af122d2aSMatthew G Knepley   PetscInt       f;
276988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
277088ed4aceSMatthew G Knepley 
277188ed4aceSMatthew G Knepley   PetscFunctionBegin;
277288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27731d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
27741d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
277588ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
277688ed4aceSMatthew G Knepley   dm->defaultSection = section;
2777af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2778af122d2aSMatthew G Knepley   if (numFields) {
2779af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2780af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2781af122d2aSMatthew G Knepley       const char *name;
2782af122d2aSMatthew G Knepley 
2783af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2784af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2785af122d2aSMatthew G Knepley     }
2786af122d2aSMatthew G Knepley   }
27871d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
27881d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
278988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
279088ed4aceSMatthew G Knepley }
279188ed4aceSMatthew G Knepley 
279288ed4aceSMatthew G Knepley #undef __FUNCT__
279388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
279488ed4aceSMatthew G Knepley /*@
279588ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
279688ed4aceSMatthew G Knepley 
27978b1ab98fSJed Brown   Collective on DM
27988b1ab98fSJed Brown 
279988ed4aceSMatthew G Knepley   Input Parameter:
280088ed4aceSMatthew G Knepley . dm - The DM
280188ed4aceSMatthew G Knepley 
280288ed4aceSMatthew G Knepley   Output Parameter:
280388ed4aceSMatthew G Knepley . section - The PetscSection
280488ed4aceSMatthew G Knepley 
280588ed4aceSMatthew G Knepley   Level: intermediate
280688ed4aceSMatthew G Knepley 
280788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
280888ed4aceSMatthew G Knepley 
280988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
281088ed4aceSMatthew G Knepley @*/
28110adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
28120adebc6cSBarry Smith {
281388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
281488ed4aceSMatthew G Knepley 
281588ed4aceSMatthew G Knepley   PetscFunctionBegin;
281688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
281788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
281888ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
281982f516ccSBarry Smith     if (!dm->defaultSection || !dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection and PetscSF in order to create a global PetscSection");
2820b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2821cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
2822ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm),dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
282388ed4aceSMatthew G Knepley   }
282488ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
282588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
282688ed4aceSMatthew G Knepley }
282788ed4aceSMatthew G Knepley 
282888ed4aceSMatthew G Knepley #undef __FUNCT__
2829b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2830b21d0597SMatthew G Knepley /*@
2831b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2832b21d0597SMatthew G Knepley 
2833b21d0597SMatthew G Knepley   Input Parameters:
2834b21d0597SMatthew G Knepley + dm - The DM
28355080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2836b21d0597SMatthew G Knepley 
2837b21d0597SMatthew G Knepley   Level: intermediate
2838b21d0597SMatthew G Knepley 
2839b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2840b21d0597SMatthew G Knepley 
2841b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2842b21d0597SMatthew G Knepley @*/
28430adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
28440adebc6cSBarry Smith {
2845b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2846b21d0597SMatthew G Knepley 
2847b21d0597SMatthew G Knepley   PetscFunctionBegin;
2848b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28495080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
28501d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2851b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2852b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2853b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2854b21d0597SMatthew G Knepley }
2855b21d0597SMatthew G Knepley 
2856b21d0597SMatthew G Knepley #undef __FUNCT__
285788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
285888ed4aceSMatthew G Knepley /*@
285988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
286088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
286188ed4aceSMatthew G Knepley 
286288ed4aceSMatthew G Knepley   Input Parameter:
286388ed4aceSMatthew G Knepley . dm - The DM
286488ed4aceSMatthew G Knepley 
286588ed4aceSMatthew G Knepley   Output Parameter:
286688ed4aceSMatthew G Knepley . sf - The PetscSF
286788ed4aceSMatthew G Knepley 
286888ed4aceSMatthew G Knepley   Level: intermediate
286988ed4aceSMatthew G Knepley 
287088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
287188ed4aceSMatthew G Knepley 
287288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
287388ed4aceSMatthew G Knepley @*/
28740adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
28750adebc6cSBarry Smith {
287688ed4aceSMatthew G Knepley   PetscInt       nroots;
287788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
287888ed4aceSMatthew G Knepley 
287988ed4aceSMatthew G Knepley   PetscFunctionBegin;
288088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
288188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
28820298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
288388ed4aceSMatthew G Knepley   if (nroots < 0) {
288488ed4aceSMatthew G Knepley     PetscSection section, gSection;
288588ed4aceSMatthew G Knepley 
288688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
288731ea6d37SMatthew G Knepley     if (section) {
288888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
288988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
289031ea6d37SMatthew G Knepley     } else {
28910298fd71SBarry Smith       *sf = NULL;
289231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
289331ea6d37SMatthew G Knepley     }
289488ed4aceSMatthew G Knepley   }
289588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
289688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
289788ed4aceSMatthew G Knepley }
289888ed4aceSMatthew G Knepley 
289988ed4aceSMatthew G Knepley #undef __FUNCT__
290088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
290188ed4aceSMatthew G Knepley /*@
290288ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
290388ed4aceSMatthew G Knepley 
290488ed4aceSMatthew G Knepley   Input Parameters:
290588ed4aceSMatthew G Knepley + dm - The DM
290688ed4aceSMatthew G Knepley - sf - The PetscSF
290788ed4aceSMatthew G Knepley 
290888ed4aceSMatthew G Knepley   Level: intermediate
290988ed4aceSMatthew G Knepley 
291088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
291188ed4aceSMatthew G Knepley 
291288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
291388ed4aceSMatthew G Knepley @*/
29140adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
29150adebc6cSBarry Smith {
291688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
291788ed4aceSMatthew G Knepley 
291888ed4aceSMatthew G Knepley   PetscFunctionBegin;
291988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
292088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
292188ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
292288ed4aceSMatthew G Knepley   dm->defaultSF = sf;
292388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
292488ed4aceSMatthew G Knepley }
292588ed4aceSMatthew G Knepley 
292688ed4aceSMatthew G Knepley #undef __FUNCT__
292788ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
292888ed4aceSMatthew G Knepley /*@C
292988ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
293088ed4aceSMatthew G Knepley   describing the data layout.
293188ed4aceSMatthew G Knepley 
293288ed4aceSMatthew G Knepley   Input Parameters:
293388ed4aceSMatthew G Knepley + dm - The DM
293488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
293588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
293688ed4aceSMatthew G Knepley 
293788ed4aceSMatthew G Knepley   Level: intermediate
293888ed4aceSMatthew G Knepley 
293988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
294088ed4aceSMatthew G Knepley @*/
294188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
294288ed4aceSMatthew G Knepley {
294382f516ccSBarry Smith   MPI_Comm       comm;
294488ed4aceSMatthew G Knepley   PetscLayout    layout;
294588ed4aceSMatthew G Knepley   const PetscInt *ranges;
294688ed4aceSMatthew G Knepley   PetscInt       *local;
294788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
294888ed4aceSMatthew G Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves, l;
294988ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
295088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
295188ed4aceSMatthew G Knepley 
295288ed4aceSMatthew G Knepley   PetscFunctionBegin;
295382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
295488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
295588ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
295688ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
295788ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
295888ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
295988ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
296088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
296188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
296288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
296388ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
296488ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
29656636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
296688ed4aceSMatthew G Knepley 
29676636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29686636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
29696636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
297088ed4aceSMatthew G Knepley   }
297188ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
297288ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
297388ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
29741f588964SMatthew G Knepley     const PetscInt *cind;
29756636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
297688ed4aceSMatthew G Knepley 
297788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
297888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
297988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
298088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
298188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29826636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
298388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
29846636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
29856636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
29866636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
2987057b4bcdSMatthew 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);
29886636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
29896636e97aSMatthew G Knepley     }
299088ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
299188ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
299288ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
299388ed4aceSMatthew G Knepley     }
299488ed4aceSMatthew G Knepley     if (gdof < 0) {
29956636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
299688ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
299788ed4aceSMatthew G Knepley 
299831d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
299931d3f06eSJed Brown         if (r < 0) r = -(r+2);
300088ed4aceSMatthew G Knepley         remote[l].rank  = r;
300188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
300288ed4aceSMatthew G Knepley       }
300388ed4aceSMatthew G Knepley     } else {
30046636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
300588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
300688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
300788ed4aceSMatthew G Knepley       }
300888ed4aceSMatthew G Knepley     }
300988ed4aceSMatthew G Knepley   }
30106636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
301188ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
301288ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
301388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
301488ed4aceSMatthew G Knepley }
3015af122d2aSMatthew G Knepley 
3016af122d2aSMatthew G Knepley #undef __FUNCT__
3017b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3018b21d0597SMatthew G Knepley /*@
3019b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3020b21d0597SMatthew G Knepley 
3021b21d0597SMatthew G Knepley   Input Parameter:
3022b21d0597SMatthew G Knepley . dm - The DM
3023b21d0597SMatthew G Knepley 
3024b21d0597SMatthew G Knepley   Output Parameter:
3025b21d0597SMatthew G Knepley . sf - The PetscSF
3026b21d0597SMatthew G Knepley 
3027b21d0597SMatthew G Knepley   Level: intermediate
3028b21d0597SMatthew G Knepley 
3029b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3030b21d0597SMatthew G Knepley 
3031057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3032b21d0597SMatthew G Knepley @*/
30330adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
30340adebc6cSBarry Smith {
3035b21d0597SMatthew G Knepley   PetscFunctionBegin;
3036b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3037b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3038b21d0597SMatthew G Knepley   *sf = dm->sf;
3039b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3040b21d0597SMatthew G Knepley }
3041b21d0597SMatthew G Knepley 
3042b21d0597SMatthew G Knepley #undef __FUNCT__
3043057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3044057b4bcdSMatthew G Knepley /*@
3045057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3046057b4bcdSMatthew G Knepley 
3047057b4bcdSMatthew G Knepley   Input Parameters:
3048057b4bcdSMatthew G Knepley + dm - The DM
3049057b4bcdSMatthew G Knepley - sf - The PetscSF
3050057b4bcdSMatthew G Knepley 
3051057b4bcdSMatthew G Knepley   Level: intermediate
3052057b4bcdSMatthew G Knepley 
3053057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3054057b4bcdSMatthew G Knepley @*/
30550adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
30560adebc6cSBarry Smith {
3057057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3058057b4bcdSMatthew G Knepley 
3059057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3060057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3061057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3062057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3063057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3064057b4bcdSMatthew G Knepley   dm->sf = sf;
3065057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3066057b4bcdSMatthew G Knepley }
3067057b4bcdSMatthew G Knepley 
3068057b4bcdSMatthew G Knepley #undef __FUNCT__
3069af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3070af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3071af122d2aSMatthew G Knepley {
3072af122d2aSMatthew G Knepley   PetscFunctionBegin;
3073af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3074af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3075af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3076af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3077af122d2aSMatthew G Knepley }
3078af122d2aSMatthew G Knepley 
3079af122d2aSMatthew G Knepley #undef __FUNCT__
3080af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3081af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3082af122d2aSMatthew G Knepley {
3083af122d2aSMatthew G Knepley   PetscInt       f;
3084af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3085af122d2aSMatthew G Knepley 
3086af122d2aSMatthew G Knepley   PetscFunctionBegin;
3087af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3088af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3089af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3090af122d2aSMatthew G Knepley   }
3091af122d2aSMatthew G Knepley   ierr          = PetscFree(dm->fields);CHKERRQ(ierr);
3092af122d2aSMatthew G Knepley   dm->numFields = numFields;
3093af122d2aSMatthew G Knepley   ierr          = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3094af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
309582f516ccSBarry Smith     ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm), (PetscContainer*) &dm->fields[f]);CHKERRQ(ierr);
3096af122d2aSMatthew G Knepley   }
3097af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3098af122d2aSMatthew G Knepley }
3099af122d2aSMatthew G Knepley 
3100af122d2aSMatthew G Knepley #undef __FUNCT__
3101af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3102af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3103af122d2aSMatthew G Knepley {
3104af122d2aSMatthew G Knepley   PetscFunctionBegin;
3105af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3106af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
310782f516ccSBarry Smith   if (!dm->fields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
310882f516ccSBarry Smith   if ((f < 0) || (f >= dm->numFields)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %d should be in [%d,%d)", f, 0, dm->numFields);
3109af122d2aSMatthew G Knepley   *field = dm->fields[f];
3110af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3111af122d2aSMatthew G Knepley }
31126636e97aSMatthew G Knepley 
31136636e97aSMatthew G Knepley #undef __FUNCT__
3114b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3115b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3116b64e0483SPeter Brune {
3117b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3118b64e0483SPeter Brune   PetscErrorCode ierr;
3119b64e0483SPeter Brune   Vec coords,ccoords;
3120b64e0483SPeter Brune   VecScatter scat;
3121b64e0483SPeter Brune   PetscFunctionBegin;
3122b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3123b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3124b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3125b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3126b64e0483SPeter Brune   if (coords && !ccoords) {
3127b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
3128b64e0483SPeter Brune     ierr = DMCreateInjection(dmc_coord,dm_coord,&scat);CHKERRQ(ierr);
3129b64e0483SPeter Brune     ierr = VecScatterBegin(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3130b64e0483SPeter Brune     ierr = VecScatterEnd(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3131b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3132b64e0483SPeter Brune     ierr = VecScatterDestroy(&scat);CHKERRQ(ierr);
3133b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3134b64e0483SPeter Brune   }
3135b64e0483SPeter Brune   PetscFunctionReturn(0);
3136b64e0483SPeter Brune }
3137b64e0483SPeter Brune 
3138b64e0483SPeter Brune #undef __FUNCT__
313903dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
314003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
314103dadc2fSPeter Brune {
314203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
314303dadc2fSPeter Brune   PetscErrorCode ierr;
314403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
314503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
314603dadc2fSPeter Brune   PetscFunctionBegin;
314703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
314803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
314903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
315003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
315103dadc2fSPeter Brune   if (coords && !ccoords) {
315203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
315303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
315403dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
315503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
315603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
315703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
315803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
315903dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
316003dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
316103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
316203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
316303dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
316403dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
316503dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
316603dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
316703dadc2fSPeter Brune   }
316803dadc2fSPeter Brune   PetscFunctionReturn(0);
316903dadc2fSPeter Brune }
317003dadc2fSPeter Brune 
317103dadc2fSPeter Brune #undef __FUNCT__
31726636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
31736636e97aSMatthew G Knepley /*@
31746636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
31756636e97aSMatthew G Knepley 
31766636e97aSMatthew G Knepley   Collective on DM
31776636e97aSMatthew G Knepley 
31786636e97aSMatthew G Knepley   Input Parameters:
31796636e97aSMatthew G Knepley + dm - the DM
31806636e97aSMatthew G Knepley - c - coordinate vector
31816636e97aSMatthew G Knepley 
31826636e97aSMatthew G Knepley   Note:
31836636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
31846636e97aSMatthew G Knepley 
31856636e97aSMatthew G Knepley   Level: intermediate
31866636e97aSMatthew G Knepley 
31876636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31886636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
31896636e97aSMatthew G Knepley @*/
31906636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
31916636e97aSMatthew G Knepley {
31926636e97aSMatthew G Knepley   PetscErrorCode ierr;
31936636e97aSMatthew G Knepley 
31946636e97aSMatthew G Knepley   PetscFunctionBegin;
31956636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31966636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
31976636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
31986636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
31996636e97aSMatthew G Knepley   dm->coordinates = c;
32006636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3201b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
320203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
32036636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32046636e97aSMatthew G Knepley }
32056636e97aSMatthew G Knepley 
32066636e97aSMatthew G Knepley #undef __FUNCT__
32076636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
32086636e97aSMatthew G Knepley /*@
32096636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
32106636e97aSMatthew G Knepley 
32116636e97aSMatthew G Knepley   Collective on DM
32126636e97aSMatthew G Knepley 
32136636e97aSMatthew G Knepley    Input Parameters:
32146636e97aSMatthew G Knepley +  dm - the DM
32156636e97aSMatthew G Knepley -  c - coordinate vector
32166636e97aSMatthew G Knepley 
32176636e97aSMatthew G Knepley   Note:
32186636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
32196636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
32206636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
32216636e97aSMatthew G Knepley 
32226636e97aSMatthew G Knepley   Level: intermediate
32236636e97aSMatthew G Knepley 
32246636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32256636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
32266636e97aSMatthew G Knepley @*/
32276636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
32286636e97aSMatthew G Knepley {
32296636e97aSMatthew G Knepley   PetscErrorCode ierr;
32306636e97aSMatthew G Knepley 
32316636e97aSMatthew G Knepley   PetscFunctionBegin;
32326636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
32346636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
32356636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
32368865f1eaSKarl Rupp 
32376636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
32388865f1eaSKarl Rupp 
32396636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
32406636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32416636e97aSMatthew G Knepley }
32426636e97aSMatthew G Knepley 
32436636e97aSMatthew G Knepley #undef __FUNCT__
32446636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
32456636e97aSMatthew G Knepley /*@
32466636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
32476636e97aSMatthew G Knepley 
32486636e97aSMatthew G Knepley   Not Collective
32496636e97aSMatthew G Knepley 
32506636e97aSMatthew G Knepley   Input Parameter:
32516636e97aSMatthew G Knepley . dm - the DM
32526636e97aSMatthew G Knepley 
32536636e97aSMatthew G Knepley   Output Parameter:
32546636e97aSMatthew G Knepley . c - global coordinate vector
32556636e97aSMatthew G Knepley 
32566636e97aSMatthew G Knepley   Note:
32576636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
32586636e97aSMatthew G Knepley 
32596636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
32606636e97aSMatthew G Knepley 
32616636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
32626636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
32636636e97aSMatthew G Knepley 
32646636e97aSMatthew G Knepley   Level: intermediate
32656636e97aSMatthew G Knepley 
32666636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32676636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
32686636e97aSMatthew G Knepley @*/
32696636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
32706636e97aSMatthew G Knepley {
32716636e97aSMatthew G Knepley   PetscErrorCode ierr;
32726636e97aSMatthew G Knepley 
32736636e97aSMatthew G Knepley   PetscFunctionBegin;
32746636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32756636e97aSMatthew G Knepley   PetscValidPointer(c,2);
32761f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
32770298fd71SBarry Smith     DM cdm = NULL;
32786636e97aSMatthew G Knepley 
32796636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
32806636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
32816636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
32826636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
32836636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
32846636e97aSMatthew G Knepley   }
32856636e97aSMatthew G Knepley   *c = dm->coordinates;
32866636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32876636e97aSMatthew G Knepley }
32886636e97aSMatthew G Knepley 
32896636e97aSMatthew G Knepley #undef __FUNCT__
32906636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
32916636e97aSMatthew G Knepley /*@
32926636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
32936636e97aSMatthew G Knepley 
32946636e97aSMatthew G Knepley   Collective on DM
32956636e97aSMatthew G Knepley 
32966636e97aSMatthew G Knepley   Input Parameter:
32976636e97aSMatthew G Knepley . dm - the DM
32986636e97aSMatthew G Knepley 
32996636e97aSMatthew G Knepley   Output Parameter:
33006636e97aSMatthew G Knepley . c - coordinate vector
33016636e97aSMatthew G Knepley 
33026636e97aSMatthew G Knepley   Note:
33036636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
33046636e97aSMatthew G Knepley 
33056636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
33066636e97aSMatthew G Knepley 
33076636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
33086636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
33096636e97aSMatthew G Knepley 
33106636e97aSMatthew G Knepley   Level: intermediate
33116636e97aSMatthew G Knepley 
33126636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33136636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
33146636e97aSMatthew G Knepley @*/
33156636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
33166636e97aSMatthew G Knepley {
33176636e97aSMatthew G Knepley   PetscErrorCode ierr;
33186636e97aSMatthew G Knepley 
33196636e97aSMatthew G Knepley   PetscFunctionBegin;
33206636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33216636e97aSMatthew G Knepley   PetscValidPointer(c,2);
33221f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
33230298fd71SBarry Smith     DM cdm = NULL;
33246636e97aSMatthew G Knepley 
33256636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
33266636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
33276636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
33286636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
33296636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
33306636e97aSMatthew G Knepley   }
33316636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
33326636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33336636e97aSMatthew G Knepley }
33346636e97aSMatthew G Knepley 
33356636e97aSMatthew G Knepley #undef __FUNCT__
33366636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
33376636e97aSMatthew G Knepley /*@
33386636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
33396636e97aSMatthew G Knepley 
33406636e97aSMatthew G Knepley   Collective on DM
33416636e97aSMatthew G Knepley 
33426636e97aSMatthew G Knepley   Input Parameter:
33436636e97aSMatthew G Knepley . dm - the DM
33446636e97aSMatthew G Knepley 
33456636e97aSMatthew G Knepley   Output Parameter:
33466636e97aSMatthew G Knepley . cdm - coordinate DM
33476636e97aSMatthew G Knepley 
33486636e97aSMatthew G Knepley   Level: intermediate
33496636e97aSMatthew G Knepley 
33506636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33516636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
33526636e97aSMatthew G Knepley @*/
33536636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
33546636e97aSMatthew G Knepley {
33556636e97aSMatthew G Knepley   PetscErrorCode ierr;
33566636e97aSMatthew G Knepley 
33576636e97aSMatthew G Knepley   PetscFunctionBegin;
33586636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33596636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
33606636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
336182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
33626636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
33636636e97aSMatthew G Knepley   }
33646636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
33656636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33666636e97aSMatthew G Knepley }
3367e87bb0d3SMatthew G Knepley 
3368e87bb0d3SMatthew G Knepley #undef __FUNCT__
3369e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3370e87bb0d3SMatthew G Knepley /*@
3371e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3372e87bb0d3SMatthew G Knepley 
3373e87bb0d3SMatthew G Knepley   Not collective
3374e87bb0d3SMatthew G Knepley 
3375e87bb0d3SMatthew G Knepley   Input Parameters:
3376e87bb0d3SMatthew G Knepley + dm - The DM
3377e87bb0d3SMatthew G Knepley - v - The Vec of points
3378e87bb0d3SMatthew G Knepley 
337961e3bb9bSMatthew G Knepley   Output Parameter:
3380e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3381e87bb0d3SMatthew G Knepley 
3382e87bb0d3SMatthew G Knepley   Level: developer
338361e3bb9bSMatthew G Knepley 
338461e3bb9bSMatthew G Knepley .keywords: point location, mesh
338561e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
338661e3bb9bSMatthew G Knepley @*/
3387e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3388e87bb0d3SMatthew G Knepley {
3389735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3390735aa83eSMatthew G Knepley 
3391e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3392e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3393e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3394e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3395735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3396735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
339782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3398e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3399e87bb0d3SMatthew G Knepley }
3400