xref: /petsc/src/dm/interface/dm.c (revision c0dedaea2979d61a615b7bf0172de54067e60e4e)
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*c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
102b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
1031411c6eeSJed Brown   *dm = v;
104a4121054SBarry Smith   PetscFunctionReturn(0);
105a4121054SBarry Smith }
106a4121054SBarry Smith 
107a4121054SBarry Smith #undef __FUNCT__
10838221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
10938221697SMatthew G. Knepley /*@
11038221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
11138221697SMatthew G. Knepley 
11238221697SMatthew G. Knepley   Collective on MPI_Comm
11338221697SMatthew G. Knepley 
11438221697SMatthew G. Knepley   Input Parameter:
11538221697SMatthew G. Knepley . dm - The original DM object
11638221697SMatthew G. Knepley 
11738221697SMatthew G. Knepley   Output Parameter:
11838221697SMatthew G. Knepley . newdm  - The new DM object
11938221697SMatthew G. Knepley 
12038221697SMatthew G. Knepley   Level: beginner
12138221697SMatthew G. Knepley 
12238221697SMatthew G. Knepley .keywords: DM, topology, create
12338221697SMatthew G. Knepley @*/
12438221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12538221697SMatthew G. Knepley {
12638221697SMatthew G. Knepley   PetscSF        sf;
12738221697SMatthew G. Knepley   Vec            coords;
12838221697SMatthew G. Knepley   void          *ctx;
12938221697SMatthew G. Knepley   PetscErrorCode ierr;
13038221697SMatthew G. Knepley 
13138221697SMatthew G. Knepley   PetscFunctionBegin;
13238221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13338221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
13438221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   if (dm->ops->clone) {
13638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13738221697SMatthew G. Knepley   }
13838221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
14238221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
14338221697SMatthew G. Knepley   if (coords) {
14438221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
14538221697SMatthew G. Knepley   } else {
14638221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14738221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14838221697SMatthew G. Knepley   }
14938221697SMatthew G. Knepley   PetscFunctionReturn(0);
15038221697SMatthew G. Knepley }
15138221697SMatthew G. Knepley 
15238221697SMatthew G. Knepley #undef __FUNCT__
1539a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1549a42bb27SBarry Smith /*@C
155564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1569a42bb27SBarry Smith 
157aa219208SBarry Smith    Logically Collective on DMDA
1589a42bb27SBarry Smith 
1599a42bb27SBarry Smith    Input Parameter:
1609a42bb27SBarry Smith +  da - initial distributed array
1618154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1629a42bb27SBarry Smith 
1639a42bb27SBarry Smith    Options Database:
164dd85299cSBarry Smith .   -dm_vec_type ctype
1659a42bb27SBarry Smith 
1669a42bb27SBarry Smith    Level: intermediate
1679a42bb27SBarry Smith 
168*c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType, DMGetVecType()
1699a42bb27SBarry Smith @*/
17019fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1719a42bb27SBarry Smith {
1729a42bb27SBarry Smith   PetscErrorCode ierr;
1739a42bb27SBarry Smith 
1749a42bb27SBarry Smith   PetscFunctionBegin;
1759a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1769a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
17719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1789a42bb27SBarry Smith   PetscFunctionReturn(0);
1799a42bb27SBarry Smith }
1809a42bb27SBarry Smith 
1819a42bb27SBarry Smith #undef __FUNCT__
182*c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
183*c0dedaeaSBarry Smith /*@C
184*c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
185*c0dedaeaSBarry Smith 
186*c0dedaeaSBarry Smith    Logically Collective on DMDA
187*c0dedaeaSBarry Smith 
188*c0dedaeaSBarry Smith    Input Parameter:
189*c0dedaeaSBarry Smith .  da - initial distributed array
190*c0dedaeaSBarry Smith 
191*c0dedaeaSBarry Smith    Output Parameter:
192*c0dedaeaSBarry Smith .  ctype - the vector type
193*c0dedaeaSBarry Smith 
194*c0dedaeaSBarry Smith    Level: intermediate
195*c0dedaeaSBarry Smith 
196*c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
197*c0dedaeaSBarry Smith @*/
198*c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
199*c0dedaeaSBarry Smith {
200*c0dedaeaSBarry Smith   PetscFunctionBegin;
201*c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
202*c0dedaeaSBarry Smith   *ctype = da->vectype;
203*c0dedaeaSBarry Smith   PetscFunctionReturn(0);
204*c0dedaeaSBarry Smith }
205*c0dedaeaSBarry Smith 
206*c0dedaeaSBarry Smith #undef __FUNCT__
2075f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
2085f1ad066SMatthew G Knepley /*@
20934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2105f1ad066SMatthew G Knepley 
2115f1ad066SMatthew G Knepley   Not collective
2125f1ad066SMatthew G Knepley 
2135f1ad066SMatthew G Knepley   Input Parameter:
2145f1ad066SMatthew G Knepley . v - The Vec
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley   Output Parameter:
2175f1ad066SMatthew G Knepley . dm - The DM
2185f1ad066SMatthew G Knepley 
2195f1ad066SMatthew G Knepley   Level: intermediate
2205f1ad066SMatthew G Knepley 
2215f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2225f1ad066SMatthew G Knepley @*/
2235f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(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);
2295f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2305f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2315f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2325f1ad066SMatthew G Knepley }
2335f1ad066SMatthew G Knepley 
2345f1ad066SMatthew G Knepley #undef __FUNCT__
2355f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2365f1ad066SMatthew G Knepley /*@
2375f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley   Not collective
2405f1ad066SMatthew G Knepley 
2415f1ad066SMatthew G Knepley   Input Parameters:
2425f1ad066SMatthew G Knepley + v - The Vec
2435f1ad066SMatthew G Knepley - dm - The DM
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley   Level: intermediate
2465f1ad066SMatthew G Knepley 
2475f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2485f1ad066SMatthew G Knepley @*/
2495f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2505f1ad066SMatthew G Knepley {
2515f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley   PetscFunctionBegin;
2545f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
255d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2565f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2575f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2585f1ad066SMatthew G Knepley }
2595f1ad066SMatthew G Knepley 
2605f1ad066SMatthew G Knepley #undef __FUNCT__
261521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
262521d9a4cSLisandro Dalcin /*@C
263521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
264521d9a4cSLisandro Dalcin 
265521d9a4cSLisandro Dalcin    Logically Collective on DM
266521d9a4cSLisandro Dalcin 
267521d9a4cSLisandro Dalcin    Input Parameter:
268521d9a4cSLisandro Dalcin +  dm - the DM context
269521d9a4cSLisandro Dalcin .  ctype - the matrix type
270521d9a4cSLisandro Dalcin 
271521d9a4cSLisandro Dalcin    Options Database:
272521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
273521d9a4cSLisandro Dalcin 
274521d9a4cSLisandro Dalcin    Level: intermediate
275521d9a4cSLisandro Dalcin 
276*c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
277521d9a4cSLisandro Dalcin @*/
27819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
279521d9a4cSLisandro Dalcin {
280521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
28188f0584fSBarry Smith 
282521d9a4cSLisandro Dalcin   PetscFunctionBegin;
283521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
284521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
28519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
286521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
287521d9a4cSLisandro Dalcin }
288521d9a4cSLisandro Dalcin 
289521d9a4cSLisandro Dalcin #undef __FUNCT__
290*c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
291*c0dedaeaSBarry Smith /*@C
292*c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
293*c0dedaeaSBarry Smith 
294*c0dedaeaSBarry Smith    Logically Collective on DM
295*c0dedaeaSBarry Smith 
296*c0dedaeaSBarry Smith    Input Parameter:
297*c0dedaeaSBarry Smith .  dm - the DM context
298*c0dedaeaSBarry Smith 
299*c0dedaeaSBarry Smith    Output Parameter:
300*c0dedaeaSBarry Smith .  ctype - the matrix type
301*c0dedaeaSBarry Smith 
302*c0dedaeaSBarry Smith    Options Database:
303*c0dedaeaSBarry Smith .   -dm_mat_type ctype
304*c0dedaeaSBarry Smith 
305*c0dedaeaSBarry Smith    Level: intermediate
306*c0dedaeaSBarry Smith 
307*c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
308*c0dedaeaSBarry Smith @*/
309*c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
310*c0dedaeaSBarry Smith {
311*c0dedaeaSBarry Smith   PetscFunctionBegin;
312*c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
313*c0dedaeaSBarry Smith   *ctype = dm->mattype;
314*c0dedaeaSBarry Smith   PetscFunctionReturn(0);
315*c0dedaeaSBarry Smith }
316*c0dedaeaSBarry Smith 
317*c0dedaeaSBarry Smith #undef __FUNCT__
318c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
319c688c046SMatthew G Knepley /*@
32034f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
321c688c046SMatthew G Knepley 
322c688c046SMatthew G Knepley   Not collective
323c688c046SMatthew G Knepley 
324c688c046SMatthew G Knepley   Input Parameter:
325c688c046SMatthew G Knepley . A - The Mat
326c688c046SMatthew G Knepley 
327c688c046SMatthew G Knepley   Output Parameter:
328c688c046SMatthew G Knepley . dm - The DM
329c688c046SMatthew G Knepley 
330c688c046SMatthew G Knepley   Level: intermediate
331c688c046SMatthew G Knepley 
332c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
333c688c046SMatthew G Knepley @*/
334c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
335c688c046SMatthew G Knepley {
336c688c046SMatthew G Knepley   PetscErrorCode ierr;
337c688c046SMatthew G Knepley 
338c688c046SMatthew G Knepley   PetscFunctionBegin;
339c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
340c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
341c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
342c688c046SMatthew G Knepley   PetscFunctionReturn(0);
343c688c046SMatthew G Knepley }
344c688c046SMatthew G Knepley 
345c688c046SMatthew G Knepley #undef __FUNCT__
346c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
347c688c046SMatthew G Knepley /*@
348c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
349c688c046SMatthew G Knepley 
350c688c046SMatthew G Knepley   Not collective
351c688c046SMatthew G Knepley 
352c688c046SMatthew G Knepley   Input Parameters:
353c688c046SMatthew G Knepley + A - The Mat
354c688c046SMatthew G Knepley - dm - The DM
355c688c046SMatthew G Knepley 
356c688c046SMatthew G Knepley   Level: intermediate
357c688c046SMatthew G Knepley 
358c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
359c688c046SMatthew G Knepley @*/
360c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
361c688c046SMatthew G Knepley {
362c688c046SMatthew G Knepley   PetscErrorCode ierr;
363c688c046SMatthew G Knepley 
364c688c046SMatthew G Knepley   PetscFunctionBegin;
365c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3668865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
367c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
368c688c046SMatthew G Knepley   PetscFunctionReturn(0);
369c688c046SMatthew G Knepley }
370c688c046SMatthew G Knepley 
371c688c046SMatthew G Knepley #undef __FUNCT__
3729a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3739a42bb27SBarry Smith /*@C
3749a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
375aa219208SBarry Smith    DMDA options in the database.
3769a42bb27SBarry Smith 
377aa219208SBarry Smith    Logically Collective on DMDA
3789a42bb27SBarry Smith 
3799a42bb27SBarry Smith    Input Parameter:
380aa219208SBarry Smith +  da - the DMDA context
3819a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3829a42bb27SBarry Smith 
3839a42bb27SBarry Smith    Notes:
3849a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3859a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3869a42bb27SBarry Smith 
3879a42bb27SBarry Smith    Level: advanced
3889a42bb27SBarry Smith 
389aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
3909a42bb27SBarry Smith 
3919a42bb27SBarry Smith .seealso: DMSetFromOptions()
3929a42bb27SBarry Smith @*/
3937087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3949a42bb27SBarry Smith {
3959a42bb27SBarry Smith   PetscErrorCode ierr;
3969a42bb27SBarry Smith 
3979a42bb27SBarry Smith   PetscFunctionBegin;
3989a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3999a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
4009a42bb27SBarry Smith   PetscFunctionReturn(0);
4019a42bb27SBarry Smith }
4029a42bb27SBarry Smith 
4039a42bb27SBarry Smith #undef __FUNCT__
40447c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
40547c6ae99SBarry Smith /*@
406aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
40747c6ae99SBarry Smith 
40847c6ae99SBarry Smith     Collective on DM
40947c6ae99SBarry Smith 
41047c6ae99SBarry Smith     Input Parameter:
41147c6ae99SBarry Smith .   dm - the DM object to destroy
41247c6ae99SBarry Smith 
41347c6ae99SBarry Smith     Level: developer
41447c6ae99SBarry Smith 
415e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
41647c6ae99SBarry Smith 
41747c6ae99SBarry Smith @*/
418fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
41947c6ae99SBarry Smith {
420af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
421dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
42247c6ae99SBarry Smith   PetscErrorCode ierr;
42347c6ae99SBarry Smith 
42447c6ae99SBarry Smith   PetscFunctionBegin;
4256bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4266bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
42787e657c6SBarry Smith 
428a546ed68SMatthew G. Knepley   /* I think it makes sense to dump all attached things when you are destroyed, which also eliminates circular references */
429a546ed68SMatthew G. Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
430a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "pmat", NULL);CHKERRQ(ierr);
431a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "nullspace", NULL);CHKERRQ(ierr);
432a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "nearnullspace", NULL);CHKERRQ(ierr);
433a546ed68SMatthew G. Knepley   }
43487e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
435732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4368865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4378865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
438732e2eb9SMatthew G Knepley   }
439dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
44071cd77b2SBarry Smith   if ((*dm)->x) {
441c688c046SMatthew G Knepley     DM obj;
442c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
443c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
44471cd77b2SBarry Smith   }
4452348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4462348bcf4SPeter Brune   if ((*dm)->x) {
4472348bcf4SPeter Brune     DM obj;
4482348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4492348bcf4SPeter Brune     if (obj == *dm) cnt++;
4502348bcf4SPeter Brune   }
451732e2eb9SMatthew G Knepley 
4526bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
453732e2eb9SMatthew G Knepley   /*
454732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
455732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
456732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
457732e2eb9SMatthew G Knepley   */
4586bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4596bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
460732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4616bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4626bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
463732e2eb9SMatthew G Knepley   }
464dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
465dfe15315SJed Brown     nnext = nlink->next;
466dfe15315SJed 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);
467dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
468dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
469dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
470dfe15315SJed Brown   }
4710298fd71SBarry Smith   (*dm)->namedglobal = NULL;
4721a266240SBarry Smith 
4732348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */
4742348bcf4SPeter Brune     nnext = nlink->next;
4752348bcf4SPeter 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);
4762348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4772348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4782348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4792348bcf4SPeter Brune   }
4800298fd71SBarry Smith   (*dm)->namedlocal = NULL;
4812348bcf4SPeter Brune 
482b17ce1afSJed Brown   /* Destroy the list of hooks */
483c833c3b5SJed Brown   {
484c833c3b5SJed Brown     DMCoarsenHookLink link,next;
485b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
486b17ce1afSJed Brown       next = link->next;
487b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
488b17ce1afSJed Brown     }
4890298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
490c833c3b5SJed Brown   }
491c833c3b5SJed Brown   {
492c833c3b5SJed Brown     DMRefineHookLink link,next;
493c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
494c833c3b5SJed Brown       next = link->next;
495c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
496c833c3b5SJed Brown     }
4970298fd71SBarry Smith     (*dm)->refinehook = NULL;
498c833c3b5SJed Brown   }
499be081cd6SPeter Brune   {
500be081cd6SPeter Brune     DMSubDomainHookLink link,next;
501be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
502be081cd6SPeter Brune       next = link->next;
503be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
504be081cd6SPeter Brune     }
5050298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
506be081cd6SPeter Brune   }
507baf369e7SPeter Brune   {
508baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
509baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
510baf369e7SPeter Brune       next = link->next;
511baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
512baf369e7SPeter Brune     }
5130298fd71SBarry Smith     (*dm)->gtolhook = NULL;
514baf369e7SPeter Brune   }
515aa1993deSMatthew G Knepley   /* Destroy the work arrays */
516aa1993deSMatthew G Knepley   {
517aa1993deSMatthew G Knepley     DMWorkLink link,next;
518aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
519aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
520aa1993deSMatthew G Knepley       next = link->next;
521aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
522aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
523aa1993deSMatthew G Knepley     }
5240298fd71SBarry Smith     (*dm)->workin = NULL;
525aa1993deSMatthew G Knepley   }
526b17ce1afSJed Brown 
52752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
52852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
52952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
53052536dc3SBarry Smith 
5311a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5321a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5331a266240SBarry Smith   }
53487e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
53571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5364dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5376bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5386bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
5396bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
540073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
54188ed4aceSMatthew G Knepley 
54288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
54388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
5448b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
54588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
54688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
547af122d2aSMatthew G Knepley 
5486636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
5496636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
5506636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
5516636e97aSMatthew G Knepley 
552af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
553af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
554af122d2aSMatthew G Knepley   }
555af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
556732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
557f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*dm);CHKERRQ(ierr);
558732e2eb9SMatthew G Knepley 
5596bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
560435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
561732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
56247c6ae99SBarry Smith   PetscFunctionReturn(0);
56347c6ae99SBarry Smith }
56447c6ae99SBarry Smith 
56547c6ae99SBarry Smith #undef __FUNCT__
566d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
567d7bf68aeSBarry Smith /*@
568d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
569d7bf68aeSBarry Smith 
570d7bf68aeSBarry Smith     Collective on DM
571d7bf68aeSBarry Smith 
572d7bf68aeSBarry Smith     Input Parameter:
573d7bf68aeSBarry Smith .   dm - the DM object to setup
574d7bf68aeSBarry Smith 
575d7bf68aeSBarry Smith     Level: developer
576d7bf68aeSBarry Smith 
577e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
578d7bf68aeSBarry Smith 
579d7bf68aeSBarry Smith @*/
5807087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
581d7bf68aeSBarry Smith {
582d7bf68aeSBarry Smith   PetscErrorCode ierr;
583d7bf68aeSBarry Smith 
584d7bf68aeSBarry Smith   PetscFunctionBegin;
585171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5868387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
587d7bf68aeSBarry Smith   if (dm->ops->setup) {
588d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
589d7bf68aeSBarry Smith   }
5908387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
591d7bf68aeSBarry Smith   PetscFunctionReturn(0);
592d7bf68aeSBarry Smith }
593d7bf68aeSBarry Smith 
594d7bf68aeSBarry Smith #undef __FUNCT__
595d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
596d7bf68aeSBarry Smith /*@
597d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
598d7bf68aeSBarry Smith 
599d7bf68aeSBarry Smith     Collective on DM
600d7bf68aeSBarry Smith 
601d7bf68aeSBarry Smith     Input Parameter:
602d7bf68aeSBarry Smith .   dm - the DM object to set options for
603d7bf68aeSBarry Smith 
604732e2eb9SMatthew G Knepley     Options Database:
605dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
606dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
607171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
608171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
609732e2eb9SMatthew G Knepley 
610d7bf68aeSBarry Smith     Level: developer
611d7bf68aeSBarry Smith 
612e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
613d7bf68aeSBarry Smith 
614d7bf68aeSBarry Smith @*/
6157087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
616d7bf68aeSBarry Smith {
617f55f929bSMatthew G Knepley   char           typeName[256] = MATAIJ;
618ca266f36SBarry Smith   PetscBool      flg;
619d7bf68aeSBarry Smith   PetscErrorCode ierr;
620d7bf68aeSBarry Smith 
621d7bf68aeSBarry Smith   PetscFunctionBegin;
622171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6233194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
6240298fd71SBarry 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);
625f9ba7244SBarry Smith   ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
626f9ba7244SBarry Smith   if (flg) {
627f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
628f9ba7244SBarry Smith   }
6298caf3d72SBarry 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);
630073dac72SJed Brown   if (flg) {
631521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
632073dac72SJed Brown   }
6330298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
634f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
635f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
636f9ba7244SBarry Smith   }
637f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
638f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
63982fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
640146574abSBarry Smith   ierr = DMViewFromOptions(dm,NULL,"-dm_view");CHKERRQ(ierr);
641d7bf68aeSBarry Smith   PetscFunctionReturn(0);
642d7bf68aeSBarry Smith }
643d7bf68aeSBarry Smith 
644d7bf68aeSBarry Smith #undef __FUNCT__
64547c6ae99SBarry Smith #define __FUNCT__ "DMView"
646fc9bc008SSatish Balay /*@C
647aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
64847c6ae99SBarry Smith 
64947c6ae99SBarry Smith     Collective on DM
65047c6ae99SBarry Smith 
65147c6ae99SBarry Smith     Input Parameter:
65247c6ae99SBarry Smith +   dm - the DM object to view
65347c6ae99SBarry Smith -   v - the viewer
65447c6ae99SBarry Smith 
65547c6ae99SBarry Smith     Level: developer
65647c6ae99SBarry Smith 
657e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
65847c6ae99SBarry Smith 
65947c6ae99SBarry Smith @*/
6607087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
66147c6ae99SBarry Smith {
66247c6ae99SBarry Smith   PetscErrorCode ierr;
66332c0f0efSBarry Smith   PetscBool      isbinary;
66447c6ae99SBarry Smith 
66547c6ae99SBarry Smith   PetscFunctionBegin;
666171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6673014e516SBarry Smith   if (!v) {
668ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6693014e516SBarry Smith   }
67032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
67132c0f0efSBarry Smith   if (isbinary) {
67255849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
67332c0f0efSBarry Smith     char     type[256];
67432c0f0efSBarry Smith 
67532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
67632c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
67732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
67832c0f0efSBarry Smith   }
6790c010503SBarry Smith   if (dm->ops->view) {
6800c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
68147c6ae99SBarry Smith   }
68247c6ae99SBarry Smith   PetscFunctionReturn(0);
68347c6ae99SBarry Smith }
68447c6ae99SBarry Smith 
68547c6ae99SBarry Smith #undef __FUNCT__
68647c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
68747c6ae99SBarry Smith /*@
688aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
68947c6ae99SBarry Smith 
69047c6ae99SBarry Smith     Collective on DM
69147c6ae99SBarry Smith 
69247c6ae99SBarry Smith     Input Parameter:
69347c6ae99SBarry Smith .   dm - the DM object
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith     Output Parameter:
69647c6ae99SBarry Smith .   vec - the global vector
69747c6ae99SBarry Smith 
698073dac72SJed Brown     Level: beginner
69947c6ae99SBarry Smith 
700e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
70147c6ae99SBarry Smith 
70247c6ae99SBarry Smith @*/
7037087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
70447c6ae99SBarry Smith {
70547c6ae99SBarry Smith   PetscErrorCode ierr;
70647c6ae99SBarry Smith 
70747c6ae99SBarry Smith   PetscFunctionBegin;
708171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
70947c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
71047c6ae99SBarry Smith   PetscFunctionReturn(0);
71147c6ae99SBarry Smith }
71247c6ae99SBarry Smith 
71347c6ae99SBarry Smith #undef __FUNCT__
71447c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
71547c6ae99SBarry Smith /*@
716aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith     Not Collective
71947c6ae99SBarry Smith 
72047c6ae99SBarry Smith     Input Parameter:
72147c6ae99SBarry Smith .   dm - the DM object
72247c6ae99SBarry Smith 
72347c6ae99SBarry Smith     Output Parameter:
72447c6ae99SBarry Smith .   vec - the local vector
72547c6ae99SBarry Smith 
726073dac72SJed Brown     Level: beginner
72747c6ae99SBarry Smith 
728e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
72947c6ae99SBarry Smith 
73047c6ae99SBarry Smith @*/
7317087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
73247c6ae99SBarry Smith {
73347c6ae99SBarry Smith   PetscErrorCode ierr;
73447c6ae99SBarry Smith 
73547c6ae99SBarry Smith   PetscFunctionBegin;
736171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
73747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
73847c6ae99SBarry Smith   PetscFunctionReturn(0);
73947c6ae99SBarry Smith }
74047c6ae99SBarry Smith 
74147c6ae99SBarry Smith #undef __FUNCT__
7421411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
7431411c6eeSJed Brown /*@
7441411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
7451411c6eeSJed Brown 
7461411c6eeSJed Brown    Collective on DM
7471411c6eeSJed Brown 
7481411c6eeSJed Brown    Input Parameter:
7491411c6eeSJed Brown .  dm - the DM that provides the mapping
7501411c6eeSJed Brown 
7511411c6eeSJed Brown    Output Parameter:
7521411c6eeSJed Brown .  ltog - the mapping
7531411c6eeSJed Brown 
7541411c6eeSJed Brown    Level: intermediate
7551411c6eeSJed Brown 
7561411c6eeSJed Brown    Notes:
7571411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
7581411c6eeSJed Brown    MatSetLocalToGlobalMapping().
7591411c6eeSJed Brown 
7601411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
7611411c6eeSJed Brown @*/
7627087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7631411c6eeSJed Brown {
7641411c6eeSJed Brown   PetscErrorCode ierr;
7651411c6eeSJed Brown 
7661411c6eeSJed Brown   PetscFunctionBegin;
7671411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7681411c6eeSJed Brown   PetscValidPointer(ltog,2);
7691411c6eeSJed Brown   if (!dm->ltogmap) {
77037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
77137d0c07bSMatthew G Knepley 
77237d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
77337d0c07bSMatthew G Knepley     if (section) {
77437d0c07bSMatthew G Knepley       PetscInt *ltog;
77537d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
77637d0c07bSMatthew G Knepley 
77737d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
77837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
77937d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
78037d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
78137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
78237d0c07bSMatthew G Knepley         PetscInt dof, off, c;
78337d0c07bSMatthew G Knepley 
78437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
78537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
78637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
78737d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
78837d0c07bSMatthew G Knepley           ltog[l] = off+c;
78937d0c07bSMatthew G Knepley         }
79037d0c07bSMatthew G Knepley       }
79137d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
7923bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
79337d0c07bSMatthew G Knepley     } else {
794184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
795184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7961411c6eeSJed Brown     }
79737d0c07bSMatthew G Knepley   }
7981411c6eeSJed Brown   *ltog = dm->ltogmap;
7991411c6eeSJed Brown   PetscFunctionReturn(0);
8001411c6eeSJed Brown }
8011411c6eeSJed Brown 
8021411c6eeSJed Brown #undef __FUNCT__
8031411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
8041411c6eeSJed Brown /*@
8051411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
8061411c6eeSJed Brown 
8071411c6eeSJed Brown    Collective on DM
8081411c6eeSJed Brown 
8091411c6eeSJed Brown    Input Parameter:
8101411c6eeSJed Brown .  da - the distributed array that provides the mapping
8111411c6eeSJed Brown 
8121411c6eeSJed Brown    Output Parameter:
8131411c6eeSJed Brown .  ltog - the block mapping
8141411c6eeSJed Brown 
8151411c6eeSJed Brown    Level: intermediate
8161411c6eeSJed Brown 
8171411c6eeSJed Brown    Notes:
8181411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
8191411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
8201411c6eeSJed Brown 
8211411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
8221411c6eeSJed Brown @*/
8237087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
8241411c6eeSJed Brown {
8251411c6eeSJed Brown   PetscErrorCode ierr;
8261411c6eeSJed Brown 
8271411c6eeSJed Brown   PetscFunctionBegin;
8281411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8291411c6eeSJed Brown   PetscValidPointer(ltog,2);
8301411c6eeSJed Brown   if (!dm->ltogmapb) {
8311411c6eeSJed Brown     PetscInt bs;
8321411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
8331411c6eeSJed Brown     if (bs > 1) {
834184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmappingblock) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
835184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
8361411c6eeSJed Brown     } else {
8371411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
8381411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
8391411c6eeSJed Brown     }
8401411c6eeSJed Brown   }
8411411c6eeSJed Brown   *ltog = dm->ltogmapb;
8421411c6eeSJed Brown   PetscFunctionReturn(0);
8431411c6eeSJed Brown }
8441411c6eeSJed Brown 
8451411c6eeSJed Brown #undef __FUNCT__
8461411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
8471411c6eeSJed Brown /*@
8481411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
8491411c6eeSJed Brown 
8501411c6eeSJed Brown    Not Collective
8511411c6eeSJed Brown 
8521411c6eeSJed Brown    Input Parameter:
8531411c6eeSJed Brown .  dm - the DM with block structure
8541411c6eeSJed Brown 
8551411c6eeSJed Brown    Output Parameter:
8561411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
8571411c6eeSJed Brown 
8581411c6eeSJed Brown    Level: intermediate
8591411c6eeSJed Brown 
8601411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
8611411c6eeSJed Brown @*/
8627087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
8631411c6eeSJed Brown {
8641411c6eeSJed Brown   PetscFunctionBegin;
8651411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8661411c6eeSJed Brown   PetscValidPointer(bs,2);
8671411c6eeSJed 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");
8681411c6eeSJed Brown   *bs = dm->bs;
8691411c6eeSJed Brown   PetscFunctionReturn(0);
8701411c6eeSJed Brown }
8711411c6eeSJed Brown 
8721411c6eeSJed Brown #undef __FUNCT__
873e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
87447c6ae99SBarry Smith /*@
875e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith     Collective on DM
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith     Input Parameter:
88047c6ae99SBarry Smith +   dm1 - the DM object
88147c6ae99SBarry Smith -   dm2 - the second, finer DM object
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith     Output Parameter:
88447c6ae99SBarry Smith +  mat - the interpolation
88547c6ae99SBarry Smith -  vec - the scaling (optional)
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith     Level: developer
88847c6ae99SBarry Smith 
88985afcc9aSBarry 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
89085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
891d52bd9f3SBarry Smith 
8921f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
893d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
89485afcc9aSBarry Smith 
89585afcc9aSBarry Smith 
896e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith @*/
899e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
90047c6ae99SBarry Smith {
90147c6ae99SBarry Smith   PetscErrorCode ierr;
90247c6ae99SBarry Smith 
90347c6ae99SBarry Smith   PetscFunctionBegin;
904171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
905171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
90625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
90747c6ae99SBarry Smith   PetscFunctionReturn(0);
90847c6ae99SBarry Smith }
90947c6ae99SBarry Smith 
91047c6ae99SBarry Smith #undef __FUNCT__
911e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
91247c6ae99SBarry Smith /*@
913e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith     Collective on DM
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith     Input Parameter:
91847c6ae99SBarry Smith +   dm1 - the DM object
91947c6ae99SBarry Smith -   dm2 - the second, finer DM object
92047c6ae99SBarry Smith 
92147c6ae99SBarry Smith     Output Parameter:
92247c6ae99SBarry Smith .   ctx - the injection
92347c6ae99SBarry Smith 
92447c6ae99SBarry Smith     Level: developer
92547c6ae99SBarry Smith 
92685afcc9aSBarry 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
92785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
92885afcc9aSBarry Smith 
929e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith @*/
932e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
93347c6ae99SBarry Smith {
93447c6ae99SBarry Smith   PetscErrorCode ierr;
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith   PetscFunctionBegin;
937171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
938171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
93947c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
94047c6ae99SBarry Smith   PetscFunctionReturn(0);
94147c6ae99SBarry Smith }
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith #undef __FUNCT__
944e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
945b412c318SBarry Smith /*@
946b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith     Collective on DM
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith     Input Parameter:
95147c6ae99SBarry Smith +   dm - the DM object
952b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
95347c6ae99SBarry Smith 
95447c6ae99SBarry Smith     Output Parameter:
95547c6ae99SBarry Smith .   coloring - the coloring
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith     Level: developer
95847c6ae99SBarry Smith 
959b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
96047c6ae99SBarry Smith 
961aab9d709SJed Brown @*/
962b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
96347c6ae99SBarry Smith {
96447c6ae99SBarry Smith   PetscErrorCode ierr;
96547c6ae99SBarry Smith 
96647c6ae99SBarry Smith   PetscFunctionBegin;
967171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
968ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
969b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
97047c6ae99SBarry Smith   PetscFunctionReturn(0);
97147c6ae99SBarry Smith }
97247c6ae99SBarry Smith 
97347c6ae99SBarry Smith #undef __FUNCT__
974950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
975b412c318SBarry Smith /*@
976950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
97747c6ae99SBarry Smith 
97847c6ae99SBarry Smith     Collective on DM
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith     Input Parameter:
981b412c318SBarry Smith .   dm - the DM object
98247c6ae99SBarry Smith 
98347c6ae99SBarry Smith     Output Parameter:
98447c6ae99SBarry Smith .   mat - the empty Jacobian
98547c6ae99SBarry Smith 
986073dac72SJed Brown     Level: beginner
98747c6ae99SBarry Smith 
98894013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
98994013140SBarry Smith        do not need to do it yourself.
99094013140SBarry Smith 
99194013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
992aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
99394013140SBarry Smith 
99494013140SBarry 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
99594013140SBarry Smith        internally by PETSc.
99694013140SBarry Smith 
99794013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
998aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
99994013140SBarry Smith 
1000b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
100147c6ae99SBarry Smith 
1002aab9d709SJed Brown @*/
1003b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
100447c6ae99SBarry Smith {
100547c6ae99SBarry Smith   PetscErrorCode ierr;
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith   PetscFunctionBegin;
1008171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1009519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
1010607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1011235683edSBarry Smith #endif
1012c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1013c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1014b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
101547c6ae99SBarry Smith   PetscFunctionReturn(0);
101647c6ae99SBarry Smith }
101747c6ae99SBarry Smith 
101847c6ae99SBarry Smith #undef __FUNCT__
1019732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1020732e2eb9SMatthew G Knepley /*@
1021950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1022732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1023732e2eb9SMatthew G Knepley 
1024732e2eb9SMatthew G Knepley   Logically Collective on DMDA
1025732e2eb9SMatthew G Knepley 
1026732e2eb9SMatthew G Knepley   Input Parameter:
1027732e2eb9SMatthew G Knepley + dm - the DM
1028732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1029732e2eb9SMatthew G Knepley 
1030732e2eb9SMatthew G Knepley   Level: developer
1031950540a4SJed Brown .seealso DMCreateMatrix()
1032732e2eb9SMatthew G Knepley @*/
1033732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1034732e2eb9SMatthew G Knepley {
1035732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1036732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1037732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1038732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1039732e2eb9SMatthew G Knepley }
1040732e2eb9SMatthew G Knepley 
1041732e2eb9SMatthew G Knepley #undef __FUNCT__
1042a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1043a89ea682SMatthew G Knepley /*@C
1044aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1045a89ea682SMatthew G Knepley 
1046a89ea682SMatthew G Knepley   Not Collective
1047a89ea682SMatthew G Knepley 
1048a89ea682SMatthew G Knepley   Input Parameters:
1049a89ea682SMatthew G Knepley + dm - the DM object
1050aa1993deSMatthew G Knepley . count - The minium size
1051aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1052a89ea682SMatthew G Knepley 
1053a89ea682SMatthew G Knepley   Output Parameter:
1054a89ea682SMatthew G Knepley . array - the work array
1055a89ea682SMatthew G Knepley 
1056a89ea682SMatthew G Knepley   Level: developer
1057a89ea682SMatthew G Knepley 
1058a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1059a89ea682SMatthew G Knepley @*/
1060aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1061a89ea682SMatthew G Knepley {
1062a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1063aa1993deSMatthew G Knepley   DMWorkLink     link;
1064aa1993deSMatthew G Knepley   size_t         size;
1065a89ea682SMatthew G Knepley 
1066a89ea682SMatthew G Knepley   PetscFunctionBegin;
1067a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1068aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1069aa1993deSMatthew G Knepley   if (dm->workin) {
1070aa1993deSMatthew G Knepley     link       = dm->workin;
1071aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1072aa1993deSMatthew G Knepley   } else {
1073aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
1074a89ea682SMatthew G Knepley   }
1075aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1076aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1077aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1078aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1079aa1993deSMatthew G Knepley     link->bytes = size*count;
1080aa1993deSMatthew G Knepley   }
1081aa1993deSMatthew G Knepley   link->next   = dm->workout;
1082aa1993deSMatthew G Knepley   dm->workout  = link;
1083aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1084a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1085a89ea682SMatthew G Knepley }
1086a89ea682SMatthew G Knepley 
1087aa1993deSMatthew G Knepley #undef __FUNCT__
1088aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1089aa1993deSMatthew G Knepley /*@C
1090aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1091aa1993deSMatthew G Knepley 
1092aa1993deSMatthew G Knepley   Not Collective
1093aa1993deSMatthew G Knepley 
1094aa1993deSMatthew G Knepley   Input Parameters:
1095aa1993deSMatthew G Knepley + dm - the DM object
1096aa1993deSMatthew G Knepley . count - The minium size
1097aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1098aa1993deSMatthew G Knepley 
1099aa1993deSMatthew G Knepley   Output Parameter:
1100aa1993deSMatthew G Knepley . array - the work array
1101aa1993deSMatthew G Knepley 
1102aa1993deSMatthew G Knepley   Level: developer
1103aa1993deSMatthew G Knepley 
1104aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1105aa1993deSMatthew G Knepley @*/
1106aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1107aa1993deSMatthew G Knepley {
1108aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1109aa1993deSMatthew G Knepley 
1110aa1993deSMatthew G Knepley   PetscFunctionBegin;
1111aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1112aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1113aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1114aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1115aa1993deSMatthew G Knepley       *p           = link->next;
1116aa1993deSMatthew G Knepley       link->next   = dm->workin;
1117aa1993deSMatthew G Knepley       dm->workin   = link;
11180298fd71SBarry Smith       *(void**)mem = NULL;
1119aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1120aa1993deSMatthew G Knepley     }
1121aa1993deSMatthew G Knepley   }
1122aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1123aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1124aa1993deSMatthew G Knepley }
1125e7c4fc90SDmitry Karpeev 
1126e7c4fc90SDmitry Karpeev #undef __FUNCT__
1127435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1128435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1129435a35e8SMatthew G Knepley {
1130435a35e8SMatthew G Knepley   PetscFunctionBegin;
1131435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
113282f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1133435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1134435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1135435a35e8SMatthew G Knepley }
1136435a35e8SMatthew G Knepley 
1137435a35e8SMatthew G Knepley #undef __FUNCT__
11384d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
11394f3b5142SJed Brown /*@C
11404d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
11414d343eeaSMatthew G Knepley 
11424d343eeaSMatthew G Knepley   Not collective
11434d343eeaSMatthew G Knepley 
11444d343eeaSMatthew G Knepley   Input Parameter:
11454d343eeaSMatthew G Knepley . dm - the DM object
11464d343eeaSMatthew G Knepley 
11474d343eeaSMatthew G Knepley   Output Parameters:
11480298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
11490298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
11500298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
11514d343eeaSMatthew G Knepley 
11524d343eeaSMatthew G Knepley   Level: intermediate
11534d343eeaSMatthew G Knepley 
115421c9b008SJed Brown   Notes:
115521c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
115621c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
115721c9b008SJed Brown   PetscFree().
115821c9b008SJed Brown 
11594d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11604d343eeaSMatthew G Knepley @*/
116137d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11624d343eeaSMatthew G Knepley {
116337d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11644d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11654d343eeaSMatthew G Knepley 
11664d343eeaSMatthew G Knepley   PetscFunctionBegin;
11674d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
116869ca1f37SDmitry Karpeev   if (numFields) {
116969ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
117069ca1f37SDmitry Karpeev     *numFields = 0;
117169ca1f37SDmitry Karpeev   }
117237d0c07bSMatthew G Knepley   if (fieldNames) {
117337d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11740298fd71SBarry Smith     *fieldNames = NULL;
117569ca1f37SDmitry Karpeev   }
117669ca1f37SDmitry Karpeev   if (fields) {
117769ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11780298fd71SBarry Smith     *fields = NULL;
117969ca1f37SDmitry Karpeev   }
118037d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
118137d0c07bSMatthew G Knepley   if (section) {
118237d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
118337d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
118437d0c07bSMatthew G Knepley 
118537d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
118637d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
118737d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt*,&fieldIndices);CHKERRQ(ierr);
118837d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
118937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
119037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
119137d0c07bSMatthew G Knepley     }
119237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
119337d0c07bSMatthew G Knepley       PetscInt gdof;
119437d0c07bSMatthew G Knepley 
119537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
119637d0c07bSMatthew G Knepley       if (gdof > 0) {
119737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
119837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
119937d0c07bSMatthew G Knepley 
120037d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
120137d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
120237d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
120337d0c07bSMatthew G Knepley         }
120437d0c07bSMatthew G Knepley       }
120537d0c07bSMatthew G Knepley     }
120637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
120737d0c07bSMatthew G Knepley       ierr          = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
120837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
120937d0c07bSMatthew G Knepley     }
121037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
121137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
121237d0c07bSMatthew G Knepley 
121337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
121437d0c07bSMatthew G Knepley       if (gdof > 0) {
121537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
121637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
121737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
121837d0c07bSMatthew G Knepley 
121937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
122037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
122137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
122237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
122337d0c07bSMatthew G Knepley           }
122437d0c07bSMatthew G Knepley         }
122537d0c07bSMatthew G Knepley       }
122637d0c07bSMatthew G Knepley     }
12278865f1eaSKarl Rupp     if (numFields) *numFields = nF;
122837d0c07bSMatthew G Knepley     if (fieldNames) {
122937d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char*), fieldNames);CHKERRQ(ierr);
123037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
123137d0c07bSMatthew G Knepley         const char *fieldName;
123237d0c07bSMatthew G Knepley 
123337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
123437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
123537d0c07bSMatthew G Knepley       }
123637d0c07bSMatthew G Knepley     }
123737d0c07bSMatthew G Knepley     if (fields) {
123837d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
123937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
124082f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
124137d0c07bSMatthew G Knepley       }
124237d0c07bSMatthew G Knepley     }
124337d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
12448865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
12458865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
124669ca1f37SDmitry Karpeev   }
12474d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
12484d343eeaSMatthew G Knepley }
12494d343eeaSMatthew G Knepley 
125016621825SDmitry Karpeev 
125116621825SDmitry Karpeev #undef __FUNCT__
125216621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
125316621825SDmitry Karpeev /*@C
125416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
125516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
125616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1257e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1258e7c4fc90SDmitry Karpeev 
1259e7c4fc90SDmitry Karpeev   Not collective
1260e7c4fc90SDmitry Karpeev 
1261e7c4fc90SDmitry Karpeev   Input Parameter:
1262e7c4fc90SDmitry Karpeev . dm - the DM object
1263e7c4fc90SDmitry Karpeev 
1264e7c4fc90SDmitry Karpeev   Output Parameters:
12650298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
12660298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
12670298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12680298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1269e7c4fc90SDmitry Karpeev 
1270e7c4fc90SDmitry Karpeev   Level: intermediate
1271e7c4fc90SDmitry Karpeev 
1272e7c4fc90SDmitry Karpeev   Notes:
1273e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1274e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1275e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1276e7c4fc90SDmitry Karpeev 
1277e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1278e7c4fc90SDmitry Karpeev @*/
127916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1280e7c4fc90SDmitry Karpeev {
1281e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1282e7c4fc90SDmitry Karpeev 
1283e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1284e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12858865f1eaSKarl Rupp   if (len) {
12868865f1eaSKarl Rupp     PetscValidPointer(len,2);
12878865f1eaSKarl Rupp     *len = 0;
12888865f1eaSKarl Rupp   }
12898865f1eaSKarl Rupp   if (namelist) {
12908865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12918865f1eaSKarl Rupp     *namelist = 0;
12928865f1eaSKarl Rupp   }
12938865f1eaSKarl Rupp   if (islist) {
12948865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12958865f1eaSKarl Rupp     *islist = 0;
12968865f1eaSKarl Rupp   }
12978865f1eaSKarl Rupp   if (dmlist) {
12988865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12998865f1eaSKarl Rupp     *dmlist = 0;
13008865f1eaSKarl Rupp   }
1301f3f0edfdSDmitry Karpeev   /*
1302f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1303f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1304f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1305f3f0edfdSDmitry Karpeev    */
1306ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
130716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1308435a35e8SMatthew G Knepley     PetscSection section;
1309435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1310435a35e8SMatthew G Knepley 
1311435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1312435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1313435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1314435a35e8SMatthew G Knepley       *len = numFields;
1315435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1316435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1317435a35e8SMatthew G Knepley         const char *fieldName;
1318435a35e8SMatthew G Knepley 
1319435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1320435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1321435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1322435a35e8SMatthew G Knepley       }
1323435a35e8SMatthew G Knepley     } else {
132469ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1325e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
13260298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1327e7c4fc90SDmitry Karpeev     }
13288865f1eaSKarl Rupp   } else {
132916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
133016621825SDmitry Karpeev   }
133116621825SDmitry Karpeev   PetscFunctionReturn(0);
133216621825SDmitry Karpeev }
133316621825SDmitry Karpeev 
133416621825SDmitry Karpeev #undef __FUNCT__
1335435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1336435a35e8SMatthew G Knepley /*@C
1337435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1338435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1339435a35e8SMatthew G Knepley 
1340435a35e8SMatthew G Knepley   Not collective
1341435a35e8SMatthew G Knepley 
1342435a35e8SMatthew G Knepley   Input Parameters:
1343435a35e8SMatthew G Knepley + dm - the DM object
1344435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
13450298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1346435a35e8SMatthew G Knepley 
1347435a35e8SMatthew G Knepley   Output Parameters:
1348435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1349435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1350435a35e8SMatthew G Knepley 
1351435a35e8SMatthew G Knepley   Level: intermediate
1352435a35e8SMatthew G Knepley 
1353435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1354435a35e8SMatthew G Knepley @*/
1355435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1356435a35e8SMatthew G Knepley {
1357435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1358435a35e8SMatthew G Knepley 
1359435a35e8SMatthew G Knepley   PetscFunctionBegin;
1360435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1361435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
13628865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
13638865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1364435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1365435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
136682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1367435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1368435a35e8SMatthew G Knepley }
1369435a35e8SMatthew G Knepley 
137016621825SDmitry Karpeev 
137116621825SDmitry Karpeev #undef __FUNCT__
137216621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
137316621825SDmitry Karpeev /*@C
13748d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13758d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13768d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13778d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13788d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
137916621825SDmitry Karpeev 
138016621825SDmitry Karpeev   Not collective
138116621825SDmitry Karpeev 
138216621825SDmitry Karpeev   Input Parameter:
138316621825SDmitry Karpeev . dm - the DM object
138416621825SDmitry Karpeev 
138516621825SDmitry Karpeev   Output Parameters:
13860298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13870298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13880298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13890298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13900298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
139116621825SDmitry Karpeev 
139216621825SDmitry Karpeev   Level: intermediate
139316621825SDmitry Karpeev 
139416621825SDmitry Karpeev   Notes:
139516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
139616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
139716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
139816621825SDmitry Karpeev 
13998d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
140016621825SDmitry Karpeev @*/
14018d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
140216621825SDmitry Karpeev {
140316621825SDmitry Karpeev   PetscErrorCode      ierr;
1404be081cd6SPeter Brune   DMSubDomainHookLink link;
1405be081cd6SPeter Brune   PetscInt            i,l;
140616621825SDmitry Karpeev 
140716621825SDmitry Karpeev   PetscFunctionBegin;
140816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
140914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14100298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14110298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14120298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14130298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1414f3f0edfdSDmitry Karpeev   /*
1415f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1416f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1417f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1418f3f0edfdSDmitry Karpeev    */
1419ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
142016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1421be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
142214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
142314a18fd3SPeter Brune     if (dmlist) {
1424be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1425be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1426be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1427be081cd6SPeter Brune         }
1428d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1429e7c4fc90SDmitry Karpeev       }
143014a18fd3SPeter Brune     }
143114a18fd3SPeter Brune     if (len) *len = l;
143214a18fd3SPeter Brune   }
1433e30e807fSPeter Brune   PetscFunctionReturn(0);
1434e30e807fSPeter Brune }
1435e30e807fSPeter Brune 
1436e30e807fSPeter Brune 
1437e30e807fSPeter Brune #undef __FUNCT__
1438e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1439e30e807fSPeter Brune /*@C
1440e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1441e30e807fSPeter Brune 
1442e30e807fSPeter Brune   Not collective
1443e30e807fSPeter Brune 
1444e30e807fSPeter Brune   Input Parameters:
1445e30e807fSPeter Brune + dm - the DM object
1446e30e807fSPeter Brune . n  - the number of subdomain scatters
1447e30e807fSPeter Brune - subdms - the local subdomains
1448e30e807fSPeter Brune 
1449e30e807fSPeter Brune   Output Parameters:
1450e30e807fSPeter Brune + n     - the number of scatters returned
1451e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1452e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1453e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1454e30e807fSPeter Brune 
1455e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1456e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1457e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1458e30e807fSPeter Brune   solution and residual data.
1459e30e807fSPeter Brune 
1460e30e807fSPeter Brune   Level: developer
1461e30e807fSPeter Brune 
1462e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1463e30e807fSPeter Brune @*/
1464e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1465e30e807fSPeter Brune {
1466e30e807fSPeter Brune   PetscErrorCode ierr;
1467e30e807fSPeter Brune 
1468e30e807fSPeter Brune   PetscFunctionBegin;
1469e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1470e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1471e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1472e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
147382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1474e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1475e7c4fc90SDmitry Karpeev }
1476e7c4fc90SDmitry Karpeev 
1477731c8d9eSDmitry Karpeev #undef __FUNCT__
147847c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
147947c6ae99SBarry Smith /*@
148047c6ae99SBarry Smith   DMRefine - Refines a DM object
148147c6ae99SBarry Smith 
148247c6ae99SBarry Smith   Collective on DM
148347c6ae99SBarry Smith 
148447c6ae99SBarry Smith   Input Parameter:
148547c6ae99SBarry Smith + dm   - the DM object
148691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
148747c6ae99SBarry Smith 
148847c6ae99SBarry Smith   Output Parameter:
14890298fd71SBarry Smith . dmf - the refined DM, or NULL
1490ae0a1c52SMatthew G Knepley 
14910298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
149247c6ae99SBarry Smith 
149347c6ae99SBarry Smith   Level: developer
149447c6ae99SBarry Smith 
1495e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
149647c6ae99SBarry Smith @*/
14977087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
149847c6ae99SBarry Smith {
149947c6ae99SBarry Smith   PetscErrorCode   ierr;
1500c833c3b5SJed Brown   DMRefineHookLink link;
150147c6ae99SBarry Smith 
150247c6ae99SBarry Smith   PetscFunctionBegin;
1503732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
150447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15054057135bSMatthew G Knepley   if (*dmf) {
150643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15078865f1eaSKarl Rupp 
15088cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15098865f1eaSKarl Rupp 
1510644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15110598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1512656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15138865f1eaSKarl Rupp 
1514e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1515c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
15168865f1eaSKarl Rupp       if (link->refinehook) {
15178865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
15188865f1eaSKarl Rupp       }
1519c833c3b5SJed Brown     }
1520c833c3b5SJed Brown   }
1521c833c3b5SJed Brown   PetscFunctionReturn(0);
1522c833c3b5SJed Brown }
1523c833c3b5SJed Brown 
1524c833c3b5SJed Brown #undef __FUNCT__
1525c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1526bb9467b5SJed Brown /*@C
1527c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1528c833c3b5SJed Brown 
1529c833c3b5SJed Brown    Logically Collective
1530c833c3b5SJed Brown 
1531c833c3b5SJed Brown    Input Arguments:
1532c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1533c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1534c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
15350298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1536c833c3b5SJed Brown 
1537c833c3b5SJed Brown    Calling sequence of refinehook:
1538c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1539c833c3b5SJed Brown 
1540c833c3b5SJed Brown +  coarse - coarse level DM
1541c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1542c833c3b5SJed Brown -  ctx - optional user-defined function context
1543c833c3b5SJed Brown 
1544c833c3b5SJed Brown    Calling sequence for interphook:
1545c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1546c833c3b5SJed Brown 
1547c833c3b5SJed Brown +  coarse - coarse level DM
1548c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1549c833c3b5SJed Brown .  fine - fine level DM to update
1550c833c3b5SJed Brown -  ctx - optional user-defined function context
1551c833c3b5SJed Brown 
1552c833c3b5SJed Brown    Level: advanced
1553c833c3b5SJed Brown 
1554c833c3b5SJed Brown    Notes:
1555c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1556c833c3b5SJed Brown 
1557c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1558c833c3b5SJed Brown 
1559bb9467b5SJed Brown    This function is currently not available from Fortran.
1560bb9467b5SJed Brown 
1561c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1562c833c3b5SJed Brown @*/
1563c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1564c833c3b5SJed Brown {
1565c833c3b5SJed Brown   PetscErrorCode   ierr;
1566c833c3b5SJed Brown   DMRefineHookLink link,*p;
1567c833c3b5SJed Brown 
1568c833c3b5SJed Brown   PetscFunctionBegin;
1569c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1570c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1571c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1572c833c3b5SJed Brown   link->refinehook = refinehook;
1573c833c3b5SJed Brown   link->interphook = interphook;
1574c833c3b5SJed Brown   link->ctx        = ctx;
15750298fd71SBarry Smith   link->next       = NULL;
1576c833c3b5SJed Brown   *p               = link;
1577c833c3b5SJed Brown   PetscFunctionReturn(0);
1578c833c3b5SJed Brown }
1579c833c3b5SJed Brown 
1580c833c3b5SJed Brown #undef __FUNCT__
1581c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1582c833c3b5SJed Brown /*@
1583c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1584c833c3b5SJed Brown 
1585c833c3b5SJed Brown    Collective if any hooks are
1586c833c3b5SJed Brown 
1587c833c3b5SJed Brown    Input Arguments:
1588c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1589c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1590c833c3b5SJed Brown -  fine - finer DM to update
1591c833c3b5SJed Brown 
1592c833c3b5SJed Brown    Level: developer
1593c833c3b5SJed Brown 
1594c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1595c833c3b5SJed Brown @*/
1596c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1597c833c3b5SJed Brown {
1598c833c3b5SJed Brown   PetscErrorCode   ierr;
1599c833c3b5SJed Brown   DMRefineHookLink link;
1600c833c3b5SJed Brown 
1601c833c3b5SJed Brown   PetscFunctionBegin;
1602c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16038865f1eaSKarl Rupp     if (link->interphook) {
16048865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16058865f1eaSKarl Rupp     }
16064057135bSMatthew G Knepley   }
160747c6ae99SBarry Smith   PetscFunctionReturn(0);
160847c6ae99SBarry Smith }
160947c6ae99SBarry Smith 
161047c6ae99SBarry Smith #undef __FUNCT__
1611eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1612eb3f98d2SBarry Smith /*@
1613eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1614eb3f98d2SBarry Smith 
1615eb3f98d2SBarry Smith     Not Collective
1616eb3f98d2SBarry Smith 
1617eb3f98d2SBarry Smith     Input Parameter:
1618eb3f98d2SBarry Smith .   dm - the DM object
1619eb3f98d2SBarry Smith 
1620eb3f98d2SBarry Smith     Output Parameter:
1621eb3f98d2SBarry Smith .   level - number of refinements
1622eb3f98d2SBarry Smith 
1623eb3f98d2SBarry Smith     Level: developer
1624eb3f98d2SBarry Smith 
16256a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1626eb3f98d2SBarry Smith 
1627eb3f98d2SBarry Smith @*/
1628eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1629eb3f98d2SBarry Smith {
1630eb3f98d2SBarry Smith   PetscFunctionBegin;
1631eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1632eb3f98d2SBarry Smith   *level = dm->levelup;
1633eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1634eb3f98d2SBarry Smith }
1635eb3f98d2SBarry Smith 
1636eb3f98d2SBarry Smith #undef __FUNCT__
1637baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1638bb9467b5SJed Brown /*@C
1639baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1640baf369e7SPeter Brune 
1641baf369e7SPeter Brune    Logically Collective
1642baf369e7SPeter Brune 
1643baf369e7SPeter Brune    Input Arguments:
1644baf369e7SPeter Brune +  dm - the DM
1645baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1646baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
16470298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1648baf369e7SPeter Brune 
1649baf369e7SPeter Brune    Calling sequence for beginhook:
1650baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1651baf369e7SPeter Brune 
1652baf369e7SPeter Brune +  dm - global DM
1653baf369e7SPeter Brune .  g - global vector
1654baf369e7SPeter Brune .  mode - mode
1655baf369e7SPeter Brune .  l - local vector
1656baf369e7SPeter Brune -  ctx - optional user-defined function context
1657baf369e7SPeter Brune 
1658baf369e7SPeter Brune 
1659baf369e7SPeter Brune    Calling sequence for endhook:
1660ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1661baf369e7SPeter Brune 
1662baf369e7SPeter Brune +  global - global DM
1663baf369e7SPeter Brune -  ctx - optional user-defined function context
1664baf369e7SPeter Brune 
1665baf369e7SPeter Brune    Level: advanced
1666baf369e7SPeter Brune 
1667baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1668baf369e7SPeter Brune @*/
1669baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1670baf369e7SPeter Brune {
1671baf369e7SPeter Brune   PetscErrorCode          ierr;
1672baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1673baf369e7SPeter Brune 
1674baf369e7SPeter Brune   PetscFunctionBegin;
1675baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1676baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1677baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1678baf369e7SPeter Brune   link->beginhook = beginhook;
1679baf369e7SPeter Brune   link->endhook   = endhook;
1680baf369e7SPeter Brune   link->ctx       = ctx;
16810298fd71SBarry Smith   link->next      = NULL;
1682baf369e7SPeter Brune   *p              = link;
1683baf369e7SPeter Brune   PetscFunctionReturn(0);
1684baf369e7SPeter Brune }
1685baf369e7SPeter Brune 
1686baf369e7SPeter Brune #undef __FUNCT__
168747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
168847c6ae99SBarry Smith /*@
168947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
169047c6ae99SBarry Smith 
169147c6ae99SBarry Smith     Neighbor-wise Collective on DM
169247c6ae99SBarry Smith 
169347c6ae99SBarry Smith     Input Parameters:
169447c6ae99SBarry Smith +   dm - the DM object
169547c6ae99SBarry Smith .   g - the global vector
169647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
169747c6ae99SBarry Smith -   l - the local vector
169847c6ae99SBarry Smith 
169947c6ae99SBarry Smith 
170047c6ae99SBarry Smith     Level: beginner
170147c6ae99SBarry Smith 
1702e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
170347c6ae99SBarry Smith 
170447c6ae99SBarry Smith @*/
17057087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
170647c6ae99SBarry Smith {
17077128ae9fSMatthew G Knepley   PetscSF                 sf;
170847c6ae99SBarry Smith   PetscErrorCode          ierr;
1709baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
171047c6ae99SBarry Smith 
171147c6ae99SBarry Smith   PetscFunctionBegin;
1712171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1713baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
17148865f1eaSKarl Rupp     if (link->beginhook) {
17158865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
17168865f1eaSKarl Rupp     }
1717baf369e7SPeter Brune   }
17187128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17197128ae9fSMatthew G Knepley   if (sf) {
17207128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17217128ae9fSMatthew G Knepley 
172282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17237128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17247128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17257128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17267128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17277128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17287128ae9fSMatthew G Knepley   } else {
1729843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17307128ae9fSMatthew G Knepley   }
173147c6ae99SBarry Smith   PetscFunctionReturn(0);
173247c6ae99SBarry Smith }
173347c6ae99SBarry Smith 
173447c6ae99SBarry Smith #undef __FUNCT__
173547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
173647c6ae99SBarry Smith /*@
173747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
173847c6ae99SBarry Smith 
173947c6ae99SBarry Smith     Neighbor-wise Collective on DM
174047c6ae99SBarry Smith 
174147c6ae99SBarry Smith     Input Parameters:
174247c6ae99SBarry Smith +   dm - the DM object
174347c6ae99SBarry Smith .   g - the global vector
174447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
174547c6ae99SBarry Smith -   l - the local vector
174647c6ae99SBarry Smith 
174747c6ae99SBarry Smith 
174847c6ae99SBarry Smith     Level: beginner
174947c6ae99SBarry Smith 
1750e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
175147c6ae99SBarry Smith 
175247c6ae99SBarry Smith @*/
17537087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
175447c6ae99SBarry Smith {
17557128ae9fSMatthew G Knepley   PetscSF                 sf;
175647c6ae99SBarry Smith   PetscErrorCode          ierr;
175761a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1758baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
175947c6ae99SBarry Smith 
176047c6ae99SBarry Smith   PetscFunctionBegin;
1761171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17627128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17637128ae9fSMatthew G Knepley   if (sf) {
176482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17657128ae9fSMatthew G Knepley 
17667128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17677128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17687128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17697128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17707128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17717128ae9fSMatthew G Knepley   } else {
1772843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17737128ae9fSMatthew G Knepley   }
1774baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1775baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1776baf369e7SPeter Brune   }
177747c6ae99SBarry Smith   PetscFunctionReturn(0);
177847c6ae99SBarry Smith }
177947c6ae99SBarry Smith 
178047c6ae99SBarry Smith #undef __FUNCT__
17819a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
178247c6ae99SBarry Smith /*@
17839a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
17849a42bb27SBarry Smith 
17859a42bb27SBarry Smith     Neighbor-wise Collective on DM
17869a42bb27SBarry Smith 
17879a42bb27SBarry Smith     Input Parameters:
17889a42bb27SBarry Smith +   dm - the DM object
1789f6813fd5SJed Brown .   l - the local vector
17909a42bb27SBarry 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
17919a42bb27SBarry Smith            base point.
1792f6813fd5SJed Brown - - the global vector
17939a42bb27SBarry Smith 
17949a42bb27SBarry 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
17959a42bb27SBarry 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
17969a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17979a42bb27SBarry Smith 
17989a42bb27SBarry Smith     Level: beginner
17999a42bb27SBarry Smith 
1800e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
18019a42bb27SBarry Smith 
18029a42bb27SBarry Smith @*/
18037087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
18049a42bb27SBarry Smith {
18057128ae9fSMatthew G Knepley   PetscSF        sf;
18069a42bb27SBarry Smith   PetscErrorCode ierr;
18079a42bb27SBarry Smith 
18089a42bb27SBarry Smith   PetscFunctionBegin;
1809171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18107128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18117128ae9fSMatthew G Knepley   if (sf) {
18127128ae9fSMatthew G Knepley     MPI_Op      op;
18137128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
18147128ae9fSMatthew G Knepley 
18157128ae9fSMatthew G Knepley     switch (mode) {
18167128ae9fSMatthew G Knepley     case INSERT_VALUES:
18177128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18188bfbc91cSJed Brown       op = MPIU_REPLACE; break;
18197128ae9fSMatthew G Knepley     case ADD_VALUES:
18207128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18217128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18227128ae9fSMatthew G Knepley     default:
182382f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18247128ae9fSMatthew G Knepley     }
18257128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18267128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18277128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18287128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18297128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18307128ae9fSMatthew G Knepley   } else {
1831843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18327128ae9fSMatthew G Knepley   }
18339a42bb27SBarry Smith   PetscFunctionReturn(0);
18349a42bb27SBarry Smith }
18359a42bb27SBarry Smith 
18369a42bb27SBarry Smith #undef __FUNCT__
18379a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
18389a42bb27SBarry Smith /*@
18399a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
184047c6ae99SBarry Smith 
184147c6ae99SBarry Smith     Neighbor-wise Collective on DM
184247c6ae99SBarry Smith 
184347c6ae99SBarry Smith     Input Parameters:
184447c6ae99SBarry Smith +   dm - the DM object
1845f6813fd5SJed Brown .   l - the local vector
184647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1847f6813fd5SJed Brown -   g - the global vector
184847c6ae99SBarry Smith 
184947c6ae99SBarry Smith 
185047c6ae99SBarry Smith     Level: beginner
185147c6ae99SBarry Smith 
1852e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
185347c6ae99SBarry Smith 
185447c6ae99SBarry Smith @*/
18557087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
185647c6ae99SBarry Smith {
18577128ae9fSMatthew G Knepley   PetscSF        sf;
185847c6ae99SBarry Smith   PetscErrorCode ierr;
185947c6ae99SBarry Smith 
186047c6ae99SBarry Smith   PetscFunctionBegin;
1861171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18627128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18637128ae9fSMatthew G Knepley   if (sf) {
18647128ae9fSMatthew G Knepley     MPI_Op      op;
18657128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
18667128ae9fSMatthew G Knepley 
18677128ae9fSMatthew G Knepley     switch (mode) {
18687128ae9fSMatthew G Knepley     case INSERT_VALUES:
18697128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18708bfbc91cSJed Brown       op = MPIU_REPLACE; break;
18717128ae9fSMatthew G Knepley     case ADD_VALUES:
18727128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18737128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18747128ae9fSMatthew G Knepley     default:
187582f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18767128ae9fSMatthew G Knepley     }
18777128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18787128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18797128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18807128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18827128ae9fSMatthew G Knepley   } else {
1883843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18847128ae9fSMatthew G Knepley   }
188547c6ae99SBarry Smith   PetscFunctionReturn(0);
188647c6ae99SBarry Smith }
188747c6ae99SBarry Smith 
188847c6ae99SBarry Smith #undef __FUNCT__
1889f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
1890f089877aSRichard Tran Mills /*@
1891bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
1892bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1893d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
1894f089877aSRichard Tran Mills 
1895bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1896f089877aSRichard Tran Mills 
1897f089877aSRichard Tran Mills    Input Parameters:
1898f089877aSRichard Tran Mills +  dm - the DM object
1899bc0a1609SRichard Tran Mills .  g - the original local vector
1900bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1901f089877aSRichard Tran Mills 
1902bc0a1609SRichard Tran Mills    Output Parameter:
1903bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1904f089877aSRichard Tran Mills 
1905f089877aSRichard Tran Mills    Level: intermediate
1906f089877aSRichard Tran Mills 
1907bc0a1609SRichard Tran Mills    Notes:
1908bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1909bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1910bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1911bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1912bc0a1609SRichard Tran Mills 
1913bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
1914bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1915f089877aSRichard Tran Mills 
1916f089877aSRichard Tran Mills @*/
1917f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
1918f089877aSRichard Tran Mills {
1919f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1920f089877aSRichard Tran Mills 
1921f089877aSRichard Tran Mills   PetscFunctionBegin;
1922f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1923f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1924f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1925f089877aSRichard Tran Mills }
1926f089877aSRichard Tran Mills 
1927f089877aSRichard Tran Mills #undef __FUNCT__
1928f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
1929f089877aSRichard Tran Mills /*@
1930bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
1931bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1932d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
1933f089877aSRichard Tran Mills 
1934bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1935f089877aSRichard Tran Mills 
1936f089877aSRichard Tran Mills    Input Parameters:
1937bc0a1609SRichard Tran Mills +  da - the DM object
1938bc0a1609SRichard Tran Mills .  g - the original local vector
1939bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1940f089877aSRichard Tran Mills 
1941bc0a1609SRichard Tran Mills    Output Parameter:
1942bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1943f089877aSRichard Tran Mills 
1944f089877aSRichard Tran Mills    Level: intermediate
1945f089877aSRichard Tran Mills 
1946bc0a1609SRichard Tran Mills    Notes:
1947bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1948bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1949bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1950bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1951bc0a1609SRichard Tran Mills 
1952bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
1953bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1954f089877aSRichard Tran Mills 
1955f089877aSRichard Tran Mills @*/
1956f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
1957f089877aSRichard Tran Mills {
1958f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1959f089877aSRichard Tran Mills 
1960f089877aSRichard Tran Mills   PetscFunctionBegin;
1961f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1962f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1963f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1964f089877aSRichard Tran Mills }
1965f089877aSRichard Tran Mills 
1966f089877aSRichard Tran Mills 
1967f089877aSRichard Tran Mills #undef __FUNCT__
196847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
196947c6ae99SBarry Smith /*@
197047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
197147c6ae99SBarry Smith 
197247c6ae99SBarry Smith     Collective on DM
197347c6ae99SBarry Smith 
197447c6ae99SBarry Smith     Input Parameter:
197547c6ae99SBarry Smith +   dm - the DM object
197691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
197747c6ae99SBarry Smith 
197847c6ae99SBarry Smith     Output Parameter:
197947c6ae99SBarry Smith .   dmc - the coarsened DM
198047c6ae99SBarry Smith 
198147c6ae99SBarry Smith     Level: developer
198247c6ae99SBarry Smith 
1983e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
198447c6ae99SBarry Smith 
198547c6ae99SBarry Smith @*/
19867087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
198747c6ae99SBarry Smith {
198847c6ae99SBarry Smith   PetscErrorCode    ierr;
1989b17ce1afSJed Brown   DMCoarsenHookLink link;
199047c6ae99SBarry Smith 
199147c6ae99SBarry Smith   PetscFunctionBegin;
1992171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
199347c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
199443842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
19958cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1996644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
19970598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1998656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1999e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2000b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2001b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2002b17ce1afSJed Brown   }
2003b17ce1afSJed Brown   PetscFunctionReturn(0);
2004b17ce1afSJed Brown }
2005b17ce1afSJed Brown 
2006b17ce1afSJed Brown #undef __FUNCT__
2007b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2008bb9467b5SJed Brown /*@C
2009b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2010b17ce1afSJed Brown 
2011b17ce1afSJed Brown    Logically Collective
2012b17ce1afSJed Brown 
2013b17ce1afSJed Brown    Input Arguments:
2014b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2015b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2016b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
20170298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2018b17ce1afSJed Brown 
2019b17ce1afSJed Brown    Calling sequence of coarsenhook:
2020b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2021b17ce1afSJed Brown 
2022b17ce1afSJed Brown +  fine - fine level DM
2023b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2024b17ce1afSJed Brown -  ctx - optional user-defined function context
2025b17ce1afSJed Brown 
2026b17ce1afSJed Brown    Calling sequence for restricthook:
2027c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2028b17ce1afSJed Brown 
2029b17ce1afSJed Brown +  fine - fine level DM
2030b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2031c833c3b5SJed Brown .  rscale - scaling vector for restriction
2032c833c3b5SJed Brown .  inject - matrix restricting by injection
2033b17ce1afSJed Brown .  coarse - coarse level DM to update
2034b17ce1afSJed Brown -  ctx - optional user-defined function context
2035b17ce1afSJed Brown 
2036b17ce1afSJed Brown    Level: advanced
2037b17ce1afSJed Brown 
2038b17ce1afSJed Brown    Notes:
2039b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2040b17ce1afSJed Brown 
2041b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2042b17ce1afSJed Brown 
2043b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2044b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2045b17ce1afSJed Brown 
2046bb9467b5SJed Brown    This function is currently not available from Fortran.
2047bb9467b5SJed Brown 
2048c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2049b17ce1afSJed Brown @*/
2050b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2051b17ce1afSJed Brown {
2052b17ce1afSJed Brown   PetscErrorCode    ierr;
2053b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2054b17ce1afSJed Brown 
2055b17ce1afSJed Brown   PetscFunctionBegin;
2056b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
20576bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2058b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2059b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2060b17ce1afSJed Brown   link->restricthook = restricthook;
2061b17ce1afSJed Brown   link->ctx          = ctx;
20620298fd71SBarry Smith   link->next         = NULL;
2063b17ce1afSJed Brown   *p                 = link;
2064b17ce1afSJed Brown   PetscFunctionReturn(0);
2065b17ce1afSJed Brown }
2066b17ce1afSJed Brown 
2067b17ce1afSJed Brown #undef __FUNCT__
2068b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2069b17ce1afSJed Brown /*@
2070b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2071b17ce1afSJed Brown 
2072b17ce1afSJed Brown    Collective if any hooks are
2073b17ce1afSJed Brown 
2074b17ce1afSJed Brown    Input Arguments:
2075b17ce1afSJed Brown +  fine - finer DM to use as a base
2076b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2077b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2078b17ce1afSJed Brown -  coarse - coarer DM to update
2079b17ce1afSJed Brown 
2080b17ce1afSJed Brown    Level: developer
2081b17ce1afSJed Brown 
2082b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2083b17ce1afSJed Brown @*/
2084b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2085b17ce1afSJed Brown {
2086b17ce1afSJed Brown   PetscErrorCode    ierr;
2087b17ce1afSJed Brown   DMCoarsenHookLink link;
2088b17ce1afSJed Brown 
2089b17ce1afSJed Brown   PetscFunctionBegin;
2090b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
20918865f1eaSKarl Rupp     if (link->restricthook) {
20928865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
20938865f1eaSKarl Rupp     }
2094b17ce1afSJed Brown   }
209547c6ae99SBarry Smith   PetscFunctionReturn(0);
209647c6ae99SBarry Smith }
209747c6ae99SBarry Smith 
209847c6ae99SBarry Smith #undef __FUNCT__
2099be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2100bb9467b5SJed Brown /*@C
2101be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
21025dbd56e3SPeter Brune 
21035dbd56e3SPeter Brune    Logically Collective
21045dbd56e3SPeter Brune 
21055dbd56e3SPeter Brune    Input Arguments:
21065dbd56e3SPeter Brune +  global - global DM
2107ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
21085dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
21090298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
21105dbd56e3SPeter Brune 
2111ec4806b8SPeter Brune 
2112ec4806b8SPeter Brune    Calling sequence for ddhook:
2113ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2114ec4806b8SPeter Brune 
2115ec4806b8SPeter Brune +  global - global DM
2116ec4806b8SPeter Brune .  block  - block DM
2117ec4806b8SPeter Brune -  ctx - optional user-defined function context
2118ec4806b8SPeter Brune 
21195dbd56e3SPeter Brune    Calling sequence for restricthook:
2120ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
21215dbd56e3SPeter Brune 
21225dbd56e3SPeter Brune +  global - global DM
21235dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
21245dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2125ec4806b8SPeter Brune .  block  - block DM
21265dbd56e3SPeter Brune -  ctx - optional user-defined function context
21275dbd56e3SPeter Brune 
21285dbd56e3SPeter Brune    Level: advanced
21295dbd56e3SPeter Brune 
21305dbd56e3SPeter Brune    Notes:
2131ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
21325dbd56e3SPeter Brune 
21335dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
21345dbd56e3SPeter Brune 
21355dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2136ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
21375dbd56e3SPeter Brune 
2138bb9467b5SJed Brown    This function is currently not available from Fortran.
2139bb9467b5SJed Brown 
21405dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
21415dbd56e3SPeter Brune @*/
2142be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
21435dbd56e3SPeter Brune {
21445dbd56e3SPeter Brune   PetscErrorCode      ierr;
2145be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
21465dbd56e3SPeter Brune 
21475dbd56e3SPeter Brune   PetscFunctionBegin;
21485dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2149be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2150be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
21515dbd56e3SPeter Brune   link->restricthook = restricthook;
2152be081cd6SPeter Brune   link->ddhook       = ddhook;
21535dbd56e3SPeter Brune   link->ctx          = ctx;
21540298fd71SBarry Smith   link->next         = NULL;
21555dbd56e3SPeter Brune   *p                 = link;
21565dbd56e3SPeter Brune   PetscFunctionReturn(0);
21575dbd56e3SPeter Brune }
21585dbd56e3SPeter Brune 
21595dbd56e3SPeter Brune #undef __FUNCT__
2160be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
21615dbd56e3SPeter Brune /*@
2162be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
21635dbd56e3SPeter Brune 
21645dbd56e3SPeter Brune    Collective if any hooks are
21655dbd56e3SPeter Brune 
21665dbd56e3SPeter Brune    Input Arguments:
21675dbd56e3SPeter Brune +  fine - finer DM to use as a base
2168be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2169be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
21705dbd56e3SPeter Brune -  coarse - coarer DM to update
21715dbd56e3SPeter Brune 
21725dbd56e3SPeter Brune    Level: developer
21735dbd56e3SPeter Brune 
21745dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
21755dbd56e3SPeter Brune @*/
2176be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
21775dbd56e3SPeter Brune {
21785dbd56e3SPeter Brune   PetscErrorCode      ierr;
2179be081cd6SPeter Brune   DMSubDomainHookLink link;
21805dbd56e3SPeter Brune 
21815dbd56e3SPeter Brune   PetscFunctionBegin;
2182be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
21838865f1eaSKarl Rupp     if (link->restricthook) {
21848865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
21858865f1eaSKarl Rupp     }
21865dbd56e3SPeter Brune   }
21875dbd56e3SPeter Brune   PetscFunctionReturn(0);
21885dbd56e3SPeter Brune }
21895dbd56e3SPeter Brune 
21905dbd56e3SPeter Brune #undef __FUNCT__
21915fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
21925fe1f584SPeter Brune /*@
21936a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
21945fe1f584SPeter Brune 
21955fe1f584SPeter Brune     Not Collective
21965fe1f584SPeter Brune 
21975fe1f584SPeter Brune     Input Parameter:
21985fe1f584SPeter Brune .   dm - the DM object
21995fe1f584SPeter Brune 
22005fe1f584SPeter Brune     Output Parameter:
22016a7d9d85SPeter Brune .   level - number of coarsenings
22025fe1f584SPeter Brune 
22035fe1f584SPeter Brune     Level: developer
22045fe1f584SPeter Brune 
22056a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
22065fe1f584SPeter Brune 
22075fe1f584SPeter Brune @*/
22085fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
22095fe1f584SPeter Brune {
22105fe1f584SPeter Brune   PetscFunctionBegin;
22115fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22125fe1f584SPeter Brune   *level = dm->leveldown;
22135fe1f584SPeter Brune   PetscFunctionReturn(0);
22145fe1f584SPeter Brune }
22155fe1f584SPeter Brune 
22165fe1f584SPeter Brune 
22175fe1f584SPeter Brune 
22185fe1f584SPeter Brune #undef __FUNCT__
221947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
222047c6ae99SBarry Smith /*@C
222147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
222247c6ae99SBarry Smith 
222347c6ae99SBarry Smith     Collective on DM
222447c6ae99SBarry Smith 
222547c6ae99SBarry Smith     Input Parameter:
222647c6ae99SBarry Smith +   dm - the DM object
222747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
222847c6ae99SBarry Smith 
222947c6ae99SBarry Smith     Output Parameter:
223047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
223147c6ae99SBarry Smith 
223247c6ae99SBarry Smith     Level: developer
223347c6ae99SBarry Smith 
2234e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
223547c6ae99SBarry Smith 
223647c6ae99SBarry Smith @*/
22377087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
223847c6ae99SBarry Smith {
223947c6ae99SBarry Smith   PetscErrorCode ierr;
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith   PetscFunctionBegin;
2242171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2243ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
224447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
224547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
224647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
224747c6ae99SBarry Smith   } else if (dm->ops->refine) {
224847c6ae99SBarry Smith     PetscInt i;
224947c6ae99SBarry Smith 
2250ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
225147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2252ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
225347c6ae99SBarry Smith     }
2254ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
225547c6ae99SBarry Smith   PetscFunctionReturn(0);
225647c6ae99SBarry Smith }
225747c6ae99SBarry Smith 
225847c6ae99SBarry Smith #undef __FUNCT__
225947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
226047c6ae99SBarry Smith /*@C
226147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
226247c6ae99SBarry Smith 
226347c6ae99SBarry Smith     Collective on DM
226447c6ae99SBarry Smith 
226547c6ae99SBarry Smith     Input Parameter:
226647c6ae99SBarry Smith +   dm - the DM object
226747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
226847c6ae99SBarry Smith 
226947c6ae99SBarry Smith     Output Parameter:
227047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
227147c6ae99SBarry Smith 
227247c6ae99SBarry Smith     Level: developer
227347c6ae99SBarry Smith 
2274e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
227547c6ae99SBarry Smith 
227647c6ae99SBarry Smith @*/
22777087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
227847c6ae99SBarry Smith {
227947c6ae99SBarry Smith   PetscErrorCode ierr;
228047c6ae99SBarry Smith 
228147c6ae99SBarry Smith   PetscFunctionBegin;
2282171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2283ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
228447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
228547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
228647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
228747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
228847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
228947c6ae99SBarry Smith     PetscInt i;
229047c6ae99SBarry Smith 
2291ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
229247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2293ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
229447c6ae99SBarry Smith     }
2295ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
229647c6ae99SBarry Smith   PetscFunctionReturn(0);
229747c6ae99SBarry Smith }
229847c6ae99SBarry Smith 
229947c6ae99SBarry Smith #undef __FUNCT__
2300e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
230147c6ae99SBarry Smith /*@
2302e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
230347c6ae99SBarry Smith    grids associated with two DMs.
230447c6ae99SBarry Smith 
230547c6ae99SBarry Smith    Collective on DM
230647c6ae99SBarry Smith 
230747c6ae99SBarry Smith    Input Parameters:
230847c6ae99SBarry Smith +  dmc - the coarse grid DM
230947c6ae99SBarry Smith -  dmf - the fine grid DM
231047c6ae99SBarry Smith 
231147c6ae99SBarry Smith    Output Parameters:
231247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
231347c6ae99SBarry Smith 
231447c6ae99SBarry Smith    Level: intermediate
231547c6ae99SBarry Smith 
231647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
231747c6ae99SBarry Smith 
2318e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
231947c6ae99SBarry Smith @*/
2320e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
232147c6ae99SBarry Smith {
232247c6ae99SBarry Smith   PetscErrorCode ierr;
232347c6ae99SBarry Smith 
232447c6ae99SBarry Smith   PetscFunctionBegin;
2325171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2326171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
232747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
232847c6ae99SBarry Smith   PetscFunctionReturn(0);
232947c6ae99SBarry Smith }
233047c6ae99SBarry Smith 
233147c6ae99SBarry Smith #undef __FUNCT__
23321a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
23331a266240SBarry Smith /*@C
23341a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
23351a266240SBarry Smith 
23361a266240SBarry Smith     Not Collective
23371a266240SBarry Smith 
23381a266240SBarry Smith     Input Parameters:
23391a266240SBarry Smith +   dm - the DM object
23401a266240SBarry Smith -   destroy - the destroy function
23411a266240SBarry Smith 
23421a266240SBarry Smith     Level: intermediate
23431a266240SBarry Smith 
2344e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
23451a266240SBarry Smith 
2346f07f9ceaSJed Brown @*/
23471a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
23481a266240SBarry Smith {
23491a266240SBarry Smith   PetscFunctionBegin;
2350171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23511a266240SBarry Smith   dm->ctxdestroy = destroy;
23521a266240SBarry Smith   PetscFunctionReturn(0);
23531a266240SBarry Smith }
23541a266240SBarry Smith 
23551a266240SBarry Smith #undef __FUNCT__
23561b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2357b07ff414SBarry Smith /*@
23581b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
235947c6ae99SBarry Smith 
236047c6ae99SBarry Smith     Not Collective
236147c6ae99SBarry Smith 
236247c6ae99SBarry Smith     Input Parameters:
236347c6ae99SBarry Smith +   dm - the DM object
236447c6ae99SBarry Smith -   ctx - the user context
236547c6ae99SBarry Smith 
236647c6ae99SBarry Smith     Level: intermediate
236747c6ae99SBarry Smith 
2368e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
236947c6ae99SBarry Smith 
237047c6ae99SBarry Smith @*/
23711b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
237247c6ae99SBarry Smith {
237347c6ae99SBarry Smith   PetscFunctionBegin;
2374171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
237547c6ae99SBarry Smith   dm->ctx = ctx;
237647c6ae99SBarry Smith   PetscFunctionReturn(0);
237747c6ae99SBarry Smith }
237847c6ae99SBarry Smith 
237947c6ae99SBarry Smith #undef __FUNCT__
23801b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
238147c6ae99SBarry Smith /*@
23821b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
238347c6ae99SBarry Smith 
238447c6ae99SBarry Smith     Not Collective
238547c6ae99SBarry Smith 
238647c6ae99SBarry Smith     Input Parameter:
238747c6ae99SBarry Smith .   dm - the DM object
238847c6ae99SBarry Smith 
238947c6ae99SBarry Smith     Output Parameter:
239047c6ae99SBarry Smith .   ctx - the user context
239147c6ae99SBarry Smith 
239247c6ae99SBarry Smith     Level: intermediate
239347c6ae99SBarry Smith 
2394e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
239547c6ae99SBarry Smith 
239647c6ae99SBarry Smith @*/
23971b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
239847c6ae99SBarry Smith {
239947c6ae99SBarry Smith   PetscFunctionBegin;
2400171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24011b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
240247c6ae99SBarry Smith   PetscFunctionReturn(0);
240347c6ae99SBarry Smith }
240447c6ae99SBarry Smith 
240547c6ae99SBarry Smith #undef __FUNCT__
240608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
240708da532bSDmitry Karpeev /*@C
240808da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
240908da532bSDmitry Karpeev 
241008da532bSDmitry Karpeev     Logically Collective on DM
241108da532bSDmitry Karpeev 
241208da532bSDmitry Karpeev     Input Parameter:
241308da532bSDmitry Karpeev +   dm - the DM object
24140298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
241508da532bSDmitry Karpeev 
241608da532bSDmitry Karpeev     Level: intermediate
241708da532bSDmitry Karpeev 
2418835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
241908da532bSDmitry Karpeev          DMSetJacobian()
242008da532bSDmitry Karpeev 
242108da532bSDmitry Karpeev @*/
242208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
242308da532bSDmitry Karpeev {
242408da532bSDmitry Karpeev   PetscFunctionBegin;
242508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
242608da532bSDmitry Karpeev   PetscFunctionReturn(0);
242708da532bSDmitry Karpeev }
242808da532bSDmitry Karpeev 
242908da532bSDmitry Karpeev #undef __FUNCT__
243008da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
243108da532bSDmitry Karpeev /*@
243208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
243308da532bSDmitry Karpeev 
243408da532bSDmitry Karpeev     Not Collective
243508da532bSDmitry Karpeev 
243608da532bSDmitry Karpeev     Input Parameter:
243708da532bSDmitry Karpeev .   dm - the DM object to destroy
243808da532bSDmitry Karpeev 
243908da532bSDmitry Karpeev     Output Parameter:
244008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
244108da532bSDmitry Karpeev 
244208da532bSDmitry Karpeev     Level: developer
244308da532bSDmitry Karpeev 
244474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
244508da532bSDmitry Karpeev 
244608da532bSDmitry Karpeev @*/
244708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
244808da532bSDmitry Karpeev {
244908da532bSDmitry Karpeev   PetscFunctionBegin;
245008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
245108da532bSDmitry Karpeev   PetscFunctionReturn(0);
245208da532bSDmitry Karpeev }
245308da532bSDmitry Karpeev 
245408da532bSDmitry Karpeev #undef __FUNCT__
245508da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
245608da532bSDmitry Karpeev /*@C
245708da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
245808da532bSDmitry Karpeev 
245908da532bSDmitry Karpeev     Logically Collective on DM
246008da532bSDmitry Karpeev 
246108da532bSDmitry Karpeev     Input Parameters:
246208da532bSDmitry Karpeev +   dm - the DM object to destroy
246308da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
246408da532bSDmitry Karpeev 
246508da532bSDmitry Karpeev     Output parameters:
246608da532bSDmitry Karpeev +   xl - lower bound
246708da532bSDmitry Karpeev -   xu - upper bound
246808da532bSDmitry Karpeev 
246908da532bSDmitry Karpeev     Level: intermediate
247008da532bSDmitry Karpeev 
247174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
247208da532bSDmitry Karpeev 
247308da532bSDmitry Karpeev @*/
247408da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
247508da532bSDmitry Karpeev {
247608da532bSDmitry Karpeev   PetscErrorCode ierr;
24775fd66863SKarl Rupp 
247808da532bSDmitry Karpeev   PetscFunctionBegin;
247908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
248008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
248108da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
248208da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
24838865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
248408da532bSDmitry Karpeev   PetscFunctionReturn(0);
248508da532bSDmitry Karpeev }
248608da532bSDmitry Karpeev 
248708da532bSDmitry Karpeev #undef __FUNCT__
2488b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2489b0ae01b7SPeter Brune /*@
2490b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2491b0ae01b7SPeter Brune 
2492b0ae01b7SPeter Brune     Not Collective
2493b0ae01b7SPeter Brune 
2494b0ae01b7SPeter Brune     Input Parameter:
2495b0ae01b7SPeter Brune .   dm - the DM object
2496b0ae01b7SPeter Brune 
2497b0ae01b7SPeter Brune     Output Parameter:
2498b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2499b0ae01b7SPeter Brune 
2500b0ae01b7SPeter Brune     Level: developer
2501b0ae01b7SPeter Brune 
2502b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2503b0ae01b7SPeter Brune 
2504b0ae01b7SPeter Brune @*/
2505b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2506b0ae01b7SPeter Brune {
2507b0ae01b7SPeter Brune   PetscFunctionBegin;
2508b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2509b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2510b0ae01b7SPeter Brune }
2511b0ae01b7SPeter Brune 
2512b0ae01b7SPeter Brune #undef  __FUNCT__
251308da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2514748fac09SDmitry Karpeev /*@C
251508da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
251608da532bSDmitry Karpeev 
251708da532bSDmitry Karpeev     Collective on DM
251808da532bSDmitry Karpeev 
251908da532bSDmitry Karpeev     Input Parameter:
252008da532bSDmitry Karpeev +   dm - the DM object
25210298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
252208da532bSDmitry Karpeev 
252308da532bSDmitry Karpeev     Level: developer
252408da532bSDmitry Karpeev 
252574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
252608da532bSDmitry Karpeev 
252708da532bSDmitry Karpeev @*/
252808da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
252908da532bSDmitry Karpeev {
253008da532bSDmitry Karpeev   PetscErrorCode ierr;
25315fd66863SKarl Rupp 
253208da532bSDmitry Karpeev   PetscFunctionBegin;
253308da532bSDmitry Karpeev   if (x) {
253408da532bSDmitry Karpeev     if (!dm->x) {
253508da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
253608da532bSDmitry Karpeev     }
253708da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
25388865f1eaSKarl Rupp   } else if (dm->x) {
253908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
254008da532bSDmitry Karpeev   }
254108da532bSDmitry Karpeev   PetscFunctionReturn(0);
254208da532bSDmitry Karpeev }
254308da532bSDmitry Karpeev 
25440298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2545264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2546264ace61SBarry Smith 
2547264ace61SBarry Smith #undef __FUNCT__
2548264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2549264ace61SBarry Smith /*@C
2550264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2551264ace61SBarry Smith 
2552264ace61SBarry Smith   Collective on DM
2553264ace61SBarry Smith 
2554264ace61SBarry Smith   Input Parameters:
2555264ace61SBarry Smith + dm     - The DM object
2556264ace61SBarry Smith - method - The name of the DM type
2557264ace61SBarry Smith 
2558264ace61SBarry Smith   Options Database Key:
2559264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2560264ace61SBarry Smith 
2561264ace61SBarry Smith   Notes:
2562e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2563264ace61SBarry Smith 
2564264ace61SBarry Smith   Level: intermediate
2565264ace61SBarry Smith 
2566264ace61SBarry Smith .keywords: DM, set, type
2567264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2568264ace61SBarry Smith @*/
256919fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2570264ace61SBarry Smith {
2571264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2572264ace61SBarry Smith   PetscBool      match;
2573264ace61SBarry Smith   PetscErrorCode ierr;
2574264ace61SBarry Smith 
2575264ace61SBarry Smith   PetscFunctionBegin;
2576264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2577251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2578264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2579264ace61SBarry Smith 
2580607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
25811c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2582ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2583264ace61SBarry Smith 
2584264ace61SBarry Smith   if (dm->ops->destroy) {
2585264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
25860298fd71SBarry Smith     dm->ops->destroy = NULL;
2587264ace61SBarry Smith   }
2588264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2589264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2590264ace61SBarry Smith   PetscFunctionReturn(0);
2591264ace61SBarry Smith }
2592264ace61SBarry Smith 
2593264ace61SBarry Smith #undef __FUNCT__
2594264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2595264ace61SBarry Smith /*@C
2596264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2597264ace61SBarry Smith 
2598264ace61SBarry Smith   Not Collective
2599264ace61SBarry Smith 
2600264ace61SBarry Smith   Input Parameter:
2601264ace61SBarry Smith . dm  - The DM
2602264ace61SBarry Smith 
2603264ace61SBarry Smith   Output Parameter:
2604264ace61SBarry Smith . type - The DM type name
2605264ace61SBarry Smith 
2606264ace61SBarry Smith   Level: intermediate
2607264ace61SBarry Smith 
2608264ace61SBarry Smith .keywords: DM, get, type, name
2609264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2610264ace61SBarry Smith @*/
261119fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2612264ace61SBarry Smith {
2613264ace61SBarry Smith   PetscErrorCode ierr;
2614264ace61SBarry Smith 
2615264ace61SBarry Smith   PetscFunctionBegin;
2616264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2617264ace61SBarry Smith   PetscValidCharPointer(type,2);
2618264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2619607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2620264ace61SBarry Smith   }
2621264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2622264ace61SBarry Smith   PetscFunctionReturn(0);
2623264ace61SBarry Smith }
2624264ace61SBarry Smith 
262567a56275SMatthew G Knepley #undef __FUNCT__
262667a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
262767a56275SMatthew G Knepley /*@C
262867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
262967a56275SMatthew G Knepley 
263067a56275SMatthew G Knepley   Collective on DM
263167a56275SMatthew G Knepley 
263267a56275SMatthew G Knepley   Input Parameters:
263367a56275SMatthew G Knepley + dm - the DM
263467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
263567a56275SMatthew G Knepley 
263667a56275SMatthew G Knepley   Output Parameter:
263767a56275SMatthew G Knepley . M - pointer to new DM
263867a56275SMatthew G Knepley 
263967a56275SMatthew G Knepley   Notes:
264067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
264167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
264267a56275SMatthew G Knepley   of the input DM.
264367a56275SMatthew G Knepley 
264467a56275SMatthew G Knepley   Level: intermediate
264567a56275SMatthew G Knepley 
264667a56275SMatthew G Knepley .seealso: DMCreate()
264767a56275SMatthew G Knepley @*/
264819fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
264967a56275SMatthew G Knepley {
265067a56275SMatthew G Knepley   DM             B;
265167a56275SMatthew G Knepley   char           convname[256];
265267a56275SMatthew G Knepley   PetscBool      sametype, issame;
265367a56275SMatthew G Knepley   PetscErrorCode ierr;
265467a56275SMatthew G Knepley 
265567a56275SMatthew G Knepley   PetscFunctionBegin;
265667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
265767a56275SMatthew G Knepley   PetscValidType(dm,1);
265867a56275SMatthew G Knepley   PetscValidPointer(M,3);
2659251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
266067a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
266167a56275SMatthew G Knepley   {
26620298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
266367a56275SMatthew G Knepley 
266467a56275SMatthew G Knepley     /*
266567a56275SMatthew G Knepley        Order of precedence:
266667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
266767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
266867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
266967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
267067a56275SMatthew G Knepley        5) Use a really basic converter.
267167a56275SMatthew G Knepley     */
267267a56275SMatthew G Knepley 
267367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
267467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
267567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
267667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
267767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
267867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26790005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
268067a56275SMatthew G Knepley     if (conv) goto foundconv;
268167a56275SMatthew G Knepley 
268267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
268382f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
268467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
268567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
268667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
268767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
268867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
268967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26900005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
269167a56275SMatthew G Knepley     if (conv) {
2692fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
269367a56275SMatthew G Knepley       goto foundconv;
269467a56275SMatthew G Knepley     }
269567a56275SMatthew G Knepley 
269667a56275SMatthew G Knepley #if 0
269767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
269867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2699fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
270067a56275SMatthew G Knepley     if (conv) goto foundconv;
270167a56275SMatthew G Knepley 
270267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
270367a56275SMatthew G Knepley     if (dm->ops->convert) {
270467a56275SMatthew G Knepley       conv = dm->ops->convert;
270567a56275SMatthew G Knepley     }
270667a56275SMatthew G Knepley     if (conv) goto foundconv;
270767a56275SMatthew G Knepley #endif
270867a56275SMatthew G Knepley 
270967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
271082f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
271167a56275SMatthew G Knepley 
271267a56275SMatthew G Knepley foundconv:
271367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
271467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
271567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
271667a56275SMatthew G Knepley   }
271767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
271867a56275SMatthew G Knepley   PetscFunctionReturn(0);
271967a56275SMatthew G Knepley }
2720264ace61SBarry Smith 
2721264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2722264ace61SBarry Smith 
2723264ace61SBarry Smith #undef __FUNCT__
2724264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2725264ace61SBarry Smith /*@C
27261c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
27271c84c290SBarry Smith 
27281c84c290SBarry Smith   Not Collective
27291c84c290SBarry Smith 
27301c84c290SBarry Smith   Input Parameters:
27311c84c290SBarry Smith + name        - The name of a new user-defined creation routine
27321c84c290SBarry Smith - create_func - The creation routine itself
27331c84c290SBarry Smith 
27341c84c290SBarry Smith   Notes:
27351c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
27361c84c290SBarry Smith 
27371c84c290SBarry Smith 
27381c84c290SBarry Smith   Sample usage:
27391c84c290SBarry Smith .vb
2740bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
27411c84c290SBarry Smith .ve
27421c84c290SBarry Smith 
27431c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
27441c84c290SBarry Smith .vb
27451c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
27461c84c290SBarry Smith     DMSetType(DM,"my_da");
27471c84c290SBarry Smith .ve
27481c84c290SBarry Smith    or at runtime via the option
27491c84c290SBarry Smith .vb
27501c84c290SBarry Smith     -da_type my_da
27511c84c290SBarry Smith .ve
2752264ace61SBarry Smith 
2753264ace61SBarry Smith   Level: advanced
27541c84c290SBarry Smith 
27551c84c290SBarry Smith .keywords: DM, register
2756bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
27571c84c290SBarry Smith 
2758264ace61SBarry Smith @*/
2759bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2760264ace61SBarry Smith {
2761264ace61SBarry Smith   PetscErrorCode ierr;
2762264ace61SBarry Smith 
2763264ace61SBarry Smith   PetscFunctionBegin;
2764a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2765264ace61SBarry Smith   PetscFunctionReturn(0);
2766264ace61SBarry Smith }
2767264ace61SBarry Smith 
2768b859378eSBarry Smith #undef __FUNCT__
2769b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2770b859378eSBarry Smith /*@C
277155849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2772b859378eSBarry Smith 
2773b859378eSBarry Smith   Collective on PetscViewer
2774b859378eSBarry Smith 
2775b859378eSBarry Smith   Input Parameters:
2776b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2777b859378eSBarry Smith            some related function before a call to DMLoad().
2778b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2779b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2780b859378eSBarry Smith 
2781b859378eSBarry Smith    Level: intermediate
2782b859378eSBarry Smith 
2783b859378eSBarry Smith   Notes:
278455849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2785b859378eSBarry Smith 
2786b859378eSBarry Smith   Notes for advanced users:
2787b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2788b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2789b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2790b859378eSBarry Smith   format is
2791b859378eSBarry Smith .vb
2792b859378eSBarry Smith      has not yet been determined
2793b859378eSBarry Smith .ve
2794b859378eSBarry Smith 
2795b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2796b859378eSBarry Smith @*/
2797b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2798b859378eSBarry Smith {
2799b859378eSBarry Smith   PetscErrorCode ierr;
280032c0f0efSBarry Smith   PetscBool      isbinary;
280155849f57SBarry Smith   PetscInt       classid;
280232c0f0efSBarry Smith   char           type[256];
2803b859378eSBarry Smith 
2804b859378eSBarry Smith   PetscFunctionBegin;
2805b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2806b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
280732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
280832c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2809b859378eSBarry Smith 
281032c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
2811ce94432eSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file");
281232c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
281332c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
28142d53ad75SBarry Smith   if (newdm->ops->load) {
2815b859378eSBarry Smith     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
28162d53ad75SBarry Smith   }
2817b859378eSBarry Smith   PetscFunctionReturn(0);
2818b859378eSBarry Smith }
2819b859378eSBarry Smith 
28207da65231SMatthew G Knepley /******************************** FEM Support **********************************/
28217da65231SMatthew G Knepley 
28227da65231SMatthew G Knepley #undef __FUNCT__
28237da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2824a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2825a6dfd86eSKarl Rupp {
28261d47ebbbSSatish Balay   PetscInt       f;
28271b30c384SMatthew G Knepley   PetscErrorCode ierr;
28281b30c384SMatthew G Knepley 
28297da65231SMatthew G Knepley   PetscFunctionBegin;
283074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
28311d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
283274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
28337da65231SMatthew G Knepley   }
28347da65231SMatthew G Knepley   PetscFunctionReturn(0);
28357da65231SMatthew G Knepley }
28367da65231SMatthew G Knepley 
28377da65231SMatthew G Knepley #undef __FUNCT__
28387da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2839a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2840a6dfd86eSKarl Rupp {
28411b30c384SMatthew G Knepley   PetscInt       f, g;
28427da65231SMatthew G Knepley   PetscErrorCode ierr;
28437da65231SMatthew G Knepley 
28447da65231SMatthew G Knepley   PetscFunctionBegin;
284574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
28461d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
284774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
28481d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
284974778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
28507da65231SMatthew G Knepley     }
285174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
28527da65231SMatthew G Knepley   }
28537da65231SMatthew G Knepley   PetscFunctionReturn(0);
28547da65231SMatthew G Knepley }
2855e7c4fc90SDmitry Karpeev 
2856970e74d5SMatthew G Knepley #undef __FUNCT__
285788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
285888ed4aceSMatthew G Knepley /*@
285988ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
286088ed4aceSMatthew G Knepley 
286188ed4aceSMatthew G Knepley   Input Parameter:
286288ed4aceSMatthew G Knepley . dm - The DM
286388ed4aceSMatthew G Knepley 
286488ed4aceSMatthew G Knepley   Output Parameter:
286588ed4aceSMatthew G Knepley . section - The PetscSection
286688ed4aceSMatthew G Knepley 
286788ed4aceSMatthew G Knepley   Level: intermediate
286888ed4aceSMatthew G Knepley 
286988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
287088ed4aceSMatthew G Knepley 
287188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
287288ed4aceSMatthew G Knepley @*/
28730adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
28740adebc6cSBarry Smith {
287588ed4aceSMatthew G Knepley   PetscFunctionBegin;
287688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
287788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
287888ed4aceSMatthew G Knepley   *section = dm->defaultSection;
287988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
288088ed4aceSMatthew G Knepley }
288188ed4aceSMatthew G Knepley 
288288ed4aceSMatthew G Knepley #undef __FUNCT__
288388ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
288488ed4aceSMatthew G Knepley /*@
288588ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
288688ed4aceSMatthew G Knepley 
288788ed4aceSMatthew G Knepley   Input Parameters:
288888ed4aceSMatthew G Knepley + dm - The DM
288988ed4aceSMatthew G Knepley - section - The PetscSection
289088ed4aceSMatthew G Knepley 
289188ed4aceSMatthew G Knepley   Level: intermediate
289288ed4aceSMatthew G Knepley 
289388ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
289488ed4aceSMatthew G Knepley 
289588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
289688ed4aceSMatthew G Knepley @*/
28970adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
28980adebc6cSBarry Smith {
2899af122d2aSMatthew G Knepley   PetscInt       numFields;
2900af122d2aSMatthew G Knepley   PetscInt       f;
290188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
290288ed4aceSMatthew G Knepley 
290388ed4aceSMatthew G Knepley   PetscFunctionBegin;
290488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29051d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
29061d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
290788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
290888ed4aceSMatthew G Knepley   dm->defaultSection = section;
2909af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2910af122d2aSMatthew G Knepley   if (numFields) {
2911af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2912af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2913af122d2aSMatthew G Knepley       const char *name;
2914af122d2aSMatthew G Knepley 
2915af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2916af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2917af122d2aSMatthew G Knepley     }
2918af122d2aSMatthew G Knepley   }
29191d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
29201d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
292188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
292288ed4aceSMatthew G Knepley }
292388ed4aceSMatthew G Knepley 
292488ed4aceSMatthew G Knepley #undef __FUNCT__
292588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
292688ed4aceSMatthew G Knepley /*@
292788ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
292888ed4aceSMatthew G Knepley 
29298b1ab98fSJed Brown   Collective on DM
29308b1ab98fSJed Brown 
293188ed4aceSMatthew G Knepley   Input Parameter:
293288ed4aceSMatthew G Knepley . dm - The DM
293388ed4aceSMatthew G Knepley 
293488ed4aceSMatthew G Knepley   Output Parameter:
293588ed4aceSMatthew G Knepley . section - The PetscSection
293688ed4aceSMatthew G Knepley 
293788ed4aceSMatthew G Knepley   Level: intermediate
293888ed4aceSMatthew G Knepley 
293988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
294088ed4aceSMatthew G Knepley 
294188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
294288ed4aceSMatthew G Knepley @*/
29430adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
29440adebc6cSBarry Smith {
294588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
294688ed4aceSMatthew G Knepley 
294788ed4aceSMatthew G Knepley   PetscFunctionBegin;
294888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
294988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
295088ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
295182f516ccSBarry 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");
2952b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2953cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
2954ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm),dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
295588ed4aceSMatthew G Knepley   }
295688ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
295788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
295888ed4aceSMatthew G Knepley }
295988ed4aceSMatthew G Knepley 
296088ed4aceSMatthew G Knepley #undef __FUNCT__
2961b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2962b21d0597SMatthew G Knepley /*@
2963b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2964b21d0597SMatthew G Knepley 
2965b21d0597SMatthew G Knepley   Input Parameters:
2966b21d0597SMatthew G Knepley + dm - The DM
29675080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2968b21d0597SMatthew G Knepley 
2969b21d0597SMatthew G Knepley   Level: intermediate
2970b21d0597SMatthew G Knepley 
2971b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2972b21d0597SMatthew G Knepley 
2973b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2974b21d0597SMatthew G Knepley @*/
29750adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
29760adebc6cSBarry Smith {
2977b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2978b21d0597SMatthew G Knepley 
2979b21d0597SMatthew G Knepley   PetscFunctionBegin;
2980b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29815080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
29821d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2983b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2984b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2985b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2986b21d0597SMatthew G Knepley }
2987b21d0597SMatthew G Knepley 
2988b21d0597SMatthew G Knepley #undef __FUNCT__
298988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
299088ed4aceSMatthew G Knepley /*@
299188ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
299288ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
299388ed4aceSMatthew G Knepley 
299488ed4aceSMatthew G Knepley   Input Parameter:
299588ed4aceSMatthew G Knepley . dm - The DM
299688ed4aceSMatthew G Knepley 
299788ed4aceSMatthew G Knepley   Output Parameter:
299888ed4aceSMatthew G Knepley . sf - The PetscSF
299988ed4aceSMatthew G Knepley 
300088ed4aceSMatthew G Knepley   Level: intermediate
300188ed4aceSMatthew G Knepley 
300288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
300388ed4aceSMatthew G Knepley 
300488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
300588ed4aceSMatthew G Knepley @*/
30060adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
30070adebc6cSBarry Smith {
300888ed4aceSMatthew G Knepley   PetscInt       nroots;
300988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
301088ed4aceSMatthew G Knepley 
301188ed4aceSMatthew G Knepley   PetscFunctionBegin;
301288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
301388ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
30140298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
301588ed4aceSMatthew G Knepley   if (nroots < 0) {
301688ed4aceSMatthew G Knepley     PetscSection section, gSection;
301788ed4aceSMatthew G Knepley 
301888ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
301931ea6d37SMatthew G Knepley     if (section) {
302088ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
302188ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
302231ea6d37SMatthew G Knepley     } else {
30230298fd71SBarry Smith       *sf = NULL;
302431ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
302531ea6d37SMatthew G Knepley     }
302688ed4aceSMatthew G Knepley   }
302788ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
302888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
302988ed4aceSMatthew G Knepley }
303088ed4aceSMatthew G Knepley 
303188ed4aceSMatthew G Knepley #undef __FUNCT__
303288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
303388ed4aceSMatthew G Knepley /*@
303488ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
303588ed4aceSMatthew G Knepley 
303688ed4aceSMatthew G Knepley   Input Parameters:
303788ed4aceSMatthew G Knepley + dm - The DM
303888ed4aceSMatthew G Knepley - sf - The PetscSF
303988ed4aceSMatthew G Knepley 
304088ed4aceSMatthew G Knepley   Level: intermediate
304188ed4aceSMatthew G Knepley 
304288ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
304388ed4aceSMatthew G Knepley 
304488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
304588ed4aceSMatthew G Knepley @*/
30460adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
30470adebc6cSBarry Smith {
304888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
304988ed4aceSMatthew G Knepley 
305088ed4aceSMatthew G Knepley   PetscFunctionBegin;
305188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
305288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
305388ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
305488ed4aceSMatthew G Knepley   dm->defaultSF = sf;
305588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
305688ed4aceSMatthew G Knepley }
305788ed4aceSMatthew G Knepley 
305888ed4aceSMatthew G Knepley #undef __FUNCT__
305988ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
306088ed4aceSMatthew G Knepley /*@C
306188ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
306288ed4aceSMatthew G Knepley   describing the data layout.
306388ed4aceSMatthew G Knepley 
306488ed4aceSMatthew G Knepley   Input Parameters:
306588ed4aceSMatthew G Knepley + dm - The DM
306688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
306788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
306888ed4aceSMatthew G Knepley 
306988ed4aceSMatthew G Knepley   Level: intermediate
307088ed4aceSMatthew G Knepley 
307188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
307288ed4aceSMatthew G Knepley @*/
307388ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
307488ed4aceSMatthew G Knepley {
307582f516ccSBarry Smith   MPI_Comm       comm;
307688ed4aceSMatthew G Knepley   PetscLayout    layout;
307788ed4aceSMatthew G Knepley   const PetscInt *ranges;
307888ed4aceSMatthew G Knepley   PetscInt       *local;
307988ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3080ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
308188ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
308288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
308388ed4aceSMatthew G Knepley 
308488ed4aceSMatthew G Knepley   PetscFunctionBegin;
308582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
308688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
308788ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
308888ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
308988ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
309088ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
309188ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
309288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
309388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
309488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
309588ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3096ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
30976636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
309888ed4aceSMatthew G Knepley 
30996636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
31006636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
31016636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
310288ed4aceSMatthew G Knepley   }
310388ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
310488ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
310588ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
31061f588964SMatthew G Knepley     const PetscInt *cind;
31076636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
310888ed4aceSMatthew G Knepley 
310988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
311088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
311188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
311288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
311388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
31146636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
311588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
31166636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
31176636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
31186636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3119057b4bcdSMatthew 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);
31206636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
31216636e97aSMatthew G Knepley     }
312288ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
312388ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
312488ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
312588ed4aceSMatthew G Knepley     }
312688ed4aceSMatthew G Knepley     if (gdof < 0) {
31276636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
312888ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
312988ed4aceSMatthew G Knepley 
313031d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
313131d3f06eSJed Brown         if (r < 0) r = -(r+2);
313288ed4aceSMatthew G Knepley         remote[l].rank  = r;
313388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
313488ed4aceSMatthew G Knepley       }
313588ed4aceSMatthew G Knepley     } else {
31366636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
313788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
313888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
313988ed4aceSMatthew G Knepley       }
314088ed4aceSMatthew G Knepley     }
314188ed4aceSMatthew G Knepley   }
31426636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
314388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
314488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
314588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
314688ed4aceSMatthew G Knepley }
3147af122d2aSMatthew G Knepley 
3148af122d2aSMatthew G Knepley #undef __FUNCT__
3149b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3150b21d0597SMatthew G Knepley /*@
3151b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3152b21d0597SMatthew G Knepley 
3153b21d0597SMatthew G Knepley   Input Parameter:
3154b21d0597SMatthew G Knepley . dm - The DM
3155b21d0597SMatthew G Knepley 
3156b21d0597SMatthew G Knepley   Output Parameter:
3157b21d0597SMatthew G Knepley . sf - The PetscSF
3158b21d0597SMatthew G Knepley 
3159b21d0597SMatthew G Knepley   Level: intermediate
3160b21d0597SMatthew G Knepley 
3161b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3162b21d0597SMatthew G Knepley 
3163057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3164b21d0597SMatthew G Knepley @*/
31650adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
31660adebc6cSBarry Smith {
3167b21d0597SMatthew G Knepley   PetscFunctionBegin;
3168b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3169b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3170b21d0597SMatthew G Knepley   *sf = dm->sf;
3171b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3172b21d0597SMatthew G Knepley }
3173b21d0597SMatthew G Knepley 
3174b21d0597SMatthew G Knepley #undef __FUNCT__
3175057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3176057b4bcdSMatthew G Knepley /*@
3177057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3178057b4bcdSMatthew G Knepley 
3179057b4bcdSMatthew G Knepley   Input Parameters:
3180057b4bcdSMatthew G Knepley + dm - The DM
3181057b4bcdSMatthew G Knepley - sf - The PetscSF
3182057b4bcdSMatthew G Knepley 
3183057b4bcdSMatthew G Knepley   Level: intermediate
3184057b4bcdSMatthew G Knepley 
3185057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3186057b4bcdSMatthew G Knepley @*/
31870adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
31880adebc6cSBarry Smith {
3189057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3190057b4bcdSMatthew G Knepley 
3191057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3192057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3193057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3194057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3195057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3196057b4bcdSMatthew G Knepley   dm->sf = sf;
3197057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3198057b4bcdSMatthew G Knepley }
3199057b4bcdSMatthew G Knepley 
3200057b4bcdSMatthew G Knepley #undef __FUNCT__
3201af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3202af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3203af122d2aSMatthew G Knepley {
3204af122d2aSMatthew G Knepley   PetscFunctionBegin;
3205af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3206af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3207af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3208af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3209af122d2aSMatthew G Knepley }
3210af122d2aSMatthew G Knepley 
3211af122d2aSMatthew G Knepley #undef __FUNCT__
3212af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3213af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3214af122d2aSMatthew G Knepley {
3215af122d2aSMatthew G Knepley   PetscInt       f;
3216af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3217af122d2aSMatthew G Knepley 
3218af122d2aSMatthew G Knepley   PetscFunctionBegin;
3219af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3220af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3221af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3222af122d2aSMatthew G Knepley   }
3223af122d2aSMatthew G Knepley   ierr          = PetscFree(dm->fields);CHKERRQ(ierr);
3224af122d2aSMatthew G Knepley   dm->numFields = numFields;
3225af122d2aSMatthew G Knepley   ierr          = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3226af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
322782f516ccSBarry Smith     ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm), (PetscContainer*) &dm->fields[f]);CHKERRQ(ierr);
3228af122d2aSMatthew G Knepley   }
3229af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3230af122d2aSMatthew G Knepley }
3231af122d2aSMatthew G Knepley 
3232af122d2aSMatthew G Knepley #undef __FUNCT__
3233af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3234af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3235af122d2aSMatthew G Knepley {
3236af122d2aSMatthew G Knepley   PetscFunctionBegin;
3237af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3238af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
323982f516ccSBarry Smith   if (!dm->fields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
324082f516ccSBarry 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);
3241af122d2aSMatthew G Knepley   *field = dm->fields[f];
3242af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3243af122d2aSMatthew G Knepley }
32446636e97aSMatthew G Knepley 
32456636e97aSMatthew G Knepley #undef __FUNCT__
3246b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3247b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3248b64e0483SPeter Brune {
3249b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3250b64e0483SPeter Brune   PetscErrorCode ierr;
3251b64e0483SPeter Brune   Vec coords,ccoords;
3252b64e0483SPeter Brune   VecScatter scat;
3253b64e0483SPeter Brune   PetscFunctionBegin;
3254b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3255b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3256b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3257b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3258b64e0483SPeter Brune   if (coords && !ccoords) {
3259b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
3260b64e0483SPeter Brune     ierr = DMCreateInjection(dmc_coord,dm_coord,&scat);CHKERRQ(ierr);
3261b64e0483SPeter Brune     ierr = VecScatterBegin(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3262b64e0483SPeter Brune     ierr = VecScatterEnd(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3263b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3264b64e0483SPeter Brune     ierr = VecScatterDestroy(&scat);CHKERRQ(ierr);
3265b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3266b64e0483SPeter Brune   }
3267b64e0483SPeter Brune   PetscFunctionReturn(0);
3268b64e0483SPeter Brune }
3269b64e0483SPeter Brune 
3270b64e0483SPeter Brune #undef __FUNCT__
327103dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
327203dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
327303dadc2fSPeter Brune {
327403dadc2fSPeter Brune   DM dm_coord,subdm_coord;
327503dadc2fSPeter Brune   PetscErrorCode ierr;
327603dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
327703dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
327803dadc2fSPeter Brune   PetscFunctionBegin;
327903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
328003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
328103dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
328203dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
328303dadc2fSPeter Brune   if (coords && !ccoords) {
328403dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
328503dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
328603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
328703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
328803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
328903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
329003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
329103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
329203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
329303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
329403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
329503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
329603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
329703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
329803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
329903dadc2fSPeter Brune   }
330003dadc2fSPeter Brune   PetscFunctionReturn(0);
330103dadc2fSPeter Brune }
330203dadc2fSPeter Brune 
330303dadc2fSPeter Brune #undef __FUNCT__
33046636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
33056636e97aSMatthew G Knepley /*@
33066636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
33076636e97aSMatthew G Knepley 
33086636e97aSMatthew G Knepley   Collective on DM
33096636e97aSMatthew G Knepley 
33106636e97aSMatthew G Knepley   Input Parameters:
33116636e97aSMatthew G Knepley + dm - the DM
33126636e97aSMatthew G Knepley - c - coordinate vector
33136636e97aSMatthew G Knepley 
33146636e97aSMatthew G Knepley   Note:
33156636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
33166636e97aSMatthew G Knepley 
33176636e97aSMatthew G Knepley   Level: intermediate
33186636e97aSMatthew G Knepley 
33196636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33206636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
33216636e97aSMatthew G Knepley @*/
33226636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
33236636e97aSMatthew G Knepley {
33246636e97aSMatthew G Knepley   PetscErrorCode ierr;
33256636e97aSMatthew G Knepley 
33266636e97aSMatthew G Knepley   PetscFunctionBegin;
33276636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
33296636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
33306636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
33316636e97aSMatthew G Knepley   dm->coordinates = c;
33326636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3333b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
333403dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
33356636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33366636e97aSMatthew G Knepley }
33376636e97aSMatthew G Knepley 
33386636e97aSMatthew G Knepley #undef __FUNCT__
33396636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
33406636e97aSMatthew G Knepley /*@
33416636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
33426636e97aSMatthew G Knepley 
33436636e97aSMatthew G Knepley   Collective on DM
33446636e97aSMatthew G Knepley 
33456636e97aSMatthew G Knepley    Input Parameters:
33466636e97aSMatthew G Knepley +  dm - the DM
33476636e97aSMatthew G Knepley -  c - coordinate vector
33486636e97aSMatthew G Knepley 
33496636e97aSMatthew G Knepley   Note:
33506636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
33516636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
33526636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
33536636e97aSMatthew G Knepley 
33546636e97aSMatthew G Knepley   Level: intermediate
33556636e97aSMatthew G Knepley 
33566636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33576636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
33586636e97aSMatthew G Knepley @*/
33596636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
33606636e97aSMatthew G Knepley {
33616636e97aSMatthew G Knepley   PetscErrorCode ierr;
33626636e97aSMatthew G Knepley 
33636636e97aSMatthew G Knepley   PetscFunctionBegin;
33646636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
33666636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
33676636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
33688865f1eaSKarl Rupp 
33696636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
33708865f1eaSKarl Rupp 
33716636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
33726636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33736636e97aSMatthew G Knepley }
33746636e97aSMatthew G Knepley 
33756636e97aSMatthew G Knepley #undef __FUNCT__
33766636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
33776636e97aSMatthew G Knepley /*@
33786636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
33796636e97aSMatthew G Knepley 
33806636e97aSMatthew G Knepley   Not Collective
33816636e97aSMatthew G Knepley 
33826636e97aSMatthew G Knepley   Input Parameter:
33836636e97aSMatthew G Knepley . dm - the DM
33846636e97aSMatthew G Knepley 
33856636e97aSMatthew G Knepley   Output Parameter:
33866636e97aSMatthew G Knepley . c - global coordinate vector
33876636e97aSMatthew G Knepley 
33886636e97aSMatthew G Knepley   Note:
33896636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
33906636e97aSMatthew G Knepley 
33916636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
33926636e97aSMatthew G Knepley 
33936636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
33946636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
33956636e97aSMatthew G Knepley 
33966636e97aSMatthew G Knepley   Level: intermediate
33976636e97aSMatthew G Knepley 
33986636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33996636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
34006636e97aSMatthew G Knepley @*/
34016636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
34026636e97aSMatthew G Knepley {
34036636e97aSMatthew G Knepley   PetscErrorCode ierr;
34046636e97aSMatthew G Knepley 
34056636e97aSMatthew G Knepley   PetscFunctionBegin;
34066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34076636e97aSMatthew G Knepley   PetscValidPointer(c,2);
34081f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
34090298fd71SBarry Smith     DM cdm = NULL;
34106636e97aSMatthew G Knepley 
34116636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
34126636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
34136636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
34146636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
34156636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
34166636e97aSMatthew G Knepley   }
34176636e97aSMatthew G Knepley   *c = dm->coordinates;
34186636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34196636e97aSMatthew G Knepley }
34206636e97aSMatthew G Knepley 
34216636e97aSMatthew G Knepley #undef __FUNCT__
34226636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
34236636e97aSMatthew G Knepley /*@
34246636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
34256636e97aSMatthew G Knepley 
34266636e97aSMatthew G Knepley   Collective on DM
34276636e97aSMatthew G Knepley 
34286636e97aSMatthew G Knepley   Input Parameter:
34296636e97aSMatthew G Knepley . dm - the DM
34306636e97aSMatthew G Knepley 
34316636e97aSMatthew G Knepley   Output Parameter:
34326636e97aSMatthew G Knepley . c - coordinate vector
34336636e97aSMatthew G Knepley 
34346636e97aSMatthew G Knepley   Note:
34356636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
34366636e97aSMatthew G Knepley 
34376636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
34386636e97aSMatthew G Knepley 
34396636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
34406636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
34416636e97aSMatthew G Knepley 
34426636e97aSMatthew G Knepley   Level: intermediate
34436636e97aSMatthew G Knepley 
34446636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
34456636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
34466636e97aSMatthew G Knepley @*/
34476636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
34486636e97aSMatthew G Knepley {
34496636e97aSMatthew G Knepley   PetscErrorCode ierr;
34506636e97aSMatthew G Knepley 
34516636e97aSMatthew G Knepley   PetscFunctionBegin;
34526636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34536636e97aSMatthew G Knepley   PetscValidPointer(c,2);
34541f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
34550298fd71SBarry Smith     DM cdm = NULL;
34566636e97aSMatthew G Knepley 
34576636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
34586636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
34596636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
34606636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
34616636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
34626636e97aSMatthew G Knepley   }
34636636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
34646636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34656636e97aSMatthew G Knepley }
34666636e97aSMatthew G Knepley 
34676636e97aSMatthew G Knepley #undef __FUNCT__
34686636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
34696636e97aSMatthew G Knepley /*@
34706636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
34716636e97aSMatthew G Knepley 
34726636e97aSMatthew G Knepley   Collective on DM
34736636e97aSMatthew G Knepley 
34746636e97aSMatthew G Knepley   Input Parameter:
34756636e97aSMatthew G Knepley . dm - the DM
34766636e97aSMatthew G Knepley 
34776636e97aSMatthew G Knepley   Output Parameter:
34786636e97aSMatthew G Knepley . cdm - coordinate DM
34796636e97aSMatthew G Knepley 
34806636e97aSMatthew G Knepley   Level: intermediate
34816636e97aSMatthew G Knepley 
34826636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
34836636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
34846636e97aSMatthew G Knepley @*/
34856636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
34866636e97aSMatthew G Knepley {
34876636e97aSMatthew G Knepley   PetscErrorCode ierr;
34886636e97aSMatthew G Knepley 
34896636e97aSMatthew G Knepley   PetscFunctionBegin;
34906636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34916636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
34926636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
349382f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
34946636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
34956636e97aSMatthew G Knepley   }
34966636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
34976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34986636e97aSMatthew G Knepley }
3499e87bb0d3SMatthew G Knepley 
3500e87bb0d3SMatthew G Knepley #undef __FUNCT__
3501e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3502e87bb0d3SMatthew G Knepley /*@
3503e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3504e87bb0d3SMatthew G Knepley 
3505e87bb0d3SMatthew G Knepley   Not collective
3506e87bb0d3SMatthew G Knepley 
3507e87bb0d3SMatthew G Knepley   Input Parameters:
3508e87bb0d3SMatthew G Knepley + dm - The DM
3509e87bb0d3SMatthew G Knepley - v - The Vec of points
3510e87bb0d3SMatthew G Knepley 
351161e3bb9bSMatthew G Knepley   Output Parameter:
3512e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3513e87bb0d3SMatthew G Knepley 
3514e87bb0d3SMatthew G Knepley   Level: developer
351561e3bb9bSMatthew G Knepley 
351661e3bb9bSMatthew G Knepley .keywords: point location, mesh
351761e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
351861e3bb9bSMatthew G Knepley @*/
3519e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3520e87bb0d3SMatthew G Knepley {
3521735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3522735aa83eSMatthew G Knepley 
3523e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3524e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3525e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3526e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3527735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3528735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
352982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3530e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3531e87bb0d3SMatthew G Knepley }
3532