xref: /petsc/src/dm/interface/dm.c (revision b412c318f95038d6b0c0d53c83ad7d4018d14d81)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
5f089877aSRichard Tran Mills PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal;
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
16dba0e793SJed Brown . prefix - prefix to use for viewing, or NULL to use prefix of 'dm'
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;
101*b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
1021411c6eeSJed Brown   *dm = v;
103a4121054SBarry Smith   PetscFunctionReturn(0);
104a4121054SBarry Smith }
105a4121054SBarry Smith 
106a4121054SBarry Smith #undef __FUNCT__
10738221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
10838221697SMatthew G. Knepley /*@
10938221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
11038221697SMatthew G. Knepley 
11138221697SMatthew G. Knepley   Collective on MPI_Comm
11238221697SMatthew G. Knepley 
11338221697SMatthew G. Knepley   Input Parameter:
11438221697SMatthew G. Knepley . dm - The original DM object
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   Output Parameter:
11738221697SMatthew G. Knepley . newdm  - The new DM object
11838221697SMatthew G. Knepley 
11938221697SMatthew G. Knepley   Level: beginner
12038221697SMatthew G. Knepley 
12138221697SMatthew G. Knepley .keywords: DM, topology, create
12238221697SMatthew G. Knepley @*/
12338221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12438221697SMatthew G. Knepley {
12538221697SMatthew G. Knepley   PetscSF        sf;
12638221697SMatthew G. Knepley   Vec            coords;
12738221697SMatthew G. Knepley   void          *ctx;
12838221697SMatthew G. Knepley   PetscErrorCode ierr;
12938221697SMatthew G. Knepley 
13038221697SMatthew G. Knepley   PetscFunctionBegin;
13138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13238221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
13338221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
13438221697SMatthew G. Knepley   if (dm->ops->clone) {
13538221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13638221697SMatthew G. Knepley   }
13738221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13838221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
14238221697SMatthew G. Knepley   if (coords) {
14338221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
14438221697SMatthew G. Knepley   } else {
14538221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14638221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14738221697SMatthew G. Knepley   }
14838221697SMatthew G. Knepley   PetscFunctionReturn(0);
14938221697SMatthew G. Knepley }
15038221697SMatthew G. Knepley 
15138221697SMatthew 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 
374a546ed68SMatthew G. Knepley   /* I think it makes sense to dump all attached things when you are destroyed, which also eliminates circular references */
375a546ed68SMatthew G. Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
376a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "pmat", NULL);CHKERRQ(ierr);
377a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "nullspace", NULL);CHKERRQ(ierr);
378a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "nearnullspace", NULL);CHKERRQ(ierr);
379a546ed68SMatthew G. Knepley   }
38087e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
381732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3828865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
3838865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
384732e2eb9SMatthew G Knepley   }
385dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
38671cd77b2SBarry Smith   if ((*dm)->x) {
387c688c046SMatthew G Knepley     DM obj;
388c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
389c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
39071cd77b2SBarry Smith   }
3912348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
3922348bcf4SPeter Brune   if ((*dm)->x) {
3932348bcf4SPeter Brune     DM obj;
3942348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
3952348bcf4SPeter Brune     if (obj == *dm) cnt++;
3962348bcf4SPeter Brune   }
397732e2eb9SMatthew G Knepley 
3986bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
399732e2eb9SMatthew G Knepley   /*
400732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
401732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
402732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
403732e2eb9SMatthew G Knepley   */
4046bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4056bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
406732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4076bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4086bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
409732e2eb9SMatthew G Knepley   }
410dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
411dfe15315SJed Brown     nnext = nlink->next;
412dfe15315SJed 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);
413dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
414dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
415dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
416dfe15315SJed Brown   }
4170298fd71SBarry Smith   (*dm)->namedglobal = NULL;
4181a266240SBarry Smith 
4192348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */
4202348bcf4SPeter Brune     nnext = nlink->next;
4212348bcf4SPeter 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);
4222348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4232348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4242348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4252348bcf4SPeter Brune   }
4260298fd71SBarry Smith   (*dm)->namedlocal = NULL;
4272348bcf4SPeter Brune 
428b17ce1afSJed Brown   /* Destroy the list of hooks */
429c833c3b5SJed Brown   {
430c833c3b5SJed Brown     DMCoarsenHookLink link,next;
431b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
432b17ce1afSJed Brown       next = link->next;
433b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
434b17ce1afSJed Brown     }
4350298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
436c833c3b5SJed Brown   }
437c833c3b5SJed Brown   {
438c833c3b5SJed Brown     DMRefineHookLink link,next;
439c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
440c833c3b5SJed Brown       next = link->next;
441c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
442c833c3b5SJed Brown     }
4430298fd71SBarry Smith     (*dm)->refinehook = NULL;
444c833c3b5SJed Brown   }
445be081cd6SPeter Brune   {
446be081cd6SPeter Brune     DMSubDomainHookLink link,next;
447be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
448be081cd6SPeter Brune       next = link->next;
449be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
450be081cd6SPeter Brune     }
4510298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
452be081cd6SPeter Brune   }
453baf369e7SPeter Brune   {
454baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
455baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
456baf369e7SPeter Brune       next = link->next;
457baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
458baf369e7SPeter Brune     }
4590298fd71SBarry Smith     (*dm)->gtolhook = NULL;
460baf369e7SPeter Brune   }
461aa1993deSMatthew G Knepley   /* Destroy the work arrays */
462aa1993deSMatthew G Knepley   {
463aa1993deSMatthew G Knepley     DMWorkLink link,next;
464aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
465aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
466aa1993deSMatthew G Knepley       next = link->next;
467aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
468aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
469aa1993deSMatthew G Knepley     }
4700298fd71SBarry Smith     (*dm)->workin = NULL;
471aa1993deSMatthew G Knepley   }
472b17ce1afSJed Brown 
47352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
47452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
47552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
47652536dc3SBarry Smith 
4771a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
4781a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
4791a266240SBarry Smith   }
48087e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
48171cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
4824dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
4836bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
4846bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
4856bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
486073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
48788ed4aceSMatthew G Knepley 
48888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
48988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
4908b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
49188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
49288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
493af122d2aSMatthew G Knepley 
4946636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
4956636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
4966636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
4976636e97aSMatthew G Knepley 
498af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
499af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
500af122d2aSMatthew G Knepley   }
501af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
502732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
503f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*dm);CHKERRQ(ierr);
504732e2eb9SMatthew G Knepley 
5056bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
506435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
507732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
50847c6ae99SBarry Smith   PetscFunctionReturn(0);
50947c6ae99SBarry Smith }
51047c6ae99SBarry Smith 
51147c6ae99SBarry Smith #undef __FUNCT__
512d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
513d7bf68aeSBarry Smith /*@
514d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
515d7bf68aeSBarry Smith 
516d7bf68aeSBarry Smith     Collective on DM
517d7bf68aeSBarry Smith 
518d7bf68aeSBarry Smith     Input Parameter:
519d7bf68aeSBarry Smith .   dm - the DM object to setup
520d7bf68aeSBarry Smith 
521d7bf68aeSBarry Smith     Level: developer
522d7bf68aeSBarry Smith 
523e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
524d7bf68aeSBarry Smith 
525d7bf68aeSBarry Smith @*/
5267087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
527d7bf68aeSBarry Smith {
528d7bf68aeSBarry Smith   PetscErrorCode ierr;
529d7bf68aeSBarry Smith 
530d7bf68aeSBarry Smith   PetscFunctionBegin;
531171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5328387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
533d7bf68aeSBarry Smith   if (dm->ops->setup) {
534d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
535d7bf68aeSBarry Smith   }
5368387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
537d7bf68aeSBarry Smith   PetscFunctionReturn(0);
538d7bf68aeSBarry Smith }
539d7bf68aeSBarry Smith 
540d7bf68aeSBarry Smith #undef __FUNCT__
541d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
542d7bf68aeSBarry Smith /*@
543d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
544d7bf68aeSBarry Smith 
545d7bf68aeSBarry Smith     Collective on DM
546d7bf68aeSBarry Smith 
547d7bf68aeSBarry Smith     Input Parameter:
548d7bf68aeSBarry Smith .   dm - the DM object to set options for
549d7bf68aeSBarry Smith 
550732e2eb9SMatthew G Knepley     Options Database:
551dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
552dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
553171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
554171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
555732e2eb9SMatthew G Knepley 
556d7bf68aeSBarry Smith     Level: developer
557d7bf68aeSBarry Smith 
558e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
559d7bf68aeSBarry Smith 
560d7bf68aeSBarry Smith @*/
5617087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
562d7bf68aeSBarry Smith {
563f55f929bSMatthew G Knepley   char           typeName[256] = MATAIJ;
564ca266f36SBarry Smith   PetscBool      flg;
565d7bf68aeSBarry Smith   PetscErrorCode ierr;
566d7bf68aeSBarry Smith 
567d7bf68aeSBarry Smith   PetscFunctionBegin;
568171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5693194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
5700298fd71SBarry 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);
571f9ba7244SBarry Smith   ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
572f9ba7244SBarry Smith   if (flg) {
573f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
574f9ba7244SBarry Smith   }
5758caf3d72SBarry 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);
576073dac72SJed Brown   if (flg) {
577521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
578073dac72SJed Brown   }
5790298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
580f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
581f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
582f9ba7244SBarry Smith   }
583f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
584f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
58582fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
586146574abSBarry Smith   ierr = DMViewFromOptions(dm,NULL,"-dm_view");CHKERRQ(ierr);
587d7bf68aeSBarry Smith   PetscFunctionReturn(0);
588d7bf68aeSBarry Smith }
589d7bf68aeSBarry Smith 
590d7bf68aeSBarry Smith #undef __FUNCT__
59147c6ae99SBarry Smith #define __FUNCT__ "DMView"
592fc9bc008SSatish Balay /*@C
593aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith     Collective on DM
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith     Input Parameter:
59847c6ae99SBarry Smith +   dm - the DM object to view
59947c6ae99SBarry Smith -   v - the viewer
60047c6ae99SBarry Smith 
60147c6ae99SBarry Smith     Level: developer
60247c6ae99SBarry Smith 
603e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith @*/
6067087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
60747c6ae99SBarry Smith {
60847c6ae99SBarry Smith   PetscErrorCode ierr;
60932c0f0efSBarry Smith   PetscBool      isbinary;
61047c6ae99SBarry Smith 
61147c6ae99SBarry Smith   PetscFunctionBegin;
612171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6133014e516SBarry Smith   if (!v) {
614ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6153014e516SBarry Smith   }
61632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
61732c0f0efSBarry Smith   if (isbinary) {
61855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
61932c0f0efSBarry Smith     char     type[256];
62032c0f0efSBarry Smith 
62132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
62232c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
62332c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
62432c0f0efSBarry Smith   }
6250c010503SBarry Smith   if (dm->ops->view) {
6260c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
62747c6ae99SBarry Smith   }
62847c6ae99SBarry Smith   PetscFunctionReturn(0);
62947c6ae99SBarry Smith }
63047c6ae99SBarry Smith 
63147c6ae99SBarry Smith #undef __FUNCT__
63247c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
63347c6ae99SBarry Smith /*@
634aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
63547c6ae99SBarry Smith 
63647c6ae99SBarry Smith     Collective on DM
63747c6ae99SBarry Smith 
63847c6ae99SBarry Smith     Input Parameter:
63947c6ae99SBarry Smith .   dm - the DM object
64047c6ae99SBarry Smith 
64147c6ae99SBarry Smith     Output Parameter:
64247c6ae99SBarry Smith .   vec - the global vector
64347c6ae99SBarry Smith 
644073dac72SJed Brown     Level: beginner
64547c6ae99SBarry Smith 
646e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
64747c6ae99SBarry Smith 
64847c6ae99SBarry Smith @*/
6497087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
65047c6ae99SBarry Smith {
65147c6ae99SBarry Smith   PetscErrorCode ierr;
65247c6ae99SBarry Smith 
65347c6ae99SBarry Smith   PetscFunctionBegin;
654171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
65647c6ae99SBarry Smith   PetscFunctionReturn(0);
65747c6ae99SBarry Smith }
65847c6ae99SBarry Smith 
65947c6ae99SBarry Smith #undef __FUNCT__
66047c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
66147c6ae99SBarry Smith /*@
662aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
66347c6ae99SBarry Smith 
66447c6ae99SBarry Smith     Not Collective
66547c6ae99SBarry Smith 
66647c6ae99SBarry Smith     Input Parameter:
66747c6ae99SBarry Smith .   dm - the DM object
66847c6ae99SBarry Smith 
66947c6ae99SBarry Smith     Output Parameter:
67047c6ae99SBarry Smith .   vec - the local vector
67147c6ae99SBarry Smith 
672073dac72SJed Brown     Level: beginner
67347c6ae99SBarry Smith 
674e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
67547c6ae99SBarry Smith 
67647c6ae99SBarry Smith @*/
6777087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
67847c6ae99SBarry Smith {
67947c6ae99SBarry Smith   PetscErrorCode ierr;
68047c6ae99SBarry Smith 
68147c6ae99SBarry Smith   PetscFunctionBegin;
682171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68347c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
68447c6ae99SBarry Smith   PetscFunctionReturn(0);
68547c6ae99SBarry Smith }
68647c6ae99SBarry Smith 
68747c6ae99SBarry Smith #undef __FUNCT__
6881411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6891411c6eeSJed Brown /*@
6901411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6911411c6eeSJed Brown 
6921411c6eeSJed Brown    Collective on DM
6931411c6eeSJed Brown 
6941411c6eeSJed Brown    Input Parameter:
6951411c6eeSJed Brown .  dm - the DM that provides the mapping
6961411c6eeSJed Brown 
6971411c6eeSJed Brown    Output Parameter:
6981411c6eeSJed Brown .  ltog - the mapping
6991411c6eeSJed Brown 
7001411c6eeSJed Brown    Level: intermediate
7011411c6eeSJed Brown 
7021411c6eeSJed Brown    Notes:
7031411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
7041411c6eeSJed Brown    MatSetLocalToGlobalMapping().
7051411c6eeSJed Brown 
7061411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
7071411c6eeSJed Brown @*/
7087087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7091411c6eeSJed Brown {
7101411c6eeSJed Brown   PetscErrorCode ierr;
7111411c6eeSJed Brown 
7121411c6eeSJed Brown   PetscFunctionBegin;
7131411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7141411c6eeSJed Brown   PetscValidPointer(ltog,2);
7151411c6eeSJed Brown   if (!dm->ltogmap) {
71637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
71737d0c07bSMatthew G Knepley 
71837d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
71937d0c07bSMatthew G Knepley     if (section) {
72037d0c07bSMatthew G Knepley       PetscInt *ltog;
72137d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
72237d0c07bSMatthew G Knepley 
72337d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
72437d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
72537d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
72637d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
72737d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
72837d0c07bSMatthew G Knepley         PetscInt dof, off, c;
72937d0c07bSMatthew G Knepley 
73037d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
73137d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
73237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
73337d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
73437d0c07bSMatthew G Knepley           ltog[l] = off+c;
73537d0c07bSMatthew G Knepley         }
73637d0c07bSMatthew G Knepley       }
73737d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
7383bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
73937d0c07bSMatthew G Knepley     } else {
740184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
741184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7421411c6eeSJed Brown     }
74337d0c07bSMatthew G Knepley   }
7441411c6eeSJed Brown   *ltog = dm->ltogmap;
7451411c6eeSJed Brown   PetscFunctionReturn(0);
7461411c6eeSJed Brown }
7471411c6eeSJed Brown 
7481411c6eeSJed Brown #undef __FUNCT__
7491411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
7501411c6eeSJed Brown /*@
7511411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7521411c6eeSJed Brown 
7531411c6eeSJed Brown    Collective on DM
7541411c6eeSJed Brown 
7551411c6eeSJed Brown    Input Parameter:
7561411c6eeSJed Brown .  da - the distributed array that provides the mapping
7571411c6eeSJed Brown 
7581411c6eeSJed Brown    Output Parameter:
7591411c6eeSJed Brown .  ltog - the block mapping
7601411c6eeSJed Brown 
7611411c6eeSJed Brown    Level: intermediate
7621411c6eeSJed Brown 
7631411c6eeSJed Brown    Notes:
7641411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7651411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7661411c6eeSJed Brown 
7671411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7681411c6eeSJed Brown @*/
7697087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7701411c6eeSJed Brown {
7711411c6eeSJed Brown   PetscErrorCode ierr;
7721411c6eeSJed Brown 
7731411c6eeSJed Brown   PetscFunctionBegin;
7741411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7751411c6eeSJed Brown   PetscValidPointer(ltog,2);
7761411c6eeSJed Brown   if (!dm->ltogmapb) {
7771411c6eeSJed Brown     PetscInt bs;
7781411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7791411c6eeSJed Brown     if (bs > 1) {
780184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmappingblock) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
781184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7821411c6eeSJed Brown     } else {
7831411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7841411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7851411c6eeSJed Brown     }
7861411c6eeSJed Brown   }
7871411c6eeSJed Brown   *ltog = dm->ltogmapb;
7881411c6eeSJed Brown   PetscFunctionReturn(0);
7891411c6eeSJed Brown }
7901411c6eeSJed Brown 
7911411c6eeSJed Brown #undef __FUNCT__
7921411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7931411c6eeSJed Brown /*@
7941411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7951411c6eeSJed Brown 
7961411c6eeSJed Brown    Not Collective
7971411c6eeSJed Brown 
7981411c6eeSJed Brown    Input Parameter:
7991411c6eeSJed Brown .  dm - the DM with block structure
8001411c6eeSJed Brown 
8011411c6eeSJed Brown    Output Parameter:
8021411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
8031411c6eeSJed Brown 
8041411c6eeSJed Brown    Level: intermediate
8051411c6eeSJed Brown 
8061411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
8071411c6eeSJed Brown @*/
8087087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
8091411c6eeSJed Brown {
8101411c6eeSJed Brown   PetscFunctionBegin;
8111411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8121411c6eeSJed Brown   PetscValidPointer(bs,2);
8131411c6eeSJed 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");
8141411c6eeSJed Brown   *bs = dm->bs;
8151411c6eeSJed Brown   PetscFunctionReturn(0);
8161411c6eeSJed Brown }
8171411c6eeSJed Brown 
8181411c6eeSJed Brown #undef __FUNCT__
819e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
82047c6ae99SBarry Smith /*@
821e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
82247c6ae99SBarry Smith 
82347c6ae99SBarry Smith     Collective on DM
82447c6ae99SBarry Smith 
82547c6ae99SBarry Smith     Input Parameter:
82647c6ae99SBarry Smith +   dm1 - the DM object
82747c6ae99SBarry Smith -   dm2 - the second, finer DM object
82847c6ae99SBarry Smith 
82947c6ae99SBarry Smith     Output Parameter:
83047c6ae99SBarry Smith +  mat - the interpolation
83147c6ae99SBarry Smith -  vec - the scaling (optional)
83247c6ae99SBarry Smith 
83347c6ae99SBarry Smith     Level: developer
83447c6ae99SBarry Smith 
83585afcc9aSBarry 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
83685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
837d52bd9f3SBarry Smith 
8381f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
839d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
84085afcc9aSBarry Smith 
84185afcc9aSBarry Smith 
842e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith @*/
845e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
84647c6ae99SBarry Smith {
84747c6ae99SBarry Smith   PetscErrorCode ierr;
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith   PetscFunctionBegin;
850171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
851171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
85225296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
85347c6ae99SBarry Smith   PetscFunctionReturn(0);
85447c6ae99SBarry Smith }
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith #undef __FUNCT__
857e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
85847c6ae99SBarry Smith /*@
859e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith     Collective on DM
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith     Input Parameter:
86447c6ae99SBarry Smith +   dm1 - the DM object
86547c6ae99SBarry Smith -   dm2 - the second, finer DM object
86647c6ae99SBarry Smith 
86747c6ae99SBarry Smith     Output Parameter:
86847c6ae99SBarry Smith .   ctx - the injection
86947c6ae99SBarry Smith 
87047c6ae99SBarry Smith     Level: developer
87147c6ae99SBarry Smith 
87285afcc9aSBarry 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
87385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
87485afcc9aSBarry Smith 
875e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith @*/
878e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
87947c6ae99SBarry Smith {
88047c6ae99SBarry Smith   PetscErrorCode ierr;
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith   PetscFunctionBegin;
883171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
884171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
88547c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
88647c6ae99SBarry Smith   PetscFunctionReturn(0);
88747c6ae99SBarry Smith }
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith #undef __FUNCT__
890e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
891*b412c318SBarry Smith /*@
892*b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
89347c6ae99SBarry Smith 
89447c6ae99SBarry Smith     Collective on DM
89547c6ae99SBarry Smith 
89647c6ae99SBarry Smith     Input Parameter:
89747c6ae99SBarry Smith +   dm - the DM object
898*b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
89947c6ae99SBarry Smith 
90047c6ae99SBarry Smith     Output Parameter:
90147c6ae99SBarry Smith .   coloring - the coloring
90247c6ae99SBarry Smith 
90347c6ae99SBarry Smith     Level: developer
90447c6ae99SBarry Smith 
905*b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
90647c6ae99SBarry Smith 
907aab9d709SJed Brown @*/
908*b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
90947c6ae99SBarry Smith {
91047c6ae99SBarry Smith   PetscErrorCode ierr;
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith   PetscFunctionBegin;
913171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
914ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
915*b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
91647c6ae99SBarry Smith   PetscFunctionReturn(0);
91747c6ae99SBarry Smith }
91847c6ae99SBarry Smith 
91947c6ae99SBarry Smith #undef __FUNCT__
920950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
921*b412c318SBarry Smith /*@
922950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
92347c6ae99SBarry Smith 
92447c6ae99SBarry Smith     Collective on DM
92547c6ae99SBarry Smith 
92647c6ae99SBarry Smith     Input Parameter:
927*b412c318SBarry Smith .   dm - the DM object
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith     Output Parameter:
93047c6ae99SBarry Smith .   mat - the empty Jacobian
93147c6ae99SBarry Smith 
932073dac72SJed Brown     Level: beginner
93347c6ae99SBarry Smith 
93494013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
93594013140SBarry Smith        do not need to do it yourself.
93694013140SBarry Smith 
93794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
938aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
93994013140SBarry Smith 
94094013140SBarry 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
94194013140SBarry Smith        internally by PETSc.
94294013140SBarry Smith 
94394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
944aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
94594013140SBarry Smith 
946*b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
94747c6ae99SBarry Smith 
948aab9d709SJed Brown @*/
949*b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
95047c6ae99SBarry Smith {
95147c6ae99SBarry Smith   PetscErrorCode ierr;
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith   PetscFunctionBegin;
954171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
955519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
956607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
957235683edSBarry Smith #endif
958c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
959c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
960*b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
96147c6ae99SBarry Smith   PetscFunctionReturn(0);
96247c6ae99SBarry Smith }
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith #undef __FUNCT__
965732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
966732e2eb9SMatthew G Knepley /*@
967950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
968732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
969732e2eb9SMatthew G Knepley 
970732e2eb9SMatthew G Knepley   Logically Collective on DMDA
971732e2eb9SMatthew G Knepley 
972732e2eb9SMatthew G Knepley   Input Parameter:
973732e2eb9SMatthew G Knepley + dm - the DM
974732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
975732e2eb9SMatthew G Knepley 
976732e2eb9SMatthew G Knepley   Level: developer
977950540a4SJed Brown .seealso DMCreateMatrix()
978732e2eb9SMatthew G Knepley @*/
979732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
980732e2eb9SMatthew G Knepley {
981732e2eb9SMatthew G Knepley   PetscFunctionBegin;
982732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
983732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
984732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
985732e2eb9SMatthew G Knepley }
986732e2eb9SMatthew G Knepley 
987732e2eb9SMatthew G Knepley #undef __FUNCT__
988a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
989a89ea682SMatthew G Knepley /*@C
990aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
991a89ea682SMatthew G Knepley 
992a89ea682SMatthew G Knepley   Not Collective
993a89ea682SMatthew G Knepley 
994a89ea682SMatthew G Knepley   Input Parameters:
995a89ea682SMatthew G Knepley + dm - the DM object
996aa1993deSMatthew G Knepley . count - The minium size
997aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
998a89ea682SMatthew G Knepley 
999a89ea682SMatthew G Knepley   Output Parameter:
1000a89ea682SMatthew G Knepley . array - the work array
1001a89ea682SMatthew G Knepley 
1002a89ea682SMatthew G Knepley   Level: developer
1003a89ea682SMatthew G Knepley 
1004a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1005a89ea682SMatthew G Knepley @*/
1006aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1007a89ea682SMatthew G Knepley {
1008a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1009aa1993deSMatthew G Knepley   DMWorkLink     link;
1010aa1993deSMatthew G Knepley   size_t         size;
1011a89ea682SMatthew G Knepley 
1012a89ea682SMatthew G Knepley   PetscFunctionBegin;
1013a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1014aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1015aa1993deSMatthew G Knepley   if (dm->workin) {
1016aa1993deSMatthew G Knepley     link       = dm->workin;
1017aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1018aa1993deSMatthew G Knepley   } else {
1019aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
1020a89ea682SMatthew G Knepley   }
1021aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1022aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1023aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1024aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1025aa1993deSMatthew G Knepley     link->bytes = size*count;
1026aa1993deSMatthew G Knepley   }
1027aa1993deSMatthew G Knepley   link->next   = dm->workout;
1028aa1993deSMatthew G Knepley   dm->workout  = link;
1029aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1030a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1031a89ea682SMatthew G Knepley }
1032a89ea682SMatthew G Knepley 
1033aa1993deSMatthew G Knepley #undef __FUNCT__
1034aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1035aa1993deSMatthew G Knepley /*@C
1036aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1037aa1993deSMatthew G Knepley 
1038aa1993deSMatthew G Knepley   Not Collective
1039aa1993deSMatthew G Knepley 
1040aa1993deSMatthew G Knepley   Input Parameters:
1041aa1993deSMatthew G Knepley + dm - the DM object
1042aa1993deSMatthew G Knepley . count - The minium size
1043aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1044aa1993deSMatthew G Knepley 
1045aa1993deSMatthew G Knepley   Output Parameter:
1046aa1993deSMatthew G Knepley . array - the work array
1047aa1993deSMatthew G Knepley 
1048aa1993deSMatthew G Knepley   Level: developer
1049aa1993deSMatthew G Knepley 
1050aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1051aa1993deSMatthew G Knepley @*/
1052aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1053aa1993deSMatthew G Knepley {
1054aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1055aa1993deSMatthew G Knepley 
1056aa1993deSMatthew G Knepley   PetscFunctionBegin;
1057aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1058aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1059aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1060aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1061aa1993deSMatthew G Knepley       *p           = link->next;
1062aa1993deSMatthew G Knepley       link->next   = dm->workin;
1063aa1993deSMatthew G Knepley       dm->workin   = link;
10640298fd71SBarry Smith       *(void**)mem = NULL;
1065aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1066aa1993deSMatthew G Knepley     }
1067aa1993deSMatthew G Knepley   }
1068aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1069aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1070aa1993deSMatthew G Knepley }
1071e7c4fc90SDmitry Karpeev 
1072e7c4fc90SDmitry Karpeev #undef __FUNCT__
1073435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1074435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1075435a35e8SMatthew G Knepley {
1076435a35e8SMatthew G Knepley   PetscFunctionBegin;
1077435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
107882f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1079435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1080435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1081435a35e8SMatthew G Knepley }
1082435a35e8SMatthew G Knepley 
1083435a35e8SMatthew G Knepley #undef __FUNCT__
10844d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10854f3b5142SJed Brown /*@C
10864d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10874d343eeaSMatthew G Knepley 
10884d343eeaSMatthew G Knepley   Not collective
10894d343eeaSMatthew G Knepley 
10904d343eeaSMatthew G Knepley   Input Parameter:
10914d343eeaSMatthew G Knepley . dm - the DM object
10924d343eeaSMatthew G Knepley 
10934d343eeaSMatthew G Knepley   Output Parameters:
10940298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10950298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10960298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10974d343eeaSMatthew G Knepley 
10984d343eeaSMatthew G Knepley   Level: intermediate
10994d343eeaSMatthew G Knepley 
110021c9b008SJed Brown   Notes:
110121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
110221c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
110321c9b008SJed Brown   PetscFree().
110421c9b008SJed Brown 
11054d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11064d343eeaSMatthew G Knepley @*/
110737d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11084d343eeaSMatthew G Knepley {
110937d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11104d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11114d343eeaSMatthew G Knepley 
11124d343eeaSMatthew G Knepley   PetscFunctionBegin;
11134d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
111469ca1f37SDmitry Karpeev   if (numFields) {
111569ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
111669ca1f37SDmitry Karpeev     *numFields = 0;
111769ca1f37SDmitry Karpeev   }
111837d0c07bSMatthew G Knepley   if (fieldNames) {
111937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11200298fd71SBarry Smith     *fieldNames = NULL;
112169ca1f37SDmitry Karpeev   }
112269ca1f37SDmitry Karpeev   if (fields) {
112369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11240298fd71SBarry Smith     *fields = NULL;
112569ca1f37SDmitry Karpeev   }
112637d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
112737d0c07bSMatthew G Knepley   if (section) {
112837d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
112937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
113037d0c07bSMatthew G Knepley 
113137d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
113237d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
113337d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt*,&fieldIndices);CHKERRQ(ierr);
113437d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
113537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
113637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113737d0c07bSMatthew G Knepley     }
113837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
113937d0c07bSMatthew G Knepley       PetscInt gdof;
114037d0c07bSMatthew G Knepley 
114137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114237d0c07bSMatthew G Knepley       if (gdof > 0) {
114337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
114537d0c07bSMatthew G Knepley 
114637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
114737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
114837d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
114937d0c07bSMatthew G Knepley         }
115037d0c07bSMatthew G Knepley       }
115137d0c07bSMatthew G Knepley     }
115237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
115337d0c07bSMatthew G Knepley       ierr          = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
115437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
115537d0c07bSMatthew G Knepley     }
115637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
115737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
115837d0c07bSMatthew G Knepley 
115937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
116037d0c07bSMatthew G Knepley       if (gdof > 0) {
116137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
116237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
116337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
116437d0c07bSMatthew G Knepley 
116537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
116637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
116737d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
116837d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
116937d0c07bSMatthew G Knepley           }
117037d0c07bSMatthew G Knepley         }
117137d0c07bSMatthew G Knepley       }
117237d0c07bSMatthew G Knepley     }
11738865f1eaSKarl Rupp     if (numFields) *numFields = nF;
117437d0c07bSMatthew G Knepley     if (fieldNames) {
117537d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char*), fieldNames);CHKERRQ(ierr);
117637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
117737d0c07bSMatthew G Knepley         const char *fieldName;
117837d0c07bSMatthew G Knepley 
117937d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
118037d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
118137d0c07bSMatthew G Knepley       }
118237d0c07bSMatthew G Knepley     }
118337d0c07bSMatthew G Knepley     if (fields) {
118437d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
118537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
118682f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
118737d0c07bSMatthew G Knepley       }
118837d0c07bSMatthew G Knepley     }
118937d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11908865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11918865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
119269ca1f37SDmitry Karpeev   }
11934d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11944d343eeaSMatthew G Knepley }
11954d343eeaSMatthew G Knepley 
119616621825SDmitry Karpeev 
119716621825SDmitry Karpeev #undef __FUNCT__
119816621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
119916621825SDmitry Karpeev /*@C
120016621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
120116621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
120216621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1203e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1204e7c4fc90SDmitry Karpeev 
1205e7c4fc90SDmitry Karpeev   Not collective
1206e7c4fc90SDmitry Karpeev 
1207e7c4fc90SDmitry Karpeev   Input Parameter:
1208e7c4fc90SDmitry Karpeev . dm - the DM object
1209e7c4fc90SDmitry Karpeev 
1210e7c4fc90SDmitry Karpeev   Output Parameters:
12110298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
12120298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
12130298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12140298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1215e7c4fc90SDmitry Karpeev 
1216e7c4fc90SDmitry Karpeev   Level: intermediate
1217e7c4fc90SDmitry Karpeev 
1218e7c4fc90SDmitry Karpeev   Notes:
1219e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1220e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1221e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1222e7c4fc90SDmitry Karpeev 
1223e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1224e7c4fc90SDmitry Karpeev @*/
122516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1226e7c4fc90SDmitry Karpeev {
1227e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1228e7c4fc90SDmitry Karpeev 
1229e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1230e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12318865f1eaSKarl Rupp   if (len) {
12328865f1eaSKarl Rupp     PetscValidPointer(len,2);
12338865f1eaSKarl Rupp     *len = 0;
12348865f1eaSKarl Rupp   }
12358865f1eaSKarl Rupp   if (namelist) {
12368865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12378865f1eaSKarl Rupp     *namelist = 0;
12388865f1eaSKarl Rupp   }
12398865f1eaSKarl Rupp   if (islist) {
12408865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12418865f1eaSKarl Rupp     *islist = 0;
12428865f1eaSKarl Rupp   }
12438865f1eaSKarl Rupp   if (dmlist) {
12448865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12458865f1eaSKarl Rupp     *dmlist = 0;
12468865f1eaSKarl Rupp   }
1247f3f0edfdSDmitry Karpeev   /*
1248f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1249f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1250f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1251f3f0edfdSDmitry Karpeev    */
1252ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
125316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1254435a35e8SMatthew G Knepley     PetscSection section;
1255435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1256435a35e8SMatthew G Knepley 
1257435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1258435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1259435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1260435a35e8SMatthew G Knepley       *len = numFields;
1261435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1262435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1263435a35e8SMatthew G Knepley         const char *fieldName;
1264435a35e8SMatthew G Knepley 
1265435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1266435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1267435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1268435a35e8SMatthew G Knepley       }
1269435a35e8SMatthew G Knepley     } else {
127069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1271e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12720298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1273e7c4fc90SDmitry Karpeev     }
12748865f1eaSKarl Rupp   } else {
127516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
127616621825SDmitry Karpeev   }
127716621825SDmitry Karpeev   PetscFunctionReturn(0);
127816621825SDmitry Karpeev }
127916621825SDmitry Karpeev 
128016621825SDmitry Karpeev #undef __FUNCT__
1281435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1282435a35e8SMatthew G Knepley /*@C
1283435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1284435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1285435a35e8SMatthew G Knepley 
1286435a35e8SMatthew G Knepley   Not collective
1287435a35e8SMatthew G Knepley 
1288435a35e8SMatthew G Knepley   Input Parameters:
1289435a35e8SMatthew G Knepley + dm - the DM object
1290435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12910298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1292435a35e8SMatthew G Knepley 
1293435a35e8SMatthew G Knepley   Output Parameters:
1294435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1295435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1296435a35e8SMatthew G Knepley 
1297435a35e8SMatthew G Knepley   Level: intermediate
1298435a35e8SMatthew G Knepley 
1299435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1300435a35e8SMatthew G Knepley @*/
1301435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1302435a35e8SMatthew G Knepley {
1303435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1304435a35e8SMatthew G Knepley 
1305435a35e8SMatthew G Knepley   PetscFunctionBegin;
1306435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1307435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
13088865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
13098865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1310435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1311435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
131282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1313435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1314435a35e8SMatthew G Knepley }
1315435a35e8SMatthew G Knepley 
131616621825SDmitry Karpeev 
131716621825SDmitry Karpeev #undef __FUNCT__
131816621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
131916621825SDmitry Karpeev /*@C
13208d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13218d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13228d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13238d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13248d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
132516621825SDmitry Karpeev 
132616621825SDmitry Karpeev   Not collective
132716621825SDmitry Karpeev 
132816621825SDmitry Karpeev   Input Parameter:
132916621825SDmitry Karpeev . dm - the DM object
133016621825SDmitry Karpeev 
133116621825SDmitry Karpeev   Output Parameters:
13320298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13330298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13340298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13350298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13360298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
133716621825SDmitry Karpeev 
133816621825SDmitry Karpeev   Level: intermediate
133916621825SDmitry Karpeev 
134016621825SDmitry Karpeev   Notes:
134116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
134216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
134316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
134416621825SDmitry Karpeev 
13458d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
134616621825SDmitry Karpeev @*/
13478d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
134816621825SDmitry Karpeev {
134916621825SDmitry Karpeev   PetscErrorCode      ierr;
1350be081cd6SPeter Brune   DMSubDomainHookLink link;
1351be081cd6SPeter Brune   PetscInt            i,l;
135216621825SDmitry Karpeev 
135316621825SDmitry Karpeev   PetscFunctionBegin;
135416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
135514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13560298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13570298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13580298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13590298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1360f3f0edfdSDmitry Karpeev   /*
1361f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1362f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1363f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1364f3f0edfdSDmitry Karpeev    */
1365ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
136616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1367be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
136814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
136914a18fd3SPeter Brune     if (dmlist) {
1370be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1371be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1372be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1373be081cd6SPeter Brune         }
1374d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1375e7c4fc90SDmitry Karpeev       }
137614a18fd3SPeter Brune     }
137714a18fd3SPeter Brune     if (len) *len = l;
137814a18fd3SPeter Brune   }
1379e30e807fSPeter Brune   PetscFunctionReturn(0);
1380e30e807fSPeter Brune }
1381e30e807fSPeter Brune 
1382e30e807fSPeter Brune 
1383e30e807fSPeter Brune #undef __FUNCT__
1384e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1385e30e807fSPeter Brune /*@C
1386e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1387e30e807fSPeter Brune 
1388e30e807fSPeter Brune   Not collective
1389e30e807fSPeter Brune 
1390e30e807fSPeter Brune   Input Parameters:
1391e30e807fSPeter Brune + dm - the DM object
1392e30e807fSPeter Brune . n  - the number of subdomain scatters
1393e30e807fSPeter Brune - subdms - the local subdomains
1394e30e807fSPeter Brune 
1395e30e807fSPeter Brune   Output Parameters:
1396e30e807fSPeter Brune + n     - the number of scatters returned
1397e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1398e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1399e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1400e30e807fSPeter Brune 
1401e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1402e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1403e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1404e30e807fSPeter Brune   solution and residual data.
1405e30e807fSPeter Brune 
1406e30e807fSPeter Brune   Level: developer
1407e30e807fSPeter Brune 
1408e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1409e30e807fSPeter Brune @*/
1410e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1411e30e807fSPeter Brune {
1412e30e807fSPeter Brune   PetscErrorCode ierr;
1413e30e807fSPeter Brune 
1414e30e807fSPeter Brune   PetscFunctionBegin;
1415e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1416e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1417e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1418e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
141982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1420e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1421e7c4fc90SDmitry Karpeev }
1422e7c4fc90SDmitry Karpeev 
1423731c8d9eSDmitry Karpeev #undef __FUNCT__
142447c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
142547c6ae99SBarry Smith /*@
142647c6ae99SBarry Smith   DMRefine - Refines a DM object
142747c6ae99SBarry Smith 
142847c6ae99SBarry Smith   Collective on DM
142947c6ae99SBarry Smith 
143047c6ae99SBarry Smith   Input Parameter:
143147c6ae99SBarry Smith + dm   - the DM object
143291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
143347c6ae99SBarry Smith 
143447c6ae99SBarry Smith   Output Parameter:
14350298fd71SBarry Smith . dmf - the refined DM, or NULL
1436ae0a1c52SMatthew G Knepley 
14370298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
143847c6ae99SBarry Smith 
143947c6ae99SBarry Smith   Level: developer
144047c6ae99SBarry Smith 
1441e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
144247c6ae99SBarry Smith @*/
14437087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
144447c6ae99SBarry Smith {
144547c6ae99SBarry Smith   PetscErrorCode   ierr;
1446c833c3b5SJed Brown   DMRefineHookLink link;
144747c6ae99SBarry Smith 
144847c6ae99SBarry Smith   PetscFunctionBegin;
1449732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
145047c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14514057135bSMatthew G Knepley   if (*dmf) {
145243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14538865f1eaSKarl Rupp 
14548cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14558865f1eaSKarl Rupp 
1456644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14570598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1458656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14598865f1eaSKarl Rupp 
1460e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1461c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14628865f1eaSKarl Rupp       if (link->refinehook) {
14638865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14648865f1eaSKarl Rupp       }
1465c833c3b5SJed Brown     }
1466c833c3b5SJed Brown   }
1467c833c3b5SJed Brown   PetscFunctionReturn(0);
1468c833c3b5SJed Brown }
1469c833c3b5SJed Brown 
1470c833c3b5SJed Brown #undef __FUNCT__
1471c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1472bb9467b5SJed Brown /*@C
1473c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1474c833c3b5SJed Brown 
1475c833c3b5SJed Brown    Logically Collective
1476c833c3b5SJed Brown 
1477c833c3b5SJed Brown    Input Arguments:
1478c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1479c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1480c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14810298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1482c833c3b5SJed Brown 
1483c833c3b5SJed Brown    Calling sequence of refinehook:
1484c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1485c833c3b5SJed Brown 
1486c833c3b5SJed Brown +  coarse - coarse level DM
1487c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1488c833c3b5SJed Brown -  ctx - optional user-defined function context
1489c833c3b5SJed Brown 
1490c833c3b5SJed Brown    Calling sequence for interphook:
1491c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1492c833c3b5SJed Brown 
1493c833c3b5SJed Brown +  coarse - coarse level DM
1494c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1495c833c3b5SJed Brown .  fine - fine level DM to update
1496c833c3b5SJed Brown -  ctx - optional user-defined function context
1497c833c3b5SJed Brown 
1498c833c3b5SJed Brown    Level: advanced
1499c833c3b5SJed Brown 
1500c833c3b5SJed Brown    Notes:
1501c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1502c833c3b5SJed Brown 
1503c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1504c833c3b5SJed Brown 
1505bb9467b5SJed Brown    This function is currently not available from Fortran.
1506bb9467b5SJed Brown 
1507c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1508c833c3b5SJed Brown @*/
1509c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1510c833c3b5SJed Brown {
1511c833c3b5SJed Brown   PetscErrorCode   ierr;
1512c833c3b5SJed Brown   DMRefineHookLink link,*p;
1513c833c3b5SJed Brown 
1514c833c3b5SJed Brown   PetscFunctionBegin;
1515c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1516c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1517c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1518c833c3b5SJed Brown   link->refinehook = refinehook;
1519c833c3b5SJed Brown   link->interphook = interphook;
1520c833c3b5SJed Brown   link->ctx        = ctx;
15210298fd71SBarry Smith   link->next       = NULL;
1522c833c3b5SJed Brown   *p               = link;
1523c833c3b5SJed Brown   PetscFunctionReturn(0);
1524c833c3b5SJed Brown }
1525c833c3b5SJed Brown 
1526c833c3b5SJed Brown #undef __FUNCT__
1527c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1528c833c3b5SJed Brown /*@
1529c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1530c833c3b5SJed Brown 
1531c833c3b5SJed Brown    Collective if any hooks are
1532c833c3b5SJed Brown 
1533c833c3b5SJed Brown    Input Arguments:
1534c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1535c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1536c833c3b5SJed Brown -  fine - finer DM to update
1537c833c3b5SJed Brown 
1538c833c3b5SJed Brown    Level: developer
1539c833c3b5SJed Brown 
1540c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1541c833c3b5SJed Brown @*/
1542c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1543c833c3b5SJed Brown {
1544c833c3b5SJed Brown   PetscErrorCode   ierr;
1545c833c3b5SJed Brown   DMRefineHookLink link;
1546c833c3b5SJed Brown 
1547c833c3b5SJed Brown   PetscFunctionBegin;
1548c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15498865f1eaSKarl Rupp     if (link->interphook) {
15508865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15518865f1eaSKarl Rupp     }
15524057135bSMatthew G Knepley   }
155347c6ae99SBarry Smith   PetscFunctionReturn(0);
155447c6ae99SBarry Smith }
155547c6ae99SBarry Smith 
155647c6ae99SBarry Smith #undef __FUNCT__
1557eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1558eb3f98d2SBarry Smith /*@
1559eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1560eb3f98d2SBarry Smith 
1561eb3f98d2SBarry Smith     Not Collective
1562eb3f98d2SBarry Smith 
1563eb3f98d2SBarry Smith     Input Parameter:
1564eb3f98d2SBarry Smith .   dm - the DM object
1565eb3f98d2SBarry Smith 
1566eb3f98d2SBarry Smith     Output Parameter:
1567eb3f98d2SBarry Smith .   level - number of refinements
1568eb3f98d2SBarry Smith 
1569eb3f98d2SBarry Smith     Level: developer
1570eb3f98d2SBarry Smith 
15716a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1572eb3f98d2SBarry Smith 
1573eb3f98d2SBarry Smith @*/
1574eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1575eb3f98d2SBarry Smith {
1576eb3f98d2SBarry Smith   PetscFunctionBegin;
1577eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1578eb3f98d2SBarry Smith   *level = dm->levelup;
1579eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1580eb3f98d2SBarry Smith }
1581eb3f98d2SBarry Smith 
1582eb3f98d2SBarry Smith #undef __FUNCT__
1583baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1584bb9467b5SJed Brown /*@C
1585baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1586baf369e7SPeter Brune 
1587baf369e7SPeter Brune    Logically Collective
1588baf369e7SPeter Brune 
1589baf369e7SPeter Brune    Input Arguments:
1590baf369e7SPeter Brune +  dm - the DM
1591baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1592baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15930298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1594baf369e7SPeter Brune 
1595baf369e7SPeter Brune    Calling sequence for beginhook:
1596baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1597baf369e7SPeter Brune 
1598baf369e7SPeter Brune +  dm - global DM
1599baf369e7SPeter Brune .  g - global vector
1600baf369e7SPeter Brune .  mode - mode
1601baf369e7SPeter Brune .  l - local vector
1602baf369e7SPeter Brune -  ctx - optional user-defined function context
1603baf369e7SPeter Brune 
1604baf369e7SPeter Brune 
1605baf369e7SPeter Brune    Calling sequence for endhook:
1606ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1607baf369e7SPeter Brune 
1608baf369e7SPeter Brune +  global - global DM
1609baf369e7SPeter Brune -  ctx - optional user-defined function context
1610baf369e7SPeter Brune 
1611baf369e7SPeter Brune    Level: advanced
1612baf369e7SPeter Brune 
1613baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1614baf369e7SPeter Brune @*/
1615baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1616baf369e7SPeter Brune {
1617baf369e7SPeter Brune   PetscErrorCode          ierr;
1618baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1619baf369e7SPeter Brune 
1620baf369e7SPeter Brune   PetscFunctionBegin;
1621baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1622baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1623baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1624baf369e7SPeter Brune   link->beginhook = beginhook;
1625baf369e7SPeter Brune   link->endhook   = endhook;
1626baf369e7SPeter Brune   link->ctx       = ctx;
16270298fd71SBarry Smith   link->next      = NULL;
1628baf369e7SPeter Brune   *p              = link;
1629baf369e7SPeter Brune   PetscFunctionReturn(0);
1630baf369e7SPeter Brune }
1631baf369e7SPeter Brune 
1632baf369e7SPeter Brune #undef __FUNCT__
163347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
163447c6ae99SBarry Smith /*@
163547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
163647c6ae99SBarry Smith 
163747c6ae99SBarry Smith     Neighbor-wise Collective on DM
163847c6ae99SBarry Smith 
163947c6ae99SBarry Smith     Input Parameters:
164047c6ae99SBarry Smith +   dm - the DM object
164147c6ae99SBarry Smith .   g - the global vector
164247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
164347c6ae99SBarry Smith -   l - the local vector
164447c6ae99SBarry Smith 
164547c6ae99SBarry Smith 
164647c6ae99SBarry Smith     Level: beginner
164747c6ae99SBarry Smith 
1648e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
164947c6ae99SBarry Smith 
165047c6ae99SBarry Smith @*/
16517087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
165247c6ae99SBarry Smith {
16537128ae9fSMatthew G Knepley   PetscSF                 sf;
165447c6ae99SBarry Smith   PetscErrorCode          ierr;
1655baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
165647c6ae99SBarry Smith 
165747c6ae99SBarry Smith   PetscFunctionBegin;
1658171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1659baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16608865f1eaSKarl Rupp     if (link->beginhook) {
16618865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16628865f1eaSKarl Rupp     }
1663baf369e7SPeter Brune   }
16647128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16657128ae9fSMatthew G Knepley   if (sf) {
16667128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16677128ae9fSMatthew G Knepley 
166882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16697128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16707128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16717128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16727128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16737128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16747128ae9fSMatthew G Knepley   } else {
1675843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16767128ae9fSMatthew G Knepley   }
167747c6ae99SBarry Smith   PetscFunctionReturn(0);
167847c6ae99SBarry Smith }
167947c6ae99SBarry Smith 
168047c6ae99SBarry Smith #undef __FUNCT__
168147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
168247c6ae99SBarry Smith /*@
168347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
168447c6ae99SBarry Smith 
168547c6ae99SBarry Smith     Neighbor-wise Collective on DM
168647c6ae99SBarry Smith 
168747c6ae99SBarry Smith     Input Parameters:
168847c6ae99SBarry Smith +   dm - the DM object
168947c6ae99SBarry Smith .   g - the global vector
169047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
169147c6ae99SBarry Smith -   l - the local vector
169247c6ae99SBarry Smith 
169347c6ae99SBarry Smith 
169447c6ae99SBarry Smith     Level: beginner
169547c6ae99SBarry Smith 
1696e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
169747c6ae99SBarry Smith 
169847c6ae99SBarry Smith @*/
16997087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
170047c6ae99SBarry Smith {
17017128ae9fSMatthew G Knepley   PetscSF                 sf;
170247c6ae99SBarry Smith   PetscErrorCode          ierr;
170361a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1704baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
170547c6ae99SBarry Smith 
170647c6ae99SBarry Smith   PetscFunctionBegin;
1707171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17087128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17097128ae9fSMatthew G Knepley   if (sf) {
171082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17117128ae9fSMatthew G Knepley 
17127128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17137128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17147128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17157128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17167128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17177128ae9fSMatthew G Knepley   } else {
1718843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17197128ae9fSMatthew G Knepley   }
1720baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1721baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1722baf369e7SPeter Brune   }
172347c6ae99SBarry Smith   PetscFunctionReturn(0);
172447c6ae99SBarry Smith }
172547c6ae99SBarry Smith 
172647c6ae99SBarry Smith #undef __FUNCT__
17279a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
172847c6ae99SBarry Smith /*@
17299a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
17309a42bb27SBarry Smith 
17319a42bb27SBarry Smith     Neighbor-wise Collective on DM
17329a42bb27SBarry Smith 
17339a42bb27SBarry Smith     Input Parameters:
17349a42bb27SBarry Smith +   dm - the DM object
1735f6813fd5SJed Brown .   l - the local vector
17369a42bb27SBarry 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
17379a42bb27SBarry Smith            base point.
1738f6813fd5SJed Brown - - the global vector
17399a42bb27SBarry Smith 
17409a42bb27SBarry 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
17419a42bb27SBarry 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
17429a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17439a42bb27SBarry Smith 
17449a42bb27SBarry Smith     Level: beginner
17459a42bb27SBarry Smith 
1746e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17479a42bb27SBarry Smith 
17489a42bb27SBarry Smith @*/
17497087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17509a42bb27SBarry Smith {
17517128ae9fSMatthew G Knepley   PetscSF        sf;
17529a42bb27SBarry Smith   PetscErrorCode ierr;
17539a42bb27SBarry Smith 
17549a42bb27SBarry Smith   PetscFunctionBegin;
1755171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17567128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17577128ae9fSMatthew G Knepley   if (sf) {
17587128ae9fSMatthew G Knepley     MPI_Op      op;
17597128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17607128ae9fSMatthew G Knepley 
17617128ae9fSMatthew G Knepley     switch (mode) {
17627128ae9fSMatthew G Knepley     case INSERT_VALUES:
17637128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17648bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17657128ae9fSMatthew G Knepley     case ADD_VALUES:
17667128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17677128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17687128ae9fSMatthew G Knepley     default:
176982f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17707128ae9fSMatthew G Knepley     }
17717128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17727128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17737128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17747128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17757128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17767128ae9fSMatthew G Knepley   } else {
1777843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17787128ae9fSMatthew G Knepley   }
17799a42bb27SBarry Smith   PetscFunctionReturn(0);
17809a42bb27SBarry Smith }
17819a42bb27SBarry Smith 
17829a42bb27SBarry Smith #undef __FUNCT__
17839a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17849a42bb27SBarry Smith /*@
17859a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
178647c6ae99SBarry Smith 
178747c6ae99SBarry Smith     Neighbor-wise Collective on DM
178847c6ae99SBarry Smith 
178947c6ae99SBarry Smith     Input Parameters:
179047c6ae99SBarry Smith +   dm - the DM object
1791f6813fd5SJed Brown .   l - the local vector
179247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1793f6813fd5SJed Brown -   g - the global vector
179447c6ae99SBarry Smith 
179547c6ae99SBarry Smith 
179647c6ae99SBarry Smith     Level: beginner
179747c6ae99SBarry Smith 
1798e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
179947c6ae99SBarry Smith 
180047c6ae99SBarry Smith @*/
18017087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
180247c6ae99SBarry Smith {
18037128ae9fSMatthew G Knepley   PetscSF        sf;
180447c6ae99SBarry Smith   PetscErrorCode ierr;
180547c6ae99SBarry Smith 
180647c6ae99SBarry Smith   PetscFunctionBegin;
1807171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18087128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18097128ae9fSMatthew G Knepley   if (sf) {
18107128ae9fSMatthew G Knepley     MPI_Op      op;
18117128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
18127128ae9fSMatthew G Knepley 
18137128ae9fSMatthew G Knepley     switch (mode) {
18147128ae9fSMatthew G Knepley     case INSERT_VALUES:
18157128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18168bfbc91cSJed Brown       op = MPIU_REPLACE; break;
18177128ae9fSMatthew G Knepley     case ADD_VALUES:
18187128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18197128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18207128ae9fSMatthew G Knepley     default:
182182f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18227128ae9fSMatthew G Knepley     }
18237128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18247128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18257128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18267128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18277128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18287128ae9fSMatthew G Knepley   } else {
1829843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18307128ae9fSMatthew G Knepley   }
183147c6ae99SBarry Smith   PetscFunctionReturn(0);
183247c6ae99SBarry Smith }
183347c6ae99SBarry Smith 
183447c6ae99SBarry Smith #undef __FUNCT__
1835f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
1836f089877aSRichard Tran Mills /*@
1837bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
1838bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1839d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
1840f089877aSRichard Tran Mills 
1841bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1842f089877aSRichard Tran Mills 
1843f089877aSRichard Tran Mills    Input Parameters:
1844f089877aSRichard Tran Mills +  dm - the DM object
1845bc0a1609SRichard Tran Mills .  g - the original local vector
1846bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1847f089877aSRichard Tran Mills 
1848bc0a1609SRichard Tran Mills    Output Parameter:
1849bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1850f089877aSRichard Tran Mills 
1851f089877aSRichard Tran Mills    Level: intermediate
1852f089877aSRichard Tran Mills 
1853bc0a1609SRichard Tran Mills    Notes:
1854bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1855bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1856bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1857bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1858bc0a1609SRichard Tran Mills 
1859bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
1860bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1861f089877aSRichard Tran Mills 
1862f089877aSRichard Tran Mills @*/
1863f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
1864f089877aSRichard Tran Mills {
1865f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1866f089877aSRichard Tran Mills 
1867f089877aSRichard Tran Mills   PetscFunctionBegin;
1868f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1869f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1870f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1871f089877aSRichard Tran Mills }
1872f089877aSRichard Tran Mills 
1873f089877aSRichard Tran Mills #undef __FUNCT__
1874f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
1875f089877aSRichard Tran Mills /*@
1876bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
1877bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1878d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
1879f089877aSRichard Tran Mills 
1880bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1881f089877aSRichard Tran Mills 
1882f089877aSRichard Tran Mills    Input Parameters:
1883bc0a1609SRichard Tran Mills +  da - the DM object
1884bc0a1609SRichard Tran Mills .  g - the original local vector
1885bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1886f089877aSRichard Tran Mills 
1887bc0a1609SRichard Tran Mills    Output Parameter:
1888bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1889f089877aSRichard Tran Mills 
1890f089877aSRichard Tran Mills    Level: intermediate
1891f089877aSRichard Tran Mills 
1892bc0a1609SRichard Tran Mills    Notes:
1893bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1894bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1895bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1896bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1897bc0a1609SRichard Tran Mills 
1898bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
1899bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1900f089877aSRichard Tran Mills 
1901f089877aSRichard Tran Mills @*/
1902f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
1903f089877aSRichard Tran Mills {
1904f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1905f089877aSRichard Tran Mills 
1906f089877aSRichard Tran Mills   PetscFunctionBegin;
1907f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1908f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1909f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1910f089877aSRichard Tran Mills }
1911f089877aSRichard Tran Mills 
1912f089877aSRichard Tran Mills 
1913f089877aSRichard Tran Mills #undef __FUNCT__
191447c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
191547c6ae99SBarry Smith /*@
191647c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
191747c6ae99SBarry Smith 
191847c6ae99SBarry Smith     Collective on DM
191947c6ae99SBarry Smith 
192047c6ae99SBarry Smith     Input Parameter:
192147c6ae99SBarry Smith +   dm - the DM object
192291d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
192347c6ae99SBarry Smith 
192447c6ae99SBarry Smith     Output Parameter:
192547c6ae99SBarry Smith .   dmc - the coarsened DM
192647c6ae99SBarry Smith 
192747c6ae99SBarry Smith     Level: developer
192847c6ae99SBarry Smith 
1929e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
193047c6ae99SBarry Smith 
193147c6ae99SBarry Smith @*/
19327087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
193347c6ae99SBarry Smith {
193447c6ae99SBarry Smith   PetscErrorCode    ierr;
1935b17ce1afSJed Brown   DMCoarsenHookLink link;
193647c6ae99SBarry Smith 
193747c6ae99SBarry Smith   PetscFunctionBegin;
1938171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
193947c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
194043842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
19418cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1942644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
19430598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1944656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1945e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1946b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1947b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1948b17ce1afSJed Brown   }
1949b17ce1afSJed Brown   PetscFunctionReturn(0);
1950b17ce1afSJed Brown }
1951b17ce1afSJed Brown 
1952b17ce1afSJed Brown #undef __FUNCT__
1953b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1954bb9467b5SJed Brown /*@C
1955b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1956b17ce1afSJed Brown 
1957b17ce1afSJed Brown    Logically Collective
1958b17ce1afSJed Brown 
1959b17ce1afSJed Brown    Input Arguments:
1960b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1961b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1962b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
19630298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1964b17ce1afSJed Brown 
1965b17ce1afSJed Brown    Calling sequence of coarsenhook:
1966b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1967b17ce1afSJed Brown 
1968b17ce1afSJed Brown +  fine - fine level DM
1969b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1970b17ce1afSJed Brown -  ctx - optional user-defined function context
1971b17ce1afSJed Brown 
1972b17ce1afSJed Brown    Calling sequence for restricthook:
1973c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1974b17ce1afSJed Brown 
1975b17ce1afSJed Brown +  fine - fine level DM
1976b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1977c833c3b5SJed Brown .  rscale - scaling vector for restriction
1978c833c3b5SJed Brown .  inject - matrix restricting by injection
1979b17ce1afSJed Brown .  coarse - coarse level DM to update
1980b17ce1afSJed Brown -  ctx - optional user-defined function context
1981b17ce1afSJed Brown 
1982b17ce1afSJed Brown    Level: advanced
1983b17ce1afSJed Brown 
1984b17ce1afSJed Brown    Notes:
1985b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1986b17ce1afSJed Brown 
1987b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1988b17ce1afSJed Brown 
1989b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1990b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1991b17ce1afSJed Brown 
1992bb9467b5SJed Brown    This function is currently not available from Fortran.
1993bb9467b5SJed Brown 
1994c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1995b17ce1afSJed Brown @*/
1996b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1997b17ce1afSJed Brown {
1998b17ce1afSJed Brown   PetscErrorCode    ierr;
1999b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2000b17ce1afSJed Brown 
2001b17ce1afSJed Brown   PetscFunctionBegin;
2002b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
20036bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2004b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2005b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2006b17ce1afSJed Brown   link->restricthook = restricthook;
2007b17ce1afSJed Brown   link->ctx          = ctx;
20080298fd71SBarry Smith   link->next         = NULL;
2009b17ce1afSJed Brown   *p                 = link;
2010b17ce1afSJed Brown   PetscFunctionReturn(0);
2011b17ce1afSJed Brown }
2012b17ce1afSJed Brown 
2013b17ce1afSJed Brown #undef __FUNCT__
2014b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2015b17ce1afSJed Brown /*@
2016b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2017b17ce1afSJed Brown 
2018b17ce1afSJed Brown    Collective if any hooks are
2019b17ce1afSJed Brown 
2020b17ce1afSJed Brown    Input Arguments:
2021b17ce1afSJed Brown +  fine - finer DM to use as a base
2022b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2023b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2024b17ce1afSJed Brown -  coarse - coarer DM to update
2025b17ce1afSJed Brown 
2026b17ce1afSJed Brown    Level: developer
2027b17ce1afSJed Brown 
2028b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2029b17ce1afSJed Brown @*/
2030b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2031b17ce1afSJed Brown {
2032b17ce1afSJed Brown   PetscErrorCode    ierr;
2033b17ce1afSJed Brown   DMCoarsenHookLink link;
2034b17ce1afSJed Brown 
2035b17ce1afSJed Brown   PetscFunctionBegin;
2036b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
20378865f1eaSKarl Rupp     if (link->restricthook) {
20388865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
20398865f1eaSKarl Rupp     }
2040b17ce1afSJed Brown   }
204147c6ae99SBarry Smith   PetscFunctionReturn(0);
204247c6ae99SBarry Smith }
204347c6ae99SBarry Smith 
204447c6ae99SBarry Smith #undef __FUNCT__
2045be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2046bb9467b5SJed Brown /*@C
2047be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
20485dbd56e3SPeter Brune 
20495dbd56e3SPeter Brune    Logically Collective
20505dbd56e3SPeter Brune 
20515dbd56e3SPeter Brune    Input Arguments:
20525dbd56e3SPeter Brune +  global - global DM
2053ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
20545dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
20550298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20565dbd56e3SPeter Brune 
2057ec4806b8SPeter Brune 
2058ec4806b8SPeter Brune    Calling sequence for ddhook:
2059ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2060ec4806b8SPeter Brune 
2061ec4806b8SPeter Brune +  global - global DM
2062ec4806b8SPeter Brune .  block  - block DM
2063ec4806b8SPeter Brune -  ctx - optional user-defined function context
2064ec4806b8SPeter Brune 
20655dbd56e3SPeter Brune    Calling sequence for restricthook:
2066ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
20675dbd56e3SPeter Brune 
20685dbd56e3SPeter Brune +  global - global DM
20695dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
20705dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2071ec4806b8SPeter Brune .  block  - block DM
20725dbd56e3SPeter Brune -  ctx - optional user-defined function context
20735dbd56e3SPeter Brune 
20745dbd56e3SPeter Brune    Level: advanced
20755dbd56e3SPeter Brune 
20765dbd56e3SPeter Brune    Notes:
2077ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
20785dbd56e3SPeter Brune 
20795dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
20805dbd56e3SPeter Brune 
20815dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2082ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
20835dbd56e3SPeter Brune 
2084bb9467b5SJed Brown    This function is currently not available from Fortran.
2085bb9467b5SJed Brown 
20865dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20875dbd56e3SPeter Brune @*/
2088be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
20895dbd56e3SPeter Brune {
20905dbd56e3SPeter Brune   PetscErrorCode      ierr;
2091be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
20925dbd56e3SPeter Brune 
20935dbd56e3SPeter Brune   PetscFunctionBegin;
20945dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2095be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2096be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
20975dbd56e3SPeter Brune   link->restricthook = restricthook;
2098be081cd6SPeter Brune   link->ddhook       = ddhook;
20995dbd56e3SPeter Brune   link->ctx          = ctx;
21000298fd71SBarry Smith   link->next         = NULL;
21015dbd56e3SPeter Brune   *p                 = link;
21025dbd56e3SPeter Brune   PetscFunctionReturn(0);
21035dbd56e3SPeter Brune }
21045dbd56e3SPeter Brune 
21055dbd56e3SPeter Brune #undef __FUNCT__
2106be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
21075dbd56e3SPeter Brune /*@
2108be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
21095dbd56e3SPeter Brune 
21105dbd56e3SPeter Brune    Collective if any hooks are
21115dbd56e3SPeter Brune 
21125dbd56e3SPeter Brune    Input Arguments:
21135dbd56e3SPeter Brune +  fine - finer DM to use as a base
2114be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2115be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
21165dbd56e3SPeter Brune -  coarse - coarer DM to update
21175dbd56e3SPeter Brune 
21185dbd56e3SPeter Brune    Level: developer
21195dbd56e3SPeter Brune 
21205dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
21215dbd56e3SPeter Brune @*/
2122be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
21235dbd56e3SPeter Brune {
21245dbd56e3SPeter Brune   PetscErrorCode      ierr;
2125be081cd6SPeter Brune   DMSubDomainHookLink link;
21265dbd56e3SPeter Brune 
21275dbd56e3SPeter Brune   PetscFunctionBegin;
2128be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
21298865f1eaSKarl Rupp     if (link->restricthook) {
21308865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
21318865f1eaSKarl Rupp     }
21325dbd56e3SPeter Brune   }
21335dbd56e3SPeter Brune   PetscFunctionReturn(0);
21345dbd56e3SPeter Brune }
21355dbd56e3SPeter Brune 
21365dbd56e3SPeter Brune #undef __FUNCT__
21375fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
21385fe1f584SPeter Brune /*@
21396a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
21405fe1f584SPeter Brune 
21415fe1f584SPeter Brune     Not Collective
21425fe1f584SPeter Brune 
21435fe1f584SPeter Brune     Input Parameter:
21445fe1f584SPeter Brune .   dm - the DM object
21455fe1f584SPeter Brune 
21465fe1f584SPeter Brune     Output Parameter:
21476a7d9d85SPeter Brune .   level - number of coarsenings
21485fe1f584SPeter Brune 
21495fe1f584SPeter Brune     Level: developer
21505fe1f584SPeter Brune 
21516a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
21525fe1f584SPeter Brune 
21535fe1f584SPeter Brune @*/
21545fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
21555fe1f584SPeter Brune {
21565fe1f584SPeter Brune   PetscFunctionBegin;
21575fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21585fe1f584SPeter Brune   *level = dm->leveldown;
21595fe1f584SPeter Brune   PetscFunctionReturn(0);
21605fe1f584SPeter Brune }
21615fe1f584SPeter Brune 
21625fe1f584SPeter Brune 
21635fe1f584SPeter Brune 
21645fe1f584SPeter Brune #undef __FUNCT__
216547c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
216647c6ae99SBarry Smith /*@C
216747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
216847c6ae99SBarry Smith 
216947c6ae99SBarry Smith     Collective on DM
217047c6ae99SBarry Smith 
217147c6ae99SBarry Smith     Input Parameter:
217247c6ae99SBarry Smith +   dm - the DM object
217347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
217447c6ae99SBarry Smith 
217547c6ae99SBarry Smith     Output Parameter:
217647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
217747c6ae99SBarry Smith 
217847c6ae99SBarry Smith     Level: developer
217947c6ae99SBarry Smith 
2180e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
218147c6ae99SBarry Smith 
218247c6ae99SBarry Smith @*/
21837087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
218447c6ae99SBarry Smith {
218547c6ae99SBarry Smith   PetscErrorCode ierr;
218647c6ae99SBarry Smith 
218747c6ae99SBarry Smith   PetscFunctionBegin;
2188171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2189ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
219047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
219147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
219247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
219347c6ae99SBarry Smith   } else if (dm->ops->refine) {
219447c6ae99SBarry Smith     PetscInt i;
219547c6ae99SBarry Smith 
2196ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
219747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2198ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
219947c6ae99SBarry Smith     }
2200ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
220147c6ae99SBarry Smith   PetscFunctionReturn(0);
220247c6ae99SBarry Smith }
220347c6ae99SBarry Smith 
220447c6ae99SBarry Smith #undef __FUNCT__
220547c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
220647c6ae99SBarry Smith /*@C
220747c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
220847c6ae99SBarry Smith 
220947c6ae99SBarry Smith     Collective on DM
221047c6ae99SBarry Smith 
221147c6ae99SBarry Smith     Input Parameter:
221247c6ae99SBarry Smith +   dm - the DM object
221347c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
221447c6ae99SBarry Smith 
221547c6ae99SBarry Smith     Output Parameter:
221647c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
221747c6ae99SBarry Smith 
221847c6ae99SBarry Smith     Level: developer
221947c6ae99SBarry Smith 
2220e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
222147c6ae99SBarry Smith 
222247c6ae99SBarry Smith @*/
22237087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
222447c6ae99SBarry Smith {
222547c6ae99SBarry Smith   PetscErrorCode ierr;
222647c6ae99SBarry Smith 
222747c6ae99SBarry Smith   PetscFunctionBegin;
2228171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2229ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
223047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
223147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
223247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
223347c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
223447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
223547c6ae99SBarry Smith     PetscInt i;
223647c6ae99SBarry Smith 
2237ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
223847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2239ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
224047c6ae99SBarry Smith     }
2241ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
224247c6ae99SBarry Smith   PetscFunctionReturn(0);
224347c6ae99SBarry Smith }
224447c6ae99SBarry Smith 
224547c6ae99SBarry Smith #undef __FUNCT__
2246e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
224747c6ae99SBarry Smith /*@
2248e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
224947c6ae99SBarry Smith    grids associated with two DMs.
225047c6ae99SBarry Smith 
225147c6ae99SBarry Smith    Collective on DM
225247c6ae99SBarry Smith 
225347c6ae99SBarry Smith    Input Parameters:
225447c6ae99SBarry Smith +  dmc - the coarse grid DM
225547c6ae99SBarry Smith -  dmf - the fine grid DM
225647c6ae99SBarry Smith 
225747c6ae99SBarry Smith    Output Parameters:
225847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
225947c6ae99SBarry Smith 
226047c6ae99SBarry Smith    Level: intermediate
226147c6ae99SBarry Smith 
226247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
226347c6ae99SBarry Smith 
2264e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
226547c6ae99SBarry Smith @*/
2266e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
226747c6ae99SBarry Smith {
226847c6ae99SBarry Smith   PetscErrorCode ierr;
226947c6ae99SBarry Smith 
227047c6ae99SBarry Smith   PetscFunctionBegin;
2271171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2272171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
227347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
227447c6ae99SBarry Smith   PetscFunctionReturn(0);
227547c6ae99SBarry Smith }
227647c6ae99SBarry Smith 
227747c6ae99SBarry Smith #undef __FUNCT__
22781a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
22791a266240SBarry Smith /*@C
22801a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
22811a266240SBarry Smith 
22821a266240SBarry Smith     Not Collective
22831a266240SBarry Smith 
22841a266240SBarry Smith     Input Parameters:
22851a266240SBarry Smith +   dm - the DM object
22861a266240SBarry Smith -   destroy - the destroy function
22871a266240SBarry Smith 
22881a266240SBarry Smith     Level: intermediate
22891a266240SBarry Smith 
2290e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
22911a266240SBarry Smith 
2292f07f9ceaSJed Brown @*/
22931a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
22941a266240SBarry Smith {
22951a266240SBarry Smith   PetscFunctionBegin;
2296171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22971a266240SBarry Smith   dm->ctxdestroy = destroy;
22981a266240SBarry Smith   PetscFunctionReturn(0);
22991a266240SBarry Smith }
23001a266240SBarry Smith 
23011a266240SBarry Smith #undef __FUNCT__
23021b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2303b07ff414SBarry Smith /*@
23041b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
230547c6ae99SBarry Smith 
230647c6ae99SBarry Smith     Not Collective
230747c6ae99SBarry Smith 
230847c6ae99SBarry Smith     Input Parameters:
230947c6ae99SBarry Smith +   dm - the DM object
231047c6ae99SBarry Smith -   ctx - the user context
231147c6ae99SBarry Smith 
231247c6ae99SBarry Smith     Level: intermediate
231347c6ae99SBarry Smith 
2314e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
231547c6ae99SBarry Smith 
231647c6ae99SBarry Smith @*/
23171b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
231847c6ae99SBarry Smith {
231947c6ae99SBarry Smith   PetscFunctionBegin;
2320171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
232147c6ae99SBarry Smith   dm->ctx = ctx;
232247c6ae99SBarry Smith   PetscFunctionReturn(0);
232347c6ae99SBarry Smith }
232447c6ae99SBarry Smith 
232547c6ae99SBarry Smith #undef __FUNCT__
23261b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
232747c6ae99SBarry Smith /*@
23281b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
232947c6ae99SBarry Smith 
233047c6ae99SBarry Smith     Not Collective
233147c6ae99SBarry Smith 
233247c6ae99SBarry Smith     Input Parameter:
233347c6ae99SBarry Smith .   dm - the DM object
233447c6ae99SBarry Smith 
233547c6ae99SBarry Smith     Output Parameter:
233647c6ae99SBarry Smith .   ctx - the user context
233747c6ae99SBarry Smith 
233847c6ae99SBarry Smith     Level: intermediate
233947c6ae99SBarry Smith 
2340e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
234147c6ae99SBarry Smith 
234247c6ae99SBarry Smith @*/
23431b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
234447c6ae99SBarry Smith {
234547c6ae99SBarry Smith   PetscFunctionBegin;
2346171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23471b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
234847c6ae99SBarry Smith   PetscFunctionReturn(0);
234947c6ae99SBarry Smith }
235047c6ae99SBarry Smith 
235147c6ae99SBarry Smith #undef __FUNCT__
235208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
235308da532bSDmitry Karpeev /*@C
235408da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
235508da532bSDmitry Karpeev 
235608da532bSDmitry Karpeev     Logically Collective on DM
235708da532bSDmitry Karpeev 
235808da532bSDmitry Karpeev     Input Parameter:
235908da532bSDmitry Karpeev +   dm - the DM object
23600298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
236108da532bSDmitry Karpeev 
236208da532bSDmitry Karpeev     Level: intermediate
236308da532bSDmitry Karpeev 
2364835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
236508da532bSDmitry Karpeev          DMSetJacobian()
236608da532bSDmitry Karpeev 
236708da532bSDmitry Karpeev @*/
236808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
236908da532bSDmitry Karpeev {
237008da532bSDmitry Karpeev   PetscFunctionBegin;
237108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
237208da532bSDmitry Karpeev   PetscFunctionReturn(0);
237308da532bSDmitry Karpeev }
237408da532bSDmitry Karpeev 
237508da532bSDmitry Karpeev #undef __FUNCT__
237608da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
237708da532bSDmitry Karpeev /*@
237808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
237908da532bSDmitry Karpeev 
238008da532bSDmitry Karpeev     Not Collective
238108da532bSDmitry Karpeev 
238208da532bSDmitry Karpeev     Input Parameter:
238308da532bSDmitry Karpeev .   dm - the DM object to destroy
238408da532bSDmitry Karpeev 
238508da532bSDmitry Karpeev     Output Parameter:
238608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
238708da532bSDmitry Karpeev 
238808da532bSDmitry Karpeev     Level: developer
238908da532bSDmitry Karpeev 
239074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
239108da532bSDmitry Karpeev 
239208da532bSDmitry Karpeev @*/
239308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
239408da532bSDmitry Karpeev {
239508da532bSDmitry Karpeev   PetscFunctionBegin;
239608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
239708da532bSDmitry Karpeev   PetscFunctionReturn(0);
239808da532bSDmitry Karpeev }
239908da532bSDmitry Karpeev 
240008da532bSDmitry Karpeev #undef __FUNCT__
240108da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
240208da532bSDmitry Karpeev /*@C
240308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
240408da532bSDmitry Karpeev 
240508da532bSDmitry Karpeev     Logically Collective on DM
240608da532bSDmitry Karpeev 
240708da532bSDmitry Karpeev     Input Parameters:
240808da532bSDmitry Karpeev +   dm - the DM object to destroy
240908da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
241008da532bSDmitry Karpeev 
241108da532bSDmitry Karpeev     Output parameters:
241208da532bSDmitry Karpeev +   xl - lower bound
241308da532bSDmitry Karpeev -   xu - upper bound
241408da532bSDmitry Karpeev 
241508da532bSDmitry Karpeev     Level: intermediate
241608da532bSDmitry Karpeev 
241774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
241808da532bSDmitry Karpeev 
241908da532bSDmitry Karpeev @*/
242008da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
242108da532bSDmitry Karpeev {
242208da532bSDmitry Karpeev   PetscErrorCode ierr;
24235fd66863SKarl Rupp 
242408da532bSDmitry Karpeev   PetscFunctionBegin;
242508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
242608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
242708da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
242808da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
24298865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
243008da532bSDmitry Karpeev   PetscFunctionReturn(0);
243108da532bSDmitry Karpeev }
243208da532bSDmitry Karpeev 
243308da532bSDmitry Karpeev #undef __FUNCT__
2434b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2435b0ae01b7SPeter Brune /*@
2436b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2437b0ae01b7SPeter Brune 
2438b0ae01b7SPeter Brune     Not Collective
2439b0ae01b7SPeter Brune 
2440b0ae01b7SPeter Brune     Input Parameter:
2441b0ae01b7SPeter Brune .   dm - the DM object
2442b0ae01b7SPeter Brune 
2443b0ae01b7SPeter Brune     Output Parameter:
2444b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2445b0ae01b7SPeter Brune 
2446b0ae01b7SPeter Brune     Level: developer
2447b0ae01b7SPeter Brune 
2448b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2449b0ae01b7SPeter Brune 
2450b0ae01b7SPeter Brune @*/
2451b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2452b0ae01b7SPeter Brune {
2453b0ae01b7SPeter Brune   PetscFunctionBegin;
2454b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2455b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2456b0ae01b7SPeter Brune }
2457b0ae01b7SPeter Brune 
2458b0ae01b7SPeter Brune #undef  __FUNCT__
245908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2460748fac09SDmitry Karpeev /*@C
246108da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
246208da532bSDmitry Karpeev 
246308da532bSDmitry Karpeev     Collective on DM
246408da532bSDmitry Karpeev 
246508da532bSDmitry Karpeev     Input Parameter:
246608da532bSDmitry Karpeev +   dm - the DM object
24670298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
246808da532bSDmitry Karpeev 
246908da532bSDmitry Karpeev     Level: developer
247008da532bSDmitry Karpeev 
247174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
247208da532bSDmitry Karpeev 
247308da532bSDmitry Karpeev @*/
247408da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
247508da532bSDmitry Karpeev {
247608da532bSDmitry Karpeev   PetscErrorCode ierr;
24775fd66863SKarl Rupp 
247808da532bSDmitry Karpeev   PetscFunctionBegin;
247908da532bSDmitry Karpeev   if (x) {
248008da532bSDmitry Karpeev     if (!dm->x) {
248108da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
248208da532bSDmitry Karpeev     }
248308da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
24848865f1eaSKarl Rupp   } else if (dm->x) {
248508da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
248608da532bSDmitry Karpeev   }
248708da532bSDmitry Karpeev   PetscFunctionReturn(0);
248808da532bSDmitry Karpeev }
248908da532bSDmitry Karpeev 
24900298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2491264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2492264ace61SBarry Smith 
2493264ace61SBarry Smith #undef __FUNCT__
2494264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2495264ace61SBarry Smith /*@C
2496264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2497264ace61SBarry Smith 
2498264ace61SBarry Smith   Collective on DM
2499264ace61SBarry Smith 
2500264ace61SBarry Smith   Input Parameters:
2501264ace61SBarry Smith + dm     - The DM object
2502264ace61SBarry Smith - method - The name of the DM type
2503264ace61SBarry Smith 
2504264ace61SBarry Smith   Options Database Key:
2505264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2506264ace61SBarry Smith 
2507264ace61SBarry Smith   Notes:
2508e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2509264ace61SBarry Smith 
2510264ace61SBarry Smith   Level: intermediate
2511264ace61SBarry Smith 
2512264ace61SBarry Smith .keywords: DM, set, type
2513264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2514264ace61SBarry Smith @*/
251519fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2516264ace61SBarry Smith {
2517264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2518264ace61SBarry Smith   PetscBool      match;
2519264ace61SBarry Smith   PetscErrorCode ierr;
2520264ace61SBarry Smith 
2521264ace61SBarry Smith   PetscFunctionBegin;
2522264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2523251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2524264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2525264ace61SBarry Smith 
2526607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
25271c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2528ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2529264ace61SBarry Smith 
2530264ace61SBarry Smith   if (dm->ops->destroy) {
2531264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
25320298fd71SBarry Smith     dm->ops->destroy = NULL;
2533264ace61SBarry Smith   }
2534264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2535264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2536264ace61SBarry Smith   PetscFunctionReturn(0);
2537264ace61SBarry Smith }
2538264ace61SBarry Smith 
2539264ace61SBarry Smith #undef __FUNCT__
2540264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2541264ace61SBarry Smith /*@C
2542264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2543264ace61SBarry Smith 
2544264ace61SBarry Smith   Not Collective
2545264ace61SBarry Smith 
2546264ace61SBarry Smith   Input Parameter:
2547264ace61SBarry Smith . dm  - The DM
2548264ace61SBarry Smith 
2549264ace61SBarry Smith   Output Parameter:
2550264ace61SBarry Smith . type - The DM type name
2551264ace61SBarry Smith 
2552264ace61SBarry Smith   Level: intermediate
2553264ace61SBarry Smith 
2554264ace61SBarry Smith .keywords: DM, get, type, name
2555264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2556264ace61SBarry Smith @*/
255719fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2558264ace61SBarry Smith {
2559264ace61SBarry Smith   PetscErrorCode ierr;
2560264ace61SBarry Smith 
2561264ace61SBarry Smith   PetscFunctionBegin;
2562264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2563264ace61SBarry Smith   PetscValidCharPointer(type,2);
2564264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2565607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2566264ace61SBarry Smith   }
2567264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2568264ace61SBarry Smith   PetscFunctionReturn(0);
2569264ace61SBarry Smith }
2570264ace61SBarry Smith 
257167a56275SMatthew G Knepley #undef __FUNCT__
257267a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
257367a56275SMatthew G Knepley /*@C
257467a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
257567a56275SMatthew G Knepley 
257667a56275SMatthew G Knepley   Collective on DM
257767a56275SMatthew G Knepley 
257867a56275SMatthew G Knepley   Input Parameters:
257967a56275SMatthew G Knepley + dm - the DM
258067a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
258167a56275SMatthew G Knepley 
258267a56275SMatthew G Knepley   Output Parameter:
258367a56275SMatthew G Knepley . M - pointer to new DM
258467a56275SMatthew G Knepley 
258567a56275SMatthew G Knepley   Notes:
258667a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
258767a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
258867a56275SMatthew G Knepley   of the input DM.
258967a56275SMatthew G Knepley 
259067a56275SMatthew G Knepley   Level: intermediate
259167a56275SMatthew G Knepley 
259267a56275SMatthew G Knepley .seealso: DMCreate()
259367a56275SMatthew G Knepley @*/
259419fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
259567a56275SMatthew G Knepley {
259667a56275SMatthew G Knepley   DM             B;
259767a56275SMatthew G Knepley   char           convname[256];
259867a56275SMatthew G Knepley   PetscBool      sametype, issame;
259967a56275SMatthew G Knepley   PetscErrorCode ierr;
260067a56275SMatthew G Knepley 
260167a56275SMatthew G Knepley   PetscFunctionBegin;
260267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
260367a56275SMatthew G Knepley   PetscValidType(dm,1);
260467a56275SMatthew G Knepley   PetscValidPointer(M,3);
2605251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
260667a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
260767a56275SMatthew G Knepley   {
26080298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
260967a56275SMatthew G Knepley 
261067a56275SMatthew G Knepley     /*
261167a56275SMatthew G Knepley        Order of precedence:
261267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
261367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
261467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
261567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
261667a56275SMatthew G Knepley        5) Use a really basic converter.
261767a56275SMatthew G Knepley     */
261867a56275SMatthew G Knepley 
261967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
262067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
262167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
262267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
262367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
262467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26250005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
262667a56275SMatthew G Knepley     if (conv) goto foundconv;
262767a56275SMatthew G Knepley 
262867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
262982f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
263067a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
263167a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
263267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
263367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
263467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
263567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26360005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
263767a56275SMatthew G Knepley     if (conv) {
2638fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
263967a56275SMatthew G Knepley       goto foundconv;
264067a56275SMatthew G Knepley     }
264167a56275SMatthew G Knepley 
264267a56275SMatthew G Knepley #if 0
264367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
264467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2645fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
264667a56275SMatthew G Knepley     if (conv) goto foundconv;
264767a56275SMatthew G Knepley 
264867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
264967a56275SMatthew G Knepley     if (dm->ops->convert) {
265067a56275SMatthew G Knepley       conv = dm->ops->convert;
265167a56275SMatthew G Knepley     }
265267a56275SMatthew G Knepley     if (conv) goto foundconv;
265367a56275SMatthew G Knepley #endif
265467a56275SMatthew G Knepley 
265567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
265682f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
265767a56275SMatthew G Knepley 
265867a56275SMatthew G Knepley foundconv:
265967a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
266067a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
266167a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
266267a56275SMatthew G Knepley   }
266367a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
266467a56275SMatthew G Knepley   PetscFunctionReturn(0);
266567a56275SMatthew G Knepley }
2666264ace61SBarry Smith 
2667264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2668264ace61SBarry Smith 
2669264ace61SBarry Smith #undef __FUNCT__
2670264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2671264ace61SBarry Smith /*@C
26721c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
26731c84c290SBarry Smith 
26741c84c290SBarry Smith   Not Collective
26751c84c290SBarry Smith 
26761c84c290SBarry Smith   Input Parameters:
26771c84c290SBarry Smith + name        - The name of a new user-defined creation routine
26781c84c290SBarry Smith - create_func - The creation routine itself
26791c84c290SBarry Smith 
26801c84c290SBarry Smith   Notes:
26811c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
26821c84c290SBarry Smith 
26831c84c290SBarry Smith 
26841c84c290SBarry Smith   Sample usage:
26851c84c290SBarry Smith .vb
2686bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
26871c84c290SBarry Smith .ve
26881c84c290SBarry Smith 
26891c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
26901c84c290SBarry Smith .vb
26911c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
26921c84c290SBarry Smith     DMSetType(DM,"my_da");
26931c84c290SBarry Smith .ve
26941c84c290SBarry Smith    or at runtime via the option
26951c84c290SBarry Smith .vb
26961c84c290SBarry Smith     -da_type my_da
26971c84c290SBarry Smith .ve
2698264ace61SBarry Smith 
2699264ace61SBarry Smith   Level: advanced
27001c84c290SBarry Smith 
27011c84c290SBarry Smith .keywords: DM, register
2702bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
27031c84c290SBarry Smith 
2704264ace61SBarry Smith @*/
2705bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2706264ace61SBarry Smith {
2707264ace61SBarry Smith   PetscErrorCode ierr;
2708264ace61SBarry Smith 
2709264ace61SBarry Smith   PetscFunctionBegin;
2710a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2711264ace61SBarry Smith   PetscFunctionReturn(0);
2712264ace61SBarry Smith }
2713264ace61SBarry Smith 
2714b859378eSBarry Smith #undef __FUNCT__
2715b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2716b859378eSBarry Smith /*@C
271755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2718b859378eSBarry Smith 
2719b859378eSBarry Smith   Collective on PetscViewer
2720b859378eSBarry Smith 
2721b859378eSBarry Smith   Input Parameters:
2722b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2723b859378eSBarry Smith            some related function before a call to DMLoad().
2724b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2725b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2726b859378eSBarry Smith 
2727b859378eSBarry Smith    Level: intermediate
2728b859378eSBarry Smith 
2729b859378eSBarry Smith   Notes:
273055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2731b859378eSBarry Smith 
2732b859378eSBarry Smith   Notes for advanced users:
2733b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2734b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2735b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2736b859378eSBarry Smith   format is
2737b859378eSBarry Smith .vb
2738b859378eSBarry Smith      has not yet been determined
2739b859378eSBarry Smith .ve
2740b859378eSBarry Smith 
2741b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2742b859378eSBarry Smith @*/
2743b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2744b859378eSBarry Smith {
2745b859378eSBarry Smith   PetscErrorCode ierr;
274632c0f0efSBarry Smith   PetscBool      isbinary;
274755849f57SBarry Smith   PetscInt       classid;
274832c0f0efSBarry Smith   char           type[256];
2749b859378eSBarry Smith 
2750b859378eSBarry Smith   PetscFunctionBegin;
2751b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2752b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
275332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
275432c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2755b859378eSBarry Smith 
275632c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
2757ce94432eSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file");
275832c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
275932c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
27602d53ad75SBarry Smith   if (newdm->ops->load) {
2761b859378eSBarry Smith     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
27622d53ad75SBarry Smith   }
2763b859378eSBarry Smith   PetscFunctionReturn(0);
2764b859378eSBarry Smith }
2765b859378eSBarry Smith 
27667da65231SMatthew G Knepley /******************************** FEM Support **********************************/
27677da65231SMatthew G Knepley 
27687da65231SMatthew G Knepley #undef __FUNCT__
27697da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2770a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2771a6dfd86eSKarl Rupp {
27721d47ebbbSSatish Balay   PetscInt       f;
27731b30c384SMatthew G Knepley   PetscErrorCode ierr;
27741b30c384SMatthew G Knepley 
27757da65231SMatthew G Knepley   PetscFunctionBegin;
277674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27771d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
277874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
27797da65231SMatthew G Knepley   }
27807da65231SMatthew G Knepley   PetscFunctionReturn(0);
27817da65231SMatthew G Knepley }
27827da65231SMatthew G Knepley 
27837da65231SMatthew G Knepley #undef __FUNCT__
27847da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2785a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2786a6dfd86eSKarl Rupp {
27871b30c384SMatthew G Knepley   PetscInt       f, g;
27887da65231SMatthew G Knepley   PetscErrorCode ierr;
27897da65231SMatthew G Knepley 
27907da65231SMatthew G Knepley   PetscFunctionBegin;
279174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27921d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
279374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
27941d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
279574778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
27967da65231SMatthew G Knepley     }
279774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
27987da65231SMatthew G Knepley   }
27997da65231SMatthew G Knepley   PetscFunctionReturn(0);
28007da65231SMatthew G Knepley }
2801e7c4fc90SDmitry Karpeev 
2802970e74d5SMatthew G Knepley #undef __FUNCT__
280388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
280488ed4aceSMatthew G Knepley /*@
280588ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
280688ed4aceSMatthew G Knepley 
280788ed4aceSMatthew G Knepley   Input Parameter:
280888ed4aceSMatthew G Knepley . dm - The DM
280988ed4aceSMatthew G Knepley 
281088ed4aceSMatthew G Knepley   Output Parameter:
281188ed4aceSMatthew G Knepley . section - The PetscSection
281288ed4aceSMatthew G Knepley 
281388ed4aceSMatthew G Knepley   Level: intermediate
281488ed4aceSMatthew G Knepley 
281588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
281688ed4aceSMatthew G Knepley 
281788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
281888ed4aceSMatthew G Knepley @*/
28190adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
28200adebc6cSBarry Smith {
282188ed4aceSMatthew G Knepley   PetscFunctionBegin;
282288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
282388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
282488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
282588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
282688ed4aceSMatthew G Knepley }
282788ed4aceSMatthew G Knepley 
282888ed4aceSMatthew G Knepley #undef __FUNCT__
282988ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
283088ed4aceSMatthew G Knepley /*@
283188ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
283288ed4aceSMatthew G Knepley 
283388ed4aceSMatthew G Knepley   Input Parameters:
283488ed4aceSMatthew G Knepley + dm - The DM
283588ed4aceSMatthew G Knepley - section - The PetscSection
283688ed4aceSMatthew G Knepley 
283788ed4aceSMatthew G Knepley   Level: intermediate
283888ed4aceSMatthew G Knepley 
283988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
284088ed4aceSMatthew G Knepley 
284188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
284288ed4aceSMatthew G Knepley @*/
28430adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
28440adebc6cSBarry Smith {
2845af122d2aSMatthew G Knepley   PetscInt       numFields;
2846af122d2aSMatthew G Knepley   PetscInt       f;
284788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
284888ed4aceSMatthew G Knepley 
284988ed4aceSMatthew G Knepley   PetscFunctionBegin;
285088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28511d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
28521d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
285388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
285488ed4aceSMatthew G Knepley   dm->defaultSection = section;
2855af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2856af122d2aSMatthew G Knepley   if (numFields) {
2857af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2858af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2859af122d2aSMatthew G Knepley       const char *name;
2860af122d2aSMatthew G Knepley 
2861af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2862af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2863af122d2aSMatthew G Knepley     }
2864af122d2aSMatthew G Knepley   }
28651d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
28661d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
286788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
286888ed4aceSMatthew G Knepley }
286988ed4aceSMatthew G Knepley 
287088ed4aceSMatthew G Knepley #undef __FUNCT__
287188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
287288ed4aceSMatthew G Knepley /*@
287388ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
287488ed4aceSMatthew G Knepley 
28758b1ab98fSJed Brown   Collective on DM
28768b1ab98fSJed Brown 
287788ed4aceSMatthew G Knepley   Input Parameter:
287888ed4aceSMatthew G Knepley . dm - The DM
287988ed4aceSMatthew G Knepley 
288088ed4aceSMatthew G Knepley   Output Parameter:
288188ed4aceSMatthew G Knepley . section - The PetscSection
288288ed4aceSMatthew G Knepley 
288388ed4aceSMatthew G Knepley   Level: intermediate
288488ed4aceSMatthew G Knepley 
288588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
288688ed4aceSMatthew G Knepley 
288788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
288888ed4aceSMatthew G Knepley @*/
28890adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
28900adebc6cSBarry Smith {
289188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
289288ed4aceSMatthew G Knepley 
289388ed4aceSMatthew G Knepley   PetscFunctionBegin;
289488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
289588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
289688ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
289782f516ccSBarry 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");
2898b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2899cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
2900ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm),dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
290188ed4aceSMatthew G Knepley   }
290288ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
290388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
290488ed4aceSMatthew G Knepley }
290588ed4aceSMatthew G Knepley 
290688ed4aceSMatthew G Knepley #undef __FUNCT__
2907b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2908b21d0597SMatthew G Knepley /*@
2909b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2910b21d0597SMatthew G Knepley 
2911b21d0597SMatthew G Knepley   Input Parameters:
2912b21d0597SMatthew G Knepley + dm - The DM
29135080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2914b21d0597SMatthew G Knepley 
2915b21d0597SMatthew G Knepley   Level: intermediate
2916b21d0597SMatthew G Knepley 
2917b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2918b21d0597SMatthew G Knepley 
2919b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2920b21d0597SMatthew G Knepley @*/
29210adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
29220adebc6cSBarry Smith {
2923b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2924b21d0597SMatthew G Knepley 
2925b21d0597SMatthew G Knepley   PetscFunctionBegin;
2926b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29275080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
29281d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2929b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2930b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2931b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2932b21d0597SMatthew G Knepley }
2933b21d0597SMatthew G Knepley 
2934b21d0597SMatthew G Knepley #undef __FUNCT__
293588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
293688ed4aceSMatthew G Knepley /*@
293788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
293888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
293988ed4aceSMatthew G Knepley 
294088ed4aceSMatthew G Knepley   Input Parameter:
294188ed4aceSMatthew G Knepley . dm - The DM
294288ed4aceSMatthew G Knepley 
294388ed4aceSMatthew G Knepley   Output Parameter:
294488ed4aceSMatthew G Knepley . sf - The PetscSF
294588ed4aceSMatthew G Knepley 
294688ed4aceSMatthew G Knepley   Level: intermediate
294788ed4aceSMatthew G Knepley 
294888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
294988ed4aceSMatthew G Knepley 
295088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
295188ed4aceSMatthew G Knepley @*/
29520adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
29530adebc6cSBarry Smith {
295488ed4aceSMatthew G Knepley   PetscInt       nroots;
295588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
295688ed4aceSMatthew G Knepley 
295788ed4aceSMatthew G Knepley   PetscFunctionBegin;
295888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
295988ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
29600298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
296188ed4aceSMatthew G Knepley   if (nroots < 0) {
296288ed4aceSMatthew G Knepley     PetscSection section, gSection;
296388ed4aceSMatthew G Knepley 
296488ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
296531ea6d37SMatthew G Knepley     if (section) {
296688ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
296788ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
296831ea6d37SMatthew G Knepley     } else {
29690298fd71SBarry Smith       *sf = NULL;
297031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
297131ea6d37SMatthew G Knepley     }
297288ed4aceSMatthew G Knepley   }
297388ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
297488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
297588ed4aceSMatthew G Knepley }
297688ed4aceSMatthew G Knepley 
297788ed4aceSMatthew G Knepley #undef __FUNCT__
297888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
297988ed4aceSMatthew G Knepley /*@
298088ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
298188ed4aceSMatthew G Knepley 
298288ed4aceSMatthew G Knepley   Input Parameters:
298388ed4aceSMatthew G Knepley + dm - The DM
298488ed4aceSMatthew G Knepley - sf - The PetscSF
298588ed4aceSMatthew G Knepley 
298688ed4aceSMatthew G Knepley   Level: intermediate
298788ed4aceSMatthew G Knepley 
298888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
298988ed4aceSMatthew G Knepley 
299088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
299188ed4aceSMatthew G Knepley @*/
29920adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
29930adebc6cSBarry Smith {
299488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
299588ed4aceSMatthew G Knepley 
299688ed4aceSMatthew G Knepley   PetscFunctionBegin;
299788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
299888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
299988ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
300088ed4aceSMatthew G Knepley   dm->defaultSF = sf;
300188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
300288ed4aceSMatthew G Knepley }
300388ed4aceSMatthew G Knepley 
300488ed4aceSMatthew G Knepley #undef __FUNCT__
300588ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
300688ed4aceSMatthew G Knepley /*@C
300788ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
300888ed4aceSMatthew G Knepley   describing the data layout.
300988ed4aceSMatthew G Knepley 
301088ed4aceSMatthew G Knepley   Input Parameters:
301188ed4aceSMatthew G Knepley + dm - The DM
301288ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
301388ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
301488ed4aceSMatthew G Knepley 
301588ed4aceSMatthew G Knepley   Level: intermediate
301688ed4aceSMatthew G Knepley 
301788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
301888ed4aceSMatthew G Knepley @*/
301988ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
302088ed4aceSMatthew G Knepley {
302182f516ccSBarry Smith   MPI_Comm       comm;
302288ed4aceSMatthew G Knepley   PetscLayout    layout;
302388ed4aceSMatthew G Knepley   const PetscInt *ranges;
302488ed4aceSMatthew G Knepley   PetscInt       *local;
302588ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3026ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
302788ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
302888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
302988ed4aceSMatthew G Knepley 
303088ed4aceSMatthew G Knepley   PetscFunctionBegin;
303182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
303288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
303388ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
303488ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
303588ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
303688ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
303788ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
303888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
303988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
304088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
304188ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3042ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
30436636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
304488ed4aceSMatthew G Knepley 
30456636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30466636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
30476636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
304888ed4aceSMatthew G Knepley   }
304988ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
305088ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
305188ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
30521f588964SMatthew G Knepley     const PetscInt *cind;
30536636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
305488ed4aceSMatthew G Knepley 
305588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
305688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
305788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
305888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
305988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30606636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
306188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
30626636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
30636636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
30646636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3065057b4bcdSMatthew 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);
30666636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
30676636e97aSMatthew G Knepley     }
306888ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
306988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
307088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
307188ed4aceSMatthew G Knepley     }
307288ed4aceSMatthew G Knepley     if (gdof < 0) {
30736636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
307488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
307588ed4aceSMatthew G Knepley 
307631d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
307731d3f06eSJed Brown         if (r < 0) r = -(r+2);
307888ed4aceSMatthew G Knepley         remote[l].rank  = r;
307988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
308088ed4aceSMatthew G Knepley       }
308188ed4aceSMatthew G Knepley     } else {
30826636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
308388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
308488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
308588ed4aceSMatthew G Knepley       }
308688ed4aceSMatthew G Knepley     }
308788ed4aceSMatthew G Knepley   }
30886636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
308988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
309088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
309188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
309288ed4aceSMatthew G Knepley }
3093af122d2aSMatthew G Knepley 
3094af122d2aSMatthew G Knepley #undef __FUNCT__
3095b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3096b21d0597SMatthew G Knepley /*@
3097b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3098b21d0597SMatthew G Knepley 
3099b21d0597SMatthew G Knepley   Input Parameter:
3100b21d0597SMatthew G Knepley . dm - The DM
3101b21d0597SMatthew G Knepley 
3102b21d0597SMatthew G Knepley   Output Parameter:
3103b21d0597SMatthew G Knepley . sf - The PetscSF
3104b21d0597SMatthew G Knepley 
3105b21d0597SMatthew G Knepley   Level: intermediate
3106b21d0597SMatthew G Knepley 
3107b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3108b21d0597SMatthew G Knepley 
3109057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3110b21d0597SMatthew G Knepley @*/
31110adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
31120adebc6cSBarry Smith {
3113b21d0597SMatthew G Knepley   PetscFunctionBegin;
3114b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3115b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3116b21d0597SMatthew G Knepley   *sf = dm->sf;
3117b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3118b21d0597SMatthew G Knepley }
3119b21d0597SMatthew G Knepley 
3120b21d0597SMatthew G Knepley #undef __FUNCT__
3121057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3122057b4bcdSMatthew G Knepley /*@
3123057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3124057b4bcdSMatthew G Knepley 
3125057b4bcdSMatthew G Knepley   Input Parameters:
3126057b4bcdSMatthew G Knepley + dm - The DM
3127057b4bcdSMatthew G Knepley - sf - The PetscSF
3128057b4bcdSMatthew G Knepley 
3129057b4bcdSMatthew G Knepley   Level: intermediate
3130057b4bcdSMatthew G Knepley 
3131057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3132057b4bcdSMatthew G Knepley @*/
31330adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
31340adebc6cSBarry Smith {
3135057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3136057b4bcdSMatthew G Knepley 
3137057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3138057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3139057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3140057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3141057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3142057b4bcdSMatthew G Knepley   dm->sf = sf;
3143057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3144057b4bcdSMatthew G Knepley }
3145057b4bcdSMatthew G Knepley 
3146057b4bcdSMatthew G Knepley #undef __FUNCT__
3147af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3148af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3149af122d2aSMatthew G Knepley {
3150af122d2aSMatthew G Knepley   PetscFunctionBegin;
3151af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3152af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3153af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3154af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3155af122d2aSMatthew G Knepley }
3156af122d2aSMatthew G Knepley 
3157af122d2aSMatthew G Knepley #undef __FUNCT__
3158af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3159af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3160af122d2aSMatthew G Knepley {
3161af122d2aSMatthew G Knepley   PetscInt       f;
3162af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3163af122d2aSMatthew G Knepley 
3164af122d2aSMatthew G Knepley   PetscFunctionBegin;
3165af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3166af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3167af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3168af122d2aSMatthew G Knepley   }
3169af122d2aSMatthew G Knepley   ierr          = PetscFree(dm->fields);CHKERRQ(ierr);
3170af122d2aSMatthew G Knepley   dm->numFields = numFields;
3171af122d2aSMatthew G Knepley   ierr          = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3172af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
317382f516ccSBarry Smith     ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm), (PetscContainer*) &dm->fields[f]);CHKERRQ(ierr);
3174af122d2aSMatthew G Knepley   }
3175af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3176af122d2aSMatthew G Knepley }
3177af122d2aSMatthew G Knepley 
3178af122d2aSMatthew G Knepley #undef __FUNCT__
3179af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3180af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3181af122d2aSMatthew G Knepley {
3182af122d2aSMatthew G Knepley   PetscFunctionBegin;
3183af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3184af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
318582f516ccSBarry Smith   if (!dm->fields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
318682f516ccSBarry 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);
3187af122d2aSMatthew G Knepley   *field = dm->fields[f];
3188af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3189af122d2aSMatthew G Knepley }
31906636e97aSMatthew G Knepley 
31916636e97aSMatthew G Knepley #undef __FUNCT__
3192b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3193b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3194b64e0483SPeter Brune {
3195b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3196b64e0483SPeter Brune   PetscErrorCode ierr;
3197b64e0483SPeter Brune   Vec coords,ccoords;
3198b64e0483SPeter Brune   VecScatter scat;
3199b64e0483SPeter Brune   PetscFunctionBegin;
3200b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3201b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3202b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3203b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3204b64e0483SPeter Brune   if (coords && !ccoords) {
3205b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
3206b64e0483SPeter Brune     ierr = DMCreateInjection(dmc_coord,dm_coord,&scat);CHKERRQ(ierr);
3207b64e0483SPeter Brune     ierr = VecScatterBegin(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3208b64e0483SPeter Brune     ierr = VecScatterEnd(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3209b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3210b64e0483SPeter Brune     ierr = VecScatterDestroy(&scat);CHKERRQ(ierr);
3211b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3212b64e0483SPeter Brune   }
3213b64e0483SPeter Brune   PetscFunctionReturn(0);
3214b64e0483SPeter Brune }
3215b64e0483SPeter Brune 
3216b64e0483SPeter Brune #undef __FUNCT__
321703dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
321803dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
321903dadc2fSPeter Brune {
322003dadc2fSPeter Brune   DM dm_coord,subdm_coord;
322103dadc2fSPeter Brune   PetscErrorCode ierr;
322203dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
322303dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
322403dadc2fSPeter Brune   PetscFunctionBegin;
322503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
322603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
322703dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
322803dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
322903dadc2fSPeter Brune   if (coords && !ccoords) {
323003dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
323103dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
323203dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
323303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
323403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
323503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
323603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
323703dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
323803dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
323903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
324003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
324103dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
324203dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
324303dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
324403dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
324503dadc2fSPeter Brune   }
324603dadc2fSPeter Brune   PetscFunctionReturn(0);
324703dadc2fSPeter Brune }
324803dadc2fSPeter Brune 
324903dadc2fSPeter Brune #undef __FUNCT__
32506636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
32516636e97aSMatthew G Knepley /*@
32526636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
32536636e97aSMatthew G Knepley 
32546636e97aSMatthew G Knepley   Collective on DM
32556636e97aSMatthew G Knepley 
32566636e97aSMatthew G Knepley   Input Parameters:
32576636e97aSMatthew G Knepley + dm - the DM
32586636e97aSMatthew G Knepley - c - coordinate vector
32596636e97aSMatthew G Knepley 
32606636e97aSMatthew G Knepley   Note:
32616636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
32626636e97aSMatthew G Knepley 
32636636e97aSMatthew G Knepley   Level: intermediate
32646636e97aSMatthew G Knepley 
32656636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32666636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
32676636e97aSMatthew G Knepley @*/
32686636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
32696636e97aSMatthew G Knepley {
32706636e97aSMatthew G Knepley   PetscErrorCode ierr;
32716636e97aSMatthew G Knepley 
32726636e97aSMatthew G Knepley   PetscFunctionBegin;
32736636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32746636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
32756636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
32766636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
32776636e97aSMatthew G Knepley   dm->coordinates = c;
32786636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3279b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
328003dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
32816636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32826636e97aSMatthew G Knepley }
32836636e97aSMatthew G Knepley 
32846636e97aSMatthew G Knepley #undef __FUNCT__
32856636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
32866636e97aSMatthew G Knepley /*@
32876636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
32886636e97aSMatthew G Knepley 
32896636e97aSMatthew G Knepley   Collective on DM
32906636e97aSMatthew G Knepley 
32916636e97aSMatthew G Knepley    Input Parameters:
32926636e97aSMatthew G Knepley +  dm - the DM
32936636e97aSMatthew G Knepley -  c - coordinate vector
32946636e97aSMatthew G Knepley 
32956636e97aSMatthew G Knepley   Note:
32966636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
32976636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
32986636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
32996636e97aSMatthew G Knepley 
33006636e97aSMatthew G Knepley   Level: intermediate
33016636e97aSMatthew G Knepley 
33026636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33036636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
33046636e97aSMatthew G Knepley @*/
33056636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
33066636e97aSMatthew G Knepley {
33076636e97aSMatthew G Knepley   PetscErrorCode ierr;
33086636e97aSMatthew G Knepley 
33096636e97aSMatthew G Knepley   PetscFunctionBegin;
33106636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
33126636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
33136636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
33148865f1eaSKarl Rupp 
33156636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
33168865f1eaSKarl Rupp 
33176636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
33186636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33196636e97aSMatthew G Knepley }
33206636e97aSMatthew G Knepley 
33216636e97aSMatthew G Knepley #undef __FUNCT__
33226636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
33236636e97aSMatthew G Knepley /*@
33246636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
33256636e97aSMatthew G Knepley 
33266636e97aSMatthew G Knepley   Not Collective
33276636e97aSMatthew G Knepley 
33286636e97aSMatthew G Knepley   Input Parameter:
33296636e97aSMatthew G Knepley . dm - the DM
33306636e97aSMatthew G Knepley 
33316636e97aSMatthew G Knepley   Output Parameter:
33326636e97aSMatthew G Knepley . c - global coordinate vector
33336636e97aSMatthew G Knepley 
33346636e97aSMatthew G Knepley   Note:
33356636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
33366636e97aSMatthew G Knepley 
33376636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
33386636e97aSMatthew G Knepley 
33396636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
33406636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
33416636e97aSMatthew G Knepley 
33426636e97aSMatthew G Knepley   Level: intermediate
33436636e97aSMatthew G Knepley 
33446636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33456636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
33466636e97aSMatthew G Knepley @*/
33476636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
33486636e97aSMatthew G Knepley {
33496636e97aSMatthew G Knepley   PetscErrorCode ierr;
33506636e97aSMatthew G Knepley 
33516636e97aSMatthew G Knepley   PetscFunctionBegin;
33526636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33536636e97aSMatthew G Knepley   PetscValidPointer(c,2);
33541f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
33550298fd71SBarry Smith     DM cdm = NULL;
33566636e97aSMatthew G Knepley 
33576636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
33586636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
33596636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
33606636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
33616636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
33626636e97aSMatthew G Knepley   }
33636636e97aSMatthew G Knepley   *c = dm->coordinates;
33646636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33656636e97aSMatthew G Knepley }
33666636e97aSMatthew G Knepley 
33676636e97aSMatthew G Knepley #undef __FUNCT__
33686636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
33696636e97aSMatthew G Knepley /*@
33706636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
33716636e97aSMatthew G Knepley 
33726636e97aSMatthew G Knepley   Collective on DM
33736636e97aSMatthew G Knepley 
33746636e97aSMatthew G Knepley   Input Parameter:
33756636e97aSMatthew G Knepley . dm - the DM
33766636e97aSMatthew G Knepley 
33776636e97aSMatthew G Knepley   Output Parameter:
33786636e97aSMatthew G Knepley . c - coordinate vector
33796636e97aSMatthew G Knepley 
33806636e97aSMatthew G Knepley   Note:
33816636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
33826636e97aSMatthew G Knepley 
33836636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
33846636e97aSMatthew G Knepley 
33856636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
33866636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
33876636e97aSMatthew G Knepley 
33886636e97aSMatthew G Knepley   Level: intermediate
33896636e97aSMatthew G Knepley 
33906636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33916636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
33926636e97aSMatthew G Knepley @*/
33936636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
33946636e97aSMatthew G Knepley {
33956636e97aSMatthew G Knepley   PetscErrorCode ierr;
33966636e97aSMatthew G Knepley 
33976636e97aSMatthew G Knepley   PetscFunctionBegin;
33986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33996636e97aSMatthew G Knepley   PetscValidPointer(c,2);
34001f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
34010298fd71SBarry Smith     DM cdm = NULL;
34026636e97aSMatthew G Knepley 
34036636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
34046636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
34056636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
34066636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
34076636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
34086636e97aSMatthew G Knepley   }
34096636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
34106636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34116636e97aSMatthew G Knepley }
34126636e97aSMatthew G Knepley 
34136636e97aSMatthew G Knepley #undef __FUNCT__
34146636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
34156636e97aSMatthew G Knepley /*@
34166636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
34176636e97aSMatthew G Knepley 
34186636e97aSMatthew G Knepley   Collective on DM
34196636e97aSMatthew G Knepley 
34206636e97aSMatthew G Knepley   Input Parameter:
34216636e97aSMatthew G Knepley . dm - the DM
34226636e97aSMatthew G Knepley 
34236636e97aSMatthew G Knepley   Output Parameter:
34246636e97aSMatthew G Knepley . cdm - coordinate DM
34256636e97aSMatthew G Knepley 
34266636e97aSMatthew G Knepley   Level: intermediate
34276636e97aSMatthew G Knepley 
34286636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
34296636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
34306636e97aSMatthew G Knepley @*/
34316636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
34326636e97aSMatthew G Knepley {
34336636e97aSMatthew G Knepley   PetscErrorCode ierr;
34346636e97aSMatthew G Knepley 
34356636e97aSMatthew G Knepley   PetscFunctionBegin;
34366636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34376636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
34386636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
343982f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
34406636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
34416636e97aSMatthew G Knepley   }
34426636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
34436636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34446636e97aSMatthew G Knepley }
3445e87bb0d3SMatthew G Knepley 
3446e87bb0d3SMatthew G Knepley #undef __FUNCT__
3447e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3448e87bb0d3SMatthew G Knepley /*@
3449e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3450e87bb0d3SMatthew G Knepley 
3451e87bb0d3SMatthew G Knepley   Not collective
3452e87bb0d3SMatthew G Knepley 
3453e87bb0d3SMatthew G Knepley   Input Parameters:
3454e87bb0d3SMatthew G Knepley + dm - The DM
3455e87bb0d3SMatthew G Knepley - v - The Vec of points
3456e87bb0d3SMatthew G Knepley 
345761e3bb9bSMatthew G Knepley   Output Parameter:
3458e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3459e87bb0d3SMatthew G Knepley 
3460e87bb0d3SMatthew G Knepley   Level: developer
346161e3bb9bSMatthew G Knepley 
346261e3bb9bSMatthew G Knepley .keywords: point location, mesh
346361e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
346461e3bb9bSMatthew G Knepley @*/
3465e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3466e87bb0d3SMatthew G Knepley {
3467735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3468735aa83eSMatthew G Knepley 
3469e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3470e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3471e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3472e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3473735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3474735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
347582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3476e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3477e87bb0d3SMatthew G Knepley }
3478